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