Test of Embedded Artists LPCXpresso baseboard ethernet, SD card, audio and OLED display facilities. The program displays the day, date and time on the baseboard OLED and sounds the Big Ben chimes on the hour and quarter hour. On initial startup the program checks that the mbed clock is set and that the chime wav files can be accessed on the SD card. If not it asks to be connected to the internet to obtain the current time and to download the wav files to the SD card.

Dependencies:   EthernetNetIf NTPClient_NetServices mbed EAOLED

Committer:
tom_coxon
Date:
Sat Aug 14 10:33:13 2010 +0000
Revision:
0:f61e8db0570d

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tom_coxon 0:f61e8db0570d 1 /* EA_BigBen
tom_coxon 0:f61e8db0570d 2
tom_coxon 0:f61e8db0570d 3 Just for fun this turns the Embedded Artists LPCXpresso baseboard into a Big Ben substitute.
tom_coxon 0:f61e8db0570d 4
tom_coxon 0:f61e8db0570d 5 Well OK it is a little smaller and the sound is a little tinnier than the real thing but it works.
tom_coxon 0:f61e8db0570d 6
tom_coxon 0:f61e8db0570d 7 This pulls together the following test programs for the Embedded Artists LPCXpresso baseboard:
tom_coxon 0:f61e8db0570d 8
tom_coxon 0:f61e8db0570d 9 EA_InternetClock (now superseded by this program)
tom_coxon 0:f61e8db0570d 10 EA_WavPlayer
tom_coxon 0:f61e8db0570d 11 EA_DownloadToSD
tom_coxon 0:f61e8db0570d 12
tom_coxon 0:f61e8db0570d 13 The program displays the day, date and time on the baseboard OLED and sounds the Big Ben chimes
tom_coxon 0:f61e8db0570d 14 on the hour and quarter hour.
tom_coxon 0:f61e8db0570d 15
tom_coxon 0:f61e8db0570d 16 On initial startup the program checks that the mbed clock is set and that the wav files can be accessed
tom_coxon 0:f61e8db0570d 17 on the SD card. If not it asks to be connected to the internet to obtain the current time and to download
tom_coxon 0:f61e8db0570d 18 the wav files to the SD card.
tom_coxon 0:f61e8db0570d 19
tom_coxon 0:f61e8db0570d 20 The wav file player works with uncompressed wav files with 8 or 16 bit sample sizes.
tom_coxon 0:f61e8db0570d 21
tom_coxon 0:f61e8db0570d 22 Setup of the baseboard:
tom_coxon 0:f61e8db0570d 23
tom_coxon 0:f61e8db0570d 24 Please set all jumpers on board to the default case except for
tom_coxon 0:f61e8db0570d 25 the following:
tom_coxon 0:f61e8db0570d 26
tom_coxon 0:f61e8db0570d 27 Audio setup:
tom_coxon 0:f61e8db0570d 28
tom_coxon 0:f61e8db0570d 29 1. Insert a jumper in J31 to connect signal PIO1_2 to the low
tom_coxon 0:f61e8db0570d 30 pass filer as described in section 4.7.2. of base board users
tom_coxon 0:f61e8db0570d 31 guide.
tom_coxon 0:f61e8db0570d 32
tom_coxon 0:f61e8db0570d 33 2. Insert three jumpers in J33 to connect PIO3_0, PIO3_1 and
tom_coxon 0:f61e8db0570d 34 PIO3_2 to control the amplifier as described in section 4.8
tom_coxon 0:f61e8db0570d 35 of base board users guide.
tom_coxon 0:f61e8db0570d 36
tom_coxon 0:f61e8db0570d 37 3. Insert a jumper in J32 and remove any from J34 to use the
tom_coxon 0:f61e8db0570d 38 internal speaker as described in section 4.8
tom_coxon 0:f61e8db0570d 39 of base board users guide.
tom_coxon 0:f61e8db0570d 40
tom_coxon 0:f61e8db0570d 41 SD Card setup:
tom_coxon 0:f61e8db0570d 42
tom_coxon 0:f61e8db0570d 43 4. Insert all five jumpers in J39 as described in section 4.3.3
tom_coxon 0:f61e8db0570d 44 of base board users guide.
tom_coxon 0:f61e8db0570d 45
tom_coxon 0:f61e8db0570d 46 5. Remove jumper marked "A" in J55 In order to connect PIO1_11
tom_coxon 0:f61e8db0570d 47 to CS signal of J40 (the SPI-SSEL signal) as described in section 4.3.3
tom_coxon 0:f61e8db0570d 48 of base board users guide.
tom_coxon 0:f61e8db0570d 49
tom_coxon 0:f61e8db0570d 50 OLED setup:
tom_coxon 0:f61e8db0570d 51
tom_coxon 0:f61e8db0570d 52 1. The LCPXpresso board does not provide the data/command signal to the mbed.
tom_coxon 0:f61e8db0570d 53 To provide this signal please connect a jumper wire from PIO2_7 of baseboard mbed socket
tom_coxon 0:f61e8db0570d 54 (empty 2nd hole below mbed pin 21) to to PIO2_5 (on J6 right side ninth hole from bottom).
tom_coxon 0:f61e8db0570d 55
tom_coxon 0:f61e8db0570d 56 */
tom_coxon 0:f61e8db0570d 57
tom_coxon 0:f61e8db0570d 58 #include "mbed.h"
tom_coxon 0:f61e8db0570d 59 #include "wavplayer.h"
tom_coxon 0:f61e8db0570d 60 #include "SDHCFileSystem.h"
tom_coxon 0:f61e8db0570d 61 #include "EthernetNetIf.h"
tom_coxon 0:f61e8db0570d 62 #include "NTPClient.h"
tom_coxon 0:f61e8db0570d 63 #include "EAOLED.h"
tom_coxon 0:f61e8db0570d 64 #include "HTTPClient.h"
tom_coxon 0:f61e8db0570d 65
tom_coxon 0:f61e8db0570d 66 int debug = 0;
tom_coxon 0:f61e8db0570d 67
tom_coxon 0:f61e8db0570d 68 EthernetNetIf eth;
tom_coxon 0:f61e8db0570d 69 NTPClient ntp;
tom_coxon 0:f61e8db0570d 70 HTTPClient http;
tom_coxon 0:f61e8db0570d 71
tom_coxon 0:f61e8db0570d 72 int ethernetUp = 0;//flag ethernet status 1 = up 0 = down
tom_coxon 0:f61e8db0570d 73
tom_coxon 0:f61e8db0570d 74 WavPlayer myWavPlayer;
tom_coxon 0:f61e8db0570d 75
tom_coxon 0:f61e8db0570d 76 EAOLED oled(p5, p21, p7, p8, p25); // mosi, dnc, sclk, cs, power
tom_coxon 0:f61e8db0570d 77
tom_coxon 0:f61e8db0570d 78 SDFileSystem sd(p5, p6, p7, p24, "sd");//mosi, miso, sclk, cs, name
tom_coxon 0:f61e8db0570d 79
tom_coxon 0:f61e8db0570d 80 DigitalOut led1(LED1);
tom_coxon 0:f61e8db0570d 81 //DigitalOut led2(LED2);
tom_coxon 0:f61e8db0570d 82 //DigitalOut led3(LED3);
tom_coxon 0:f61e8db0570d 83 DigitalOut led4(LED4);
tom_coxon 0:f61e8db0570d 84
tom_coxon 0:f61e8db0570d 85 Ticker secTicker;
tom_coxon 0:f61e8db0570d 86 Ticker blink;
tom_coxon 0:f61e8db0570d 87
tom_coxon 0:f61e8db0570d 88 int hour, minute, second, bongCount;
tom_coxon 0:f61e8db0570d 89
tom_coxon 0:f61e8db0570d 90 char day[12];
tom_coxon 0:f61e8db0570d 91 char dmy[12];
tom_coxon 0:f61e8db0570d 92 char hms[12];
tom_coxon 0:f61e8db0570d 93
tom_coxon 0:f61e8db0570d 94
tom_coxon 0:f61e8db0570d 95 void startUpMsgToOLED() {
tom_coxon 0:f61e8db0570d 96 oled.cls();
tom_coxon 0:f61e8db0570d 97 oled.locate(0,1);
tom_coxon 0:f61e8db0570d 98 oled.printf(" mbed clock\n");
tom_coxon 0:f61e8db0570d 99 oled.locate(0,3);
tom_coxon 0:f61e8db0570d 100 oled.printf(" connect\n");
tom_coxon 0:f61e8db0570d 101 oled.locate(0,5);
tom_coxon 0:f61e8db0570d 102 oled.printf(" ethernet\n");
tom_coxon 0:f61e8db0570d 103 oled.locate(0,7);
tom_coxon 0:f61e8db0570d 104 oled.printf(" to start..\n");
tom_coxon 0:f61e8db0570d 105 }
tom_coxon 0:f61e8db0570d 106
tom_coxon 0:f61e8db0570d 107 //sets up ethernet connection only once
tom_coxon 0:f61e8db0570d 108 void ethernetSetup() {
tom_coxon 0:f61e8db0570d 109 if (!ethernetUp) {
tom_coxon 0:f61e8db0570d 110 printf("Connecting to network ...\r\n");
tom_coxon 0:f61e8db0570d 111 EthernetErr ethErr = eth.setup();
tom_coxon 0:f61e8db0570d 112 if (ethErr) {
tom_coxon 0:f61e8db0570d 113 printf("Error %d in setup.\r\n", ethErr);
tom_coxon 0:f61e8db0570d 114 }
tom_coxon 0:f61e8db0570d 115 printf("Network interface is up\r\n");
tom_coxon 0:f61e8db0570d 116 ethernetUp = 1;
tom_coxon 0:f61e8db0570d 117 }
tom_coxon 0:f61e8db0570d 118 }
tom_coxon 0:f61e8db0570d 119
tom_coxon 0:f61e8db0570d 120 //blinks led4 during download
tom_coxon 0:f61e8db0570d 121 void blinkLED4() {
tom_coxon 0:f61e8db0570d 122 led4 = !led4;
tom_coxon 0:f61e8db0570d 123 }
tom_coxon 0:f61e8db0570d 124
tom_coxon 0:f61e8db0570d 125 void downloadFileToSD(char *url, char *path) {
tom_coxon 0:f61e8db0570d 126 oled.cls();
tom_coxon 0:f61e8db0570d 127 oled.locate(0,1);
tom_coxon 0:f61e8db0570d 128 oled.printf("Downloading\n");
tom_coxon 0:f61e8db0570d 129 oled.locate(0,3);
tom_coxon 0:f61e8db0570d 130 oled.printf(" Wav files\n");
tom_coxon 0:f61e8db0570d 131 oled.locate(0,5);
tom_coxon 0:f61e8db0570d 132 oled.printf("Please wait\n");
tom_coxon 0:f61e8db0570d 133
tom_coxon 0:f61e8db0570d 134 HTTPFile httpfile(path);
tom_coxon 0:f61e8db0570d 135
tom_coxon 0:f61e8db0570d 136 printf("Downloading to ");
tom_coxon 0:f61e8db0570d 137 printf("%s", path);
tom_coxon 0:f61e8db0570d 138 printf(" please wait ... \r\n");
tom_coxon 0:f61e8db0570d 139
tom_coxon 0:f61e8db0570d 140 blink.attach(& blinkLED4, 0.5);
tom_coxon 0:f61e8db0570d 141
tom_coxon 0:f61e8db0570d 142 HTTPResult result = http.get(url, &httpfile);
tom_coxon 0:f61e8db0570d 143
tom_coxon 0:f61e8db0570d 144 if (result == HTTP_OK) {
tom_coxon 0:f61e8db0570d 145 printf("File downloaded OK\r\n");
tom_coxon 0:f61e8db0570d 146 } else {
tom_coxon 0:f61e8db0570d 147 printf("Error during download %d\r\n", result);
tom_coxon 0:f61e8db0570d 148 }
tom_coxon 0:f61e8db0570d 149
tom_coxon 0:f61e8db0570d 150 blink.detach();
tom_coxon 0:f61e8db0570d 151
tom_coxon 0:f61e8db0570d 152 led4 = 0;
tom_coxon 0:f61e8db0570d 153 }
tom_coxon 0:f61e8db0570d 154
tom_coxon 0:f61e8db0570d 155 void downloadFiles() {
tom_coxon 0:f61e8db0570d 156
tom_coxon 0:f61e8db0570d 157 ethernetSetup();
tom_coxon 0:f61e8db0570d 158
tom_coxon 0:f61e8db0570d 159 printf("\r\n----------- Starting file download ------------\r\n");
tom_coxon 0:f61e8db0570d 160
tom_coxon 0:f61e8db0570d 161 downloadFileToSD("http://homepage.ntlworld.com/green_bean/mbed/bong.wav", "/sd/bong.wav" );
tom_coxon 0:f61e8db0570d 162 downloadFileToSD("http://homepage.ntlworld.com/green_bean/mbed/quarter.wav", "/sd/quarter.wav" );
tom_coxon 0:f61e8db0570d 163 downloadFileToSD("http://homepage.ntlworld.com/green_bean/mbed/hour.wav", "/sd/hour.wav" );
tom_coxon 0:f61e8db0570d 164
tom_coxon 0:f61e8db0570d 165 printf("-------------- Files downloaded ------------\r\n");
tom_coxon 0:f61e8db0570d 166
tom_coxon 0:f61e8db0570d 167 }
tom_coxon 0:f61e8db0570d 168
tom_coxon 0:f61e8db0570d 169 void checkChimeWavFilesExist() {
tom_coxon 0:f61e8db0570d 170 int filesExist = 1;//default exist
tom_coxon 0:f61e8db0570d 171 FILE *fp1 = fopen("/sd/bong.wav", "rb");
tom_coxon 0:f61e8db0570d 172 if (fp1 == NULL) {
tom_coxon 0:f61e8db0570d 173 filesExist = 0;
tom_coxon 0:f61e8db0570d 174 } else fclose(fp1);
tom_coxon 0:f61e8db0570d 175 FILE *fp2 = fopen("/sd/quarter.wav", "rb");
tom_coxon 0:f61e8db0570d 176 if (fp2 == NULL) {
tom_coxon 0:f61e8db0570d 177 filesExist = 0;
tom_coxon 0:f61e8db0570d 178 } else fclose(fp2);
tom_coxon 0:f61e8db0570d 179 FILE *fp3 = fopen("/sd/hour.wav", "rb");
tom_coxon 0:f61e8db0570d 180 if (fp3 == NULL) {
tom_coxon 0:f61e8db0570d 181 filesExist = 0;
tom_coxon 0:f61e8db0570d 182 } else fclose(fp3);
tom_coxon 0:f61e8db0570d 183
tom_coxon 0:f61e8db0570d 184 if (!filesExist) downloadFiles();
tom_coxon 0:f61e8db0570d 185 }
tom_coxon 0:f61e8db0570d 186
tom_coxon 0:f61e8db0570d 187 void setmbedClock() {
tom_coxon 0:f61e8db0570d 188
tom_coxon 0:f61e8db0570d 189 startUpMsgToOLED();
tom_coxon 0:f61e8db0570d 190 printf("Setting mbed clock\r\n");
tom_coxon 0:f61e8db0570d 191
tom_coxon 0:f61e8db0570d 192 ethernetSetup();
tom_coxon 0:f61e8db0570d 193
tom_coxon 0:f61e8db0570d 194 printf("Connecting to time server...\r\n");
tom_coxon 0:f61e8db0570d 195
tom_coxon 0:f61e8db0570d 196 Host server(IpAddr(), 123, "0.uk.pool.ntp.org");
tom_coxon 0:f61e8db0570d 197 ntp.setTime(server);
tom_coxon 0:f61e8db0570d 198
tom_coxon 0:f61e8db0570d 199 printf("Time updated\r\n");
tom_coxon 0:f61e8db0570d 200 oled.cls();
tom_coxon 0:f61e8db0570d 201 }
tom_coxon 0:f61e8db0570d 202
tom_coxon 0:f61e8db0570d 203 void updateTimeDisplay() {
tom_coxon 0:f61e8db0570d 204
tom_coxon 0:f61e8db0570d 205 time_t seconds;
tom_coxon 0:f61e8db0570d 206 seconds = time(NULL);
tom_coxon 0:f61e8db0570d 207
tom_coxon 0:f61e8db0570d 208 struct tm *timeStruct = localtime(&seconds);
tom_coxon 0:f61e8db0570d 209
tom_coxon 0:f61e8db0570d 210 if (timeStruct->tm_isdst) {//daylight savings is on add one hour
tom_coxon 0:f61e8db0570d 211 seconds+=3600;
tom_coxon 0:f61e8db0570d 212 tm *timeStruct = localtime(&seconds);
tom_coxon 0:f61e8db0570d 213 }
tom_coxon 0:f61e8db0570d 214
tom_coxon 0:f61e8db0570d 215 strftime(day,sizeof(day)," %A ", timeStruct);//localtime(&seconds));
tom_coxon 0:f61e8db0570d 216 strftime(dmy,sizeof(dmy),"%d/%m/%Y", timeStruct);//localtime(&seconds));
tom_coxon 0:f61e8db0570d 217 strftime(hms,sizeof(hms),"%H:%M:%S", timeStruct);//localtime(&seconds));
tom_coxon 0:f61e8db0570d 218
tom_coxon 0:f61e8db0570d 219 //global hour, minute & second used in main() to set chimes
tom_coxon 0:f61e8db0570d 220 hour = timeStruct->tm_hour;
tom_coxon 0:f61e8db0570d 221 minute = timeStruct->tm_min;
tom_coxon 0:f61e8db0570d 222 second = timeStruct->tm_sec;
tom_coxon 0:f61e8db0570d 223
tom_coxon 0:f61e8db0570d 224 if (debug == 1) printf("%s %s %s\r\n", hms,day,dmy);
tom_coxon 0:f61e8db0570d 225
tom_coxon 0:f61e8db0570d 226 oled.locate((12-strlen(day))/2,1);
tom_coxon 0:f61e8db0570d 227 oled.printf("%s", day);
tom_coxon 0:f61e8db0570d 228 oled.locate((12-strlen(hms))/2,4);
tom_coxon 0:f61e8db0570d 229 oled.printf("%s", hms);
tom_coxon 0:f61e8db0570d 230 oled.locate((12-strlen(dmy))/2,7);
tom_coxon 0:f61e8db0570d 231 oled.printf("%s", dmy);
tom_coxon 0:f61e8db0570d 232
tom_coxon 0:f61e8db0570d 233 }
tom_coxon 0:f61e8db0570d 234
tom_coxon 0:f61e8db0570d 235 void checkIfChimesRequired() {
tom_coxon 0:f61e8db0570d 236
tom_coxon 0:f61e8db0570d 237 //check if time to sound hour
tom_coxon 0:f61e8db0570d 238 if (minute == 59 && second == 34) {
tom_coxon 0:f61e8db0570d 239 second ++; // ensure does not reenter this before updateTime
tom_coxon 0:f61e8db0570d 240 secTicker.detach();
tom_coxon 0:f61e8db0570d 241 myWavPlayer.play_wave("/sd/hour.wav");
tom_coxon 0:f61e8db0570d 242 secTicker.attach(& updateTimeDisplay, 1);
tom_coxon 0:f61e8db0570d 243 bongCount = (hour % 12) + 1; // No. bongs to count the hours
tom_coxon 0:f61e8db0570d 244 //check if time to sound quarter hour
tom_coxon 0:f61e8db0570d 245 } else if (minute % 15 == 0 && second == 0 ) { // will not strike on hour as caught above
tom_coxon 0:f61e8db0570d 246 second ++; // ensure does not reenter this before updateTime
tom_coxon 0:f61e8db0570d 247 secTicker.detach();
tom_coxon 0:f61e8db0570d 248 myWavPlayer.play_wave("/sd/quarter.wav");
tom_coxon 0:f61e8db0570d 249 secTicker.attach(& updateTimeDisplay, 1);
tom_coxon 0:f61e8db0570d 250 //check if time for next hour bong
tom_coxon 0:f61e8db0570d 251 } else if (bongCount > 0 && second % 5 == 0) {
tom_coxon 0:f61e8db0570d 252 second ++; // ensure does not reenter this before updateTime
tom_coxon 0:f61e8db0570d 253 secTicker.detach();
tom_coxon 0:f61e8db0570d 254 myWavPlayer.play_wave("/sd/bong.wav");
tom_coxon 0:f61e8db0570d 255 bongCount --;
tom_coxon 0:f61e8db0570d 256 secTicker.attach(& updateTimeDisplay, 1);
tom_coxon 0:f61e8db0570d 257 }
tom_coxon 0:f61e8db0570d 258 }
tom_coxon 0:f61e8db0570d 259
tom_coxon 0:f61e8db0570d 260 int main() {
tom_coxon 0:f61e8db0570d 261
tom_coxon 0:f61e8db0570d 262 printf("\r\n------ Starting Big Ben -------\r\n");
tom_coxon 0:f61e8db0570d 263
tom_coxon 0:f61e8db0570d 264 startUpMsgToOLED();
tom_coxon 0:f61e8db0570d 265
tom_coxon 0:f61e8db0570d 266 //Check if chime wav files are on SD card if not download them
tom_coxon 0:f61e8db0570d 267 checkChimeWavFilesExist();
tom_coxon 0:f61e8db0570d 268
tom_coxon 0:f61e8db0570d 269 //Check if clock already set if < year 1990 assume needs setting
tom_coxon 0:f61e8db0570d 270 time_t seconds = time(NULL);
tom_coxon 0:f61e8db0570d 271 if (seconds < 630720000) {
tom_coxon 0:f61e8db0570d 272 setmbedClock();
tom_coxon 0:f61e8db0570d 273 }
tom_coxon 0:f61e8db0570d 274 oled.cls();
tom_coxon 0:f61e8db0570d 275
tom_coxon 0:f61e8db0570d 276
tom_coxon 0:f61e8db0570d 277 // starting ticker to update time display once per second
tom_coxon 0:f61e8db0570d 278 secTicker.attach(& updateTimeDisplay, 1);
tom_coxon 0:f61e8db0570d 279
tom_coxon 0:f61e8db0570d 280 // On startup the quarter hour chime will play to ensure the SD card can be read
tom_coxon 0:f61e8db0570d 281 // if you do not want this to happen remove the comment from the following line
tom_coxon 0:f61e8db0570d 282 // second = 1; // so does not sound the quarter hour on first start
tom_coxon 0:f61e8db0570d 283
tom_coxon 0:f61e8db0570d 284 while (1) {
tom_coxon 0:f61e8db0570d 285
tom_coxon 0:f61e8db0570d 286 checkIfChimesRequired();
tom_coxon 0:f61e8db0570d 287
tom_coxon 0:f61e8db0570d 288 wait(0.1);
tom_coxon 0:f61e8db0570d 289 }
tom_coxon 0:f61e8db0570d 290
tom_coxon 0:f61e8db0570d 291 }