OLED128x64lite

OLED 128x64 SSD1306 or SH1106 on I2C. PB8=SCL PB9=SDA needs a single foactory/gpio/i2c/config object
Author: Smashed Transistors
License: LGPL
Github: tiar/HW/OLED128x64lite.axo

Inlets

charptr32 line1

Outlets

None

Attributes

objref scope

combo type

combo I2CADDR

Declaration
enum SSD1306 {
  LCDWIDTH = 128,
  LCDHEIGHT = 64,
  SETCONTRAST = 0x81, // 2byte cmd,256 contrast steps, reset = 7F
  DISPLAYON = 0xAF,   // entire display on
  DISPLAYOFF = 0xAE,  // entire display off
  DISPLAYALLON_RESUME = 0xA4,
  NORMALDISPLAY = 0xA6,     // invert = 0xA7
  DEACTIVATE_SCROLL = 0x2E, // stop control scroll conf by 26 27 29 2A
  MEMORYMODE =
      0x20, // 2byte cmd, 0 horizontal, 1 vertical, 2 page addressing, 3 invalid
  COLUMNADDR = 0x21,     // 3bytes, start, end (included) valid in horizontal or
                         // vertical mode
  PAGEADDR = 0x22,       // 3bytes, start, end (included) valid in horizontal or
                         // vertical mode
  SETSTARTLINE_0 = 0x40, // set display RAM start line at 0
  SEGREMAP = 0xA0,       // segment remap 0 mapped to SEG0
  SETMULTIPLEX = 0xA8,   // 2 byte cmd, set mux ratio
  COMSCANDEC = 0xC8, // scan from COM[N-1] to COM[0] (0xC0 is COM0 to COM[N-1])
  SETDISPLAYOFFSET = 0xD3,   // 2 byte cmd, vertical shift
  SETCOMPINS = 0xDA,         // 2 byte cmd, seq com pin conf, left right remap
  SETDISPLAYCLOCKDIV = 0xD5, // 2 byte cmd, low nibble A[3:0]+1 = div ratio,
                             // high nibble A[7:4] freq, reset 1000b
  SETPRECHARGE =
      0xD9, // 2 byte cmd, precharge period A[3:0] phase1 A[7:4] phase2, reset:2
  SETVCOMDETECT = 0xDB, // 2 byte Vcomh deselect level A[6:4] 000b 0.65xVcc 010b
                        // 0.77(reset) 011b 0.83
  CHARGEPUMP = 0x8D,    // Enable charge pump seq: 0x8D, 0x14, 0xAF (Charge pump
                        // setting, enable charge pump, display on)
  EXTERNALVCC = 0x1,
  SWITCHCAPVCC = 0x2
};
uint8_t const nibbleToByte[16] = {
    0b00000000, 0b00000011, 0b00001100, 0b00001111, 0b00110000, 0b00110011,
    0b00111100, 0b00111111, 0b11000000, 0b11000011, 0b11001100, 0b11001111,
    0b11110000, 0b11110011, 0b11111100, 0b11111111};

uint8_t *txbuf;
uint8_t *rxbuf;

uint8_t text[11];    // text input
uint8_t textBuf[11]; // text input copy (to avoid flicker)
uint8_t tY[128];     // scope input copied and rescaled
bool disable;
// SETUP
// ------------------
void cmd(uint8_t c) {
  txbuf[0] = 0;
  txbuf[1] = c;
  i2cMasterTransmitTimeout(&I2CD1, attr_I2CADDR, txbuf, 2, rxbuf, 0, 30);
}
void cmd(uint8_t c1, uint8_t c2) {
  cmd(c1);
  cmd(c2);
}
void cmd(uint8_t c1, uint8_t c2, uint8_t c3) {
  cmd(c1, c2);
  cmd(c3);
}

// _____________________________________________________________________
void fill(uint8_t v) {
  i2cAcquireBus(&I2CD1);
  cmd(COLUMNADDR, 0, 127); // Column start end
  cmd(PAGEADDR, 0, 7);     // Page start end
  txbuf[0] = 0x40;
  for (int i = 1; i < 129; i++)
    txbuf[i] = v;
  for (int p = 0; p < 8; p++) {
    i2cMasterTransmitTimeout(&I2CD1, attr_I2CADDR, txbuf, 129, rxbuf, 0, 30);
  }
  i2cReleaseBus(&I2CD1);
}

/* returns i
 */
int drawTxt(int i, int NBC, uint8_t *tb, int page, uint8_t *tPage) {
  for (int nc = 0; nc < NBC; nc++) {
    int ascii_32 = tb[nc] - ' ';
    const uint8_t *adChar = tiar_font5x8 + ascii_32 * 5;
    for (int slice = 0; slice < 5; slice++) { // slices are two pixel wide
      uint8_t s;
      if ((page & 1) == 0) {
        s = nibbleToByte[adChar[slice] & 15]; // low nibble
      } else {
        s = nibbleToByte[(adChar[slice] >> 4) & 15]; // high nibble
      }
      tPage[i] = s;
      i++; // two pixel wide
      tPage[i] = s;
      i++;
    }
    tPage[i] = 0;
    i++; // separator space 1 pixel wide => 11 pixels per char
  }
  return i;
}
// _____________________________________________________________________
// scaled x2 text
// returns a page to be sent to the SSD1306 based on contents of text
void calcTextPage(int page, uint8_t tPage[128]) {
  int i = 0;
  int tLine = page / 2;
  uint8_t *tb = textBuf + 11 * tLine;
  if (tb[0] >= ' ') { // full text line
    i = drawTxt(i, 11, tb, page, tPage);
  }
  for (; i < 128; i++) {
    tPage[i] = 0;
  } // space padding
}
// _____________________________________________________________________
// opt function draw

// LSB up
uint8_t const tBar[9] = {0b00000000, 0b10000000, 0b11000000,
                         0b11100000, 0b11110000, 0b11111000,
                         0b11111100, 0b11111110, 0b11111111};

/*
page0
    1
    .
    7
*/

uint8_t vBar(uint8_t val, int page) {
  uint8_t _page = 7 - (val / 8);
  if (page > _page)
    return 0b11111111; // below => light
  else if (page < _page)
    return 0; // above => dark
  else
    return tBar[val & 7];
}

// on the Oled display, a "page" is a 128x8 stripe of pixels
// described by 128 bytes. The 128x64 OLED consists of 8 "pages"
// I calculate and transmit one page at a time.
// It saves memory as small buffers are enough to transmit them.
void calcScopePage(int page, uint8_t tPage[128]) {
  uint16_t y0 = tY[0];
  uint16_t y1 = tY[1];
  uint16_t y2;
  for (int i = 0; i < 128; i++) {
    if (i < 127)
      y2 = tY[i + 1];
    uint16_t yM, ym;
    yM = ym = y1;
    uint16_t y = (y0 + y1) >> 1;
    yM = y > yM ? y : yM;
    ym = y < ym ? y : ym;
    y = (y2 + y1) >> 1;
    yM = y > yM ? y : yM;
    ym = y < ym ? y : ym;
    if (ym == yM)
      if (yM > 0)
        ym--;
      else
        yM++;
    tPage[i] = vBar(yM, page) & ~vBar(ym, page);
    y0 = y1;
    y1 = y2;
  }
}
// _____________________________________________________________________
void sendPage(int page) {
  /*
    Note: I consider that having a little flickering is not a big deal
    (compared to potential audio glitches).
    So, i do not use the chSysLock() chSysUnlock to protect the memcpy.
  */
  i2cAcquireBus(&I2CD1);
  // prepare transmission to the "page"
  cmd(COLUMNADDR, 0, 127);   // Column start end
  cmd(PAGEADDR, page, page); // Page start end
  if (attr_type == 1106) {
    cmd(0xB0 + page);     // set page address
    cmd(2 & 0xf);         // set lower column address
    cmd(0x10 | (2 >> 4)); // set higher column address
  }
  i2cReleaseBus(&I2CD1);

  { // Title and scope
    // on the beginning of drawing (page 0) we update the buffers
    if (page == 0) {
      // chSysLock();
      // update the textBuffer
      memcpy(textBuf, text, 11);
      // update scope buffer
      memcpy(tY, attr_scope.t, 128);
      // chSysUnlock();
      // scale for the display
      int8_t *stY = (int8_t *)tY;
      for (int i = 0; i < 128; i++) {
        tY[i] = ((stY[i] + 64) * 3) >> 3;
      }
    }
    if (page < 2) {
      calcTextPage(page, txbuf + 1);
    } else {
      calcScopePage(page, txbuf + 1);
    }
  }
  // transmission of the page

  // transmit the page
  txbuf[0] = 0x40;
  i2cAcquireBus(&I2CD1);
  i2cMasterTransmitTimeout(&I2CD1, attr_I2CADDR, txbuf, 129, rxbuf, 0, 30);
  i2cReleaseBus(&I2CD1);
}
// _____________________________________________________________________
void init() {
  i2cAcquireBus(&I2CD1);
  // Init sequence
  if (attr_type == 1106 || attr_type == 1306) {
    cmd(DISPLAYOFF);
    // 2 byte cmd,
    //  low nibble A[3:0]+1 = div ratio,
    //  high nibble A[7:4] freq  reset 1000b
    cmd(SETDISPLAYCLOCKDIV, 0x80);
    cmd(SETMULTIPLEX, LCDHEIGHT - 1);
    cmd(SETDISPLAYOFFSET, attr_type == 1306 ? 0x00 : 0x01);
    cmd(SETSTARTLINE_0);
    cmd(CHARGEPUMP, 0x14);
    cmd(MEMORYMODE, 0x00); // horizontal
    cmd(SEGREMAP | 0x1);
    cmd(COMSCANDEC);
    // 128 x 64
    cmd(SETCOMPINS, 0x12);
    cmd(SETCONTRAST, 0xCF);
    cmd(SETPRECHARGE, 0xF1);
    cmd(SETVCOMDETECT, 0x40);
    cmd(DISPLAYALLON_RESUME);
    cmd(NORMALDISPLAY);
    cmd(DEACTIVATE_SCROLL);
    cmd(DISPLAYON);
  } else {

    cmd(DISPLAYOFF);
    cmd(SETDISPLAYCLOCKDIV, 0x0a0); // vs 80
    cmd(SETMULTIPLEX, 0x03f);       // vs not in comment
    cmd(SETSTARTLINE_0);            // idem
    cmd(MEMORYMODE, 0x00);          // 02 vs00		/* page addressing mode vs line
                           // addressing mode*/

    cmd(SEGREMAP | 0x1); // idem
    cmd(COMSCANDEC);     // idem
                     // Flipmode
                     // U8X8_C(0x0a0),				/* segment remap
                     // a0/a1*/ U8X8_C(0x0c0),				/* c0:
                     // scan dir normal, c8: reverse */

    cmd(SETCOMPINS,
        0x012); // idem		/* com pin HW config, sequential com pin config
                // (bit 4), disable left/right remap (bit 5) */

    cmd(SETCONTRAST,
        0x06f); // vs 0xCF 		/* [2] set contrast control */
    cmd(SETPRECHARGE,
        0x0d3); // vs 0xF1 		/* [2] pre-charge period 0x022/f1*/
    cmd(SETVCOMDETECT,
        0x020); // vs 0x40 		/* vcomh deselect level */
                // if vcomh is 0, then this will give the biggest range for
                // contrast control issue #98 restored the old values for the
                // noname constructor, because vcomh=0 will not work for all
                // OLEDs, #116

    cmd(DEACTIVATE_SCROLL); // idem				/* Deactivate scroll
                            // */
    cmd(DISPLAYALLON_RESUME); // idem				/* output ram to
                              // display */
    cmd(NORMALDISPLAY); // idem				/* none inverted normal
                        // display mode */
    cmd(DISPLAYON);
  }

  i2cReleaseBus(&I2CD1);
}
// _____________________________________________________________________
void setup() {
  static uint8_t _txbuf[132] __attribute__((section(".sram2")));
  static uint8_t _rxbuf[8] __attribute__((section(".sram2")));
  txbuf = _txbuf;
  rxbuf = _rxbuf;
  init();
}

// _____________________________________________________________________
// THREADS
msg_t ThreadX2() {
  setup();
  while (!chThdShouldTerminate()) {
    if (!disable) {
      for (int i = 0; i < 8; i++) {
        sendPage(i);
      }
    }
    chThdSleepMilliseconds(32);
  }
  chThdExit((msg_t)0);
}

static msg_t ThreadX(void *arg) { ((attr_parent *)arg)->ThreadX2(); }

// 128 is not enough, try 192 <-----------
WORKING_AREA(waThreadX, 192);
Thread *Thd;
Init
for (int i = 0; i < 11; i++) {
  text[i] = textBuf[i] = ' ';
}
Thd = chThdCreateStatic(waThreadX, sizeof(waThreadX), NORMALPRIO, ThreadX,
                        (void *)this);
Control Rate
disable = false;

if (inlet_line1 != NULL) {
  int i = 0;
  while (i < 11 & inlet_line1[i] != '\0') {
    text[i] = inlet_line1[i];
    i++;
  }
  while (i < 11) {
    text[i] = ' ';
    i++;
  }
}
Dispose
chThdTerminate(Thd);
chThdWait(Thd);

Privacy

© 2024 Zrna Research