An mbed library for 4D Systems uOLED-160-G1

Dependents:   OLED-Driver

Committer:
sblair
Date:
Sun Dec 19 16:42:32 2010 +0000
Revision:
0:296a7ee30b3a
Initial version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sblair 0:296a7ee30b3a 1 /**
sblair 0:296a7ee30b3a 2 * mbed library for 4D Systems uOLED-160-G1
sblair 0:296a7ee30b3a 3 *
sblair 0:296a7ee30b3a 4 *
sblair 0:296a7ee30b3a 5 * Copyright (c) 2010 Steven Blair
sblair 0:296a7ee30b3a 6 *
sblair 0:296a7ee30b3a 7 * Permission is hereby granted, free of charge, to any person obtaining a copy
sblair 0:296a7ee30b3a 8 * of this software and associated documentation files (the "Software"), to deal
sblair 0:296a7ee30b3a 9 * in the Software without restriction, including without limitation the rights
sblair 0:296a7ee30b3a 10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
sblair 0:296a7ee30b3a 11 * copies of the Software, and to permit persons to whom the Software is
sblair 0:296a7ee30b3a 12 * furnished to do so, subject to the following conditions:
sblair 0:296a7ee30b3a 13 *
sblair 0:296a7ee30b3a 14 * The above copyright notice and this permission notice shall be included in
sblair 0:296a7ee30b3a 15 * all copies or substantial portions of the Software.
sblair 0:296a7ee30b3a 16 *
sblair 0:296a7ee30b3a 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
sblair 0:296a7ee30b3a 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
sblair 0:296a7ee30b3a 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
sblair 0:296a7ee30b3a 20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
sblair 0:296a7ee30b3a 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
sblair 0:296a7ee30b3a 22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
sblair 0:296a7ee30b3a 23 * THE SOFTWARE.
sblair 0:296a7ee30b3a 24 */
sblair 0:296a7ee30b3a 25
sblair 0:296a7ee30b3a 26 #include "mbed.h"
sblair 0:296a7ee30b3a 27 #include "OLED160G1.h"
sblair 0:296a7ee30b3a 28
sblair 0:296a7ee30b3a 29
sblair 0:296a7ee30b3a 30 OLED160G1::OLED160G1(PinName serialTx, PinName serialRx, PinName resetPin) : s(serialTx, serialRx), reset(resetPin) {
sblair 0:296a7ee30b3a 31 s.baud(OLED_BAUDRATE);
sblair 0:296a7ee30b3a 32
sblair 0:296a7ee30b3a 33 while (s.readable()) {
sblair 0:296a7ee30b3a 34 s.getc();
sblair 0:296a7ee30b3a 35 }
sblair 0:296a7ee30b3a 36
sblair 0:296a7ee30b3a 37 locate(0, 0);
sblair 0:296a7ee30b3a 38 setFontColor(0xFFFF);
sblair 0:296a7ee30b3a 39 _fontSize = OLED_FONT5X7;
sblair 0:296a7ee30b3a 40 }
sblair 0:296a7ee30b3a 41
sblair 0:296a7ee30b3a 42 void OLED160G1::resetDisplay() {
sblair 0:296a7ee30b3a 43 reset = 0;
sblair 0:296a7ee30b3a 44 wait_ms(200);
sblair 0:296a7ee30b3a 45 reset = 1;
sblair 0:296a7ee30b3a 46 wait_ms(200);
sblair 0:296a7ee30b3a 47 }
sblair 0:296a7ee30b3a 48
sblair 0:296a7ee30b3a 49 void OLED160G1::getResponse() {
sblair 0:296a7ee30b3a 50 char response = OLED_NAK;
sblair 0:296a7ee30b3a 51 lastCount = 0;
sblair 0:296a7ee30b3a 52 NAKCount = 0;
sblair 0:296a7ee30b3a 53
sblair 0:296a7ee30b3a 54 while (!s.readable() || response == OLED_NAK) {
sblair 0:296a7ee30b3a 55 wait_ms(1);
sblair 0:296a7ee30b3a 56 lastCount++;
sblair 0:296a7ee30b3a 57 if (s.readable()) {
sblair 0:296a7ee30b3a 58 response = s.getc(); // Read response
sblair 0:296a7ee30b3a 59 if (response == OLED_ACK) {
sblair 0:296a7ee30b3a 60 return;
sblair 0:296a7ee30b3a 61 }
sblair 0:296a7ee30b3a 62 else if (response == OLED_NAK) {
sblair 0:296a7ee30b3a 63 NAKCount++;
sblair 0:296a7ee30b3a 64 }
sblair 0:296a7ee30b3a 65 }
sblair 0:296a7ee30b3a 66 }
sblair 0:296a7ee30b3a 67 }
sblair 0:296a7ee30b3a 68
sblair 0:296a7ee30b3a 69 // Initialise OLED display. You must first activate serial comunication!
sblair 0:296a7ee30b3a 70 void OLED160G1::init() {
sblair 0:296a7ee30b3a 71 resetDisplay();
sblair 0:296a7ee30b3a 72
sblair 0:296a7ee30b3a 73 wait_ms(OLED_INIT_DELAY); // wait for initialisation
sblair 0:296a7ee30b3a 74
sblair 0:296a7ee30b3a 75 s.putc(OLED_DETECT_BAUDRATE); // send byte for OLED to autodetect baudrate
sblair 0:296a7ee30b3a 76
sblair 0:296a7ee30b3a 77 getResponse();
sblair 0:296a7ee30b3a 78 }
sblair 0:296a7ee30b3a 79
sblair 0:296a7ee30b3a 80 int OLED160G1::toRGB(int red, int green, int blue) {
sblair 0:296a7ee30b3a 81 int outR = ((red * 31) / 255);
sblair 0:296a7ee30b3a 82 int outG = ((green * 63) / 255);
sblair 0:296a7ee30b3a 83 int outB = ((blue * 31) / 255);
sblair 0:296a7ee30b3a 84
sblair 0:296a7ee30b3a 85 return (outR << 11) | (outG << 5) | outB;
sblair 0:296a7ee30b3a 86 }
sblair 0:296a7ee30b3a 87
sblair 0:296a7ee30b3a 88 void OLED160G1::eraseScreen() {
sblair 0:296a7ee30b3a 89 s.putc(OLED_CLEAR);
sblair 0:296a7ee30b3a 90
sblair 0:296a7ee30b3a 91 wait(1); // TODO: why needed?
sblair 0:296a7ee30b3a 92
sblair 0:296a7ee30b3a 93 getResponse();
sblair 0:296a7ee30b3a 94
sblair 0:296a7ee30b3a 95 _row = 0;
sblair 0:296a7ee30b3a 96 _column = 0;
sblair 0:296a7ee30b3a 97 }
sblair 0:296a7ee30b3a 98
sblair 0:296a7ee30b3a 99 void OLED160G1::drawPixel(char x, char y, int color) {
sblair 0:296a7ee30b3a 100 s.putc(OLED_PUTPIXEL);
sblair 0:296a7ee30b3a 101 s.putc(x);
sblair 0:296a7ee30b3a 102 s.putc(y);
sblair 0:296a7ee30b3a 103
sblair 0:296a7ee30b3a 104 s.putc(color >> 8); // MSB
sblair 0:296a7ee30b3a 105 s.putc(color & 0xFF); // LSB
sblair 0:296a7ee30b3a 106
sblair 0:296a7ee30b3a 107 getResponse();
sblair 0:296a7ee30b3a 108 }
sblair 0:296a7ee30b3a 109
sblair 0:296a7ee30b3a 110 void OLED160G1::drawLine(char x1, char y1, char x2, char y2, int color) {
sblair 0:296a7ee30b3a 111 s.putc(OLED_LINE); // Line
sblair 0:296a7ee30b3a 112
sblair 0:296a7ee30b3a 113 s.putc(x1);
sblair 0:296a7ee30b3a 114 s.putc(y1);
sblair 0:296a7ee30b3a 115 s.putc(x2);
sblair 0:296a7ee30b3a 116 s.putc(y2);
sblair 0:296a7ee30b3a 117
sblair 0:296a7ee30b3a 118 s.putc(color >> 8); // MSB
sblair 0:296a7ee30b3a 119 s.putc(color & 0xFF); // LSB
sblair 0:296a7ee30b3a 120
sblair 0:296a7ee30b3a 121 getResponse();
sblair 0:296a7ee30b3a 122 }
sblair 0:296a7ee30b3a 123
sblair 0:296a7ee30b3a 124 void OLED160G1::drawRectangle(char x, char y, char width, char height, int color) {
sblair 0:296a7ee30b3a 125 s.putc(OLED_RECTANGLE);
sblair 0:296a7ee30b3a 126
sblair 0:296a7ee30b3a 127 s.putc(x);
sblair 0:296a7ee30b3a 128 s.putc(y);
sblair 0:296a7ee30b3a 129
sblair 0:296a7ee30b3a 130 s.putc(x+width);
sblair 0:296a7ee30b3a 131 s.putc(y+height);
sblair 0:296a7ee30b3a 132
sblair 0:296a7ee30b3a 133 s.putc(color >> 8); // MSB
sblair 0:296a7ee30b3a 134 s.putc(color & 0xFF); // LSB
sblair 0:296a7ee30b3a 135
sblair 0:296a7ee30b3a 136 //
sblair 0:296a7ee30b3a 137 // if (filled == 1) { printByte(0x01); } // Filled
sblair 0:296a7ee30b3a 138 //else { printByte(0x00); } // Outline
sblair 0:296a7ee30b3a 139 //
sblair 0:296a7ee30b3a 140 getResponse();
sblair 0:296a7ee30b3a 141 }
sblair 0:296a7ee30b3a 142
sblair 0:296a7ee30b3a 143 void OLED160G1::drawCircle(char x, char y, char radius, int color) {
sblair 0:296a7ee30b3a 144 s.putc(OLED_CIRCLE);
sblair 0:296a7ee30b3a 145
sblair 0:296a7ee30b3a 146 s.putc(x);
sblair 0:296a7ee30b3a 147 s.putc(y);
sblair 0:296a7ee30b3a 148 s.putc(radius);
sblair 0:296a7ee30b3a 149
sblair 0:296a7ee30b3a 150 s.putc(color >> 8); // MSB
sblair 0:296a7ee30b3a 151 s.putc(color & 0xFF); // LSB
sblair 0:296a7ee30b3a 152
sblair 0:296a7ee30b3a 153 getResponse();
sblair 0:296a7ee30b3a 154 }
sblair 0:296a7ee30b3a 155
sblair 0:296a7ee30b3a 156 void OLED160G1::setFontSize(char fontSize) {
sblair 0:296a7ee30b3a 157 s.putc(OLED_SETFONTSIZE);
sblair 0:296a7ee30b3a 158 s.putc(fontSize);
sblair 0:296a7ee30b3a 159 _fontSize = fontSize;
sblair 0:296a7ee30b3a 160
sblair 0:296a7ee30b3a 161 getResponse();
sblair 0:296a7ee30b3a 162 }
sblair 0:296a7ee30b3a 163
sblair 0:296a7ee30b3a 164 void OLED160G1::setFontColor(int fontColor) {
sblair 0:296a7ee30b3a 165 _fontColor = fontColor;
sblair 0:296a7ee30b3a 166 }
sblair 0:296a7ee30b3a 167
sblair 0:296a7ee30b3a 168 void OLED160G1::setPenSize(char penSize) {
sblair 0:296a7ee30b3a 169 s.putc(OLED_PEN_SIZE);
sblair 0:296a7ee30b3a 170
sblair 0:296a7ee30b3a 171 s.putc(penSize);
sblair 0:296a7ee30b3a 172
sblair 0:296a7ee30b3a 173 _penSize = penSize;
sblair 0:296a7ee30b3a 174
sblair 0:296a7ee30b3a 175 getResponse();
sblair 0:296a7ee30b3a 176 }
sblair 0:296a7ee30b3a 177
sblair 0:296a7ee30b3a 178 void OLED160G1::setTextBackgroundType(char textBackgroundType) {
sblair 0:296a7ee30b3a 179 s.putc(OLED_SET_TEXT_BACKGROUND_TYPE);
sblair 0:296a7ee30b3a 180 s.putc(textBackgroundType);
sblair 0:296a7ee30b3a 181
sblair 0:296a7ee30b3a 182 getResponse();
sblair 0:296a7ee30b3a 183 }
sblair 0:296a7ee30b3a 184
sblair 0:296a7ee30b3a 185 void OLED160G1::setBackgroundColor(int color) {
sblair 0:296a7ee30b3a 186 s.putc(OLED_SET_BACKGROUND_COLOR);
sblair 0:296a7ee30b3a 187
sblair 0:296a7ee30b3a 188 s.putc(color >> 8); // MSB
sblair 0:296a7ee30b3a 189 s.putc(color & 0xFF); // LSB
sblair 0:296a7ee30b3a 190
sblair 0:296a7ee30b3a 191 getResponse();
sblair 0:296a7ee30b3a 192 }
sblair 0:296a7ee30b3a 193
sblair 0:296a7ee30b3a 194 void OLED160G1::drawText(char column, char row, char font_size, char *mytext, int color) {
sblair 0:296a7ee30b3a 195 s.putc(OLED_TEXT);
sblair 0:296a7ee30b3a 196
sblair 0:296a7ee30b3a 197 // Adjust to center of the screen (26 Columns at font size 0)
sblair 0:296a7ee30b3a 198 //int newCol = 13 - (strlen(mytext)/2);
sblair 0:296a7ee30b3a 199 //printByte(newCol); // column
sblair 0:296a7ee30b3a 200 s.putc(column); // column
sblair 0:296a7ee30b3a 201
sblair 0:296a7ee30b3a 202 s.putc(row); // row
sblair 0:296a7ee30b3a 203 s.putc(font_size); // font size (0 = 5x7 font, 1 = 8x8 font, 2 = 8x12 font)
sblair 0:296a7ee30b3a 204
sblair 0:296a7ee30b3a 205 s.putc(color >> 8); // MSB
sblair 0:296a7ee30b3a 206 s.putc(color & 0xFF); // LSB
sblair 0:296a7ee30b3a 207
sblair 0:296a7ee30b3a 208 for (int i = 0; i < strlen(mytext); i++) {
sblair 0:296a7ee30b3a 209 s.putc(mytext[i]); // character to write
sblair 0:296a7ee30b3a 210 }
sblair 0:296a7ee30b3a 211 s.putc(0x00); // string terminator (always 0x00)
sblair 0:296a7ee30b3a 212
sblair 0:296a7ee30b3a 213 getResponse();
sblair 0:296a7ee30b3a 214 }
sblair 0:296a7ee30b3a 215
sblair 0:296a7ee30b3a 216 void OLED160G1::drawSingleChar(char column, char row, char theChar, int color) {
sblair 0:296a7ee30b3a 217 s.putc(OLED_TEXTFORMATED);
sblair 0:296a7ee30b3a 218
sblair 0:296a7ee30b3a 219 s.putc(theChar);
sblair 0:296a7ee30b3a 220 s.putc(column);
sblair 0:296a7ee30b3a 221 s.putc(row);
sblair 0:296a7ee30b3a 222
sblair 0:296a7ee30b3a 223 s.putc(color >> 8); // MSB
sblair 0:296a7ee30b3a 224 s.putc(color & 0xFF); // LSB
sblair 0:296a7ee30b3a 225
sblair 0:296a7ee30b3a 226 getResponse();
sblair 0:296a7ee30b3a 227 }
sblair 0:296a7ee30b3a 228
sblair 0:296a7ee30b3a 229 void OLED160G1::displayControl(char mode, char value) {
sblair 0:296a7ee30b3a 230 s.putc(OLED_COMMAND_CONTROL);
sblair 0:296a7ee30b3a 231
sblair 0:296a7ee30b3a 232 s.putc(mode);
sblair 0:296a7ee30b3a 233 s.putc(value);
sblair 0:296a7ee30b3a 234
sblair 0:296a7ee30b3a 235 getResponse();
sblair 0:296a7ee30b3a 236 }
sblair 0:296a7ee30b3a 237
sblair 0:296a7ee30b3a 238 char OLED160G1::getPenSize() {
sblair 0:296a7ee30b3a 239 return _penSize;
sblair 0:296a7ee30b3a 240 }
sblair 0:296a7ee30b3a 241
sblair 0:296a7ee30b3a 242 int OLED160G1::_putc(int value) {
sblair 0:296a7ee30b3a 243 if (value == '\n') {
sblair 0:296a7ee30b3a 244 _column = 0;
sblair 0:296a7ee30b3a 245 _row++;
sblair 0:296a7ee30b3a 246 if(_row >= rows()) {
sblair 0:296a7ee30b3a 247 _row = 0;
sblair 0:296a7ee30b3a 248 }
sblair 0:296a7ee30b3a 249 } else {
sblair 0:296a7ee30b3a 250 drawSingleChar(_column, _row, value, _fontColor);
sblair 0:296a7ee30b3a 251
sblair 0:296a7ee30b3a 252 wait_ms(1); //TODO: why is this needed?
sblair 0:296a7ee30b3a 253
sblair 0:296a7ee30b3a 254 _column++;
sblair 0:296a7ee30b3a 255 if (_column >= columns()) {
sblair 0:296a7ee30b3a 256 _column = 0;
sblair 0:296a7ee30b3a 257 _row++;
sblair 0:296a7ee30b3a 258 if(_row >= rows()) {
sblair 0:296a7ee30b3a 259 _row = 0;
sblair 0:296a7ee30b3a 260 }
sblair 0:296a7ee30b3a 261 }
sblair 0:296a7ee30b3a 262 }
sblair 0:296a7ee30b3a 263 return value;
sblair 0:296a7ee30b3a 264 }
sblair 0:296a7ee30b3a 265
sblair 0:296a7ee30b3a 266 int OLED160G1::_getc() {
sblair 0:296a7ee30b3a 267 return -1;
sblair 0:296a7ee30b3a 268 }
sblair 0:296a7ee30b3a 269
sblair 0:296a7ee30b3a 270 void OLED160G1::locate(int column, int row) {
sblair 0:296a7ee30b3a 271 _column = column;
sblair 0:296a7ee30b3a 272 _row = row;
sblair 0:296a7ee30b3a 273 }
sblair 0:296a7ee30b3a 274
sblair 0:296a7ee30b3a 275 int OLED160G1::rows() {
sblair 0:296a7ee30b3a 276 return 16;
sblair 0:296a7ee30b3a 277 }
sblair 0:296a7ee30b3a 278
sblair 0:296a7ee30b3a 279 int OLED160G1::columns() {
sblair 0:296a7ee30b3a 280 return 26;
sblair 0:296a7ee30b3a 281 }