bool SSD1306_Init()

in BalancingRobot/Software/HighLevelApp/i2c_oled.c [35:166]


bool SSD1306_Init(bool useVerticalDisplay)
{
    _useVerticalDisplay = useVerticalDisplay;

    if (useVerticalDisplay)
    {
        DisplayWidth = SSD1306_TALL_LCDWIDTH;
        DisplayHeight = SSD1306_TALL_LCDHEIGHT;
    }
    else
    {
        DisplayWidth = SSD1306_WIDE_LCDWIDTH;
        DisplayHeight = SSD1306_WIDE_LCDHEIGHT;
    }

    if (_i2cfd > -1)
    {
        return true;
    }

    _i2cfd = I2CMaster_Open(MT3620_I2C_ISU2);
    if (_i2cfd == -1)
    {
        return false;
    }

    int result = I2CMaster_SetBusSpeed(_i2cfd, I2C_BUS_SPEED_FAST);
    if (result != 0)
    {
        return false;
    }

    result = I2CMaster_SetTimeout(_i2cfd, 100);
    if (result != 0) {
        return false;
    }

    result = I2CMaster_SetDefaultTargetAddress(_i2cfd, oledDisplayAddress);
    if (result != 0) {
        Log_Debug("I2CMaster_SetDefaultTargetAddress (1): errno=%d (%s)\n", errno, strerror(errno));
        return false;
    }

    uint8_t data = 0x00;	// I2C OLED Display 'turn off'
    I2C_DeviceAddress _devAddr = 0x00;
    bool deviceFound = false;

    for (int x = 0; x <= 0x7f; x++)
    {
        _devAddr = x;
        ssize_t ret = I2CMaster_Write(_i2cfd, _devAddr, &data, 1);
        if (ret != -1)
        {
            Log_Debug("Found address: 0x%02x\n", x);
            deviceFound = true;
        }
    }

    if (!deviceFound)
    {
        Log_Debug("Didn't find an I2C devices\n");
        return false;
    }

    if (!_useVerticalDisplay)
    {
        // 128x64
        ssd1306_command(0xAE); // display off
        ssd1306_command(0xD5); // clock
        ssd1306_command(0x81); // upper nibble is rate, lower nibble is divisor
        ssd1306_command(0xA8); // mux ratio
        ssd1306_command(0x3F); // rtfm
        ssd1306_command(0xD3); // display offset
        ssd1306_command(0x00); // rtfm
        ssd1306_command(0x00);
        ssd1306_command(0x8D); // charge pump
        ssd1306_command(0x14); // enable
        ssd1306_command(0x20); // memory addr mode
        ssd1306_command(0x00); // horizontal
        ssd1306_command(0xA1); // segment remap
        ssd1306_command(0xA5); // display on
        ssd1306_command(0xC8); // com scan direction
        ssd1306_command(0xDA); // com hardware cfg
        ssd1306_command(0x12); // alt com cfg
        ssd1306_command(0x81); // contrast aka current
        ssd1306_command(0x7F); // 128 is midpoint
        ssd1306_command(0xD9); // precharge
        ssd1306_command(0x11); // rtfm
        ssd1306_command(0xDB); // vcomh deselect level
        ssd1306_command(0x20); // rtfm
        ssd1306_command(0xA6); // non-inverted
        ssd1306_command(0xA4); // display scan on
        ssd1306_command(0x2e);
        ssd1306_command(0xAF); // drivers on
    }
    else
    {
        // 128x32
        ssd1306_command(0xAE); // display off
        ssd1306_command(0xD5); // clock
        ssd1306_command(0x81); // upper nibble is rate, lower nibble is divisor
        ssd1306_command(0xA8); // mux ratio
        ssd1306_command(0x1F); // rtfm
        ssd1306_command(0xD3); // display offset
        ssd1306_command(0x00); // rtfm
        ssd1306_command(0x00);
        ssd1306_command(0x8D); // charge pump
        ssd1306_command(0x14); // enable
        ssd1306_command(0x20); // memory addr mode
        ssd1306_command(0x00); // horizontal
        ssd1306_command(0xA1); // segment remap
        ssd1306_command(0xA5); // display on
        ssd1306_command(0xC8); // com scan direction
        ssd1306_command(0xDA); // com hardware cfg
        ssd1306_command(0x02); // alt com cfg
        ssd1306_command(0x81); // contrast aka current
        ssd1306_command(0x7F); // 128 is midpoint
        ssd1306_command(0xD9); // precharge
        ssd1306_command(0x11); // rtfm
        ssd1306_command(0xDB); // vcomh deselect level
        ssd1306_command(0x20); // rtfm
        ssd1306_command(0xA6); // non-inverted
        ssd1306_command(0xA4); // display scan on
        ssd1306_command(0xAF); // drivers on
    }

    SSD1306_Clear();
    SSD1306_Display();
    

    return true;
}