Dependencies: mbed lwip EAOLED
main.cpp@0:eb1247cc069b, 2011-01-12 (annotated)
- Committer:
- lynxeyed_atsu
- Date:
- Wed Jan 12 06:46:04 2011 +0000
- Revision:
- 0:eb1247cc069b
Network clock and OLED driver
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lynxeyed_atsu | 0:eb1247cc069b | 1 | /* |
lynxeyed_atsu | 0:eb1247cc069b | 2 | Demo of Embedded Artists LPCXpresso baseboard ethernet and OLED display facilities. |
lynxeyed_atsu | 0:eb1247cc069b | 3 | |
lynxeyed_atsu | 0:eb1247cc069b | 4 | Uses HTTP client to obtain the current time and displays the day, date and time on the OLED display. |
lynxeyed_atsu | 0:eb1247cc069b | 5 | |
lynxeyed_atsu | 0:eb1247cc069b | 6 | Based on RTCsetwithHTTPClient by nucho. |
lynxeyed_atsu | 0:eb1247cc069b | 7 | |
lynxeyed_atsu | 0:eb1247cc069b | 8 | NOTE: The LCPXpresso board does not provide the data/command signal to the mbed. |
lynxeyed_atsu | 0:eb1247cc069b | 9 | To provide this signal please connect a jumper wire from PIO2_7 of baseboard mbed socket |
lynxeyed_atsu | 0:eb1247cc069b | 10 | (empty 2nd hole below mbed pin 21) to to PIO2_5 (on J6 right side ninth hole from bottom). |
lynxeyed_atsu | 0:eb1247cc069b | 11 | |
lynxeyed_atsu | 0:eb1247cc069b | 12 | All jumpers on the board can be left in the default locations. |
lynxeyed_atsu | 0:eb1247cc069b | 13 | |
lynxeyed_atsu | 0:eb1247cc069b | 14 | */ |
lynxeyed_atsu | 0:eb1247cc069b | 15 | #include <stdio.h> |
lynxeyed_atsu | 0:eb1247cc069b | 16 | #include <string.h> |
lynxeyed_atsu | 0:eb1247cc069b | 17 | #include "mbed.h" |
lynxeyed_atsu | 0:eb1247cc069b | 18 | #include "HTTPClient.h" |
lynxeyed_atsu | 0:eb1247cc069b | 19 | #include "spioled.h" |
lynxeyed_atsu | 0:eb1247cc069b | 20 | |
lynxeyed_atsu | 0:eb1247cc069b | 21 | //#include "EAOLED.h" |
lynxeyed_atsu | 0:eb1247cc069b | 22 | |
lynxeyed_atsu | 0:eb1247cc069b | 23 | #define PAGE_SIZE 77 |
lynxeyed_atsu | 0:eb1247cc069b | 24 | #define countof(x) ( sizeof(x) / sizeof(x[0]) ) |
lynxeyed_atsu | 0:eb1247cc069b | 25 | |
lynxeyed_atsu | 0:eb1247cc069b | 26 | HTTPClient http; |
lynxeyed_atsu | 0:eb1247cc069b | 27 | SPIOLED oled(p21, p22, p23, p5, p6, p7);//CS, RES, DC, mosi, miso(No Connect), sclk |
lynxeyed_atsu | 0:eb1247cc069b | 28 | |
lynxeyed_atsu | 0:eb1247cc069b | 29 | bool kstate = false; |
lynxeyed_atsu | 0:eb1247cc069b | 30 | unsigned int kbuf; |
lynxeyed_atsu | 0:eb1247cc069b | 31 | unsigned char x_locale = 0; |
lynxeyed_atsu | 0:eb1247cc069b | 32 | unsigned char y_locale = 0; |
lynxeyed_atsu | 0:eb1247cc069b | 33 | |
lynxeyed_atsu | 0:eb1247cc069b | 34 | |
lynxeyed_atsu | 0:eb1247cc069b | 35 | unsigned int findface(unsigned short c) { |
lynxeyed_atsu | 0:eb1247cc069b | 36 | unsigned int p = 0; |
lynxeyed_atsu | 0:eb1247cc069b | 37 | int i, sum; |
lynxeyed_atsu | 0:eb1247cc069b | 38 | for (sum = i = 0; i < countof(font8table); i++) { |
lynxeyed_atsu | 0:eb1247cc069b | 39 | if (font8table[i].start <= c && c <= font8table[i].end) { |
lynxeyed_atsu | 0:eb1247cc069b | 40 | p = (sum + c - font8table[i].start); |
lynxeyed_atsu | 0:eb1247cc069b | 41 | break; |
lynxeyed_atsu | 0:eb1247cc069b | 42 | } |
lynxeyed_atsu | 0:eb1247cc069b | 43 | sum += font8table[i].end - font8table[i].start + 1; |
lynxeyed_atsu | 0:eb1247cc069b | 44 | } |
lynxeyed_atsu | 0:eb1247cc069b | 45 | return p; |
lynxeyed_atsu | 0:eb1247cc069b | 46 | } |
lynxeyed_atsu | 0:eb1247cc069b | 47 | |
lynxeyed_atsu | 0:eb1247cc069b | 48 | void drawc(unsigned char c) { |
lynxeyed_atsu | 0:eb1247cc069b | 49 | if (kstate) { // 2nd byte of shift-jis |
lynxeyed_atsu | 0:eb1247cc069b | 50 | kstate = false; |
lynxeyed_atsu | 0:eb1247cc069b | 51 | unsigned int p = findface(kbuf << 8 | c); |
lynxeyed_atsu | 0:eb1247cc069b | 52 | oled.PutChar( x_locale , y_locale ,p); |
lynxeyed_atsu | 0:eb1247cc069b | 53 | // printf("%x %x\r\n",( kbuf << 8 | c),p); //for debug |
lynxeyed_atsu | 0:eb1247cc069b | 54 | x_locale += X_Witch; |
lynxeyed_atsu | 0:eb1247cc069b | 55 | if (x_locale + (X_Witch - 1) >= Dis_X_MAX) { |
lynxeyed_atsu | 0:eb1247cc069b | 56 | x_locale = 0; |
lynxeyed_atsu | 0:eb1247cc069b | 57 | y_locale += Y_Witch; |
lynxeyed_atsu | 0:eb1247cc069b | 58 | if (y_locale >= Dis_Y_MAX)y_locale=0; |
lynxeyed_atsu | 0:eb1247cc069b | 59 | } |
lynxeyed_atsu | 0:eb1247cc069b | 60 | |
lynxeyed_atsu | 0:eb1247cc069b | 61 | } else if ((0x81 <= c && c <= 0x9f) || (0xe0 <= c && c <= 0xfc)) { // 1st byte of shift-jis |
lynxeyed_atsu | 0:eb1247cc069b | 62 | kstate = true; |
lynxeyed_atsu | 0:eb1247cc069b | 63 | kbuf = c; |
lynxeyed_atsu | 0:eb1247cc069b | 64 | } else { |
lynxeyed_atsu | 0:eb1247cc069b | 65 | oled.PutChar_ABC( x_locale , y_locale ,c); |
lynxeyed_atsu | 0:eb1247cc069b | 66 | x_locale += 6; |
lynxeyed_atsu | 0:eb1247cc069b | 67 | if (x_locale + (X_Witch - 1) >= Dis_X_MAX) { |
lynxeyed_atsu | 0:eb1247cc069b | 68 | x_locale = 0; |
lynxeyed_atsu | 0:eb1247cc069b | 69 | y_locale += Y_Witch; |
lynxeyed_atsu | 0:eb1247cc069b | 70 | if (y_locale >= Dis_Y_MAX)y_locale=0; |
lynxeyed_atsu | 0:eb1247cc069b | 71 | } |
lynxeyed_atsu | 0:eb1247cc069b | 72 | } |
lynxeyed_atsu | 0:eb1247cc069b | 73 | |
lynxeyed_atsu | 0:eb1247cc069b | 74 | } |
lynxeyed_atsu | 0:eb1247cc069b | 75 | |
lynxeyed_atsu | 0:eb1247cc069b | 76 | void draws(unsigned char x, unsigned char y, char *s) { |
lynxeyed_atsu | 0:eb1247cc069b | 77 | unsigned char c; |
lynxeyed_atsu | 0:eb1247cc069b | 78 | x_locale = x; |
lynxeyed_atsu | 0:eb1247cc069b | 79 | y_locale = y; |
lynxeyed_atsu | 0:eb1247cc069b | 80 | while ((c = *s++) != '\0')drawc(c); |
lynxeyed_atsu | 0:eb1247cc069b | 81 | } |
lynxeyed_atsu | 0:eb1247cc069b | 82 | |
lynxeyed_atsu | 0:eb1247cc069b | 83 | int main(void) { |
lynxeyed_atsu | 0:eb1247cc069b | 84 | char s[256]; |
lynxeyed_atsu | 0:eb1247cc069b | 85 | //printf("\r\n----------- starting ---------\r\n"); |
lynxeyed_atsu | 0:eb1247cc069b | 86 | /* |
lynxeyed_atsu | 0:eb1247cc069b | 87 | oled.cls(); |
lynxeyed_atsu | 0:eb1247cc069b | 88 | oled.locate(0,1); |
lynxeyed_atsu | 0:eb1247cc069b | 89 | oled.printf(" mbed clock\n"); |
lynxeyed_atsu | 0:eb1247cc069b | 90 | oled.locate(0,3); |
lynxeyed_atsu | 0:eb1247cc069b | 91 | oled.printf(" connect\n"); |
lynxeyed_atsu | 0:eb1247cc069b | 92 | oled.locate(0,5); |
lynxeyed_atsu | 0:eb1247cc069b | 93 | oled.printf(" ethernet\n"); |
lynxeyed_atsu | 0:eb1247cc069b | 94 | oled.locate(0,7); |
lynxeyed_atsu | 0:eb1247cc069b | 95 | oled.printf(" to start..\n"); |
lynxeyed_atsu | 0:eb1247cc069b | 96 | */ |
lynxeyed_atsu | 0:eb1247cc069b | 97 | oled.Full_Screen(0xffff); |
lynxeyed_atsu | 0:eb1247cc069b | 98 | char result[PAGE_SIZE+1]; |
lynxeyed_atsu | 0:eb1247cc069b | 99 | char buf[40]; |
lynxeyed_atsu | 0:eb1247cc069b | 100 | char day[12]; |
lynxeyed_atsu | 0:eb1247cc069b | 101 | char dmy[12]; |
lynxeyed_atsu | 0:eb1247cc069b | 102 | char hms[12]; |
lynxeyed_atsu | 0:eb1247cc069b | 103 | |
lynxeyed_atsu | 0:eb1247cc069b | 104 | char *e; |
lynxeyed_atsu | 0:eb1247cc069b | 105 | |
lynxeyed_atsu | 0:eb1247cc069b | 106 | time_t seconds; |
lynxeyed_atsu | 0:eb1247cc069b | 107 | printf("getting Data\r\n"); |
lynxeyed_atsu | 0:eb1247cc069b | 108 | http.get("http://ntp-a1.nict.go.jp/cgi-bin/ntp", result,PAGE_SIZE); |
lynxeyed_atsu | 0:eb1247cc069b | 109 | result[PAGE_SIZE]='\0'; |
lynxeyed_atsu | 0:eb1247cc069b | 110 | |
lynxeyed_atsu | 0:eb1247cc069b | 111 | int start_body=strstr(result,"<BODY>")-result+7; |
lynxeyed_atsu | 0:eb1247cc069b | 112 | int end_body = strstr(result,"</BODY>")-result-1; |
lynxeyed_atsu | 0:eb1247cc069b | 113 | strncpy(buf, result+start_body, end_body-start_body); |
lynxeyed_atsu | 0:eb1247cc069b | 114 | buf[end_body-start_body]='\0'; |
lynxeyed_atsu | 0:eb1247cc069b | 115 | printf("got data\r\n"); |
lynxeyed_atsu | 0:eb1247cc069b | 116 | seconds = strtoul(buf,&e,10)-2208988800UL; |
lynxeyed_atsu | 0:eb1247cc069b | 117 | seconds += 32400;// JST to BST - change this to suit your local time |
lynxeyed_atsu | 0:eb1247cc069b | 118 | set_time(seconds); |
lynxeyed_atsu | 0:eb1247cc069b | 119 | |
lynxeyed_atsu | 0:eb1247cc069b | 120 | // oled.cls(); |
lynxeyed_atsu | 0:eb1247cc069b | 121 | |
lynxeyed_atsu | 0:eb1247cc069b | 122 | while (1) { |
lynxeyed_atsu | 0:eb1247cc069b | 123 | |
lynxeyed_atsu | 0:eb1247cc069b | 124 | |
lynxeyed_atsu | 0:eb1247cc069b | 125 | seconds = time(NULL); |
lynxeyed_atsu | 0:eb1247cc069b | 126 | strftime(day,sizeof(day),"%A", localtime(&seconds)); |
lynxeyed_atsu | 0:eb1247cc069b | 127 | strftime(dmy,sizeof(dmy),"%d/%m/%Y", localtime(&seconds)); |
lynxeyed_atsu | 0:eb1247cc069b | 128 | strftime(hms,sizeof(hms),"%H:%M:%S", localtime(&seconds)); |
lynxeyed_atsu | 0:eb1247cc069b | 129 | |
lynxeyed_atsu | 0:eb1247cc069b | 130 | |
lynxeyed_atsu | 0:eb1247cc069b | 131 | |
lynxeyed_atsu | 0:eb1247cc069b | 132 | oled.ChangeFontColor(0); |
lynxeyed_atsu | 0:eb1247cc069b | 133 | //printf("%s %s %s\r\n", hms,day,dmy); |
lynxeyed_atsu | 0:eb1247cc069b | 134 | sprintf(s,"%s %s %s", hms,day,dmy); |
lynxeyed_atsu | 0:eb1247cc069b | 135 | draws(1,16,s); |
lynxeyed_atsu | 0:eb1247cc069b | 136 | //printf("%s %s %s\r\n", hms,day,dmy); |
lynxeyed_atsu | 0:eb1247cc069b | 137 | /* oled.locate((12-strlen(day))/2,1); |
lynxeyed_atsu | 0:eb1247cc069b | 138 | oled.printf("%s", day); |
lynxeyed_atsu | 0:eb1247cc069b | 139 | oled.locate((12-strlen(hms))/2,4); |
lynxeyed_atsu | 0:eb1247cc069b | 140 | oled.printf("%s", hms); |
lynxeyed_atsu | 0:eb1247cc069b | 141 | oled.locate((12-strlen(dmy))/2,7); |
lynxeyed_atsu | 0:eb1247cc069b | 142 | oled.printf("%s", dmy); |
lynxeyed_atsu | 0:eb1247cc069b | 143 | */ |
lynxeyed_atsu | 0:eb1247cc069b | 144 | |
lynxeyed_atsu | 0:eb1247cc069b | 145 | // draws(1,0,"%s",day); |
lynxeyed_atsu | 0:eb1247cc069b | 146 | |
lynxeyed_atsu | 0:eb1247cc069b | 147 | |
lynxeyed_atsu | 0:eb1247cc069b | 148 | wait_ms(1000); |
lynxeyed_atsu | 0:eb1247cc069b | 149 | |
lynxeyed_atsu | 0:eb1247cc069b | 150 | |
lynxeyed_atsu | 0:eb1247cc069b | 151 | |
lynxeyed_atsu | 0:eb1247cc069b | 152 | |
lynxeyed_atsu | 0:eb1247cc069b | 153 | } |
lynxeyed_atsu | 0:eb1247cc069b | 154 | } |
lynxeyed_atsu | 0:eb1247cc069b | 155 |