$ show madmachineio/SwiftIO
SwiftIO is a Swift framework for microcontrollers that provides an easy-to-use interface for hardware peripherals. It abstracts low-level hardware details into Swift classes, allowing developers to interact with GPIO, ADC, PWM, timers, communication protocols (I2C, SPI, UART), and more without dealing with complex register manipulation. The library is structured into a C layer (CSwiftIO) for direct hardware access and a Swift layer that wraps this functionality in a safe, idiomatic API. It emphasizes error handling through Swift's Result type and provides consistent patterns for resource management and peripheral interaction.
The LLM identified 2 files worth highlighting for reuse:
Provides safe and ergonomic conversion of C error codes to Swift Result types, including functions like `valueOrErrno` and `nothingOrErrno` for converting return values, and `validateLength` for buffer length validation. This utility is used throughout the codebase to ensure consistent and safe error handling when interacting with C APIs, demonstrating a non-obvious pattern for robust Swift/C interop.
Wraps the Zephyr I2C driver in a Swift class that manages resource lifecycle (open in init, close in deinit), provides configuration for bus speed, and offers methods for reading and writing data with proper error handling via Result types. It demonstrates a clean pattern for wrapping hardware peripherals in Swift, isolating unsafe C code behind a safe, easy-to-use interface, and is self-contained enough to be extracted with minimal modification (requiring only the trivial ID type and the error handling utilities).