Skip to content

Conversation

@ralphweng2023
Copy link

Description

This PR adds a new I2C device management API for Raspberry Pi GPIO, following the existing pattern established by the ADC module. It provides a higher-level abstraction for communicating with I2C devices like LED matrices.

New Features

Core I2C Device API (raspi_i2c.h):

  • open_i2c_device() - Open an I2C device by name, bus, address, and type
  • i2c_write_byte(), i2c_write_register() - Write to I2C device
  • i2c_read_byte(), i2c_read_register() - Read from I2C device
  • close_i2c_device(), close_all_i2c_devices() - Close devices
  • has_i2c_device(), i2c_device_named() - Device registry lookups

HT16K33 LED Matrix Support:

  • ht16k33_init() - Initialize display (oscillator on, display on)
  • ht16k33_set_brightness() - Set brightness level (0-15)
  • ht16k33_clear() - Clear the display
  • ht16k33_draw_pattern() - Draw an 8x8 pattern
  • ht16k33_turn_off() - Turn off the display

Device Types:

  • I2C_DEVICE_GENERIC - Generic I2C device
  • I2C_DEVICE_HT16K33_8X8 - 8x8 LED Matrix (address 0x70)
  • I2C_DEVICE_HT16K33_14SEG - 14-segment display (address 0x70)
  • I2C_DEVICE_PCF8574 - I/O Expander (address 0x20)

Example Usage

#include "raspi_i2c.h"

void demo_led_matrix() {
    raspi_init();
    
    // Open the LED matrix
    i2c_device led = open_i2c_device("led_matrix", I2C_DEVICE_HT16K33_8X8);
    
    ht16k33_init(led);
    ht16k33_set_brightness(led, 8);
    
    // Draw a heart pattern
    unsigned char heart[] = {
        0b01100110, 0b11111111, 0b11111111, 0b11111111,
        0b01111110, 0b00111100, 0b00011000, 0b00000000
    };
    ht16k33_draw_pattern(led, heart);
    
    delay(2000);
    
    close_i2c_device(led);
    raspi_cleanup();
}

Type of change

  • New feature (non-breaking change which adds functionality)

How Has This Been Tested?

Built and tested on macOS using CMake:

cd projects/cmake
cmake --preset macOS
cmake --build build/

cd ../../bin
./skunit_tests "[i2c]"
# Result: All tests passed (20 assertions in 5 test cases)

./skunit_tests --order rand  
# Result: All tests passed (1283 assertions in 84 test cases)

Testing Checklist

  • Code compiles without errors on macOS
  • All existing unit tests pass
  • New I2C unit tests pass (5 test cases, 20 assertions)
  • Null pointer handling works correctly
  • Device registry functions work correctly
  • Tested on Raspberry Pi with HT16K33 (requires hardware)

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code in hard-to-understand areas
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective
  • New and existing unit tests pass locally with my changes

New files:
- raspi_i2c.h: Header with i2c_device type and functions
- raspi_i2c.cpp: Implementation following ADC pattern
- unit_test_raspi_i2c.cpp: Unit tests for I2C API

Core I2C functions:
- open_i2c_device(): Open I2C device by name, bus, address, type
- i2c_write_byte(), i2c_write_register(): Write to device
- i2c_read_byte(), i2c_read_register(): Read from device
- close_i2c_device(), close_all_i2c_devices(): Cleanup
- has_i2c_device(), i2c_device_named(): Registry lookups

HT16K33 LED Matrix helpers:
- ht16k33_init(): Initialize display
- ht16k33_set_brightness(): Set brightness 0-15
- ht16k33_clear(): Clear display
- ht16k33_draw_pattern(): Draw 8x8 pattern
- ht16k33_turn_off(): Turn off display

Also:
- Added I2C_DEVICE_PTR to backend_types.h
- Added i2c_device_type enum to types.h
- Unit tests verify registry functions and null pointer handling
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants