What's the proper way to deal with multiple devices sharing an i2c bus?
Posted: Tue Jun 05, 2018 6:26 pm
I built an example project to use u8g2 via esp-idf on a lolin32 board that has an onboard SSD1306 OLED display hardwired via i2c using pins 4 and 5. As part of the setup, it uses Neil Kolban's u8g2_esp32_hal example code to initialize u8g2.
The project can be seen at https://github.com/jskubick/u8g2-exampl ... -sda5-scl4
Now, I want to add code to use additional devices sharing the same i2c bus as the display, and I'm not really sure how to proceed...
The project can be seen at https://github.com/jskubick/u8g2-exampl ... -sda5-scl4
Now, I want to add code to use additional devices sharing the same i2c bus as the display, and I'm not really sure how to proceed...
- rewrite u8g2_esp32_hal.c to turn it into a more general-purpose class for encapsulating an i2c_cmd_handle_t object, treat it like a Singleton, and use it to communicate with EVERYTHING that's on the pin 4/5 i2c bus?
- slightly refactor u8g2_esp32_hal.c to turn it into a more general-purpose class for encapsulating an i2c_cmd_handle_t object, but instantiate a separate object for every device on the bus. And maybe come up with some locking mechanism to ensure that only a single thread at a time can touch the i2c bus, and ensure that communication sequences don't get interrupted (say, if a thread that reads a SHT21 temperature/humidity sensor decides to run at the exact same instant another thread is updating the SSD1306 display).
- Some other strategy?