Fracanto Dependency Injection

Fracanto Dependency Injection #2 — The Five Peripheral Drivers

The Five Peripheral Drivers — same pattern, different hardware

All peripheral drivers in the framework implement DI following the same scheme, differing only in the injected operations that correspond to each hardware's specifics.

The ADS8866 ADC driver requires three functions: spi_transfer for reading measurements via SPI, cs_set for the chip select signal, and optionally mux_select for controlling an upstream analog multiplexer with multiple input channels. The associated context additionally contains calibration data (offset and gain per channel), which the driver uses to independently convert the raw value into a normalized int16 value and write it into the IO manager's input array.

The DAC8552 DAC driver is the counterpart: it injects spi_write (send instead of receive) and cs_set. Its inverse calibration converts the normalized value back into a DAC raw value. Both SPI drivers — ADC and DAC — follow the same pattern but with mirrored data direction.

The PCM5102A audio DAC driver operates at a different level: instead of individual SPI transfers, it injects i2s_start_dma and i2s_stop_dma for continuous DMA streaming. It implements the audio_ops_t vtable of the audio pipeline and thus forms the bridge between the DI pattern (hardware access) and the vtable pattern (framework integration).

The encoder driver gets by with a single injected function: gpio_read reads the state of a GPIO pin. On top of this, the driver builds quadrature decoding and button debouncing, feeding the results as events into the panel queue.

The SSD1306 OLED driver injects i2c_write_cmd and i2c_write_data separately, because the SSD1306 controller distinguishes between command and data bytes in the I2C protocol. As the only driver, it does not implement a framework vtable but instead offers a standalone API with framebuffer and integrated 5×7 font.