Final MEMBITT Code

Dependencies:   4DGL-uLCD-SE SDFileSystem mbed

Fork of reminder_main by Senior Design

Committer:
ashea6
Date:
Wed Dec 07 17:37:12 2016 +0000
Revision:
7:d85521f4f0c3
Parent:
6:ce8d4456e28b
done

Who changed what in which revision?

UserRevisionLine numberNew contents of line
deronmai 0:fef6b4d65600 1 #include "mbed.h"
deronmai 0:fef6b4d65600 2 #include <string>
deronmai 0:fef6b4d65600 3 #include <vector>
deronmai 0:fef6b4d65600 4 #include "utils.h"
deronmai 1:29de429a7e15 5 #include "SDFileSystem.h"
ashea6 7:d85521f4f0c3 6 //#include "uLCD_4DGL.h"
deronmai 0:fef6b4d65600 7 DigitalOut myled(LED1);
ashea6 7:d85521f4f0c3 8 DigitalOut myled2(LED2);
ashea6 7:d85521f4f0c3 9 DigitalOut myled3(LED3);
ashea6 7:d85521f4f0c3 10 DigitalOut myled4(LED4);
ashea6 7:d85521f4f0c3 11
deronmai 0:fef6b4d65600 12
deronmai 4:ae6216936ddf 13
deronmai 4:ae6216936ddf 14 InterruptIn pb1(p21);
deronmai 4:ae6216936ddf 15 InterruptIn pb2(p22);
deronmai 4:ae6216936ddf 16
ashea6 7:d85521f4f0c3 17 //uLCD_4DGL uLCD(p28,p27,p29);
deronmai 1:29de429a7e15 18 SDFileSystem sd(p5, p6, p7, p8, "sd");
deronmai 1:29de429a7e15 19 //PinDetect pb1(p21); // receive new schedule
ashea6 7:d85521f4f0c3 20 //Serial pc(USBTX, USBRX); // tx, rx .... for testing purposes (remove later)
ashea6 7:d85521f4f0c3 21 Serial pc(p28, p27);
ashea6 7:d85521f4f0c3 22 Serial mypc(USBTX, USBRX);
deronmai 1:29de429a7e15 23 Serial easyVR(p13,p14);
deronmai 4:ae6216936ddf 24 void pb1_hit_interrupt (void);
deronmai 4:ae6216936ddf 25 void pb2_hit_interrupt (void);
deronmai 4:ae6216936ddf 26
deronmai 0:fef6b4d65600 27
deronmai 0:fef6b4d65600 28 // Global vars
deronmai 0:fef6b4d65600 29 time_t ct_time;
deronmai 0:fef6b4d65600 30 struct tm * timeinfo;
deronmai 1:29de429a7e15 31 schedule myschedule;
deronmai 0:fef6b4d65600 32
deronmai 1:29de429a7e15 33 // Function declares
deronmai 0:fef6b4d65600 34 void clock_main(struct reminder *current_reminder);
deronmai 0:fef6b4d65600 35 int estimate_time(struct reminder current_reminder);
deronmai 1:29de429a7e15 36 void write_schedule_sd(char *buffer, int size);
deronmai 1:29de429a7e15 37 void speak(int num);
deronmai 1:29de429a7e15 38 void synthesis(reminder x);
deronmai 1:29de429a7e15 39 void synTimes(reminder x);
deronmai 1:29de429a7e15 40 int schedule_read();
deronmai 1:29de429a7e15 41 void writetest();
deronmai 1:29de429a7e15 42 void packdata(int send); // if send == 1 send data to pc
deronmai 1:29de429a7e15 43 //void pb1_hit_callback (void);
deronmai 1:29de429a7e15 44 void receive_schedule();
deronmai 0:fef6b4d65600 45
deronmai 1:29de429a7e15 46 int main()
deronmai 1:29de429a7e15 47 {
deronmai 0:fef6b4d65600 48 // in real project, we will set the time via the internet
deronmai 1:29de429a7e15 49 //uLCD.printf("Hello world\n");
deronmai 0:fef6b4d65600 50 // localtime(
deronmai 4:ae6216936ddf 51 pb1.mode(PullUp);
deronmai 4:ae6216936ddf 52 // Delay for initial pullup to take effect
deronmai 4:ae6216936ddf 53 wait(.01);
deronmai 4:ae6216936ddf 54 // Attach the address of the interrupt handler routine for pushbutton
deronmai 4:ae6216936ddf 55 pb1.fall(&pb1_hit_interrupt);
deronmai 4:ae6216936ddf 56 pb2.mode(PullUp);
deronmai 4:ae6216936ddf 57 wait(.01);
deronmai 4:ae6216936ddf 58 pb2.fall(&pb2_hit_interrupt);
deronmai 6:ce8d4456e28b 59
deronmai 4:ae6216936ddf 60 srand (time(NULL)); // initialize random numbers
deronmai 1:29de429a7e15 61 /*
deronmai 1:29de429a7e15 62 int i = 0;
deronmai 1:29de429a7e15 63 while(1)
deronmai 1:29de429a7e15 64 {
deronmai 0:fef6b4d65600 65
deronmai 1:29de429a7e15 66 uLCD.printf("%i",i);
deronmai 1:29de429a7e15 67 wait(1);
deronmai 1:29de429a7e15 68 }*/
deronmai 1:29de429a7e15 69 /*
deronmai 0:fef6b4d65600 70 reminder test_reminder;
deronmai 0:fef6b4d65600 71 // test_reminder.tm_time.tm_sec = 0;
deronmai 0:fef6b4d65600 72 test_reminder.tm_time.tm_min = 36;
deronmai 0:fef6b4d65600 73 test_reminder.tm_time.tm_hour = 11;
deronmai 0:fef6b4d65600 74 for (int i = 0; i < 7; i++) {
deronmai 0:fef6b4d65600 75 if (i == 1 || i == 3 || i == 4 || i == 5) test_reminder.reminder_days[i] = 1; // set MWHF
deronmai 0:fef6b4d65600 76 else test_reminder.reminder_days[i] = 0;
deronmai 1:29de429a7e15 77 }*/
deronmai 0:fef6b4d65600 78 // test_reminder.tm_time.tm_mday = 1; // day of the month
deronmai 0:fef6b4d65600 79 // test_reminder.tm_time.tm_mon = 0; // months since January
deronmai 0:fef6b4d65600 80 // test_reminder.tm_time.tm_year = 0; // years since 1970
deronmai 1:29de429a7e15 81 //myschedule.addReminder(test_reminder);
deronmai 6:ce8d4456e28b 82 // wake up EasyVR
deronmai 6:ce8d4456e28b 83 easyVR.putc('b');
deronmai 6:ce8d4456e28b 84 while (easyVR.getc()!='o') {
deronmai 6:ce8d4456e28b 85 easyVR.putc('b');
deronmai 6:ce8d4456e28b 86 wait(0.1);
deronmai 6:ce8d4456e28b 87 }
deronmai 6:ce8d4456e28b 88
ashea6 7:d85521f4f0c3 89 //uLCD.printf("waiting to receive schedule\n");
deronmai 1:29de429a7e15 90 receive_schedule();
deronmai 1:29de429a7e15 91 int current_time = schedule_read();
deronmai 1:29de429a7e15 92 //uLCD.printf("current time set to: %i", current_time);
deronmai 1:29de429a7e15 93
deronmai 2:f5f8c11849a2 94 set_time(current_time-18000); // Set RTC time to Wed, 28 Oct 2009 14:24:30
deronmai 2:f5f8c11849a2 95 // subtract 5*60*60 for eastern time
deronmai 1:29de429a7e15 96 time_t clock;
deronmai 1:29de429a7e15 97 time_t seconds = time(NULL);
ashea6 7:d85521f4f0c3 98 //uLCD.printf("Time as a basic string = %s", ctime(&seconds));
deronmai 1:29de429a7e15 99 //packdata(1);
deronmai 1:29de429a7e15 100 wait(1);
deronmai 1:29de429a7e15 101
deronmai 0:fef6b4d65600 102 int flag = 0;
ashea6 7:d85521f4f0c3 103 myled2 = 1;
deronmai 1:29de429a7e15 104 //Day currentDay = Wednesday;
ashea6 7:d85521f4f0c3 105 //uLCD.printf("current day is %i\n",timeinfo->tm_wday);
deronmai 1:29de429a7e15 106 int time_diff;
ashea6 7:d85521f4f0c3 107 int dex = 0;
ashea6 7:d85521f4f0c3 108 int stop_point = myschedule.getSize();
deronmai 0:fef6b4d65600 109 while(1) {
deronmai 1:29de429a7e15 110 for (int i = 0; i < myschedule.getSize(); i++) {
deronmai 0:fef6b4d65600 111 timeinfo = localtime (&clock); // ct_time not set right now
deronmai 0:fef6b4d65600 112 time(&clock);
ashea6 7:d85521f4f0c3 113 //Why this was printing on receive data app:
ashea6 7:d85521f4f0c3 114 mypc.printf("Time as a basic string = %s\n\r", ctime(&clock));
deronmai 1:29de429a7e15 115 reminder tmpReminder = myschedule.getCurrentReminder();
deronmai 1:29de429a7e15 116 // CHANGE TO POTENTAILLY NOT INCREMENTING CURRENT REMINDER?
deronmai 1:29de429a7e15 117 flag = 0;
ashea6 7:d85521f4f0c3 118 //mypc.printf("%s\n\r", tmpReminder.notification);
deronmai 0:fef6b4d65600 119 while (!flag) {
ashea6 7:d85521f4f0c3 120
deronmai 1:29de429a7e15 121 if (tmpReminder.reminder_days[timeinfo->tm_wday] != 1) {
ashea6 7:d85521f4f0c3 122 mypc.printf("what day is it: %i %i\n\r", timeinfo->tm_wday, tmpReminder.reminder_days[timeinfo->tm_wday]);
ashea6 7:d85521f4f0c3 123 //pc.printf("%i\n\r", myschedule.reminder_list[i].reminder_days[currentDay]);
deronmai 0:fef6b4d65600 124 i++; // current reminder not set to go off today; go to next reminder
deronmai 1:29de429a7e15 125 tmpReminder = myschedule.nextReminder();
deronmai 0:fef6b4d65600 126 }
deronmai 0:fef6b4d65600 127 // may need to check if the current reminder is not last one, otherwise out of bounds ERROR
deronmai 0:fef6b4d65600 128 else {
deronmai 1:29de429a7e15 129 time_diff = estimate_time(tmpReminder);
ashea6 7:d85521f4f0c3 130 mypc.printf("time diff %i\n\r", time_diff);
deronmai 1:29de429a7e15 131 //uLCD.locate(0,0);
ashea6 7:d85521f4f0c3 132 //uLCD.printf("time_diff is %i\n\r", time_diff);
deronmai 1:29de429a7e15 133 if (time_diff < 0) {
deronmai 1:29de429a7e15 134 i++;
deronmai 1:29de429a7e15 135 tmpReminder = myschedule.nextReminder();
deronmai 1:29de429a7e15 136 } else flag = 1;
deronmai 0:fef6b4d65600 137 // sleep(sleep_time); // try and sleep here
deronmai 0:fef6b4d65600 138 // wait(sleep_time); // replace this wait function with a smart sleep to save power and then poll after that
deronmai 0:fef6b4d65600 139 }
deronmai 0:fef6b4d65600 140 //wait(1);
deronmai 0:fef6b4d65600 141 }
ashea6 7:d85521f4f0c3 142
deronmai 1:29de429a7e15 143 if (tmpReminder.notification == "App") {
ashea6 7:d85521f4f0c3 144 myled3 = 1;
ashea6 7:d85521f4f0c3 145 //uLCD.printf("checking app\n");
deronmai 1:29de429a7e15 146 //if (timeinfo->tm_min == tmpReminder.min && timeinfo->tm_hour == tmpReminder.hour - 1) {
deronmai 1:29de429a7e15 147 //if (tmpReminder.hour - timeinfo->tm_hour <= 1) {
ashea6 7:d85521f4f0c3 148 if (time_diff <= 60) {
deronmai 6:ce8d4456e28b 149
deronmai 1:29de429a7e15 150 myled = true;
deronmai 1:29de429a7e15 151 synthesis(tmpReminder);
ashea6 7:d85521f4f0c3 152 //uLCD.printf("moving to next reminder\n");
ashea6 7:d85521f4f0c3 153 myschedule.nextReminder();
ashea6 7:d85521f4f0c3 154 //uLCD.printf("moved to next reminder\n");
ashea6 7:d85521f4f0c3 155 flag = 0;
ashea6 7:d85521f4f0c3 156 dex++;
ashea6 7:d85521f4f0c3 157 if (dex == stop_point){
deronmai 6:ce8d4456e28b 158 while (1) {}
deronmai 6:ce8d4456e28b 159 }
deronmai 1:29de429a7e15 160 }
deronmai 1:29de429a7e15 161 } else {
deronmai 1:29de429a7e15 162 if (timeinfo->tm_min == tmpReminder.min && timeinfo->tm_hour == tmpReminder.hour) {
deronmai 1:29de429a7e15 163 myled = true;
deronmai 1:29de429a7e15 164 synthesis(tmpReminder);
ashea6 7:d85521f4f0c3 165 //uLCD.printf("moving to next reminder\n");
ashea6 7:d85521f4f0c3 166 myschedule.nextReminder();
ashea6 7:d85521f4f0c3 167 //uLCD.printf("moved to next reminder\n");
ashea6 7:d85521f4f0c3 168 flag = 0;
ashea6 7:d85521f4f0c3 169 dex++;
ashea6 7:d85521f4f0c3 170 if (dex == stop_point){
deronmai 6:ce8d4456e28b 171 while (1) {}
deronmai 6:ce8d4456e28b 172 }
deronmai 1:29de429a7e15 173 }
deronmai 0:fef6b4d65600 174 }
deronmai 6:ce8d4456e28b 175 //if (myschedule.getSize() ==
deronmai 6:ce8d4456e28b 176 // wait(1);
deronmai 0:fef6b4d65600 177 }
ashea6 7:d85521f4f0c3 178 //uLCD.printf("All reminders done for the day\n");
deronmai 6:ce8d4456e28b 179 //while(1) {}
deronmai 0:fef6b4d65600 180 }
deronmai 0:fef6b4d65600 181 }
deronmai 0:fef6b4d65600 182
deronmai 1:29de429a7e15 183 int estimate_time(struct reminder current_reminder)
deronmai 1:29de429a7e15 184 {
deronmai 0:fef6b4d65600 185 // estimates the remaining time until the next reminder so program can sleep till then to save battery
deronmai 1:29de429a7e15 186 //uLCD.printf("current hour: %i note hour %i\n\r", timeinfo->tm_hour, current_reminder.hour);
deronmai 1:29de429a7e15 187 int estimated_time = (60*current_reminder.hour) - (60*timeinfo->tm_hour) +
deronmai 1:29de429a7e15 188 (current_reminder.min - timeinfo->tm_min);
deronmai 1:29de429a7e15 189 // convert hours to min, find estimated time before reminder plus 1 hour (60 min)
deronmai 1:29de429a7e15 190
deronmai 0:fef6b4d65600 191 return estimated_time;
deronmai 1:29de429a7e15 192
deronmai 1:29de429a7e15 193
deronmai 1:29de429a7e15 194 //if (current_reminder.reminder_days[currentDay] == 1) //
deronmai 1:29de429a7e15 195
deronmai 1:29de429a7e15 196 }
deronmai 1:29de429a7e15 197
deronmai 1:29de429a7e15 198 // use pushbutton to do this
deronmai 1:29de429a7e15 199 void receive_schedule()
deronmai 1:29de429a7e15 200 {
deronmai 1:29de429a7e15 201 pc.baud(9600);
deronmai 1:29de429a7e15 202 char buff[1000];
deronmai 1:29de429a7e15 203 int i = 0;
deronmai 1:29de429a7e15 204 bool flag = false;
deronmai 1:29de429a7e15 205 while(!flag) {
deronmai 1:29de429a7e15 206 buff[i] = pc.getc();
ashea6 7:d85521f4f0c3 207 //uLCD.printf("%c", buff[i]);
deronmai 1:29de429a7e15 208 //blue.putc(buff[i]);
deronmai 1:29de429a7e15 209 flag = buff[i] == '^';
ashea6 7:d85521f4f0c3 210 //if (flag) uLCD.printf("flag detected");
deronmai 1:29de429a7e15 211 i++;
deronmai 1:29de429a7e15 212 }
deronmai 1:29de429a7e15 213 write_schedule_sd(buff, i);
deronmai 1:29de429a7e15 214 }
deronmai 1:29de429a7e15 215
deronmai 1:29de429a7e15 216 void write_schedule_sd(char *buffer, int size)
deronmai 1:29de429a7e15 217 {
deronmai 1:29de429a7e15 218 mkdir("/sd/mydir", 0777);
deronmai 1:29de429a7e15 219 FILE *fp = fopen("/sd/mydir/schedule.txt", "w");
deronmai 1:29de429a7e15 220 if(fp == NULL) {
deronmai 1:29de429a7e15 221 error("Could not open file for write\n");
deronmai 1:29de429a7e15 222 }
deronmai 1:29de429a7e15 223 fprintf(fp, buffer);
deronmai 1:29de429a7e15 224 fclose(fp);
deronmai 1:29de429a7e15 225 }
deronmai 1:29de429a7e15 226
deronmai 1:29de429a7e15 227 int schedule_read()
deronmai 1:29de429a7e15 228 {
deronmai 1:29de429a7e15 229 char time_read[1];
deronmai 1:29de429a7e15 230 int set_time = 0;;
deronmai 1:29de429a7e15 231 //uLCD.locate(0,0);
deronmai 1:29de429a7e15 232 //uLCD.printf("reading schedule\r\n");
deronmai 1:29de429a7e15 233 FILE *fp = fopen("/sd/mydir/schedule.txt", "r");
deronmai 1:29de429a7e15 234 if(fp == NULL) {
deronmai 1:29de429a7e15 235 error("Could not open file to read\n");
deronmai 1:29de429a7e15 236 }
deronmai 1:29de429a7e15 237
deronmai 1:29de429a7e15 238 // get time
deronmai 1:29de429a7e15 239 fread(time_read,1,1,fp);
deronmai 1:29de429a7e15 240 if (time_read[0] != '[') {
ashea6 7:d85521f4f0c3 241 //uLCD.locate(0,0);
ashea6 7:d85521f4f0c3 242 //uLCD.printf("[ not found, instead found %s\r\n",time_read[0]);
deronmai 1:29de429a7e15 243 error("Time in incorrect format, expected [, got %s\n",time_read[0]);
deronmai 1:29de429a7e15 244 }
deronmai 1:29de429a7e15 245 while (time_read[0] != ']') {
deronmai 1:29de429a7e15 246 fread(time_read,1,1,fp);
deronmai 1:29de429a7e15 247 if (time_read[0] != ']') set_time = (set_time*10)+(time_read[0] - '0');
deronmai 1:29de429a7e15 248 }
deronmai 1:29de429a7e15 249
deronmai 1:29de429a7e15 250 //set_time(set_time);
deronmai 1:29de429a7e15 251
deronmai 1:29de429a7e15 252
deronmai 1:29de429a7e15 253
deronmai 1:29de429a7e15 254
deronmai 1:29de429a7e15 255 //uLCD.locate(0,0);
deronmai 1:29de429a7e15 256 //uLCD.printf("opened successfully\r\n");
deronmai 1:29de429a7e15 257 char reminder_size[1];
deronmai 1:29de429a7e15 258 int header = 1;
deronmai 1:29de429a7e15 259 int reminder_count = 0;
deronmai 1:29de429a7e15 260 //fgets(reminder_size, sizeof(reminder_size), fp);
deronmai 1:29de429a7e15 261 fread(reminder_size,1,1,fp);
deronmai 1:29de429a7e15 262 if (reminder_size[0] != '{') {
ashea6 7:d85521f4f0c3 263 //uLCD.locate(0,0);
ashea6 7:d85521f4f0c3 264 //uLCD.printf("{ not found, instead found %s\r\n",reminder_size[0]);
deronmai 1:29de429a7e15 265 error("Schedule in incorrect format, expected {, got %s\n",reminder_size[0]);
deronmai 1:29de429a7e15 266 }
deronmai 1:29de429a7e15 267 while (header) {
deronmai 1:29de429a7e15 268 char test[1];
deronmai 1:29de429a7e15 269 //fgets(reminder_size, sizeof(reminder_size), fp);
deronmai 1:29de429a7e15 270 fread(test,1,1,fp);
deronmai 1:29de429a7e15 271 if (test[0] == '|') header = 0;
deronmai 1:29de429a7e15 272 else {
deronmai 1:29de429a7e15 273 //uLCD.printf("%s %i",reminder_size[0], (int)reminder_size[0]);
ashea6 7:d85521f4f0c3 274 //uLCD.printf("%c",test[0]);
deronmai 1:29de429a7e15 275 //wait(4);
deronmai 1:29de429a7e15 276 reminder_count = (reminder_count*10)+(test[0] - '0');
deronmai 1:29de429a7e15 277 }
deronmai 1:29de429a7e15 278 }
ashea6 7:d85521f4f0c3 279 //uLCD.cls();
deronmai 1:29de429a7e15 280 //uLCD.locate(0,0);
deronmai 1:29de429a7e15 281 //uLCD.printf("parsing schedule reminder count is %i\r\n", reminder_count);
deronmai 1:29de429a7e15 282 //wait(5);
deronmai 1:29de429a7e15 283 reminder tmp;
deronmai 1:29de429a7e15 284
deronmai 1:29de429a7e15 285 char buf[REMINDER_SIZE];
deronmai 1:29de429a7e15 286 //int days;
deronmai 1:29de429a7e15 287 for (int i = 0; i < reminder_count; i++) {
deronmai 1:29de429a7e15 288 //fgets(buf, sizeof(buf), fp);
deronmai 1:29de429a7e15 289 fread(buf,sizeof(buf),1,fp);
ashea6 7:d85521f4f0c3 290 //uLCD.cls();
ashea6 7:d85521f4f0c3 291 //uLCD.locate(0,0);
ashea6 7:d85521f4f0c3 292 //uLCD.printf("buf is %s\r\n",buf);
deronmai 1:29de429a7e15 293 wait(.1);
deronmai 1:29de429a7e15 294 char *pch;
deronmai 1:29de429a7e15 295 pch = strtok (buf,"+");
deronmai 1:29de429a7e15 296 tmp.notification = pch;
ashea6 7:d85521f4f0c3 297 //uLCD.printf("%s\n", pch);
deronmai 1:29de429a7e15 298 wait(.1);
deronmai 1:29de429a7e15 299 pch = strtok (NULL,"+");
deronmai 1:29de429a7e15 300 //uLCD.printf("%s\n", pch);
deronmai 1:29de429a7e15 301 if (strlen(pch) == 1) tmp.hour = (int) (pch[0] - '0');
deronmai 1:29de429a7e15 302 else tmp.hour = (int) ((pch[0] - '0') * 10 + (pch[1] - '0'));
deronmai 1:29de429a7e15 303 //if (tmp.hour > 24
ashea6 7:d85521f4f0c3 304 //uLCD.printf("%i\n", tmp.hour);
deronmai 1:29de429a7e15 305 //wait(1);
deronmai 1:29de429a7e15 306 pch = strtok (NULL,"+");
deronmai 1:29de429a7e15 307 if (strlen(pch) == 1) tmp.min = (int) (pch[0] - '0');
deronmai 1:29de429a7e15 308 else tmp.min = (int) ((pch[0] - '0') * 10 + (pch[1] - '0'));
ashea6 7:d85521f4f0c3 309 //uLCD.printf("%i\n", tmp.min);
deronmai 1:29de429a7e15 310 wait(.1);
deronmai 1:29de429a7e15 311 pch = strtok (NULL,"+");
deronmai 1:29de429a7e15 312 for (int i = 0; i < 7; i++) {
deronmai 1:29de429a7e15 313 tmp.reminder_days[i] = (int) pch[i] - '0';
deronmai 1:29de429a7e15 314 //days = days/10;
ashea6 7:d85521f4f0c3 315 //uLCD.printf("%i\n", tmp.reminder_days[i]);
deronmai 1:29de429a7e15 316 //wait(.1);
deronmai 1:29de429a7e15 317 }
deronmai 1:29de429a7e15 318
deronmai 1:29de429a7e15 319 //add to the (schedule) vector here
deronmai 1:29de429a7e15 320 myschedule.addReminder(tmp);
deronmai 1:29de429a7e15 321 wait(5);
deronmai 1:29de429a7e15 322 }
deronmai 1:29de429a7e15 323 fclose(fp);
ashea6 7:d85521f4f0c3 324 //uLCD.cls();
ashea6 7:d85521f4f0c3 325 //uLCD.printf("returned time here");
deronmai 1:29de429a7e15 326
deronmai 1:29de429a7e15 327 return set_time;
deronmai 1:29de429a7e15 328 }
deronmai 1:29de429a7e15 329 /*
deronmai 1:29de429a7e15 330 void clock_main(struct reminder *current_reminder) {
deronmai 1:29de429a7e15 331
deronmai 1:29de429a7e15 332 }
deronmai 1:29de429a7e15 333 */
deronmai 1:29de429a7e15 334
deronmai 1:29de429a7e15 335 void packdata(int send)
deronmai 1:29de429a7e15 336 {
ashea6 7:d85521f4f0c3 337 /*
deronmai 1:29de429a7e15 338 dateTime testdt;
deronmai 1:29de429a7e15 339 testdt.month = 1;
deronmai 1:29de429a7e15 340 testdt.day = 2;
deronmai 1:29de429a7e15 341 testdt.year = 2009;
deronmai 1:29de429a7e15 342 myschedule.updateRecord(testdt, 1);
deronmai 1:29de429a7e15 343 myschedule.updateRecord(testdt, 0);
ashea6 7:d85521f4f0c3 344 */
deronmai 1:29de429a7e15 345 int rem_num = 0; // number of reminders
deronmai 1:29de429a7e15 346 char con[2];
deronmai 1:29de429a7e15 347 string record_str = "";
deronmai 1:29de429a7e15 348 string part;
deronmai 1:29de429a7e15 349 int data_entries;
deronmai 1:29de429a7e15 350 record tmpr;
deronmai 1:29de429a7e15 351 dateTime tmpd;
ashea6 7:d85521f4f0c3 352 //uLCD.printf("test start\r\n");
deronmai 1:29de429a7e15 353 for (int i = 0; i < myschedule.getSize(); i++) {
ashea6 7:d85521f4f0c3 354 //uLCD.printf("schedule entries exist\r\n");
deronmai 1:29de429a7e15 355 data_entries = myschedule.getNumRecords(i);
deronmai 1:29de429a7e15 356 if (data_entries) { // more than 1 data entry
deronmai 1:29de429a7e15 357 rem_num++;
deronmai 1:29de429a7e15 358 char de[2];
deronmai 1:29de429a7e15 359 sprintf(con, "%d", i);
deronmai 1:29de429a7e15 360 sprintf(de, "%d", data_entries);
deronmai 1:29de429a7e15 361 record_str += "|r" + (string)con + "(" + (string)de + ")";
deronmai 1:29de429a7e15 362 tmpr = myschedule.getRecord(i);
deronmai 1:29de429a7e15 363 for (int j = 0; j < data_entries; j++) {
deronmai 1:29de429a7e15 364 tmpd = tmpr.date[j];
ashea6 7:d85521f4f0c3 365 char tmpmon[2], tmpday[2], tmpyear[4];
ashea6 7:d85521f4f0c3 366 sprintf(tmpmon, "%i", tmpd.month);
ashea6 7:d85521f4f0c3 367 sprintf(tmpday, "%i", tmpd.day);
ashea6 7:d85521f4f0c3 368 sprintf(tmpyear, "%i", tmpd.year);
deronmai 1:29de429a7e15 369 part = "[";
deronmai 1:29de429a7e15 370 if (tmpd.month < 10) part += "0";
deronmai 1:29de429a7e15 371 part += (string) tmpmon + "-";
deronmai 1:29de429a7e15 372 if (tmpd.day < 10) part += "0";
deronmai 1:29de429a7e15 373 part += (string)tmpday + "-" + (string)tmpyear + "]";
deronmai 1:29de429a7e15 374 char tmpval[1];
ashea6 7:d85521f4f0c3 375 sprintf(tmpval, "%i", tmpr.data[j]);
deronmai 1:29de429a7e15 376 part += tmpval;
ashea6 7:d85521f4f0c3 377 //uLCD.printf("%s\r\n", part.c_str());
deronmai 1:29de429a7e15 378 //part = "[" + tmpd.month + "-" + tmpd.day + "-" + tmpd.year + "]";
deronmai 1:29de429a7e15 379 record_str += part;
deronmai 1:29de429a7e15 380 }
deronmai 1:29de429a7e15 381 }
deronmai 1:29de429a7e15 382 }
deronmai 1:29de429a7e15 383 char remnum[2];
deronmai 1:29de429a7e15 384 sprintf(remnum, "%d", rem_num);
deronmai 1:29de429a7e15 385 record_str = "{" + (string)remnum + record_str + "}";
ashea6 7:d85521f4f0c3 386 //uLCD.printf("%s\r\n", record_str.c_str());
ashea6 7:d85521f4f0c3 387 //uLCD.printf("starting data record write\r\n");
deronmai 1:29de429a7e15 388
deronmai 1:29de429a7e15 389
deronmai 1:29de429a7e15 390 // save data to buffer
deronmai 1:29de429a7e15 391 const char *buffer = record_str.c_str();
deronmai 1:29de429a7e15 392 mkdir("/sd/mydir", 0777);
deronmai 1:29de429a7e15 393 FILE *fp = fopen("/sd/mydir/data.txt", "w");
deronmai 1:29de429a7e15 394 if(fp == NULL) {
deronmai 1:29de429a7e15 395 error("Could not open file for write\n");
deronmai 1:29de429a7e15 396 }
deronmai 1:29de429a7e15 397 fprintf(fp, buffer);
deronmai 1:29de429a7e15 398 fclose(fp);
deronmai 1:29de429a7e15 399
deronmai 1:29de429a7e15 400 // if send is 1, send to desktop
deronmai 1:29de429a7e15 401 if (send) {
deronmai 1:29de429a7e15 402 pc.printf("%s", buffer);
deronmai 1:29de429a7e15 403 }
deronmai 1:29de429a7e15 404
deronmai 0:fef6b4d65600 405 }
deronmai 0:fef6b4d65600 406
deronmai 0:fef6b4d65600 407
deronmai 1:29de429a7e15 408 void writetest()
deronmai 1:29de429a7e15 409 {
ashea6 7:d85521f4f0c3 410 //uLCD.printf("starting test\r\n");
deronmai 1:29de429a7e15 411 char *buffer = "hello1";
deronmai 1:29de429a7e15 412 mkdir("/sd/mydir", 0777);
deronmai 1:29de429a7e15 413 FILE *fp = fopen("/sd/mydir/data.txt", "w");
deronmai 1:29de429a7e15 414 if(fp == NULL) {
deronmai 1:29de429a7e15 415 error("Could not open file for write\n");
deronmai 1:29de429a7e15 416 }
deronmai 1:29de429a7e15 417 fprintf(fp, buffer);
deronmai 1:29de429a7e15 418 fclose(fp);
ashea6 7:d85521f4f0c3 419 //uLCD.printf("first opened\r\n");
deronmai 1:29de429a7e15 420 char *buffer2 = "good2";
deronmai 1:29de429a7e15 421 FILE *fq = fopen("/sd/mydir/data.txt", "w");
deronmai 1:29de429a7e15 422 if(fq == NULL) {
deronmai 1:29de429a7e15 423 error("Could not open file for write\n");
deronmai 1:29de429a7e15 424 }
deronmai 1:29de429a7e15 425 fprintf(fq, buffer2);
deronmai 1:29de429a7e15 426 fclose(fq);
ashea6 7:d85521f4f0c3 427 //uLCD.printf("second opened\r\n");
deronmai 1:29de429a7e15 428
deronmai 1:29de429a7e15 429
deronmai 1:29de429a7e15 430 char open[10];
deronmai 1:29de429a7e15 431 FILE *fw = fopen("/sd/mydir/data.txt", "r");
deronmai 1:29de429a7e15 432 if(fw == NULL) {
deronmai 1:29de429a7e15 433 error("Could not open file to read\n");
deronmai 1:29de429a7e15 434 }
deronmai 1:29de429a7e15 435 fread(open,sizeof(open),8,fw);
ashea6 7:d85521f4f0c3 436 //uLCD.printf("%s", open);
ashea6 7:d85521f4f0c3 437 //uLCD.printf("should have printed\r\n");
deronmai 1:29de429a7e15 438
deronmai 1:29de429a7e15 439 }
deronmai 1:29de429a7e15 440
deronmai 1:29de429a7e15 441 // Function to play a sound file on speaker
deronmai 1:29de429a7e15 442 void speak(int num)
deronmai 1:29de429a7e15 443 {
deronmai 1:29de429a7e15 444
deronmai 1:29de429a7e15 445 // Send Play Sound command
deronmai 1:29de429a7e15 446 easyVR.putc('w');
deronmai 1:29de429a7e15 447 // small delay is needed between characters
deronmai 1:29de429a7e15 448 wait(.002);
deronmai 1:29de429a7e15 449 // Sound table index
deronmai 1:29de429a7e15 450 easyVR.putc('A' + num/32);
deronmai 1:29de429a7e15 451 wait(.002);
deronmai 1:29de429a7e15 452 easyVR.putc('A' + num%32);
deronmai 1:29de429a7e15 453 wait(.002);
deronmai 1:29de429a7e15 454 // max volume
deronmai 1:29de429a7e15 455 easyVR.putc('P');
deronmai 1:29de429a7e15 456 // Wait for response of 'o' as playback ends
deronmai 1:29de429a7e15 457 while (easyVR.getc()!='o') {}
deronmai 1:29de429a7e15 458 wait(.25);
deronmai 1:29de429a7e15 459 }
deronmai 1:29de429a7e15 460
deronmai 1:29de429a7e15 461 void synthesis(reminder x)
deronmai 1:29de429a7e15 462 {
ashea6 7:d85521f4f0c3 463 //uLCD.printf("speaking syn\n");
deronmai 1:29de429a7e15 464 if(x.notification == "App") {
deronmai 1:29de429a7e15 465 speak(synWords[3]);
deronmai 1:29de429a7e15 466 wait(0.001);
deronmai 1:29de429a7e15 467 synTimes(x);
deronmai 1:29de429a7e15 468 } else {
deronmai 1:29de429a7e15 469 //reminder
deronmai 1:29de429a7e15 470 int asking = 1;
deronmai 1:29de429a7e15 471 int result;
deronmai 1:29de429a7e15 472 int repeat = 0;
deronmai 1:29de429a7e15 473 int response = 0;
deronmai 1:29de429a7e15 474 while (asking) {
ashea6 7:d85521f4f0c3 475 mypc.printf("asking\r\n");
deronmai 1:29de429a7e15 476 speak(synWords[2]);
deronmai 1:29de429a7e15 477 wait(0.001);
deronmai 1:29de429a7e15 478 //ask if they've taken their medication
deronmai 1:29de429a7e15 479 speak(synWords[6]);
deronmai 1:29de429a7e15 480 wait(0.001);
deronmai 1:29de429a7e15 481 //intake and process yes/no response
deronmai 1:29de429a7e15 482 easyVR.putc('d');
deronmai 1:29de429a7e15 483 wait(.001);
deronmai 1:29de429a7e15 484 easyVR.putc('B');
deronmai 1:29de429a7e15 485 clock_t endwait;
deronmai 1:29de429a7e15 486 endwait = clock() + 60 * CLOCKS_PER_SEC ;
ashea6 7:d85521f4f0c3 487 while (easyVR.readable()!=0 && clock() < endwait) {
ashea6 7:d85521f4f0c3 488 mypc.printf("listening\r\n");
ashea6 7:d85521f4f0c3 489 }
deronmai 1:29de429a7e15 490 if (clock() < endwait) {
deronmai 1:29de429a7e15 491 response = 1;
deronmai 1:29de429a7e15 492 //asking = 0;
deronmai 1:29de429a7e15 493 } else {
deronmai 1:29de429a7e15 494 repeat++;
deronmai 1:29de429a7e15 495 if (repeat > 2) {
deronmai 1:29de429a7e15 496 result = -1;
deronmai 1:29de429a7e15 497 asking = 0;
deronmai 1:29de429a7e15 498 }
deronmai 1:29de429a7e15 499 }
deronmai 1:29de429a7e15 500 if (response) {
deronmai 1:29de429a7e15 501 char rchar=easyVR.getc();
deronmai 1:29de429a7e15 502 pc.putc(rchar);
deronmai 1:29de429a7e15 503 if (rchar == 'r') {
deronmai 1:29de429a7e15 504 easyVR.putc(' ');
deronmai 1:29de429a7e15 505 rchar = easyVR.getc();
deronmai 1:29de429a7e15 506 //for a yes response
ashea6 7:d85521f4f0c3 507 if (rchar=='A' || rchar=='C' || rchar=='D' || rchar=='F' || rchar == 'J') {
ashea6 7:d85521f4f0c3 508 //uLCD.printf("yes");
deronmai 1:29de429a7e15 509 //yes++;
deronmai 1:29de429a7e15 510 //response++;
deronmai 1:29de429a7e15 511 result = 1;
ashea6 7:d85521f4f0c3 512 } else if (rchar == 'B' || rchar=='E' || rchar=='G' || rchar=='I' || rchar=='K') {
ashea6 7:d85521f4f0c3 513 //uLCD.printf("no");
deronmai 1:29de429a7e15 514 result = 0;
deronmai 1:29de429a7e15 515 //no++;
deronmai 1:29de429a7e15 516 //response++;
deronmai 1:29de429a7e15 517 }
deronmai 1:29de429a7e15 518 asking = 0;
ashea6 7:d85521f4f0c3 519 //uLCD.printf("asking = 0\n");
deronmai 1:29de429a7e15 520
deronmai 1:29de429a7e15 521 } else if (rchar == 'e') {
ashea6 7:d85521f4f0c3 522 //uLCD.printf("error");
deronmai 1:29de429a7e15 523 result = -1;
deronmai 1:29de429a7e15 524 repeat++;
deronmai 1:29de429a7e15 525 if (repeat > 2) {
deronmai 1:29de429a7e15 526 result = -1;
deronmai 1:29de429a7e15 527 asking = 0;
deronmai 1:29de429a7e15 528 // try again
deronmai 1:29de429a7e15 529 }
ashea6 7:d85521f4f0c3 530 } // else uLCD.printf("%c ",rchar);
deronmai 1:29de429a7e15 531 }
deronmai 1:29de429a7e15 532 }
deronmai 1:29de429a7e15 533
ashea6 7:d85521f4f0c3 534 //uLCD.printf("done\n");
deronmai 1:29de429a7e15 535 dateTime dt_record;
ashea6 7:d85521f4f0c3 536 dt_record.month = timeinfo->tm_mon+1; // months since january (0-11) so add 1
ashea6 7:d85521f4f0c3 537 dt_record.day = timeinfo->tm_mday;
ashea6 7:d85521f4f0c3 538 dt_record.year = timeinfo->tm_year + 1900; // years since 1990
ashea6 7:d85521f4f0c3 539 mypc.printf("month %i\r\n", dt_record.month);
ashea6 7:d85521f4f0c3 540 mypc.printf("day %i\r\n", dt_record.day);
ashea6 7:d85521f4f0c3 541 mypc.printf("year %i\r\n", dt_record.year);
ashea6 7:d85521f4f0c3 542 mypc.printf("result %i\r\n", result);
deronmai 1:29de429a7e15 543 myschedule.updateRecord(dt_record, result);
deronmai 1:29de429a7e15 544 // do error checking for no response
deronmai 1:29de429a7e15 545 //thank the user
deronmai 1:29de429a7e15 546 speak(synWords[7]);
deronmai 1:29de429a7e15 547 }
deronmai 1:29de429a7e15 548
deronmai 1:29de429a7e15 549 myled = 0;
deronmai 1:29de429a7e15 550 wait(0.001);
deronmai 1:29de429a7e15 551
deronmai 1:29de429a7e15 552 }
deronmai 1:29de429a7e15 553
deronmai 1:29de429a7e15 554 void synTimes(reminder x)
deronmai 1:29de429a7e15 555 {
ashea6 7:d85521f4f0c3 556 //uLCD.printf("speaking time\n");
deronmai 1:29de429a7e15 557 // schedule.reminder_list....
deronmai 1:29de429a7e15 558 // x is selected reminder
deronmai 1:29de429a7e15 559 //two switch case statements: one for if the second number is a 5, one if it's a 0
deronmai 1:29de429a7e15 560 int ampm = 0;
deronmai 1:29de429a7e15 561 int hour = x.hour;
ashea6 7:d85521f4f0c3 562 if(x.hour >= 12) {
ashea6 7:d85521f4f0c3 563 if (x.hour != 12)
ashea6 7:d85521f4f0c3 564 hour = x.hour % 12;
deronmai 1:29de429a7e15 565 ampm = 24;
deronmai 1:29de429a7e15 566 } else
deronmai 1:29de429a7e15 567 ampm = 22;
deronmai 1:29de429a7e15 568
deronmai 1:29de429a7e15 569 // need to add speaking hours?
deronmai 1:29de429a7e15 570 speak(synTime[hour-1]);
deronmai 1:29de429a7e15 571 wait(0.002);
deronmai 1:29de429a7e15 572
deronmai 1:29de429a7e15 573 int minutes[2];
deronmai 1:29de429a7e15 574 minutes[0] = x.min/10;
deronmai 1:29de429a7e15 575 minutes[1] = x.min%10;
deronmai 1:29de429a7e15 576
deronmai 1:29de429a7e15 577 switch(minutes[0]) {
deronmai 1:29de429a7e15 578 case 0:
deronmai 1:29de429a7e15 579 if (minutes[1] == 0) {
deronmai 4:ae6216936ddf 580 // say "OH"
deronmai 4:ae6216936ddf 581 // speak(synWords[1]);
deronmai 4:ae6216936ddf 582 // wait(0.002);
deronmai 4:ae6216936ddf 583 // say "o'clock"
deronmai 1:29de429a7e15 584 speak(synWords[0]);
deronmai 1:29de429a7e15 585 wait(0.002);
deronmai 1:29de429a7e15 586 }
deronmai 5:69da174d0f56 587 else {
deronmai 5:69da174d0f56 588 // say "OH"
deronmai 5:69da174d0f56 589 speak(synWords[1]);
deronmai 5:69da174d0f56 590 wait(0.002);
deronmai 5:69da174d0f56 591 }
deronmai 1:29de429a7e15 592 break;
deronmai 1:29de429a7e15 593 case 1:
deronmai 1:29de429a7e15 594 if (minutes[1] == 0) {
deronmai 1:29de429a7e15 595 speak(synTime[10-1]);
deronmai 1:29de429a7e15 596 wait(0.002);
deronmai 1:29de429a7e15 597 } else {
deronmai 1:29de429a7e15 598 //say "fifteen"
deronmai 1:29de429a7e15 599 speak(synTime[13-1]);
deronmai 1:29de429a7e15 600 wait(0.002);
deronmai 1:29de429a7e15 601 }
deronmai 1:29de429a7e15 602 break;
deronmai 1:29de429a7e15 603 case 2:
deronmai 1:29de429a7e15 604 //say "twenty"
deronmai 1:29de429a7e15 605 speak(synTime[17-1]);
deronmai 1:29de429a7e15 606 wait(0.002);
deronmai 1:29de429a7e15 607 break;
deronmai 1:29de429a7e15 608 case 3:
deronmai 1:29de429a7e15 609 //say "thirty"
deronmai 1:29de429a7e15 610 speak(synTime[16-1]);
deronmai 1:29de429a7e15 611 wait(0.002);
deronmai 1:29de429a7e15 612 break;
deronmai 1:29de429a7e15 613 case 4:
deronmai 1:29de429a7e15 614 //say "forty"
deronmai 1:29de429a7e15 615 speak(synTime[15-1]);
deronmai 1:29de429a7e15 616 wait(0.002);
deronmai 1:29de429a7e15 617 break;
deronmai 1:29de429a7e15 618 case 5:
deronmai 1:29de429a7e15 619 //say "fifty"
deronmai 1:29de429a7e15 620 speak(synTime[14-1]);
deronmai 1:29de429a7e15 621 wait(0.002);
deronmai 1:29de429a7e15 622 break;
deronmai 1:29de429a7e15 623 }
deronmai 1:29de429a7e15 624 if (minutes[0] != 1) {
deronmai 1:29de429a7e15 625 if (minutes[1] == 5) {
deronmai 1:29de429a7e15 626 //say "5"
deronmai 1:29de429a7e15 627 speak(synTime[5-1]);
deronmai 1:29de429a7e15 628 wait(0.002);
deronmai 1:29de429a7e15 629 }
deronmai 1:29de429a7e15 630 }
deronmai 1:29de429a7e15 631 speak(ampm);
ashea6 7:d85521f4f0c3 632 //uLCD.printf("done speaking\n");
deronmai 1:29de429a7e15 633 }
deronmai 0:fef6b4d65600 634 /*
deronmai 1:29de429a7e15 635 void pb1_hit_callback (void) {
deronmai 1:29de429a7e15 636 uLCD.printf("push button pressed - receive schedule\n");
deronmai 1:29de429a7e15 637 receive_schedule();
deronmai 4:ae6216936ddf 638 }*/
deronmai 4:ae6216936ddf 639
deronmai 4:ae6216936ddf 640 void pb1_hit_interrupt (void)
deronmai 4:ae6216936ddf 641 {
ashea6 7:d85521f4f0c3 642 //uLCD.printf("push button 1 pressed - send schedule\n");
deronmai 4:ae6216936ddf 643 //receive_schedule();
deronmai 4:ae6216936ddf 644 packdata(1);
deronmai 4:ae6216936ddf 645 }
deronmai 4:ae6216936ddf 646
ashea6 7:d85521f4f0c3 647 //what does this function do if it only wrote to the uLCD?
deronmai 4:ae6216936ddf 648 void pb2_hit_interrupt (void)
deronmai 4:ae6216936ddf 649 {
ashea6 7:d85521f4f0c3 650 //uLCD.printf("push button 2 pressed - cognitive game\n");
deronmai 4:ae6216936ddf 651 }
deronmai 4:ae6216936ddf 652
deronmai 4:ae6216936ddf 653
deronmai 4:ae6216936ddf 654 void cognitivegame()
deronmai 4:ae6216936ddf 655 {
deronmai 4:ae6216936ddf 656 /*
deronmai 4:ae6216936ddf 657 vector<string> wordset1;
deronmai 4:ae6216936ddf 658 vector<string> wordset2;
deronmai 4:ae6216936ddf 659 vector<string> wordset3;
deronmai 4:ae6216936ddf 660 vector<string> wordset4;
deronmai 4:ae6216936ddf 661 vector<string> wordset5;
deronmai 4:ae6216936ddf 662 */
deronmai 4:ae6216936ddf 663 int correct = 0;
deronmai 4:ae6216936ddf 664 int timesplayed;
ashea6 7:d85521f4f0c3 665 //uLCD.printf("Speak: Your recall wordset will now play\n");
deronmai 4:ae6216936ddf 666 // speak(recall game)
deronmai 4:ae6216936ddf 667 wait(3);
deronmai 4:ae6216936ddf 668
deronmai 4:ae6216936ddf 669 int wordset = rand() % 5; // pick random wordset between 1 and 5
deronmai 4:ae6216936ddf 670
ashea6 7:d85521f4f0c3 671 //uLCD.printf("Playing wordset %i\n", wordset);
deronmai 4:ae6216936ddf 672 switch(wordset) {
deronmai 4:ae6216936ddf 673 case 0: {
deronmai 4:ae6216936ddf 674 //speak(wordset1);
deronmai 4:ae6216936ddf 675 break;
deronmai 4:ae6216936ddf 676 }
deronmai 4:ae6216936ddf 677 case 1: {
deronmai 4:ae6216936ddf 678 //speak(wordset2);
deronmai 4:ae6216936ddf 679 break;
deronmai 4:ae6216936ddf 680 }
deronmai 4:ae6216936ddf 681 case 2: {
deronmai 4:ae6216936ddf 682 //speak(wordset3);
deronmai 4:ae6216936ddf 683 break;
deronmai 4:ae6216936ddf 684 }
deronmai 4:ae6216936ddf 685 case 3: {
deronmai 4:ae6216936ddf 686 //speak(wordset4);
deronmai 4:ae6216936ddf 687 break;
deronmai 4:ae6216936ddf 688 }
deronmai 4:ae6216936ddf 689 case 4: {
deronmai 4:ae6216936ddf 690 //speak(wordset5);
deronmai 4:ae6216936ddf 691 break;
deronmai 4:ae6216936ddf 692 }
deronmai 4:ae6216936ddf 693 }
deronmai 4:ae6216936ddf 694
ashea6 7:d85521f4f0c3 695 //uLCD.printf("Was _____ the first word in the set?\n");
ashea6 7:d85521f4f0c3 696 //uLCD.printf("Was _____ the last word in the set?\n");
deronmai 4:ae6216936ddf 697 }