Ebrahim Atya
/
grpass_clock
Group assignment alarm clock
main.cpp@0:65f054e83dac, 2019-05-21 (annotated)
- Committer:
- ebrahimatya
- Date:
- Tue May 21 08:29:41 2019 +0000
- Revision:
- 0:65f054e83dac
- Child:
- 4:61d3a7c1b411
init
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ebrahimatya | 0:65f054e83dac | 1 | #include "main.h" |
ebrahimatya | 0:65f054e83dac | 2 | |
ebrahimatya | 0:65f054e83dac | 3 | /******************************************************************* |
ebrahimatya | 0:65f054e83dac | 4 | * Print_lcd |
ebrahimatya | 0:65f054e83dac | 5 | * |
ebrahimatya | 0:65f054e83dac | 6 | * This function clears the LCD, then either prints 1. the alarm time |
ebrahimatya | 0:65f054e83dac | 7 | * or 2. the current time and alarm clock menu state. |
ebrahimatya | 0:65f054e83dac | 8 | ******************************************************************/ |
ebrahimatya | 0:65f054e83dac | 9 | void print_lcd() |
ebrahimatya | 0:65f054e83dac | 10 | { |
ebrahimatya | 0:65f054e83dac | 11 | lcd.cls(); |
ebrahimatya | 0:65f054e83dac | 12 | /* Menu options 2 and above deal with the alarm */ |
ebrahimatya | 0:65f054e83dac | 13 | if (menu > 1) |
ebrahimatya | 0:65f054e83dac | 14 | print_time(ahour, amin); |
ebrahimatya | 0:65f054e83dac | 15 | else |
ebrahimatya | 0:65f054e83dac | 16 | print_time(cTime->tm_hour, cTime->tm_min); |
ebrahimatya | 0:65f054e83dac | 17 | print_menu(); |
ebrahimatya | 0:65f054e83dac | 18 | } |
ebrahimatya | 0:65f054e83dac | 19 | |
ebrahimatya | 0:65f054e83dac | 20 | /****************************************************************** |
ebrahimatya | 0:65f054e83dac | 21 | * Print_time |
ebrahimatya | 0:65f054e83dac | 22 | * |
ebrahimatya | 0:65f054e83dac | 23 | * This function takes an hour(in 24hr time) and a minute and |
ebrahimatya | 0:65f054e83dac | 24 | * prints the correct format to the lcd. |
ebrahimatya | 0:65f054e83dac | 25 | *****************************************************************/ |
ebrahimatya | 0:65f054e83dac | 26 | void print_time(int hour, int minute) |
ebrahimatya | 0:65f054e83dac | 27 | { |
ebrahimatya | 0:65f054e83dac | 28 | /* Variable tmphr is 24 hour time adjusted for timezone */ |
ebrahimatya | 0:65f054e83dac | 29 | int tmphr = hour + timezone; |
ebrahimatya | 0:65f054e83dac | 30 | while (tmphr < 0) |
ebrahimatya | 0:65f054e83dac | 31 | tmphr += 24; |
ebrahimatya | 0:65f054e83dac | 32 | tmphr %= 24; |
ebrahimatya | 0:65f054e83dac | 33 | |
ebrahimatya | 0:65f054e83dac | 34 | /* Variable printhr is 12 hour time for printing */ |
ebrahimatya | 0:65f054e83dac | 35 | int printhr = tmphr % 12; |
ebrahimatya | 0:65f054e83dac | 36 | if (printhr == 0) |
ebrahimatya | 0:65f054e83dac | 37 | printhr += 12; |
ebrahimatya | 0:65f054e83dac | 38 | lcd.printf("%2d:%02d ", printhr, minute); |
ebrahimatya | 0:65f054e83dac | 39 | |
ebrahimatya | 0:65f054e83dac | 40 | /* tmphr allows for checking of AM/PM */ |
ebrahimatya | 0:65f054e83dac | 41 | if ((tmphr > 11) && (tmphr < 24)) |
ebrahimatya | 0:65f054e83dac | 42 | lcd.printf("PM\n"); |
ebrahimatya | 0:65f054e83dac | 43 | else |
ebrahimatya | 0:65f054e83dac | 44 | lcd.printf("AM\n"); |
ebrahimatya | 0:65f054e83dac | 45 | } |
ebrahimatya | 0:65f054e83dac | 46 | |
ebrahimatya | 0:65f054e83dac | 47 | /******************************************************************* |
ebrahimatya | 0:65f054e83dac | 48 | * Print_menu |
ebrahimatya | 0:65f054e83dac | 49 | * |
ebrahimatya | 0:65f054e83dac | 50 | * This function prints the menu state of the alarm clock to the LCD. |
ebrahimatya | 0:65f054e83dac | 51 | * The menu options are 1. Viewing current time 2. Setting Timezone |
ebrahimatya | 0:65f054e83dac | 52 | * 3. Setting alarm hour 4. Setting alarm minute 5. Toggling alarm |
ebrahimatya | 0:65f054e83dac | 53 | ******************************************************************/ |
ebrahimatya | 0:65f054e83dac | 54 | void print_menu() |
ebrahimatya | 0:65f054e83dac | 55 | { |
ebrahimatya | 0:65f054e83dac | 56 | switch (menu){ |
ebrahimatya | 0:65f054e83dac | 57 | case 0: |
ebrahimatya | 0:65f054e83dac | 58 | break; |
ebrahimatya | 0:65f054e83dac | 59 | case 1: |
ebrahimatya | 0:65f054e83dac | 60 | lcd.printf("TZ is (UTC) %+d\n", timezone); |
ebrahimatya | 0:65f054e83dac | 61 | break; |
ebrahimatya | 0:65f054e83dac | 62 | case 2: |
ebrahimatya | 0:65f054e83dac | 63 | lcd.printf("Set Alarm Hour\n"); |
ebrahimatya | 0:65f054e83dac | 64 | break; |
ebrahimatya | 0:65f054e83dac | 65 | case 3: |
ebrahimatya | 0:65f054e83dac | 66 | lcd.printf("Set Alarm Min\n"); |
ebrahimatya | 0:65f054e83dac | 67 | break; |
ebrahimatya | 0:65f054e83dac | 68 | case 4: |
ebrahimatya | 0:65f054e83dac | 69 | lcd.printf("Alarm is "); |
ebrahimatya | 0:65f054e83dac | 70 | if (alarmstate == false) |
ebrahimatya | 0:65f054e83dac | 71 | lcd.printf("Off\n"); |
ebrahimatya | 0:65f054e83dac | 72 | else |
ebrahimatya | 0:65f054e83dac | 73 | lcd.printf("On\n"); |
ebrahimatya | 0:65f054e83dac | 74 | break; |
ebrahimatya | 0:65f054e83dac | 75 | default: |
ebrahimatya | 0:65f054e83dac | 76 | lcd.printf("Invalid menu %d\n", menu); |
ebrahimatya | 0:65f054e83dac | 77 | break; |
ebrahimatya | 0:65f054e83dac | 78 | } |
ebrahimatya | 0:65f054e83dac | 79 | } |
ebrahimatya | 0:65f054e83dac | 80 | |
ebrahimatya | 0:65f054e83dac | 81 | /****************************************************************** |
ebrahimatya | 0:65f054e83dac | 82 | * Change_menu |
ebrahimatya | 0:65f054e83dac | 83 | * |
ebrahimatya | 0:65f054e83dac | 84 | * This function uses a dedicated pushbutton that changes the menu. |
ebrahimatya | 0:65f054e83dac | 85 | * The state variable, menu, gives the user the ability to cycle |
ebrahimatya | 0:65f054e83dac | 86 | * through the 5 alarm clock menus. |
ebrahimatya | 0:65f054e83dac | 87 | *****************************************************************/ |
ebrahimatya | 0:65f054e83dac | 88 | void change_menu() |
ebrahimatya | 0:65f054e83dac | 89 | { |
ebrahimatya | 0:65f054e83dac | 90 | alarm_snooze(); |
ebrahimatya | 0:65f054e83dac | 91 | menu = (menu + 1) % 5; |
ebrahimatya | 0:65f054e83dac | 92 | print_lcd(); |
ebrahimatya | 0:65f054e83dac | 93 | } |
ebrahimatya | 0:65f054e83dac | 94 | |
ebrahimatya | 0:65f054e83dac | 95 | /****************************************************************** |
ebrahimatya | 0:65f054e83dac | 96 | * Push_plus |
ebrahimatya | 0:65f054e83dac | 97 | * |
ebrahimatya | 0:65f054e83dac | 98 | * This function uses a dedicated pushbutton to only increment or |
ebrahimatya | 0:65f054e83dac | 99 | * "Add" the value of variables of an alarm clock i.e. minute, hour, |
ebrahimatya | 0:65f054e83dac | 100 | * time zone |
ebrahimatya | 0:65f054e83dac | 101 | *****************************************************************/ |
ebrahimatya | 0:65f054e83dac | 102 | void push_plus() |
ebrahimatya | 0:65f054e83dac | 103 | { |
ebrahimatya | 0:65f054e83dac | 104 | alarm_snooze(); |
ebrahimatya | 0:65f054e83dac | 105 | button_press(1); |
ebrahimatya | 0:65f054e83dac | 106 | } |
ebrahimatya | 0:65f054e83dac | 107 | |
ebrahimatya | 0:65f054e83dac | 108 | /****************************************************************** |
ebrahimatya | 0:65f054e83dac | 109 | * Push_minus |
ebrahimatya | 0:65f054e83dac | 110 | * |
ebrahimatya | 0:65f054e83dac | 111 | * This function uses a dedicated pushbutton to only decrement |
ebrahimatya | 0:65f054e83dac | 112 | * or "Subtract" the value of variables of an alarm clock |
ebrahimatya | 0:65f054e83dac | 113 | * i.e. minute, hour, time zone |
ebrahimatya | 0:65f054e83dac | 114 | *****************************************************************/ |
ebrahimatya | 0:65f054e83dac | 115 | void push_minus() |
ebrahimatya | 0:65f054e83dac | 116 | { |
ebrahimatya | 0:65f054e83dac | 117 | alarm_snooze(); |
ebrahimatya | 0:65f054e83dac | 118 | button_press(-1); |
ebrahimatya | 0:65f054e83dac | 119 | } |
ebrahimatya | 0:65f054e83dac | 120 | |
ebrahimatya | 0:65f054e83dac | 121 | /****************************************************************** |
ebrahimatya | 0:65f054e83dac | 122 | * Button_press |
ebrahimatya | 0:65f054e83dac | 123 | * |
ebrahimatya | 0:65f054e83dac | 124 | * This function performs an action based on which menu item is |
ebrahimatya | 0:65f054e83dac | 125 | * currently selected. It is called whenever the "Add" or |
ebrahimatya | 0:65f054e83dac | 126 | * "Subtract" button is pressed. |
ebrahimatya | 0:65f054e83dac | 127 | *****************************************************************/ |
ebrahimatya | 0:65f054e83dac | 128 | void button_press(int input) |
ebrahimatya | 0:65f054e83dac | 129 | { |
ebrahimatya | 0:65f054e83dac | 130 | switch (menu) |
ebrahimatya | 0:65f054e83dac | 131 | { |
ebrahimatya | 0:65f054e83dac | 132 | case 0: |
ebrahimatya | 0:65f054e83dac | 133 | break; |
ebrahimatya | 0:65f054e83dac | 134 | case 1: |
ebrahimatya | 0:65f054e83dac | 135 | timezone += 12; |
ebrahimatya | 0:65f054e83dac | 136 | timezone += input; |
ebrahimatya | 0:65f054e83dac | 137 | while (timezone < 0) |
ebrahimatya | 0:65f054e83dac | 138 | timezone += 24; |
ebrahimatya | 0:65f054e83dac | 139 | timezone %= 24; |
ebrahimatya | 0:65f054e83dac | 140 | timezone -= 12; |
ebrahimatya | 0:65f054e83dac | 141 | break; |
ebrahimatya | 0:65f054e83dac | 142 | case 2: |
ebrahimatya | 0:65f054e83dac | 143 | ahour += input; |
ebrahimatya | 0:65f054e83dac | 144 | while (ahour < 0) |
ebrahimatya | 0:65f054e83dac | 145 | ahour += 24; |
ebrahimatya | 0:65f054e83dac | 146 | ahour %= 24; |
ebrahimatya | 0:65f054e83dac | 147 | break; |
ebrahimatya | 0:65f054e83dac | 148 | case 3: |
ebrahimatya | 0:65f054e83dac | 149 | amin += input; |
ebrahimatya | 0:65f054e83dac | 150 | while (amin < 0) |
ebrahimatya | 0:65f054e83dac | 151 | amin += 60; |
ebrahimatya | 0:65f054e83dac | 152 | amin %= 60; |
ebrahimatya | 0:65f054e83dac | 153 | break; |
ebrahimatya | 0:65f054e83dac | 154 | case 4: |
ebrahimatya | 0:65f054e83dac | 155 | alarmstate = !alarmstate; // Turn alarm on/off |
ebrahimatya | 0:65f054e83dac | 156 | break; |
ebrahimatya | 0:65f054e83dac | 157 | default: |
ebrahimatya | 0:65f054e83dac | 158 | lcd.printf("Invalid state %d\n", menu); |
ebrahimatya | 0:65f054e83dac | 159 | break; |
ebrahimatya | 0:65f054e83dac | 160 | } |
ebrahimatya | 0:65f054e83dac | 161 | print_lcd(); |
ebrahimatya | 0:65f054e83dac | 162 | } |
ebrahimatya | 0:65f054e83dac | 163 | |
ebrahimatya | 0:65f054e83dac | 164 | /****************************************************************** |
ebrahimatya | 0:65f054e83dac | 165 | * Alarm_ring |
ebrahimatya | 0:65f054e83dac | 166 | * |
ebrahimatya | 0:65f054e83dac | 167 | * This function rings the alarm by flashing the leds |
ebrahimatya | 0:65f054e83dac | 168 | *****************************************************************/ |
ebrahimatya | 0:65f054e83dac | 169 | void alarm_ring() |
ebrahimatya | 0:65f054e83dac | 170 | { |
ebrahimatya | 0:65f054e83dac | 171 | if (led == 0x0F) |
ebrahimatya | 0:65f054e83dac | 172 | led = 0; |
ebrahimatya | 0:65f054e83dac | 173 | else |
ebrahimatya | 0:65f054e83dac | 174 | led = (led << 1) | 1; |
ebrahimatya | 0:65f054e83dac | 175 | } |
ebrahimatya | 0:65f054e83dac | 176 | |
ebrahimatya | 0:65f054e83dac | 177 | /****************************************************************** |
ebrahimatya | 0:65f054e83dac | 178 | * Alarm_snooze |
ebrahimatya | 0:65f054e83dac | 179 | * |
ebrahimatya | 0:65f054e83dac | 180 | * This function turns off the alarm |
ebrahimatya | 0:65f054e83dac | 181 | *****************************************************************/ |
ebrahimatya | 0:65f054e83dac | 182 | void alarm_snooze() |
ebrahimatya | 0:65f054e83dac | 183 | { |
ebrahimatya | 0:65f054e83dac | 184 | if (ringflag == true) { |
ebrahimatya | 0:65f054e83dac | 185 | ringflag = false; |
ebrahimatya | 0:65f054e83dac | 186 | snooze = true; |
ebrahimatya | 0:65f054e83dac | 187 | ring.detach(); |
ebrahimatya | 0:65f054e83dac | 188 | led = 0; |
ebrahimatya | 0:65f054e83dac | 189 | } |
ebrahimatya | 0:65f054e83dac | 190 | } |
ebrahimatya | 0:65f054e83dac | 191 | |
ebrahimatya | 0:65f054e83dac | 192 | /****************************************************************** |
ebrahimatya | 0:65f054e83dac | 193 | * Alarm_check |
ebrahimatya | 0:65f054e83dac | 194 | * |
ebrahimatya | 0:65f054e83dac | 195 | * This function compares the alarm time vs the current time. Once |
ebrahimatya | 0:65f054e83dac | 196 | * alarm time and real time match it begins ringing the alarm. |
ebrahimatya | 0:65f054e83dac | 197 | * Once the times differ then it turns off the alarm. |
ebrahimatya | 0:65f054e83dac | 198 | *****************************************************************/ |
ebrahimatya | 0:65f054e83dac | 199 | void alarm_check() |
ebrahimatya | 0:65f054e83dac | 200 | { |
ebrahimatya | 0:65f054e83dac | 201 | if ((cTime->tm_min == amin) && (cTime->tm_hour == ahour)) { |
ebrahimatya | 0:65f054e83dac | 202 | if ((alarmstate == true) && (ringflag == false) && (snooze == false)) { |
ebrahimatya | 0:65f054e83dac | 203 | ringflag = true; |
ebrahimatya | 0:65f054e83dac | 204 | ring.attach(&alarm_ring, .5); // Set up a Ticker for the alarm, |
ebrahimatya | 0:65f054e83dac | 205 | // calls alarm_ring every .5 seconds until stopped |
ebrahimatya | 0:65f054e83dac | 206 | } |
ebrahimatya | 0:65f054e83dac | 207 | } |
ebrahimatya | 0:65f054e83dac | 208 | else { |
ebrahimatya | 0:65f054e83dac | 209 | alarm_snooze(); |
ebrahimatya | 0:65f054e83dac | 210 | snooze = false; |
ebrahimatya | 0:65f054e83dac | 211 | } |
ebrahimatya | 0:65f054e83dac | 212 | } |
ebrahimatya | 0:65f054e83dac | 213 | |
ebrahimatya | 0:65f054e83dac | 214 | int main() { |
ebrahimatya | 0:65f054e83dac | 215 | |
ebrahimatya | 0:65f054e83dac | 216 | /* Set up Ethernet */ |
ebrahimatya | 0:65f054e83dac | 217 | lcd.cls(); |
ebrahimatya | 0:65f054e83dac | 218 | lcd.printf("Setting up Eth\n"); |
ebrahimatya | 0:65f054e83dac | 219 | EthernetErr ethErr = eth.setup(); |
ebrahimatya | 0:65f054e83dac | 220 | if (ethErr) { |
ebrahimatya | 0:65f054e83dac | 221 | lcd.cls(); |
ebrahimatya | 0:65f054e83dac | 222 | lcd.printf("Error with Eth\nNum: %d", ethErr); |
ebrahimatya | 0:65f054e83dac | 223 | return -1; |
ebrahimatya | 0:65f054e83dac | 224 | } |
ebrahimatya | 0:65f054e83dac | 225 | |
ebrahimatya | 0:65f054e83dac | 226 | |
ebrahimatya | 0:65f054e83dac | 227 | /* Set up NTP */ |
ebrahimatya | 0:65f054e83dac | 228 | lcd.printf("Setting up NTP\n"); |
ebrahimatya | 0:65f054e83dac | 229 | Host server(IpAddr(), 123, "0.uk.pool.ntp.org"); |
ebrahimatya | 0:65f054e83dac | 230 | ntp.setTime(server); |
ebrahimatya | 0:65f054e83dac | 231 | |
ebrahimatya | 0:65f054e83dac | 232 | /* Initialize all the interrupts for each of the pushbuttons */ |
ebrahimatya | 0:65f054e83dac | 233 | statechange.rise(&change_menu); |
ebrahimatya | 0:65f054e83dac | 234 | plus.rise(&push_plus); |
ebrahimatya | 0:65f054e83dac | 235 | minus.rise(&push_minus); |
ebrahimatya | 0:65f054e83dac | 236 | |
ebrahimatya | 0:65f054e83dac | 237 | /* Temporary variables for control loop */ |
ebrahimatya | 0:65f054e83dac | 238 | time_t rtc_time = time(NULL); |
ebrahimatya | 0:65f054e83dac | 239 | int minute = -1; |
ebrahimatya | 0:65f054e83dac | 240 | |
ebrahimatya | 0:65f054e83dac | 241 | /* Initialize alarm time */ |
ebrahimatya | 0:65f054e83dac | 242 | ahour = 0; |
ebrahimatya | 0:65f054e83dac | 243 | amin = 0; |
ebrahimatya | 0:65f054e83dac | 244 | |
ebrahimatya | 0:65f054e83dac | 245 | /* Main control loop */ |
ebrahimatya | 0:65f054e83dac | 246 | while(1) |
ebrahimatya | 0:65f054e83dac | 247 | { |
ebrahimatya | 0:65f054e83dac | 248 | /* Update current time */ |
ebrahimatya | 0:65f054e83dac | 249 | rtc_time = time(NULL); |
ebrahimatya | 0:65f054e83dac | 250 | cTime = localtime(&rtc_time); |
ebrahimatya | 0:65f054e83dac | 251 | |
ebrahimatya | 0:65f054e83dac | 252 | /* Only redraw the lcd display if anything changes */ |
ebrahimatya | 0:65f054e83dac | 253 | if (cTime->tm_min != minute) { |
ebrahimatya | 0:65f054e83dac | 254 | minute = cTime->tm_min; |
ebrahimatya | 0:65f054e83dac | 255 | print_lcd(); |
ebrahimatya | 0:65f054e83dac | 256 | |
ebrahimatya | 0:65f054e83dac | 257 | /* Update time from NTP server if it's midnight UTC */ |
ebrahimatya | 0:65f054e83dac | 258 | if ((cTime->tm_min == 0) && (cTime->tm_hour == 0)) { |
ebrahimatya | 0:65f054e83dac | 259 | Host server(IpAddr(), 123, "0.north-america.pool.ntp.org"); |
ebrahimatya | 0:65f054e83dac | 260 | ntp.setTime(server); |
ebrahimatya | 0:65f054e83dac | 261 | } |
ebrahimatya | 0:65f054e83dac | 262 | } |
ebrahimatya | 0:65f054e83dac | 263 | |
ebrahimatya | 0:65f054e83dac | 264 | /* Check to see if the alarm should be started/stopped */ |
ebrahimatya | 0:65f054e83dac | 265 | alarm_check(); |
ebrahimatya | 0:65f054e83dac | 266 | } |
ebrahimatya | 0:65f054e83dac | 267 | return 0; |
ebrahimatya | 0:65f054e83dac | 268 | } |