├── .gitignore ├── .travis.yml ├── README.md ├── lib ├── Adafruit_GFX │ ├── Adafruit_GFX.cpp │ ├── Adafruit_GFX.h │ ├── README.md │ ├── ft2build.h │ ├── gfxfont.h │ ├── glcdfont.c │ ├── library.properties │ └── license.txt ├── ESP_SSD1306 │ ├── ESP_SSD1306.cpp │ └── ESP_SSD1306.h └── readme.txt ├── platformio.ini └── src └── main.ino /.gitignore: -------------------------------------------------------------------------------- 1 | .pioenvs 2 | .project 3 | 4 | .settings/* 5 | src/*.ino_ 6 | 7 | .cproject 8 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | # Continuous Integration (CI) is the practice, in software 2 | # engineering, of merging all developer working copies with a shared mainline 3 | # several times a day < http://docs.platformio.org/en/latest/ci/index.html > 4 | # 5 | # Documentation: 6 | # 7 | # * Travis CI Embedded Builds with PlatformIO 8 | # < https://docs.travis-ci.com/user/integration/platformio/ > 9 | # 10 | # * PlatformIO integration with Travis CI 11 | # < http://docs.platformio.org/en/latest/ci/travis.html > 12 | # 13 | # * User Guide for `platformio ci` command 14 | # < http://docs.platformio.org/en/latest/userguide/cmd_ci.html > 15 | # 16 | # 17 | # Please choice one of the following templates (proposed below) and uncomment 18 | # it (remove "# " before each line) or use own configuration according to the 19 | # Travis CI documentation (see above). 20 | # 21 | 22 | 23 | # 24 | # Template #1: General project. Test it using existing `platformio.ini`. 25 | # 26 | 27 | # language: python 28 | # python: 29 | # - "2.7" 30 | # 31 | # sudo: false 32 | # cache: 33 | # directories: 34 | # - "~/.platformio" 35 | # 36 | # install: 37 | # - pip install -U platformio 38 | # 39 | # script: 40 | # - platformio run 41 | 42 | 43 | # 44 | # Template #2: The project is intended to by used as a library with examples 45 | # 46 | 47 | # language: python 48 | # python: 49 | # - "2.7" 50 | # 51 | # sudo: false 52 | # cache: 53 | # directories: 54 | # - "~/.platformio" 55 | # 56 | # env: 57 | # - PLATFORMIO_CI_SRC=path/to/test/file.c 58 | # - PLATFORMIO_CI_SRC=examples/file.ino 59 | # - PLATFORMIO_CI_SRC=path/to/test/directory 60 | # 61 | # install: 62 | # - pip install -U platformio 63 | # 64 | # script: 65 | # - platformio ci --lib="." --board=TYPE_1 --board=TYPE_2 --board=TYPE_N 66 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Overview 2 | A base IoT template for ESP8266-based WiFi boards. 3 | 4 | Check out the next iteration of the project at https://github.com/aperepel/iot-mqtt-spiffs-deepsleep-oled for more advanced features. 5 | 6 | * On startup, it enters the SmartConfig mode and 7 | awaits for WiFi credentials to be provided (the easiest is to use your smartphone on the same network). There 8 | are alternative, less robust methods, but not covered here. 9 | * Once it gets WiFi credentials, it connects to the configured network 10 | * Everything is accompanied by OLED prompts and debug info on UART/Serial. 11 | * After connection is established, the board's IP is printed. 12 | * The board turns off the OLED display 13 | * Press the button once - it flashes the logo and prints the IP address. Turns off the display again. 14 | * Hold the button for 5 seconds - wipes out WiFi credentials and goes into the SmartConfig mode (e.g. 15 | when you need to connect to a different WiFi network.) 16 | 17 | # Show Me! 18 | [![IMAGE ALT TEXT](https://i.ytimg.com/vi/mdC_2jkhn_g/2.jpg?time=1452273766992)](https://www.youtube.com/watch?v=mdC_2jkhn_g "IoT ESP8266 SSD1306 OLED SmartConfig Starter in 4K") 19 | 20 | Note: the wiring and code were updated since the video was shot (SDA pin moved from D2 to D7) - current version won't flash the LED on every RX/TX. 21 | 22 | # Why? 23 | Over time I recognized repeatable patterns and best practices I followed for my projects and wanted to 24 | capture those as a starter project. E.g. if you're interested in a connected device which goes beyond 25 | blinking an LED and take provisioning/deployment/security and operations seriously. 26 | 27 | From this state, use your favorite MQTT library, host the WebServer, POST JSON to your REST API endpoint, etc. 28 | All while interacting with the whole gamut of sensors, the usual Arduino way. 29 | 30 | # TBD 31 | * more efficient sleep for when running on battery (currently draws ~20mA in always-on mode) 32 | * ~~Fritzing wiring diagram (if it has esp8266 components or similar?)~~ http://fritzing.org/projects/iot-esp8266-ssd1306-oled-starter 33 | * A video demonstrating the flow 34 | 35 | # Components 36 | * Any ESP8266/esp12e/NodeMCU (no Lua, only using native Arduino/C++ code) - https://github.com/esp8266/esp8266-wiki/wiki 37 | * SSD_1306 128x64 OLED display in I2C mode (not SPI) 38 | * A push button + a 10K resistor to control display and force WiFi credential resets 39 | * ESP8266 SmartConfig Android app: https://play.google.com/store/apps/details?id=com.cmmakerclub.iot.esptouch . 40 | Used to perform initial WiFi setup and provide credentials to the board. *no passwords are hardcoded 41 | in the source code*. 42 | * For iOS users (I haven't tried it, but..) https://github.com/EspressifApp/EsptouchForIOS 43 | * http://www.platformio.org (seriously, save yourself some trouble and start using it today) 44 | 45 | # Building 46 | ``` 47 | # serial port monitor part is optional, but nice 48 | platformio run -t upload && platformio serialports monitor --baud 115200 49 | ``` 50 | 51 | # IDE Support 52 | Really, any IDE which PlatformIO supports will do (which in practice means everything.) 53 | From free offerings I prefer Eclipse CDT for C++ development (a piece of me dies every time I use 54 | Eclipse, but it still is light years ahead of other free alternatives for IoT development ;) ) 55 | ``` 56 | # read PlatformIO reference docs for details, but here's an Eclipse example (respond Y when prompted) 57 | platformio init --ide eclipse --board esp12e 58 | ``` 59 | In Eclipse, right-click in the project pane -> Import from Existing sources. 60 | 61 | # Notes 62 | * The `ESP_SSD1306` library is a display driver fork of the `Adafruit_SSD1306` with changes required for ESP8266. 63 | * I had to inline the `Adafruit_GFX` library to pick up a more recent version from the one listed in PlatformIO repos. 64 | -------------------------------------------------------------------------------- /lib/Adafruit_GFX/Adafruit_GFX.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This is the core graphics library for all our displays, providing a common 3 | set of graphics primitives (points, lines, circles, etc.). It needs to be 4 | paired with a hardware-specific library for each display device we carry 5 | (to handle the lower-level functions). 6 | 7 | Adafruit invests time and resources providing this open source code, please 8 | support Adafruit & open-source hardware by purchasing products from Adafruit! 9 | 10 | Copyright (c) 2013 Adafruit Industries. All rights reserved. 11 | 12 | Redistribution and use in source and binary forms, with or without 13 | modification, are permitted provided that the following conditions are met: 14 | 15 | - Redistributions of source code must retain the above copyright notice, 16 | this list of conditions and the following disclaimer. 17 | - Redistributions in binary form must reproduce the above copyright notice, 18 | this list of conditions and the following disclaimer in the documentation 19 | and/or other materials provided with the distribution. 20 | 21 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 22 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 25 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 | POSSIBILITY OF SUCH DAMAGE. 32 | */ 33 | 34 | #include "../Adafruit_GFX/Adafruit_GFX.h" 35 | 36 | #include "../Adafruit_GFX/glcdfont.c" 37 | #ifdef __AVR__ 38 | #include 39 | #elif defined(ESP8266) 40 | #include 41 | #else 42 | #define pgm_read_byte(addr) (*(const unsigned char *)(addr)) 43 | #define pgm_read_word(addr) (*(const unsigned short *)(addr)) 44 | #endif 45 | 46 | #ifndef min 47 | #define min(a,b) (((a) < (b)) ? (a) : (b)) 48 | #endif 49 | 50 | Adafruit_GFX::Adafruit_GFX(int16_t w, int16_t h): 51 | WIDTH(w), HEIGHT(h) 52 | { 53 | _width = WIDTH; 54 | _height = HEIGHT; 55 | rotation = 0; 56 | cursor_y = cursor_x = 0; 57 | textsize = 1; 58 | textcolor = textbgcolor = 0xFFFF; 59 | wrap = true; 60 | _cp437 = false; 61 | gfxFont = NULL; 62 | } 63 | 64 | // Draw a circle outline 65 | void Adafruit_GFX::drawCircle(int16_t x0, int16_t y0, int16_t r, 66 | uint16_t color) { 67 | int16_t f = 1 - r; 68 | int16_t ddF_x = 1; 69 | int16_t ddF_y = -2 * r; 70 | int16_t x = 0; 71 | int16_t y = r; 72 | 73 | drawPixel(x0 , y0+r, color); 74 | drawPixel(x0 , y0-r, color); 75 | drawPixel(x0+r, y0 , color); 76 | drawPixel(x0-r, y0 , color); 77 | 78 | while (x= 0) { 80 | y--; 81 | ddF_y += 2; 82 | f += ddF_y; 83 | } 84 | x++; 85 | ddF_x += 2; 86 | f += ddF_x; 87 | 88 | drawPixel(x0 + x, y0 + y, color); 89 | drawPixel(x0 - x, y0 + y, color); 90 | drawPixel(x0 + x, y0 - y, color); 91 | drawPixel(x0 - x, y0 - y, color); 92 | drawPixel(x0 + y, y0 + x, color); 93 | drawPixel(x0 - y, y0 + x, color); 94 | drawPixel(x0 + y, y0 - x, color); 95 | drawPixel(x0 - y, y0 - x, color); 96 | } 97 | } 98 | 99 | void Adafruit_GFX::drawCircleHelper( int16_t x0, int16_t y0, 100 | int16_t r, uint8_t cornername, uint16_t color) { 101 | int16_t f = 1 - r; 102 | int16_t ddF_x = 1; 103 | int16_t ddF_y = -2 * r; 104 | int16_t x = 0; 105 | int16_t y = r; 106 | 107 | while (x= 0) { 109 | y--; 110 | ddF_y += 2; 111 | f += ddF_y; 112 | } 113 | x++; 114 | ddF_x += 2; 115 | f += ddF_x; 116 | if (cornername & 0x4) { 117 | drawPixel(x0 + x, y0 + y, color); 118 | drawPixel(x0 + y, y0 + x, color); 119 | } 120 | if (cornername & 0x2) { 121 | drawPixel(x0 + x, y0 - y, color); 122 | drawPixel(x0 + y, y0 - x, color); 123 | } 124 | if (cornername & 0x8) { 125 | drawPixel(x0 - y, y0 + x, color); 126 | drawPixel(x0 - x, y0 + y, color); 127 | } 128 | if (cornername & 0x1) { 129 | drawPixel(x0 - y, y0 - x, color); 130 | drawPixel(x0 - x, y0 - y, color); 131 | } 132 | } 133 | } 134 | 135 | void Adafruit_GFX::fillCircle(int16_t x0, int16_t y0, int16_t r, 136 | uint16_t color) { 137 | drawFastVLine(x0, y0-r, 2*r+1, color); 138 | fillCircleHelper(x0, y0, r, 3, 0, color); 139 | } 140 | 141 | // Used to do circles and roundrects 142 | void Adafruit_GFX::fillCircleHelper(int16_t x0, int16_t y0, int16_t r, 143 | uint8_t cornername, int16_t delta, uint16_t color) { 144 | 145 | int16_t f = 1 - r; 146 | int16_t ddF_x = 1; 147 | int16_t ddF_y = -2 * r; 148 | int16_t x = 0; 149 | int16_t y = r; 150 | 151 | while (x= 0) { 153 | y--; 154 | ddF_y += 2; 155 | f += ddF_y; 156 | } 157 | x++; 158 | ddF_x += 2; 159 | f += ddF_x; 160 | 161 | if (cornername & 0x1) { 162 | drawFastVLine(x0+x, y0-y, 2*y+1+delta, color); 163 | drawFastVLine(x0+y, y0-x, 2*x+1+delta, color); 164 | } 165 | if (cornername & 0x2) { 166 | drawFastVLine(x0-x, y0-y, 2*y+1+delta, color); 167 | drawFastVLine(x0-y, y0-x, 2*x+1+delta, color); 168 | } 169 | } 170 | } 171 | 172 | // Bresenham's algorithm - thx wikpedia 173 | void Adafruit_GFX::drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, 174 | uint16_t color) { 175 | int16_t steep = abs(y1 - y0) > abs(x1 - x0); 176 | if (steep) { 177 | adagfxswap(x0, y0); 178 | adagfxswap(x1, y1); 179 | } 180 | 181 | if (x0 > x1) { 182 | adagfxswap(x0, x1); 183 | adagfxswap(y0, y1); 184 | } 185 | 186 | int16_t dx, dy; 187 | dx = x1 - x0; 188 | dy = abs(y1 - y0); 189 | 190 | int16_t err = dx / 2; 191 | int16_t ystep; 192 | 193 | if (y0 < y1) { 194 | ystep = 1; 195 | } else { 196 | ystep = -1; 197 | } 198 | 199 | for (; x0<=x1; x0++) { 200 | if (steep) { 201 | drawPixel(y0, x0, color); 202 | } else { 203 | drawPixel(x0, y0, color); 204 | } 205 | err -= dy; 206 | if (err < 0) { 207 | y0 += ystep; 208 | err += dx; 209 | } 210 | } 211 | } 212 | 213 | // Draw a rectangle 214 | void Adafruit_GFX::drawRect(int16_t x, int16_t y, int16_t w, int16_t h, 215 | uint16_t color) { 216 | drawFastHLine(x, y, w, color); 217 | drawFastHLine(x, y+h-1, w, color); 218 | drawFastVLine(x, y, h, color); 219 | drawFastVLine(x+w-1, y, h, color); 220 | } 221 | 222 | void Adafruit_GFX::drawFastVLine(int16_t x, int16_t y, 223 | int16_t h, uint16_t color) { 224 | // Update in subclasses if desired! 225 | drawLine(x, y, x, y+h-1, color); 226 | } 227 | 228 | void Adafruit_GFX::drawFastHLine(int16_t x, int16_t y, 229 | int16_t w, uint16_t color) { 230 | // Update in subclasses if desired! 231 | drawLine(x, y, x+w-1, y, color); 232 | } 233 | 234 | void Adafruit_GFX::fillRect(int16_t x, int16_t y, int16_t w, int16_t h, 235 | uint16_t color) { 236 | // Update in subclasses if desired! 237 | for (int16_t i=x; i= y1 >= y0) 287 | if (y0 > y1) { 288 | adagfxswap(y0, y1); adagfxswap(x0, x1); 289 | } 290 | if (y1 > y2) { 291 | adagfxswap(y2, y1); adagfxswap(x2, x1); 292 | } 293 | if (y0 > y1) { 294 | adagfxswap(y0, y1); adagfxswap(x0, x1); 295 | } 296 | 297 | if(y0 == y2) { // Handle awkward all-on-same-line case as its own thing 298 | a = b = x0; 299 | if(x1 < a) a = x1; 300 | else if(x1 > b) b = x1; 301 | if(x2 < a) a = x2; 302 | else if(x2 > b) b = x2; 303 | drawFastHLine(a, y0, b-a+1, color); 304 | return; 305 | } 306 | 307 | int16_t 308 | dx01 = x1 - x0, 309 | dy01 = y1 - y0, 310 | dx02 = x2 - x0, 311 | dy02 = y2 - y0, 312 | dx12 = x2 - x1, 313 | dy12 = y2 - y1; 314 | int32_t 315 | sa = 0, 316 | sb = 0; 317 | 318 | // For upper part of triangle, find scanline crossings for segments 319 | // 0-1 and 0-2. If y1=y2 (flat-bottomed triangle), the scanline y1 320 | // is included here (and second loop will be skipped, avoiding a /0 321 | // error there), otherwise scanline y1 is skipped here and handled 322 | // in the second loop...which also avoids a /0 error here if y0=y1 323 | // (flat-topped triangle). 324 | if(y1 == y2) last = y1; // Include y1 scanline 325 | else last = y1-1; // Skip it 326 | 327 | for(y=y0; y<=last; y++) { 328 | a = x0 + sa / dy01; 329 | b = x0 + sb / dy02; 330 | sa += dx01; 331 | sb += dx02; 332 | /* longhand: 333 | a = x0 + (x1 - x0) * (y - y0) / (y1 - y0); 334 | b = x0 + (x2 - x0) * (y - y0) / (y2 - y0); 335 | */ 336 | if(a > b) adagfxswap(a,b); 337 | drawFastHLine(a, y, b-a+1, color); 338 | } 339 | 340 | // For lower part of triangle, find scanline crossings for segments 341 | // 0-2 and 1-2. This loop is skipped if y1=y2. 342 | sa = dx12 * (y - y1); 343 | sb = dx02 * (y - y0); 344 | for(; y<=y2; y++) { 345 | a = x1 + sa / dy12; 346 | b = x0 + sb / dy02; 347 | sa += dx12; 348 | sb += dx02; 349 | /* longhand: 350 | a = x1 + (x2 - x1) * (y - y1) / (y2 - y1); 351 | b = x0 + (x2 - x0) * (y - y0) / (y2 - y0); 352 | */ 353 | if(a > b) adagfxswap(a,b); 354 | drawFastHLine(a, y, b-a+1, color); 355 | } 356 | } 357 | 358 | // Draw a 1-bit image (bitmap) at the specified (x,y) position from the 359 | // provided bitmap buffer (must be PROGMEM memory) using the specified 360 | // foreground color (unset bits are transparent). 361 | void Adafruit_GFX::drawBitmap(int16_t x, int16_t y, 362 | const uint8_t *bitmap, int16_t w, int16_t h, uint16_t color) { 363 | 364 | int16_t i, j, byteWidth = (w + 7) / 8; 365 | uint8_t byte; 366 | 367 | for(j=0; j>= 1; 440 | else byte = pgm_read_byte(bitmap + j * byteWidth + i / 8); 441 | if(byte & 0x01) drawPixel(x+i, y+j, color); 442 | } 443 | } 444 | } 445 | 446 | #if ARDUINO >= 100 447 | size_t Adafruit_GFX::write(uint8_t c) { 448 | #else 449 | void Adafruit_GFX::write(uint8_t c) { 450 | #endif 451 | 452 | if(!gfxFont) { // 'Classic' built-in font 453 | 454 | if(c == '\n') { 455 | cursor_y += textsize*8; 456 | cursor_x = 0; 457 | } else if(c == '\r') { 458 | // skip em 459 | } else { 460 | if(wrap && ((cursor_x + textsize * 6) >= _width)) { // Heading off edge? 461 | cursor_x = 0; // Reset x to zero 462 | cursor_y += textsize * 8; // Advance y one line 463 | } 464 | drawChar(cursor_x, cursor_y, c, textcolor, textbgcolor, textsize); 465 | cursor_x += textsize * 6; 466 | } 467 | 468 | } else { // Custom font 469 | 470 | if(c == '\n') { 471 | cursor_x = 0; 472 | cursor_y += (int16_t)textsize * 473 | (uint8_t)pgm_read_byte(&gfxFont->yAdvance); 474 | } else if(c != '\r') { 475 | uint8_t first = pgm_read_byte(&gfxFont->first); 476 | if((c >= first) && (c <= (uint8_t)pgm_read_byte(&gfxFont->last))) { 477 | uint8_t c2 = c - pgm_read_byte(&gfxFont->first); 478 | GFXglyph *glyph = &(((GFXglyph *)pgm_read_word(&gfxFont->glyph))[c2]); 479 | uint8_t w = pgm_read_byte(&glyph->width), 480 | h = pgm_read_byte(&glyph->height); 481 | if((w > 0) && (h > 0)) { // Is there an associated bitmap? 482 | int16_t xo = (int8_t)pgm_read_byte(&glyph->xOffset); // sic 483 | if(wrap && ((cursor_x + textsize * (xo + w)) >= _width)) { 484 | // Drawing character would go off right edge; wrap to new line 485 | cursor_x = 0; 486 | cursor_y += (int16_t)textsize * 487 | (uint8_t)pgm_read_byte(&gfxFont->yAdvance); 488 | } 489 | drawChar(cursor_x, cursor_y, c, textcolor, textbgcolor, textsize); 490 | } 491 | cursor_x += pgm_read_byte(&glyph->xAdvance) * (int16_t)textsize; 492 | } 493 | } 494 | 495 | } 496 | #if ARDUINO >= 100 497 | return 1; 498 | #endif 499 | } 500 | 501 | // Draw a character 502 | void Adafruit_GFX::drawChar(int16_t x, int16_t y, unsigned char c, 503 | uint16_t color, uint16_t bg, uint8_t size) { 504 | 505 | if(!gfxFont) { // 'Classic' built-in font 506 | 507 | if((x >= _width) || // Clip right 508 | (y >= _height) || // Clip bottom 509 | ((x + 6 * size - 1) < 0) || // Clip left 510 | ((y + 8 * size - 1) < 0)) // Clip top 511 | return; 512 | 513 | if(!_cp437 && (c >= 176)) c++; // Handle 'classic' charset behavior 514 | 515 | for(int8_t i=0; i<6; i++ ) { 516 | uint8_t line; 517 | if(i < 5) line = pgm_read_byte(font+(c*5)+i); 518 | else line = 0x0; 519 | for(int8_t j=0; j<8; j++, line >>= 1) { 520 | if(line & 0x1) { 521 | if(size == 1) drawPixel(x+i, y+j, color); 522 | else fillRect(x+(i*size), y+(j*size), size, size, color); 523 | } else if(bg != color) { 524 | if(size == 1) drawPixel(x+i, y+j, bg); 525 | else fillRect(x+i*size, y+j*size, size, size, bg); 526 | } 527 | } 528 | } 529 | 530 | } else { // Custom font 531 | 532 | // Character is assumed previously filtered by write() to eliminate 533 | // newlines, returns, non-printable characters, etc. Calling drawChar() 534 | // directly with 'bad' characters of font may cause mayhem! 535 | 536 | c -= pgm_read_byte(&gfxFont->first); 537 | GFXglyph *glyph = &(((GFXglyph *)pgm_read_word(&gfxFont->glyph))[c]); 538 | uint8_t *bitmap = (uint8_t *)pgm_read_word(&gfxFont->bitmap); 539 | 540 | uint16_t bo = pgm_read_word(&glyph->bitmapOffset); 541 | uint8_t w = pgm_read_byte(&glyph->width), 542 | h = pgm_read_byte(&glyph->height), 543 | xa = pgm_read_byte(&glyph->xAdvance); 544 | int8_t xo = pgm_read_byte(&glyph->xOffset), 545 | yo = pgm_read_byte(&glyph->yOffset); 546 | uint8_t xx, yy, bits, bit = 0; 547 | int16_t xo16, yo16; 548 | 549 | if(size > 1) { 550 | xo16 = xo; 551 | yo16 = yo; 552 | } 553 | 554 | // Todo: Add character clipping here 555 | 556 | // NOTE: THERE IS NO 'BACKGROUND' COLOR OPTION ON CUSTOM FONTS. 557 | // THIS IS ON PURPOSE AND BY DESIGN. The background color feature 558 | // has typically been used with the 'classic' font to overwrite old 559 | // screen contents with new data. This ONLY works because the 560 | // characters are a uniform size; it's not a sensible thing to do with 561 | // proportionally-spaced fonts with glyphs of varying sizes (and that 562 | // may overlap). To replace previously-drawn text when using a custom 563 | // font, use the getTextBounds() function to determine the smallest 564 | // rectangle encompassing a string, erase the area with fillRect(), 565 | // then draw new text. This WILL infortunately 'blink' the text, but 566 | // is unavoidable. Drawing 'background' pixels will NOT fix this, 567 | // only creates a new set of problems. Have an idea to work around 568 | // this (a canvas object type for MCUs that can afford the RAM and 569 | // displays supporting setAddrWindow() and pushColors()), but haven't 570 | // implemented this yet. 571 | 572 | for(yy=0; yy 0) ? s : 1; 606 | } 607 | 608 | void Adafruit_GFX::setTextColor(uint16_t c) { 609 | // For 'transparent' background, we'll set the bg 610 | // to the same as fg instead of using a flag 611 | textcolor = textbgcolor = c; 612 | } 613 | 614 | void Adafruit_GFX::setTextColor(uint16_t c, uint16_t b) { 615 | textcolor = c; 616 | textbgcolor = b; 617 | } 618 | 619 | void Adafruit_GFX::setTextWrap(boolean w) { 620 | wrap = w; 621 | } 622 | 623 | uint8_t Adafruit_GFX::getRotation(void) const { 624 | return rotation; 625 | } 626 | 627 | void Adafruit_GFX::setRotation(uint8_t x) { 628 | rotation = (x & 3); 629 | switch(rotation) { 630 | case 0: 631 | case 2: 632 | _width = WIDTH; 633 | _height = HEIGHT; 634 | break; 635 | case 1: 636 | case 3: 637 | _width = HEIGHT; 638 | _height = WIDTH; 639 | break; 640 | } 641 | } 642 | 643 | // Enable (or disable) Code Page 437-compatible charset. 644 | // There was an error in glcdfont.c for the longest time -- one character 645 | // (#176, the 'light shade' block) was missing -- this threw off the index 646 | // of every character that followed it. But a TON of code has been written 647 | // with the erroneous character indices. By default, the library uses the 648 | // original 'wrong' behavior and old sketches will still work. Pass 'true' 649 | // to this function to use correct CP437 character values in your code. 650 | void Adafruit_GFX::cp437(boolean x) { 651 | _cp437 = x; 652 | } 653 | 654 | void Adafruit_GFX::setFont(const GFXfont *f) { 655 | if(f) { // Font struct pointer passed in? 656 | if(!gfxFont) { // And no current font struct? 657 | // Switching from classic to new font behavior. 658 | // Move cursor pos down 6 pixels so it's on baseline. 659 | cursor_y += 6; 660 | } 661 | } else if(gfxFont) { // NULL passed. Current font struct defined? 662 | // Switching from new to classic font behavior. 663 | // Move cursor pos up 6 pixels so it's at top-left of char. 664 | cursor_y -= 6; 665 | } 666 | gfxFont = (GFXfont *)f; 667 | } 668 | 669 | // Pass string and a cursor position, returns UL corner and W,H. 670 | void Adafruit_GFX::getTextBounds(char *str, int16_t x, int16_t y, 671 | int16_t *x1, int16_t *y1, uint16_t *w, uint16_t *h) { 672 | uint8_t c; // Current character 673 | 674 | *x1 = x; 675 | *y1 = y; 676 | *w = *h = 0; 677 | 678 | if(gfxFont) { 679 | 680 | GFXglyph *glyph; 681 | uint8_t first = pgm_read_byte(&gfxFont->first), 682 | last = pgm_read_byte(&gfxFont->last), 683 | gw, gh, xa; 684 | int8_t xo, yo; 685 | int16_t minx = _width, miny = _height, maxx = -1, maxy = -1, 686 | gx1, gy1, gx2, gy2, ts = (int16_t)textsize, 687 | ya = ts * (uint8_t)pgm_read_byte(&gfxFont->yAdvance); 688 | 689 | while((c = *str++)) { 690 | if(c != '\n') { // Not a newline 691 | if(c != '\r') { // Not a carriage return, is normal char 692 | if((c >= first) && (c <= last)) { // Char present in current font 693 | c -= first; 694 | glyph = &(((GFXglyph *)pgm_read_word(&gfxFont->glyph))[c]); 695 | gw = pgm_read_byte(&glyph->width); 696 | gh = pgm_read_byte(&glyph->height); 697 | xa = pgm_read_byte(&glyph->xAdvance); 698 | xo = pgm_read_byte(&glyph->xOffset); 699 | yo = pgm_read_byte(&glyph->yOffset); 700 | if(wrap && ((x + (((int16_t)xo + gw) * ts)) >= _width)) { 701 | // Line wrap 702 | x = 0; // Reset x to 0 703 | y += ya; // Advance y by 1 line 704 | } 705 | gx1 = x + xo * ts; 706 | gy1 = y + yo * ts; 707 | gx2 = gx1 + gw * ts - 1; 708 | gy2 = gy1 + gh * ts - 1; 709 | if(gx1 < minx) minx = gx1; 710 | if(gy1 < miny) miny = gy1; 711 | if(gx2 > maxx) maxx = gx2; 712 | if(gy2 > maxy) maxy = gy2; 713 | x += xa * ts; 714 | } 715 | } // Carriage return = do nothing 716 | } else { // Newline 717 | x = 0; // Reset x 718 | y += ya; // Advance y by 1 line 719 | } 720 | } 721 | // End of string 722 | *x1 = minx; 723 | *y1 = miny; 724 | if(maxx >= minx) *w = maxx - minx + 1; 725 | if(maxy >= miny) *h = maxy - miny + 1; 726 | 727 | } else { // Default font 728 | 729 | uint16_t lineWidth = 0, maxWidth = 0; // Width of current, all lines 730 | 731 | while((c = *str++)) { 732 | if(c != '\n') { // Not a newline 733 | if(c != '\r') { // Not a carriage return, is normal char 734 | if(wrap && ((x + textsize * 6) >= _width)) { 735 | x = 0; // Reset x to 0 736 | y += textsize * 8; // Advance y by 1 line 737 | if(lineWidth > maxWidth) maxWidth = lineWidth; // Save widest line 738 | lineWidth = textsize * 6; // First char on new line 739 | } else { // No line wrap, just keep incrementing X 740 | lineWidth += textsize * 6; // Includes interchar x gap 741 | } 742 | } // Carriage return = do nothing 743 | } else { // Newline 744 | x = 0; // Reset x to 0 745 | y += textsize * 8; // Advance y by 1 line 746 | if(lineWidth > maxWidth) maxWidth = lineWidth; // Save widest line 747 | lineWidth = 0; // Reset lineWidth for new line 748 | } 749 | } 750 | // End of string 751 | if(lineWidth) y += textsize * 8; // Add height of last (or only) line 752 | *w = maxWidth - 1; // Don't include last interchar x gap 753 | *h = y - *y1; 754 | 755 | } // End classic vs custom font 756 | } 757 | 758 | // Same as above, but for PROGMEM strings 759 | void Adafruit_GFX::getTextBounds(const __FlashStringHelper *str, 760 | int16_t x, int16_t y, int16_t *x1, int16_t *y1, uint16_t *w, uint16_t *h) { 761 | uint8_t *s = (uint8_t *)str, c; 762 | 763 | *x1 = x; 764 | *y1 = y; 765 | *w = *h = 0; 766 | 767 | if(gfxFont) { 768 | 769 | GFXglyph *glyph; 770 | uint8_t first = pgm_read_byte(&gfxFont->first), 771 | last = pgm_read_byte(&gfxFont->last), 772 | gw, gh, xa; 773 | int8_t xo, yo; 774 | int16_t minx = _width, miny = _height, maxx = -1, maxy = -1, 775 | gx1, gy1, gx2, gy2, ts = (int16_t)textsize, 776 | ya = ts * (uint8_t)pgm_read_byte(&gfxFont->yAdvance); 777 | 778 | while((c = pgm_read_byte(s++))) { 779 | if(c != '\n') { // Not a newline 780 | if(c != '\r') { // Not a carriage return, is normal char 781 | if((c >= first) && (c <= last)) { // Char present in current font 782 | c -= first; 783 | glyph = &(((GFXglyph *)pgm_read_word(&gfxFont->glyph))[c]); 784 | gw = pgm_read_byte(&glyph->width); 785 | gh = pgm_read_byte(&glyph->height); 786 | xa = pgm_read_byte(&glyph->xAdvance); 787 | xo = pgm_read_byte(&glyph->xOffset); 788 | yo = pgm_read_byte(&glyph->yOffset); 789 | if(wrap && ((x + (((int16_t)xo + gw) * ts)) >= _width)) { 790 | // Line wrap 791 | x = 0; // Reset x to 0 792 | y += ya; // Advance y by 1 line 793 | } 794 | gx1 = x + xo * ts; 795 | gy1 = y + yo * ts; 796 | gx2 = gx1 + gw * ts - 1; 797 | gy2 = gy1 + gh * ts - 1; 798 | if(gx1 < minx) minx = gx1; 799 | if(gy1 < miny) miny = gy1; 800 | if(gx2 > maxx) maxx = gx2; 801 | if(gy2 > maxy) maxy = gy2; 802 | x += xa * ts; 803 | } 804 | } // Carriage return = do nothing 805 | } else { // Newline 806 | x = 0; // Reset x 807 | y += ya; // Advance y by 1 line 808 | } 809 | } 810 | // End of string 811 | *x1 = minx; 812 | *y1 = miny; 813 | if(maxx >= minx) *w = maxx - minx + 1; 814 | if(maxy >= miny) *h = maxy - miny + 1; 815 | 816 | } else { // Default font 817 | 818 | uint16_t lineWidth = 0, maxWidth = 0; // Width of current, all lines 819 | 820 | while((c = pgm_read_byte(s++))) { 821 | if(c != '\n') { // Not a newline 822 | if(c != '\r') { // Not a carriage return, is normal char 823 | if(wrap && ((x + textsize * 6) >= _width)) { 824 | x = 0; // Reset x to 0 825 | y += textsize * 8; // Advance y by 1 line 826 | if(lineWidth > maxWidth) maxWidth = lineWidth; // Save widest line 827 | lineWidth = textsize * 6; // First char on new line 828 | } else { // No line wrap, just keep incrementing X 829 | lineWidth += textsize * 6; // Includes interchar x gap 830 | } 831 | } // Carriage return = do nothing 832 | } else { // Newline 833 | x = 0; // Reset x to 0 834 | y += textsize * 8; // Advance y by 1 line 835 | if(lineWidth > maxWidth) maxWidth = lineWidth; // Save widest line 836 | lineWidth = 0; // Reset lineWidth for new line 837 | } 838 | } 839 | // End of string 840 | if(lineWidth) y += textsize * 8; // Add height of last (or only) line 841 | *w = maxWidth - 1; // Don't include last interchar x gap 842 | *h = y - *y1; 843 | 844 | } // End classic vs custom font 845 | } 846 | 847 | // Return the size of the display (per current rotation) 848 | int16_t Adafruit_GFX::width(void) const { 849 | return _width; 850 | } 851 | 852 | int16_t Adafruit_GFX::height(void) const { 853 | return _height; 854 | } 855 | 856 | void Adafruit_GFX::invertDisplay(boolean i) { 857 | // Do nothing, must be subclassed if supported by hardware 858 | } 859 | 860 | /***************************************************************************/ 861 | // code for the GFX button UI element 862 | 863 | Adafruit_GFX_Button::Adafruit_GFX_Button(void) { 864 | _gfx = 0; 865 | } 866 | 867 | void Adafruit_GFX_Button::initButton( 868 | Adafruit_GFX *gfx, int16_t x, int16_t y, uint8_t w, uint8_t h, 869 | uint16_t outline, uint16_t fill, uint16_t textcolor, 870 | char *label, uint8_t textsize) 871 | { 872 | _x = x; 873 | _y = y; 874 | _w = w; 875 | _h = h; 876 | _outlinecolor = outline; 877 | _fillcolor = fill; 878 | _textcolor = textcolor; 879 | _textsize = textsize; 880 | _gfx = gfx; 881 | strncpy(_label, label, 9); 882 | _label[9] = 0; 883 | } 884 | 885 | void Adafruit_GFX_Button::drawButton(boolean inverted) { 886 | uint16_t fill, outline, text; 887 | 888 | if(!inverted) { 889 | fill = _fillcolor; 890 | outline = _outlinecolor; 891 | text = _textcolor; 892 | } else { 893 | fill = _textcolor; 894 | outline = _outlinecolor; 895 | text = _fillcolor; 896 | } 897 | 898 | _gfx->fillRoundRect(_x - (_w/2), _y - (_h/2), _w, _h, min(_w,_h)/4, fill); 899 | _gfx->drawRoundRect(_x - (_w/2), _y - (_h/2), _w, _h, min(_w,_h)/4, outline); 900 | 901 | _gfx->setCursor(_x - strlen(_label)*3*_textsize, _y-4*_textsize); 902 | _gfx->setTextColor(text); 903 | _gfx->setTextSize(_textsize); 904 | _gfx->print(_label); 905 | } 906 | 907 | boolean Adafruit_GFX_Button::contains(int16_t x, int16_t y) { 908 | if ((x < (_x - _w/2)) || (x > (_x + _w/2))) return false; 909 | if ((y < (_y - _h/2)) || (y > (_y + _h/2))) return false; 910 | return true; 911 | } 912 | 913 | void Adafruit_GFX_Button::press(boolean p) { 914 | laststate = currstate; 915 | currstate = p; 916 | } 917 | 918 | boolean Adafruit_GFX_Button::isPressed() { return currstate; } 919 | boolean Adafruit_GFX_Button::justPressed() { return (currstate && !laststate); } 920 | boolean Adafruit_GFX_Button::justReleased() { return (!currstate && laststate); } 921 | 922 | // ------------------------------------------------------------------------- 923 | 924 | // GFXcanvas1 and GFXcanvas16 (currently a WIP, don't get too comfy with the 925 | // implementation) provide 1- and 16-bit offscreen canvases, the address of 926 | // which can be passed to drawBitmap() or pushColors() (the latter appears 927 | // to only be in Adafruit_TFTLCD at this time). This is here mostly to 928 | // help with the recently-added proportionally-spaced fonts; adds a way to 929 | // refresh a section of the screen without a massive flickering clear-and- 930 | // redraw...but maybe you'll find other uses too. VERY RAM-intensive, since 931 | // the buffer is in MCU memory and not the display driver...GXFcanvas1 might 932 | // be minimally useful on an Uno-class board, but this and GFXcanvas16 are 933 | // much more likely to require at least a Mega or various recent ARM-type 934 | // boards (recomment, as the text+bitmap draw can be pokey). GFXcanvas1 935 | // requires 1 bit per pixel (rounded up to nearest byte per scanline), 936 | // GFXcanvas16 requires 2 bytes per pixel (no scanline pad). 937 | // NOT EXTENSIVELY TESTED YET. MAY CONTAIN WORST BUGS KNOWN TO HUMANKIND. 938 | 939 | GFXcanvas1::GFXcanvas1(uint16_t w, uint16_t h) : Adafruit_GFX(w, h) { 940 | uint16_t bytes = ((w + 7) / 8) * h; 941 | if((buffer = (uint8_t *)malloc(bytes))) { 942 | memset(buffer, 0, bytes); 943 | } 944 | } 945 | 946 | GFXcanvas1::~GFXcanvas1(void) { 947 | if(buffer) free(buffer); 948 | } 949 | 950 | uint8_t* GFXcanvas1::getBuffer(void) { 951 | return buffer; 952 | } 953 | 954 | void GFXcanvas1::drawPixel(int16_t x, int16_t y, uint16_t color) { 955 | // Bitmask tables of 0x80>>X and ~(0x80>>X), because X>>Y is slow on AVR 956 | static const uint8_t PROGMEM 957 | GFXsetBit[] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 }, 958 | GFXclrBit[] = { 0x7F, 0xBF, 0xDF, 0xEF, 0xF7, 0xFB, 0xFD, 0xFE }; 959 | 960 | if(buffer) { 961 | if((x < 0) || (y < 0) || (x >= _width) || (y >= _height)) return; 962 | 963 | int16_t t; 964 | switch(rotation) { 965 | case 1: 966 | t = x; 967 | x = WIDTH - 1 - y; 968 | y = t; 969 | break; 970 | case 2: 971 | x = WIDTH - 1 - x; 972 | y = HEIGHT - 1 - y; 973 | break; 974 | case 3: 975 | t = x; 976 | x = y; 977 | y = HEIGHT - 1 - t; 978 | break; 979 | } 980 | 981 | uint8_t *ptr = &buffer[(x / 8) + y * ((WIDTH + 7) / 8)]; 982 | if(color) *ptr |= pgm_read_byte(&GFXsetBit[x & 7]); 983 | else *ptr &= pgm_read_byte(&GFXclrBit[x & 7]); 984 | } 985 | } 986 | 987 | void GFXcanvas1::fillScreen(uint16_t color) { 988 | if(buffer) { 989 | uint16_t bytes = ((WIDTH + 7) / 8) * HEIGHT; 990 | memset(buffer, color ? 0xFF : 0x00, bytes); 991 | } 992 | } 993 | 994 | GFXcanvas16::GFXcanvas16(uint16_t w, uint16_t h) : Adafruit_GFX(w, h) { 995 | uint16_t bytes = w * h * 2; 996 | if((buffer = (uint16_t *)malloc(bytes))) { 997 | memset(buffer, 0, bytes); 998 | } 999 | } 1000 | 1001 | GFXcanvas16::~GFXcanvas16(void) { 1002 | if(buffer) free(buffer); 1003 | } 1004 | 1005 | uint16_t* GFXcanvas16::getBuffer(void) { 1006 | return buffer; 1007 | } 1008 | 1009 | void GFXcanvas16::drawPixel(int16_t x, int16_t y, uint16_t color) { 1010 | if(buffer) { 1011 | if((x < 0) || (y < 0) || (x >= _width) || (y >= _height)) return; 1012 | 1013 | int16_t t; 1014 | switch(rotation) { 1015 | case 1: 1016 | t = x; 1017 | x = WIDTH - 1 - y; 1018 | y = t; 1019 | break; 1020 | case 2: 1021 | x = WIDTH - 1 - x; 1022 | y = HEIGHT - 1 - y; 1023 | break; 1024 | case 3: 1025 | t = x; 1026 | x = y; 1027 | y = HEIGHT - 1 - t; 1028 | break; 1029 | } 1030 | 1031 | buffer[x + y * WIDTH] = color; 1032 | } 1033 | } 1034 | 1035 | void GFXcanvas16::fillScreen(uint16_t color) { 1036 | if(buffer) { 1037 | uint8_t hi = color >> 8, lo = color & 0xFF; 1038 | if(hi == lo) { 1039 | memset(buffer, lo, WIDTH * HEIGHT * 2); 1040 | } else { 1041 | uint16_t i, pixels = WIDTH * HEIGHT; 1042 | for(i=0; i= 100 5 | #include "Arduino.h" 6 | #include "Print.h" 7 | #else 8 | #include "WProgram.h" 9 | #endif 10 | 11 | #include "../Adafruit_GFX/gfxfont.h" 12 | 13 | #define adagfxswap(a, b) { int16_t t = a; a = b; b = t; } 14 | 15 | 16 | #define swap(a, b) adagfxswap(a, b) 17 | 18 | 19 | class Adafruit_GFX : public Print { 20 | 21 | public: 22 | 23 | Adafruit_GFX(int16_t w, int16_t h); // Constructor 24 | 25 | // This MUST be defined by the subclass: 26 | virtual void drawPixel(int16_t x, int16_t y, uint16_t color) = 0; 27 | 28 | // These MAY be overridden by the subclass to provide device-specific 29 | // optimized code. Otherwise 'generic' versions are used. 30 | virtual void 31 | drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color), 32 | drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color), 33 | drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color), 34 | drawRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color), 35 | fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color), 36 | fillScreen(uint16_t color), 37 | invertDisplay(boolean i); 38 | 39 | // These exist only with Adafruit_GFX (no subclass overrides) 40 | void 41 | drawCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color), 42 | drawCircleHelper(int16_t x0, int16_t y0, int16_t r, uint8_t cornername, 43 | uint16_t color), 44 | fillCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color), 45 | fillCircleHelper(int16_t x0, int16_t y0, int16_t r, uint8_t cornername, 46 | int16_t delta, uint16_t color), 47 | drawTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, 48 | int16_t x2, int16_t y2, uint16_t color), 49 | fillTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, 50 | int16_t x2, int16_t y2, uint16_t color), 51 | drawRoundRect(int16_t x0, int16_t y0, int16_t w, int16_t h, 52 | int16_t radius, uint16_t color), 53 | fillRoundRect(int16_t x0, int16_t y0, int16_t w, int16_t h, 54 | int16_t radius, uint16_t color), 55 | drawBitmap(int16_t x, int16_t y, const uint8_t *bitmap, 56 | int16_t w, int16_t h, uint16_t color), 57 | drawBitmap(int16_t x, int16_t y, const uint8_t *bitmap, 58 | int16_t w, int16_t h, uint16_t color, uint16_t bg), 59 | drawBitmap(int16_t x, int16_t y, uint8_t *bitmap, 60 | int16_t w, int16_t h, uint16_t color), 61 | drawBitmap(int16_t x, int16_t y, uint8_t *bitmap, 62 | int16_t w, int16_t h, uint16_t color, uint16_t bg), 63 | drawXBitmap(int16_t x, int16_t y, const uint8_t *bitmap, 64 | int16_t w, int16_t h, uint16_t color), 65 | drawChar(int16_t x, int16_t y, unsigned char c, uint16_t color, 66 | uint16_t bg, uint8_t size), 67 | setCursor(int16_t x, int16_t y), 68 | setTextColor(uint16_t c), 69 | setTextColor(uint16_t c, uint16_t bg), 70 | setTextSize(uint8_t s), 71 | setTextWrap(boolean w), 72 | setRotation(uint8_t r), 73 | cp437(boolean x=true), 74 | setFont(const GFXfont *f = NULL), 75 | getTextBounds(char *string, int16_t x, int16_t y, 76 | int16_t *x1, int16_t *y1, uint16_t *w, uint16_t *h), 77 | getTextBounds(const __FlashStringHelper *s, int16_t x, int16_t y, 78 | int16_t *x1, int16_t *y1, uint16_t *w, uint16_t *h); 79 | 80 | #if ARDUINO >= 100 81 | virtual size_t write(uint8_t); 82 | #else 83 | virtual void write(uint8_t); 84 | #endif 85 | 86 | int16_t height(void) const; 87 | int16_t width(void) const; 88 | 89 | uint8_t getRotation(void) const; 90 | 91 | // get current cursor position (get rotation safe maximum values, using: width() for x, height() for y) 92 | int16_t getCursorX(void) const; 93 | int16_t getCursorY(void) const; 94 | 95 | protected: 96 | const int16_t 97 | WIDTH, HEIGHT; // This is the 'raw' display w/h - never changes 98 | int16_t 99 | _width, _height, // Display w/h as modified by current rotation 100 | cursor_x, cursor_y; 101 | uint16_t 102 | textcolor, textbgcolor; 103 | uint8_t 104 | textsize, 105 | rotation; 106 | boolean 107 | wrap, // If set, 'wrap' text at right edge of display 108 | _cp437; // If set, use correct CP437 charset (default is off) 109 | GFXfont 110 | *gfxFont; 111 | }; 112 | 113 | class Adafruit_GFX_Button { 114 | 115 | public: 116 | Adafruit_GFX_Button(void); 117 | void initButton(Adafruit_GFX *gfx, int16_t x, int16_t y, 118 | uint8_t w, uint8_t h, uint16_t outline, uint16_t fill, 119 | uint16_t textcolor, char *label, uint8_t textsize); 120 | void drawButton(boolean inverted = false); 121 | boolean contains(int16_t x, int16_t y); 122 | 123 | void press(boolean p); 124 | boolean isPressed(); 125 | boolean justPressed(); 126 | boolean justReleased(); 127 | 128 | private: 129 | Adafruit_GFX *_gfx; 130 | int16_t _x, _y; 131 | uint16_t _w, _h; 132 | uint8_t _textsize; 133 | uint16_t _outlinecolor, _fillcolor, _textcolor; 134 | char _label[10]; 135 | 136 | boolean currstate, laststate; 137 | }; 138 | 139 | class GFXcanvas1 : public Adafruit_GFX { 140 | 141 | public: 142 | GFXcanvas1(uint16_t w, uint16_t h); 143 | ~GFXcanvas1(void); 144 | void drawPixel(int16_t x, int16_t y, uint16_t color), 145 | fillScreen(uint16_t color); 146 | uint8_t *getBuffer(void); 147 | private: 148 | uint8_t *buffer; 149 | }; 150 | 151 | class GFXcanvas16 : public Adafruit_GFX { 152 | GFXcanvas16(uint16_t w, uint16_t h); 153 | ~GFXcanvas16(void); 154 | void drawPixel(int16_t x, int16_t y, uint16_t color), 155 | fillScreen(uint16_t color); 156 | uint16_t *getBuffer(void); 157 | private: 158 | uint16_t *buffer; 159 | }; 160 | 161 | #endif // _ADAFRUIT_GFX_H 162 | -------------------------------------------------------------------------------- /lib/Adafruit_GFX/README.md: -------------------------------------------------------------------------------- 1 | # Adafruit GFX Library 2 | 3 | This is the core graphics library for all our displays, providing a common set of graphics primitives (points, lines, circles, etc.). It needs to be paired with a hardware-specific library for each display device we carry (to handle the lower-level functions). 4 | 5 | Adafruit invests time and resources providing this open source code, please support Adafruit and open-source hardware by purchasing products from Adafruit! 6 | 7 | Written by Limor Fried/Ladyada for Adafruit Industries. 8 | BSD license, check license.txt for more information. 9 | All text above must be included in any redistribution. 10 | 11 | Recent Arduino IDE releases include the Library Manager for easy installation. Otherwise, to download, click the DOWNLOAD ZIP button, uncompress and rename the uncompressed folder Adafruit_GFX. Confirm that the Adafruit_GFX folder contains Adafruit_GFX.cpp and Adafruit_GFX.h. Place the Adafruit_GFX library folder your /Libraries/ folder. You may need to create the Libraries subfolder if its your first library. Restart the IDE. 12 | 13 | # Useful Resources 14 | 15 | - Image2Code: This is a handy Java GUI utility to convert a BMP file into the array code necessary to display the image with the drawBitmap function. Check out the code at ehubin's GitHub repository: https://github.com/ehubin/Adafruit-GFX-Library/tree/master/Img2Code 16 | 17 | - drawXBitmap function: You can use the GIMP photo editor to save a .xbm file and use the array saved in the file to draw a bitmap with the drawXBitmap function. See the pull request here for more details: https://github.com/adafruit/Adafruit-GFX-Library/pull/31 18 | 19 | - 'Fonts' folder contains bitmap fonts for use with recent (1.1 and later) Adafruit_GFX. To use a font in your Arduino sketch, #include the corresponding .h file and pass address of GFXfont struct to setFont(). Pass NULL to revert to 'classic' fixed-space bitmap font. 20 | 21 | - 'fontconvert' folder contains a command-line tool for converting TTF fonts to Adafruit_GFX .h format. 22 | -------------------------------------------------------------------------------- /lib/Adafruit_GFX/ft2build.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************/ 2 | /* */ 3 | /* ft2build.h */ 4 | /* */ 5 | /* FreeType 2 build and setup macros. */ 6 | /* */ 7 | /* Copyright 1996-2015 by */ 8 | /* David Turner, Robert Wilhelm, and Werner Lemberg. */ 9 | /* */ 10 | /* This file is part of the FreeType project, and may only be used, */ 11 | /* modified, and distributed under the terms of the FreeType project */ 12 | /* license, LICENSE.TXT. By continuing to use, modify, or distribute */ 13 | /* this file you indicate that you have read the license and */ 14 | /* understand and accept it fully. */ 15 | /* */ 16 | /***************************************************************************/ 17 | 18 | 19 | /*************************************************************************/ 20 | /* */ 21 | /* This is the `entry point' for FreeType header file inclusions. It is */ 22 | /* the only header file which should be included directly; all other */ 23 | /* FreeType header files should be accessed with macro names (after */ 24 | /* including `ft2build.h'). */ 25 | /* */ 26 | /* A typical example is */ 27 | /* */ 28 | /* #include */ 29 | /* #include FT_FREETYPE_H */ 30 | /* */ 31 | /*************************************************************************/ 32 | 33 | 34 | #ifndef __FT2BUILD_H__ 35 | #define __FT2BUILD_H__ 36 | 37 | #include 38 | 39 | #endif /* __FT2BUILD_H__ */ 40 | 41 | 42 | /* END */ 43 | -------------------------------------------------------------------------------- /lib/Adafruit_GFX/gfxfont.h: -------------------------------------------------------------------------------- 1 | // Font structures for newer Adafruit_GFX (1.1 and later). 2 | // Example fonts are included in 'Fonts' directory. 3 | // To use a font in your Arduino sketch, #include the corresponding .h 4 | // file and pass address of GFXfont struct to setFont(). Pass NULL to 5 | // revert to 'classic' fixed-space bitmap font. 6 | 7 | #ifndef _GFXFONT_H_ 8 | #define _GFXFONT_H_ 9 | 10 | typedef struct { // Data stored PER GLYPH 11 | uint16_t bitmapOffset; // Pointer into GFXfont->bitmap 12 | uint8_t width, height; // Bitmap dimensions in pixels 13 | uint8_t xAdvance; // Distance to advance cursor (x axis) 14 | int8_t xOffset, yOffset; // Dist from cursor pos to UL corner 15 | } GFXglyph; 16 | 17 | typedef struct { // Data stored for FONT AS A WHOLE: 18 | uint8_t *bitmap; // Glyph bitmaps, concatenated 19 | GFXglyph *glyph; // Glyph array 20 | uint8_t first, last; // ASCII extents 21 | uint8_t yAdvance; // Newline distance (y axis) 22 | } GFXfont; 23 | 24 | #endif // _GFXFONT_H_ 25 | -------------------------------------------------------------------------------- /lib/Adafruit_GFX/glcdfont.c: -------------------------------------------------------------------------------- 1 | // This is the 'classic' fixed-space bitmap font for Adafruit_GFX since 1.0. 2 | // See gfxfont.h for newer custom bitmap font info. 3 | 4 | #ifndef FONT5X7_H 5 | #define FONT5X7_H 6 | 7 | #ifdef __AVR__ 8 | #include 9 | #include 10 | #elif defined(ESP8266) 11 | #include 12 | #else 13 | #define PROGMEM 14 | #endif 15 | 16 | // Standard ASCII 5x7 font 17 | 18 | static const unsigned char font[] PROGMEM = { 19 | 0x00, 0x00, 0x00, 0x00, 0x00, 20 | 0x3E, 0x5B, 0x4F, 0x5B, 0x3E, 21 | 0x3E, 0x6B, 0x4F, 0x6B, 0x3E, 22 | 0x1C, 0x3E, 0x7C, 0x3E, 0x1C, 23 | 0x18, 0x3C, 0x7E, 0x3C, 0x18, 24 | 0x1C, 0x57, 0x7D, 0x57, 0x1C, 25 | 0x1C, 0x5E, 0x7F, 0x5E, 0x1C, 26 | 0x00, 0x18, 0x3C, 0x18, 0x00, 27 | 0xFF, 0xE7, 0xC3, 0xE7, 0xFF, 28 | 0x00, 0x18, 0x24, 0x18, 0x00, 29 | 0xFF, 0xE7, 0xDB, 0xE7, 0xFF, 30 | 0x30, 0x48, 0x3A, 0x06, 0x0E, 31 | 0x26, 0x29, 0x79, 0x29, 0x26, 32 | 0x40, 0x7F, 0x05, 0x05, 0x07, 33 | 0x40, 0x7F, 0x05, 0x25, 0x3F, 34 | 0x5A, 0x3C, 0xE7, 0x3C, 0x5A, 35 | 0x7F, 0x3E, 0x1C, 0x1C, 0x08, 36 | 0x08, 0x1C, 0x1C, 0x3E, 0x7F, 37 | 0x14, 0x22, 0x7F, 0x22, 0x14, 38 | 0x5F, 0x5F, 0x00, 0x5F, 0x5F, 39 | 0x06, 0x09, 0x7F, 0x01, 0x7F, 40 | 0x00, 0x66, 0x89, 0x95, 0x6A, 41 | 0x60, 0x60, 0x60, 0x60, 0x60, 42 | 0x94, 0xA2, 0xFF, 0xA2, 0x94, 43 | 0x08, 0x04, 0x7E, 0x04, 0x08, 44 | 0x10, 0x20, 0x7E, 0x20, 0x10, 45 | 0x08, 0x08, 0x2A, 0x1C, 0x08, 46 | 0x08, 0x1C, 0x2A, 0x08, 0x08, 47 | 0x1E, 0x10, 0x10, 0x10, 0x10, 48 | 0x0C, 0x1E, 0x0C, 0x1E, 0x0C, 49 | 0x30, 0x38, 0x3E, 0x38, 0x30, 50 | 0x06, 0x0E, 0x3E, 0x0E, 0x06, 51 | 0x00, 0x00, 0x00, 0x00, 0x00, 52 | 0x00, 0x00, 0x5F, 0x00, 0x00, 53 | 0x00, 0x07, 0x00, 0x07, 0x00, 54 | 0x14, 0x7F, 0x14, 0x7F, 0x14, 55 | 0x24, 0x2A, 0x7F, 0x2A, 0x12, 56 | 0x23, 0x13, 0x08, 0x64, 0x62, 57 | 0x36, 0x49, 0x56, 0x20, 0x50, 58 | 0x00, 0x08, 0x07, 0x03, 0x00, 59 | 0x00, 0x1C, 0x22, 0x41, 0x00, 60 | 0x00, 0x41, 0x22, 0x1C, 0x00, 61 | 0x2A, 0x1C, 0x7F, 0x1C, 0x2A, 62 | 0x08, 0x08, 0x3E, 0x08, 0x08, 63 | 0x00, 0x80, 0x70, 0x30, 0x00, 64 | 0x08, 0x08, 0x08, 0x08, 0x08, 65 | 0x00, 0x00, 0x60, 0x60, 0x00, 66 | 0x20, 0x10, 0x08, 0x04, 0x02, 67 | 0x3E, 0x51, 0x49, 0x45, 0x3E, 68 | 0x00, 0x42, 0x7F, 0x40, 0x00, 69 | 0x72, 0x49, 0x49, 0x49, 0x46, 70 | 0x21, 0x41, 0x49, 0x4D, 0x33, 71 | 0x18, 0x14, 0x12, 0x7F, 0x10, 72 | 0x27, 0x45, 0x45, 0x45, 0x39, 73 | 0x3C, 0x4A, 0x49, 0x49, 0x31, 74 | 0x41, 0x21, 0x11, 0x09, 0x07, 75 | 0x36, 0x49, 0x49, 0x49, 0x36, 76 | 0x46, 0x49, 0x49, 0x29, 0x1E, 77 | 0x00, 0x00, 0x14, 0x00, 0x00, 78 | 0x00, 0x40, 0x34, 0x00, 0x00, 79 | 0x00, 0x08, 0x14, 0x22, 0x41, 80 | 0x14, 0x14, 0x14, 0x14, 0x14, 81 | 0x00, 0x41, 0x22, 0x14, 0x08, 82 | 0x02, 0x01, 0x59, 0x09, 0x06, 83 | 0x3E, 0x41, 0x5D, 0x59, 0x4E, 84 | 0x7C, 0x12, 0x11, 0x12, 0x7C, 85 | 0x7F, 0x49, 0x49, 0x49, 0x36, 86 | 0x3E, 0x41, 0x41, 0x41, 0x22, 87 | 0x7F, 0x41, 0x41, 0x41, 0x3E, 88 | 0x7F, 0x49, 0x49, 0x49, 0x41, 89 | 0x7F, 0x09, 0x09, 0x09, 0x01, 90 | 0x3E, 0x41, 0x41, 0x51, 0x73, 91 | 0x7F, 0x08, 0x08, 0x08, 0x7F, 92 | 0x00, 0x41, 0x7F, 0x41, 0x00, 93 | 0x20, 0x40, 0x41, 0x3F, 0x01, 94 | 0x7F, 0x08, 0x14, 0x22, 0x41, 95 | 0x7F, 0x40, 0x40, 0x40, 0x40, 96 | 0x7F, 0x02, 0x1C, 0x02, 0x7F, 97 | 0x7F, 0x04, 0x08, 0x10, 0x7F, 98 | 0x3E, 0x41, 0x41, 0x41, 0x3E, 99 | 0x7F, 0x09, 0x09, 0x09, 0x06, 100 | 0x3E, 0x41, 0x51, 0x21, 0x5E, 101 | 0x7F, 0x09, 0x19, 0x29, 0x46, 102 | 0x26, 0x49, 0x49, 0x49, 0x32, 103 | 0x03, 0x01, 0x7F, 0x01, 0x03, 104 | 0x3F, 0x40, 0x40, 0x40, 0x3F, 105 | 0x1F, 0x20, 0x40, 0x20, 0x1F, 106 | 0x3F, 0x40, 0x38, 0x40, 0x3F, 107 | 0x63, 0x14, 0x08, 0x14, 0x63, 108 | 0x03, 0x04, 0x78, 0x04, 0x03, 109 | 0x61, 0x59, 0x49, 0x4D, 0x43, 110 | 0x00, 0x7F, 0x41, 0x41, 0x41, 111 | 0x02, 0x04, 0x08, 0x10, 0x20, 112 | 0x00, 0x41, 0x41, 0x41, 0x7F, 113 | 0x04, 0x02, 0x01, 0x02, 0x04, 114 | 0x40, 0x40, 0x40, 0x40, 0x40, 115 | 0x00, 0x03, 0x07, 0x08, 0x00, 116 | 0x20, 0x54, 0x54, 0x78, 0x40, 117 | 0x7F, 0x28, 0x44, 0x44, 0x38, 118 | 0x38, 0x44, 0x44, 0x44, 0x28, 119 | 0x38, 0x44, 0x44, 0x28, 0x7F, 120 | 0x38, 0x54, 0x54, 0x54, 0x18, 121 | 0x00, 0x08, 0x7E, 0x09, 0x02, 122 | 0x18, 0xA4, 0xA4, 0x9C, 0x78, 123 | 0x7F, 0x08, 0x04, 0x04, 0x78, 124 | 0x00, 0x44, 0x7D, 0x40, 0x00, 125 | 0x20, 0x40, 0x40, 0x3D, 0x00, 126 | 0x7F, 0x10, 0x28, 0x44, 0x00, 127 | 0x00, 0x41, 0x7F, 0x40, 0x00, 128 | 0x7C, 0x04, 0x78, 0x04, 0x78, 129 | 0x7C, 0x08, 0x04, 0x04, 0x78, 130 | 0x38, 0x44, 0x44, 0x44, 0x38, 131 | 0xFC, 0x18, 0x24, 0x24, 0x18, 132 | 0x18, 0x24, 0x24, 0x18, 0xFC, 133 | 0x7C, 0x08, 0x04, 0x04, 0x08, 134 | 0x48, 0x54, 0x54, 0x54, 0x24, 135 | 0x04, 0x04, 0x3F, 0x44, 0x24, 136 | 0x3C, 0x40, 0x40, 0x20, 0x7C, 137 | 0x1C, 0x20, 0x40, 0x20, 0x1C, 138 | 0x3C, 0x40, 0x30, 0x40, 0x3C, 139 | 0x44, 0x28, 0x10, 0x28, 0x44, 140 | 0x4C, 0x90, 0x90, 0x90, 0x7C, 141 | 0x44, 0x64, 0x54, 0x4C, 0x44, 142 | 0x00, 0x08, 0x36, 0x41, 0x00, 143 | 0x00, 0x00, 0x77, 0x00, 0x00, 144 | 0x00, 0x41, 0x36, 0x08, 0x00, 145 | 0x02, 0x01, 0x02, 0x04, 0x02, 146 | 0x3C, 0x26, 0x23, 0x26, 0x3C, 147 | 0x1E, 0xA1, 0xA1, 0x61, 0x12, 148 | 0x3A, 0x40, 0x40, 0x20, 0x7A, 149 | 0x38, 0x54, 0x54, 0x55, 0x59, 150 | 0x21, 0x55, 0x55, 0x79, 0x41, 151 | 0x22, 0x54, 0x54, 0x78, 0x42, // a-umlaut 152 | 0x21, 0x55, 0x54, 0x78, 0x40, 153 | 0x20, 0x54, 0x55, 0x79, 0x40, 154 | 0x0C, 0x1E, 0x52, 0x72, 0x12, 155 | 0x39, 0x55, 0x55, 0x55, 0x59, 156 | 0x39, 0x54, 0x54, 0x54, 0x59, 157 | 0x39, 0x55, 0x54, 0x54, 0x58, 158 | 0x00, 0x00, 0x45, 0x7C, 0x41, 159 | 0x00, 0x02, 0x45, 0x7D, 0x42, 160 | 0x00, 0x01, 0x45, 0x7C, 0x40, 161 | 0x7D, 0x12, 0x11, 0x12, 0x7D, // A-umlaut 162 | 0xF0, 0x28, 0x25, 0x28, 0xF0, 163 | 0x7C, 0x54, 0x55, 0x45, 0x00, 164 | 0x20, 0x54, 0x54, 0x7C, 0x54, 165 | 0x7C, 0x0A, 0x09, 0x7F, 0x49, 166 | 0x32, 0x49, 0x49, 0x49, 0x32, 167 | 0x3A, 0x44, 0x44, 0x44, 0x3A, // o-umlaut 168 | 0x32, 0x4A, 0x48, 0x48, 0x30, 169 | 0x3A, 0x41, 0x41, 0x21, 0x7A, 170 | 0x3A, 0x42, 0x40, 0x20, 0x78, 171 | 0x00, 0x9D, 0xA0, 0xA0, 0x7D, 172 | 0x3D, 0x42, 0x42, 0x42, 0x3D, // O-umlaut 173 | 0x3D, 0x40, 0x40, 0x40, 0x3D, 174 | 0x3C, 0x24, 0xFF, 0x24, 0x24, 175 | 0x48, 0x7E, 0x49, 0x43, 0x66, 176 | 0x2B, 0x2F, 0xFC, 0x2F, 0x2B, 177 | 0xFF, 0x09, 0x29, 0xF6, 0x20, 178 | 0xC0, 0x88, 0x7E, 0x09, 0x03, 179 | 0x20, 0x54, 0x54, 0x79, 0x41, 180 | 0x00, 0x00, 0x44, 0x7D, 0x41, 181 | 0x30, 0x48, 0x48, 0x4A, 0x32, 182 | 0x38, 0x40, 0x40, 0x22, 0x7A, 183 | 0x00, 0x7A, 0x0A, 0x0A, 0x72, 184 | 0x7D, 0x0D, 0x19, 0x31, 0x7D, 185 | 0x26, 0x29, 0x29, 0x2F, 0x28, 186 | 0x26, 0x29, 0x29, 0x29, 0x26, 187 | 0x30, 0x48, 0x4D, 0x40, 0x20, 188 | 0x38, 0x08, 0x08, 0x08, 0x08, 189 | 0x08, 0x08, 0x08, 0x08, 0x38, 190 | 0x2F, 0x10, 0xC8, 0xAC, 0xBA, 191 | 0x2F, 0x10, 0x28, 0x34, 0xFA, 192 | 0x00, 0x00, 0x7B, 0x00, 0x00, 193 | 0x08, 0x14, 0x2A, 0x14, 0x22, 194 | 0x22, 0x14, 0x2A, 0x14, 0x08, 195 | 0x55, 0x00, 0x55, 0x00, 0x55, // #176 (25% block) missing in old code 196 | 0xAA, 0x55, 0xAA, 0x55, 0xAA, // 50% block 197 | 0xFF, 0x55, 0xFF, 0x55, 0xFF, // 75% block 198 | 0x00, 0x00, 0x00, 0xFF, 0x00, 199 | 0x10, 0x10, 0x10, 0xFF, 0x00, 200 | 0x14, 0x14, 0x14, 0xFF, 0x00, 201 | 0x10, 0x10, 0xFF, 0x00, 0xFF, 202 | 0x10, 0x10, 0xF0, 0x10, 0xF0, 203 | 0x14, 0x14, 0x14, 0xFC, 0x00, 204 | 0x14, 0x14, 0xF7, 0x00, 0xFF, 205 | 0x00, 0x00, 0xFF, 0x00, 0xFF, 206 | 0x14, 0x14, 0xF4, 0x04, 0xFC, 207 | 0x14, 0x14, 0x17, 0x10, 0x1F, 208 | 0x10, 0x10, 0x1F, 0x10, 0x1F, 209 | 0x14, 0x14, 0x14, 0x1F, 0x00, 210 | 0x10, 0x10, 0x10, 0xF0, 0x00, 211 | 0x00, 0x00, 0x00, 0x1F, 0x10, 212 | 0x10, 0x10, 0x10, 0x1F, 0x10, 213 | 0x10, 0x10, 0x10, 0xF0, 0x10, 214 | 0x00, 0x00, 0x00, 0xFF, 0x10, 215 | 0x10, 0x10, 0x10, 0x10, 0x10, 216 | 0x10, 0x10, 0x10, 0xFF, 0x10, 217 | 0x00, 0x00, 0x00, 0xFF, 0x14, 218 | 0x00, 0x00, 0xFF, 0x00, 0xFF, 219 | 0x00, 0x00, 0x1F, 0x10, 0x17, 220 | 0x00, 0x00, 0xFC, 0x04, 0xF4, 221 | 0x14, 0x14, 0x17, 0x10, 0x17, 222 | 0x14, 0x14, 0xF4, 0x04, 0xF4, 223 | 0x00, 0x00, 0xFF, 0x00, 0xF7, 224 | 0x14, 0x14, 0x14, 0x14, 0x14, 225 | 0x14, 0x14, 0xF7, 0x00, 0xF7, 226 | 0x14, 0x14, 0x14, 0x17, 0x14, 227 | 0x10, 0x10, 0x1F, 0x10, 0x1F, 228 | 0x14, 0x14, 0x14, 0xF4, 0x14, 229 | 0x10, 0x10, 0xF0, 0x10, 0xF0, 230 | 0x00, 0x00, 0x1F, 0x10, 0x1F, 231 | 0x00, 0x00, 0x00, 0x1F, 0x14, 232 | 0x00, 0x00, 0x00, 0xFC, 0x14, 233 | 0x00, 0x00, 0xF0, 0x10, 0xF0, 234 | 0x10, 0x10, 0xFF, 0x10, 0xFF, 235 | 0x14, 0x14, 0x14, 0xFF, 0x14, 236 | 0x10, 0x10, 0x10, 0x1F, 0x00, 237 | 0x00, 0x00, 0x00, 0xF0, 0x10, 238 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 239 | 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 240 | 0xFF, 0xFF, 0xFF, 0x00, 0x00, 241 | 0x00, 0x00, 0x00, 0xFF, 0xFF, 242 | 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 243 | 0x38, 0x44, 0x44, 0x38, 0x44, 244 | 0xFC, 0x4A, 0x4A, 0x4A, 0x34, // sharp-s or beta 245 | 0x7E, 0x02, 0x02, 0x06, 0x06, 246 | 0x02, 0x7E, 0x02, 0x7E, 0x02, 247 | 0x63, 0x55, 0x49, 0x41, 0x63, 248 | 0x38, 0x44, 0x44, 0x3C, 0x04, 249 | 0x40, 0x7E, 0x20, 0x1E, 0x20, 250 | 0x06, 0x02, 0x7E, 0x02, 0x02, 251 | 0x99, 0xA5, 0xE7, 0xA5, 0x99, 252 | 0x1C, 0x2A, 0x49, 0x2A, 0x1C, 253 | 0x4C, 0x72, 0x01, 0x72, 0x4C, 254 | 0x30, 0x4A, 0x4D, 0x4D, 0x30, 255 | 0x30, 0x48, 0x78, 0x48, 0x30, 256 | 0xBC, 0x62, 0x5A, 0x46, 0x3D, 257 | 0x3E, 0x49, 0x49, 0x49, 0x00, 258 | 0x7E, 0x01, 0x01, 0x01, 0x7E, 259 | 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 260 | 0x44, 0x44, 0x5F, 0x44, 0x44, 261 | 0x40, 0x51, 0x4A, 0x44, 0x40, 262 | 0x40, 0x44, 0x4A, 0x51, 0x40, 263 | 0x00, 0x00, 0xFF, 0x01, 0x03, 264 | 0xE0, 0x80, 0xFF, 0x00, 0x00, 265 | 0x08, 0x08, 0x6B, 0x6B, 0x08, 266 | 0x36, 0x12, 0x36, 0x24, 0x36, 267 | 0x06, 0x0F, 0x09, 0x0F, 0x06, 268 | 0x00, 0x00, 0x18, 0x18, 0x00, 269 | 0x00, 0x00, 0x10, 0x10, 0x00, 270 | 0x30, 0x40, 0xFF, 0x01, 0x01, 271 | 0x00, 0x1F, 0x01, 0x01, 0x1E, 272 | 0x00, 0x19, 0x1D, 0x17, 0x12, 273 | 0x00, 0x3C, 0x3C, 0x3C, 0x3C, 274 | 0x00, 0x00, 0x00, 0x00, 0x00 // #255 NBSP 275 | }; 276 | #endif // FONT5X7_H 277 | -------------------------------------------------------------------------------- /lib/Adafruit_GFX/library.properties: -------------------------------------------------------------------------------- 1 | name=Adafruit GFX Library 2 | version=1.1.3 3 | author=Adafruit 4 | maintainer=Adafruit 5 | sentence=Adafruit GFX graphics core library, this is the 'core' class that all our other graphics libraries derive from. 6 | paragraph=Install this library in addition to the display library for your hardware. 7 | category=Display 8 | url=https://github.com/adafruit/Adafruit-GFX-Library 9 | architectures=* 10 | -------------------------------------------------------------------------------- /lib/Adafruit_GFX/license.txt: -------------------------------------------------------------------------------- 1 | Software License Agreement (BSD License) 2 | 3 | Copyright (c) 2012 Adafruit Industries. All rights reserved. 4 | 5 | Redistribution and use in source and binary forms, with or without 6 | modification, are permitted provided that the following conditions are met: 7 | 8 | - Redistributions of source code must retain the above copyright notice, 9 | this list of conditions and the following disclaimer. 10 | - Redistributions in binary form must reproduce the above copyright notice, 11 | this list of conditions and the following disclaimer in the documentation 12 | and/or other materials provided with the distribution. 13 | 14 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 15 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 18 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 19 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 20 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 21 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 22 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 23 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 24 | POSSIBILITY OF SUCH DAMAGE. 25 | -------------------------------------------------------------------------------- /lib/ESP_SSD1306/ESP_SSD1306.cpp: -------------------------------------------------------------------------------- 1 | /********************************************************************* 2 | * 3 | * Modified Adafruit Library to work with OLED SSD1306_128_64 4 | * with SPI communication only ** 5 | * 6 | * //commented for ESP8266 compatibility indicates the files commented 7 | * with respect to the default Adafruit library 8 | * 9 | * //Added for compatibility with ESP8266 board indicates where has been 10 | * added code with respect to default Adafruit library 11 | * 12 | * Appart from that all the classes name Adafruit_SSD1306 have been replaced 13 | * for ESP_SSD1306, and has been included the header "ESP_SSD1306.h" 14 | * 15 | * 16 | * 17 | * By: Jordi Segura 18 | * 19 | This is a library for Monochrome OLEDs based on SSD1306 drivers 20 | 21 | Pick one up today in the adafruit shop! 22 | ------> http://www.adafruit.com/category/63_98 23 | 24 | These displays use SPI to communicate, 4 or 5 pins are required to 25 | interface 26 | 27 | Adafruit invests time and resources providing this open source code, 28 | please support Adafruit and open-source hardware by purchasing 29 | products from Adafruit! 30 | 31 | Written by Limor Fried/Ladyada for Adafruit Industries. 32 | BSD license, check license.txt for more information 33 | All text above, and the splash screen below must be included in any redistribution 34 | *********************************************************************/ 35 | 36 | //#include //commented for ESP8266 compatibility 37 | #ifndef __SAM3X8E__ 38 | // #include //commented for ESP8266 compatibility 39 | #endif 40 | #include 41 | 42 | #include //needed for I2C comm 43 | 44 | #include "../Adafruit_GFX/Adafruit_GFX.h" 45 | #include "ESP_SSD1306.h" //Instead of "Adafruit_SSD1306.h" 46 | 47 | // the memory buffer for the LCD 48 | 49 | static uint8_t buffer[SSD1306_LCDHEIGHT * SSD1306_LCDWIDTH / 8] = { 50 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 51 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 52 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 53 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 54 | 0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 55 | 0x00, 0x80, 0x80, 0xC0, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 56 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 57 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 58 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 59 | 0x00, 0x00, 0x00, 0x00, 0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xF8, 0xE0, 0x00, 0x00, 0x00, 0x00, 60 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x80, 61 | 0x80, 0x80, 0x00, 0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00, 0xFF, 62 | #if (SSD1306_LCDHEIGHT * SSD1306_LCDWIDTH > 96*16) 63 | 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 64 | 0x80, 0xFF, 0xFF, 0x80, 0x80, 0x00, 0x80, 0x80, 0x00, 0x80, 0x80, 0x80, 0x80, 0x00, 0x80, 0x80, 65 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 0x8C, 0x8E, 0x84, 0x00, 0x00, 0x80, 0xF8, 66 | 0xF8, 0xF8, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 67 | 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xE0, 0xE0, 0xC0, 0x80, 68 | 0x00, 0xE0, 0xFC, 0xFE, 0xFF, 0xFF, 0xFF, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 69 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFE, 0xFF, 0xC7, 0x01, 0x01, 70 | 0x01, 0x01, 0x83, 0xFF, 0xFF, 0x00, 0x00, 0x7C, 0xFE, 0xC7, 0x01, 0x01, 0x01, 0x01, 0x83, 0xFF, 71 | 0xFF, 0xFF, 0x00, 0x38, 0xFE, 0xC7, 0x83, 0x01, 0x01, 0x01, 0x83, 0xC7, 0xFF, 0xFF, 0x00, 0x00, 72 | 0x01, 0xFF, 0xFF, 0x01, 0x01, 0x00, 0xFF, 0xFF, 0x07, 0x01, 0x01, 0x01, 0x00, 0x00, 0x7F, 0xFF, 73 | 0x80, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x7F, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x01, 0xFF, 74 | 0xFF, 0xFF, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 75 | 0x03, 0x0F, 0x3F, 0x7F, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xE7, 0xC7, 0xC7, 0x8F, 76 | 0x8F, 0x9F, 0xBF, 0xFF, 0xFF, 0xC3, 0xC0, 0xF0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC, 0xFC, 0xFC, 77 | 0xFC, 0xFC, 0xFC, 0xFC, 0xFC, 0xF8, 0xF8, 0xF0, 0xF0, 0xE0, 0xC0, 0x00, 0x01, 0x03, 0x03, 0x03, 78 | 0x03, 0x03, 0x01, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x03, 0x03, 0x03, 0x01, 0x01, 79 | 0x03, 0x01, 0x00, 0x00, 0x00, 0x01, 0x03, 0x03, 0x03, 0x03, 0x01, 0x01, 0x03, 0x03, 0x00, 0x00, 80 | 0x00, 0x03, 0x03, 0x00, 0x00, 0x00, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 81 | 0x03, 0x03, 0x03, 0x03, 0x03, 0x01, 0x00, 0x00, 0x00, 0x01, 0x03, 0x01, 0x00, 0x00, 0x00, 0x03, 82 | 0x03, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 83 | #if (SSD1306_LCDHEIGHT == 64) 84 | 0x00, 0x00, 0x00, 0x80, 0xC0, 0xE0, 0xF0, 0xF9, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x3F, 0x1F, 0x0F, 85 | 0x87, 0xC7, 0xF7, 0xFF, 0xFF, 0x1F, 0x1F, 0x3D, 0xFC, 0xF8, 0xF8, 0xF8, 0xF8, 0x7C, 0x7D, 0xFF, 86 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x3F, 0x0F, 0x07, 0x00, 0x30, 0x30, 0x00, 0x00, 87 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 88 | 0x00, 0x00, 0x00, 0x00, 0xFE, 0xFE, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 89 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0xC0, 0x00, 90 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x30, 0x00, 0x00, 0x00, 0x00, 91 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 92 | 0x00, 0xC0, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x7F, 0x3F, 0x1F, 93 | 0x0F, 0x07, 0x1F, 0x7F, 0xFF, 0xFF, 0xF8, 0xF8, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xF8, 0xE0, 94 | 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFE, 0xFE, 0x00, 0x00, 95 | 0x00, 0xFC, 0xFE, 0xFC, 0x0C, 0x06, 0x06, 0x0E, 0xFC, 0xF8, 0x00, 0x00, 0xF0, 0xF8, 0x1C, 0x0E, 96 | 0x06, 0x06, 0x06, 0x0C, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0xFE, 0xFE, 0x00, 0x00, 0x00, 0x00, 0xFC, 97 | 0xFE, 0xFC, 0x00, 0x18, 0x3C, 0x7E, 0x66, 0xE6, 0xCE, 0x84, 0x00, 0x00, 0x06, 0xFF, 0xFF, 0x06, 98 | 0x06, 0xFC, 0xFE, 0xFC, 0x0C, 0x06, 0x06, 0x06, 0x00, 0x00, 0xFE, 0xFE, 0x00, 0x00, 0xC0, 0xF8, 99 | 0xFC, 0x4E, 0x46, 0x46, 0x46, 0x4E, 0x7C, 0x78, 0x40, 0x18, 0x3C, 0x76, 0xE6, 0xCE, 0xCC, 0x80, 100 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 101 | 0x00, 0x00, 0x00, 0x00, 0x01, 0x07, 0x0F, 0x1F, 0x1F, 0x3F, 0x3F, 0x3F, 0x3F, 0x1F, 0x0F, 0x03, 102 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x0F, 0x00, 0x00, 103 | 0x00, 0x0F, 0x0F, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x0F, 0x00, 0x00, 0x03, 0x07, 0x0E, 0x0C, 104 | 0x18, 0x18, 0x0C, 0x06, 0x0F, 0x0F, 0x0F, 0x00, 0x00, 0x01, 0x0F, 0x0E, 0x0C, 0x18, 0x0C, 0x0F, 105 | 0x07, 0x01, 0x00, 0x04, 0x0E, 0x0C, 0x18, 0x0C, 0x0F, 0x07, 0x00, 0x00, 0x00, 0x0F, 0x0F, 0x00, 106 | 0x00, 0x0F, 0x0F, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x0F, 0x00, 0x00, 0x00, 0x07, 107 | 0x07, 0x0C, 0x0C, 0x18, 0x1C, 0x0C, 0x06, 0x06, 0x00, 0x04, 0x0E, 0x0C, 0x18, 0x0C, 0x0F, 0x07, 108 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 109 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 110 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 111 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 112 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 113 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 114 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 115 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 116 | #endif 117 | #endif 118 | }; 119 | 120 | 121 | 122 | // the most basic function, set a single pixel 123 | void ESP_SSD1306::drawPixel(int16_t x, int16_t y, uint16_t color) { 124 | if ((x < 0) || (x >= width()) || (y < 0) || (y >= height())) 125 | return; 126 | 127 | // check rotation, move pixel around if necessary 128 | switch (getRotation()) { 129 | case 1: 130 | swap(x, y); 131 | x = WIDTH - x - 1; 132 | break; 133 | case 2: 134 | x = WIDTH - x - 1; 135 | y = HEIGHT - y - 1; 136 | break; 137 | case 3: 138 | swap(x, y); 139 | y = HEIGHT - y - 1; 140 | break; 141 | } 142 | 143 | // x is which column 144 | switch (color) 145 | { 146 | case WHITE: buffer[x+ (y/8)*SSD1306_LCDWIDTH] |= (1 << (y&7)); break; 147 | case BLACK: buffer[x+ (y/8)*SSD1306_LCDWIDTH] &= ~(1 << (y&7)); break; 148 | case INVERSE: buffer[x+ (y/8)*SSD1306_LCDWIDTH] ^= (1 << (y&7)); break; 149 | } 150 | 151 | } 152 | 153 | ESP_SSD1306::ESP_SSD1306(int8_t SID, int8_t SCLK, int8_t DC, int8_t RST, int8_t CS) : Adafruit_GFX(SSD1306_LCDWIDTH, SSD1306_LCDHEIGHT) { 154 | cs = CS; 155 | rst = RST; 156 | dc = DC; 157 | sclk = SCLK; 158 | sid = SID; 159 | hwSPI = false; 160 | } 161 | 162 | // constructor for hardware SPI - we indicate DataCommand, ChipSelect, Reset 163 | ESP_SSD1306::ESP_SSD1306(int8_t DC, int8_t RST, int8_t CS) : Adafruit_GFX(SSD1306_LCDWIDTH, SSD1306_LCDHEIGHT) { 164 | dc = DC; 165 | rst = RST; 166 | cs = CS; 167 | hwSPI = true; 168 | } 169 | 170 | // initializer for I2C - we only indicate the reset pin! 171 | ESP_SSD1306::ESP_SSD1306(int8_t reset) : 172 | Adafruit_GFX(SSD1306_LCDWIDTH, SSD1306_LCDHEIGHT) { 173 | sclk = dc = cs = sid = -1; 174 | rst = reset; 175 | } 176 | 177 | 178 | void ESP_SSD1306::begin(uint8_t vccstate, uint8_t i2caddr, bool reset) { 179 | _vccstate = vccstate; 180 | _i2caddr = i2caddr; 181 | 182 | // set pin directions 183 | if (sid != -1){ 184 | pinMode(dc, OUTPUT); 185 | pinMode(cs, OUTPUT); 186 | //commented for ESP8266 compatibility 187 | // csport = portOutputRegister(digitalPinToPort(cs)); 188 | // cspinmask = digitalPinToBitMask(cs); 189 | // dcport = portOutputRegister(digitalPinToPort(dc)); 190 | // dcpinmask = digitalPinToBitMask(dc); 191 | if (!hwSPI){ 192 | // set pins for software-SPI 193 | pinMode(sid, OUTPUT); 194 | pinMode(sclk, OUTPUT); 195 | //commented for ESP8266 compatibility 196 | // clkport = portOutputRegister(digitalPinToPort(sclk)); 197 | // clkpinmask = digitalPinToBitMask(sclk); 198 | // mosiport = portOutputRegister(digitalPinToPort(sid)); 199 | // mosipinmask = digitalPinToBitMask(sid); 200 | } 201 | if (hwSPI){ 202 | SPI.begin (); 203 | #ifdef __SAM3X8E__ 204 | SPI.setClockDivider (9); // 9.3 MHz 205 | #elif defined ESP8266 //Added for compatibility with ESP8266 board 206 | SPI.setClockDivider (SPI_CLOCK_DIV2); // 26/2 = 13 MHz (freq ESP8266 26 MHz) 207 | #else 208 | SPI.setClockDivider (SPI_CLOCK_DIV2); // 8 MHz 209 | #endif 210 | // SPI.setDataMode(SPI_MODE0); //optional 211 | // SPI.setBitOrder(MSBFIRST); //optional 212 | } 213 | } 214 | else 215 | { 216 | // I2C Init 217 | Wire.begin(); 218 | #ifdef __SAM3X8E__ 219 | // Force 400 KHz I2C, rawr! (Uses pins 20, 21 for SDA, SCL) 220 | TWI1->TWI_CWGR = 0; 221 | TWI1->TWI_CWGR = ((VARIANT_MCK / (2 * 400000)) - 4) * 0x101; 222 | #endif 223 | } 224 | 225 | if (reset) { 226 | // Setup reset pin direction (used by both SPI and I2C) 227 | pinMode(rst, OUTPUT); 228 | digitalWrite(rst, HIGH); 229 | // VDD (3.3V) goes high at start, lets just chill for a ms 230 | delay(1); 231 | // bring reset low 232 | digitalWrite(rst, LOW); 233 | // wait 10ms 234 | delay(10); 235 | // bring out of reset 236 | digitalWrite(rst, HIGH); 237 | // turn on VCC (9V?) 238 | } 239 | 240 | #if defined SSD1306_128_32 241 | // Init sequence for 128x32 OLED module 242 | ssd1306_command(SSD1306_DISPLAYOFF); // 0xAE 243 | ssd1306_command(SSD1306_SETDISPLAYCLOCKDIV); // 0xD5 244 | ssd1306_command(0x80); // the suggested ratio 0x80 245 | ssd1306_command(SSD1306_SETMULTIPLEX); // 0xA8 246 | ssd1306_command(0x1F); 247 | ssd1306_command(SSD1306_SETDISPLAYOFFSET); // 0xD3 248 | ssd1306_command(0x0); // no offset 249 | ssd1306_command(SSD1306_SETSTARTLINE | 0x0); // line #0 250 | ssd1306_command(SSD1306_CHARGEPUMP); // 0x8D 251 | if (vccstate == SSD1306_EXTERNALVCC) 252 | { ssd1306_command(0x10); } 253 | else 254 | { ssd1306_command(0x14); } 255 | ssd1306_command(SSD1306_MEMORYMODE); // 0x20 256 | ssd1306_command(0x00); // 0x0 act like ks0108 257 | ssd1306_command(SSD1306_SEGREMAP | 0x1); 258 | ssd1306_command(SSD1306_COMSCANDEC); 259 | ssd1306_command(SSD1306_SETCOMPINS); // 0xDA 260 | ssd1306_command(0x02); 261 | ssd1306_command(SSD1306_SETCONTRAST); // 0x81 262 | ssd1306_command(0x8F); 263 | ssd1306_command(SSD1306_SETPRECHARGE); // 0xd9 264 | if (vccstate == SSD1306_EXTERNALVCC) 265 | { ssd1306_command(0x22); } 266 | else 267 | { ssd1306_command(0xF1); } 268 | ssd1306_command(SSD1306_SETVCOMDETECT); // 0xDB 269 | ssd1306_command(0x40); 270 | ssd1306_command(SSD1306_DISPLAYALLON_RESUME); // 0xA4 271 | ssd1306_command(SSD1306_NORMALDISPLAY); // 0xA6 272 | #endif 273 | 274 | #if defined SSD1306_128_64 275 | // Init sequence for 128x64 OLED module 276 | ssd1306_command(SSD1306_DISPLAYOFF); // 0xAE 277 | ssd1306_command(SSD1306_SETDISPLAYCLOCKDIV); // 0xD5 278 | ssd1306_command(0x80); // the suggested ratio 0x80 279 | ssd1306_command(SSD1306_SETMULTIPLEX); // 0xA8 280 | ssd1306_command(0x3F); 281 | ssd1306_command(SSD1306_SETDISPLAYOFFSET); // 0xD3 282 | ssd1306_command(0x0); // no offset 283 | ssd1306_command(SSD1306_SETSTARTLINE | 0x0); // line #0 284 | ssd1306_command(SSD1306_CHARGEPUMP); // 0x8D 285 | if (vccstate == SSD1306_EXTERNALVCC) 286 | { ssd1306_command(0x10); } 287 | else 288 | { ssd1306_command(0x14); } 289 | ssd1306_command(SSD1306_MEMORYMODE); // 0x20 290 | ssd1306_command(0x00); // 0x0 act like ks0108 291 | ssd1306_command(SSD1306_SEGREMAP | 0x1); 292 | ssd1306_command(SSD1306_COMSCANDEC); 293 | ssd1306_command(SSD1306_SETCOMPINS); // 0xDA 294 | ssd1306_command(0x12); 295 | ssd1306_command(SSD1306_SETCONTRAST); // 0x81 296 | if (vccstate == SSD1306_EXTERNALVCC) 297 | { ssd1306_command(0x9F); } 298 | else 299 | { ssd1306_command(0xCF); } 300 | ssd1306_command(SSD1306_SETPRECHARGE); // 0xd9 301 | if (vccstate == SSD1306_EXTERNALVCC) 302 | { ssd1306_command(0x22); } 303 | else 304 | { ssd1306_command(0xF1); } 305 | ssd1306_command(SSD1306_SETVCOMDETECT); // 0xDB 306 | ssd1306_command(0x40); 307 | ssd1306_command(SSD1306_DISPLAYALLON_RESUME); // 0xA4 308 | ssd1306_command(SSD1306_NORMALDISPLAY); // 0xA6 309 | #endif 310 | 311 | #if defined SSD1306_96_16 312 | // Init sequence for 96x16 OLED module 313 | ssd1306_command(SSD1306_DISPLAYOFF); // 0xAE 314 | ssd1306_command(SSD1306_SETDISPLAYCLOCKDIV); // 0xD5 315 | ssd1306_command(0x80); // the suggested ratio 0x80 316 | ssd1306_command(SSD1306_SETMULTIPLEX); // 0xA8 317 | ssd1306_command(0x0F); 318 | ssd1306_command(SSD1306_SETDISPLAYOFFSET); // 0xD3 319 | ssd1306_command(0x00); // no offset 320 | ssd1306_command(SSD1306_SETSTARTLINE | 0x0); // line #0 321 | ssd1306_command(SSD1306_CHARGEPUMP); // 0x8D 322 | if (vccstate == SSD1306_EXTERNALVCC) 323 | { ssd1306_command(0x10); } 324 | else 325 | { ssd1306_command(0x14); } 326 | ssd1306_command(SSD1306_MEMORYMODE); // 0x20 327 | ssd1306_command(0x00); // 0x0 act like ks0108 328 | ssd1306_command(SSD1306_SEGREMAP | 0x1); 329 | ssd1306_command(SSD1306_COMSCANDEC); 330 | ssd1306_command(SSD1306_SETCOMPINS); // 0xDA 331 | ssd1306_command(0x2); //ada x12 332 | ssd1306_command(SSD1306_SETCONTRAST); // 0x81 333 | if (vccstate == SSD1306_EXTERNALVCC) 334 | { ssd1306_command(0x10); } 335 | else 336 | { ssd1306_command(0xAF); } 337 | ssd1306_command(SSD1306_SETPRECHARGE); // 0xd9 338 | if (vccstate == SSD1306_EXTERNALVCC) 339 | { ssd1306_command(0x22); } 340 | else 341 | { ssd1306_command(0xF1); } 342 | ssd1306_command(SSD1306_SETVCOMDETECT); // 0xDB 343 | ssd1306_command(0x40); 344 | ssd1306_command(SSD1306_DISPLAYALLON_RESUME); // 0xA4 345 | ssd1306_command(SSD1306_NORMALDISPLAY); // 0xA6 346 | #endif 347 | 348 | ssd1306_command(SSD1306_DISPLAYON);//--turn on oled panel 349 | } 350 | 351 | 352 | void ESP_SSD1306::invertDisplay(uint8_t i) { 353 | if (i) { 354 | ssd1306_command(SSD1306_INVERTDISPLAY); 355 | } else { 356 | ssd1306_command(SSD1306_NORMALDISPLAY); 357 | } 358 | } 359 | 360 | void ESP_SSD1306::ssd1306_command(uint8_t c) { 361 | if (sid != -1) 362 | { 363 | // SPI 364 | digitalWrite(cs, HIGH); //uncommented for ESP8266 compatibility 365 | // *csport |= cspinmask; //commented for ESP8266 compatibility 366 | digitalWrite(dc, LOW); //uncommented for ESP8266 compatibility 367 | // *dcport &= ~dcpinmask; //commented for ESP8266 compatibility 368 | digitalWrite(cs, LOW); //uncommented for ESP8266 compatibility 369 | // *csport &= ~cspinmask; //commented for ESP8266 compatibility 370 | fastSPIwrite(c); 371 | digitalWrite(cs, HIGH); //uncommented for ESP8266 compatibility 372 | // *csport |= cspinmask; //commented for ESP8266 compatibility 373 | } 374 | else 375 | { 376 | // I2C 377 | uint8_t control = 0x00; // Co = 0, D/C = 0 378 | Wire.beginTransmission(_i2caddr); 379 | WIRE_WRITE(control); 380 | WIRE_WRITE(c); 381 | Wire.endTransmission(); 382 | } 383 | } 384 | 385 | // startscrollright 386 | // Activate a right handed scroll for rows start through stop 387 | // Hint, the display is 16 rows tall. To scroll the whole display, run: 388 | // display.scrollright(0x00, 0x0F) 389 | void ESP_SSD1306::startscrollright(uint8_t start, uint8_t stop){ 390 | ssd1306_command(SSD1306_RIGHT_HORIZONTAL_SCROLL); 391 | ssd1306_command(0X00); 392 | ssd1306_command(start); 393 | ssd1306_command(0X00); 394 | ssd1306_command(stop); 395 | ssd1306_command(0X00); 396 | ssd1306_command(0XFF); 397 | ssd1306_command(SSD1306_ACTIVATE_SCROLL); 398 | } 399 | 400 | // startscrollleft 401 | // Activate a right handed scroll for rows start through stop 402 | // Hint, the display is 16 rows tall. To scroll the whole display, run: 403 | // display.scrollright(0x00, 0x0F) 404 | void ESP_SSD1306::startscrollleft(uint8_t start, uint8_t stop){ 405 | ssd1306_command(SSD1306_LEFT_HORIZONTAL_SCROLL); 406 | ssd1306_command(0X00); 407 | ssd1306_command(start); 408 | ssd1306_command(0X00); 409 | ssd1306_command(stop); 410 | ssd1306_command(0X00); 411 | ssd1306_command(0XFF); 412 | ssd1306_command(SSD1306_ACTIVATE_SCROLL); 413 | } 414 | 415 | // startscrolldiagright 416 | // Activate a diagonal scroll for rows start through stop 417 | // Hint, the display is 16 rows tall. To scroll the whole display, run: 418 | // display.scrollright(0x00, 0x0F) 419 | void ESP_SSD1306::startscrolldiagright(uint8_t start, uint8_t stop){ 420 | ssd1306_command(SSD1306_SET_VERTICAL_SCROLL_AREA); 421 | ssd1306_command(0X00); 422 | ssd1306_command(SSD1306_LCDHEIGHT); 423 | ssd1306_command(SSD1306_VERTICAL_AND_RIGHT_HORIZONTAL_SCROLL); 424 | ssd1306_command(0X00); 425 | ssd1306_command(start); 426 | ssd1306_command(0X00); 427 | ssd1306_command(stop); 428 | ssd1306_command(0X01); 429 | ssd1306_command(SSD1306_ACTIVATE_SCROLL); 430 | } 431 | 432 | // startscrolldiagleft 433 | // Activate a diagonal scroll for rows start through stop 434 | // Hint, the display is 16 rows tall. To scroll the whole display, run: 435 | // display.scrollright(0x00, 0x0F) 436 | void ESP_SSD1306::startscrolldiagleft(uint8_t start, uint8_t stop){ 437 | ssd1306_command(SSD1306_SET_VERTICAL_SCROLL_AREA); 438 | ssd1306_command(0X00); 439 | ssd1306_command(SSD1306_LCDHEIGHT); 440 | ssd1306_command(SSD1306_VERTICAL_AND_LEFT_HORIZONTAL_SCROLL); 441 | ssd1306_command(0X00); 442 | ssd1306_command(start); 443 | ssd1306_command(0X00); 444 | ssd1306_command(stop); 445 | ssd1306_command(0X01); 446 | ssd1306_command(SSD1306_ACTIVATE_SCROLL); 447 | } 448 | 449 | void ESP_SSD1306::stopscroll(void){ 450 | ssd1306_command(SSD1306_DEACTIVATE_SCROLL); 451 | } 452 | 453 | // Dim the display 454 | // dim = true: display is dimmed 455 | // dim = false: display is normal 456 | void ESP_SSD1306::dim(boolean dim) { 457 | uint8_t contrast; 458 | 459 | if (dim) { 460 | contrast = 0; // Dimmed display 461 | } else { 462 | if (_vccstate == SSD1306_EXTERNALVCC) { 463 | contrast = 0x9F; 464 | } else { 465 | contrast = 0xCF; 466 | } 467 | } 468 | // the range of contrast to too small to be really useful 469 | // it is useful to dim the display 470 | ssd1306_command(SSD1306_SETCONTRAST); 471 | ssd1306_command(contrast); 472 | } 473 | 474 | void ESP_SSD1306::ssd1306_data(uint8_t c) { 475 | if (sid != -1) 476 | { 477 | // SPI 478 | digitalWrite(cs, HIGH); //uncommented for ESP8266 compatibility 479 | //*csport |= cspinmask; //commented for ESP8266 compatibility 480 | digitalWrite(dc, HIGH); //uncommented for ESP8266 compatibility 481 | //*dcport |= dcpinmask; //commented for ESP8266 compatibility 482 | digitalWrite(cs, LOW); //uncommented for ESP8266 compatibility 483 | //*csport &= ~cspinmask; //commented for ESP8266 compatibility 484 | fastSPIwrite(c); 485 | digitalWrite(cs, HIGH); //uncommented for ESP8266 compatibility 486 | //*csport |= cspinmask; //commented for ESP8266 compatibility 487 | } 488 | else 489 | { 490 | // I2C 491 | uint8_t control = 0x40; // Co = 0, D/C = 1 492 | Wire.beginTransmission(_i2caddr); 493 | WIRE_WRITE(control); 494 | WIRE_WRITE(c); 495 | Wire.endTransmission(); 496 | } 497 | } 498 | 499 | void ESP_SSD1306::display(void) { 500 | ssd1306_command(SSD1306_COLUMNADDR); 501 | ssd1306_command(0); // Column start address (0 = reset) 502 | ssd1306_command(SSD1306_LCDWIDTH-1); // Column end address (127 = reset) 503 | 504 | ssd1306_command(SSD1306_PAGEADDR); 505 | ssd1306_command(0); // Page start address (0 = reset) 506 | #if SSD1306_LCDHEIGHT == 64 507 | ssd1306_command(7); // Page end address 508 | #endif 509 | #if SSD1306_LCDHEIGHT == 32 510 | ssd1306_command(3); // Page end address 511 | #endif 512 | #if SSD1306_LCDHEIGHT == 16 513 | ssd1306_command(1); // Page end address 514 | #endif 515 | 516 | if (sid != -1) 517 | { 518 | // SPI 519 | digitalWrite(cs, HIGH); //added for ESP8266 compatibility 520 | // *csport |= cspinmask; //commented for ESP8266 compatibility 521 | digitalWrite(dc, HIGH); //added for ESP8266 compatibility 522 | // *dcport |= dcpinmask; //commented for ESP8266 compatibility 523 | digitalWrite(cs, LOW); //added for ESP8266 compatibility 524 | // *csport &= ~cspinmask; //commented for ESP8266 compatibility 525 | 526 | for (uint16_t i=0; i<(SSD1306_LCDWIDTH*SSD1306_LCDHEIGHT/8); i++) { 527 | fastSPIwrite(buffer[i]); 528 | //ssd1306_data(buffer[i]); 529 | } 530 | digitalWrite(cs, HIGH); //added for ESP8266 compatibility 531 | // *csport |= cspinmask; //commented for ESP8266 compatibility 532 | } 533 | else 534 | { 535 | // save I2C bitrate 536 | #ifndef __SAM3X8E__ 537 | // uint8_t twbrbackup = TWBR; //commented for ESP8266 compatibility 538 | // TWBR = 12; // upgrade to 400KHz! //commented for ESP8266 compatibility 539 | #endif 540 | 541 | //Serial.println(TWBR, DEC); 542 | //Serial.println(TWSR & 0x3, DEC); 543 | 544 | // I2C 545 | for (uint16_t i=0; i<(SSD1306_LCDWIDTH*SSD1306_LCDHEIGHT/8); i++) { 546 | // send a bunch of data in one xmission 547 | Wire.beginTransmission(_i2caddr); 548 | WIRE_WRITE(0x40); 549 | for (uint8_t x=0; x<16; x++) { 550 | WIRE_WRITE(buffer[i]); 551 | i++; 552 | } 553 | i--; 554 | Wire.endTransmission(); 555 | } 556 | #ifndef __SAM3X8E__ 557 | // TWBR = twbrbackup; //commented for ESP8266 compatibility 558 | #endif 559 | } 560 | } 561 | 562 | // clear everything 563 | void ESP_SSD1306::clearDisplay(void) { 564 | memset(buffer, 0, (SSD1306_LCDWIDTH*SSD1306_LCDHEIGHT/8)); 565 | } 566 | 567 | 568 | inline void ESP_SSD1306::fastSPIwrite(uint8_t d) { 569 | 570 | if(hwSPI) { 571 | (void)SPI.transfer(d); 572 | } else { 573 | for(uint8_t bit = 0x80; bit; bit >>= 1) { 574 | *clkport &= ~clkpinmask; 575 | if(d & bit) *mosiport |= mosipinmask; 576 | else *mosiport &= ~mosipinmask; 577 | *clkport |= clkpinmask; 578 | } 579 | } 580 | //*csport |= cspinmask; 581 | } 582 | 583 | void ESP_SSD1306::drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color) { 584 | boolean bSwap = false; 585 | switch(rotation) { 586 | case 0: 587 | // 0 degree rotation, do nothing 588 | break; 589 | case 1: 590 | // 90 degree rotation, swap x & y for rotation, then invert x 591 | bSwap = true; 592 | swap(x, y); 593 | x = WIDTH - x - 1; 594 | break; 595 | case 2: 596 | // 180 degree rotation, invert x and y - then shift y around for height. 597 | x = WIDTH - x - 1; 598 | y = HEIGHT - y - 1; 599 | x -= (w-1); 600 | break; 601 | case 3: 602 | // 270 degree rotation, swap x & y for rotation, then invert y and adjust y for w (not to become h) 603 | bSwap = true; 604 | swap(x, y); 605 | y = HEIGHT - y - 1; 606 | y -= (w-1); 607 | break; 608 | } 609 | 610 | if(bSwap) { 611 | drawFastVLineInternal(x, y, w, color); 612 | } else { 613 | drawFastHLineInternal(x, y, w, color); 614 | } 615 | } 616 | 617 | void ESP_SSD1306::drawFastHLineInternal(int16_t x, int16_t y, int16_t w, uint16_t color) { 618 | // Do bounds/limit checks 619 | if(y < 0 || y >= HEIGHT) { return; } 620 | 621 | // make sure we don't try to draw below 0 622 | if(x < 0) { 623 | w += x; 624 | x = 0; 625 | } 626 | 627 | // make sure we don't go off the edge of the display 628 | if( (x + w) > WIDTH) { 629 | w = (WIDTH - x); 630 | } 631 | 632 | // if our width is now negative, punt 633 | if(w <= 0) { return; } 634 | 635 | // set up the pointer for movement through the buffer 636 | register uint8_t *pBuf = buffer; 637 | // adjust the buffer pointer for the current row 638 | pBuf += ((y/8) * SSD1306_LCDWIDTH); 639 | // and offset x columns in 640 | pBuf += x; 641 | 642 | register uint8_t mask = 1 << (y&7); 643 | 644 | switch (color) 645 | { 646 | case WHITE: while(w--) { *pBuf++ |= mask; }; break; 647 | case BLACK: mask = ~mask; while(w--) { *pBuf++ &= mask; }; break; 648 | case INVERSE: while(w--) { *pBuf++ ^= mask; }; break; 649 | } 650 | } 651 | 652 | void ESP_SSD1306::drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color) { 653 | bool bSwap = false; 654 | switch(rotation) { 655 | case 0: 656 | break; 657 | case 1: 658 | // 90 degree rotation, swap x & y for rotation, then invert x and adjust x for h (now to become w) 659 | bSwap = true; 660 | swap(x, y); 661 | x = WIDTH - x - 1; 662 | x -= (h-1); 663 | break; 664 | case 2: 665 | // 180 degree rotation, invert x and y - then shift y around for height. 666 | x = WIDTH - x - 1; 667 | y = HEIGHT - y - 1; 668 | y -= (h-1); 669 | break; 670 | case 3: 671 | // 270 degree rotation, swap x & y for rotation, then invert y 672 | bSwap = true; 673 | swap(x, y); 674 | y = HEIGHT - y - 1; 675 | break; 676 | } 677 | 678 | if(bSwap) { 679 | drawFastHLineInternal(x, y, h, color); 680 | } else { 681 | drawFastVLineInternal(x, y, h, color); 682 | } 683 | } 684 | 685 | 686 | void ESP_SSD1306::drawFastVLineInternal(int16_t x, int16_t __y, int16_t __h, uint16_t color) { 687 | 688 | // do nothing if we're off the left or right side of the screen 689 | if(x < 0 || x >= WIDTH) { return; } 690 | 691 | // make sure we don't try to draw below 0 692 | if(__y < 0) { 693 | // __y is negative, this will subtract enough from __h to account for __y being 0 694 | __h += __y; 695 | __y = 0; 696 | 697 | } 698 | 699 | // make sure we don't go past the height of the display 700 | if( (__y + __h) > HEIGHT) { 701 | __h = (HEIGHT - __y); 702 | } 703 | 704 | // if our height is now negative, punt 705 | if(__h <= 0) { 706 | return; 707 | } 708 | 709 | // this display doesn't need ints for coordinates, use local byte registers for faster juggling 710 | register uint8_t y = __y; 711 | register uint8_t h = __h; 712 | 713 | 714 | // set up the pointer for fast movement through the buffer 715 | register uint8_t *pBuf = buffer; 716 | // adjust the buffer pointer for the current row 717 | pBuf += ((y/8) * SSD1306_LCDWIDTH); 718 | // and offset x columns in 719 | pBuf += x; 720 | 721 | // do the first partial byte, if necessary - this requires some masking 722 | register uint8_t mod = (y&7); 723 | if(mod) { 724 | // mask off the high n bits we want to set 725 | mod = 8-mod; 726 | 727 | // note - lookup table results in a nearly 10% performance improvement in fill* functions 728 | // register uint8_t mask = ~(0xFF >> (mod)); 729 | static uint8_t premask[8] = {0x00, 0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xFE }; 730 | register uint8_t mask = premask[mod]; 731 | 732 | // adjust the mask if we're not going to reach the end of this byte 733 | if( h < mod) { 734 | mask &= (0XFF >> (mod-h)); 735 | } 736 | 737 | switch (color) 738 | { 739 | case WHITE: *pBuf |= mask; break; 740 | case BLACK: *pBuf &= ~mask; break; 741 | case INVERSE: *pBuf ^= mask; break; 742 | } 743 | 744 | // fast exit if we're done here! 745 | if(h= 8) { 755 | if (color == INVERSE) { // separate copy of the code so we don't impact performance of the black/white write version with an extra comparison per loop 756 | do { 757 | *pBuf=~(*pBuf); 758 | 759 | // adjust the buffer forward 8 rows worth of data 760 | pBuf += SSD1306_LCDWIDTH; 761 | 762 | // adjust h & y (there's got to be a faster way for me to do this, but this should still help a fair bit for now) 763 | h -= 8; 764 | } while(h >= 8); 765 | } 766 | else { 767 | // store a local value to work with 768 | register uint8_t val = (color == WHITE) ? 255 : 0; 769 | 770 | do { 771 | // write our value in 772 | *pBuf = val; 773 | 774 | // adjust the buffer forward 8 rows worth of data 775 | pBuf += SSD1306_LCDWIDTH; 776 | 777 | // adjust h & y (there's got to be a faster way for me to do this, but this should still help a fair bit for now) 778 | h -= 8; 779 | } while(h >= 8); 780 | } 781 | } 782 | 783 | // now do the final partial byte, if necessary 784 | if(h) { 785 | mod = h & 7; 786 | // this time we want to mask the low bits of the byte, vs the high bits we did above 787 | // register uint8_t mask = (1 << mod) - 1; 788 | // note - lookup table results in a nearly 10% performance improvement in fill* functions 789 | static uint8_t postmask[8] = {0x00, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F }; 790 | register uint8_t mask = postmask[mod]; 791 | switch (color) 792 | { 793 | case WHITE: *pBuf |= mask; break; 794 | case BLACK: *pBuf &= ~mask; break; 795 | case INVERSE: *pBuf ^= mask; break; 796 | } 797 | } 798 | } 799 | -------------------------------------------------------------------------------- /lib/ESP_SSD1306/ESP_SSD1306.h: -------------------------------------------------------------------------------- 1 | /********************************************************************* 2 | * 3 | ** Modified Adafruit Library to work with OLED SSD1306_128_64 with SPI communication only ** 4 | * By: Jordi Segura 5 | * 6 | This is a library for Monochrome OLEDs based on SSD1306 drivers 7 | 8 | Pick one up today in the adafruit shop! 9 | ------> http://www.adafruit.com/category/63_98 10 | 11 | These displays use SPI to communicate, 4 or 5 pins are required to 12 | interface 13 | 14 | Adafruit invests time and resources providing this open source code, 15 | please support Adafruit and open-source hardware by purchasing 16 | products from Adafruit! 17 | 18 | Written by Limor Fried/Ladyada for Adafruit Industries. 19 | BSD license, check license.txt for more information 20 | All text above, and the splash screen must be included in any redistribution 21 | *********************************************************************/ 22 | 23 | #if ARDUINO >= 100 24 | #include "Arduino.h" 25 | #define WIRE_WRITE Wire.write 26 | #else 27 | #include "WProgram.h" 28 | #define WIRE_WRITE Wire.send 29 | #endif 30 | 31 | #ifdef __SAM3X8E__ 32 | typedef volatile RwReg PortReg; 33 | typedef uint32_t PortMask; 34 | #else 35 | typedef volatile uint8_t PortReg; 36 | typedef uint8_t PortMask; 37 | #endif 38 | 39 | #include 40 | #include "../Adafruit_GFX/Adafruit_GFX.h" 41 | 42 | /* For the Adafruit_GFX library to work with the ESP8266, please be sure to include the "#elif defined ESP8266" line in Adafruit_GFX.cpp 43 | Otherwise comment the line #define pgm_read_byte(addr)... 44 | 45 | #ifdef __AVR__ 46 | #include 47 | #elif defined ESP8266 //Added for compatibility with ESP8266 BOARD 48 | #else 49 | #define pgm_read_byte(addr) (*(const unsigned char *)(addr)) 50 | #endif 51 | */ 52 | 53 | 54 | #define BLACK 0 55 | #define WHITE 1 56 | #define INVERSE 2 57 | 58 | #define SSD1306_I2C_ADDRESS 0x3C // 011110+SA0+RW - 0x3C or 0x3D 59 | // Address for 128x32 is 0x3C 60 | // Address for 128x64 is 0x3D (default) or 0x3C (if SA0 is grounded) 61 | 62 | /*========================================================================= 63 | SSD1306 Displays 64 | ----------------------------------------------------------------------- 65 | The driver is used in multiple displays (128x64, 128x32, etc.). 66 | Select the appropriate display below to create an appropriately 67 | sized framebuffer, etc. 68 | 69 | SSD1306_128_64 128x64 pixel display 70 | 71 | SSD1306_128_32 128x32 pixel display 72 | 73 | SSD1306_96_16 74 | 75 | -----------------------------------------------------------------------*/ 76 | #define SSD1306_128_64 77 | // #define SSD1306_128_32 78 | // #define SSD1306_96_16 79 | /*=========================================================================*/ 80 | 81 | #if defined SSD1306_128_64 && defined SSD1306_128_32 82 | #error "Only one SSD1306 display can be specified at once in SSD1306.h" 83 | #endif 84 | #if !defined SSD1306_128_64 && !defined SSD1306_128_32 && !defined SSD1306_96_16 85 | #error "At least one SSD1306 display must be specified in SSD1306.h" 86 | #endif 87 | 88 | #if defined SSD1306_128_64 89 | #define SSD1306_LCDWIDTH 128 90 | #define SSD1306_LCDHEIGHT 64 91 | #endif 92 | #if defined SSD1306_128_32 93 | #define SSD1306_LCDWIDTH 128 94 | #define SSD1306_LCDHEIGHT 32 95 | #endif 96 | #if defined SSD1306_96_16 97 | #define SSD1306_LCDWIDTH 96 98 | #define SSD1306_LCDHEIGHT 16 99 | #endif 100 | 101 | #define SSD1306_SETCONTRAST 0x81 102 | #define SSD1306_DISPLAYALLON_RESUME 0xA4 103 | #define SSD1306_DISPLAYALLON 0xA5 104 | #define SSD1306_NORMALDISPLAY 0xA6 105 | #define SSD1306_INVERTDISPLAY 0xA7 106 | #define SSD1306_DISPLAYOFF 0xAE 107 | #define SSD1306_DISPLAYON 0xAF 108 | 109 | #define SSD1306_SETDISPLAYOFFSET 0xD3 110 | #define SSD1306_SETCOMPINS 0xDA 111 | 112 | #define SSD1306_SETVCOMDETECT 0xDB 113 | 114 | #define SSD1306_SETDISPLAYCLOCKDIV 0xD5 115 | #define SSD1306_SETPRECHARGE 0xD9 116 | 117 | #define SSD1306_SETMULTIPLEX 0xA8 118 | 119 | #define SSD1306_SETLOWCOLUMN 0x00 120 | #define SSD1306_SETHIGHCOLUMN 0x10 121 | 122 | #define SSD1306_SETSTARTLINE 0x40 123 | 124 | #define SSD1306_MEMORYMODE 0x20 125 | #define SSD1306_COLUMNADDR 0x21 126 | #define SSD1306_PAGEADDR 0x22 127 | 128 | #define SSD1306_COMSCANINC 0xC0 129 | #define SSD1306_COMSCANDEC 0xC8 130 | 131 | #define SSD1306_SEGREMAP 0xA0 132 | 133 | #define SSD1306_CHARGEPUMP 0x8D 134 | 135 | #define SSD1306_EXTERNALVCC 0x1 136 | #define SSD1306_SWITCHCAPVCC 0x2 137 | 138 | // Scrolling #defines 139 | #define SSD1306_ACTIVATE_SCROLL 0x2F 140 | #define SSD1306_DEACTIVATE_SCROLL 0x2E 141 | #define SSD1306_SET_VERTICAL_SCROLL_AREA 0xA3 142 | #define SSD1306_RIGHT_HORIZONTAL_SCROLL 0x26 143 | #define SSD1306_LEFT_HORIZONTAL_SCROLL 0x27 144 | #define SSD1306_VERTICAL_AND_RIGHT_HORIZONTAL_SCROLL 0x29 145 | #define SSD1306_VERTICAL_AND_LEFT_HORIZONTAL_SCROLL 0x2A 146 | 147 | class ESP_SSD1306 : public Adafruit_GFX { 148 | public: 149 | ESP_SSD1306(int8_t SID, int8_t SCLK, int8_t DC, int8_t RST, int8_t CS); 150 | ESP_SSD1306(int8_t DC, int8_t RST, int8_t CS); //Constructor for hardware SPI 151 | ESP_SSD1306(int8_t RST); 152 | 153 | void begin(uint8_t switchvcc = SSD1306_SWITCHCAPVCC, uint8_t i2caddr = SSD1306_I2C_ADDRESS, bool reset=true); 154 | void ssd1306_command(uint8_t c); 155 | void ssd1306_data(uint8_t c); 156 | 157 | void clearDisplay(void); 158 | void invertDisplay(uint8_t i); 159 | void display(); 160 | 161 | void startscrollright(uint8_t start, uint8_t stop); 162 | void startscrollleft(uint8_t start, uint8_t stop); 163 | 164 | void startscrolldiagright(uint8_t start, uint8_t stop); 165 | void startscrolldiagleft(uint8_t start, uint8_t stop); 166 | void stopscroll(void); 167 | 168 | void dim(boolean dim); 169 | 170 | void drawPixel(int16_t x, int16_t y, uint16_t color); 171 | 172 | virtual void drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color); 173 | virtual void drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color); 174 | 175 | private: 176 | int8_t _i2caddr, _vccstate, sid, sclk, dc, rst, cs; 177 | void fastSPIwrite(uint8_t c); 178 | 179 | boolean hwSPI; 180 | PortReg *mosiport, *clkport, *csport, *dcport; 181 | PortMask mosipinmask, clkpinmask, cspinmask, dcpinmask; 182 | 183 | inline void drawFastVLineInternal(int16_t x, int16_t y, int16_t h, uint16_t color) __attribute__((always_inline)); 184 | inline void drawFastHLineInternal(int16_t x, int16_t y, int16_t w, uint16_t color) __attribute__((always_inline)); 185 | 186 | }; 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | -------------------------------------------------------------------------------- /lib/readme.txt: -------------------------------------------------------------------------------- 1 | 2 | This directory is intended for the project specific (private) libraries. 3 | PlatformIO will compile them to static libraries and link to executable file. 4 | 5 | The source code of each library should be placed in separate directory, like 6 | "lib/private_lib/[here are source files]". 7 | 8 | For example, see how can be organised `Foo` and `Bar` libraries: 9 | 10 | |--lib 11 | | |--Bar 12 | | | |--docs 13 | | | |--examples 14 | | | |--src 15 | | | |- Bar.c 16 | | | |- Bar.h 17 | | |--Foo 18 | | | |- Foo.c 19 | | | |- Foo.h 20 | | |- readme.txt --> THIS FILE 21 | |- platformio.ini 22 | |--src 23 | |- main.c 24 | 25 | Then in `src/main.c` you should use: 26 | 27 | #include 28 | #include 29 | 30 | // rest H/C/CPP code 31 | 32 | PlatformIO will find your libraries automatically, configure preprocessor's 33 | include paths and build them. 34 | 35 | See additional options for PlatformIO Library Dependency Finder `lib_*`: 36 | 37 | http://docs.platformio.org/en/latest/projectconf.html#lib-install 38 | 39 | -------------------------------------------------------------------------------- /platformio.ini: -------------------------------------------------------------------------------- 1 | # 2 | # Project Configuration File 3 | # 4 | # A detailed documentation with the EXAMPLES is located here: 5 | # http://docs.platformio.org/en/latest/projectconf.html 6 | # 7 | 8 | # A sign `#` at the beginning of the line indicates a comment 9 | # Comment lines are ignored. 10 | 11 | # Simple and base environment 12 | # [env:mybaseenv] 13 | # platform = %INSTALLED_PLATFORM_NAME_HERE% 14 | # framework = 15 | # board = 16 | # 17 | # Automatic targets - enable auto-uploading 18 | # targets = upload 19 | 20 | [env:esp12e] 21 | platform = espressif 22 | framework = arduino 23 | board = esp12e 24 | 25 | # Button lib 26 | lib_install=77 27 | -------------------------------------------------------------------------------- /src/main.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include // only for compilation 4 | #include 5 | #include 6 | #include 7 | 8 | #define BUTTON_PIN D2 9 | #define PULLUP true 10 | #define INVERT true 11 | #define DEBOUNCE_MS 20 12 | 13 | #define OLED_RESET 16 14 | #define PIN_SDA D7 15 | #define PIN_SCL D6 16 | #define I2C_ADDRESS 0x3c 17 | 18 | #define HWX_LOGO_W 128 19 | #define HWX_LOGO_H 49 20 | #define OLED_WIDTH 128 21 | #define OLED_HEIGHT 64 22 | 23 | Button btn(BUTTON_PIN, PULLUP, INVERT, DEBOUNCE_MS); 24 | ESP_SSD1306 oled(OLED_RESET); 25 | 26 | unsigned long pressedAtMillis; 27 | unsigned long const interval = 5000; 28 | unsigned long pressedForMillis; 29 | 30 | // hortonworks logo for a splash screen 31 | byte img[] = { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 32 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 33 | 0xe0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 34 | 0x1, 0xfc, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 35 | 0x0, 0x1, 0xfc, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 36 | 0x0, 0x0, 0x3, 0xfe, 0xf8, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 37 | 0x10, 0x0, 0x0, 0x0, 0x7b, 0xfe, 0x64, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 38 | 0x0, 0x0, 0x1f, 0x0, 0x0, 0xf, 0xf3, 0xff, 0x76, 0x0, 0x0, 0x0, 0x0, 0x0, 39 | 0x0, 0x0, 0x0, 0x0, 0x3f, 0xa0, 0x0, 0x1f, 0xf7, 0xff, 0x7e, 0x0, 0x0, 0x0, 40 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x3f, 0x9e, 0x0, 0x1f, 0xf7, 0xff, 0x3f, 0x0, 41 | 0x0, 0x0, 0x0, 0x0, 0x4, 0x0, 0x0, 0xf, 0xbf, 0xdb, 0x0, 0x3f, 0xf3, 0xff, 42 | 0xbf, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0x80, 0x0, 0x7f, 0x7f, 0xdf, 0x0, 0x7f, 43 | 0xf9, 0xff, 0x9f, 0x80, 0x0, 0x0, 0x0, 0x0, 0xf, 0xd8, 0x0, 0xff, 0x7f, 44 | 0xcf, 0x80, 0xff, 0xfc, 0xff, 0xdf, 0x80, 0x0, 0x0, 0x0, 0x1, 0xcf, 0xda, 45 | 0x1, 0xff, 0x3f, 0xef, 0x80, 0xff, 0xfe, 0x7f, 0xcf, 0x80, 0x0, 0x0, 0x0, 46 | 0xf, 0xdf, 0xce, 0x1, 0xff, 0x9f, 0xe7, 0xc1, 0xff, 0xff, 0x9f, 0xe7, 0x80, 47 | 0x0, 0x0, 0x0, 0x1f, 0xdf, 0xef, 0x3, 0xff, 0xcf, 0xf7, 0xc1, 0x7f, 0xff, 48 | 0xcf, 0xe3, 0x80, 0x0, 0x0, 0x0, 0x1f, 0xcf, 0xe7, 0x3, 0xff, 0xf7, 0xf1, 49 | 0xc1, 0x3f, 0xff, 0xe0, 0x3, 0x80, 0x0, 0x0, 0x0, 0x3f, 0xe7, 0xf7, 0x87, 50 | 0xff, 0xf9, 0xf9, 0xc1, 0x1f, 0xff, 0xf8, 0x3, 0x80, 0x0, 0x0, 0x0, 0x7f, 51 | 0xf3, 0xf3, 0x84, 0xff, 0xfc, 0x1, 0xc2, 0xdf, 0xff, 0xfe, 0x3, 0x80, 0x0, 52 | 0x0, 0x0, 0x7f, 0xfc, 0xf1, 0x84, 0xff, 0xff, 0x81, 0xc2, 0xdf, 0xff, 0xfe, 53 | 0x3, 0x88, 0x0, 0x0, 0x0, 0x5f, 0xfe, 0x1, 0x8a, 0x7f, 0xff, 0xc1, 0xc0, 54 | 0xcf, 0xfe, 0x3f, 0x3, 0x8c, 0x0, 0x0, 0x0, 0xf, 0xff, 0x81, 0x8b, 0x7f, 55 | 0xe7, 0xc1, 0xc0, 0xef, 0xe0, 0x9f, 0x3, 0x80, 0x0, 0x0, 0x0, 0x2f, 0xff, 56 | 0xc1, 0x83, 0x7f, 0x3, 0xc1, 0x81, 0xef, 0x81, 0xdf, 0x1, 0xb0, 0x0, 0x0, 57 | 0x0, 0x2f, 0xe1, 0xc1, 0x83, 0x3c, 0x1b, 0xc0, 0x99, 0xe7, 0x81, 0xcf, 0x0, 58 | 0x38, 0x0, 0x0, 0x0, 0x67, 0x5, 0xe1, 0xa7, 0x3c, 0x19, 0xe0, 0x11, 0xe7, 59 | 0x81, 0xcf, 0x80, 0x0, 0x0, 0x0, 0x0, 0x77, 0x4, 0xe0, 0x7, 0x3c, 0x19, 60 | 0xe0, 0x1, 0xc7, 0x81, 0xc7, 0x80, 0x0, 0x0, 0x0, 0x0, 0x67, 0x4, 0xe0, 0x7, 61 | 0x1c, 0x18, 0xe0, 0x1, 0xc3, 0x81, 0xc3, 0x80, 0x0, 0x0, 0x0, 0x0, 0x63, 62 | 0xc, 0x70, 0x7, 0x1c, 0x18, 0xf0, 0x1, 0xc3, 0xc1, 0xc3, 0xc0, 0x0, 0x0, 63 | 0x0, 0x0, 0x63, 0xc, 0x70, 0x7, 0x1c, 0x18, 0x70, 0x1, 0xc3, 0xc1, 0xc1, 64 | 0xc0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 65 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 66 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 67 | 0x0, 0x0, 0x1, 0xc0, 0x0, 0x0, 0x70, 0x38, 0x0, 0x0, 0xe, 0x0, 0x0, 0x0, 68 | 0x0, 0x0, 0x0, 0x0, 0x1, 0xc0, 0x0, 0x14, 0x70, 0x38, 0x0, 0x0, 0xe, 0x0, 69 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0xc0, 0x0, 0x2a, 0x70, 0x38, 0x0, 0x0, 70 | 0xe, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0xc0, 0x0, 0x34, 0x70, 0x38, 71 | 0x0, 0x0, 0xe, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0xc0, 0x0, 0x14, 72 | 0x70, 0x38, 0x0, 0x0, 0xe, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0xc0, 73 | 0x0, 0x0, 0x70, 0x38, 0x7e, 0xf, 0xdf, 0x87, 0xe0, 0xff, 0x38, 0x70, 0xe3, 74 | 0xf0, 0xfd, 0xc7, 0x8f, 0xc0, 0x7f, 0xf8, 0xff, 0x9f, 0xdf, 0xcf, 0xf8, 75 | 0xff, 0xb8, 0x70, 0xe7, 0xf8, 0xff, 0xcf, 0x9f, 0xe0, 0x7f, 0xf9, 0xf7, 76 | 0xdf, 0xdf, 0x9f, 0x7c, 0xff, 0x9c, 0xf9, 0xcf, 0x7c, 0xfd, 0xcf, 0x3c, 77 | 0xf0, 0x7f, 0xf9, 0xc1, 0xde, 0xe, 0x1c, 0x1c, 0xe1, 0xdc, 0xf9, 0xde, 0x1e, 78 | 0xf1, 0xfe, 0x38, 0x0, 0x7f, 0xfb, 0xc1, 0xce, 0xe, 0x3c, 0x1c, 0xe1, 0xdc, 79 | 0xf9, 0xdc, 0xe, 0xe1, 0xfc, 0x3f, 0x80, 0x70, 0x3b, 0x81, 0xdc, 0xe, 0x38, 80 | 0x1c, 0xe1, 0xcc, 0xd9, 0x9c, 0xe, 0xe1, 0xfc, 0x1f, 0xe0, 0x70, 0x3b, 0x81, 81 | 0xcc, 0xe, 0x3c, 0x1c, 0xe1, 0xcf, 0xdf, 0x9c, 0xe, 0xe1, 0xfe, 0x7, 0xf0, 82 | 0x70, 0x39, 0xc1, 0xcc, 0x7, 0x1c, 0x1c, 0xe1, 0xcf, 0xdf, 0x9c, 0x1e, 0xe1, 83 | 0xef, 0x0, 0x70, 0x70, 0x39, 0xe3, 0xdc, 0x7, 0x1e, 0x3c, 0xe1, 0xc7, 0x8f, 84 | 0x9e, 0x3c, 0xe1, 0xc7, 0x3c, 0x70, 0x70, 0x38, 0xff, 0x9c, 0x7, 0xcf, 0xf8, 85 | 0xe1, 0xc7, 0x8f, 0xf, 0xfc, 0xe1, 0xc7, 0x9f, 0xf0, 0x70, 0x38, 0x7f, 0x1c, 86 | 0x3, 0xc7, 0xf0, 0xe1, 0xc7, 0x8f, 0x7, 0xf8, 0xe1, 0xc3, 0xdf, 0xe0, 0x0, 87 | 0x0, 0x1c, 0x8, 0x0, 0x81, 0xc0, 0xc0, 0x83, 0x6, 0x1, 0xe0, 0x40, 0x81, 88 | 0x7, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 89 | 0x0, 0x0, 0x0 }; 90 | 91 | void splashLogo() { 92 | oled.clearDisplay(); 93 | // center the logo 94 | oled.drawBitmap((OLED_WIDTH - HWX_LOGO_W) / 2, 95 | (OLED_HEIGHT - HWX_LOGO_H) / 2, 96 | img, HWX_LOGO_W, HWX_LOGO_H, WHITE); 97 | oled.display(); 98 | } 99 | 100 | void setup() { 101 | Serial.begin(115200); 102 | // Wire.setClock(400000); 103 | Wire.begin(PIN_SDA, PIN_SCL); 104 | oled.begin(SSD1306_SWITCHCAPVCC, I2C_ADDRESS, true); 105 | // oled.clearDisplay(); 106 | splashLogo(); 107 | delay(2000); 108 | oled.clearDisplay(); 109 | 110 | oled.setTextSize(1); 111 | oled.setTextColor(WHITE); 112 | oled.setCursor(0, 0); 113 | oled.setTextWrap(true); 114 | 115 | connectWiFi(); 116 | } 117 | 118 | void connectWiFi() { 119 | WiFi.mode(WIFI_STA); 120 | 121 | // Start SmartConfig if necessary 122 | if (WiFi.SSID() == "") { 123 | 124 | Serial.println("Beginning SmartConfig"); 125 | oled.println("Beginning SmartConfig"); 126 | oled.display(); 127 | WiFi.beginSmartConfig(); 128 | 129 | while (WiFi.status() != WL_CONNECTED) { 130 | delay(1000); 131 | Serial.print("."); 132 | oled.print("."); 133 | oled.display(); 134 | if (WiFi.smartConfigDone()) { 135 | Serial.println(); 136 | Serial.println("SmartConfig completed"); 137 | oled.println(); 138 | oled.println("SmartConfig completed"); 139 | oled.display(); 140 | WiFi.stopSmartConfig(); 141 | delay(2000); 142 | break; 143 | } 144 | } 145 | } else { 146 | Serial.println(String("Using a saved SSID: ") + WiFi.SSID()); 147 | oled.println(String("Using a saved SSID: ") + WiFi.SSID()); 148 | oled.display(); 149 | } 150 | 151 | Serial.print("Connecting.."); 152 | oled.print("Connecting..."); 153 | oled.display(); 154 | WiFi.begin(); 155 | while (WiFi.status() != WL_CONNECTED) { 156 | delay(1000); 157 | Serial.print("."); 158 | oled.print("."); 159 | oled.display(); 160 | } 161 | Serial.println("Ok"); 162 | oled.println("Ok"); 163 | oled.display(); 164 | 165 | Serial.println(String("My IP: ") + WiFi.localIP().toString()); 166 | oled.println(String("My IP: ") + WiFi.localIP().toString()); 167 | oled.display(); 168 | delay(3000); 169 | } 170 | 171 | void displayOff() { 172 | return oled.ssd1306_command(0xAE); 173 | } 174 | 175 | void displayOn() { 176 | oled.ssd1306_command(0xAF); 177 | } 178 | 179 | void loop() { 180 | btn.read(); 181 | 182 | if (btn.wasPressed()) { 183 | pressedAtMillis = millis(); 184 | } 185 | 186 | if (btn.wasReleased()) { 187 | if (pressedForMillis > interval) { 188 | // we have a long press, reset 189 | displayOn(); 190 | Serial.println("Resetting WiFi credentials"); 191 | oled.invertDisplay(true); 192 | oled.println("Resetting WiFi credentials"); 193 | oled.display(); 194 | WiFi.disconnect(); 195 | 196 | delay(3000); 197 | ESP.restart(); 198 | } else { 199 | // short press, turn on the display 200 | displayOn(); 201 | splashLogo(); 202 | oled.clearDisplay(); 203 | delay(3000); 204 | // somewhere around mid-screen with this font and size 205 | oled.setCursor(0, 30); 206 | oled.println(String("My IP: ") + WiFi.localIP().toString()); 207 | oled.display(); 208 | delay(3000); 209 | displayOff(); 210 | } 211 | } 212 | 213 | pressedForMillis = millis() - pressedAtMillis; 214 | 215 | displayOff(); 216 | 217 | } 218 | --------------------------------------------------------------------------------