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