Files
charge-controller-firmware/lib/Adafruit_GFX/OledSSD1306.cpp
T
Martin Jaeger 117455390b OLED library refactoring to prepare Zephyr integration
- Made Adafruit GFX library independent of mbed Stream class
- Major cleanup/rewrite of SSD1306 specific class
- Dynamically allocated std::vector display buffer replaced with statically
  allocated std::array (leads to more calculated RAM usage only because
  previous heap usage is not considered in memory summary)
- Commands for brightness control and to prevent shifted display now
  integrated in SSD1306 class
2019-12-20 10:44:53 +01:00

166 lines
4.3 KiB
C++

/*
* SPDX-License-Identifier: BSD-2-Clause
*
* Copyright (c) 2012 Adafruit Industries. All rights reserved.
*
* This class is partly based on the library developed by Adafruit and modified by Neal Horman
* 7/14/2012 for use in mbed.
*
* Significant modifications were done by Martin Jäger (11/2019) to use it also in Zephyr
*/
#include "mbed.h"
#include "OledSSD1306.h"
#include "logo.h"
#define SSD1306_SETCONTRAST 0x81
#define SSD1306_DISPLAYALLON_RESUME 0xA4
#define SSD1306_DISPLAYALLON 0xA5
#define SSD1306_NORMALDISPLAY 0xA6
#define SSD1306_INVERTDISPLAY 0xA7
#define SSD1306_DISPLAYOFF 0xAE
#define SSD1306_DISPLAYON 0xAF
#define SSD1306_SETDISPLAYOFFSET 0xD3
#define SSD1306_SETCOMPINS 0xDA
#define SSD1306_SETVCOMDETECT 0xDB
#define SSD1306_SETDISPLAYCLOCKDIV 0xD5
#define SSD1306_SETPRECHARGE 0xD9
#define SSD1306_SETMULTIPLEX 0xA8
#define SSD1306_SETLOWCOLUMN 0x00
#define SSD1306_SETHIGHCOLUMN 0x10
#define SSD1306_SETSTARTLINE 0x40
#define SSD1306_MEMORYMODE 0x20
#define SSD1306_COMSCANINC 0xC0
#define SSD1306_COMSCANDEC 0xC8
#define SSD1306_SEGREMAP 0xA0
#define SSD1306_CHARGEPUMP 0x8D
OledSSD1306::OledSSD1306(I2C &i2c, uint8_t i2cAddress, uint8_t brightness) :
Adafruit_GFX(SSD1306_WIDTH, SSD1306_HEIGHT),
mi2c(i2c),
mi2cAddress(i2cAddress)
{
command(SSD1306_DISPLAYOFF);
command(SSD1306_SETDISPLAYCLOCKDIV);
command(0x80); // the suggested ratio 0x80
command(SSD1306_SETMULTIPLEX);
command(_rawHeight-1);
command(SSD1306_SETDISPLAYOFFSET);
command(0x0); // no offset
command(SSD1306_SETSTARTLINE | 0x0); // line #0
command(SSD1306_CHARGEPUMP);
command((SSD1306_SWITCHCAPVCC == SSD1306_EXTERNALVCC) ? 0x10 : 0x14);
command(SSD1306_MEMORYMODE);
command(0x00); // 0x0 act like ks0108
command(SSD1306_SEGREMAP | 0x1);
command(SSD1306_COMSCANDEC);
command(SSD1306_SETCOMPINS);
command(_rawHeight == 32 ? 0x02 : 0x12); // TODO - calculate based on _rawHieght ?
command(SSD1306_SETCONTRAST);
command(_rawHeight == 32 ? 0x8F : ((SSD1306_SWITCHCAPVCC == SSD1306_EXTERNALVCC) ? 0x9F : 0xCF) );
command(SSD1306_SETPRECHARGE);
command((SSD1306_SWITCHCAPVCC == SSD1306_EXTERNALVCC) ? 0x22 : 0xF1);
command(SSD1306_SETVCOMDETECT);
command(0x40);
command(SSD1306_DISPLAYALLON_RESUME);
command(SSD1306_NORMALDISPLAY);
command(SSD1306_DISPLAYON);
// make sure that display has not shifted even without power on reset
command(0x22);
command(0x00);
command(0x07);
command(SSD1306_SETCONTRAST);
command(brightness);
splash();
display();
}
void OledSSD1306::drawPixel(int16_t x, int16_t y, uint16_t color)
{
if ((x < 0) || (x >= width()) || (y < 0) || (y >= height()))
return;
switch (getRotation())
{
case 1:
swap(x, y);
x = _rawWidth - x - 1;
break;
case 2:
x = _rawWidth - x - 1;
y = _rawHeight - y - 1;
break;
case 3:
swap(x, y);
y = _rawHeight - y - 1;
break;
}
// x is which column
if (color == WHITE) {
buffer[x + (y/8)*_rawWidth] |= _BV((y % 8));
}
else {
buffer[x + (y/8)*_rawWidth] &= ~_BV((y % 8));
}
}
void OledSSD1306::invert(bool i)
{
command(i ? SSD1306_INVERTDISPLAY : SSD1306_NORMALDISPLAY);
}
// Send the display buffer out to the display
void OledSSD1306::display(void)
{
command(SSD1306_SETLOWCOLUMN | 0x0); // low col = 0
command(SSD1306_SETHIGHCOLUMN | 0x0); // hi col = 0
command(SSD1306_SETSTARTLINE | 0x0); // line #0
sendBuffer();
}
void OledSSD1306::clear(void)
{
std::fill(buffer.begin(), buffer.end(), 0);
}
void OledSSD1306::splash(void)
{
std::copy(&logo[0], &logo[0] + sizeof(logo), buffer.begin());
}
void OledSSD1306::sendBuffer()
{
char buf[17];
buf[0] = 0x40; // Data Mode
// send display buffer in 16 byte chunks
for (uint16_t i = 0; i < buffer.size(); i += 16)
{
for(uint8_t x = 1; x < sizeof(buf); x++) {
uint16_t pos = i + x - 1;
if (buffer.size() > pos) {
buf[x] = buffer[pos];
}
}
mi2c.write(mi2cAddress, buf, sizeof(buf));
}
};