An mbed library for 4D Systems uOLED-160-G1
Embed:
(wiki syntax)
Show/hide line numbers
OLED160G1.cpp
00001 /** 00002 * mbed library for 4D Systems uOLED-160-G1 00003 * 00004 * 00005 * Copyright (c) 2010 Steven Blair 00006 * 00007 * Permission is hereby granted, free of charge, to any person obtaining a copy 00008 * of this software and associated documentation files (the "Software"), to deal 00009 * in the Software without restriction, including without limitation the rights 00010 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00011 * copies of the Software, and to permit persons to whom the Software is 00012 * furnished to do so, subject to the following conditions: 00013 * 00014 * The above copyright notice and this permission notice shall be included in 00015 * all copies or substantial portions of the Software. 00016 * 00017 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00018 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00019 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00020 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00021 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00022 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00023 * THE SOFTWARE. 00024 */ 00025 00026 #include "mbed.h" 00027 #include "OLED160G1.h" 00028 00029 00030 OLED160G1::OLED160G1(PinName serialTx, PinName serialRx, PinName resetPin) : s(serialTx, serialRx), reset(resetPin) { 00031 s.baud(OLED_BAUDRATE); 00032 00033 while (s.readable()) { 00034 s.getc(); 00035 } 00036 00037 locate(0, 0); 00038 setFontColor(0xFFFF); 00039 _fontSize = OLED_FONT5X7; 00040 } 00041 00042 void OLED160G1::resetDisplay() { 00043 reset = 0; 00044 wait_ms(200); 00045 reset = 1; 00046 wait_ms(200); 00047 } 00048 00049 void OLED160G1::getResponse() { 00050 char response = OLED_NAK; 00051 lastCount = 0; 00052 NAKCount = 0; 00053 00054 while (!s.readable() || response == OLED_NAK) { 00055 wait_ms(1); 00056 lastCount++; 00057 if (s.readable()) { 00058 response = s.getc(); // Read response 00059 if (response == OLED_ACK) { 00060 return; 00061 } 00062 else if (response == OLED_NAK) { 00063 NAKCount++; 00064 } 00065 } 00066 } 00067 } 00068 00069 // Initialise OLED display. You must first activate serial comunication! 00070 void OLED160G1::init() { 00071 resetDisplay(); 00072 00073 wait_ms(OLED_INIT_DELAY); // wait for initialisation 00074 00075 s.putc(OLED_DETECT_BAUDRATE); // send byte for OLED to autodetect baudrate 00076 00077 getResponse(); 00078 } 00079 00080 int OLED160G1::toRGB(int red, int green, int blue) { 00081 int outR = ((red * 31) / 255); 00082 int outG = ((green * 63) / 255); 00083 int outB = ((blue * 31) / 255); 00084 00085 return (outR << 11) | (outG << 5) | outB; 00086 } 00087 00088 void OLED160G1::eraseScreen() { 00089 s.putc(OLED_CLEAR); 00090 00091 wait(1); // TODO: why needed? 00092 00093 getResponse(); 00094 00095 _row = 0; 00096 _column = 0; 00097 } 00098 00099 void OLED160G1::drawPixel(char x, char y, int color) { 00100 s.putc(OLED_PUTPIXEL); 00101 s.putc(x); 00102 s.putc(y); 00103 00104 s.putc(color >> 8); // MSB 00105 s.putc(color & 0xFF); // LSB 00106 00107 getResponse(); 00108 } 00109 00110 void OLED160G1::drawLine(char x1, char y1, char x2, char y2, int color) { 00111 s.putc(OLED_LINE); // Line 00112 00113 s.putc(x1); 00114 s.putc(y1); 00115 s.putc(x2); 00116 s.putc(y2); 00117 00118 s.putc(color >> 8); // MSB 00119 s.putc(color & 0xFF); // LSB 00120 00121 getResponse(); 00122 } 00123 00124 void OLED160G1::drawRectangle(char x, char y, char width, char height, int color) { 00125 s.putc(OLED_RECTANGLE); 00126 00127 s.putc(x); 00128 s.putc(y); 00129 00130 s.putc(x+width); 00131 s.putc(y+height); 00132 00133 s.putc(color >> 8); // MSB 00134 s.putc(color & 0xFF); // LSB 00135 00136 // 00137 // if (filled == 1) { printByte(0x01); } // Filled 00138 //else { printByte(0x00); } // Outline 00139 // 00140 getResponse(); 00141 } 00142 00143 void OLED160G1::drawCircle(char x, char y, char radius, int color) { 00144 s.putc(OLED_CIRCLE); 00145 00146 s.putc(x); 00147 s.putc(y); 00148 s.putc(radius); 00149 00150 s.putc(color >> 8); // MSB 00151 s.putc(color & 0xFF); // LSB 00152 00153 getResponse(); 00154 } 00155 00156 void OLED160G1::setFontSize(char fontSize) { 00157 s.putc(OLED_SETFONTSIZE); 00158 s.putc(fontSize); 00159 _fontSize = fontSize; 00160 00161 getResponse(); 00162 } 00163 00164 void OLED160G1::setFontColor(int fontColor) { 00165 _fontColor = fontColor; 00166 } 00167 00168 void OLED160G1::setPenSize(char penSize) { 00169 s.putc(OLED_PEN_SIZE); 00170 00171 s.putc(penSize); 00172 00173 _penSize = penSize; 00174 00175 getResponse(); 00176 } 00177 00178 void OLED160G1::setTextBackgroundType(char textBackgroundType) { 00179 s.putc(OLED_SET_TEXT_BACKGROUND_TYPE); 00180 s.putc(textBackgroundType); 00181 00182 getResponse(); 00183 } 00184 00185 void OLED160G1::setBackgroundColor(int color) { 00186 s.putc(OLED_SET_BACKGROUND_COLOR); 00187 00188 s.putc(color >> 8); // MSB 00189 s.putc(color & 0xFF); // LSB 00190 00191 getResponse(); 00192 } 00193 00194 void OLED160G1::drawText(char column, char row, char font_size, char *mytext, int color) { 00195 s.putc(OLED_TEXT); 00196 00197 // Adjust to center of the screen (26 Columns at font size 0) 00198 //int newCol = 13 - (strlen(mytext)/2); 00199 //printByte(newCol); // column 00200 s.putc(column); // column 00201 00202 s.putc(row); // row 00203 s.putc(font_size); // font size (0 = 5x7 font, 1 = 8x8 font, 2 = 8x12 font) 00204 00205 s.putc(color >> 8); // MSB 00206 s.putc(color & 0xFF); // LSB 00207 00208 for (int i = 0; i < strlen(mytext); i++) { 00209 s.putc(mytext[i]); // character to write 00210 } 00211 s.putc(0x00); // string terminator (always 0x00) 00212 00213 getResponse(); 00214 } 00215 00216 void OLED160G1::drawSingleChar(char column, char row, char theChar, int color) { 00217 s.putc(OLED_TEXTFORMATED); 00218 00219 s.putc(theChar); 00220 s.putc(column); 00221 s.putc(row); 00222 00223 s.putc(color >> 8); // MSB 00224 s.putc(color & 0xFF); // LSB 00225 00226 getResponse(); 00227 } 00228 00229 void OLED160G1::displayControl(char mode, char value) { 00230 s.putc(OLED_COMMAND_CONTROL); 00231 00232 s.putc(mode); 00233 s.putc(value); 00234 00235 getResponse(); 00236 } 00237 00238 char OLED160G1::getPenSize() { 00239 return _penSize; 00240 } 00241 00242 int OLED160G1::_putc(int value) { 00243 if (value == '\n') { 00244 _column = 0; 00245 _row++; 00246 if(_row >= rows()) { 00247 _row = 0; 00248 } 00249 } else { 00250 drawSingleChar(_column, _row, value, _fontColor); 00251 00252 wait_ms(1); //TODO: why is this needed? 00253 00254 _column++; 00255 if (_column >= columns()) { 00256 _column = 0; 00257 _row++; 00258 if(_row >= rows()) { 00259 _row = 0; 00260 } 00261 } 00262 } 00263 return value; 00264 } 00265 00266 int OLED160G1::_getc() { 00267 return -1; 00268 } 00269 00270 void OLED160G1::locate(int column, int row) { 00271 _column = column; 00272 _row = row; 00273 } 00274 00275 int OLED160G1::rows() { 00276 return 16; 00277 } 00278 00279 int OLED160G1::columns() { 00280 return 26; 00281 }
Generated on Fri Jul 15 2022 19:03:01 by 1.7.2