example using the course class

Dependencies:   4DGL-uLCD-SE Course mbed SDFileSystem

Committer:
kkizirian
Date:
Wed Dec 07 22:41:57 2016 +0000
Revision:
5:77eafa8aaec6
Parent:
4:d4a652a577a0
Fixed course 5 declaration

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kkizirian 0:6ced83c58574 1 #include "mbed.h"
kkizirian 0:6ced83c58574 2 #include "Course.h"
kkizirian 0:6ced83c58574 3 #include <string>
kkizirian 0:6ced83c58574 4 #include <vector>
kkizirian 0:6ced83c58574 5 #include "uLCD_4DGL.h"
kkizirian 4:d4a652a577a0 6 #include "SDFileSystem.h"
kkizirian 0:6ced83c58574 7
kkizirian 4:d4a652a577a0 8 SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board
kkizirian 0:6ced83c58574 9 uLCD_4DGL uLCD(p9,p10,p11); // serial tx, serial rx, reset pin;
kkizirian 0:6ced83c58574 10 vector<Course> courseVec;
kkizirian 4:d4a652a577a0 11 vector<Course> readingCourseVec;
kkizirian 0:6ced83c58574 12 int addCourseToVector(vector<Course>& cVec, Course newCourse);
kkizirian 4:d4a652a577a0 13 void writeClassFile(vector<Course>& cVec), readClassFile(vector<Course>& cVec);
kkizirian 0:6ced83c58574 14
kkizirian 0:6ced83c58574 15 int main() {
kkizirian 4:d4a652a577a0 16 Course course1("COC", 6, 5, "PM");
kkizirian 4:d4a652a577a0 17 Course course2("CLH", 8, 25, "AM");
kkizirian 4:d4a652a577a0 18 Course course3("VAN", 8, 15, "PM");
kkizirian 4:d4a652a577a0 19 Course course4("KLS", 8, 05, "PM");
kkizirian 5:77eafa8aaec6 20 Course course5("VAN", 8, 05, "PM");
kkizirian 4:d4a652a577a0 21
kkizirian 3:4984f0a32f32 22
kkizirian 3:4984f0a32f32 23 int test = 0;
kkizirian 3:4984f0a32f32 24 test = addCourseToVector(courseVec, course1);
kkizirian 3:4984f0a32f32 25 test = addCourseToVector(courseVec, course2);
kkizirian 3:4984f0a32f32 26 test = addCourseToVector(courseVec, course3);
kkizirian 3:4984f0a32f32 27 test = addCourseToVector(courseVec, course4);
kkizirian 3:4984f0a32f32 28 test = addCourseToVector(courseVec, course5);
kkizirian 3:4984f0a32f32 29 if (test == 0)
kkizirian 3:4984f0a32f32 30 uLCD.printf("uh oh!");
kkizirian 4:d4a652a577a0 31
kkizirian 4:d4a652a577a0 32 mkdir("/sd/classdir", 0777);
kkizirian 3:4984f0a32f32 33
kkizirian 4:d4a652a577a0 34 writeClassFile(courseVec);
kkizirian 4:d4a652a577a0 35 readClassFile(readingCourseVec);
kkizirian 4:d4a652a577a0 36
kkizirian 0:6ced83c58574 37 uLCD.cls();
kkizirian 4:d4a652a577a0 38 uLCD.locate(0, 1);
kkizirian 4:d4a652a577a0 39 uLCD.printf("Display readingCourseVec..");
kkizirian 4:d4a652a577a0 40 for (int i = 0; i < readingCourseVec.size(); i++) {
kkizirian 4:d4a652a577a0 41 uLCD.locate(0, i+2);
kkizirian 4:d4a652a577a0 42 uLCD.printf("%s", readingCourseVec[i].getDisplayString());
kkizirian 3:4984f0a32f32 43 }
kkizirian 4:d4a652a577a0 44
kkizirian 0:6ced83c58574 45 }
kkizirian 0:6ced83c58574 46
kkizirian 0:6ced83c58574 47 int addCourseToVector(vector<Course>& cVec, Course newCourse) {
kkizirian 3:4984f0a32f32 48 int numIterations = 0;
kkizirian 3:4984f0a32f32 49
kkizirian 0:6ced83c58574 50 if (cVec.size() == 0) {
kkizirian 0:6ced83c58574 51 cVec.push_back(newCourse);
kkizirian 0:6ced83c58574 52 return 1;
kkizirian 0:6ced83c58574 53 }
kkizirian 0:6ced83c58574 54
kkizirian 0:6ced83c58574 55 for (int i = 0; i < cVec.size(); i++) {
kkizirian 4:d4a652a577a0 56 numIterations++;
kkizirian 0:6ced83c58574 57 if (cVec[i].getAMPM_toInt() < newCourse.getAMPM_toInt())
kkizirian 0:6ced83c58574 58 continue;
kkizirian 0:6ced83c58574 59 else if (newCourse.getAMPM_toInt() < cVec[i].getAMPM_toInt()) {
kkizirian 0:6ced83c58574 60 cVec.insert(cVec.begin()+i, newCourse);
kkizirian 0:6ced83c58574 61 return 1;
kkizirian 0:6ced83c58574 62 }
kkizirian 0:6ced83c58574 63 else if (cVec[i].getAMPM_toInt() == newCourse.getAMPM_toInt()) {
kkizirian 4:d4a652a577a0 64 if (cVec[i].getHour_forCompare() < newCourse.getHour_forCompare())
kkizirian 0:6ced83c58574 65 continue;
kkizirian 4:d4a652a577a0 66 else if (newCourse.getHour_forCompare() < cVec[i].getHour_forCompare()) {
kkizirian 0:6ced83c58574 67 cVec.insert(cVec.begin()+i, newCourse);
kkizirian 0:6ced83c58574 68 return 1;
kkizirian 0:6ced83c58574 69 }
kkizirian 4:d4a652a577a0 70 else if (cVec[i].getHour_forCompare() == newCourse.getHour_forCompare()) {
kkizirian 0:6ced83c58574 71 if (cVec[i].getMinute() < newCourse.getMinute())
kkizirian 0:6ced83c58574 72 continue;
kkizirian 0:6ced83c58574 73 else if (newCourse.getMinute() < cVec[i].getMinute()) {
kkizirian 0:6ced83c58574 74 cVec.insert(cVec.begin()+i, newCourse);
kkizirian 0:6ced83c58574 75 return 1;
kkizirian 0:6ced83c58574 76 }
kkizirian 4:d4a652a577a0 77 else if (cVec[i].getMinute() == newCourse.getMinute()) {
kkizirian 4:d4a652a577a0 78 uLCD.cls();
kkizirian 4:d4a652a577a0 79 uLCD.locate(0,0);
kkizirian 4:d4a652a577a0 80 uLCD.printf("Can not add coure!");
kkizirian 4:d4a652a577a0 81 uLCD.locate(0,1);
kkizirian 4:d4a652a577a0 82 uLCD.printf("Course already at");
kkizirian 4:d4a652a577a0 83 uLCD.locate(0,2);
kkizirian 4:d4a652a577a0 84 uLCD.printf("%i:%s%s", newCourse.getHour(), newCourse.getMinute_toString(), newCourse.getAMPM());
kkizirian 4:d4a652a577a0 85 wait(5);
kkizirian 0:6ced83c58574 86 return 0;
kkizirian 4:d4a652a577a0 87 }
kkizirian 0:6ced83c58574 88 }
kkizirian 0:6ced83c58574 89 }
kkizirian 0:6ced83c58574 90 }
kkizirian 4:d4a652a577a0 91
kkizirian 3:4984f0a32f32 92 if (numIterations == cVec.size()) {
kkizirian 3:4984f0a32f32 93 cVec.push_back(newCourse);
kkizirian 3:4984f0a32f32 94 return 1;
kkizirian 3:4984f0a32f32 95 }
kkizirian 4:d4a652a577a0 96
kkizirian 0:6ced83c58574 97 return 0;
kkizirian 0:6ced83c58574 98 }
kkizirian 4:d4a652a577a0 99
kkizirian 4:d4a652a577a0 100 void writeClassFile(vector<Course>& cVec) {
kkizirian 4:d4a652a577a0 101 FILE *writeClass = fopen("/sd/classdir/classes.txt", "w");
kkizirian 4:d4a652a577a0 102 if (writeClass != NULL) {
kkizirian 4:d4a652a577a0 103 string line = "";
kkizirian 4:d4a652a577a0 104 for (int i = 0; i < cVec.size(); i++) {
kkizirian 4:d4a652a577a0 105 if (i != (cVec.size() - 1))
kkizirian 4:d4a652a577a0 106 line = cVec[i].getFileString() + "\n";
kkizirian 4:d4a652a577a0 107 else
kkizirian 4:d4a652a577a0 108 line = cVec[i].getFileString();
kkizirian 4:d4a652a577a0 109 fprintf(writeClass, line.c_str());
kkizirian 4:d4a652a577a0 110 }
kkizirian 4:d4a652a577a0 111 fclose(writeClass);
kkizirian 4:d4a652a577a0 112 }
kkizirian 4:d4a652a577a0 113 }
kkizirian 4:d4a652a577a0 114
kkizirian 4:d4a652a577a0 115 void readClassFile(vector<Course>& cVec) {
kkizirian 4:d4a652a577a0 116 cVec.clear();
kkizirian 4:d4a652a577a0 117
kkizirian 4:d4a652a577a0 118 FILE *readFp = fopen("/sd/classdir/classes.txt", "r");
kkizirian 4:d4a652a577a0 119 char line[15];
kkizirian 4:d4a652a577a0 120 char buildingBuf[4];
kkizirian 4:d4a652a577a0 121 char hourBuf[3];
kkizirian 4:d4a652a577a0 122 int hour;
kkizirian 4:d4a652a577a0 123 char minuteBuf[3];
kkizirian 4:d4a652a577a0 124 int minute;
kkizirian 4:d4a652a577a0 125 char ampmBuf[3];
kkizirian 4:d4a652a577a0 126 uLCD.cls();
kkizirian 4:d4a652a577a0 127 uLCD.locate(0, 1);
kkizirian 4:d4a652a577a0 128 uLCD.printf("Reading class file...");
kkizirian 4:d4a652a577a0 129
kkizirian 4:d4a652a577a0 130 memset(buildingBuf, 0, sizeof(buildingBuf));
kkizirian 4:d4a652a577a0 131 memset(hourBuf, 0, sizeof(hourBuf));
kkizirian 4:d4a652a577a0 132 memset(minuteBuf, 0, sizeof(minuteBuf));
kkizirian 4:d4a652a577a0 133 memset(ampmBuf, 0, sizeof(ampmBuf));
kkizirian 4:d4a652a577a0 134 memset(line, 0, sizeof(line));
kkizirian 4:d4a652a577a0 135
kkizirian 4:d4a652a577a0 136 if (readFp == NULL)
kkizirian 4:d4a652a577a0 137 return;
kkizirian 4:d4a652a577a0 138 else {
kkizirian 4:d4a652a577a0 139 while (!feof(readFp)) {
kkizirian 4:d4a652a577a0 140 fgets(line, 15, readFp);
kkizirian 4:d4a652a577a0 141 if(line[8] == NULL)
kkizirian 4:d4a652a577a0 142 continue;
kkizirian 4:d4a652a577a0 143 memcpy(buildingBuf, line, 3);
kkizirian 4:d4a652a577a0 144 memcpy(hourBuf, &line[4], 2);
kkizirian 4:d4a652a577a0 145 memcpy(minuteBuf, &line[7], 2);
kkizirian 4:d4a652a577a0 146 memcpy(ampmBuf, &line[10], 2);
kkizirian 4:d4a652a577a0 147
kkizirian 4:d4a652a577a0 148 string building = buildingBuf;
kkizirian 4:d4a652a577a0 149 hour = atoi(hourBuf);
kkizirian 4:d4a652a577a0 150 minute = atoi(minuteBuf);
kkizirian 4:d4a652a577a0 151 string ampm = ampmBuf;
kkizirian 4:d4a652a577a0 152
kkizirian 4:d4a652a577a0 153 Course temp(building, hour, minute, ampm);
kkizirian 4:d4a652a577a0 154 cVec.push_back(temp);
kkizirian 4:d4a652a577a0 155 }
kkizirian 4:d4a652a577a0 156 }
kkizirian 4:d4a652a577a0 157 fclose(readFp);
kkizirian 4:d4a652a577a0 158 return;
kkizirian 4:d4a652a577a0 159 }