Group assignment alarm clock

Dependencies:   mbed TextLCD

Committer:
mmlhein
Date:
Fri May 24 07:37:44 2019 +0000
Revision:
13:33ee593b3816
Parent:
12:6b899d5bb944
done

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mmlhein 4:61d3a7c1b411 1 #include "mbed.h"
mmlhein 4:61d3a7c1b411 2 #include "TextLCD.h"
mmlhein 9:b53b3bb0166a 3 #include <string>
mmlhein 6:167533262100 4 using namespace std;
mmlhein 4:61d3a7c1b411 5
mmlhein 11:8695cb20e94c 6 TextLCD lcd(p19, p20, p25, p22, p23, p24); //rs, e, d4-d7
mmlhein 11:8695cb20e94c 7 Ticker blinker_cursor, blinker_numer, blinker_menu, clock_viewer, check_alarm, sound_alarm, just_alarm;
mmlhein 4:61d3a7c1b411 8 InterruptIn joycenter(p14);
mmlhein 4:61d3a7c1b411 9 InterruptIn up(p15);
mmlhein 4:61d3a7c1b411 10 InterruptIn down(p12);
mmlhein 4:61d3a7c1b411 11 InterruptIn left(p13);
mmlhein 4:61d3a7c1b411 12 InterruptIn right(p16);
mmlhein 4:61d3a7c1b411 13 Serial pc(USBTX, USBRX); // tx, rx
mmlhein 9:b53b3bb0166a 14 PwmOut buzzer(p26);
mmlhein 4:61d3a7c1b411 15
mmlhein 6:167533262100 16 int row, col, r_hour, r_min, r_sec, r_apm, epoch, alarm_hour, alarm_min, alarm_sec, alarm_apm;
mmlhein 9:b53b3bb0166a 17 bool state_init, state_menu, state_alarm, alarm_up, state_set, alarm_ring;
mmlhein 6:167533262100 18 string alarm_string, clock_string;
ebrahimatya 0:65f054e83dac 19
mmlhein 9:b53b3bb0166a 20 /*
mmlhein 9:b53b3bb0166a 21 cursor {left,right} movement only work in state_init, state_alarm, state_menu, state_set
mmlhein 9:b53b3bb0166a 22 */
mmlhein 4:61d3a7c1b411 23 void cursor_left()
ebrahimatya 0:65f054e83dac 24 {
mmlhein 4:61d3a7c1b411 25 if(state_init) {
mmlhein 9:b53b3bb0166a 26 if (col >0 && col <13) {
mmlhein 4:61d3a7c1b411 27 if (col == 12)
mmlhein 4:61d3a7c1b411 28 col = 10;
mmlhein 4:61d3a7c1b411 29 else if (col ==10)
mmlhein 4:61d3a7c1b411 30 col = 7;
mmlhein 4:61d3a7c1b411 31 else if (col ==7)
mmlhein 4:61d3a7c1b411 32 col = 4;
mmlhein 4:61d3a7c1b411 33 else if (col == 4) {
mmlhein 4:61d3a7c1b411 34 col =3;
mmlhein 4:61d3a7c1b411 35 row =1;
mmlhein 4:61d3a7c1b411 36 } else if (col ==3 && row == 1) {
mmlhein 4:61d3a7c1b411 37 col = 12;
mmlhein 4:61d3a7c1b411 38 row = 0;
mmlhein 4:61d3a7c1b411 39 } else
mmlhein 4:61d3a7c1b411 40 col = 4;
mmlhein 9:b53b3bb0166a 41 } else
mmlhein 4:61d3a7c1b411 42 col = 4;
mmlhein 9:b53b3bb0166a 43 } else if(state_menu) {
mmlhein 9:b53b3bb0166a 44 if (col >0 && col <13) {
mmlhein 9:b53b3bb0166a 45 if(col == 3) {
hafizul_arif 5:fc218b8312eb 46 col = 9;
mmlhein 9:b53b3bb0166a 47 } else if(col == 9) {
mmlhein 9:b53b3bb0166a 48 col = 3;
mmlhein 9:b53b3bb0166a 49 } else {
mmlhein 9:b53b3bb0166a 50 col=3;
mmlhein 9:b53b3bb0166a 51 row=1;
hafizul_arif 5:fc218b8312eb 52 }
mmlhein 9:b53b3bb0166a 53 } else {
hafizul_arif 5:fc218b8312eb 54 col =3;
hafizul_arif 5:fc218b8312eb 55 row =1;
mmlhein 9:b53b3bb0166a 56 }
mmlhein 9:b53b3bb0166a 57 } else if(state_alarm) {
mmlhein 9:b53b3bb0166a 58 if (col >0 && col <13) {
mmlhein 9:b53b3bb0166a 59 if (col == 12)
mmlhein 9:b53b3bb0166a 60 col = 10;
mmlhein 9:b53b3bb0166a 61 else if (col ==10)
mmlhein 9:b53b3bb0166a 62 col = 7;
mmlhein 9:b53b3bb0166a 63 else if (col ==7)
mmlhein 9:b53b3bb0166a 64 col = 4;
mmlhein 9:b53b3bb0166a 65 else if (col == 4) {
mmlhein 9:b53b3bb0166a 66 col =3;
mmlhein 9:b53b3bb0166a 67 row =1;
mmlhein 9:b53b3bb0166a 68 } else if (col ==3 && row == 1) {
mmlhein 9:b53b3bb0166a 69 col = 9;
mmlhein 9:b53b3bb0166a 70 row = 1;
mmlhein 9:b53b3bb0166a 71 } else if( col == 9 && row ==1) {
mmlhein 9:b53b3bb0166a 72 col = 12;
mmlhein 9:b53b3bb0166a 73 row = 0;
mmlhein 9:b53b3bb0166a 74 } else
mmlhein 9:b53b3bb0166a 75 col = 4;
mmlhein 9:b53b3bb0166a 76 } else {
mmlhein 9:b53b3bb0166a 77 col=3;
mmlhein 9:b53b3bb0166a 78 row=1;
mmlhein 9:b53b3bb0166a 79 }
mmlhein 9:b53b3bb0166a 80 } else if (state_set) {
mmlhein 9:b53b3bb0166a 81 if (col >0 && col <13) {
mmlhein 9:b53b3bb0166a 82 if (col == 12)
mmlhein 9:b53b3bb0166a 83 col = 10;
mmlhein 9:b53b3bb0166a 84 else if (col ==10)
mmlhein 9:b53b3bb0166a 85 col = 7;
mmlhein 9:b53b3bb0166a 86 else if (col ==7)
mmlhein 9:b53b3bb0166a 87 col = 4;
mmlhein 9:b53b3bb0166a 88 else if (col == 4) {
mmlhein 9:b53b3bb0166a 89 col =3;
mmlhein 9:b53b3bb0166a 90 row =1;
mmlhein 9:b53b3bb0166a 91 } else if (col ==3 && row == 1) {
mmlhein 9:b53b3bb0166a 92 col = 9;
mmlhein 9:b53b3bb0166a 93 row = 1;
mmlhein 9:b53b3bb0166a 94 } else if (col ==9 && row==1) {
mmlhein 9:b53b3bb0166a 95 col = 12;
mmlhein 9:b53b3bb0166a 96 row = 0;
mmlhein 9:b53b3bb0166a 97 } else {
mmlhein 9:b53b3bb0166a 98 col = 4;
mmlhein 9:b53b3bb0166a 99 }
mmlhein 9:b53b3bb0166a 100 } else {
hafizul_arif 5:fc218b8312eb 101 col = 9;
hafizul_arif 5:fc218b8312eb 102 row = 1;
mmlhein 9:b53b3bb0166a 103 }
mmlhein 9:b53b3bb0166a 104 }
mmlhein 4:61d3a7c1b411 105 pc.printf("Cursor left: %i,%i\r\n", col, row);
ebrahimatya 0:65f054e83dac 106 }
ebrahimatya 0:65f054e83dac 107
mmlhein 4:61d3a7c1b411 108 void cursor_right()
ebrahimatya 0:65f054e83dac 109 {
mmlhein 4:61d3a7c1b411 110 if(state_init) {
mmlhein 9:b53b3bb0166a 111 if (col >0 && col <13) {
mmlhein 9:b53b3bb0166a 112 if (col == 12) {
mmlhein 4:61d3a7c1b411 113 col =3;
mmlhein 4:61d3a7c1b411 114 row =1;
mmlhein 9:b53b3bb0166a 115 } else if (col ==10)
mmlhein 4:61d3a7c1b411 116 col = 12;
mmlhein 4:61d3a7c1b411 117 else if (col ==7)
mmlhein 4:61d3a7c1b411 118 col = 10;
mmlhein 4:61d3a7c1b411 119 else if (col == 4) {
mmlhein 4:61d3a7c1b411 120 col = 7;
mmlhein 4:61d3a7c1b411 121 } else if (col ==3 && row == 1) {
mmlhein 4:61d3a7c1b411 122 col = 4;
mmlhein 4:61d3a7c1b411 123 row = 0;
mmlhein 4:61d3a7c1b411 124 } else
mmlhein 4:61d3a7c1b411 125 col = 4;
mmlhein 9:b53b3bb0166a 126 } else
mmlhein 4:61d3a7c1b411 127 col = 4;
mmlhein 9:b53b3bb0166a 128 } else if(state_menu) {
mmlhein 9:b53b3bb0166a 129 if (col >0 && col <13) {
mmlhein 9:b53b3bb0166a 130 if(col == 3) {
hafizul_arif 5:fc218b8312eb 131 col = 9;
mmlhein 9:b53b3bb0166a 132 } else if(col == 9) {
mmlhein 9:b53b3bb0166a 133 col = 3;
hafizul_arif 5:fc218b8312eb 134 }
mmlhein 9:b53b3bb0166a 135 } else {
mmlhein 9:b53b3bb0166a 136 col = 3;
mmlhein 9:b53b3bb0166a 137 row =1;
mmlhein 9:b53b3bb0166a 138 }
mmlhein 9:b53b3bb0166a 139 } else if(state_alarm) {
mmlhein 9:b53b3bb0166a 140 if (col >0 && col <13) {
mmlhein 9:b53b3bb0166a 141 if (col == 12) {
hafizul_arif 5:fc218b8312eb 142 col =9;
hafizul_arif 5:fc218b8312eb 143 row =1;
mmlhein 9:b53b3bb0166a 144 } else if (col ==10)
mmlhein 9:b53b3bb0166a 145 col = 12;
mmlhein 9:b53b3bb0166a 146 else if (col ==7)
mmlhein 9:b53b3bb0166a 147 col = 10;
mmlhein 9:b53b3bb0166a 148 else if (col == 4) {
mmlhein 9:b53b3bb0166a 149 col = 7;
mmlhein 9:b53b3bb0166a 150 } else if (col ==3 && row == 1) {
mmlhein 9:b53b3bb0166a 151 col = 4;
mmlhein 9:b53b3bb0166a 152 row = 0;
mmlhein 9:b53b3bb0166a 153 } else if(col == 9 && row == 1) {
mmlhein 9:b53b3bb0166a 154 col = 3;
mmlhein 9:b53b3bb0166a 155 row = 1;
mmlhein 9:b53b3bb0166a 156 } else
mmlhein 9:b53b3bb0166a 157 col = 4;
mmlhein 9:b53b3bb0166a 158 } else {
mmlhein 9:b53b3bb0166a 159 col = 3;
mmlhein 9:b53b3bb0166a 160 row=1;
mmlhein 9:b53b3bb0166a 161 }
mmlhein 9:b53b3bb0166a 162 } else if (state_set) {
mmlhein 9:b53b3bb0166a 163 if (col >0 && col <13) {
mmlhein 9:b53b3bb0166a 164 if (col == 12) {
mmlhein 9:b53b3bb0166a 165 col =9;
mmlhein 9:b53b3bb0166a 166 row =1;
mmlhein 9:b53b3bb0166a 167 } else if (col ==10)
hafizul_arif 5:fc218b8312eb 168 col = 12;
hafizul_arif 5:fc218b8312eb 169 else if (col ==7)
hafizul_arif 5:fc218b8312eb 170 col = 10;
hafizul_arif 5:fc218b8312eb 171 else if (col == 4) {
hafizul_arif 5:fc218b8312eb 172 col = 7;
hafizul_arif 5:fc218b8312eb 173 } else if (col ==3 && row == 1) {
hafizul_arif 5:fc218b8312eb 174 col = 4;
hafizul_arif 5:fc218b8312eb 175 row = 0;
mmlhein 9:b53b3bb0166a 176 } else if (col == 9 && row ==1) {
mmlhein 12:6b899d5bb944 177 col = 3;
hafizul_arif 5:fc218b8312eb 178 row = 1;
mmlhein 9:b53b3bb0166a 179 } else {
hafizul_arif 5:fc218b8312eb 180 col = 4;
mmlhein 9:b53b3bb0166a 181 }
mmlhein 9:b53b3bb0166a 182 } else {
mmlhein 9:b53b3bb0166a 183 col = 9;
mmlhein 9:b53b3bb0166a 184 row = 1;
mmlhein 9:b53b3bb0166a 185 }
mmlhein 9:b53b3bb0166a 186 }
mmlhein 4:61d3a7c1b411 187 pc.printf("Cursor right: %i,%i\r\n", col, row);
ebrahimatya 0:65f054e83dac 188 }
mmlhein 9:b53b3bb0166a 189 /*
mmlhein 9:b53b3bb0166a 190 chg_time_{up,down} only functing at state_init or state_alarm
mmlhein 9:b53b3bb0166a 191 */
mmlhein 9:b53b3bb0166a 192 void chg_time_up()
mmlhein 9:b53b3bb0166a 193 {
mmlhein 12:6b899d5bb944 194 if(state_init or state_set) {
mmlhein 9:b53b3bb0166a 195 if (col ==10) {
mmlhein 4:61d3a7c1b411 196 r_sec = r_sec +1;
mmlhein 4:61d3a7c1b411 197 if (r_sec > 59)
mmlhein 9:b53b3bb0166a 198 r_sec = 0;
mmlhein 9:b53b3bb0166a 199 } else if (col ==7) {
mmlhein 4:61d3a7c1b411 200 r_min = r_min + 1;
mmlhein 4:61d3a7c1b411 201 if (r_min > 59)
mmlhein 9:b53b3bb0166a 202 r_min = 0;
mmlhein 9:b53b3bb0166a 203 } else if (col ==4) {
mmlhein 4:61d3a7c1b411 204 r_hour = r_hour + 1;
mmlhein 4:61d3a7c1b411 205 if (r_hour > 12)
mmlhein 9:b53b3bb0166a 206 r_hour = 1;
mmlhein 9:b53b3bb0166a 207 } else if (col == 12) {
mmlhein 4:61d3a7c1b411 208 if(r_apm==0)
mmlhein 4:61d3a7c1b411 209 r_apm=1;
mmlhein 4:61d3a7c1b411 210 else
mmlhein 4:61d3a7c1b411 211 r_apm=0;
mmlhein 9:b53b3bb0166a 212 } else
mmlhein 4:61d3a7c1b411 213 pc.printf("lmfao");
mmlhein 6:167533262100 214 pc.printf("\nUP: %d:%d:%d %d\r\n", r_hour, r_min, r_sec, r_apm);
mmlhein 9:b53b3bb0166a 215 } else if(state_alarm) {
mmlhein 9:b53b3bb0166a 216 if (col ==10) {
hafizul_arif 5:fc218b8312eb 217 alarm_sec = alarm_sec +1;
hafizul_arif 5:fc218b8312eb 218 if (alarm_sec > 59)
mmlhein 9:b53b3bb0166a 219 alarm_sec = 0;
mmlhein 9:b53b3bb0166a 220 } else if (col ==7) {
hafizul_arif 5:fc218b8312eb 221 alarm_min = alarm_min + 1;
hafizul_arif 5:fc218b8312eb 222 if (alarm_min > 59)
mmlhein 9:b53b3bb0166a 223 alarm_min = 0;
mmlhein 9:b53b3bb0166a 224 } else if (col ==4) {
hafizul_arif 5:fc218b8312eb 225 alarm_hour = alarm_hour + 1;
hafizul_arif 5:fc218b8312eb 226 if (alarm_hour > 12)
mmlhein 9:b53b3bb0166a 227 alarm_hour = 1;
mmlhein 9:b53b3bb0166a 228 } else if (col == 12) {
hafizul_arif 5:fc218b8312eb 229 if(alarm_apm==0)
hafizul_arif 5:fc218b8312eb 230 alarm_apm=1;
hafizul_arif 5:fc218b8312eb 231 else
hafizul_arif 5:fc218b8312eb 232 alarm_apm=0;
mmlhein 9:b53b3bb0166a 233 } else
hafizul_arif 5:fc218b8312eb 234 pc.printf("lmfao");
mmlhein 6:167533262100 235 pc.printf("\nUP: %d:%d:%d %d\r\n", alarm_hour, alarm_min, alarm_sec, alarm_apm);
mmlhein 9:b53b3bb0166a 236 }
mmlhein 9:b53b3bb0166a 237
mmlhein 4:61d3a7c1b411 238 }
mmlhein 9:b53b3bb0166a 239
mmlhein 4:61d3a7c1b411 240 void chg_time_down()
ebrahimatya 0:65f054e83dac 241 {
mmlhein 12:6b899d5bb944 242 if(state_init or state_set) {
mmlhein 9:b53b3bb0166a 243 if (col ==10) {
mmlhein 4:61d3a7c1b411 244 r_sec = r_sec - 1;
mmlhein 4:61d3a7c1b411 245 if (r_sec < 0)
mmlhein 9:b53b3bb0166a 246 r_sec = 59;
mmlhein 9:b53b3bb0166a 247 } else if (col ==7) {
mmlhein 4:61d3a7c1b411 248 r_min = r_min - 1;
mmlhein 4:61d3a7c1b411 249 if (r_min < 0)
mmlhein 9:b53b3bb0166a 250 r_min = 59;
mmlhein 9:b53b3bb0166a 251 } else if (col ==4) {
mmlhein 4:61d3a7c1b411 252 r_hour = r_hour - 1;
mmlhein 4:61d3a7c1b411 253 if (r_hour < 1)
mmlhein 9:b53b3bb0166a 254 r_hour = 12;
mmlhein 9:b53b3bb0166a 255 } else if (col == 12) {
mmlhein 4:61d3a7c1b411 256 if(r_apm==0)
mmlhein 4:61d3a7c1b411 257 r_apm=1;
mmlhein 4:61d3a7c1b411 258 else
mmlhein 4:61d3a7c1b411 259 r_apm=0;
mmlhein 9:b53b3bb0166a 260 } else
mmlhein 4:61d3a7c1b411 261 pc.printf("lmfao");
mmlhein 9:b53b3bb0166a 262 pc.printf("\nDown clock: %d:%d:%d %d\r\n", r_hour, r_min, r_sec, r_apm);
mmlhein 9:b53b3bb0166a 263 } else if(state_alarm) {
mmlhein 9:b53b3bb0166a 264 if (col ==10) {
hafizul_arif 5:fc218b8312eb 265 alarm_sec = alarm_sec - 1;
hafizul_arif 5:fc218b8312eb 266 if (alarm_sec < 0)
mmlhein 9:b53b3bb0166a 267 alarm_sec = 59;
mmlhein 9:b53b3bb0166a 268 } else if (col ==7) {
hafizul_arif 5:fc218b8312eb 269 alarm_min = alarm_min - 1;
hafizul_arif 5:fc218b8312eb 270 if (alarm_min < 0)
mmlhein 9:b53b3bb0166a 271 alarm_min = 59;
mmlhein 9:b53b3bb0166a 272 } else if (col ==4) {
hafizul_arif 5:fc218b8312eb 273 alarm_hour = alarm_hour - 1;
hafizul_arif 5:fc218b8312eb 274 if (alarm_hour < 1)
mmlhein 9:b53b3bb0166a 275 alarm_hour = 12;
mmlhein 9:b53b3bb0166a 276 } else if (col == 12) {
hafizul_arif 5:fc218b8312eb 277 if(alarm_apm==0)
hafizul_arif 5:fc218b8312eb 278 alarm_apm=1;
hafizul_arif 5:fc218b8312eb 279 else
hafizul_arif 5:fc218b8312eb 280 alarm_apm=0;
mmlhein 9:b53b3bb0166a 281 } else
hafizul_arif 5:fc218b8312eb 282 pc.printf("lmfao");
mmlhein 9:b53b3bb0166a 283 pc.printf("\nDown alarm: %d:%d:%d %d\r\n", alarm_hour, alarm_min, alarm_sec, alarm_apm);
mmlhein 9:b53b3bb0166a 284 }
ebrahimatya 0:65f054e83dac 285 }
mmlhein 9:b53b3bb0166a 286 /*
mmlhein 9:b53b3bb0166a 287 Display sections.
mmlhein 9:b53b3bb0166a 288 */
mmlhein 9:b53b3bb0166a 289 void show_menu()
mmlhein 9:b53b3bb0166a 290 {
mmlhein 9:b53b3bb0166a 291 if(alarm_up) {
mmlhein 6:167533262100 292 lcd.locate(2,1);
mmlhein 6:167533262100 293 lcd.printf("*");
mmlhein 9:b53b3bb0166a 294 } else {
mmlhein 6:167533262100 295 lcd.locate(2,1);
mmlhein 6:167533262100 296 lcd.printf(" ");
mmlhein 6:167533262100 297 }
mmlhein 6:167533262100 298 lcd.locate(3,1);
mmlhein 6:167533262100 299 lcd.printf("Alarm");
mmlhein 6:167533262100 300 lcd.locate(9,1);
mmlhein 6:167533262100 301 lcd.printf("Set");
mmlhein 6:167533262100 302 }
mmlhein 9:b53b3bb0166a 303 void view_clock()
mmlhein 9:b53b3bb0166a 304 {
mmlhein 9:b53b3bb0166a 305 lcd.locate(3,0);
mmlhein 9:b53b3bb0166a 306 time_t seconds = time(NULL);
mmlhein 9:b53b3bb0166a 307 char buffer[32];
mmlhein 9:b53b3bb0166a 308 strftime(buffer, 32, "%I:%M:%S %p", localtime(&seconds));
mmlhein 9:b53b3bb0166a 309 lcd.printf("%s", buffer);
mmlhein 9:b53b3bb0166a 310
mmlhein 9:b53b3bb0166a 311 //Print main Menu
mmlhein 9:b53b3bb0166a 312 show_menu();
mmlhein 9:b53b3bb0166a 313 // Update our clock string.
mmlhein 9:b53b3bb0166a 314 clock_string = buffer;
mmlhein 8:c8421fc56e8f 315 }
mmlhein 9:b53b3bb0166a 316 void blink_cursor()
mmlhein 9:b53b3bb0166a 317 {
mmlhein 6:167533262100 318 lcd.locate(col, row);
mmlhein 6:167533262100 319 lcd.printf("_");
mmlhein 9:b53b3bb0166a 320 }
mmlhein 6:167533262100 321 void blink_num()
mmlhein 6:167533262100 322 {
mmlhein 9:b53b3bb0166a 323
mmlhein 9:b53b3bb0166a 324 if (r_hour > 9) {
mmlhein 6:167533262100 325 lcd.locate(3,0);
mmlhein 6:167533262100 326 lcd.printf("%d:", r_hour);
mmlhein 9:b53b3bb0166a 327 } else {
mmlhein 6:167533262100 328 lcd.locate(3,0);
mmlhein 6:167533262100 329 lcd.printf("0%d:", r_hour);
mmlhein 6:167533262100 330 }
mmlhein 9:b53b3bb0166a 331 if (r_min > 9) {
mmlhein 6:167533262100 332 lcd.locate(6,0);
mmlhein 6:167533262100 333 lcd.printf("%d:", r_min);
mmlhein 9:b53b3bb0166a 334 } else {
mmlhein 6:167533262100 335 lcd.locate(6,0);
mmlhein 6:167533262100 336 lcd.printf("0%d:", r_min);
mmlhein 6:167533262100 337 }
mmlhein 9:b53b3bb0166a 338 if (r_sec > 9) {
mmlhein 6:167533262100 339 lcd.locate(9,0);
mmlhein 6:167533262100 340 lcd.printf("%d", r_sec);
mmlhein 9:b53b3bb0166a 341 } else {
mmlhein 6:167533262100 342 lcd.locate(9,0);
mmlhein 6:167533262100 343 lcd.printf("0%d", r_sec);
mmlhein 6:167533262100 344 }
mmlhein 6:167533262100 345 lcd.locate(12,0);
mmlhein 6:167533262100 346 if (r_apm ==0)
mmlhein 6:167533262100 347 lcd.printf("AM");
mmlhein 6:167533262100 348 else
mmlhein 6:167533262100 349 lcd.printf("PM");
mmlhein 6:167533262100 350 lcd.locate(3,1);
mmlhein 6:167533262100 351 lcd.printf("Done");
mmlhein 9:b53b3bb0166a 352
mmlhein 9:b53b3bb0166a 353 if (state_set) { // Print "Back" if this is state_set
mmlhein 9:b53b3bb0166a 354 lcd.locate(9,1);
mmlhein 9:b53b3bb0166a 355 lcd.printf("Back");
mmlhein 9:b53b3bb0166a 356 }
mmlhein 6:167533262100 357 return;
mmlhein 6:167533262100 358 }
hafizul_arif 5:fc218b8312eb 359
mmlhein 9:b53b3bb0166a 360 void alarm_disp()
mmlhein 9:b53b3bb0166a 361 {
mmlhein 9:b53b3bb0166a 362 lcd.cls();
mmlhein 9:b53b3bb0166a 363 if (alarm_hour > 9) {
hafizul_arif 5:fc218b8312eb 364 lcd.locate(3,0);
hafizul_arif 5:fc218b8312eb 365 lcd.printf("%d:", alarm_hour);
mmlhein 9:b53b3bb0166a 366 } else {
hafizul_arif 5:fc218b8312eb 367 lcd.locate(3,0);
hafizul_arif 5:fc218b8312eb 368 lcd.printf("0%d:", alarm_hour);
hafizul_arif 5:fc218b8312eb 369 }
mmlhein 9:b53b3bb0166a 370 if (alarm_min > 9) {
hafizul_arif 5:fc218b8312eb 371 lcd.locate(6,0);
hafizul_arif 5:fc218b8312eb 372 lcd.printf("%d:", alarm_min);
mmlhein 9:b53b3bb0166a 373 } else {
hafizul_arif 5:fc218b8312eb 374 lcd.locate(6,0);
hafizul_arif 5:fc218b8312eb 375 lcd.printf("0%d:", alarm_min);
hafizul_arif 5:fc218b8312eb 376 }
mmlhein 9:b53b3bb0166a 377 if (alarm_sec > 9) {
hafizul_arif 5:fc218b8312eb 378 lcd.locate(9,0);
hafizul_arif 5:fc218b8312eb 379 lcd.printf("%d", alarm_sec);
mmlhein 9:b53b3bb0166a 380 } else {
hafizul_arif 5:fc218b8312eb 381 lcd.locate(9,0);
hafizul_arif 5:fc218b8312eb 382 lcd.printf("0%d", alarm_sec);
hafizul_arif 5:fc218b8312eb 383 }
hafizul_arif 5:fc218b8312eb 384 lcd.locate(12,0);
hafizul_arif 5:fc218b8312eb 385 if (alarm_apm ==0)
hafizul_arif 5:fc218b8312eb 386 lcd.printf("AM");
hafizul_arif 5:fc218b8312eb 387 else
hafizul_arif 5:fc218b8312eb 388 lcd.printf("PM");
hafizul_arif 5:fc218b8312eb 389 lcd.locate(3,1);
hafizul_arif 5:fc218b8312eb 390 lcd.printf("Off");
hafizul_arif 5:fc218b8312eb 391 lcd.locate(9,1);
hafizul_arif 5:fc218b8312eb 392 lcd.printf("On");
hafizul_arif 5:fc218b8312eb 393 return;
hafizul_arif 5:fc218b8312eb 394 }
mmlhein 9:b53b3bb0166a 395
mmlhein 9:b53b3bb0166a 396 void alarm_sound()
mmlhein 9:b53b3bb0166a 397 {
mmlhein 9:b53b3bb0166a 398 float frequency[]= {659,554,659,554,440,494,554,587,494,659,554,440}; //frequency array
mmlhein 9:b53b3bb0166a 399 float beat[]= {1,1,1,1,1,0.5,0.5,1,1,1,1,2}; //beat array
mmlhein 9:b53b3bb0166a 400 lcd.cls();
mmlhein 9:b53b3bb0166a 401 lcd.printf("Press button to off alarm");
mmlhein 9:b53b3bb0166a 402 while(1) {
mmlhein 9:b53b3bb0166a 403 for(int i=0; i<=11; i++) {
mmlhein 9:b53b3bb0166a 404 buzzer.period(1/(2*frequency[i]));// set PWM period
mmlhein 9:b53b3bb0166a 405 buzzer=0.8;// set duty cycle
mmlhein 9:b53b3bb0166a 406 wait(0.4*beat[i]);// hold for beat period    
mmlhein 9:b53b3bb0166a 407 }
mmlhein 9:b53b3bb0166a 408 if(joycenter) { //Off the alarm
mmlhein 9:b53b3bb0166a 409 buzzer.period(0);
mmlhein 9:b53b3bb0166a 410 buzzer=0;
mmlhein 9:b53b3bb0166a 411 col=0;
mmlhein 9:b53b3bb0166a 412 row=0;
mmlhein 9:b53b3bb0166a 413 lcd.cls();
mmlhein 9:b53b3bb0166a 414 break;
mmlhein 9:b53b3bb0166a 415 }
mmlhein 9:b53b3bb0166a 416 }
mmlhein 9:b53b3bb0166a 417 }
mmlhein 9:b53b3bb0166a 418
mmlhein 9:b53b3bb0166a 419
mmlhein 9:b53b3bb0166a 420 //Function alarm_chk is in progress
mmlhein 9:b53b3bb0166a 421 void alarm_chk()
mmlhein 9:b53b3bb0166a 422 {
mmlhein 9:b53b3bb0166a 423 //we basically, comparing 2 strings. alarm string and the clock strings if match we ring it
mmlhein 9:b53b3bb0166a 424 //we want to disable cursor movement.
mmlhein 9:b53b3bb0166a 425 if(clock_string == alarm_string) {
mmlhein 9:b53b3bb0166a 426 pc.printf("%s %s : same\r\n", alarm_string, clock_string);
mmlhein 9:b53b3bb0166a 427 //Disable cursor movement.
mmlhein 9:b53b3bb0166a 428 state_menu = false;
mmlhein 9:b53b3bb0166a 429 state_set = false;
mmlhein 9:b53b3bb0166a 430 state_alarm = false;
mmlhein 9:b53b3bb0166a 431 blinker_cursor.detach();
mmlhein 9:b53b3bb0166a 432 alarm_sound();
mmlhein 9:b53b3bb0166a 433 }
mmlhein 9:b53b3bb0166a 434 blinker_cursor.attach(&blink_cursor, 0.1);
mmlhein 9:b53b3bb0166a 435 }
mmlhein 9:b53b3bb0166a 436
mmlhein 8:c8421fc56e8f 437 void __set_alarm_string()
mmlhein 8:c8421fc56e8f 438 {
mmlhein 8:c8421fc56e8f 439 char buffer_alarm[32];
mmlhein 8:c8421fc56e8f 440 alarm_string.clear();
mmlhein 8:c8421fc56e8f 441 if(alarm_hour <9) {
mmlhein 8:c8421fc56e8f 442 sprintf (buffer_alarm, "0%d:", alarm_hour);
mmlhein 8:c8421fc56e8f 443 } else {
mmlhein 8:c8421fc56e8f 444 sprintf (buffer_alarm, "%d:", alarm_hour);
mmlhein 8:c8421fc56e8f 445 }
mmlhein 8:c8421fc56e8f 446 alarm_string += buffer_alarm;
mmlhein 8:c8421fc56e8f 447 if (alarm_min <9) {
mmlhein 8:c8421fc56e8f 448 sprintf (buffer_alarm, "0%d:", alarm_min);
mmlhein 8:c8421fc56e8f 449 } else {
mmlhein 8:c8421fc56e8f 450 sprintf (buffer_alarm, "%d:", alarm_min);
mmlhein 8:c8421fc56e8f 451 }
mmlhein 8:c8421fc56e8f 452 alarm_string += buffer_alarm;
mmlhein 8:c8421fc56e8f 453 if (alarm_sec<9) {
mmlhein 8:c8421fc56e8f 454 sprintf (buffer_alarm, "0%d", alarm_sec);
mmlhein 8:c8421fc56e8f 455 } else {
mmlhein 8:c8421fc56e8f 456 sprintf (buffer_alarm, "%d", alarm_sec);
mmlhein 8:c8421fc56e8f 457 }
mmlhein 8:c8421fc56e8f 458 alarm_string += buffer_alarm;
mmlhein 8:c8421fc56e8f 459 if(alarm_apm ==0) {
mmlhein 8:c8421fc56e8f 460 sprintf (buffer_alarm, " %s", "AM");
mmlhein 8:c8421fc56e8f 461 } else {
mmlhein 8:c8421fc56e8f 462 sprintf (buffer_alarm, " %s", "PM");
mmlhein 8:c8421fc56e8f 463 }
mmlhein 8:c8421fc56e8f 464 alarm_string += buffer_alarm;
mmlhein 8:c8421fc56e8f 465 }
mmlhein 9:b53b3bb0166a 466
mmlhein 9:b53b3bb0166a 467 /*
mmlhein 9:b53b3bb0166a 468 state section.
mmlhein 9:b53b3bb0166a 469 */
mmlhein 8:c8421fc56e8f 470 void setup_alarm()
mmlhein 8:c8421fc56e8f 471 {
hafizul_arif 5:fc218b8312eb 472 state_alarm = true;
hafizul_arif 5:fc218b8312eb 473 state_menu = false;
mmlhein 11:8695cb20e94c 474 just_alarm.attach(&alarm_disp,0.5);
mmlhein 8:c8421fc56e8f 475 while(1) {
hafizul_arif 5:fc218b8312eb 476 wait(1);
mmlhein 8:c8421fc56e8f 477 if(col == 3 && row ==1) {
mmlhein 8:c8421fc56e8f 478 if(joycenter) { //Turn off Alarm and back to main menu
mmlhein 11:8695cb20e94c 479 just_alarm.detach();//stop alarm_disp
hafizul_arif 5:fc218b8312eb 480 state_alarm = false;
hafizul_arif 5:fc218b8312eb 481 state_menu = true;
hafizul_arif 5:fc218b8312eb 482 alarm_up = false;
hafizul_arif 5:fc218b8312eb 483 check_alarm.detach();
mmlhein 8:c8421fc56e8f 484 alarm_string.clear();
mmlhein 9:b53b3bb0166a 485 col=0;
mmlhein 9:b53b3bb0166a 486 row=0;
hafizul_arif 5:fc218b8312eb 487 break;
hafizul_arif 5:fc218b8312eb 488 }
mmlhein 8:c8421fc56e8f 489 } else if(col == 9 && row == 1) {//Turn on Alarm and back to main menu
mmlhein 8:c8421fc56e8f 490 if(joycenter) {
mmlhein 11:8695cb20e94c 491 just_alarm.detach();
hafizul_arif 5:fc218b8312eb 492 state_alarm = false;
hafizul_arif 5:fc218b8312eb 493 state_menu = true;
mmlhein 9:b53b3bb0166a 494 alarm_up = true;
mmlhein 9:b53b3bb0166a 495 // Turn on the alarm.
hafizul_arif 5:fc218b8312eb 496 check_alarm.attach(&alarm_chk,1);
mmlhein 9:b53b3bb0166a 497 // Set alarm string
mmlhein 8:c8421fc56e8f 498 __set_alarm_string();
mmlhein 9:b53b3bb0166a 499 col=0;
mmlhein 9:b53b3bb0166a 500 row=0;
hafizul_arif 5:fc218b8312eb 501 break;
hafizul_arif 5:fc218b8312eb 502 }
hafizul_arif 5:fc218b8312eb 503 }
mmlhein 9:b53b3bb0166a 504 state_alarm = true;
mmlhein 9:b53b3bb0166a 505 state_menu = false;
hafizul_arif 5:fc218b8312eb 506 }
hafizul_arif 5:fc218b8312eb 507 }
hafizul_arif 5:fc218b8312eb 508
mmlhein 9:b53b3bb0166a 509 void setup_init()
mmlhein 9:b53b3bb0166a 510 {
mmlhein 12:6b899d5bb944 511 col=0;
mmlhein 12:6b899d5bb944 512 row=0;
mmlhein 10:2a47ac757d14 513 if (state_set) {
mmlhein 10:2a47ac757d14 514 state_init = false;
mmlhein 10:2a47ac757d14 515 state_menu = false;
mmlhein 10:2a47ac757d14 516 } else {
mmlhein 10:2a47ac757d14 517 state_init = true;
mmlhein 10:2a47ac757d14 518 state_menu = false;
mmlhein 10:2a47ac757d14 519 }
mmlhein 10:2a47ac757d14 520
mmlhein 8:c8421fc56e8f 521 lcd.cls();
mmlhein 4:61d3a7c1b411 522 blinker_cursor.attach(&blink_cursor, 0.1);
mmlhein 4:61d3a7c1b411 523 blinker_numer.attach(&blink_num, 0.5);
mmlhein 9:b53b3bb0166a 524 while (1) {
mmlhein 12:6b899d5bb944 525 wait(1);
mmlhein 4:61d3a7c1b411 526 // This is where, we select "Done", and we press joycenter to end the initial setup.
mmlhein 9:b53b3bb0166a 527 if(col==3 && row==1) {
mmlhein 9:b53b3bb0166a 528 if(joycenter) {
mmlhein 9:b53b3bb0166a 529 blinker_numer.detach(); // Stop viewing number.
mmlhein 9:b53b3bb0166a 530 blinker_cursor.detach(); // Stop blinking the cursor.
mmlhein 9:b53b3bb0166a 531
mmlhein 4:61d3a7c1b411 532 //Calculate the unix timestamp
mmlhein 4:61d3a7c1b411 533 //1h : 3600, 1m: 60, 1s: 1
mmlhein 9:b53b3bb0166a 534 if(r_apm==0) { // AM
mmlhein 4:61d3a7c1b411 535 if(r_hour ==12)
mmlhein 4:61d3a7c1b411 536 epoch = (0 *3600) + (r_min *60) +(r_sec);
mmlhein 4:61d3a7c1b411 537 else
mmlhein 4:61d3a7c1b411 538 epoch = (r_hour *3600) + (r_min *60) +(r_sec);
mmlhein 9:b53b3bb0166a 539 } else if (r_apm ==1) { //PM
mmlhein 4:61d3a7c1b411 540 if(r_hour ==12)
mmlhein 4:61d3a7c1b411 541 epoch = (r_hour *3600) + (r_min *60) +(r_sec);
mmlhein 4:61d3a7c1b411 542 else
mmlhein 4:61d3a7c1b411 543 epoch = 43200+(r_hour *3600) + (r_min *60) +(r_sec);
mmlhein 9:b53b3bb0166a 544 }
mmlhein 9:b53b3bb0166a 545
mmlhein 8:c8421fc56e8f 546 pc.printf("%d\r\n", epoch);
mmlhein 9:b53b3bb0166a 547 // Set time base on epoch value.
mmlhein 8:c8421fc56e8f 548 set_time(epoch);
mmlhein 9:b53b3bb0166a 549
mmlhein 9:b53b3bb0166a 550 // When setup_init() done, we move the cursor to default state,
mmlhein 9:b53b3bb0166a 551 row=0;
mmlhein 9:b53b3bb0166a 552 col=0;
mmlhein 9:b53b3bb0166a 553
mmlhein 4:61d3a7c1b411 554 state_init = false;
mmlhein 9:b53b3bb0166a 555 state_menu = true;
mmlhein 10:2a47ac757d14 556 state_set = false;
mmlhein 9:b53b3bb0166a 557 break;
mmlhein 4:61d3a7c1b411 558 }
mmlhein 10:2a47ac757d14 559 } else if (col ==9 && row ==1) {//back button is pressed
mmlhein 13:33ee593b3816 560 if(joycenter) { // When setup_init() done, we move the cursor to default state,
mmlhein 12:6b899d5bb944 561 blinker_numer.detach(); // Stop viewing number.
mmlhein 12:6b899d5bb944 562 blinker_cursor.detach(); // Stop blinking the cursor.
mmlhein 12:6b899d5bb944 563 row=0;
mmlhein 12:6b899d5bb944 564 col=0;
mmlhein 12:6b899d5bb944 565 state_init =false;
mmlhein 12:6b899d5bb944 566 state_menu = true;
mmlhein 12:6b899d5bb944 567 state_set = false;
mmlhein 12:6b899d5bb944 568 break;
mmlhein 12:6b899d5bb944 569 }
mmlhein 9:b53b3bb0166a 570 }
ebrahimatya 0:65f054e83dac 571 }
ebrahimatya 0:65f054e83dac 572 }
mmlhein 9:b53b3bb0166a 573
mmlhein 9:b53b3bb0166a 574 /*
mmlhein 9:b53b3bb0166a 575 Always back to main_menu state.
mmlhein 9:b53b3bb0166a 576 */
mmlhein 9:b53b3bb0166a 577 void main_menu()
mmlhein 4:61d3a7c1b411 578 {
mmlhein 9:b53b3bb0166a 579 state_menu = true;
mmlhein 9:b53b3bb0166a 580 // Start viewing clock!
mmlhein 9:b53b3bb0166a 581 clock_viewer.attach(&view_clock,1);
mmlhein 9:b53b3bb0166a 582 blinker_cursor.attach(&blink_cursor, 0.1);
mmlhein 9:b53b3bb0166a 583 lcd.cls();
ebrahimatya 0:65f054e83dac 584
mmlhein 9:b53b3bb0166a 585 while(1) {
hafizul_arif 5:fc218b8312eb 586 wait(1);
mmlhein 9:b53b3bb0166a 587 if(col == 3 && row == 1) {
mmlhein 9:b53b3bb0166a 588 if(joycenter) {
hafizul_arif 5:fc218b8312eb 589 clock_viewer.detach();
mmlhein 6:167533262100 590 setup_alarm();
hafizul_arif 5:fc218b8312eb 591 clock_viewer.attach(&view_clock,1);
hafizul_arif 5:fc218b8312eb 592 }
mmlhein 9:b53b3bb0166a 593 } else if(col == 9 && row ==1) {
mmlhein 9:b53b3bb0166a 594 if(joycenter) {
mmlhein 8:c8421fc56e8f 595 clock_viewer.detach();
mmlhein 9:b53b3bb0166a 596 state_set = true;
mmlhein 8:c8421fc56e8f 597 setup_init();
mmlhein 12:6b899d5bb944 598 blinker_cursor.attach(&blink_cursor, 0.1);
mmlhein 12:6b899d5bb944 599 lcd.cls();
mmlhein 8:c8421fc56e8f 600 clock_viewer.attach(&view_clock,1);
hafizul_arif 5:fc218b8312eb 601 }
hafizul_arif 5:fc218b8312eb 602 }
mmlhein 9:b53b3bb0166a 603 state_menu = true;
hafizul_arif 5:fc218b8312eb 604 }
hafizul_arif 5:fc218b8312eb 605 }
hafizul_arif 5:fc218b8312eb 606
mmlhein 9:b53b3bb0166a 607 /*
mmlhein 9:b53b3bb0166a 608 main function, duh.
mmlhein 9:b53b3bb0166a 609 */
mmlhein 9:b53b3bb0166a 610 int main()
mmlhein 9:b53b3bb0166a 611 {
mmlhein 11:8695cb20e94c 612 lcd.locate(0,0);
mmlhein 11:8695cb20e94c 613 lcd.printf("Press button to start");
mmlhein 13:33ee593b3816 614 while(1) {
mmlhein 13:33ee593b3816 615 if(joycenter) {
mmlhein 11:8695cb20e94c 616 break;
mmlhein 11:8695cb20e94c 617 }
mmlhein 11:8695cb20e94c 618 }
mmlhein 9:b53b3bb0166a 619 // Initial value for our cursor
mmlhein 4:61d3a7c1b411 620 row =0 ;
mmlhein 4:61d3a7c1b411 621 col=0 ;
mmlhein 9:b53b3bb0166a 622
mmlhein 9:b53b3bb0166a 623 // Initial value for alarm
mmlhein 6:167533262100 624 alarm_hour=12;
mmlhein 6:167533262100 625 alarm_min=0;
mmlhein 9:b53b3bb0166a 626 alarm_sec=0;
mmlhein 6:167533262100 627 alarm_apm=0;
mmlhein 9:b53b3bb0166a 628 // Initial value for clock
mmlhein 4:61d3a7c1b411 629 r_hour=12 ;
mmlhein 4:61d3a7c1b411 630 r_min=0 ;
mmlhein 4:61d3a7c1b411 631 r_sec = 0;
mmlhein 4:61d3a7c1b411 632 r_apm = 0; //0-AM, 1-PM
mmlhein 4:61d3a7c1b411 633 epoch = 0; // 12:00:00 AM
mmlhein 9:b53b3bb0166a 634
mmlhein 9:b53b3bb0166a 635 // Set joystick to interrupt and call specific function the cursor value.
mmlhein 6:167533262100 636 left.rise(&cursor_left);
mmlhein 6:167533262100 637 right.rise(&cursor_right);
mmlhein 6:167533262100 638 up.rise(&chg_time_up);
mmlhein 6:167533262100 639 down.rise(&chg_time_down);
mmlhein 9:b53b3bb0166a 640
mmlhein 9:b53b3bb0166a 641 // Initialize all the state to false.
mmlhein 9:b53b3bb0166a 642 state_init = false;
mmlhein 9:b53b3bb0166a 643 state_menu = false;
mmlhein 9:b53b3bb0166a 644 state_alarm = false;
mmlhein 9:b53b3bb0166a 645 alarm_up = false;
mmlhein 9:b53b3bb0166a 646 state_set = false;
mmlhein 9:b53b3bb0166a 647 alarm_ring = false;
mmlhein 9:b53b3bb0166a 648
mmlhein 6:167533262100 649 /*Run setup init -> show main menu
mmlhein 9:b53b3bb0166a 650 |_ setup Alarm (state_alarm)
mmlhein 6:167533262100 651 | |_Off alarm
mmlhein 6:167533262100 652 | |_On alarm
mmlhein 9:b53b3bb0166a 653 |_ Change time (state_init)
mmlhein 9:b53b3bb0166a 654
mmlhein 9:b53b3bb0166a 655 The up/down only work in state_init and state_alarm
mmlhein 9:b53b3bb0166a 656 The left/right should work on every state except in state which alarm is ringging.
mmlhein 9:b53b3bb0166a 657
mmlhein 9:b53b3bb0166a 658 Every time we choose the option in any state, the default value for cursor is
mmlhein 9:b53b3bb0166a 659 col=0 and row=0. This to avoid accidently go to another state.
mmlhein 6:167533262100 660 */
mmlhein 11:8695cb20e94c 661
mmlhein 4:61d3a7c1b411 662 setup_init();
hafizul_arif 5:fc218b8312eb 663 main_menu();
mmlhein 9:b53b3bb0166a 664 }