Paul Staron / Mbed 2 deprecated KL25Z_DCF77_OLED160

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers OLED160G1.cpp Source File

OLED160G1.cpp

00001 #include "mbed.h"
00002 #include "OLED160G1.h"
00003 
00004 
00005 OLED160G1::OLED160G1(PinName serialTx, PinName serialRx, PinName resetPin) : s(serialTx, serialRx), reset(resetPin) {
00006     s.baud(OLED_BAUDRATE);
00007     
00008     while (s.readable()) {
00009         s.getc();
00010     }
00011     
00012     locate(0, 0);
00013     setFontColor(0xFFFF);
00014     _fontSize = OLED_FONT5X7;
00015 }
00016 
00017 void OLED160G1::resetDisplay() {
00018     reset = 0;
00019     wait_ms(100);
00020     reset = 1;
00021     wait_ms(200);
00022 }
00023 
00024 void OLED160G1::getResponse() {
00025     char response = OLED_NAK;
00026     lastCount = 0;
00027     NAKCount = 0;
00028     
00029     while (!s.readable() || response == OLED_NAK) {
00030         wait_ms(1);
00031         lastCount++;
00032         if (s.readable()) {
00033             response = s.getc();    // Read response
00034             if (response == OLED_ACK) {
00035                 return;
00036             }
00037             else if (response == OLED_NAK) {
00038                 NAKCount++;
00039             }
00040         }
00041     }
00042 }
00043 
00044 // Initialise OLED display. You must first activate serial comunication!
00045 void OLED160G1::init() {
00046     resetDisplay();
00047 
00048     wait_ms(OLED_INIT_DELAY);       // wait for initialisation
00049 
00050     s.putc(OLED_DETECT_BAUDRATE);   // send byte for OLED to autodetect baudrate
00051     
00052     getResponse();
00053 }
00054 
00055 int OLED160G1::toRGB(int red, int green, int blue) {
00056     int outR = ((red * 31) / 255);
00057     int outG = ((green * 63) / 255);
00058     int outB = ((blue * 31) / 255);
00059 
00060     return (outR << 11) | (outG << 5) | outB;
00061 }
00062 
00063 void OLED160G1::eraseScreen() {
00064     s.putc(OLED_CLEAR);
00065     
00066     wait(1);    // TODO: why needed?
00067     
00068     getResponse();
00069     
00070     _row = 0;
00071     _column = 0;
00072 }
00073 
00074 void OLED160G1::drawPixel(char x, char y, int color) {
00075     s.putc(OLED_PUTPIXEL);
00076     s.putc(x);
00077     s.putc(y);
00078 
00079     s.putc(color >> 8);     // MSB
00080     s.putc(color & 0xFF);   // LSB
00081 
00082     getResponse();
00083 }
00084 
00085 void OLED160G1::drawLine(char x1, char y1, char x2, char y2, int color) {
00086     s.putc(OLED_LINE);      // Line
00087 
00088     s.putc(x1);
00089     s.putc(y1);
00090     s.putc(x2);
00091     s.putc(y2);
00092 
00093     s.putc(color >> 8);      // MSB          
00094     s.putc(color & 0xFF);    // LSB
00095 
00096     getResponse();
00097 }
00098 
00099 void OLED160G1::drawRectangle(char x, char y, char width, char height, int color) {
00100     s.putc(OLED_RECTANGLE); 
00101 
00102     s.putc(x);
00103     s.putc(y);
00104 
00105     s.putc(x+width);
00106     s.putc(y+height);
00107 
00108     s.putc(color >> 8);      // MSB          
00109     s.putc(color & 0xFF);    // LSB
00110     
00111     //
00112    // if (filled == 1) { printByte(0x01); }   // Filled
00113     //else { printByte(0x00); }               // Outline
00114     //
00115     getResponse();
00116 }
00117 
00118 void OLED160G1::drawCircle(char x, char y, char radius, int color) {
00119     s.putc(OLED_CIRCLE); 
00120 
00121     s.putc(x);
00122     s.putc(y);
00123     s.putc(radius);
00124 
00125     s.putc(color >> 8);     // MSB          
00126     s.putc(color & 0xFF);   // LSB
00127 
00128     getResponse();
00129 }
00130 
00131 void OLED160G1::setFontSize(char fontSize) {
00132     s.putc(OLED_SETFONTSIZE);
00133     s.putc(fontSize);
00134     _fontSize = fontSize;
00135     
00136     getResponse();
00137 }
00138 
00139 void OLED160G1::setFontColor(int fontColor) {
00140     _fontColor = fontColor;
00141 }
00142 
00143 void OLED160G1::setPenSize(char penSize) {
00144     s.putc(OLED_PEN_SIZE);
00145     
00146     s.putc(penSize);
00147     
00148     _penSize = penSize;
00149     
00150     getResponse();
00151 }
00152 
00153 void OLED160G1::setTextBackgroundType(char textBackgroundType) {
00154     s.putc(OLED_SET_TEXT_BACKGROUND_TYPE);
00155     s.putc(textBackgroundType);
00156     
00157     getResponse();
00158 }
00159 
00160 void OLED160G1::setBackgroundColor(int color) {
00161     s.putc(OLED_SET_BACKGROUND_COLOR);
00162     
00163     s.putc(color >> 8);      // MSB          
00164     s.putc(color & 0xFF);    // LSB
00165     
00166     getResponse();
00167 }
00168 
00169 void OLED160G1::drawText(char column, char row, char font_size, char *mytext, int color) {
00170     s.putc(OLED_TEXT);
00171     
00172     // Adjust to center of the screen (26 Columns at font size 0)
00173     //int newCol = 13 - (strlen(mytext)/2);
00174     //printByte(newCol); // column
00175     s.putc(column); // column
00176     
00177     s.putc(row); // row
00178     s.putc(font_size); // font size (0 = 5x7 font, 1 = 8x8 font, 2 = 8x12 font)
00179 
00180     s.putc(color >> 8);      // MSB          
00181     s.putc(color & 0xFF);    // LSB
00182 
00183     for (int i = 0;  i < strlen(mytext); i++) {
00184         s.putc(mytext[i]); // character to write
00185     }
00186     s.putc(0x00);   // string terminator (always 0x00)
00187 
00188     getResponse();
00189 }
00190 
00191 void OLED160G1::drawSingleChar(char column, char row, char theChar, int color) {
00192     s.putc(OLED_TEXTFORMATED);
00193     
00194     s.putc(theChar);
00195     s.putc(column);
00196     s.putc(row);
00197     
00198     s.putc(color >> 8);      // MSB          
00199     s.putc(color & 0xFF);    // LSB
00200 
00201     getResponse();
00202 }
00203 
00204 void OLED160G1::displayControl(char mode, char value) {
00205     s.putc(OLED_COMMAND_CONTROL);
00206     
00207     s.putc(mode);
00208     s.putc(value);
00209     
00210     getResponse();
00211 }
00212 
00213 char OLED160G1::getPenSize() {
00214     return _penSize;
00215 }
00216     
00217 int OLED160G1::_putc(int value) {
00218     if (value == '\n') {
00219         _column = 0;
00220         _row++;
00221         if(_row >= rows()) {
00222             _row = 0;
00223         }
00224     } else {
00225         drawSingleChar(_column, _row, value, _fontColor);
00226         
00227         wait_ms(1);    //TODO: why is this needed?
00228         
00229         _column++;
00230         if (_column >= columns()) {
00231             _column = 0;
00232             _row++;
00233             if(_row >= rows()) {
00234                 _row = 0;
00235             }
00236         }
00237     }
00238     return value;
00239 }
00240 
00241 int OLED160G1::_getc() {
00242     return -1;
00243 }
00244 
00245 void OLED160G1::locate(int column, int row) {
00246     _column = column;
00247     _row = row;
00248 }
00249 
00250 int OLED160G1::rows() { 
00251     return 16;
00252 }
00253 
00254 int OLED160G1::columns() { 
00255     return 26;
00256 }