Clock
Dependencies: 4DGL-uLCD-SE EthernetInterface NTPClient TextLCD mbed PinDetect SDFileSystem wave_player mbed-rtos
Fork of Internet_LCD_Clock by
Internet_LCD_Clock.cpp@3:b4e7f126c80a, 2016-04-11 (annotated)
- Committer:
- ashea6
- Date:
- Mon Apr 11 17:56:48 2016 +0000
- Revision:
- 3:b4e7f126c80a
- Parent:
- 1:09fcc9b81f23
- Child:
- 5:818735a07b88
functions
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
4180_1 | 0:5c5226aac712 | 1 | #include "mbed.h" |
4180_1 | 0:5c5226aac712 | 2 | #include "EthernetNetIf.h" |
4180_1 | 0:5c5226aac712 | 3 | #include "NTPClient.h" |
ashea6 | 3:b4e7f126c80a | 4 | #include "uLCD_4DGL.h" |
ashea6 | 3:b4e7f126c80a | 5 | #include "Speaker.h" |
ashea6 | 3:b4e7f126c80a | 6 | #include "SDFileSystem.h" |
ashea6 | 3:b4e7f126c80a | 7 | #include "wave_player.h" |
ashea6 | 3:b4e7f126c80a | 8 | #include <vector> |
ashea6 | 3:b4e7f126c80a | 9 | #include <string> |
ashea6 | 3:b4e7f126c80a | 10 | |
4180_1 | 0:5c5226aac712 | 11 | // Internet of Things clock example: LCD time is set via internet NTP time server |
ashea6 | 3:b4e7f126c80a | 12 | uLCD_4DGL lcd(p28,p27,p29); // create a global LCD object |
4180_1 | 0:5c5226aac712 | 13 | EthernetNetIf eth; |
4180_1 | 0:5c5226aac712 | 14 | NTPClient ntp; |
ashea6 | 3:b4e7f126c80a | 15 | SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card |
ashea6 | 3:b4e7f126c80a | 16 | AnalogOut DACout(p18); //speaker |
ashea6 | 3:b4e7f126c80a | 17 | wave_player waver(&DACout); |
ashea6 | 3:b4e7f126c80a | 18 | PinDetect snooze(p15); //snooze button |
ashea6 | 3:b4e7f126c80a | 19 | PinDetect off(p16); //turn alarm off |
ashea6 | 3:b4e7f126c80a | 20 | |
ashea6 | 3:b4e7f126c80a | 21 | #define snoozeTime 420 |
ashea6 | 3:b4e7f126c80a | 22 | |
ashea6 | 3:b4e7f126c80a | 23 | //Global Variables |
ashea6 | 3:b4e7f126c80a | 24 | //variables for SD sound |
ashea6 | 3:b4e7f126c80a | 25 | vector <string> filenames; |
ashea6 | 3:b4e7f126c80a | 26 | int current = 0; |
ashea6 | 3:b4e7f126c80a | 27 | bool play = false; sd_insert = false; |
ashea6 | 3:b4e7f126c80a | 28 | static int veclen = 5; |
ashea6 | 3:b4e7f126c80a | 29 | FILE *wave_file; |
ashea6 | 3:b4e7f126c80a | 30 | //variables for alarm |
ashea6 | 3:b4e7f126c80a | 31 | //system time structure |
ashea6 | 3:b4e7f126c80a | 32 | time_t ctTime; //ctTime = current time |
ashea6 | 3:b4e7f126c80a | 33 | // Base alarm time-24 hour clock |
ashea6 | 3:b4e7f126c80a | 34 | int baseAlarmHour = 0; //0-23 |
ashea6 | 3:b4e7f126c80a | 35 | int baseAlarmMin = 0; |
ashea6 | 3:b4e7f126c80a | 36 | // Current alarm time |
ashea6 | 3:b4e7f126c80a | 37 | int curAlarmHour = 0; //0-23 |
ashea6 | 3:b4e7f126c80a | 38 | int curAlarmMin = 0; |
ashea6 | 3:b4e7f126c80a | 39 | |
ashea6 | 3:b4e7f126c80a | 40 | //Check for SD Card |
ashea6 | 3:b4e7f126c80a | 41 | void sd_check(){ |
ashea6 | 3:b4e7f126c80a | 42 | int sdPre = sdd.read(); |
ashea6 | 3:b4e7f126c80a | 43 | while (sdPre == 0){ |
ashea6 | 3:b4e7f126c80a | 44 | lcd.locate(0,0); |
ashea6 | 3:b4e7f126c80a | 45 | lcd.printf("Insert SD card"); |
ashea6 | 3:b4e7f126c80a | 46 | sdPre = sdd.read(); |
ashea6 | 3:b4e7f126c80a | 47 | wait (.5); |
ashea6 | 3:b4e7f126c80a | 48 | } |
ashea6 | 3:b4e7f126c80a | 49 | lcd.cls(); |
ashea6 | 3:b4e7f126c80a | 50 | } |
ashea6 | 3:b4e7f126c80a | 51 | |
ashea6 | 3:b4e7f126c80a | 52 | //Read File Names |
ashea6 | 3:b4e7f126c80a | 53 | void read_file_names(char *dir) |
ashea6 | 3:b4e7f126c80a | 54 | { |
ashea6 | 3:b4e7f126c80a | 55 | DIR *dp; |
ashea6 | 3:b4e7f126c80a | 56 | struct dirent *dirp; |
ashea6 | 3:b4e7f126c80a | 57 | dp = opendir(dir); |
ashea6 | 3:b4e7f126c80a | 58 | //read all directory and file names in current directory into filename vector |
ashea6 | 3:b4e7f126c80a | 59 | while((dirp = readdir(dp)) != NULL) { |
ashea6 | 3:b4e7f126c80a | 60 | filenames.push_back(string(dirp->d_name)); |
ashea6 | 3:b4e7f126c80a | 61 | } |
ashea6 | 3:b4e7f126c80a | 62 | } |
ashea6 | 3:b4e7f126c80a | 63 | |
ashea6 | 3:b4e7f126c80a | 64 | //Play file from SD card |
ashea6 | 3:b4e7f126c80a | 65 | void play_file() |
ashea6 | 3:b4e7f126c80a | 66 | { |
ashea6 | 3:b4e7f126c80a | 67 | bool* play_point = &play; |
ashea6 | 3:b4e7f126c80a | 68 | string file_name("/sd/"); |
ashea6 | 3:b4e7f126c80a | 69 | file_name += filenames[current]; |
ashea6 | 3:b4e7f126c80a | 70 | wave_file = fopen(file_name.c_str(),"r"); |
ashea6 | 3:b4e7f126c80a | 71 | while(play){ |
ashea6 | 3:b4e7f126c80a | 72 | waver.play(wave_file, play_point); |
ashea6 | 3:b4e7f126c80a | 73 | } |
ashea6 | 3:b4e7f126c80a | 74 | fclose(wave_file); |
ashea6 | 3:b4e7f126c80a | 75 | } |
ashea6 | 3:b4e7f126c80a | 76 | |
ashea6 | 3:b4e7f126c80a | 77 | //Interrupt-Snooze Function |
ashea6 | 3:b4e7f126c80a | 78 | void snooze_hit_callback (void) |
ashea6 | 3:b4e7f126c80a | 79 | { |
ashea6 | 3:b4e7f126c80a | 80 | play = false; |
ashea6 | 3:b4e7f126c80a | 81 | time_t = newtime; |
ashea6 | 3:b4e7f126c80a | 82 | struct tm * timeinfo; |
ashea6 | 3:b4e7f126c80a | 83 | |
ashea6 | 3:b4e7f126c80a | 84 | newtime = ctTime + snoozeTime; |
ashea6 | 3:b4e7f126c80a | 85 | timeinfo = localtime (&newtime); |
ashea6 | 3:b4e7f126c80a | 86 | curAlarmMin = timeinfo.tm_min; |
ashea6 | 3:b4e7f126c80a | 87 | curAlarmHour = timeinfo.tm_hour; |
ashea6 | 3:b4e7f126c80a | 88 | } |
ashea6 | 3:b4e7f126c80a | 89 | |
ashea6 | 3:b4e7f126c80a | 90 | //Interrupt- Off Function |
ashea6 | 3:b4e7f126c80a | 91 | void off_hit_callback (void) |
ashea6 | 3:b4e7f126c80a | 92 | { |
ashea6 | 3:b4e7f126c80a | 93 | play = false; |
ashea6 | 3:b4e7f126c80a | 94 | curAlarmMin = baseAlarmMin; |
ashea6 | 3:b4e7f126c80a | 95 | curAlarmHour = baseAlarmHour; |
ashea6 | 3:b4e7f126c80a | 96 | } |
ashea6 | 3:b4e7f126c80a | 97 | //Time Compare Function |
ashea6 | 3:b4e7f126c80a | 98 | void timeCompare() |
ashea6 | 3:b4e7f126c80a | 99 | { |
ashea6 | 3:b4e7f126c80a | 100 | struct tm * timeinfo; |
ashea6 | 3:b4e7f126c80a | 101 | |
ashea6 | 3:b4e7f126c80a | 102 | //time(&ctTime); |
ashea6 | 3:b4e7f126c80a | 103 | timeinfo = localtime (&ctTime); |
ashea6 | 3:b4e7f126c80a | 104 | if (timeinfo.tm_min == curAlarmMin && timeinfo.tm_hour == curAlarmHour) |
ashea6 | 3:b4e7f126c80a | 105 | { |
ashea6 | 3:b4e7f126c80a | 106 | play = true; |
ashea6 | 3:b4e7f126c80a | 107 | play_file(); |
ashea6 | 3:b4e7f126c80a | 108 | } |
ashea6 | 3:b4e7f126c80a | 109 | } |
ashea6 | 3:b4e7f126c80a | 110 | |
4180_1 | 0:5c5226aac712 | 111 | |
4180_1 | 0:5c5226aac712 | 112 | int main() { |
ashea6 | 3:b4e7f126c80a | 113 | |
ashea6 | 3:b4e7f126c80a | 114 | snooze.mode(PullUp); |
ashea6 | 3:b4e7f126c80a | 115 | off.mode(PullUp); |
ashea6 | 3:b4e7f126c80a | 116 | wait(.01); |
ashea6 | 3:b4e7f126c80a | 117 | snooze.attach_deasserted(&snooze_hit_callback); |
ashea6 | 3:b4e7f126c80a | 118 | off.attach_deasserted(&off_hit_callback); |
ashea6 | 3:b4e7f126c80a | 119 | snooze.setSampleFrequency(); |
ashea6 | 3:b4e7f126c80a | 120 | off.setSampleFrequency(); |
ashea6 | 3:b4e7f126c80a | 121 | |
4180_1 | 0:5c5226aac712 | 122 | //clear LCD |
4180_1 | 0:5c5226aac712 | 123 | lcd.cls(); |
4180_1 | 0:5c5226aac712 | 124 | // lcd.printf prints to LCD display; |
4180_1 | 0:5c5226aac712 | 125 | lcd.printf("Get IP addr..."); |
4180_1 | 0:5c5226aac712 | 126 | EthernetErr ethErr = eth.setup(); |
4180_1 | 0:5c5226aac712 | 127 | //Get an Internet IP address using DHCP |
4180_1 | 0:5c5226aac712 | 128 | if (ethErr) { |
4180_1 | 0:5c5226aac712 | 129 | //error or timeout getting an IP address |
4180_1 | 0:5c5226aac712 | 130 | lcd.cls(); |
4180_1 | 0:5c5226aac712 | 131 | lcd.printf("Network Error \n\r %d",ethErr); |
4180_1 | 0:5c5226aac712 | 132 | return -1; |
4180_1 | 0:5c5226aac712 | 133 | } |
4180_1 | 0:5c5226aac712 | 134 | lcd.cls(); |
4180_1 | 0:5c5226aac712 | 135 | lcd.printf("Reading Time...\n\r"); |
4180_1 | 0:5c5226aac712 | 136 | //specify time server URL |
4180_1 | 0:5c5226aac712 | 137 | Host server(IpAddr(), 123, "0.uk.pool.ntp.org"); |
4180_1 | 0:5c5226aac712 | 138 | //Read time from server |
4180_1 | 0:5c5226aac712 | 139 | ntp.setTime(server); |
4180_1 | 0:5c5226aac712 | 140 | lcd.printf("Time set"); |
4180_1 | 0:5c5226aac712 | 141 | //Delay for human time to read LCD display |
4180_1 | 0:5c5226aac712 | 142 | wait(1); |
4180_1 | 0:5c5226aac712 | 143 | while (1) { |
4180_1 | 0:5c5226aac712 | 144 | // loop and periodically update the LCD's time display |
4180_1 | 0:5c5226aac712 | 145 | lcd.cls(); |
4180_1 | 0:5c5226aac712 | 146 | ctTime = time(NULL); |
4180_1 | 0:5c5226aac712 | 147 | lcd.printf("UTC: %s", ctime(&ctTime)); |
ashea6 | 3:b4e7f126c80a | 148 | timeCompare(); |
4180_1 | 0:5c5226aac712 | 149 | wait(.25); |
4180_1 | 0:5c5226aac712 | 150 | } |
4180_1 | 0:5c5226aac712 | 151 | } |