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:
Fri Apr 22 16:02:12 2016 +0000
Revision:
6:e08b5dbdfbcf
Parent:
5:818735a07b88
dkdkdkd

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 6:e08b5dbdfbcf 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 6:e08b5dbdfbcf 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 6:e08b5dbdfbcf 120 FILE *wave_file;
ashea6 6:e08b5dbdfbcf 121 printf("\n\n\nHello, wave world!\n");
ashea6 5:818735a07b88 122 wave_file=fopen("/sd/bob.wav","r");
ashea6 6:e08b5dbdfbcf 123 waver.play(wave_file);
ashea6 3:b4e7f126c80a 124 fclose(wave_file);
ashea6 3:b4e7f126c80a 125 }
ashea6 3:b4e7f126c80a 126
ashea6 3:b4e7f126c80a 127 //Interrupt-Snooze Function
ashea6 3:b4e7f126c80a 128 void snooze_hit_callback (void)
ashea6 3:b4e7f126c80a 129 {
ashea6 5:818735a07b88 130 myled1 = !myled1;
ashea6 5:818735a07b88 131 playing = false;
ashea6 5:818735a07b88 132 time_t newtime;
ashea6 3:b4e7f126c80a 133 struct tm * timeinfo;
ashea6 5:818735a07b88 134
ashea6 3:b4e7f126c80a 135 newtime = ctTime + snoozeTime;
ashea6 5:818735a07b88 136 // time (&newtime);
ashea6 3:b4e7f126c80a 137 timeinfo = localtime (&newtime);
ashea6 5:818735a07b88 138 curAlarmMin = timeinfo->tm_min;
ashea6 5:818735a07b88 139 curAlarmHour = timeinfo->tm_hour;
ashea6 3:b4e7f126c80a 140 }
ashea6 3:b4e7f126c80a 141
ashea6 5:818735a07b88 142 //Interrupt- Off Function
ashea6 3:b4e7f126c80a 143 void off_hit_callback (void)
ashea6 3:b4e7f126c80a 144 {
ashea6 5:818735a07b88 145 myled2 = !myled2;
ashea6 5:818735a07b88 146 playing = false;
ashea6 3:b4e7f126c80a 147 curAlarmMin = baseAlarmMin;
ashea6 3:b4e7f126c80a 148 curAlarmHour = baseAlarmHour;
ashea6 3:b4e7f126c80a 149 }
ashea6 3:b4e7f126c80a 150 //Time Compare Function
ashea6 3:b4e7f126c80a 151 void timeCompare()
ashea6 3:b4e7f126c80a 152 {
ashea6 5:818735a07b88 153 struct tm * timeinfo;
ashea6 5:818735a07b88 154
ashea6 5:818735a07b88 155 //time(&ctTime);
ashea6 5:818735a07b88 156 timeinfo = localtime (&ctTime);
ashea6 5:818735a07b88 157 if (timeinfo->tm_min == curAlarmMin && timeinfo->tm_hour == curAlarmHour) {
ashea6 5:818735a07b88 158 playing = true;
ashea6 5:818735a07b88 159 play_file();
ashea6 5:818735a07b88 160
ashea6 5:818735a07b88 161 }
ashea6 5:818735a07b88 162 }
ashea6 3:b4e7f126c80a 163
4180_1 0:5c5226aac712 164
ashea6 5:818735a07b88 165 int main()
ashea6 5:818735a07b88 166 {
ashea6 6:e08b5dbdfbcf 167 play_file();
ashea6 3:b4e7f126c80a 168 snooze.mode(PullUp);
ashea6 3:b4e7f126c80a 169 off.mode(PullUp);
ashea6 5:818735a07b88 170 wait(0.01);
ashea6 3:b4e7f126c80a 171 snooze.attach_deasserted(&snooze_hit_callback);
ashea6 3:b4e7f126c80a 172 off.attach_deasserted(&off_hit_callback);
ashea6 3:b4e7f126c80a 173 snooze.setSampleFrequency();
ashea6 3:b4e7f126c80a 174 off.setSampleFrequency();
ashea6 5:818735a07b88 175
ashea6 5:818735a07b88 176 //time_t ct_time;
ashea6 5:818735a07b88 177
ashea6 5:818735a07b88 178
ashea6 5:818735a07b88 179 // Initialize LCD
ashea6 5:818735a07b88 180 uLCD.baudrate(115200);
ashea6 5:818735a07b88 181 uLCD.background_color(BLACK);
ashea6 5:818735a07b88 182 uLCD.cls();
ashea6 5:818735a07b88 183
ashea6 5:818735a07b88 184 // Connect to network and wait for DHCP
ashea6 5:818735a07b88 185 uLCD.locate(0,0);
ashea6 5:818735a07b88 186 uLCD.printf("Getting IP Address\n");
ashea6 5:818735a07b88 187 eth.init();
ashea6 5:818735a07b88 188 if ( eth.connect(60000) == -1 ) {
ashea6 5:818735a07b88 189 uLCD.printf("ERROR: Could not\nget IP address");
4180_1 0:5c5226aac712 190 return -1;
4180_1 0:5c5226aac712 191 }
ashea6 5:818735a07b88 192 uLCD.printf("IP address is \n%s\n\n",eth.getIPAddress());
4180_1 0:5c5226aac712 193 wait(1);
ashea6 5:818735a07b88 194
ashea6 5:818735a07b88 195 // Read time from server
ashea6 5:818735a07b88 196 uLCD.printf("Reading time...\n\r");
ashea6 5:818735a07b88 197 ntp_client.setTime(domain_name, port_number);
ashea6 5:818735a07b88 198 uLCD.printf("Time set\n");
ashea6 5:818735a07b88 199 wait(2);
ashea6 5:818735a07b88 200 eth.disconnect();
ashea6 5:818735a07b88 201
ashea6 5:818735a07b88 202 // Reset LCD
ashea6 5:818735a07b88 203 uLCD.background_color(WHITE);
ashea6 5:818735a07b88 204 uLCD.textbackground_color(WHITE);
ashea6 5:818735a07b88 205 uLCD.color(RED);
ashea6 5:818735a07b88 206 uLCD.cls();
ashea6 5:818735a07b88 207 uLCD.text_height(2);
ashea6 5:818735a07b88 208
ashea6 5:818735a07b88 209 // flippera.attach(&flip_time, 1.0);
ashea6 5:818735a07b88 210 Thread time_thread(thread_time);
ashea6 5:818735a07b88 211
ashea6 5:818735a07b88 212 // Loop and update clock
4180_1 0:5c5226aac712 213 while (1) {
ashea6 5:818735a07b88 214 /*
ashea6 5:818735a07b88 215 myled1 = !myled1;
ashea6 5:818735a07b88 216 char time_buffer[80];
ashea6 5:818735a07b88 217 uLCD.locate(0, 1);
4180_1 0:5c5226aac712 218 ctTime = time(NULL);
ashea6 5:818735a07b88 219 strftime(time_buffer, 80, " %a %b %d\n %T %p %z\n %Z\n", \
ashea6 5:818735a07b88 220 localtime(&ctTime));
ashea6 5:818735a07b88 221 uLCD.printf(" UTC/GMT:\n%s", time_buffer);
ashea6 5:818735a07b88 222 */
ashea6 3:b4e7f126c80a 223 timeCompare();
ashea6 5:818735a07b88 224 wait(0.1);
ashea6 5:818735a07b88 225
4180_1 0:5c5226aac712 226 }
ashea6 5:818735a07b88 227
ashea6 5:818735a07b88 228
4180_1 0:5c5226aac712 229 }