4180 / Mbed 2 deprecated Internet_LCD_Clock

Dependencies:   4DGL-uLCD-SE EthernetInterface NTPClient TextLCD mbed PinDetect SDFileSystem wave_player mbed-rtos

Fork of Internet_LCD_Clock by jim hamblen

Committer:
ashea6
Date:
Wed Apr 20 20:28:20 2016 +0000
Revision:
5:818735a07b88
Parent:
3:b4e7f126c80a
Child:
6:e08b5dbdfbcf
wave error

Who changed what in which revision?

UserRevisionLine numberNew contents of line
4180_1 0:5c5226aac712 1 #include "mbed.h"
ashea6 5:818735a07b88 2 //#include "EthernetNetIf.h"
4180_1 0:5c5226aac712 3 #include "NTPClient.h"
ashea6 5:818735a07b88 4 #include "EthernetInterface.h"
ashea6 3:b4e7f126c80a 5 #include "uLCD_4DGL.h"
ashea6 3:b4e7f126c80a 6 #include "Speaker.h"
ashea6 3:b4e7f126c80a 7 #include "SDFileSystem.h"
ashea6 3:b4e7f126c80a 8 #include "wave_player.h"
ashea6 5:818735a07b88 9 #include "PinDetect.h"
ashea6 5:818735a07b88 10
ashea6 5:818735a07b88 11
ashea6 3:b4e7f126c80a 12 #include <vector>
ashea6 3:b4e7f126c80a 13 #include <string>
ashea6 3:b4e7f126c80a 14
ashea6 5:818735a07b88 15 // Parameters
ashea6 5:818735a07b88 16 char* domain_name = "0.uk.pool.ntp.org";
ashea6 5:818735a07b88 17 int port_number = 123;
ashea6 5:818735a07b88 18
ashea6 5:818735a07b88 19 // Networking
ashea6 5:818735a07b88 20 EthernetInterface eth;
ashea6 5:818735a07b88 21 NTPClient ntp_client;
ashea6 5:818735a07b88 22
4180_1 0:5c5226aac712 23 // Internet of Things clock example: LCD time is set via internet NTP time server
ashea6 5:818735a07b88 24 uLCD_4DGL uLCD(p28,p27,p29); // create a global LCD object
ashea6 5:818735a07b88 25 //EthernetNetIf eth;
ashea6 5:818735a07b88 26 //NTPClient ntp;
ashea6 5:818735a07b88 27 SDFileSystem sd(p11, p12, p13, p14, "sd"); //SD card
ashea6 3:b4e7f126c80a 28 AnalogOut DACout(p18); //speaker
ashea6 3:b4e7f126c80a 29 wave_player waver(&DACout);
ashea6 5:818735a07b88 30 PinDetect snooze(p19); //snooze button
ashea6 5:818735a07b88 31 PinDetect off(p20); //turn alarm off
ashea6 5:818735a07b88 32 DigitalIn sdd(p12);
ashea6 5:818735a07b88 33 DigitalOut myled1(LED1);
ashea6 5:818735a07b88 34 DigitalOut myled2(LED2);
ashea6 3:b4e7f126c80a 35
ashea6 5:818735a07b88 36 #define snoozeTime 60
ashea6 3:b4e7f126c80a 37
ashea6 3:b4e7f126c80a 38 //Global Variables
ashea6 3:b4e7f126c80a 39 //variables for SD sound
ashea6 5:818735a07b88 40 vector <string> filenames;
ashea6 3:b4e7f126c80a 41 int current = 0;
ashea6 5:818735a07b88 42 bool playing = false;
ashea6 5:818735a07b88 43 bool sd_insert = false;
ashea6 3:b4e7f126c80a 44 static int veclen = 5;
ashea6 3:b4e7f126c80a 45 FILE *wave_file;
ashea6 5:818735a07b88 46 //variables for alarm
ashea6 3:b4e7f126c80a 47 //system time structure
ashea6 3:b4e7f126c80a 48 time_t ctTime; //ctTime = current time
ashea6 5:818735a07b88 49 // Base alarm time-24 hour clock
ashea6 3:b4e7f126c80a 50 int baseAlarmHour = 0; //0-23
ashea6 5:818735a07b88 51 int baseAlarmMin = 0;
ashea6 3:b4e7f126c80a 52 // Current alarm time
ashea6 5:818735a07b88 53 int curAlarmHour = 18; //0-23
ashea6 5:818735a07b88 54 int curAlarmMin = 9;
ashea6 5:818735a07b88 55 //SongPlayer mySpeaker(p26);
ashea6 5:818735a07b88 56 Ticker flippera;
ashea6 5:818735a07b88 57
ashea6 5:818735a07b88 58 //alarm tone
ashea6 5:818735a07b88 59 float note[17]= {1568.0,1396.9,1244.5,1244.5,1396.9,1568.0,1568.0,1568.0,1396.9,
ashea6 5:818735a07b88 60 1244.5,1396.9,1568.0,1396.9,1244.5,1174.7,1244.5,1244.5,
ashea6 5:818735a07b88 61 };
ashea6 5:818735a07b88 62 float duration[17]= {0.48,0.24,0.72,0.48,0.24,0.48,0.24,0.24,0.24,
ashea6 5:818735a07b88 63 0.24,0.24,0.24,0.24,0.48,0.24,0.48,0.48
ashea6 5:818735a07b88 64 };
ashea6 5:818735a07b88 65
ashea6 5:818735a07b88 66 void flip_time()
ashea6 5:818735a07b88 67 {
ashea6 5:818735a07b88 68 myled1 = !myled1;
ashea6 5:818735a07b88 69 char time_buffer[80];
ashea6 5:818735a07b88 70 uLCD.locate(0, 1);
ashea6 5:818735a07b88 71 ctTime = time(NULL);
ashea6 5:818735a07b88 72 strftime(time_buffer, 80, " %a %b %d\n %T %p %z\n %Z\n", \
ashea6 5:818735a07b88 73 localtime(&ctTime));
ashea6 5:818735a07b88 74 uLCD.printf(" UTC/GMT:\n%s", time_buffer);
ashea6 5:818735a07b88 75 }
ashea6 5:818735a07b88 76
ashea6 5:818735a07b88 77 void thread_time(void const *args)
ashea6 5:818735a07b88 78 {
ashea6 5:818735a07b88 79 while(1) {
ashea6 5:818735a07b88 80 myled1 = !myled1;
ashea6 5:818735a07b88 81 char time_buffer[80];
ashea6 5:818735a07b88 82 uLCD.locate(0, 1);
ashea6 5:818735a07b88 83 ctTime = time(NULL);
ashea6 5:818735a07b88 84 strftime(time_buffer, 80, " %a %b %d\n %T %p %z\n %Z\n", \
ashea6 5:818735a07b88 85 localtime(&ctTime));
ashea6 5:818735a07b88 86 uLCD.printf(" UTC/GMT:\n%s", time_buffer);
ashea6 5:818735a07b88 87 Thread::wait(1000);
ashea6 5:818735a07b88 88 }
ashea6 5:818735a07b88 89 }
ashea6 5:818735a07b88 90
ashea6 3:b4e7f126c80a 91
ashea6 3:b4e7f126c80a 92 //Check for SD Card
ashea6 5:818735a07b88 93 void sd_check()
ashea6 5:818735a07b88 94 {
ashea6 3:b4e7f126c80a 95 int sdPre = sdd.read();
ashea6 5:818735a07b88 96 while (sdPre == 0) {
ashea6 5:818735a07b88 97 uLCD.locate(0,0);
ashea6 5:818735a07b88 98 uLCD.printf("Insert SD card");
ashea6 3:b4e7f126c80a 99 sdPre = sdd.read();
ashea6 3:b4e7f126c80a 100 wait (.5);
ashea6 3:b4e7f126c80a 101 }
ashea6 5:818735a07b88 102 uLCD.cls();
ashea6 3:b4e7f126c80a 103 }
ashea6 3:b4e7f126c80a 104
ashea6 3:b4e7f126c80a 105 //Read File Names
ashea6 3:b4e7f126c80a 106 void read_file_names(char *dir)
ashea6 3:b4e7f126c80a 107 {
ashea6 3:b4e7f126c80a 108 DIR *dp;
ashea6 3:b4e7f126c80a 109 struct dirent *dirp;
ashea6 3:b4e7f126c80a 110 dp = opendir(dir);
ashea6 3:b4e7f126c80a 111 //read all directory and file names in current directory into filename vector
ashea6 3:b4e7f126c80a 112 while((dirp = readdir(dp)) != NULL) {
ashea6 3:b4e7f126c80a 113 filenames.push_back(string(dirp->d_name));
ashea6 3:b4e7f126c80a 114 }
ashea6 3:b4e7f126c80a 115 }
ashea6 3:b4e7f126c80a 116
ashea6 3:b4e7f126c80a 117 //Play file from SD card
ashea6 3:b4e7f126c80a 118 void play_file()
ashea6 3:b4e7f126c80a 119 {
ashea6 5:818735a07b88 120 bool* play_point = &playing;
ashea6 5:818735a07b88 121 //string file_name("/sd/");
ashea6 5:818735a07b88 122 //file_name += filenames[current];
ashea6 5:818735a07b88 123 // wave_file = fopen(file_name.c_str(),"r");
ashea6 5:818735a07b88 124 wave_file=fopen("/sd/bob.wav","r");
ashea6 5:818735a07b88 125 while(playing) {
ashea6 3:b4e7f126c80a 126 waver.play(wave_file, play_point);
ashea6 5:818735a07b88 127 // mySpeaker.PlaySong(note,duration);
ashea6 3:b4e7f126c80a 128 }
ashea6 3:b4e7f126c80a 129 fclose(wave_file);
ashea6 3:b4e7f126c80a 130 }
ashea6 3:b4e7f126c80a 131
ashea6 3:b4e7f126c80a 132 //Interrupt-Snooze Function
ashea6 3:b4e7f126c80a 133 void snooze_hit_callback (void)
ashea6 3:b4e7f126c80a 134 {
ashea6 5:818735a07b88 135 myled1 = !myled1;
ashea6 5:818735a07b88 136 playing = false;
ashea6 5:818735a07b88 137 time_t newtime;
ashea6 3:b4e7f126c80a 138 struct tm * timeinfo;
ashea6 5:818735a07b88 139
ashea6 3:b4e7f126c80a 140 newtime = ctTime + snoozeTime;
ashea6 5:818735a07b88 141 // time (&newtime);
ashea6 3:b4e7f126c80a 142 timeinfo = localtime (&newtime);
ashea6 5:818735a07b88 143 curAlarmMin = timeinfo->tm_min;
ashea6 5:818735a07b88 144 curAlarmHour = timeinfo->tm_hour;
ashea6 3:b4e7f126c80a 145 }
ashea6 3:b4e7f126c80a 146
ashea6 5:818735a07b88 147 //Interrupt- Off Function
ashea6 3:b4e7f126c80a 148 void off_hit_callback (void)
ashea6 3:b4e7f126c80a 149 {
ashea6 5:818735a07b88 150 myled2 = !myled2;
ashea6 5:818735a07b88 151 playing = false;
ashea6 3:b4e7f126c80a 152 curAlarmMin = baseAlarmMin;
ashea6 3:b4e7f126c80a 153 curAlarmHour = baseAlarmHour;
ashea6 3:b4e7f126c80a 154 }
ashea6 3:b4e7f126c80a 155 //Time Compare Function
ashea6 3:b4e7f126c80a 156 void timeCompare()
ashea6 3:b4e7f126c80a 157 {
ashea6 5:818735a07b88 158 struct tm * timeinfo;
ashea6 5:818735a07b88 159
ashea6 5:818735a07b88 160 //time(&ctTime);
ashea6 5:818735a07b88 161 timeinfo = localtime (&ctTime);
ashea6 5:818735a07b88 162 if (timeinfo->tm_min == curAlarmMin && timeinfo->tm_hour == curAlarmHour) {
ashea6 5:818735a07b88 163 playing = true;
ashea6 5:818735a07b88 164 play_file();
ashea6 5:818735a07b88 165
ashea6 5:818735a07b88 166 }
ashea6 5:818735a07b88 167 }
ashea6 3:b4e7f126c80a 168
4180_1 0:5c5226aac712 169
ashea6 5:818735a07b88 170 int main()
ashea6 5:818735a07b88 171 {
ashea6 3:b4e7f126c80a 172
ashea6 3:b4e7f126c80a 173 snooze.mode(PullUp);
ashea6 3:b4e7f126c80a 174 off.mode(PullUp);
ashea6 5:818735a07b88 175 wait(0.01);
ashea6 3:b4e7f126c80a 176 snooze.attach_deasserted(&snooze_hit_callback);
ashea6 3:b4e7f126c80a 177 off.attach_deasserted(&off_hit_callback);
ashea6 3:b4e7f126c80a 178 snooze.setSampleFrequency();
ashea6 3:b4e7f126c80a 179 off.setSampleFrequency();
ashea6 5:818735a07b88 180
ashea6 5:818735a07b88 181 //time_t ct_time;
ashea6 5:818735a07b88 182
ashea6 5:818735a07b88 183
ashea6 5:818735a07b88 184 // Initialize LCD
ashea6 5:818735a07b88 185 uLCD.baudrate(115200);
ashea6 5:818735a07b88 186 uLCD.background_color(BLACK);
ashea6 5:818735a07b88 187 uLCD.cls();
ashea6 5:818735a07b88 188
ashea6 5:818735a07b88 189 // Connect to network and wait for DHCP
ashea6 5:818735a07b88 190 uLCD.locate(0,0);
ashea6 5:818735a07b88 191 uLCD.printf("Getting IP Address\n");
ashea6 5:818735a07b88 192 eth.init();
ashea6 5:818735a07b88 193 if ( eth.connect(60000) == -1 ) {
ashea6 5:818735a07b88 194 uLCD.printf("ERROR: Could not\nget IP address");
4180_1 0:5c5226aac712 195 return -1;
4180_1 0:5c5226aac712 196 }
ashea6 5:818735a07b88 197 uLCD.printf("IP address is \n%s\n\n",eth.getIPAddress());
4180_1 0:5c5226aac712 198 wait(1);
ashea6 5:818735a07b88 199
ashea6 5:818735a07b88 200 // Read time from server
ashea6 5:818735a07b88 201 uLCD.printf("Reading time...\n\r");
ashea6 5:818735a07b88 202 ntp_client.setTime(domain_name, port_number);
ashea6 5:818735a07b88 203 uLCD.printf("Time set\n");
ashea6 5:818735a07b88 204 wait(2);
ashea6 5:818735a07b88 205 eth.disconnect();
ashea6 5:818735a07b88 206
ashea6 5:818735a07b88 207 // Reset LCD
ashea6 5:818735a07b88 208 uLCD.background_color(WHITE);
ashea6 5:818735a07b88 209 uLCD.textbackground_color(WHITE);
ashea6 5:818735a07b88 210 uLCD.color(RED);
ashea6 5:818735a07b88 211 uLCD.cls();
ashea6 5:818735a07b88 212 uLCD.text_height(2);
ashea6 5:818735a07b88 213
ashea6 5:818735a07b88 214 // flippera.attach(&flip_time, 1.0);
ashea6 5:818735a07b88 215 Thread time_thread(thread_time);
ashea6 5:818735a07b88 216
ashea6 5:818735a07b88 217 // Loop and update clock
4180_1 0:5c5226aac712 218 while (1) {
ashea6 5:818735a07b88 219 /*
ashea6 5:818735a07b88 220 myled1 = !myled1;
ashea6 5:818735a07b88 221 char time_buffer[80];
ashea6 5:818735a07b88 222 uLCD.locate(0, 1);
4180_1 0:5c5226aac712 223 ctTime = time(NULL);
ashea6 5:818735a07b88 224 strftime(time_buffer, 80, " %a %b %d\n %T %p %z\n %Z\n", \
ashea6 5:818735a07b88 225 localtime(&ctTime));
ashea6 5:818735a07b88 226 uLCD.printf(" UTC/GMT:\n%s", time_buffer);
ashea6 5:818735a07b88 227 */
ashea6 3:b4e7f126c80a 228 timeCompare();
ashea6 5:818735a07b88 229 wait(0.1);
ashea6 5:818735a07b88 230
4180_1 0:5c5226aac712 231 }
ashea6 5:818735a07b88 232
ashea6 5:818735a07b88 233
4180_1 0:5c5226aac712 234 }