example using the course class
Dependencies: 4DGL-uLCD-SE Course mbed SDFileSystem
Diff: main.cpp
- Revision:
- 4:d4a652a577a0
- Parent:
- 3:4984f0a32f32
- Child:
- 5:77eafa8aaec6
diff -r 4984f0a32f32 -r d4a652a577a0 main.cpp --- a/main.cpp Mon Dec 05 02:50:59 2016 +0000 +++ b/main.cpp Wed Dec 07 22:30:19 2016 +0000 @@ -3,19 +3,22 @@ #include <string> #include <vector> #include "uLCD_4DGL.h" +#include "SDFileSystem.h" +SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board uLCD_4DGL uLCD(p9,p10,p11); // serial tx, serial rx, reset pin; vector<Course> courseVec; +vector<Course> readingCourseVec; int addCourseToVector(vector<Course>& cVec, Course newCourse); +void writeClassFile(vector<Course>& cVec), readClassFile(vector<Course>& cVec); int main() { - Course course1("HWY", 6, 5, "PM"); - Course course2("LVE", 8, 10, "AM"); - Course course3("CLH", 8, 25, "AM"); - Course course4("KLS", 8, 15, "AM"); - Course course5("LVE", 2, 30, "PM"); - Course course6("CLH", 2, 30, "PM"); - Course course7("CLH", 8, 15, "PM"); + Course course1("COC", 6, 5, "PM"); + Course course2("CLH", 8, 25, "AM"); + Course course3("VAN", 8, 15, "PM"); + Course course4("KLS", 8, 05, "PM"); + Course course5("van", 8, 05, "PM"); + int test = 0; test = addCourseToVector(courseVec, course1); @@ -23,23 +26,25 @@ test = addCourseToVector(courseVec, course3); test = addCourseToVector(courseVec, course4); test = addCourseToVector(courseVec, course5); - test = addCourseToVector(courseVec, course6); if (test == 0) uLCD.printf("uh oh!"); - test = addCourseToVector(courseVec, course7); + + mkdir("/sd/classdir", 0777); + writeClassFile(courseVec); + readClassFile(readingCourseVec); + uLCD.cls(); - for (int i = 0; i < courseVec.size(); i++) { - uLCD.locate(0, i); - uLCD.printf("%s", courseVec[i].getDisplayString()); + uLCD.locate(0, 1); + uLCD.printf("Display readingCourseVec.."); + for (int i = 0; i < readingCourseVec.size(); i++) { + uLCD.locate(0, i+2); + uLCD.printf("%s", readingCourseVec[i].getDisplayString()); } + } -// this adds the paramter newCourse to the vector cVec in the correct -// location in the vector by time, returns 1 if successful and 0 if -// unsuccessful int addCourseToVector(vector<Course>& cVec, Course newCourse) { - int numIterations = 0; if (cVec.size() == 0) { @@ -48,6 +53,7 @@ } for (int i = 0; i < cVec.size(); i++) { + numIterations++; if (cVec[i].getAMPM_toInt() < newCourse.getAMPM_toInt()) continue; else if (newCourse.getAMPM_toInt() < cVec[i].getAMPM_toInt()) { @@ -55,29 +61,99 @@ return 1; } else if (cVec[i].getAMPM_toInt() == newCourse.getAMPM_toInt()) { - if (cVec[i].getHour() < newCourse.getHour()) + if (cVec[i].getHour_forCompare() < newCourse.getHour_forCompare()) continue; - else if (newCourse.getHour() < cVec[i].getHour()) { + else if (newCourse.getHour_forCompare() < cVec[i].getHour_forCompare()) { cVec.insert(cVec.begin()+i, newCourse); return 1; } - else if (cVec[i].getHour() == newCourse.getHour()) { + else if (cVec[i].getHour_forCompare() == newCourse.getHour_forCompare()) { if (cVec[i].getMinute() < newCourse.getMinute()) continue; else if (newCourse.getMinute() < cVec[i].getMinute()) { cVec.insert(cVec.begin()+i, newCourse); return 1; } - else if (cVec[i].getMinute() == newCourse.getMinute()) + else if (cVec[i].getMinute() == newCourse.getMinute()) { + uLCD.cls(); + uLCD.locate(0,0); + uLCD.printf("Can not add coure!"); + uLCD.locate(0,1); + uLCD.printf("Course already at"); + uLCD.locate(0,2); + uLCD.printf("%i:%s%s", newCourse.getHour(), newCourse.getMinute_toString(), newCourse.getAMPM()); + wait(5); return 0; + } } } } - + if (numIterations == cVec.size()) { cVec.push_back(newCourse); return 1; } - + return 0; } + +void writeClassFile(vector<Course>& cVec) { + FILE *writeClass = fopen("/sd/classdir/classes.txt", "w"); + if (writeClass != NULL) { + string line = ""; + for (int i = 0; i < cVec.size(); i++) { + if (i != (cVec.size() - 1)) + line = cVec[i].getFileString() + "\n"; + else + line = cVec[i].getFileString(); + fprintf(writeClass, line.c_str()); + } + fclose(writeClass); + } +} + +void readClassFile(vector<Course>& cVec) { + cVec.clear(); + + FILE *readFp = fopen("/sd/classdir/classes.txt", "r"); + char line[15]; + char buildingBuf[4]; + char hourBuf[3]; + int hour; + char minuteBuf[3]; + int minute; + char ampmBuf[3]; + uLCD.cls(); + uLCD.locate(0, 1); + uLCD.printf("Reading class file..."); + + memset(buildingBuf, 0, sizeof(buildingBuf)); + memset(hourBuf, 0, sizeof(hourBuf)); + memset(minuteBuf, 0, sizeof(minuteBuf)); + memset(ampmBuf, 0, sizeof(ampmBuf)); + memset(line, 0, sizeof(line)); + + if (readFp == NULL) + return; + else { + while (!feof(readFp)) { + fgets(line, 15, readFp); + if(line[8] == NULL) + continue; + memcpy(buildingBuf, line, 3); + memcpy(hourBuf, &line[4], 2); + memcpy(minuteBuf, &line[7], 2); + memcpy(ampmBuf, &line[10], 2); + + string building = buildingBuf; + hour = atoi(hourBuf); + minute = atoi(minuteBuf); + string ampm = ampmBuf; + + Course temp(building, hour, minute, ampm); + cVec.push_back(temp); + } + } + fclose(readFp); + return; +} \ No newline at end of file