OLED128x64

OLED 128x64 SSD1306 on I2C. PB8=SCL PB9=SDA
Author:
License: GPL
Github: tiar/HW/OLED128x64.axo

Inlets

charptr32 line1

charptr32 line2

charptr32 line3

charptr32 line4

int32.positive mode

Outlets

None

Attributes

objref scope

combo type

combo I2CADDR

Declaration
// add include tiar_font5x8
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 cpt;
uint8_t *txbuf;
uint8_t *rxbuf;
int32_t mode;

uint8_t text[11 * 4];
uint8_t tY[128];

// SETUP
// ------------------
void cmd(uint8_t c) {
  txbuf[0] = 0;
  txbuf[1] = c;
  i2cMasterTransmitTimeout(&I2CD1, attr_I2CADDR, txbuf, 2, rxbuf, 0, 30);
  // chThdSleepMilliseconds(1);
}

// _____________________________________________________________________
void fill(uint8_t v) {
  cmd(COLUMNADDR);
  cmd(0);
  cmd(127); // Column start end
  cmd(PAGEADDR);
  cmd(0);
  cmd(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);
    // chThdSleepMilliseconds(1);
  }
}
// _____________________________________________________________________
// 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;
  for (int nc = 0; nc < 11; nc++) {
    int ascii_32 = text[nc + 11 * tLine] - ' ';
    if (ascii_32 < 0 || ascii_32 >= 128 - 32)
      ascii_32 = 0;
    const uint8_t *adChar = tiar_font5x8 + ascii_32 * 5;
    for (int slice = 0; slice < 5; slice++) {
      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++;
      tPage[i] = s;
      i++;
    }
    tPage[i] = 0;
    i++; // separator space
  }
  while (i < 128) {
    tPage[i] = 0;
    i++;
  } // 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];
}
void _calcScopePage(int page, uint8_t tPage[128]) {
  for (int i = 0; i < 128; i++) {
    uint16_t y = ((attr_scope.t[i] + 64) * 3) / 8;
    tPage[i] = vBar(y, page);
  }
}
void calcScopePage(int page, uint8_t tPage[128]) {
  if (page < 2) {
    calcTextPage(page, tPage);
  } else {
    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 sendTextPage(int page) {
  cmd(COLUMNADDR);
  cmd(0);
  cmd(127); // Column start end
  cmd(PAGEADDR);
  cmd(page);
  cmd(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
  }

  txbuf[0] = 0x40;
  if (mode == 0) {
    calcTextPage(page, txbuf + 1);
    i2cMasterTransmitTimeout(&I2CD1, attr_I2CADDR, txbuf, 129, rxbuf, 0, 30);
  } else if (mode == 1) {
    if (page == 1)
      for (int i = 0; i < 128; i++)
        tY[i] = ((attr_scope.t[i] + 64) * 3) >> 3;
    calcScopePage(page, txbuf + 1);
    i2cMasterTransmitTimeout(&I2CD1, attr_I2CADDR, txbuf, 129, rxbuf, 0, 30);
  }
}
// _____________________________________________________________________
void init() {
  // 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);
  cmd(0x80);
  cmd(SETMULTIPLEX);
  cmd(LCDHEIGHT - 1);
  cmd(SETDISPLAYOFFSET);
  cmd(attr_type == 1306 ? 0x00 : 0x01);
  cmd(SETSTARTLINE_0);
  cmd(CHARGEPUMP);
  cmd(0x14);
  cmd(MEMORYMODE);
  cmd(0x00);
  cmd(SEGREMAP | 0x1);
  cmd(COMSCANDEC);
  // 128 x 64
  cmd(SETCOMPINS);
  cmd(0x12);
  cmd(SETCONTRAST);
  cmd(0xCF);
  cmd(SETPRECHARGE);
  cmd(0xF1);
  cmd(SETVCOMDETECT);
  cmd(0x40);
  cmd(DISPLAYALLON_RESUME);
  cmd(NORMALDISPLAY);
  cmd(DEACTIVATE_SCROLL);
  cmd(DISPLAYON);
}
// _____________________________________________________________________
void setup() {
  static uint8_t _txbuf[129] __attribute__((section(".sram2")));
  static uint8_t _rxbuf[8] __attribute__((section(".sram2")));
  txbuf = _txbuf;
  rxbuf = _rxbuf;
  init();
}
// _____________________________________________________________________
void loop() {
  uint8_t p[] = {0, 1, 4, 5, 3, 6, 2, 7};
  sendTextPage(p[cpt % 8]);
  cpt++;
}
// _____________________________________________________________________
// THREADS
msg_t ThreadX2() {
  setup();
  while (!chThdShouldTerminate()) {
    loop();
    chThdSleepMilliseconds(1);
  }
  chThdExit((msg_t)0);
}

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

WORKING_AREA(waThreadX, 1024);
Thread *Thd;
Init
cpt = 0;
for (int i = 0; i < 11 * 4; i++) {
  text[i] = ' ';
}

// setup the pins
palSetPadMode(GPIOB, 8,
              PAL_MODE_ALTERNATE(4) | PAL_STM32_PUDR_PULLUP |
                  PAL_STM32_OTYPE_OPENDRAIN); // SCL
palSetPadMode(GPIOB, 9,
              PAL_MODE_ALTERNATE(4) | PAL_STM32_PUDR_PULLUP |
                  PAL_STM32_OTYPE_OPENDRAIN); // SDA
static const I2CConfig i2cfg = {
    OPMODE_I2C,
    400000,
    FAST_DUTY_CYCLE_2,
};
/*static const I2CConfig i2cfg = {
    OPMODE_I2C,
    100000,
    STD_DUTY_CYCLE,
};*/
i2cStart(&I2CD1, &i2cfg);
Thd = chThdCreateStatic(waThreadX, sizeof(waThreadX), NORMALPRIO, ThreadX,
                        (void *)this);
Control Rate
mode = inlet_mode;

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 (mode == 0) {
  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);
i2cStop(&I2CD1);
palSetPadMode(GPIOB, 8, PAL_MODE_INPUT_ANALOG);
palSetPadMode(GPIOB, 9, PAL_MODE_INPUT_ANALOG);

Privacy

© 2024 Zrna Research