├── LICENSE ├── OLED.cpp ├── OLED.h ├── README.md ├── example └── example.ino ├── keywords.txt └── library.properties /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /OLED.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "OLED.h" 3 | 4 | //==========================================================// 5 | 6 | // Small 8x8 font 7 | static const char myFont[][8] PROGMEM = { 8 | {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, 9 | {0x00,0x00,0x5F,0x00,0x00,0x00,0x00,0x00}, 10 | {0x00,0x00,0x07,0x00,0x07,0x00,0x00,0x00}, 11 | {0x00,0x14,0x7F,0x14,0x7F,0x14,0x00,0x00}, 12 | {0x00,0x24,0x2A,0x7F,0x2A,0x12,0x00,0x00}, 13 | {0x00,0x23,0x13,0x08,0x64,0x62,0x00,0x00}, 14 | {0x00,0x36,0x49,0x55,0x22,0x50,0x00,0x00}, 15 | {0x00,0x00,0x05,0x03,0x00,0x00,0x00,0x00}, 16 | {0x00,0x1C,0x22,0x41,0x00,0x00,0x00,0x00}, 17 | {0x00,0x41,0x22,0x1C,0x00,0x00,0x00,0x00}, 18 | {0x00,0x08,0x2A,0x1C,0x2A,0x08,0x00,0x00}, 19 | {0x00,0x08,0x08,0x3E,0x08,0x08,0x00,0x00}, 20 | {0x00,0xA0,0x60,0x00,0x00,0x00,0x00,0x00}, 21 | {0x00,0x08,0x08,0x08,0x08,0x08,0x00,0x00}, 22 | {0x00,0x60,0x60,0x00,0x00,0x00,0x00,0x00}, 23 | {0x00,0x20,0x10,0x08,0x04,0x02,0x00,0x00}, 24 | {0x00,0x3E,0x51,0x49,0x45,0x3E,0x00,0x00}, 25 | {0x00,0x00,0x42,0x7F,0x40,0x00,0x00,0x00}, 26 | {0x00,0x62,0x51,0x49,0x49,0x46,0x00,0x00}, 27 | {0x00,0x22,0x41,0x49,0x49,0x36,0x00,0x00}, 28 | {0x00,0x18,0x14,0x12,0x7F,0x10,0x00,0x00}, 29 | {0x00,0x27,0x45,0x45,0x45,0x39,0x00,0x00}, 30 | {0x00,0x3C,0x4A,0x49,0x49,0x30,0x00,0x00}, 31 | {0x00,0x01,0x71,0x09,0x05,0x03,0x00,0x00}, 32 | {0x00,0x36,0x49,0x49,0x49,0x36,0x00,0x00}, 33 | {0x00,0x06,0x49,0x49,0x29,0x1E,0x00,0x00}, 34 | {0x00,0x00,0x36,0x36,0x00,0x00,0x00,0x00}, 35 | {0x00,0x00,0xAC,0x6C,0x00,0x00,0x00,0x00}, 36 | {0x00,0x08,0x14,0x22,0x41,0x00,0x00,0x00}, 37 | {0x00,0x14,0x14,0x14,0x14,0x14,0x00,0x00}, 38 | {0x00,0x41,0x22,0x14,0x08,0x00,0x00,0x00}, 39 | {0x00,0x02,0x01,0x51,0x09,0x06,0x00,0x00}, 40 | {0x00,0x32,0x49,0x79,0x41,0x3E,0x00,0x00}, 41 | {0x00,0x7E,0x09,0x09,0x09,0x7E,0x00,0x00}, 42 | {0x00,0x7F,0x49,0x49,0x49,0x36,0x00,0x00}, 43 | {0x00,0x3E,0x41,0x41,0x41,0x22,0x00,0x00}, 44 | {0x00,0x7F,0x41,0x41,0x22,0x1C,0x00,0x00}, 45 | {0x00,0x7F,0x49,0x49,0x49,0x41,0x00,0x00}, 46 | {0x00,0x7F,0x09,0x09,0x09,0x01,0x00,0x00}, 47 | {0x00,0x3E,0x41,0x41,0x51,0x72,0x00,0x00}, 48 | {0x00,0x7F,0x08,0x08,0x08,0x7F,0x00,0x00}, 49 | {0x00,0x41,0x7F,0x41,0x00,0x00,0x00,0x00}, 50 | {0x00,0x20,0x40,0x41,0x3F,0x01,0x00,0x00}, 51 | {0x00,0x7F,0x08,0x14,0x22,0x41,0x00,0x00}, 52 | {0x00,0x7F,0x40,0x40,0x40,0x40,0x00,0x00}, 53 | {0x00,0x7F,0x02,0x0C,0x02,0x7F,0x00,0x00}, 54 | {0x00,0x7F,0x04,0x08,0x10,0x7F,0x00,0x00}, 55 | {0x00,0x3E,0x41,0x41,0x41,0x3E,0x00,0x00}, 56 | {0x00,0x7F,0x09,0x09,0x09,0x06,0x00,0x00}, 57 | {0x00,0x3E,0x41,0x51,0x21,0x5E,0x00,0x00}, 58 | {0x00,0x7F,0x09,0x19,0x29,0x46,0x00,0x00}, 59 | {0x00,0x26,0x49,0x49,0x49,0x32,0x00,0x00}, 60 | {0x00,0x01,0x01,0x7F,0x01,0x01,0x00,0x00}, 61 | {0x00,0x3F,0x40,0x40,0x40,0x3F,0x00,0x00}, 62 | {0x00,0x1F,0x20,0x40,0x20,0x1F,0x00,0x00}, 63 | {0x00,0x3F,0x40,0x38,0x40,0x3F,0x00,0x00}, 64 | {0x00,0x63,0x14,0x08,0x14,0x63,0x00,0x00}, 65 | {0x00,0x03,0x04,0x78,0x04,0x03,0x00,0x00}, 66 | {0x00,0x61,0x51,0x49,0x45,0x43,0x00,0x00}, 67 | {0x00,0x7F,0x41,0x41,0x00,0x00,0x00,0x00}, 68 | {0x00,0x02,0x04,0x08,0x10,0x20,0x00,0x00}, 69 | {0x00,0x41,0x41,0x7F,0x00,0x00,0x00,0x00}, 70 | {0x00,0x04,0x02,0x01,0x02,0x04,0x00,0x00}, 71 | {0x00,0x80,0x80,0x80,0x80,0x80,0x00,0x00}, 72 | {0x00,0x01,0x02,0x04,0x00,0x00,0x00,0x00}, 73 | {0x00,0x20,0x54,0x54,0x54,0x78,0x00,0x00}, 74 | {0x00,0x7F,0x48,0x44,0x44,0x38,0x00,0x00}, 75 | {0x00,0x38,0x44,0x44,0x28,0x00,0x00,0x00}, 76 | {0x00,0x38,0x44,0x44,0x48,0x7F,0x00,0x00}, 77 | {0x00,0x38,0x54,0x54,0x54,0x18,0x00,0x00}, 78 | {0x00,0x08,0x7E,0x09,0x02,0x00,0x00,0x00}, 79 | {0x00,0x18,0xA4,0xA4,0xA4,0x7C,0x00,0x00}, 80 | {0x00,0x7F,0x08,0x04,0x04,0x78,0x00,0x00}, 81 | {0x00,0x00,0x7D,0x00,0x00,0x00,0x00,0x00}, 82 | {0x00,0x80,0x84,0x7D,0x00,0x00,0x00,0x00}, 83 | {0x00,0x7F,0x10,0x28,0x44,0x00,0x00,0x00}, 84 | {0x00,0x41,0x7F,0x40,0x00,0x00,0x00,0x00}, 85 | {0x00,0x7C,0x04,0x18,0x04,0x78,0x00,0x00}, 86 | {0x00,0x7C,0x08,0x04,0x7C,0x00,0x00,0x00}, 87 | {0x00,0x38,0x44,0x44,0x38,0x00,0x00,0x00}, 88 | {0x00,0xFC,0x24,0x24,0x18,0x00,0x00,0x00}, 89 | {0x00,0x18,0x24,0x24,0xFC,0x00,0x00,0x00}, 90 | {0x00,0x00,0x7C,0x08,0x04,0x00,0x00,0x00}, 91 | {0x00,0x48,0x54,0x54,0x24,0x00,0x00,0x00}, 92 | {0x00,0x04,0x7F,0x44,0x00,0x00,0x00,0x00}, 93 | {0x00,0x3C,0x40,0x40,0x7C,0x00,0x00,0x00}, 94 | {0x00,0x1C,0x20,0x40,0x20,0x1C,0x00,0x00}, 95 | {0x00,0x3C,0x40,0x30,0x40,0x3C,0x00,0x00}, 96 | {0x00,0x44,0x28,0x10,0x28,0x44,0x00,0x00}, 97 | {0x00,0x1C,0xA0,0xA0,0x7C,0x00,0x00,0x00}, 98 | {0x00,0x44,0x64,0x54,0x4C,0x44,0x00,0x00}, 99 | {0x00,0x08,0x36,0x41,0x00,0x00,0x00,0x00}, 100 | {0x00,0x00,0x7F,0x00,0x00,0x00,0x00,0x00}, 101 | {0x00,0x41,0x36,0x08,0x00,0x00,0x00,0x00}, 102 | {0x00,0x02,0x01,0x01,0x02,0x01,0x00,0x00}, 103 | {0x00,0x02,0x05,0x05,0x02,0x00,0x00,0x00} 104 | }; 105 | 106 | 107 | //==========================================================// 108 | // Resets display depending on the actual mode. 109 | void OLED::reset_display(void) 110 | { 111 | displayOff(); 112 | clear_display(); 113 | displayOn(); 114 | } 115 | 116 | //==========================================================// 117 | // Turns display on. 118 | void OLED::displayOn(void) 119 | { 120 | sendcommand(0xaf); //display on 121 | } 122 | 123 | //==========================================================// 124 | // Turns display off. 125 | void OLED::displayOff(void) 126 | { 127 | sendcommand(0xae); //display off 128 | } 129 | 130 | //==========================================================// 131 | // Clears the display by sendind 0 to all the screen map. 132 | void OLED::clear_display(void) 133 | { 134 | unsigned char i,k; 135 | for(k=0;k<8;k++) 136 | { 137 | setXY(k,0); 138 | { 139 | for(i=0;i<(128 + 2 * _offset);i++) //locate all COL 140 | { 141 | SendChar(0); //clear all COL 142 | //delay(10); 143 | } 144 | } 145 | } 146 | } 147 | 148 | //==========================================================// 149 | // Actually this sends a byte, not a char to draw in the display. 150 | // Display's chars uses 8 byte font the small ones and 96 bytes 151 | // for the big number font. 152 | void OLED::SendChar(unsigned char data) 153 | { 154 | //if (interrupt && !doing_menu) return; // Stop printing only if interrupt is call but not in button functions 155 | 156 | Wire.beginTransmission(_address); // begin transmitting 157 | Wire.write(0x40);//data mode 158 | Wire.write(data); 159 | Wire.endTransmission(); // stop transmitting 160 | } 161 | 162 | //==========================================================// 163 | // Prints a display char (not just a byte) in coordinates X Y, 164 | // being multiples of 8. This means we have 16 COLS (0-15) 165 | // and 8 ROWS (0-7). 166 | void OLED::sendCharXY(unsigned char data, int X, int Y) 167 | { 168 | setXY(X, Y); 169 | Wire.beginTransmission(_address); // begin transmitting 170 | Wire.write(0x40);//data mode 171 | 172 | for(int i=0;i<8;i++) 173 | Wire.write(pgm_read_byte(myFont[data-0x20]+i)); 174 | 175 | Wire.endTransmission(); // stop transmitting 176 | } 177 | 178 | //==========================================================// 179 | // Used to send commands to the display. 180 | void OLED::sendcommand(unsigned char com) 181 | { 182 | Wire.beginTransmission(_address); //begin transmitting 183 | Wire.write(0x80); //command mode 184 | Wire.write(com); 185 | Wire.endTransmission(); // stop transmitting 186 | } 187 | 188 | //==========================================================// 189 | // Set the cursor position in a 16 COL * 8 ROW map. 190 | void OLED::setXY(unsigned char row,unsigned char col) 191 | { 192 | sendcommand(0xb0+row); //set page address 193 | sendcommand(_offset+(8*col&0x0f)); //set low col address 194 | sendcommand(0x10+((8*col>>4)&0x0f)); //set high col address 195 | } 196 | 197 | 198 | //==========================================================// 199 | // Prints a string regardless the cursor position. 200 | void OLED::sendStr(unsigned char *string) 201 | { 202 | unsigned char i=0; 203 | while(*string) 204 | { 205 | for(i=0;i<8;i++) 206 | { 207 | SendChar(pgm_read_byte(myFont[*string-0x20]+i)); 208 | } 209 | *string++; 210 | } 211 | } 212 | 213 | //==========================================================// 214 | // Prints a string in coordinates X Y, being multiples of 8. 215 | // This means we have 16 COLS (0-15) and 8 ROWS (0-7). 216 | void OLED::sendStrXY( const char *string, int X, int Y) 217 | { 218 | setXY(X,Y); 219 | unsigned char i=0; 220 | while(*string) 221 | { 222 | for(i=0;i<8;i++) 223 | { 224 | SendChar(pgm_read_byte(myFont[*string-0x20]+i)); 225 | } 226 | *string++; 227 | } 228 | } 229 | 230 | 231 | //==========================================================// 232 | // Inits oled and draws logo at startup 233 | void OLED::init_OLED(void) 234 | { 235 | sendcommand(0xae); //display off 236 | sendcommand(0xa6); //Set Normal Display (default) 237 | // Adafruit Init sequence for 128x64 OLED module 238 | sendcommand(0xAE); //DISPLAYOFF 239 | sendcommand(0xD5); //SETDISPLAYCLOCKDIV 240 | sendcommand(0x80); // the suggested ratio 0x80 241 | sendcommand(0xA8); //SSD1306_SETMULTIPLEX 242 | sendcommand(0x3F); 243 | sendcommand(0xD3); //SETDISPLAYOFFSET 244 | sendcommand(0x0); //no offset 245 | sendcommand(0x40 | 0x0); //SETSTARTLINE 246 | sendcommand(0x8D); //CHARGEPUMP 247 | sendcommand(0x14); 248 | sendcommand(0x20); //MEMORYMODE 249 | sendcommand(0x00); //0x0 act like ks0108 250 | 251 | //sendcommand(0xA0 | 0x1); //SEGREMAP //Rotate screen 180 deg 252 | sendcommand(0xA0); 253 | 254 | //sendcommand(0xC8); //COMSCANDEC Rotate screen 180 Deg 255 | sendcommand(0xC0); 256 | 257 | sendcommand(0xDA); //0xDA 258 | sendcommand(0x12); //COMSCANDEC 259 | sendcommand(0x81); //SETCONTRAS 260 | sendcommand(0xCF); // 261 | sendcommand(0xd9); //SETPRECHARGE 262 | sendcommand(0xF1); 263 | sendcommand(0xDB); //SETVCOMDETECT 264 | sendcommand(0x40); 265 | sendcommand(0xA4); //DISPLAYALLON_RESUME 266 | sendcommand(0xA6); //NORMALDISPLAY 267 | 268 | clear_display(); 269 | sendcommand(0x2e); // stop scroll 270 | //----------------------------REVERSE comments----------------------------// 271 | sendcommand(0xa0); //seg re-map 0->127(default) 272 | sendcommand(0xa1); //seg re-map 127->0 273 | sendcommand(0xc8); 274 | delay(1000); 275 | //----------------------------REVERSE comments----------------------------// 276 | // sendcommand(0xa7); //Set Inverse Display 277 | // sendcommand(0xae); //display off 278 | sendcommand(0x20); //Set Memory Addressing Mode 279 | sendcommand(0x00); //Set Memory Addressing Mode ab Horizontal addressing mode 280 | // sendcommand(0x02); // Set Memory Addressing Mode ab Page addressing mode(RESET) 281 | } 282 | 283 | 284 | //==========================================================// 285 | 286 | 287 | OLED::OLED(uint8_t sda, uint8_t scl, uint8_t address, uint8_t offset) { 288 | _sda = sda; 289 | _scl = scl; 290 | _address = address; 291 | _offset = offset; 292 | } 293 | 294 | void OLED::begin(void) { 295 | // set up i2c 296 | Wire.begin(_sda, _scl); 297 | init_OLED(); 298 | reset_display(); 299 | } 300 | 301 | void OLED::on(void) { 302 | DEBUG_PRINTLN("on"); 303 | displayOn(); 304 | } 305 | 306 | void OLED::off(void) { 307 | DEBUG_PRINTLN("off"); 308 | displayOff(); 309 | } 310 | 311 | void OLED::clear(void) { 312 | DEBUG_PRINTLN("clear"); 313 | clear_display(); 314 | } 315 | 316 | void OLED::print(char *s, uint8_t r, uint8_t c) { 317 | DEBUG_PRINT("print "); 318 | DEBUG_PRINT(r); 319 | DEBUG_PRINT(","); 320 | DEBUG_PRINT(c); 321 | DEBUG_PRINT(" "); 322 | DEBUG_PRINTLN(s); 323 | sendStrXY(s, r, c); 324 | } 325 | 326 | -------------------------------------------------------------------------------- /OLED.h: -------------------------------------------------------------------------------- 1 | #ifndef OLED_H 2 | #define OLED_H 3 | 4 | #if ARDUINO >= 100 5 | #include "Arduino.h" 6 | #else 7 | #include "WProgram.h" 8 | #endif 9 | 10 | // Uncomment to enable printing out nice debug messages. 11 | //#define OLED_DEBUG 12 | 13 | // Define where debug output will be printed. 14 | #define DEBUG_PRINTER Serial 15 | 16 | // Setup debug printing macros. 17 | #ifdef OLED_DEBUG 18 | #define DEBUG_PRINT(...) { DEBUG_PRINTER.print(__VA_ARGS__); } 19 | #define DEBUG_PRINTLN(...) { DEBUG_PRINTER.println(__VA_ARGS__); } 20 | #else 21 | #define DEBUG_PRINT(...) {} 22 | #define DEBUG_PRINTLN(...) {} 23 | #endif 24 | 25 | class OLED { 26 | public: 27 | OLED(uint8_t sda, uint8_t scl, uint8_t address=0x3c, uint8_t offset=0); 28 | void begin(void); 29 | void on(void); 30 | void off(void); 31 | void clear(void); 32 | void print(char *s, uint8_t r=0, uint8_t c=0); 33 | 34 | private: 35 | uint8_t _sda, _scl, _address, _offset; 36 | void reset_display(void); 37 | void displayOn(void); 38 | void displayOff(void); 39 | void clear_display(void); 40 | void SendChar(unsigned char data); 41 | void sendCharXY(unsigned char data, int X, int Y); 42 | void sendcommand(unsigned char com); 43 | void setXY(unsigned char row,unsigned char col); 44 | void sendStr(unsigned char *string); 45 | void sendStrXY( const char *string, int X, int Y); 46 | void init_OLED(void); 47 | }; 48 | 49 | #endif 50 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | This is an [esp8266-Arduino](https://github.com/esp8266/Arduino) library for I2C-OLED displays. 2 | 3 | ## Credits 4 | 5 | Original code was taken from [ESP8266-I2C-OLED](https://github.com/costonisp/ESP8266-I2C-OLED) project and was modified in form of a library suitable for using with [Arduino IDE](https://www.arduino.cc/en/Main/Software). 6 | 7 | ## Installation 8 | 9 | - Click on the `Download ZIP` button in the top right corner. 10 | - Uncompress it. 11 | - Rename the uncompressed folder to `OLED`. 12 | - Check that the OLED folder contains OLED.cpp and OLED.h files. 13 | - Place the `OLED` folder in your `/libraries/` folder - you may need to create the `libraries` subfolder if it is your first library. 14 | - Restart the IDE. 15 | -------------------------------------------------------------------------------- /example/example.ino: -------------------------------------------------------------------------------- 1 | // Example sketch for testing OLED display 2 | 3 | // We need to include Wire.h for I2C communication 4 | #include 5 | #include "OLED.h" 6 | 7 | // Declare OLED display 8 | // display(SDA, SCL); 9 | // SDA and SCL are the GPIO pins of ESP8266 that are connected to respective pins of display. 10 | OLED display(2, 14); 11 | 12 | void setup() { 13 | Serial.begin(9600); 14 | Serial.println("OLED test!"); 15 | 16 | // Initialize display 17 | display.begin(); 18 | 19 | // Test message 20 | display.print("Hello World"); 21 | delay(3*1000); 22 | 23 | // Test long message 24 | display.print("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."); 25 | delay(3*1000); 26 | 27 | // Test display clear 28 | display.clear(); 29 | delay(3*1000); 30 | 31 | // Test message postioning 32 | display.print("TOP-LEFT"); 33 | display.print("4th row", 4); 34 | display.print("RIGHT-BOTTOM", 7, 4); 35 | delay(3*1000); 36 | 37 | // Test display OFF 38 | display.off(); 39 | display.print("3rd row", 3, 8); 40 | delay(3*1000); 41 | 42 | // Test display ON 43 | display.on(); 44 | delay(3*1000); 45 | } 46 | 47 | int r = 0, c = 0; 48 | 49 | void loop() { 50 | r = r % 8; 51 | c = micros() % 6; 52 | 53 | if (r == 0) 54 | display.clear(); 55 | 56 | display.print("Hello World", r++, c++); 57 | 58 | delay(500); 59 | } 60 | -------------------------------------------------------------------------------- /keywords.txt: -------------------------------------------------------------------------------- 1 | ########################################################## 2 | # Syntax Coloring Map For ESP8266-OLED Display Library 3 | ########################################################## 4 | 5 | ########################################################## 6 | # Datatypes (KEYWORD1) 7 | ########################################################## 8 | 9 | OLED KEYWORD1 10 | 11 | ########################################################## 12 | # Methods and Functions (KEYWORD2) 13 | ########################################################## 14 | 15 | begin KEYWORD2 16 | clear KEYWORD2 17 | off KEYWORD2 18 | on KEYWORD2 19 | print KEYWORD2 20 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=ESP8266-OLED Display Library 2 | version=1.0 3 | author=Klar Systems 4 | maintainer=Klar Systems 5 | sentence=esp8266-Arduino library for I2C-OLED displays 6 | paragraph=esp8266-Arduino library for I2C-OLED displays 7 | category=Display 8 | url=https://github.com/klarsys/esp8266-OLED 9 | architectures=esp8266 10 | --------------------------------------------------------------------------------