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