OLED128x64niceWOScope

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

Inlets

charptr32 line1

charptr32 line2

charptr32 line3

charptr32 line4

bool32 disable

Outlets

None

Attributes

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;
int32_t mode;

uint8_t text[11 * 4];    // text inputs
uint8_t textBuf[11 * 4]; // text inputs copy (to avoid flicker)
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);
}

// _____________________________________________________________________

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(int nc = 0; nc < 11; 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

    }*/
  } else if (tb[0] == 1) { // strbar
    uint8_t filledBar = ((page & 1) == 0) ? 0b11100000 : 0b00111111;
    i = drawTxt(i, 5, tb + 2, page, tPage);
    while (i < 63) {
      tPage[i] = 0;
      i++;
    }                  // space padding
    if (tb[1] < 128) { // positive value => filled bar
      tPage[i] = 0;
      i++;
      for (i = 64; i < 63 + tb[1] && i < 128; i++) {
        tPage[i] = filledBar;
      }
    } else { // negative value => hollow bar
      tPage[i] = filledBar;
      i++;
      uint8_t hollowBar = ((page & 1) == 0) ? 0b00100000 : 0b00100000;
      for (i = 64; i < 63 + 256 - tb[1] && i < 127; i++) {
        tPage[i] = hollowBar;
      }
      tPage[i] = filledBar;
      i++;
    }
  } else if (tb[0] == 2) { // bar
    uint8_t filledBar = ((page & 1) == 0) ? 0b11100000 : 0b00111111;
    if (tb[1] <= 128) { // positive value => filled bar
      tPage[i] = 0;
      i++;
      for (i = 0; i < tb[1]; i++) {
        tPage[i] = filledBar;
      }
    } else { // negative value => hollow bar
      tPage[i] = filledBar;
      i++;
      uint8_t hollowBar = ((page & 1) == 0) ? 0b00100000 : 0b00100000;
      for (; i < 256 - tb[1]; i++) {
        tPage[i] = hollowBar;
      }
      tPage[i] = filledBar;
      i++;
    }
  } else if (tb[0] == 3) { // strbar2
    uint8_t filledBar = ((page & 1) == 0) ? 0b11110000 : 0b01111000;
    i = drawTxt(i, 5, tb + 3, page, tPage);
    for (; i < 63; i++) {
      tPage[i] = 0;
    } // space padding
    int v = tb[1 + (page & 1)];
    if (v < 128) { // positive value => filled bar
      tPage[i] = 0;
      i++;
      for (i = 64; i < 63 + v && i < 128; i++) {
        tPage[i] = filledBar;
      }
    } else { // negative value => hollow bar
      tPage[i] = filledBar;
      i++;
      uint8_t hollowBar = ((page & 1) == 0) ? 0b10010000 : 0b01001000;
      for (i = 64; i < 63 + 256 - v && i < 127; i++) {
        tPage[i] = hollowBar;
      }
      tPage[i] = filledBar;
      i++;
    }
  }
  //_______________________________________________________
  else if (tb[0] == 4) { // bar dble
    uint8_t filledBar = ((page & 1) == 0) ? 0b11110000 : 0b01111000;
    int v = tb[1 + (page & 1)];
    if (v <= 128) { // positive value => filled bar
      for (i = 0; i < v; i++) {
        tPage[i] = filledBar;
      }
    } else { // negative value => hollow bar
      tPage[i] = filledBar;
      i++;
      uint8_t hollowBar = ((page & 1) == 0) ? 0b10010000 : 0b01001000;
      for (; i < 256 - v; i++) {
        tPage[i] = hollowBar;
      }
      tPage[i] = filledBar;
      i++;
    }
  }
  for (; i < 128; i++) {
    tPage[i] = 0;
  } // space padding
}
// _____________________________________________________________________
/*
page0
    1
    .
    7
*/
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);

  // 4 text lines
  // on the beginning of drawing (page 0) we update the buffers
  if (page == 0) {
    memcpy(textBuf, text, 11 * 4);
  }
  calcTextPage(page, txbuf + 1);

  // 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
  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);
  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 * 4; i++) {
  text[i] = textBuf[i] = ' ';
}
Thd = chThdCreateStatic(waThreadX, sizeof(waThreadX), NORMALPRIO, ThreadX,
                        (void *)this);
Control Rate
disable = inlet_disable; // can be useful when playing with future modules
                         // targetting the same display

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++;
  }
}

if (inlet_line2 != NULL) {
  int i = 0;
  while (i < 11 & inlet_line2[i] != '\0') {
    text[i + 11] = inlet_line2[i];
    i++;
  }
  while (i < 11) {
    text[i + 11] = ' ';
    i++;
  }
}
if (inlet_line3 != NULL) {
  int i = 0;
  while (i < 11 & inlet_line3[i] != '\0') {
    text[i + 2 * 11] = inlet_line3[i];
    i++;
  }
  while (i < 11) {
    text[i + 2 * 11] = ' ';
    i++;
  }
}
if (inlet_line4 != NULL) {
  int i = 0;
  while (i < 11 & inlet_line4[i] != '\0') {
    text[i + 3 * 11] = inlet_line4[i];
    i++;
  }
  while (i < 11) {
    text[i + 3 * 11] = ' ';
    i++;
  }
}
Dispose
chThdTerminate(Thd);
chThdWait(Thd);

Privacy

© 2024 Zrna Research