-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
1181 lines (1089 loc) · 30.7 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <iostream>
#include <fstream>
#include <ctime>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <sstream>
#include <string>
#include <math.h>
//#include <oled-exp.h>
//#include <onion-i2c.h>
using namespace std;
int displayWeather(string time, string date);
int displayStats(string time, string date);
int display(string str);
string getWeather();
string getTemperature();
string getStatistics();
template <typename T>
string NumberToString ( T Number ){
ostringstream ss;
ss << Number;
return ss.str();
}
int StringToInt(string line){
int temp;
stringstream ss(line);
ss >> temp;
return temp;
}
class GPIO{
public:
void addPin(const char pin[]);
void removePin(const char pin[]);
void writeInOut(const char pin[], const int pinSize, const char direction[]);
void writeValue(const char pin[], const int pinSize,const char value);
void readValue(const char pin[], const int pinSize, char value[2]);
GPIO(); //constructor
~GPIO(); //destructor
};
void GPIO::addPin(const char pin[]){
ofstream outfile;
outfile.open("/sys/class/gpio/export");
if (!outfile.is_open()){
cout << "file did not open" << endl;
} else {
outfile << pin;
//cout << "Pin " << pin << " added" << endl;
}
outfile.close();
}
void GPIO::removePin(const char pin[]){
ofstream outfile;
outfile.open("/sys/class/gpio/unexport");
if (!outfile.is_open()){
cout << "file did not open" << endl;
} else {
outfile << pin;
//cout << "Pin " << pin << " removed" << endl;
}
}
void GPIO::writeInOut(const char pin[], const int pinSize, const char direction[]){
ofstream outfile;
char filename[pinSize+31]="/sys/class/gpio/gpio";
char end[11]="/direction";
for (int i=20;i<pinSize+31;i++){
if (i<pinSize+20){
filename[i]=pin[i-20];
} else if (i<pinSize+30){
filename[i]=end[i-pinSize-20];
} else {
filename[i]=0;
}
}
//cout << filename << endl;
outfile.open(filename); //Open file
if (!outfile.is_open()){
cout << "file did not open" << endl;
} else {
outfile << direction;
//cout << "Pin " << pin << " direction: " << direction << endl;
}
outfile.close(); //Close
}
void GPIO::writeValue(const char pin[], const int pinSize,const char value){
ofstream outfile;
char filename[pinSize+27]="/sys/class/gpio/gpio";
char end[7]="/value";
for (int i=20;i<pinSize+27;i++){
if (i<pinSize+20){
filename[i]=pin[i-20];
} else if (i<pinSize+27){
filename[i]=end[i-pinSize-20];
} else {
filename[i]=0;
}
}
outfile.open(filename); //Open file
if (!outfile.is_open()){
cout << "file did not open" << endl;
} else {
outfile << value;
//cout << "Pin " << pin << " status: " << value << endl;
}
outfile.close(); //Close
}
void GPIO::readValue(const char pin[], const int pinSize, char value[2]){
ifstream infile;
char filename[pinSize+27]="/sys/class/gpio/gpio";
char end[7]="/value";
for (int i=20;i<pinSize+27;i++){
if (i<pinSize+20){
filename[i]=pin[i-20];
} else if (i<pinSize+27){
filename[i]=end[i-pinSize-20];
} else {
filename[i]=0;
}
}
infile.open(filename);
if (!infile.is_open()){
cout << "file did not open" << endl;
} else {
char line[2];
int flag=0;
while(flag<1){ //Read File
infile.getline(line,2);
//cout << "line: " << line << endl;
for (int i=0;i<2;i++){
value[i]=line[i];
}
flag++;
}
//cout << "Pin " << pin << " status: " << value << endl;
}
}
string getTime(int& hour, int& minute){
//Get Time
time_t timeNow;
struct tm * timeinfo;
char buffer[80];
time (&timeNow);
timeinfo = localtime(&timeNow);
strftime(buffer,sizeof(buffer),"%I:%M:%S",timeinfo);
string str(buffer);
char buffer1[80];
time (&timeNow);
timeinfo = localtime(&timeNow);
strftime(buffer1,sizeof(buffer1),"%I",timeinfo);
string hours(buffer1);
hour=StringToInt(hours);
char buffer2[80];
time (&timeNow);
timeinfo = localtime(&timeNow);
strftime(buffer2,sizeof(buffer2),"%M",timeinfo);
string minutes(buffer2);
minute=StringToInt(minutes);
return str;
}
string getDate(int& month, int& day){
time_t timeNow;
struct tm * timeinfo;
char buffer[80];
time (&timeNow);
timeinfo = localtime(&timeNow);
strftime(buffer,sizeof(buffer),"%Y/%m/%d",timeinfo);
string str(buffer);
char buffer1[80];
time (&timeNow);
timeinfo = localtime(&timeNow);
strftime(buffer1,sizeof(buffer1),"%m",timeinfo);
string months(buffer1);
month=StringToInt(months);
char buffer2[80];
time (&timeNow);
timeinfo = localtime(&timeNow);
strftime(buffer2,sizeof(buffer2),"%d",timeinfo);
string days(buffer2);
day=StringToInt(days);
return str;
}
class Alarm{
private:
int hour;
int minute;
string time;
public:
void addHour();
void addMinute();
void setAlarm();
string getTime();
int getHour();
int getMinute();
Alarm(); //constructor
~Alarm(); //destructor
};
void Alarm::addHour(){
int temp=hour;
temp++;
if (temp>24){
temp=0;
}
hour=temp;
}
void Alarm::addMinute(){
int temp=minute;
temp++;
if (temp>60){
temp=0;
}
minute=temp;
}
void Alarm::setAlarm(){
string hours;
if (hour<10){
hours="0"+NumberToString(hour);
} else {
hours=NumberToString(hour);
}
string minutes;
if (minute<10){
minutes="0"+NumberToString(minute);
} else {
minutes=NumberToString(minute);
}
time=hours+":"+minutes;
}
string Alarm::getTime(){
return time;
}
int Alarm::getHour(){
return hour;
}
int Alarm::getMinute(){
return minute;
}
Alarm::Alarm(){
hour=0;
minute=0;
time="00:00:00";
}
float hoursSlept(int sleepStartHour, int sleepStartMinute, int sleepEndHour, int sleepEndMinute,
int sleepStartMonth, int sleepStartDay, int sleepEndMonth, int sleepEndDay){
//If the user sleeps and wakes up on the same date
float sleepMinutes=0;
int daysPassed=sleepEndDay-sleepStartDay;
int monthsPassed=sleepEndMonth-sleepStartMonth;
int startMinutes=(sleepStartHour*60+sleepStartMinute);
int endMinutes=(sleepEndHour*60+sleepEndMinute);
if (daysPassed==0 && monthsPassed==0){
sleepMinutes=endMinutes-startMinutes;
} else if (daysPassed>0 && monthsPassed==0){
sleepMinutes=24*60*daysPassed-startMinutes+endMinutes;
} else if (monthsPassed>0){
//Assuming they dont sleep for a month
sleepMinutes=24*60-startMinutes;
daysPassed=sleepEndDay-1;
sleepMinutes+=24*60*daysPassed+endMinutes;
}
return sleepMinutes/60;
}
void writeStatFile(float hoursSlept, int hour, int minute, string date){
ofstream outfile;
string line;
hour = hour-21;
string hourLine;
if (abs(hour)<10 && hour<0){
hourLine="-0"+NumberToString(abs(hour));
} else if (abs(hour)>10 && hour<0){
hourLine="-0"+NumberToString(abs(hour));
} else if (hour>=10){
hourLine=NumberToString(hour);
} else if (hour>=0 && hour <10){
hourLine="0"+NumberToString(hour);
}
string minuteLine;
if (minute<10){
minuteLine="0"+NumberToString(minute);
} else {
minuteLine=NumberToString(minute);
}
stringstream stream;
stream << fixed << setprecision(2) << hoursSlept;
string temp = stream.str();
line = temp + "," + hourLine + ":" + minuteLine + "," + date;
cout << line << endl;
outfile.open("SleepInfo.txt");
if (!outfile.is_open()){
cout << "file did not open" << endl;
} else {
outfile << line;
}
outfile.close();
}
//The optimal sleep time for healthy adults (18+) is 7.5-8.5 hours, says
//http://healthysleep.med.harvard.edu/need-sleep/what-can-you-do/assess-needs
// Struct recording data for each month
struct Month {
int monthNum = 0;
int numOfDays;
};
// Struct serving as array for month struct
struct Year {
int numOfMonths = 0;
Month* months;
};
// Struct recording data for each night slept
struct DailyStats {
float hrsSlept;
char timeSlept[5];
char date[10];
int combTime;
int combDate;
int minutes;
};
// Strut collecting statistics for one week of sleep
struct WeeklyStats {
float minHrsSlept;
float avgHrsSlept;
float maxHrsSlept;
float stdDev;
string earliestTimeSlept;
string latestTimeSlept;
string avgTimeSlept;
};
// Node struct
struct Node {
DailyStats dStats;
Node* next;
};
// Method declaration
string rewriteTime(int time);
bool addNode(Node*& list, const DailyStats& dayRecords);
bool deleteNode(Node*& list, const int day);
void displayList(const Node* list);
DailyStats* get(Node* list, int index);
int testMethod(const char filename[], DailyStats ds, Node*& records, int& daysRecorded);
int computeStatistics(Node*& records, WeeklyStats& ws, const int daysRecorded);
int writeFile(const char filename[], const WeeklyStats& ws);
// Method for adding node to list
bool addNode(Node*& list, const DailyStats& dayRecords) {
Node* newNode = new Node;
if (newNode) {
newNode->dStats = dayRecords;
newNode->next = list;
list = newNode;
return true;
}
return false;
}
// Method for removing node from list
bool deleteNode(Node*& list, const int day) {
Node* cookie = list;
if (cookie == 0)
return false;
if (cookie->dStats.combDate == day) {
list = list->next;
delete cookie;
return true;
}
while (cookie->next) {
if (cookie->next->dStats.combDate == day) {
Node* tmp = cookie->next;
cookie->next = cookie->next->next;
delete tmp;
return true;
}
cookie = cookie->next;
}
return false;
}
void displayList(const Node* list) {
const Node* cookie = list;
int i = 0;
while (cookie) {
cout << i << ": " << cookie->dStats.combTime << " --> ";
cookie = cookie->next;
i++;
}
cout << "EOL" << endl;
}
DailyStats* get(Node* list, int index) {
Node* cookie = list;
int counter = 0;
if (index == 0) {
return &(cookie->dStats);
}
while (cookie->next) {
cookie = cookie->next;
counter++;
if (counter == index) {
return &(cookie->dStats);
}
}
return NULL;
}
int testMethod(DailyStats ds, Node*& records, int& daysRecorded) {
const int maxLineLength = 100;
char line[maxLineLength];
ifstream infile;
infile.open("SleepInfo.txt");
if (!infile.is_open()) {
return -1;
}
bool done = false;
int fileLineNumber = 0;
if(!infile.getline(line, maxLineLength)) {
if (infile.eof()) {
done = true;
}
else
return -1;
}
while (!done) {
float tempHrsSlept = 0;
float tempHSAfterDec = 0;
int tempBedTime = 0;
int tempHour = 0;
int tempMinute = 0;
int minutesFromRef = 0;
int cMinutesFromRef = 0;
int tempDate = 0;
int tempYear = 0;
int tempMonth = 0;
int tempDay = 0;
int sign = 1;
bool comma1 = false;
bool comma2 = false;
bool decCheck = false;
bool colonCheck = false;
bool slash1 = false;
bool slash2 = false;
for (int i = 0; line[i] != 0; i++) {
if (line[i] != ',' && !comma1 && !comma2) { // Detect hours slept
if (line[i] == '.') {
decCheck = true;
}
else if (line[i] >= '0' && line[i] <= '9' && !decCheck) {
tempHrsSlept = tempHrsSlept*10 + line[i] - '0';
}
else if (line[i] >= '0' && line[i] <= '9' && decCheck) {
tempHSAfterDec = tempHSAfterDec*10 + line[i] - '0';
}
tempHrsSlept = tempHrsSlept + (tempHSAfterDec/10);
}
else if (line[i] == ',' && !comma1 && !comma2) { // Detect comma after hours slept
comma1 = true;
}
else if (line[i] != ',' && comma1 && !comma2) { // Detect bedtime -- Note that the bedtime is relative to 9 PM. (Ex. 0311 = 12:11 AM)
if (line[i] >= '0' && line[i] <= '9' && !colonCheck) {
tempHour = tempHour*10 + line[i] - '0';
tempBedTime = tempBedTime*10 + line[i] - '0';
minutesFromRef = minutesFromRef*10 + line[i] - '0';
}
else if (line[i] >= '0' && line[i] <= '9' && colonCheck) {
tempMinute = tempMinute*10 + line[i] - '0';
tempBedTime = tempBedTime*10 + line[i] - '0';
cMinutesFromRef = cMinutesFromRef*10 + line[i] - '0';
}
else if (line[i] == '-') {
sign = -1;
}
else if (line[i] == ':') {
colonCheck = true;
}
}
else if (line[i] == ',' && comma1 && !comma2) { // Detect comma after bedtime
comma2 = true;
}
else if (line[i] != ',' && comma1 && comma2) { // Detect date
if (line[i] >= '0' && line[i] <= '9' && !slash1 && !slash2) {
tempYear = tempYear*10 + line[i] - '0';
tempDate = tempDate*10 + line[i] - '0';
}
else if (line[i] >= '0' && line[i] <= '9' && slash1 && !slash2) {
tempMonth = tempMonth*10 + line[i] - '0';
tempDate = tempDate*10 + line[i] - '0';
}
else if (line[i] >= '0' && line[i] <= '9' && slash1 && slash2) {
tempDay = tempDay*10 + line[i] - '0';
tempDate = tempDate*10 + line[i] - '0';
}
else if (line[i] == '/' && !slash1 && !slash2) {
slash1 = true;
}
else if (line[i] == '/' && slash1 && !slash2) {
slash2 = true;
}
}
}
char tBedTime[5];
char tDate[10];
//char array for bedtime
if (tempHour < 10) {
tBedTime[0] = '0';
}
else {
tBedTime[0] = tempHour / 10 + '0';
}
tBedTime[1] = tempHour % 10;
tBedTime[2] = ':';
if (tempMinute < 10) {
tBedTime[3] = '0';
}
else {
tBedTime[3] = tempMinute / 10 + '0';
}
tBedTime[4] = tempMinute % 10;
//char array for date
tDate[0] = '2';
for (int i = 1; i < 4; i++) {
tDate[i] = (int)(tempYear/pow(10, 3-i)) % (int)(tempYear/pow(10, 4-i)) + '0';
}
tDate[5] = '/';
if (tempMonth < 10) {
tDate[6] = '0';
tDate[7] = tempMonth + '0';
}
else {
tDate[6] = '1';
tDate[7] = tempMonth % 10 + '0';
}
tDate[8] = '/';
if (tempDay < 10) {
tDate[9] = '0';
tDate[10] = tempDay + '0';
}
else {
tDate[9] = tempDay / 10 + '0';
tDate[10] = tempDay % 10 + '0';
}
//
for (int i = 0; i < 5; i++) {
ds.timeSlept[i] = tBedTime[i];
}
for (int i = 0; i < 10; i++) {
ds.date[i] = tDate[i];
}
minutesFromRef = (minutesFromRef*60 + cMinutesFromRef)*sign;
tempBedTime = tempBedTime*sign;
ds.combTime = tempBedTime;
ds.hrsSlept = tempHrsSlept;
ds.combDate = tempDate;
ds.minutes = minutesFromRef;
addNode(records, ds);
daysRecorded++;
++fileLineNumber;
if (!infile.getline(line, maxLineLength)) {
if (infile.eof()) {
done = true;
}
else {
return -1;
}
}
}
}
int computeStatistics(Node*& records, WeeklyStats& ws, const int daysRecorded) {
if (daysRecorded == 0) {
return -1;
}
float tempMin = get(records, 0)->hrsSlept;
float tempMax = get(records, 0)->hrsSlept;
float sumHrsSlept = get(records, 0)->hrsSlept;
for (int i = 1; i < daysRecorded; i++) {
if (tempMin > get(records, i)->hrsSlept) {
tempMin = get(records,i)->hrsSlept;
}
if (tempMax < get(records, i)->hrsSlept) {
tempMax = get(records, i)->hrsSlept;
}
sumHrsSlept += get(records, i)->hrsSlept;
}
ws.minHrsSlept = tempMin;
ws.maxHrsSlept = tempMax;
ws.avgHrsSlept = sumHrsSlept/daysRecorded;
int earliest = get(records, 0)->combTime;
int latest = get(records, 0)->combTime;
int sumTimeSlept = get(records, 0)->combTime;
int minSum = get(records, 0)->minutes;
float msAverage = 0;
float dSum = 0;
int averageTS = 0;
for (int i = 1; i < daysRecorded; i++) {
if (earliest > get(records, i)->combTime) {
earliest = get(records, i)->combTime;
}
if (latest < get(records, i)->combTime) {
latest = get(records, i)->combTime;
}
sumTimeSlept += get(records, i)->combTime;
minSum += get(records, i)->minutes;
}
averageTS = sumTimeSlept/daysRecorded;
msAverage = minSum/daysRecorded;
for (int i = 0; i < daysRecorded; i++) {
dSum += pow((get(records, i)-> minutes - msAverage),2);
}
ws.stdDev = sqrt(dSum/daysRecorded);
ws.earliestTimeSlept = rewriteTime(earliest);
ws.latestTimeSlept = rewriteTime(latest);
ws.avgTimeSlept = rewriteTime(averageTS);
return 0;
}
int addToList(Node*& records, DailyStats ds, int currentDate, int currentTime) { // run method when alarm is turned off
int snoozeCounter;
}
int writeFile(const WeeklyStats& ws) {
ofstream outfile;
outfile.open("SleepStatistics.txt");
if (!outfile.is_open()) {
return -1;
}
outfile << "Min: " << ws.minHrsSlept << "hrs \r\n" << endl;
outfile << "Max: " << ws.maxHrsSlept << "hrs \r\n" << endl;
outfile << "Avg: " << ws.avgHrsSlept << "hrs \r\n" << endl;
//outfile << "Earliest BT: " << ws.earliestTimeSlept << "\n" << endl;
//outfile << "Latest BT: " << ws.latestTimeSlept << "\n" << endl;
//outfile << "Avg BT: " << ws.avgTimeSlept << "\r\n" << endl;
//outfile << "Deviation: " << ws.stdDev << "\r\n" << endl;
if (ws.avgHrsSlept < 7.5) {
outfile << "Not Well :(" << endl;
}
else if (ws.avgHrsSlept > 7.5 && ws.avgHrsSlept < 8.5) {
outfile << "Well :)" << endl;
}
else if (ws.avgHrsSlept > 8.5) {
outfile << "Too Much!" << endl;
}
outfile.close();
return 0;
}
string rewriteTime(int time) {
bool pos = true;
if (time < 0) {
pos = false;
}
char newTime[5];
time += 2100;
if (time >= 2400) {
time -= 2400;
}
if (!pos) {
if (time % 100 != 0) {
time = (time / 100) * 100 + (time % 100) - 40;
}
else {
time = (time / 100) * 100;
}
}
int refMins = 60;
int misc = 60 - (time % 100);
for (int i = 0; i < 5; i++) {
if (time < 1000) {
newTime[0] = '0';
if (time < 100) {
newTime[1] = '0';
if (time < 10) {
newTime[3] = '0';
if (time < 1) {
newTime[4] = '0';
}
else {
newTime[4] = time + '0';
}
}
else { // else < 10
newTime[3] = (time / 10) + '0';
newTime[4] = (time % 10) + '0';
}
}
else { // else < 100
newTime[1] = (time/100) + '0';
newTime[3] = ((time / 10) % 10) + '0';
newTime[4] = (time % 10) + '0';
}
}
else { // else < 1000
newTime[0] = time/1000 + '0';
newTime[1] = ((time/100) % 10) + '0';
newTime[3] = ((time/10) % 10) + '0';
newTime[4] = (time % 10) + '0';
}
}
newTime[2] = ':';
return newTime;
}
string displayWeather() {
if (getTemperature() == "" || getWeather() == "") {
//display("Error reading from file");
return NULL;
}
string str = "Temp: " + getTemperature() + " degrees Weather: " + getWeather();
return str;
}
string getWeather() {
// get line from HTML file
ifstream infile;
infile.open("weather.txt");
if (!infile.is_open()) {
return "";
}
const int length = 1000;
char line[length];
if (!infile.getline(line, length)) {
return "";
}
infile.close();
// find weather condition in text
string weather;
int j = 0;
for (int i = 0; i < length; i++) {
if (line[i - 5] == 'm' && line[i - 4] == 'a' && line[i - 3] == 'i' && line[i - 2] == 'n' && line[i + 1] == '"') {
bool done = false;
while (!done) {
weather += line[i + j + 2];
j++;
if (line[i + j + 2] == '"') {
done = true;
}
}
}
}
return weather;
}
string getTemperature() {
// get line from HTML file
ifstream infile;
infile.open("weather.txt");
if (!infile.is_open()) {
return "";
}
const int length = 1000;
char line[length];
if (!infile.getline(line, length)) {
return "";
}
infile.close();
// find temperature in text
string temperature;
int j = 0;
for (int i = 0; i < length; i++) {
if (line[i - 5] == 't' && line[i - 4] == 'e' && line[i - 3] == 'm' && line[i - 2] == 'p' && line[i] == ':') {
bool done = false;
while (!done) {
temperature += line[i + j + 1];
j++;
if (line[i + j + 1] == ',') {
done = true;
}
}
}
}
return temperature;
}
int getStatistics(string& str0,string& str1,string& str2,string& str3,string& str4,string& str5) {
// get line from HTML file
ifstream infile;
infile.open("SleepStatistics.txt");
if (!infile.is_open()) {
return -1;
}
const int length = 1000;
char line[length];
bool done = false;
int i=0;
while (!done) {
if (!infile.getline(line, length)) {
if (infile.eof()) {
done = true;
} else {
return -1;
}
}
if (i==0){
str0+= line;
} else if (i==1){
str1+=line;
} else if (i==2){
str2+=line;
} else if (i==3){
str3+=line;
} else if (i==4){
str4+=line;
} else if (i==5){
str5+=line;
}
i++;
}
infile.close();
return 0;
}
int main(int args,char* argv[]){
//declare my GPIO
GPIO myPin;
//Reset pins
myPin.removePin("0");
myPin.removePin("1");
myPin.removePin("2");
myPin.removePin("3");
myPin.removePin("6");
//Add output pins (6)
myPin.addPin("6");
myPin.writeInOut("6",1,"out");
//Add snooze button input pin(1)
myPin.addPin("1");
myPin.writeInOut("1",1,"in");
//Add weight sensing input pin(3)
myPin.addPin("3");
myPin.writeInOut("3",1,"in");
char value0[2]; //tracks value of pin0
char value1[2]; //tracks value of pin1
char value2[2]; //tracks value of pin2
char value3[2]; //tracks value of pin3
//Decalare Alarm Time
Alarm myAlarm;
bool alarmReset=false;
//Timer loop start
const int NUM_SECONDS=1;
int count=1;
int valueCount1=0;
double time_counter=0;
clock_t startTime = clock();
clock_t last_time = startTime;
//ONLY FOR DEMO PURPOSES
int secondsToDelay=atoi(argv[1]);
//Boolean variables
bool flag=true;
bool held=false;
bool onBed=false;
bool alarmOn=false;
//Sleep variables
string sleepStartTime;
string sleepEndTime;
string sleepStartDate;
string sleepEndDate;
int sleepStartHour;
int sleepStartMinute;
int sleepEndHour;
int sleepEndMinute;
int sleepStartDay;
int sleepEndDay;
int sleepStartMonth;
int sleepEndMonth;
int alarmCounter=31;
int currentHour;
int currentMinute;
int currentDay;
int currentMonth;
char init [100]={};
string previousTime=getTime(currentHour,currentMinute).substr(0,5);
strcpy(init,(getTime(currentHour,currentMinute).substr(0,5) + " " + getDate(currentMonth,currentDay)).c_str());
char* initPointer = init;
int displayFlag=false;
int displayCounter=0;
int onBedCounter=0;
//Call screen update
oledDriverInit(); //initiate the oled expansion screen
oledClear();
oledWrite(initPointer);
Node* records = 0;
//Start timer loop
while(flag){
startTime=clock();
time_counter+=(double)(startTime-last_time);
last_time=startTime;
if (time_counter>(double)(NUM_SECONDS*CLOCKS_PER_SEC)){
time_counter-=(double)(NUM_SECONDS*CLOCKS_PER_SEC);
count++;
myPin.readValue("1",1,value1); //Read the value of pin1 (input)
myPin.readValue("3",1,value3); //Read the value of pin3 (input)
cout << getTime(currentHour,currentMinute) << endl;
cout << getDate(currentMonth,currentDay) << endl;
//Update the time if the current time on OLED display if there is a change in minutes and the alarm is not on and the user is not currently
//reseting the alarm and the display has been shown for longer than 10 seconds
if (getTime(currentHour,currentMinute).substr(0,5) != previousTime && alarmOn==false && alarmReset==false && displayCounter>10){
char temp [100]={};
strcpy(temp,(getTime(currentHour,currentMinute).substr(0,5) + " " + getDate(currentMonth,currentDay)).c_str());
char* pointer = temp;
//Call screen update
oledScrollStop ();
oledClear();
oledWrite(pointer);
}
previousTime = getTime(currentHour,currentMinute).substr(0,5); //set previous time
if (onBed){ //The alarm can only be set when the user is on the bed
//Alarm goes off the current time is equal to the set alarm time
if (myAlarm.getHour()==currentHour && myAlarm.getMinute()==currentMinute && alarmCounter>10){
cout << "ring ring!" << endl;
oledClear();
oledScrollStop ();
char alarmText[11] = "ring ring!";
char* alarmPointer=alarmText;
oledWrite(alarmPointer);
alarmOn=true; //value signalling that the alarm is on
}
if (alarmOn==true && atoi(value1)==1){ //If the snooze button is pressed when the alarm is going off, allow snooze of 10s
alarmCounter=0;
oledClear();
char snoozeText[26] = "Zzzzzzzzzzzzzzzzzzzzz";
char* snoozePointer=snoozeText;
oledWrite(snoozePointer);
oledScroll(0, OLED_EXP_SCROLL_SPEED_5_FRAMES, 0, OLED_EXP_CHAR_ROWS - 1);
}
}
if (alarmOn){ //if the alarm is currently on increase the counter, the only way to turn off the alarm is to get off the bed
alarmCounter++;
}