main reminder functionality

Dependencies:   mbed 4DGL-uLCD-SE SDFileSystem

Committer:
deronmai
Date:
Tue Dec 06 23:32:59 2016 +0000
Revision:
6:ce8d4456e28b
Parent:
5:69da174d0f56
Child:
7:d85521f4f0c3
updated

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