hs loovee / Mbed 2 deprecated Xadow_Watch_RGBOLED

Dependencies:   DS_1337_1 mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 //#include "USBSerial.h"
00003 #include "Defs.h"
00004 #include "pinmap.h"
00005 #include "font_watch.h"
00006 #include "DS1337.h"
00007 
00008 
00009 #define HARD_SPI        1
00010 
00011 //USBSerial pc;
00012 
00013 // functions
00014 void spiWrite(unsigned char data);
00015 void writeCommand(unsigned char cmd);
00016 void writeData(unsigned char data);
00017 void writeColor(unsigned int data);
00018 void initOLED(void);
00019 void clearOLED(void);
00020 void fillRect(unsigned char x0, unsigned char y0, unsigned char x1, unsigned char y1, unsigned int outlineColor, unsigned int fillColor);
00021 void drawLine(unsigned char x0, unsigned char y0, unsigned char x1, unsigned char y1, unsigned int color);
00022 void fillOLED(unsigned int color);
00023 
00024 //DigitalOut blue(P0_20);
00025 DigitalOut white(P0_23);
00026 DigitalOut PowerON_OLED(P0_15);
00027 DigitalOut DC_OLED(P0_11);
00028 
00029 DigitalOut CS_OLED(P0_2);
00030 
00031 
00032 #if HARD_SPI
00033 
00034 SPI spi(P0_9, P0_20, P0_10);        // mosi, miso, sclk
00035 
00036 #else
00037 DigitalOut MOSI_OLED(P0_9);
00038 DigitalOut SCK_OLED(P0_10);
00039 
00040 #endif
00041 
00042 
00043 DS1337 RTC(P0_5, P0_4);
00044 
00045 
00046 Serial uart(P0_19, P0_18);
00047 
00048 
00049 typedef struct{
00050 
00051 int hour;
00052 int minutes;
00053 int second;
00054 int year;
00055 int month;
00056 int day;
00057 int week;
00058 
00059 }TIME_S;
00060 
00061 TIME_S time_now, time_buf;
00062 
00063 void time_init()
00064 {
00065     RTC.readTime();
00066     time_now.hour      = RTC.getHours();
00067     time_now.minutes   = RTC.getMinutes();
00068     time_now.second    = RTC.getSeconds();
00069     time_now.year      = RTC.getYears();
00070     time_now.month     = RTC.getMonths();
00071     time_now.day       = RTC.getDays();
00072     time_now.week      = RTC.getDayOfWeek(); 
00073     
00074     time_buf.hour      = RTC.getHours();
00075     time_buf.minutes   = RTC.getMinutes();
00076     time_buf.second    = RTC.getSeconds();
00077     time_buf.year      = RTC.getYears();
00078     time_buf.month     = RTC.getMonths();
00079     time_buf.day       = RTC.getDays();
00080     time_buf.week      = RTC.getDayOfWeek(); 
00081 }
00082 
00083 void time_refresh()
00084 {
00085     RTC.readTime();
00086     time_now.hour      = RTC.getHours();
00087     time_now.minutes   = RTC.getMinutes();
00088     time_now.second    = RTC.getSeconds();
00089     time_now.year      = RTC.getYears();
00090     time_now.month     = RTC.getMonths();
00091     time_now.day       = RTC.getDays();
00092     time_now.week      = RTC.getDayOfWeek(); 
00093 }
00094 
00095 void setTime()
00096 {
00097     RTC.setSeconds(50);
00098     RTC.setMinutes(33);
00099     RTC.setHours(16);
00100     RTC.setDays(18);
00101     RTC.setDayOfWeek(2);
00102     RTC.setMonths(3);
00103     RTC.setYears(2014);
00104     
00105     RTC.setTime();
00106 }
00107 
00108 
00109 void drawPix(int x, int y, unsigned int color)
00110 {
00111     //fillRect(x, y, x, y, color, color);
00112     
00113     int x1 = 95-y;
00114     int y1 = x;
00115     drawLine(x1, y1, x1, y1, color);
00116 }
00117 
00118 void drawBuff(int x, int y, int buf_len, int buf_width, int color, const unsigned char *buff)
00119 {
00120     for(int i=0; i<(buf_width/8); i++)
00121     {
00122         for(int j=0; j<buf_len; j++)
00123         {
00124             for(int k=0; k<8; k++)
00125             {
00126                 int clr_ = (buff[i*buf_len+j] & (0x01<<k)) ? color : 0;
00127                 drawPix(j+x, 8*i+k+y, clr_);
00128             }
00129         }
00130     }
00131 }
00132 
00133 void dispFont(int x, int y, int color, const unsigned char *font)
00134 {
00135     drawBuff(x, y, 11, 32, color, font);
00136 }
00137 
00138 void dispFont_small(int x, int y, int color, const unsigned char *font)
00139 {   
00140     drawBuff(x, y, 8, 16, color, font);
00141 }
00142 
00143 void dispChar(char c, int x, int y, int color)
00144 {
00145     c = c-32;
00146     drawBuff(x, y, 8, 16, color, font_ascii[c]);
00147 }
00148 
00149 unsigned int make_color(unsigned char r, unsigned char g, unsigned char b)
00150 {
00151     unsigned int color_ = r;
00152 
00153     color_ = r>>3;                              // red
00154     color_ = color_<<6 | (g>>2);                // green
00155     color_ = color_<<5 | (b>>3);                // blue
00156 
00157     return color_;
00158 }
00159 
00160 void dispString(char *str)
00161 {
00162     clearOLED();
00163     
00164     int color = make_color(255,255, 255);
00165     
00166     int x=0, y=0;
00167     
00168     int pix = 0;
00169     
00170     while(*str)
00171     {
00172         dispChar(*str, x, y, make_color(255, 255, 255));
00173         str++;
00174         x += 8;
00175         pix++;
00176         
00177         if(pix == 8)
00178         {
00179             pix = 0;
00180             x = 0;
00181             y += 16;
00182         }
00183     }
00184 }
00185 
00186 void dispDot(int x, int y, int color)
00187 {
00188     drawBuff(x, y, 7, 32, color, font_dot);
00189 }
00190 
00191 void refresh_time()
00192 {
00193     int y = 60;
00194     int color_ = make_color(255, 255, 255); 
00195     dispFont(0, y, color_, font_num[time_now.hour/10]);
00196     dispFont(14, y, color_, font_num[time_now.hour%10]);
00197     dispDot(28, y-2, color_);
00198     dispFont(38, y, color_, font_num[time_now.minutes/10]);
00199     dispFont(52, y, color_, font_num[time_now.minutes%10]);
00200 }
00201 
00202 void refresh_day()
00203 {
00204     int color_ = make_color(125, 255, 62);
00205     
00206     dispChar('M', 0, 40, color_);
00207     dispChar('a', 8, 40, color_);
00208     dispChar('r', 16, 40, color_);
00209 
00210    // dispChar()
00211    
00212    dispFont_small(30, 40, color_, font_ascii[time_now.day/10+'0'-32]);
00213    dispFont_small(38, 40, color_, font_ascii[time_now.day%10+'0'-32]);
00214 }
00215 
00216 void show_time()
00217 {
00218     clearOLED();
00219     int color_ = make_color(255, 255, 255);
00220     refresh_day();
00221     for(int i=0; i<63; i++)
00222     {
00223         drawPix(i, 56, color_);
00224         drawPix(i, 57, color_);
00225     }
00226     refresh_time();
00227 }
00228 
00229 
00230 int main()
00231 {
00232     uart.baud(38400);
00233     
00234     pin_function(P0_15,1);
00235     pin_function(P0_10,1);
00236 
00237     //pin_mode(P0_15,OpenDrain);
00238     PowerON_OLED = 0; // power on OLED module
00239     wait_ms(5);
00240     PowerON_OLED = 1; // power off OLED module
00241     wait_ms(5);
00242     PowerON_OLED = 0; // power on OLED module
00243     wait_ms(5);
00244 
00245 #if HARD_SPI
00246     spi.format(8,3);
00247     spi.frequency(1000000);
00248 #else
00249     SCK_OLED = 1;
00250     CS_OLED = 1;
00251 #endif
00252     initOLED();
00253     clearOLED();
00254     //unsigned int color = 0;
00255     // blue = 0;
00256     
00257     
00258     dispString("xadow   smart   watch");
00259     
00260     wait(1);
00261     //setTime();
00262     time_init();
00263     show_time();
00264 
00265     char ble_str[40];
00266     int len_str = 0;
00267     
00268     
00269     drawBuff(12, 0, 40, 40, make_color(153, 153, 255), font_ophw);
00270     
00271     for(;;)
00272     {
00273         time_refresh();
00274         
00275         if(time_now.day != time_buf.day)
00276         {
00277             refresh_day();
00278         }
00279         
00280         if(time_now.minutes != time_buf.minutes)
00281         {
00282             refresh_time();
00283         }
00284         
00285        wait(0.1);
00286         
00287         
00288         while (uart.readable()) 
00289         {
00290             ble_str[len_str++] = uart.getc();
00291         }
00292         
00293         if(len_str == 1 && (ble_str[0] == 't' || ble_str[0] == 'T'))
00294         {
00295             time_refresh();
00296             uart.printf("%d/%d/%d\r\n", time_now.year, time_now.month, time_now.day);
00297             uart.printf("%d:%d:%d\r\n", time_now.hour, time_now.minutes, time_now.second);
00298             len_str = 0;
00299         }
00300         else if(len_str>0)
00301         {
00302             ble_str[len_str] = '\0';
00303             dispString(ble_str);
00304             
00305             wait(3);
00306             show_time();
00307             len_str = 0;
00308             
00309         }
00310         
00311     }
00312 }
00313 
00314 void spiWrite(unsigned char data)
00315 {
00316     
00317 #if HARD_SPI
00318     
00319     spi.write(data);
00320 #else
00321     for(unsigned char i=0;i<8;i++)
00322     {
00323         SCK_OLED = 0;
00324         if(data&0x80)
00325         {
00326             MOSI_OLED = 1;
00327         }
00328         else
00329         {
00330             MOSI_OLED = 0;
00331         }
00332         data<<=1;
00333         wait_us(1);
00334         SCK_OLED = 1;
00335         wait_us(1);
00336     }
00337 #endif
00338 }
00339 
00340 void writeCommand(unsigned char cmd)
00341 {
00342     DC_OLED = 0;
00343     CS_OLED = 0;
00344     spiWrite(cmd);
00345     CS_OLED = 1;
00346 }
00347 
00348 void writeData(unsigned char data)
00349 {
00350     DC_OLED = 1;
00351     CS_OLED = 0;
00352     spiWrite(data);
00353     CS_OLED = 1;
00354 }
00355 
00356 void writeColor(unsigned int data)
00357 {
00358     DC_OLED = 1;
00359     CS_OLED = 0;
00360     spiWrite(data>>8);
00361     spiWrite(data);
00362     CS_OLED = 1;
00363 }
00364 
00365 void initOLED(void)
00366 {
00367     writeCommand(SSD1331_CMD_DISPLAYOFF);           // 0xAE
00368     writeCommand(SSD1331_CMD_SETREMAP);             // 0xA0
00369 #if defined SSD1331_COLORORDER_RGB
00370     writeCommand(0x72);                             // RGB Color
00371 #else
00372     writeCommand(0x76);                             // BGR Color
00373 #endif
00374     writeCommand(SSD1331_CMD_STARTLINE);            // 0xA1
00375     writeCommand(0x0);
00376     writeCommand(SSD1331_CMD_DISPLAYOFFSET);        // 0xA2
00377     writeCommand(0x0);
00378     writeCommand(SSD1331_CMD_NORMALDISPLAY);        // 0xA4
00379     writeCommand(SSD1331_CMD_SETMULTIPLEX);         // 0xA8
00380     writeCommand(0x3F);                             // 0x3F 1/64 duty
00381     writeCommand(SSD1331_CMD_SETMASTER);            // 0xAD
00382     writeCommand(0x8E);
00383     writeCommand(SSD1331_CMD_POWERMODE);            // 0xB0
00384     writeCommand(0x0B);
00385     writeCommand(SSD1331_CMD_PRECHARGE);    // 0xB1
00386     writeCommand(0x31);
00387     writeCommand(SSD1331_CMD_CLOCKDIV);     // 0xB3
00388     writeCommand(0xF0);  // 7:4 = Oscillator Frequency, 3:0 = CLK Div Ratio (A[3:0]+1 = 1..16)
00389     writeCommand(SSD1331_CMD_PRECHARGEA);   // 0x8A
00390     writeCommand(0x64);
00391     writeCommand(SSD1331_CMD_PRECHARGEB);   // 0x8B
00392     writeCommand(0x78);
00393     writeCommand(SSD1331_CMD_PRECHARGEA);   // 0x8C
00394     writeCommand(0x64);
00395     writeCommand(SSD1331_CMD_PRECHARGELEVEL);   // 0xBB
00396     writeCommand(0x3A);
00397     writeCommand(SSD1331_CMD_VCOMH);        // 0xBE
00398     writeCommand(0x3E);
00399     writeCommand(SSD1331_CMD_MASTERCURRENT);    // 0x87
00400     writeCommand(0x06);
00401     writeCommand(SSD1331_CMD_CONTRASTA);    // 0x81
00402     writeCommand(0x91);
00403     writeCommand(SSD1331_CMD_CONTRASTB);    // 0x82
00404     writeCommand(0x50);
00405     writeCommand(SSD1331_CMD_CONTRASTC);    // 0x83
00406     writeCommand(0x7D);
00407     writeCommand(SSD1331_CMD_DISPLAYON);    //--turn on oled panel
00408     
00409 }
00410 
00411 void clearOLED(void)
00412 {
00413     fillRect(0,0,95,63,0,0);
00414 }
00415 
00416 void fillOLED(unsigned int color)
00417 {
00418     fillRect(0,0,95,63,color,color);
00419 }
00420 
00421 void fillRect(unsigned char x0, unsigned char y0, unsigned char x1, unsigned char y1, unsigned int outlineColor, unsigned int fillColor)
00422 {
00423     writeCommand(SSD1331_CMD_FILL);
00424     writeCommand(0x01);
00425 
00426     writeCommand(SSD1331_CMD_DRAWRECT);
00427     writeCommand(x0);                           // Starting column
00428     writeCommand(y0);                           // Starting row
00429     writeCommand(x1); // End column
00430     writeCommand(y1); // End row
00431 
00432     // Outline color
00433     writeCommand((uint8_t)((outlineColor >> 11) << 1));
00434     writeCommand((uint8_t)((outlineColor >> 5) & 0x3F));
00435     writeCommand((uint8_t)((outlineColor << 1) & 0x3F));
00436     // Fill color
00437     writeCommand((uint8_t)((fillColor >> 11) << 1));
00438     writeCommand((uint8_t)((fillColor >> 5) & 0x3F));
00439     writeCommand((uint8_t)((fillColor << 1) & 0x3F));
00440 
00441     // Delay while the fill completes
00442     wait_ms(SSD1331_DELAYS_HWFILL);
00443 }
00444 
00445 void drawLine(unsigned char x0, unsigned char y0, unsigned char x1, unsigned char y1, unsigned int color)
00446 {
00447     writeCommand(SSD1331_CMD_DRAWLINE);
00448     writeCommand(x0);
00449     writeCommand(y0);
00450     writeCommand(x1);
00451     writeCommand(y1);
00452     //wait_ms(SSD1331_DELAYS_HWLINE);
00453     writeCommand((uint8_t)((color >> 11) << 1));
00454     writeCommand((uint8_t)((color >> 5) & 0x3F));
00455     writeCommand((uint8_t)((color << 1) & 0x3F));
00456     // wait_ms(SSD1331_DELAYS_HWLINE);
00457 }