Group assignment alarm clock

Dependencies:   mbed TextLCD

Committer:
mmlhein
Date:
Thu May 23 05:52:25 2019 +0000
Revision:
8:c8421fc56e8f
Parent:
7:b9d3bc940d2c
Child:
9:b53b3bb0166a
complete system, but required bit adjustment.

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 6:167533262100 3 #include <string>
mmlhein 6:167533262100 4 using namespace std;
mmlhein 4:61d3a7c1b411 5
mmlhein 8:c8421fc56e8f 6 TextLCD lcd(p19, p20, p25, p22, p23, p24);
mmlhein 8:c8421fc56e8f 7 Ticker blinker_cursor, blinker_numer, blinker_menu, clock_viewer, check_alarm, sound_alarm;
mmlhein 4:61d3a7c1b411 8 // Initially we set joystick to intterupt.
mmlhein 4:61d3a7c1b411 9 InterruptIn joycenter(p14);
mmlhein 4:61d3a7c1b411 10 InterruptIn up(p15);
mmlhein 4:61d3a7c1b411 11 InterruptIn down(p12);
mmlhein 4:61d3a7c1b411 12 InterruptIn left(p13);
mmlhein 4:61d3a7c1b411 13 InterruptIn right(p16);
mmlhein 4:61d3a7c1b411 14 Serial pc(USBTX, USBRX); // tx, rx
mmlhein 8:c8421fc56e8f 15 PwmOut buzzer(p21);
mmlhein 4:61d3a7c1b411 16
mmlhein 6:167533262100 17 int row, col, r_hour, r_min, r_sec, r_apm, epoch, alarm_hour, alarm_min, alarm_sec, alarm_apm;
hafizul_arif 5:fc218b8312eb 18 bool state_init, state_menu, state_alarm, alarm_up;
mmlhein 6:167533262100 19 string alarm_string, clock_string;
ebrahimatya 0:65f054e83dac 20
ebrahimatya 0:65f054e83dac 21
mmlhein 4:61d3a7c1b411 22 void cursor_left()
ebrahimatya 0:65f054e83dac 23 {
mmlhein 4:61d3a7c1b411 24 if(state_init) {
mmlhein 4:61d3a7c1b411 25 if (col >0 && col <13)
mmlhein 4:61d3a7c1b411 26 if (col == 12)
mmlhein 4:61d3a7c1b411 27 col = 10;
mmlhein 4:61d3a7c1b411 28 else if (col ==10)
mmlhein 4:61d3a7c1b411 29 col = 7;
mmlhein 4:61d3a7c1b411 30 else if (col ==7)
mmlhein 4:61d3a7c1b411 31 col = 4;
mmlhein 4:61d3a7c1b411 32 else if (col == 4) {
mmlhein 4:61d3a7c1b411 33 col =3;
mmlhein 4:61d3a7c1b411 34 row =1;
mmlhein 4:61d3a7c1b411 35 } else if (col ==3 && row == 1) {
mmlhein 4:61d3a7c1b411 36 col = 12;
mmlhein 4:61d3a7c1b411 37 row = 0;
mmlhein 4:61d3a7c1b411 38 } else
mmlhein 4:61d3a7c1b411 39 col = 4;
mmlhein 4:61d3a7c1b411 40 else
mmlhein 4:61d3a7c1b411 41 col = 4;
hafizul_arif 5:fc218b8312eb 42 } else if(state_menu){
hafizul_arif 5:fc218b8312eb 43
hafizul_arif 5:fc218b8312eb 44 if(col == 3){
hafizul_arif 5:fc218b8312eb 45 col = 9;
hafizul_arif 5:fc218b8312eb 46 } else if(col == 9){
hafizul_arif 5:fc218b8312eb 47 col = 3;
hafizul_arif 5:fc218b8312eb 48 }
hafizul_arif 5:fc218b8312eb 49 } else if(state_alarm){
hafizul_arif 5:fc218b8312eb 50 if (col == 12)
hafizul_arif 5:fc218b8312eb 51 col = 10;
hafizul_arif 5:fc218b8312eb 52 else if (col ==10)
hafizul_arif 5:fc218b8312eb 53 col = 7;
hafizul_arif 5:fc218b8312eb 54 else if (col ==7)
hafizul_arif 5:fc218b8312eb 55 col = 4;
hafizul_arif 5:fc218b8312eb 56 else if (col == 4) {
hafizul_arif 5:fc218b8312eb 57 col =3;
hafizul_arif 5:fc218b8312eb 58 row =1;
hafizul_arif 5:fc218b8312eb 59 } else if (col ==3 && row == 1) {
hafizul_arif 5:fc218b8312eb 60 col = 9;
hafizul_arif 5:fc218b8312eb 61 row = 1;
hafizul_arif 5:fc218b8312eb 62 } else if( col == 9 && row ==1) {
hafizul_arif 5:fc218b8312eb 63 col = 12;
hafizul_arif 5:fc218b8312eb 64 row = 0;
hafizul_arif 5:fc218b8312eb 65 } else
hafizul_arif 5:fc218b8312eb 66 col = 4;
hafizul_arif 5:fc218b8312eb 67 }else
hafizul_arif 5:fc218b8312eb 68 col = 4;
hafizul_arif 5:fc218b8312eb 69
mmlhein 4:61d3a7c1b411 70 pc.printf("Cursor left: %i,%i\r\n", col, row);
mmlhein 4:61d3a7c1b411 71
ebrahimatya 0:65f054e83dac 72 }
ebrahimatya 0:65f054e83dac 73
mmlhein 4:61d3a7c1b411 74
mmlhein 4:61d3a7c1b411 75 void cursor_right()
ebrahimatya 0:65f054e83dac 76 {
mmlhein 4:61d3a7c1b411 77 if(state_init) {
mmlhein 4:61d3a7c1b411 78 if (col >0 && col <13)
mmlhein 4:61d3a7c1b411 79 if (col == 12){
mmlhein 4:61d3a7c1b411 80 col =3;
mmlhein 4:61d3a7c1b411 81 row =1;
mmlhein 4:61d3a7c1b411 82 }
mmlhein 4:61d3a7c1b411 83
mmlhein 4:61d3a7c1b411 84 else if (col ==10)
mmlhein 4:61d3a7c1b411 85 col = 12;
mmlhein 4:61d3a7c1b411 86 else if (col ==7)
mmlhein 4:61d3a7c1b411 87 col = 10;
mmlhein 4:61d3a7c1b411 88 else if (col == 4) {
mmlhein 4:61d3a7c1b411 89 col = 7;
mmlhein 4:61d3a7c1b411 90 } else if (col ==3 && row == 1) {
mmlhein 4:61d3a7c1b411 91 col = 4;
mmlhein 4:61d3a7c1b411 92 row = 0;
mmlhein 4:61d3a7c1b411 93 } else
mmlhein 4:61d3a7c1b411 94 col = 4;
mmlhein 4:61d3a7c1b411 95 else
mmlhein 4:61d3a7c1b411 96 col = 4;
hafizul_arif 5:fc218b8312eb 97 }else if(state_menu){
hafizul_arif 5:fc218b8312eb 98
hafizul_arif 5:fc218b8312eb 99 if(col == 3){
hafizul_arif 5:fc218b8312eb 100 col = 9;
hafizul_arif 5:fc218b8312eb 101 } else if(col == 9){
hafizul_arif 5:fc218b8312eb 102 col = 3;
hafizul_arif 5:fc218b8312eb 103 }
hafizul_arif 5:fc218b8312eb 104 }else if(state_alarm){
hafizul_arif 5:fc218b8312eb 105 if (col >0 && col <13)
hafizul_arif 5:fc218b8312eb 106 if (col == 12){
hafizul_arif 5:fc218b8312eb 107 col =9;
hafizul_arif 5:fc218b8312eb 108 row =1;
hafizul_arif 5:fc218b8312eb 109 }
hafizul_arif 5:fc218b8312eb 110
hafizul_arif 5:fc218b8312eb 111 else if (col ==10)
hafizul_arif 5:fc218b8312eb 112 col = 12;
hafizul_arif 5:fc218b8312eb 113 else if (col ==7)
hafizul_arif 5:fc218b8312eb 114 col = 10;
hafizul_arif 5:fc218b8312eb 115 else if (col == 4) {
hafizul_arif 5:fc218b8312eb 116 col = 7;
hafizul_arif 5:fc218b8312eb 117 } else if (col ==3 && row == 1) {
hafizul_arif 5:fc218b8312eb 118 col = 4;
hafizul_arif 5:fc218b8312eb 119 row = 0;
hafizul_arif 5:fc218b8312eb 120 } else if(col == 9 && row == 1){
hafizul_arif 5:fc218b8312eb 121 col = 3;
hafizul_arif 5:fc218b8312eb 122 row = 1;
hafizul_arif 5:fc218b8312eb 123 } else
hafizul_arif 5:fc218b8312eb 124 col = 4;
hafizul_arif 5:fc218b8312eb 125 }else
hafizul_arif 5:fc218b8312eb 126 col = 4;
hafizul_arif 5:fc218b8312eb 127
mmlhein 4:61d3a7c1b411 128 pc.printf("Cursor right: %i,%i\r\n", col, row);
ebrahimatya 0:65f054e83dac 129 }
ebrahimatya 0:65f054e83dac 130
mmlhein 4:61d3a7c1b411 131 void chg_time_up(){
hafizul_arif 5:fc218b8312eb 132 if(state_init){
hafizul_arif 5:fc218b8312eb 133
mmlhein 4:61d3a7c1b411 134 if (col ==10){
mmlhein 4:61d3a7c1b411 135 r_sec = r_sec +1;
mmlhein 4:61d3a7c1b411 136 if (r_sec > 59)
mmlhein 4:61d3a7c1b411 137 r_sec = 0;
mmlhein 4:61d3a7c1b411 138 }
mmlhein 4:61d3a7c1b411 139 else if (col ==7){
mmlhein 4:61d3a7c1b411 140 r_min = r_min + 1;
mmlhein 4:61d3a7c1b411 141 if (r_min > 59)
mmlhein 4:61d3a7c1b411 142 r_min = 0; }
mmlhein 4:61d3a7c1b411 143 else if (col ==4){
mmlhein 4:61d3a7c1b411 144 r_hour = r_hour + 1;
mmlhein 4:61d3a7c1b411 145 if (r_hour > 12)
mmlhein 4:61d3a7c1b411 146 r_hour = 1;}
mmlhein 4:61d3a7c1b411 147 else if (col == 12){
mmlhein 4:61d3a7c1b411 148 if(r_apm==0)
mmlhein 4:61d3a7c1b411 149 r_apm=1;
mmlhein 4:61d3a7c1b411 150 else
mmlhein 4:61d3a7c1b411 151 r_apm=0;
mmlhein 4:61d3a7c1b411 152 }
mmlhein 4:61d3a7c1b411 153 else
mmlhein 4:61d3a7c1b411 154 pc.printf("lmfao");
mmlhein 6:167533262100 155 pc.printf("\nUP: %d:%d:%d %d\r\n", r_hour, r_min, r_sec, r_apm);
hafizul_arif 5:fc218b8312eb 156 } else if(state_alarm) {
mmlhein 6:167533262100 157
hafizul_arif 5:fc218b8312eb 158
hafizul_arif 5:fc218b8312eb 159
hafizul_arif 5:fc218b8312eb 160 if (col ==10){
hafizul_arif 5:fc218b8312eb 161 alarm_sec = alarm_sec +1;
hafizul_arif 5:fc218b8312eb 162 if (alarm_sec > 59)
hafizul_arif 5:fc218b8312eb 163 alarm_sec = 0;
hafizul_arif 5:fc218b8312eb 164 }
hafizul_arif 5:fc218b8312eb 165 else if (col ==7){
hafizul_arif 5:fc218b8312eb 166 alarm_min = alarm_min + 1;
hafizul_arif 5:fc218b8312eb 167 if (alarm_min > 59)
hafizul_arif 5:fc218b8312eb 168 alarm_min = 0; }
hafizul_arif 5:fc218b8312eb 169 else if (col ==4){
hafizul_arif 5:fc218b8312eb 170 alarm_hour = alarm_hour + 1;
hafizul_arif 5:fc218b8312eb 171 if (alarm_hour > 12)
hafizul_arif 5:fc218b8312eb 172 alarm_hour = 1;}
hafizul_arif 5:fc218b8312eb 173 else if (col == 12){
hafizul_arif 5:fc218b8312eb 174 if(alarm_apm==0)
hafizul_arif 5:fc218b8312eb 175 alarm_apm=1;
hafizul_arif 5:fc218b8312eb 176 else
hafizul_arif 5:fc218b8312eb 177 alarm_apm=0;
hafizul_arif 5:fc218b8312eb 178 }
hafizul_arif 5:fc218b8312eb 179 else
hafizul_arif 5:fc218b8312eb 180 pc.printf("lmfao");
mmlhein 6:167533262100 181 pc.printf("\nUP: %d:%d:%d %d\r\n", alarm_hour, alarm_min, alarm_sec, alarm_apm);
hafizul_arif 5:fc218b8312eb 182 }
mmlhein 6:167533262100 183
mmlhein 4:61d3a7c1b411 184 }
mmlhein 4:61d3a7c1b411 185 void chg_time_down()
ebrahimatya 0:65f054e83dac 186 {
hafizul_arif 5:fc218b8312eb 187 if(state_init){
mmlhein 4:61d3a7c1b411 188 if (col ==10){
mmlhein 4:61d3a7c1b411 189 r_sec = r_sec - 1;
mmlhein 4:61d3a7c1b411 190 if (r_sec < 0)
mmlhein 4:61d3a7c1b411 191 r_sec = 59;
mmlhein 4:61d3a7c1b411 192 }
mmlhein 4:61d3a7c1b411 193 else if (col ==7){
mmlhein 4:61d3a7c1b411 194 r_min = r_min - 1;
mmlhein 4:61d3a7c1b411 195 if (r_min < 0)
mmlhein 4:61d3a7c1b411 196 r_min = 59;}
mmlhein 4:61d3a7c1b411 197 else if (col ==4){
mmlhein 4:61d3a7c1b411 198 r_hour = r_hour - 1;
mmlhein 4:61d3a7c1b411 199 if (r_hour < 1)
mmlhein 4:61d3a7c1b411 200 r_hour = 12; }
mmlhein 4:61d3a7c1b411 201 else if (col == 12){
mmlhein 4:61d3a7c1b411 202 if(r_apm==0)
mmlhein 4:61d3a7c1b411 203 r_apm=1;
mmlhein 4:61d3a7c1b411 204 else
mmlhein 4:61d3a7c1b411 205 r_apm=0;
mmlhein 4:61d3a7c1b411 206 }
mmlhein 4:61d3a7c1b411 207 else
mmlhein 4:61d3a7c1b411 208 pc.printf("lmfao");
hafizul_arif 5:fc218b8312eb 209 } else if(state_alarm){
hafizul_arif 5:fc218b8312eb 210 if (col ==10){
hafizul_arif 5:fc218b8312eb 211 alarm_sec = alarm_sec - 1;
hafizul_arif 5:fc218b8312eb 212 if (alarm_sec < 0)
hafizul_arif 5:fc218b8312eb 213 alarm_sec = 59;
hafizul_arif 5:fc218b8312eb 214 }
hafizul_arif 5:fc218b8312eb 215 else if (col ==7){
hafizul_arif 5:fc218b8312eb 216 alarm_min = alarm_min - 1;
hafizul_arif 5:fc218b8312eb 217 if (alarm_min < 0)
hafizul_arif 5:fc218b8312eb 218 alarm_min = 59;}
hafizul_arif 5:fc218b8312eb 219 else if (col ==4){
hafizul_arif 5:fc218b8312eb 220 alarm_hour = alarm_hour - 1;
hafizul_arif 5:fc218b8312eb 221 if (alarm_hour < 1)
hafizul_arif 5:fc218b8312eb 222 alarm_hour = 12; }
hafizul_arif 5:fc218b8312eb 223 else if (col == 12){
hafizul_arif 5:fc218b8312eb 224 if(alarm_apm==0)
hafizul_arif 5:fc218b8312eb 225 alarm_apm=1;
hafizul_arif 5:fc218b8312eb 226 else
hafizul_arif 5:fc218b8312eb 227 alarm_apm=0;
hafizul_arif 5:fc218b8312eb 228 }
hafizul_arif 5:fc218b8312eb 229 else
hafizul_arif 5:fc218b8312eb 230 pc.printf("lmfao");
hafizul_arif 5:fc218b8312eb 231 }
hafizul_arif 5:fc218b8312eb 232
mmlhein 4:61d3a7c1b411 233 pc.printf("\nDOWN: %d:%d:%d\r\n", r_hour, r_min, r_sec);
ebrahimatya 0:65f054e83dac 234 }
ebrahimatya 0:65f054e83dac 235
mmlhein 6:167533262100 236 void show_menu(){
mmlhein 6:167533262100 237 if(alarm_up){
mmlhein 6:167533262100 238 lcd.locate(2,1);
mmlhein 6:167533262100 239 lcd.printf("*");
mmlhein 6:167533262100 240 }else {
mmlhein 6:167533262100 241 lcd.locate(2,1);
mmlhein 6:167533262100 242 lcd.printf(" ");
mmlhein 6:167533262100 243 }
mmlhein 6:167533262100 244 lcd.locate(3,1);
mmlhein 6:167533262100 245 lcd.printf("Alarm");
mmlhein 6:167533262100 246 lcd.locate(9,1);
mmlhein 6:167533262100 247 lcd.printf("Set");
mmlhein 6:167533262100 248 }
mmlhein 8:c8421fc56e8f 249 void alarm_sound(){
mmlhein 8:c8421fc56e8f 250 float frequency[]= {659,554,659,554,440,494,554,587,494,659,554,440}; //frequency array
mmlhein 8:c8421fc56e8f 251 float beat[]= {1,1,1,1,1,0.5,0.5,1,1,1,1,2}; //beat array
mmlhein 8:c8421fc56e8f 252 lcd.cls();
mmlhein 8:c8421fc56e8f 253 lcd.printf("Press button to off alarm");
mmlhein 8:c8421fc56e8f 254 while(1){
mmlhein 8:c8421fc56e8f 255 for(int i=0; i<=11; i++) {
mmlhein 8:c8421fc56e8f 256 buzzer.period(1/(2*frequency[i]));// set PWM period
mmlhein 8:c8421fc56e8f 257 buzzer=0.8;// set duty cycle
mmlhein 8:c8421fc56e8f 258 wait(0.4*beat[i]);// hold for beat period    
mmlhein 8:c8421fc56e8f 259 }
mmlhein 8:c8421fc56e8f 260 if(joycenter){//Off the alarm
mmlhein 8:c8421fc56e8f 261 buzzer.period(0);
mmlhein 8:c8421fc56e8f 262 buzzer=0;
mmlhein 8:c8421fc56e8f 263 break;
mmlhein 8:c8421fc56e8f 264 }
mmlhein 8:c8421fc56e8f 265 }
mmlhein 8:c8421fc56e8f 266 }
mmlhein 6:167533262100 267 //Function alarm_chk is in progress
mmlhein 6:167533262100 268 void alarm_chk(){
mmlhein 7:b9d3bc940d2c 269 //we basically, comparing 2 strings. alarm string and the clock strings if match we ring it
mmlhein 8:c8421fc56e8f 270 if(clock_string == alarm_string){
mmlhein 6:167533262100 271 pc.printf("%s %s : same\r\n", alarm_string, clock_string);
mmlhein 8:c8421fc56e8f 272 blinker_cursor.detach();
mmlhein 8:c8421fc56e8f 273 alarm_sound();
mmlhein 8:c8421fc56e8f 274 }
mmlhein 6:167533262100 275 }
mmlhein 6:167533262100 276
mmlhein 6:167533262100 277 void blink_cursor(){
mmlhein 6:167533262100 278 lcd.locate(col, row);
mmlhein 6:167533262100 279 lcd.printf("_");
mmlhein 6:167533262100 280 }
mmlhein 6:167533262100 281 void blink_num()
mmlhein 6:167533262100 282 {
mmlhein 6:167533262100 283
mmlhein 6:167533262100 284 if (r_hour > 9){
mmlhein 6:167533262100 285 lcd.locate(3,0);
mmlhein 6:167533262100 286 lcd.printf("%d:", r_hour);
mmlhein 6:167533262100 287 }
mmlhein 6:167533262100 288 else{
mmlhein 6:167533262100 289 lcd.locate(3,0);
mmlhein 6:167533262100 290 lcd.printf("0%d:", r_hour);
mmlhein 6:167533262100 291 }
mmlhein 6:167533262100 292 if (r_min > 9){
mmlhein 6:167533262100 293 lcd.locate(6,0);
mmlhein 6:167533262100 294 lcd.printf("%d:", r_min);
mmlhein 6:167533262100 295 }
mmlhein 6:167533262100 296 else{
mmlhein 6:167533262100 297 lcd.locate(6,0);
mmlhein 6:167533262100 298 lcd.printf("0%d:", r_min);
mmlhein 6:167533262100 299 }
mmlhein 6:167533262100 300 if (r_sec > 9){
mmlhein 6:167533262100 301 lcd.locate(9,0);
mmlhein 6:167533262100 302 lcd.printf("%d", r_sec);
mmlhein 6:167533262100 303 }
mmlhein 6:167533262100 304 else{
mmlhein 6:167533262100 305 lcd.locate(9,0);
mmlhein 6:167533262100 306 lcd.printf("0%d", r_sec);
mmlhein 6:167533262100 307 }
mmlhein 6:167533262100 308 lcd.locate(12,0);
mmlhein 6:167533262100 309 if (r_apm ==0)
mmlhein 6:167533262100 310 lcd.printf("AM");
mmlhein 6:167533262100 311 else
mmlhein 6:167533262100 312 lcd.printf("PM");
mmlhein 6:167533262100 313 lcd.locate(3,1);
mmlhein 6:167533262100 314 lcd.printf("Done");
mmlhein 6:167533262100 315 return;
mmlhein 6:167533262100 316 }
hafizul_arif 5:fc218b8312eb 317
hafizul_arif 5:fc218b8312eb 318 void alarm_disp(){
hafizul_arif 5:fc218b8312eb 319 lcd.cls();
hafizul_arif 5:fc218b8312eb 320 if (alarm_hour > 9){
hafizul_arif 5:fc218b8312eb 321 lcd.locate(3,0);
hafizul_arif 5:fc218b8312eb 322 lcd.printf("%d:", alarm_hour);
hafizul_arif 5:fc218b8312eb 323 }
hafizul_arif 5:fc218b8312eb 324 else{
hafizul_arif 5:fc218b8312eb 325 lcd.locate(3,0);
hafizul_arif 5:fc218b8312eb 326 lcd.printf("0%d:", alarm_hour);
hafizul_arif 5:fc218b8312eb 327 }
hafizul_arif 5:fc218b8312eb 328 if (alarm_min > 9){
hafizul_arif 5:fc218b8312eb 329 lcd.locate(6,0);
hafizul_arif 5:fc218b8312eb 330 lcd.printf("%d:", alarm_min);
hafizul_arif 5:fc218b8312eb 331 }
hafizul_arif 5:fc218b8312eb 332 else{
hafizul_arif 5:fc218b8312eb 333 lcd.locate(6,0);
hafizul_arif 5:fc218b8312eb 334 lcd.printf("0%d:", alarm_min);
hafizul_arif 5:fc218b8312eb 335 }
hafizul_arif 5:fc218b8312eb 336 if (alarm_sec > 9){
hafizul_arif 5:fc218b8312eb 337 lcd.locate(9,0);
hafizul_arif 5:fc218b8312eb 338 lcd.printf("%d", alarm_sec);
hafizul_arif 5:fc218b8312eb 339 }
hafizul_arif 5:fc218b8312eb 340 else{
hafizul_arif 5:fc218b8312eb 341 lcd.locate(9,0);
hafizul_arif 5:fc218b8312eb 342 lcd.printf("0%d", alarm_sec);
hafizul_arif 5:fc218b8312eb 343 }
hafizul_arif 5:fc218b8312eb 344 lcd.locate(12,0);
hafizul_arif 5:fc218b8312eb 345 if (alarm_apm ==0)
hafizul_arif 5:fc218b8312eb 346 lcd.printf("AM");
hafizul_arif 5:fc218b8312eb 347 else
hafizul_arif 5:fc218b8312eb 348 lcd.printf("PM");
hafizul_arif 5:fc218b8312eb 349 lcd.locate(3,1);
hafizul_arif 5:fc218b8312eb 350 lcd.printf("Off");
hafizul_arif 5:fc218b8312eb 351 lcd.locate(9,1);
hafizul_arif 5:fc218b8312eb 352 lcd.printf("On");
hafizul_arif 5:fc218b8312eb 353 return;
hafizul_arif 5:fc218b8312eb 354 }
mmlhein 8:c8421fc56e8f 355 void __set_alarm_string()
mmlhein 8:c8421fc56e8f 356 {
mmlhein 8:c8421fc56e8f 357 char buffer_alarm[32];
mmlhein 8:c8421fc56e8f 358 alarm_string.clear();
mmlhein 8:c8421fc56e8f 359 if(alarm_hour <9) {
mmlhein 8:c8421fc56e8f 360 sprintf (buffer_alarm, "0%d:", alarm_hour);
mmlhein 8:c8421fc56e8f 361 } else {
mmlhein 8:c8421fc56e8f 362 sprintf (buffer_alarm, "%d:", alarm_hour);
mmlhein 8:c8421fc56e8f 363 }
mmlhein 8:c8421fc56e8f 364 alarm_string += buffer_alarm;
mmlhein 8:c8421fc56e8f 365 if (alarm_min <9) {
mmlhein 8:c8421fc56e8f 366 sprintf (buffer_alarm, "0%d:", alarm_min);
mmlhein 8:c8421fc56e8f 367 } else {
mmlhein 8:c8421fc56e8f 368 sprintf (buffer_alarm, "%d:", alarm_min);
mmlhein 8:c8421fc56e8f 369 }
mmlhein 8:c8421fc56e8f 370 alarm_string += buffer_alarm;
mmlhein 8:c8421fc56e8f 371 if (alarm_sec<9) {
mmlhein 8:c8421fc56e8f 372 sprintf (buffer_alarm, "0%d", alarm_sec);
mmlhein 8:c8421fc56e8f 373 } else {
mmlhein 8:c8421fc56e8f 374 sprintf (buffer_alarm, "%d", alarm_sec);
mmlhein 8:c8421fc56e8f 375 }
mmlhein 8:c8421fc56e8f 376 alarm_string += buffer_alarm;
mmlhein 8:c8421fc56e8f 377 if(alarm_apm ==0) {
mmlhein 8:c8421fc56e8f 378 sprintf (buffer_alarm, " %s", "AM");
mmlhein 8:c8421fc56e8f 379 } else {
mmlhein 8:c8421fc56e8f 380 sprintf (buffer_alarm, " %s", "PM");
mmlhein 8:c8421fc56e8f 381 }
mmlhein 8:c8421fc56e8f 382 alarm_string += buffer_alarm;
mmlhein 8:c8421fc56e8f 383 }
mmlhein 8:c8421fc56e8f 384 void setup_alarm()
mmlhein 8:c8421fc56e8f 385 {
hafizul_arif 5:fc218b8312eb 386 state_alarm = true;
hafizul_arif 5:fc218b8312eb 387 state_menu = false;
hafizul_arif 5:fc218b8312eb 388 blinker_numer.attach(&alarm_disp,0.5);
mmlhein 8:c8421fc56e8f 389
mmlhein 8:c8421fc56e8f 390 while(1) {
hafizul_arif 5:fc218b8312eb 391 wait(1);
mmlhein 8:c8421fc56e8f 392 if(col == 3 && row ==1) {
mmlhein 8:c8421fc56e8f 393 if(joycenter) { //Turn off Alarm and back to main menu
hafizul_arif 5:fc218b8312eb 394 blinker_numer.detach();
hafizul_arif 5:fc218b8312eb 395 state_alarm = false;
hafizul_arif 5:fc218b8312eb 396 state_menu = true;
hafizul_arif 5:fc218b8312eb 397 alarm_up = false;
hafizul_arif 5:fc218b8312eb 398 check_alarm.detach();
mmlhein 8:c8421fc56e8f 399 alarm_string.clear();
hafizul_arif 5:fc218b8312eb 400 break;
mmlhein 8:c8421fc56e8f 401
hafizul_arif 5:fc218b8312eb 402 }
mmlhein 8:c8421fc56e8f 403 } else if(col == 9 && row == 1) {//Turn on Alarm and back to main menu
mmlhein 8:c8421fc56e8f 404 if(joycenter) {
hafizul_arif 5:fc218b8312eb 405 alarm_up = true;
hafizul_arif 5:fc218b8312eb 406 blinker_numer.detach();
hafizul_arif 5:fc218b8312eb 407 state_alarm = false;
hafizul_arif 5:fc218b8312eb 408 state_menu = true;
hafizul_arif 5:fc218b8312eb 409 check_alarm.attach(&alarm_chk,1);
mmlhein 8:c8421fc56e8f 410 __set_alarm_string();
hafizul_arif 5:fc218b8312eb 411 break;
hafizul_arif 5:fc218b8312eb 412 }
mmlhein 8:c8421fc56e8f 413
mmlhein 8:c8421fc56e8f 414
hafizul_arif 5:fc218b8312eb 415 }
hafizul_arif 5:fc218b8312eb 416 }
hafizul_arif 5:fc218b8312eb 417 }
hafizul_arif 5:fc218b8312eb 418
hafizul_arif 5:fc218b8312eb 419
mmlhein 4:61d3a7c1b411 420 void setup_init(){
mmlhein 4:61d3a7c1b411 421 state_init = true;
mmlhein 8:c8421fc56e8f 422 lcd.cls();
mmlhein 4:61d3a7c1b411 423 blinker_cursor.attach(&blink_cursor, 0.1);
mmlhein 4:61d3a7c1b411 424 blinker_numer.attach(&blink_num, 0.5);
mmlhein 4:61d3a7c1b411 425 while (1){
mmlhein 4:61d3a7c1b411 426 // This is where, we select "Done", and we press joycenter to end the initial setup.
mmlhein 4:61d3a7c1b411 427 if(col==3 && row==1){
mmlhein 4:61d3a7c1b411 428 if(joycenter){
mmlhein 4:61d3a7c1b411 429 blinker_numer.detach();
mmlhein 4:61d3a7c1b411 430
mmlhein 4:61d3a7c1b411 431 //Calculate the unix timestamp
mmlhein 4:61d3a7c1b411 432 //1h : 3600, 1m: 60, 1s: 1
mmlhein 4:61d3a7c1b411 433 if(r_apm==0){// AM
mmlhein 4:61d3a7c1b411 434 if(r_hour ==12)
mmlhein 4:61d3a7c1b411 435 epoch = (0 *3600) + (r_min *60) +(r_sec);
mmlhein 4:61d3a7c1b411 436 else
mmlhein 4:61d3a7c1b411 437 epoch = (r_hour *3600) + (r_min *60) +(r_sec);
mmlhein 4:61d3a7c1b411 438 }
mmlhein 4:61d3a7c1b411 439 else if (r_apm ==1){//PM
mmlhein 4:61d3a7c1b411 440 if(r_hour ==12)
mmlhein 4:61d3a7c1b411 441 epoch = (r_hour *3600) + (r_min *60) +(r_sec);
mmlhein 4:61d3a7c1b411 442 else
mmlhein 4:61d3a7c1b411 443 epoch = 43200+(r_hour *3600) + (r_min *60) +(r_sec);
mmlhein 4:61d3a7c1b411 444 }
mmlhein 4:61d3a7c1b411 445
mmlhein 8:c8421fc56e8f 446 pc.printf("%d\r\n", epoch);
mmlhein 8:c8421fc56e8f 447 set_time(epoch);
mmlhein 4:61d3a7c1b411 448 state_init = false;
mmlhein 4:61d3a7c1b411 449 break;
mmlhein 4:61d3a7c1b411 450 }
mmlhein 4:61d3a7c1b411 451 }
ebrahimatya 0:65f054e83dac 452 }
ebrahimatya 0:65f054e83dac 453 }
mmlhein 4:61d3a7c1b411 454 void view_clock()
mmlhein 4:61d3a7c1b411 455 {
mmlhein 4:61d3a7c1b411 456 lcd.locate(3,0);
mmlhein 4:61d3a7c1b411 457 time_t seconds = time(NULL);
mmlhein 4:61d3a7c1b411 458 char buffer[32];
mmlhein 4:61d3a7c1b411 459 strftime(buffer, 32, "%I:%M:%S %p", localtime(&seconds));
mmlhein 4:61d3a7c1b411 460 lcd.printf("%s", buffer);
hafizul_arif 5:fc218b8312eb 461 show_menu();
mmlhein 6:167533262100 462 clock_string = buffer;
hafizul_arif 5:fc218b8312eb 463
mmlhein 4:61d3a7c1b411 464 }
ebrahimatya 0:65f054e83dac 465
hafizul_arif 5:fc218b8312eb 466 void main_menu(){
hafizul_arif 5:fc218b8312eb 467 state_menu = true;
mmlhein 6:167533262100 468
hafizul_arif 5:fc218b8312eb 469 blinker_cursor.attach(&blink_cursor, 0.1);
hafizul_arif 5:fc218b8312eb 470
hafizul_arif 5:fc218b8312eb 471 while(1){
hafizul_arif 5:fc218b8312eb 472 wait(1);
hafizul_arif 5:fc218b8312eb 473 if(col == 3 && row == 1){
hafizul_arif 5:fc218b8312eb 474 if(joycenter){
hafizul_arif 5:fc218b8312eb 475 clock_viewer.detach();
mmlhein 6:167533262100 476 setup_alarm();
hafizul_arif 5:fc218b8312eb 477 clock_viewer.attach(&view_clock,1);
mmlhein 8:c8421fc56e8f 478
hafizul_arif 5:fc218b8312eb 479 }
hafizul_arif 5:fc218b8312eb 480 } else if(col == 9 && row ==1){
hafizul_arif 5:fc218b8312eb 481 if(joycenter){
mmlhein 8:c8421fc56e8f 482 clock_viewer.detach();
mmlhein 8:c8421fc56e8f 483 setup_init();
mmlhein 8:c8421fc56e8f 484 clock_viewer.attach(&view_clock,1);
hafizul_arif 5:fc218b8312eb 485 }
hafizul_arif 5:fc218b8312eb 486 }
hafizul_arif 5:fc218b8312eb 487 }
hafizul_arif 5:fc218b8312eb 488 }
hafizul_arif 5:fc218b8312eb 489
hafizul_arif 5:fc218b8312eb 490
mmlhein 4:61d3a7c1b411 491 int main(){
ebrahimatya 0:65f054e83dac 492
mmlhein 4:61d3a7c1b411 493
mmlhein 4:61d3a7c1b411 494 // Initial value
mmlhein 4:61d3a7c1b411 495 row =0 ;
mmlhein 4:61d3a7c1b411 496 col=0 ;
mmlhein 6:167533262100 497 alarm_hour=12;
mmlhein 6:167533262100 498 alarm_min=0;
mmlhein 8:c8421fc56e8f 499 alarm_sec=10;
mmlhein 6:167533262100 500 alarm_apm=0;
mmlhein 4:61d3a7c1b411 501 r_hour=12 ;
mmlhein 4:61d3a7c1b411 502 r_min=0 ;
mmlhein 4:61d3a7c1b411 503 r_sec = 0;
mmlhein 4:61d3a7c1b411 504 r_apm = 0; //0-AM, 1-PM
mmlhein 4:61d3a7c1b411 505 epoch = 0; // 12:00:00 AM
mmlhein 4:61d3a7c1b411 506
mmlhein 6:167533262100 507 // Change the cursor value.
mmlhein 6:167533262100 508 left.rise(&cursor_left);
mmlhein 6:167533262100 509 right.rise(&cursor_right);
mmlhein 6:167533262100 510 up.rise(&chg_time_up);
mmlhein 6:167533262100 511 down.rise(&chg_time_down);
mmlhein 6:167533262100 512
mmlhein 6:167533262100 513
mmlhein 6:167533262100 514 /*Run setup init -> show main menu
mmlhein 6:167533262100 515 |_ setup Alarm
mmlhein 6:167533262100 516 | |_Off alarm
mmlhein 6:167533262100 517 | |_On alarm
mmlhein 6:167533262100 518 |_ Change time
mmlhein 6:167533262100 519 */
mmlhein 4:61d3a7c1b411 520 setup_init();
mmlhein 8:c8421fc56e8f 521
mmlhein 4:61d3a7c1b411 522 clock_viewer.attach(&view_clock,1);
ebrahimatya 0:65f054e83dac 523 lcd.cls();
hafizul_arif 5:fc218b8312eb 524 main_menu();
hafizul_arif 5:fc218b8312eb 525
ebrahimatya 0:65f054e83dac 526
ebrahimatya 0:65f054e83dac 527
ebrahimatya 0:65f054e83dac 528
ebrahimatya 0:65f054e83dac 529 }