From 5c2bedd10fe47656641098c0994f70008403f2a0 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sat, 30 May 2020 19:10:34 +0200 Subject: [PATCH] extract constructor and remove SPI conditions from official library --- twinmax/display.cpp | 121 ++++++++++++++++++++++++++++++++++++++++++++ twinmax/display.h | 22 ++++++++ 2 files changed, 143 insertions(+) create mode 100644 twinmax/display.cpp create mode 100644 twinmax/display.h diff --git a/twinmax/display.cpp b/twinmax/display.cpp new file mode 100644 index 0000000..985a71c --- /dev/null +++ b/twinmax/display.cpp @@ -0,0 +1,121 @@ +#include +#include +#include "display.h" + +#include + +#define WIRE_MAX 32 +#define TRANSACTION_START wire->setClock(400000UL); +#define TRANSACTION_END wire->setClock(100000UL); + +void temporary(){ + + const uint8_t resetPin = 4; + + // Adafruit_SSD1306 display(width, height, &Wire, resetPin); + Display display(resetPin); + + + // / address for I2C 128x32 + // display.begin(SSD1306_SWITCHCAPVCC, 0x3C) + + // display.clearDisplay(); + + // display.drawPixel(x, y, SSD1306_WHITE) + // display.drawPixel(x, y, SSD1306_BLACK) + +} + +void Display::send_command(const uint8_t c){ + _wire->beginTransmission(_i2caddr); + _wire->write((uint8_t) 0x00); + _wire->write(c); + _wire->endTransmission(); +} + +void Display::send_commands(const uint8_t *cc, const uint8_t n){ + _wire->beginTransmission(_i2caddr); + _wire->write((uint8_t) 0x00); + _wire->write(c); + _wire->endTransmission(); + uint8_t written = 1; + while(n--){ + if( written >= WIRE_MAX ){ + _wire->endTransmission(); + _wire->beginTransmission(_i2caddr); + _wire->write((uint8_t) 0x00); + written = 1; + } + _wire->write(pgm_read_byte(cc++)); + written++; + } + _wire->endTransmission(); +} + + +void Display::Display(const resetPin){ + _rstPin = resetPin; + _wire = &Wire; + + _wire.begin(); + + // reset sequence + if( _rstPin >= 0 ){ + pinMode(rstPin, OUTPUT); + digitalWrite(rstPin, HIGH); + delay(1); + digitalWrite(rstPin, LOW); + delay(10); + digitalWrite(rstPin, HIGH); + } + + TRANSACTION_START + + // init sequence + static const uint8_t PROGMEM init1[] = { + DSP_DISPLAYOFF, + DSP_SETDISPLAYCLOCKDIV, + 0x80, // suggested ratio + DSP_SETMULTIPLEX + }; + send_commands(init1, sizeof(init1)); + send_command(_height - 1); + + static const uint8_t PROGMEM init2[] = { + DSP_SETDISPLAYOFFSET, + 0x0, + DSP_SETSTARTLINE | 0x0, + DSP_CHARGEPUMP + }; + sendcommands(init2, sizeof(init2)); + send_command(0x14); + + static const uint8_t PROGMEM init3[] = { + DSP_MEMORYMODE, + 0x00, + DSP_SEGREMAP | 0x1, + DSP_COMSCANDEC + }; + sendcommands(init3, sizeof(init3)); + + sendcommand(DSP_SETCOMPINS); + sendcommand(_comPin); + sendcommand(DSP_SETCONTRAST); + sendcommand(_contrast); + send_command(DSP_SETPRECHARGE); + send_command(0xF1); + + static const uint8_t PROGMEM init5[] = { + DSP_SETVCOMPDETECT, + 0x40, + DSP_DISPLAYALLON_RESUME, + DSP_NORMALDISPLAY, + DSP_DEACTIVATE_SCROLL, + DSP_DISPLAY_ON + }; + sendcommands(init5, sizeof(init5)); + + TRANSACTION_END + + return; +} \ No newline at end of file diff --git a/twinmax/display.h b/twinmax/display.h new file mode 100644 index 0000000..9fb1578 --- /dev/null +++ b/twinmax/display.h @@ -0,0 +1,22 @@ +#ifndef _DISPLAY_H_DEF_ +#define _DISPLAY_H_DEF_ + + class Display { + public: + void Display(const uint8_t width=128, const uint8_t height=32, const resetPin=-1); + void begin(); + + protected: + void send_command(const uint8_t c); + void send_commands(const uint8_t *cc, const uint8_t n); + private: + const uint8_t _width { 128 }; + const uint8_t _height { 32 }; + const uint8_t _comPin { 0x02 }; + const uint8_t _i2caddr { 0x3C }; + const uint8_t _contrast { 0x8F }; + uint8_t _rstPin { -1 }; + TwoWire* _wire { nullptr }; + }; + +#endif