main reminder functionality

Dependencies:   mbed 4DGL-uLCD-SE SDFileSystem

Committer:
deronmai
Date:
Tue Dec 06 03:43:30 2016 +0000
Revision:
4:ae6216936ddf
Parent:
3:3eda3d120742
Child:
5:69da174d0f56
Push buttons and cognitive game

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