displaying on SSD1306, 128x64 pixels OLED
OLED.cpp@6:c69f08f464b5, 2020-02-23 (annotated)
- Committer:
- bvirk
- Date:
- Sun Feb 23 01:57:35 2020 +0000
- Revision:
- 6:c69f08f464b5
- Parent:
- 4:19da6ea94042
- Child:
- 8:5972683a7190
All examples in own files - demo of command pattern using functionpointer array to chose example
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bvirk | 3:f36427797fd7 | 1 | |
bvirk | 3:f36427797fd7 | 2 | #include "MicroBit.h" |
bvirk | 3:f36427797fd7 | 3 | #include "MicroBitI2C.h" |
bvirk | 3:f36427797fd7 | 4 | |
bvirk | 3:f36427797fd7 | 5 | #include "OLEDGlobals.h" |
bvirk | 3:f36427797fd7 | 6 | #include "OLED.h" |
bvirk | 4:19da6ea94042 | 7 | #include "cppNorm.h" |
bvirk | 4:19da6ea94042 | 8 | |
bvirk | 4:19da6ea94042 | 9 | /** |
bvirk | 4:19da6ea94042 | 10 | * Create a representation of a 128x64 pixels, ssd1306 compatble OLED display |
bvirk | 4:19da6ea94042 | 11 | * |
bvirk | 4:19da6ea94042 | 12 | **/ |
bvirk | 3:f36427797fd7 | 13 | OLED::OLED() : |
bvirk | 3:f36427797fd7 | 14 | i2c(I2C_SDA0,I2C_SCL0) |
bvirk | 3:f36427797fd7 | 15 | ,charX(0) |
bvirk | 3:f36427797fd7 | 16 | ,charY(0) |
bvirk | 3:f36427797fd7 | 17 | ,displayWidth(128) |
bvirk | 3:f36427797fd7 | 18 | ,displayHeight(64 / 8) |
bvirk | 4:19da6ea94042 | 19 | ,screenSize(0) |
bvirk | 4:19da6ea94042 | 20 | ,pendingNewline(false) |
bvirk | 4:19da6ea94042 | 21 | {} |
bvirk | 3:f36427797fd7 | 22 | |
bvirk | 3:f36427797fd7 | 23 | void OLED::command(uint8_t cmd) { |
bvirk | 3:f36427797fd7 | 24 | char buf[2]; |
bvirk | 3:f36427797fd7 | 25 | buf[0] = '\0'; |
bvirk | 3:f36427797fd7 | 26 | buf[1] = cmd; |
bvirk | 3:f36427797fd7 | 27 | i2c.write(chipAdress,buf,2); |
bvirk | 3:f36427797fd7 | 28 | } |
bvirk | 4:19da6ea94042 | 29 | void OLED::init() { |
bvirk | 4:19da6ea94042 | 30 | init(128, 64); |
bvirk | 4:19da6ea94042 | 31 | } |
bvirk | 4:19da6ea94042 | 32 | |
bvirk | 3:f36427797fd7 | 33 | |
bvirk | 3:f36427797fd7 | 34 | void OLED::init(uint8_t width, uint8_t height) { |
bvirk | 3:f36427797fd7 | 35 | command(SSD1306_DISPLAYOFF); |
bvirk | 3:f36427797fd7 | 36 | command(SSD1306_SETDISPLAYCLOCKDIV); |
bvirk | 3:f36427797fd7 | 37 | command(0x80); // the suggested ratio 0x80 |
bvirk | 3:f36427797fd7 | 38 | command(SSD1306_SETMULTIPLEX); |
bvirk | 3:f36427797fd7 | 39 | command(0x3F); |
bvirk | 3:f36427797fd7 | 40 | command(SSD1306_SETDISPLAYOFFSET); |
bvirk | 3:f36427797fd7 | 41 | command(0x0); // no offset |
bvirk | 3:f36427797fd7 | 42 | command(SSD1306_SETSTARTLINE | 0x0); // line #0 |
bvirk | 3:f36427797fd7 | 43 | command(SSD1306_CHARGEPUMP); |
bvirk | 3:f36427797fd7 | 44 | command(0x14); |
bvirk | 3:f36427797fd7 | 45 | command(SSD1306_MEMORYMODE); |
bvirk | 3:f36427797fd7 | 46 | command(0x00); // 0x0 act like ks0108 |
bvirk | 3:f36427797fd7 | 47 | command(SSD1306_SEGREMAP | 0x1); |
bvirk | 3:f36427797fd7 | 48 | command(SSD1306_COMSCANDEC); |
bvirk | 3:f36427797fd7 | 49 | command(SSD1306_SETCOMPINS); |
bvirk | 3:f36427797fd7 | 50 | command(0x12); |
bvirk | 3:f36427797fd7 | 51 | command(SSD1306_SETCONTRAST); |
bvirk | 3:f36427797fd7 | 52 | command(0xCF); |
bvirk | 3:f36427797fd7 | 53 | command(SSD1306_SETPRECHARGE); |
bvirk | 3:f36427797fd7 | 54 | command(0xF1); |
bvirk | 3:f36427797fd7 | 55 | command(SSD1306_SETVCOMDETECT); |
bvirk | 3:f36427797fd7 | 56 | command(0x40); |
bvirk | 3:f36427797fd7 | 57 | command(SSD1306_DISPLAYALLON_RESUME); |
bvirk | 3:f36427797fd7 | 58 | command(SSD1306_NORMALDISPLAY); |
bvirk | 3:f36427797fd7 | 59 | command(SSD1306_DISPLAYON); |
bvirk | 3:f36427797fd7 | 60 | displayWidth = width; |
bvirk | 3:f36427797fd7 | 61 | displayHeight = height / 8; |
bvirk | 3:f36427797fd7 | 62 | screenSize = displayWidth * displayHeight; |
bvirk | 3:f36427797fd7 | 63 | charX = xOffset; |
bvirk | 3:f36427797fd7 | 64 | charY = yOffset; |
bvirk | 3:f36427797fd7 | 65 | loadStarted = false; |
bvirk | 3:f36427797fd7 | 66 | loadPercent = 0; |
bvirk | 3:f36427797fd7 | 67 | clear(); |
bvirk | 3:f36427797fd7 | 68 | } |
bvirk | 3:f36427797fd7 | 69 | |
bvirk | 3:f36427797fd7 | 70 | void OLED::clear() { |
bvirk | 3:f36427797fd7 | 71 | loadStarted = false; |
bvirk | 3:f36427797fd7 | 72 | loadPercent = 0; |
bvirk | 3:f36427797fd7 | 73 | command(SSD1306_SETCOLUMNADRESS); |
bvirk | 3:f36427797fd7 | 74 | command(0x00); |
bvirk | 3:f36427797fd7 | 75 | command(displayWidth - 1); |
bvirk | 3:f36427797fd7 | 76 | command(SSD1306_SETPAGEADRESS); |
bvirk | 3:f36427797fd7 | 77 | command(0x00); |
bvirk | 3:f36427797fd7 | 78 | command(displayHeight - 1); |
bvirk | 3:f36427797fd7 | 79 | char data[17]; |
bvirk | 3:f36427797fd7 | 80 | data[0] = 0x40; // Data Mode; |
bvirk | 3:f36427797fd7 | 81 | for (int8_t i = 1; i < 17; i++) |
bvirk | 3:f36427797fd7 | 82 | data[i] = 0x00; |
bvirk | 3:f36427797fd7 | 83 | // send display buffer in 16 byte chunks; |
bvirk | 3:f36427797fd7 | 84 | for (int16_t i = 0; i < screenSize; i += 16) { |
bvirk | 3:f36427797fd7 | 85 | i2c.write(chipAdress, data, 17,false); |
bvirk | 3:f36427797fd7 | 86 | } |
bvirk | 3:f36427797fd7 | 87 | charX = xOffset; |
bvirk | 3:f36427797fd7 | 88 | charY = yOffset; |
bvirk | 4:19da6ea94042 | 89 | setTextArea(32,true); |
bvirk | 3:f36427797fd7 | 90 | } |
bvirk | 3:f36427797fd7 | 91 | |
bvirk | 3:f36427797fd7 | 92 | |
bvirk | 3:f36427797fd7 | 93 | void OLED::drawChar(uint8_t x, uint8_t y, uint8_t chr ) { |
bvirk | 3:f36427797fd7 | 94 | command(SSD1306_SETCOLUMNADRESS); |
bvirk | 3:f36427797fd7 | 95 | command(x); |
bvirk | 3:f36427797fd7 | 96 | command(x + 5); |
bvirk | 3:f36427797fd7 | 97 | command(SSD1306_SETPAGEADRESS); |
bvirk | 3:f36427797fd7 | 98 | command(y); |
bvirk | 3:f36427797fd7 | 99 | command(y + 1); |
bvirk | 4:19da6ea94042 | 100 | textArea[x/6][y]=chr; |
bvirk | 3:f36427797fd7 | 101 | char line[2]; |
bvirk | 3:f36427797fd7 | 102 | line[0] = 0x40; |
bvirk | 3:f36427797fd7 | 103 | for (int8_t i = 0; i < 6; i++) { |
bvirk | 3:f36427797fd7 | 104 | if (i == 5) |
bvirk | 3:f36427797fd7 | 105 | line[1] = 0x00; |
bvirk | 3:f36427797fd7 | 106 | else |
bvirk | 3:f36427797fd7 | 107 | line[1] = font[chr][i]; |
bvirk | 3:f36427797fd7 | 108 | i2c.write(chipAdress, line, 2, false); |
bvirk | 3:f36427797fd7 | 109 | } |
bvirk | 3:f36427797fd7 | 110 | } |
bvirk | 3:f36427797fd7 | 111 | |
bvirk | 4:19da6ea94042 | 112 | |
bvirk | 4:19da6ea94042 | 113 | void OLED::setTextArea(uint8_t chr, bool setLine8=false) { |
bvirk | 4:19da6ea94042 | 114 | for (int8_t line = 0; line <= 8; line++) |
bvirk | 4:19da6ea94042 | 115 | if (line < 8 || setLine8) |
bvirk | 4:19da6ea94042 | 116 | for (int8_t xPos = 0; xPos < 22; xPos++) |
bvirk | 4:19da6ea94042 | 117 | textArea[xPos][line]=chr; |
bvirk | 4:19da6ea94042 | 118 | } |
bvirk | 4:19da6ea94042 | 119 | |
bvirk | 4:19da6ea94042 | 120 | |
bvirk | 4:19da6ea94042 | 121 | void OLED::scroll() { |
bvirk | 4:19da6ea94042 | 122 | for (int8_t line = 1; line <= 8; line++) |
bvirk | 4:19da6ea94042 | 123 | for (int8_t xPos = 0; xPos < 22; xPos++) |
bvirk | 4:19da6ea94042 | 124 | drawChar(6*xPos,line-1,textArea[xPos][line]); |
bvirk | 4:19da6ea94042 | 125 | } |
bvirk | 4:19da6ea94042 | 126 | |
bvirk | 4:19da6ea94042 | 127 | void OLED::newLine() { |
bvirk | 4:19da6ea94042 | 128 | charY++; |
bvirk | 4:19da6ea94042 | 129 | charX = xOffset; |
bvirk | 4:19da6ea94042 | 130 | if (charY == 8) { |
bvirk | 4:19da6ea94042 | 131 | charY = 7; |
bvirk | 4:19da6ea94042 | 132 | scroll(); |
bvirk | 4:19da6ea94042 | 133 | } |
bvirk | 4:19da6ea94042 | 134 | pendingNewline=false; |
bvirk | 4:19da6ea94042 | 135 | } |
bvirk | 4:19da6ea94042 | 136 | |
bvirk | 6:c69f08f464b5 | 137 | void OLED::puts(string str) { |
bvirk | 3:f36427797fd7 | 138 | for (uint16_t i = 0; i < str.length(); i++) { |
bvirk | 6:c69f08f464b5 | 139 | if (str.charAt(i) == '\r') |
bvirk | 4:19da6ea94042 | 140 | charX = xOffset; |
bvirk | 6:c69f08f464b5 | 141 | else if (str.charAt(i) == '\n') { |
bvirk | 6:c69f08f464b5 | 142 | if (pendingNewline) |
bvirk | 6:c69f08f464b5 | 143 | newLine(); |
bvirk | 6:c69f08f464b5 | 144 | pendingNewline=true; |
bvirk | 6:c69f08f464b5 | 145 | } else { |
bvirk | 6:c69f08f464b5 | 146 | if (pendingNewline || charX > displayWidth - 6) |
bvirk | 6:c69f08f464b5 | 147 | newLine(); |
bvirk | 6:c69f08f464b5 | 148 | drawChar(charX, charY, (uint8_t)str.charAt(i)); |
bvirk | 6:c69f08f464b5 | 149 | charX += 6; |
bvirk | 3:f36427797fd7 | 150 | } |
bvirk | 3:f36427797fd7 | 151 | } |
bvirk | 3:f36427797fd7 | 152 | } |
bvirk | 4:19da6ea94042 | 153 | |
bvirk | 6:c69f08f464b5 | 154 | uint8_t OLED::printf(const char * fmt, ...) { |
bvirk | 6:c69f08f464b5 | 155 | va_list args; |
bvirk | 6:c69f08f464b5 | 156 | va_start(args, fmt); |
bvirk | 6:c69f08f464b5 | 157 | uint8_t len = min(vsnprintf(printf_text, printf_textSize, fmt, args), printf_textSize); |
bvirk | 6:c69f08f464b5 | 158 | va_end(args); |
bvirk | 6:c69f08f464b5 | 159 | printf_text[len] = '\0'; |
bvirk | 6:c69f08f464b5 | 160 | puts(printf_text); |
bvirk | 6:c69f08f464b5 | 161 | return len; |
bvirk | 4:19da6ea94042 | 162 | } |