Search code examples
arduinoavrled

Write data to SSD1306 via I2C


I'm using an SSD1306 OLED and have a question about it.

When writing data to its buffer via I2C, some libraries write 16 bytes every time.

For example:

void SSD1306::sendFramebuffer(const uint8_t *buffer) {
  // Set Column Address (0x00 - 0x7F)
  sendCommand(SSD1306_COLUMNADDR);
  sendCommand(0x00);
  sendCommand(0x7F);
  // Set Page Address (0x00 - 0x07)
  sendCommand(SSD1306_PAGEADDR);
  sendCommand(0x00);
  sendCommand(0x07);
  for (uint16_t i = 0;i < SSD1306_BUFFERSIZE;) {
    i2c.start();
    i2c.write(0x40);
    for (uint8_t j = 0;j < 16; ++j, ++i) {
      i2c.write(buffer[i]);
    }
    i2c.stop();
  }
}

Why don't they write 1024 bytes directly?


Solution

  • Most of the I2C libraries I've seen source code for, including that for the Aruduino, chunk the data in this fashion. While the I2C standard doesn't require this, as other poster mentioned, there may be buffer considerations. The .stop() command here might signal the device to process the 16 bytes just sent and prepare for more.

    Invariably, you need to read the datasheet for your device and understand what it expects in order to display properly. They say "RTFM" in software, but hardware is at least as unforgiving. You must read and follow the datasheet when interfacing with external hardware devices.