The Smart Watch using SSD1306 and I2C. The Time obtain using SNTP protocol from NTP Server.

Dependencies:   WIZnetInterface SNTPClinet mbed-dev

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SSD1306.h Source File

SSD1306.h

00001 #include "data.h"
00002 #define addr    (0x78)
00003 
00004 #if defined(TARGET_WIZwiki_W7500)
00005 #define SDA                  PA_10
00006 #define SCL                  PA_9
00007 #endif
00008 
00009 #if defined(TARGET_WIZwiki_W7500ECO)
00010 #define SDA                  PA_10
00011 #define SCL                  PA_9
00012 #endif
00013 
00014 I2C i2c(SDA, SCL);
00015 
00016 void send_cmd(uint8_t cmd){
00017      char c[2] ={0x00,cmd};
00018      
00019        i2c.write(addr,c,2,1);
00020      
00021 }
00022 
00023 void send_data(uint8_t data){
00024     char c[2] = {0x40,data};
00025     i2c.write(addr,c,2,1);
00026     
00027 }
00028 
00029 void init(void)
00030 {
00031     wait_ms(5); // TBD
00032        
00033     send_cmd(0xae); // display off
00034     send_cmd(0xd5); // display divide ratio/osc. freq. ratio
00035     send_cmd(0x80);
00036     send_cmd(0xa8); // multiplex ation mode: 63
00037     send_cmd(0x3f);
00038     send_cmd(0xd3); // set display offset
00039     send_cmd(0x00);
00040     send_cmd(0x40); // set display start line
00041     send_cmd(0x8d); // set display offset
00042     send_cmd(0x14);
00043     send_cmd(0xa1); // segment remap
00044     send_cmd(0xc8); // set COM output scan direction
00045     send_cmd(0xda); // common pads hardware: alternative
00046     send_cmd(0x12);
00047     send_cmd(0x81); // contrast control
00048     send_cmd(0xcf);
00049     send_cmd(0xd9); // set pre-charge period
00050     send_cmd(0xf1);
00051     send_cmd(0xdb); // VCOM deselect level mode
00052     send_cmd(0x40); // set vcomh = 0.83 * VCC
00053     send_cmd(0xa4); // set entire display on/off
00054     send_cmd(0xa6); // set normal display
00055     send_cmd(0xaf); // set display on
00056 }
00057 
00058 // set position (x, 8*y)
00059 void locate(int x, int y){
00060     send_cmd(0xb0+y);
00061     send_cmd(((x&0xf0)>>4)|0x10);
00062     send_cmd((x&0x0f)|0x01);
00063 }
00064 
00065 void cls(void){
00066     int x, y;
00067     for(y = 0; y < 8; y++){
00068         locate(0, y);
00069         for(x = 0; x < 128; x++) send_data(0x00);
00070     }
00071 }
00072 
00073 
00074 
00075  void OLED_ShowStr(unsigned char x, unsigned char y, char ch[], unsigned char TextSize)
00076 {
00077     unsigned char c = 0,i = 0,j = 0;
00078     switch(TextSize)
00079     {
00080         case 1:
00081         {
00082             while(ch[j] != '\0')
00083             {
00084                 c = ch[j] - 32;
00085                 if(x > 126)
00086                 {
00087                     x = 0;
00088                     y++;
00089                 }
00090                 locate(x,y);
00091                 for(i=0;i<6;i++)
00092                     send_data(F6x8[c][i]);
00093                 x += 6;
00094                 j++;
00095             }
00096         }break;
00097         case 2:
00098         {
00099             while(ch[j] != '\0')
00100             {
00101                 c = ch[j] - 32;
00102                 if(x > 120)
00103                 {
00104                     x = 0;
00105                     y++;
00106                 }
00107                 locate(x,y);
00108                 for(i=0;i<8;i++)
00109                     send_data(F8X16[c*16+i]);
00110                 locate(x,y+1);
00111                 for(i=0;i<8;i++)
00112                     send_data(F8X16[c*16+i+8]);
00113                 x += 8;
00114                 j++;
00115             }
00116         }break;
00117     }
00118 }
00119 void OLED_DrawBMP(unsigned char x0,
00120                            unsigned char y0,unsigned char x1,
00121                            unsigned char y1,unsigned char BMP[])
00122 {
00123     unsigned int j=0;
00124     unsigned char x,y;
00125 
00126   if(y1%8==0)
00127         y = y1/8;
00128   else
00129         y = y1/8 + 1;
00130     for(y=y0;y<y1;y++)
00131     {
00132         locate(x0,y);
00133     for(x=x0;x<x1;x++)
00134         {
00135             send_data(BMP[j++]);
00136         }
00137     }
00138 }
00139 //==========================================================//
00140 // Prints a display big number (96 bytes) in coordinates X Y,
00141 // being multiples of 8. This means we have 16 COLS (0-15)
00142 // and 8 ROWS (0-7).
00143 void printBigNumber(unsigned char s, int x, int y)
00144 {
00145     locate(x,y);
00146     int salto=0;
00147     for(int i=0; i<96; i++) {
00148         if(s == ' ') {
00149             send_data(0);
00150         } else
00151             send_data(bigNumbers[s-0x30][i]);
00152 
00153         if(salto == 23) {
00154             salto = 0;
00155             x++;
00156             locate(x,y);
00157         } else {
00158             salto++;
00159         }
00160     }
00161 }
00162 void printBigTime(char *s)
00163 {
00164 
00165     int y=0;
00166     int lon = strlen(s);
00167     if(lon == 6) {
00168         y = 0;
00169     } else if (lon == 5) {
00170         y = 3;
00171     } else if (lon == 4) {
00172         y = 6;
00173     }
00174       else if(lon == 3) {
00175         y = 9;
00176     } else if (lon == 2) {
00177         y = 12;
00178     } else if (lon == 1) {
00179         y = 15;
00180     }
00181 
00182     int x = 2;
00183     while(*s) {
00184         printBigNumber(*s, x, y);
00185 
00186         y+=3;
00187         x=2;
00188         locate(x,y);
00189         *s++;
00190     }
00191 }
00192 void LED_P23x32Str(unsigned char x, unsigned char y, char ch[])
00193 {
00194   unsigned char c = 0, i = 0, j = 0, k = 0;
00195   while (ch[j] != '\0')
00196   {
00197     c = ch[j] - '0';
00198     if (x > 120) {
00199       x = 0;
00200       y++;
00201     }
00202 
00203     for (k = 0; k < 4; k++) {
00204       locate(x, y + k);
00205       for (i = 0; i < 23; i++) {
00206         send_data(F23x32[c * 23 * 4 + k * 23 + i]);
00207       }
00208     }
00209     x += 25;
00210     j++;
00211   }
00212 }
00213 
00214