|
| 1 | +/* |
| 2 | + SeeedOLED.cpp - SSD130x OLED Driver Library |
| 3 | + 2011 Copyright (c) Seeed Technology Inc. All right reserved. |
| 4 | + |
| 5 | + Author: Visweswara R |
| 6 | + |
| 7 | + This library is free software; you can redistribute it and/or |
| 8 | + modify it under the terms of the GNU Lesser General Public |
| 9 | + License as published by the Free Software Foundation; either |
| 10 | + version 2.1 of the License, or (at your option) any later version. |
| 11 | +
|
| 12 | + This library is distributed in the hope that it will be useful, |
| 13 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | + Lesser General Public License for more details. |
| 16 | +
|
| 17 | + You should have received a copy of the GNU Lesser General Public |
| 18 | + License along with this library; if not, write to the Free Software |
| 19 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | +*/ |
| 21 | +#include "Arduino.h" |
| 22 | +#include "Wire.h" |
| 23 | +#include "SeeedOLED.h" |
| 24 | + |
| 25 | +#include <avr/pgmspace.h> |
| 26 | + |
| 27 | +// 8x8 Font ASCII 32 - 127 Implemented |
| 28 | +// Users can modify this to support more characters(glyphs) |
| 29 | +// BasicFont is placed in code memory. |
| 30 | + |
| 31 | +// This font be freely used without any restriction(It is placed in public domain) |
| 32 | +const unsigned char BasicFont[][8] PROGMEM= |
| 33 | +{ |
| 34 | + {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, |
| 35 | + {0x00,0x00,0x5F,0x00,0x00,0x00,0x00,0x00}, |
| 36 | + {0x00,0x00,0x07,0x00,0x07,0x00,0x00,0x00}, |
| 37 | + {0x00,0x14,0x7F,0x14,0x7F,0x14,0x00,0x00}, |
| 38 | + {0x00,0x24,0x2A,0x7F,0x2A,0x12,0x00,0x00}, |
| 39 | + {0x00,0x23,0x13,0x08,0x64,0x62,0x00,0x00}, |
| 40 | + {0x00,0x36,0x49,0x55,0x22,0x50,0x00,0x00}, |
| 41 | + {0x00,0x00,0x05,0x03,0x00,0x00,0x00,0x00}, |
| 42 | + {0x00,0x1C,0x22,0x41,0x00,0x00,0x00,0x00}, |
| 43 | + {0x00,0x41,0x22,0x1C,0x00,0x00,0x00,0x00}, |
| 44 | + {0x00,0x08,0x2A,0x1C,0x2A,0x08,0x00,0x00}, |
| 45 | + {0x00,0x08,0x08,0x3E,0x08,0x08,0x00,0x00}, |
| 46 | + {0x00,0xA0,0x60,0x00,0x00,0x00,0x00,0x00}, |
| 47 | + {0x00,0x08,0x08,0x08,0x08,0x08,0x00,0x00}, |
| 48 | + {0x00,0x60,0x60,0x00,0x00,0x00,0x00,0x00}, |
| 49 | + {0x00,0x20,0x10,0x08,0x04,0x02,0x00,0x00}, |
| 50 | + {0x00,0x3E,0x51,0x49,0x45,0x3E,0x00,0x00}, |
| 51 | + {0x00,0x00,0x42,0x7F,0x40,0x00,0x00,0x00}, |
| 52 | + {0x00,0x62,0x51,0x49,0x49,0x46,0x00,0x00}, |
| 53 | + {0x00,0x22,0x41,0x49,0x49,0x36,0x00,0x00}, |
| 54 | + {0x00,0x18,0x14,0x12,0x7F,0x10,0x00,0x00}, |
| 55 | + {0x00,0x27,0x45,0x45,0x45,0x39,0x00,0x00}, |
| 56 | + {0x00,0x3C,0x4A,0x49,0x49,0x30,0x00,0x00}, |
| 57 | + {0x00,0x01,0x71,0x09,0x05,0x03,0x00,0x00}, |
| 58 | + {0x00,0x36,0x49,0x49,0x49,0x36,0x00,0x00}, |
| 59 | + {0x00,0x06,0x49,0x49,0x29,0x1E,0x00,0x00}, |
| 60 | + {0x00,0x00,0x36,0x36,0x00,0x00,0x00,0x00}, |
| 61 | + {0x00,0x00,0xAC,0x6C,0x00,0x00,0x00,0x00}, |
| 62 | + {0x00,0x08,0x14,0x22,0x41,0x00,0x00,0x00}, |
| 63 | + {0x00,0x14,0x14,0x14,0x14,0x14,0x00,0x00}, |
| 64 | + {0x00,0x41,0x22,0x14,0x08,0x00,0x00,0x00}, |
| 65 | + {0x00,0x02,0x01,0x51,0x09,0x06,0x00,0x00}, |
| 66 | + {0x00,0x32,0x49,0x79,0x41,0x3E,0x00,0x00}, |
| 67 | + {0x00,0x7E,0x09,0x09,0x09,0x7E,0x00,0x00}, |
| 68 | + {0x00,0x7F,0x49,0x49,0x49,0x36,0x00,0x00}, |
| 69 | + {0x00,0x3E,0x41,0x41,0x41,0x22,0x00,0x00}, |
| 70 | + {0x00,0x7F,0x41,0x41,0x22,0x1C,0x00,0x00}, |
| 71 | + {0x00,0x7F,0x49,0x49,0x49,0x41,0x00,0x00}, |
| 72 | + {0x00,0x7F,0x09,0x09,0x09,0x01,0x00,0x00}, |
| 73 | + {0x00,0x3E,0x41,0x41,0x51,0x72,0x00,0x00}, |
| 74 | + {0x00,0x7F,0x08,0x08,0x08,0x7F,0x00,0x00}, |
| 75 | + {0x00,0x41,0x7F,0x41,0x00,0x00,0x00,0x00}, |
| 76 | + {0x00,0x20,0x40,0x41,0x3F,0x01,0x00,0x00}, |
| 77 | + {0x00,0x7F,0x08,0x14,0x22,0x41,0x00,0x00}, |
| 78 | + {0x00,0x7F,0x40,0x40,0x40,0x40,0x00,0x00}, |
| 79 | + {0x00,0x7F,0x02,0x0C,0x02,0x7F,0x00,0x00}, |
| 80 | + {0x00,0x7F,0x04,0x08,0x10,0x7F,0x00,0x00}, |
| 81 | + {0x00,0x3E,0x41,0x41,0x41,0x3E,0x00,0x00}, |
| 82 | + {0x00,0x7F,0x09,0x09,0x09,0x06,0x00,0x00}, |
| 83 | + {0x00,0x3E,0x41,0x51,0x21,0x5E,0x00,0x00}, |
| 84 | + {0x00,0x7F,0x09,0x19,0x29,0x46,0x00,0x00}, |
| 85 | + {0x00,0x26,0x49,0x49,0x49,0x32,0x00,0x00}, |
| 86 | + {0x00,0x01,0x01,0x7F,0x01,0x01,0x00,0x00}, |
| 87 | + {0x00,0x3F,0x40,0x40,0x40,0x3F,0x00,0x00}, |
| 88 | + {0x00,0x1F,0x20,0x40,0x20,0x1F,0x00,0x00}, |
| 89 | + {0x00,0x3F,0x40,0x38,0x40,0x3F,0x00,0x00}, |
| 90 | + {0x00,0x63,0x14,0x08,0x14,0x63,0x00,0x00}, |
| 91 | + {0x00,0x03,0x04,0x78,0x04,0x03,0x00,0x00}, |
| 92 | + {0x00,0x61,0x51,0x49,0x45,0x43,0x00,0x00}, |
| 93 | + {0x00,0x7F,0x41,0x41,0x00,0x00,0x00,0x00}, |
| 94 | + {0x00,0x02,0x04,0x08,0x10,0x20,0x00,0x00}, |
| 95 | + {0x00,0x41,0x41,0x7F,0x00,0x00,0x00,0x00}, |
| 96 | + {0x00,0x04,0x02,0x01,0x02,0x04,0x00,0x00}, |
| 97 | + {0x00,0x80,0x80,0x80,0x80,0x80,0x00,0x00}, |
| 98 | + {0x00,0x01,0x02,0x04,0x00,0x00,0x00,0x00}, |
| 99 | + {0x00,0x20,0x54,0x54,0x54,0x78,0x00,0x00}, |
| 100 | + {0x00,0x7F,0x48,0x44,0x44,0x38,0x00,0x00}, |
| 101 | + {0x00,0x38,0x44,0x44,0x28,0x00,0x00,0x00}, |
| 102 | + {0x00,0x38,0x44,0x44,0x48,0x7F,0x00,0x00}, |
| 103 | + {0x00,0x38,0x54,0x54,0x54,0x18,0x00,0x00}, |
| 104 | + {0x00,0x08,0x7E,0x09,0x02,0x00,0x00,0x00}, |
| 105 | + {0x00,0x18,0xA4,0xA4,0xA4,0x7C,0x00,0x00}, |
| 106 | + {0x00,0x7F,0x08,0x04,0x04,0x78,0x00,0x00}, |
| 107 | + {0x00,0x00,0x7D,0x00,0x00,0x00,0x00,0x00}, |
| 108 | + {0x00,0x80,0x84,0x7D,0x00,0x00,0x00,0x00}, |
| 109 | + {0x00,0x7F,0x10,0x28,0x44,0x00,0x00,0x00}, |
| 110 | + {0x00,0x41,0x7F,0x40,0x00,0x00,0x00,0x00}, |
| 111 | + {0x00,0x7C,0x04,0x18,0x04,0x78,0x00,0x00}, |
| 112 | + {0x00,0x7C,0x08,0x04,0x7C,0x00,0x00,0x00}, |
| 113 | + {0x00,0x38,0x44,0x44,0x38,0x00,0x00,0x00}, |
| 114 | + {0x00,0xFC,0x24,0x24,0x18,0x00,0x00,0x00}, |
| 115 | + {0x00,0x18,0x24,0x24,0xFC,0x00,0x00,0x00}, |
| 116 | + {0x00,0x00,0x7C,0x08,0x04,0x00,0x00,0x00}, |
| 117 | + {0x00,0x48,0x54,0x54,0x24,0x00,0x00,0x00}, |
| 118 | + {0x00,0x04,0x7F,0x44,0x00,0x00,0x00,0x00}, |
| 119 | + {0x00,0x3C,0x40,0x40,0x7C,0x00,0x00,0x00}, |
| 120 | + {0x00,0x1C,0x20,0x40,0x20,0x1C,0x00,0x00}, |
| 121 | + {0x00,0x3C,0x40,0x30,0x40,0x3C,0x00,0x00}, |
| 122 | + {0x00,0x44,0x28,0x10,0x28,0x44,0x00,0x00}, |
| 123 | + {0x00,0x1C,0xA0,0xA0,0x7C,0x00,0x00,0x00}, |
| 124 | + {0x00,0x44,0x64,0x54,0x4C,0x44,0x00,0x00}, |
| 125 | + {0x00,0x08,0x36,0x41,0x00,0x00,0x00,0x00}, |
| 126 | + {0x00,0x00,0x7F,0x00,0x00,0x00,0x00,0x00}, |
| 127 | + {0x00,0x41,0x36,0x08,0x00,0x00,0x00,0x00}, |
| 128 | + {0x00,0x02,0x01,0x01,0x02,0x01,0x00,0x00}, |
| 129 | + {0x00,0x02,0x05,0x05,0x02,0x00,0x00,0x00} |
| 130 | +}; |
| 131 | +void SeeedOLED::sendData(unsigned char Data) |
| 132 | +{ |
| 133 | + Wire.beginTransmission(SeeedOLED_Address); // begin I2C transmission |
| 134 | + Wire.write(SeeedOLED_Data_Mode); // data mode |
| 135 | + Wire.write(Data); |
| 136 | + Wire.endTransmission(); // stop I2C transmission |
| 137 | +} |
| 138 | + |
| 139 | +void SeeedOLED::init(void) |
| 140 | +{ |
| 141 | + sendCommand(SeeedOLED_Display_Off_Cmd); //display off |
| 142 | + delay(10); |
| 143 | + sendCommand(SeeedOLED_Display_On_Cmd); //display on |
| 144 | + delay(10); |
| 145 | + sendCommand(SeeedOLED_Normal_Display_Cmd); //Set Normal Display (default) |
| 146 | +} |
| 147 | + |
| 148 | +void SeeedOLED::sendCommand(unsigned char command) |
| 149 | +{ |
| 150 | + Wire.beginTransmission(SeeedOLED_Address); // begin I2C communication |
| 151 | + Wire.write(SeeedOLED_Command_Mode); // Set OLED Command mode |
| 152 | + Wire.write(command); |
| 153 | + Wire.endTransmission(); // End I2C communication |
| 154 | +} |
| 155 | + |
| 156 | +void SeeedOLED::setBrightness(unsigned char Brightness) |
| 157 | +{ |
| 158 | + sendCommand(SeeedOLED_Set_Brightness_Cmd); |
| 159 | + sendCommand(Brightness); |
| 160 | +} |
| 161 | + |
| 162 | +void SeeedOLED::setHorizontalMode() |
| 163 | +{ |
| 164 | + addressingMode = HORIZONTAL_MODE; |
| 165 | + sendCommand(0x20); //set addressing mode |
| 166 | + sendCommand(0x00); //set horizontal addressing mode |
| 167 | +} |
| 168 | + |
| 169 | +void SeeedOLED::setPageMode() |
| 170 | +{ |
| 171 | + addressingMode = PAGE_MODE; |
| 172 | + sendCommand(0x20); //set addressing mode |
| 173 | + sendCommand(0x02); //set page addressing mode |
| 174 | +} |
| 175 | + |
| 176 | + |
| 177 | +void SeeedOLED::setTextXY(unsigned char Row, unsigned char Column) |
| 178 | +{ |
| 179 | + sendCommand(0xB0 + Row); //set page address |
| 180 | + sendCommand(0x00 + (8*Column & 0x0F)); //set column lower address |
| 181 | + sendCommand(0x10 + ((8*Column>>4)&0x0F)); //set column higher address |
| 182 | +} |
| 183 | + |
| 184 | + |
| 185 | +void SeeedOLED::clearDisplay() |
| 186 | +{ |
| 187 | + unsigned char i,j; |
| 188 | + for(j=0;j<8;j++) |
| 189 | + { |
| 190 | + setTextXY(j,0); |
| 191 | + { |
| 192 | + for(i=0;i<128;i++) //clear all columns |
| 193 | + { |
| 194 | + putChar(' '); |
| 195 | + } |
| 196 | + } |
| 197 | + } |
| 198 | + setTextXY(0,0); |
| 199 | +} |
| 200 | + |
| 201 | + |
| 202 | + |
| 203 | +void SeeedOLED::putChar(unsigned char C) |
| 204 | +{ |
| 205 | + if(C < 32 || C > 127) //Ignore non-printable ASCII characters. This can be modified for multilingual font. |
| 206 | + { |
| 207 | + C=' '; //Space |
| 208 | + } |
| 209 | + unsigned char i=0; |
| 210 | + for(i=0;i<8;i++) |
| 211 | + { |
| 212 | + //read bytes from code memory |
| 213 | + sendData(pgm_read_byte(&BasicFont[C-32][i])); //font array starts at 0, ASCII starts at 32. Hence the translation |
| 214 | + } |
| 215 | +} |
| 216 | + |
| 217 | +void SeeedOLED::putString(const char *String) |
| 218 | +{ |
| 219 | + unsigned char i=0; |
| 220 | + while(String[i]) |
| 221 | + { |
| 222 | + putChar(String[i]); |
| 223 | + i++; |
| 224 | + } |
| 225 | +} |
| 226 | + |
| 227 | +unsigned char SeeedOLED::putNumber(long long_num) |
| 228 | +{ |
| 229 | + unsigned char char_buffer[10]=""; |
| 230 | + unsigned char i = 0; |
| 231 | + unsigned char f = 0; |
| 232 | + |
| 233 | + if (long_num < 0) |
| 234 | + { |
| 235 | + f=1; |
| 236 | + putChar('-'); |
| 237 | + long_num = -long_num; |
| 238 | + } |
| 239 | + else if (long_num == 0) |
| 240 | + { |
| 241 | + f=1; |
| 242 | + putChar('0'); |
| 243 | + return f; |
| 244 | + } |
| 245 | + |
| 246 | + while (long_num > 0) |
| 247 | + { |
| 248 | + char_buffer[i++] = long_num % 10; |
| 249 | + long_num /= 10; |
| 250 | + } |
| 251 | + |
| 252 | + f=f+i; |
| 253 | + for(; i > 0; i--) |
| 254 | + { |
| 255 | + putChar('0'+ char_buffer[i - 1]); |
| 256 | + } |
| 257 | + return f; |
| 258 | + |
| 259 | +} |
| 260 | + |
| 261 | +void SeeedOLED::drawBitmap(unsigned char *bitmaparray,int bytes) |
| 262 | +{ |
| 263 | + char localAddressMode = addressingMode; |
| 264 | + if(addressingMode != HORIZONTAL_MODE) |
| 265 | + { |
| 266 | + //Bitmap is drawn in horizontal mode |
| 267 | + setHorizontalMode(); |
| 268 | + } |
| 269 | + |
| 270 | + for(int i=0;i<bytes;i++) |
| 271 | + { |
| 272 | + sendData(pgm_read_byte(&bitmaparray[i])); |
| 273 | + } |
| 274 | + |
| 275 | + if(localAddressMode == PAGE_MODE) |
| 276 | + { |
| 277 | + //If pageMode was used earlier, restore it. |
| 278 | + setPageMode(); |
| 279 | + } |
| 280 | + |
| 281 | +} |
| 282 | + |
| 283 | +void SeeedOLED::setHorizontalScrollProperties(bool direction,unsigned char startPage, unsigned char endPage, unsigned char scrollSpeed) |
| 284 | +{ |
| 285 | +/* |
| 286 | +Use the following defines for 'direction' : |
| 287 | +
|
| 288 | + Scroll_Left |
| 289 | + Scroll_Right |
| 290 | +
|
| 291 | +Use the following defines for 'scrollSpeed' : |
| 292 | +
|
| 293 | + Scroll_2Frames |
| 294 | + Scroll_3Frames |
| 295 | + Scroll_4Frames |
| 296 | + Scroll_5Frames |
| 297 | + Scroll_25Frames |
| 298 | + Scroll_64Frames |
| 299 | + Scroll_128Frames |
| 300 | + Scroll_256Frames |
| 301 | +
|
| 302 | +*/ |
| 303 | + |
| 304 | + if(Scroll_Right == direction) |
| 305 | + { |
| 306 | + //Scroll Right |
| 307 | + sendCommand(0x26); |
| 308 | + } |
| 309 | + else |
| 310 | + { |
| 311 | + //Scroll Left |
| 312 | + sendCommand(0x27); |
| 313 | + |
| 314 | + } |
| 315 | + sendCommand(0x00); |
| 316 | + sendCommand(startPage); |
| 317 | + sendCommand(scrollSpeed); |
| 318 | + sendCommand(endPage); |
| 319 | + sendCommand(0x00); |
| 320 | + sendCommand(0xFF); |
| 321 | +} |
| 322 | + |
| 323 | +void SeeedOLED::activateScroll() |
| 324 | +{ |
| 325 | + sendCommand(SeeedOLED_Activate_Scroll_Cmd); |
| 326 | +} |
| 327 | + |
| 328 | +void SeeedOLED::deactivateScroll() |
| 329 | +{ |
| 330 | + sendCommand(SeeedOLED_Dectivate_Scroll_Cmd); |
| 331 | +} |
| 332 | + |
| 333 | +void SeeedOLED::setNormalDisplay() |
| 334 | +{ |
| 335 | + sendCommand(SeeedOLED_Normal_Display_Cmd); |
| 336 | +} |
| 337 | + |
| 338 | +void SeeedOLED::setInverseDisplay() |
| 339 | +{ |
| 340 | + sendCommand(SeeedOLED_Inverse_Display_Cmd); |
| 341 | +} |
| 342 | + |
| 343 | + |
| 344 | +SeeedOLED SeeedOled; // Preinstantiate Objects |
| 345 | + |
0 commit comments