Guides the user to their classes

Dependencies:   4DGL-uLCD-SE Course SDFileSystem mbed PinDetect LSM9DS1_Library_cal MBed_Adafruit-GPS-Library

Committer:
kkizirian
Date:
Wed Dec 07 22:43:54 2016 +0000
Revision:
1:65298ed43a58
Parent:
0:ce60014510e5
Child:
2:c708e2027970
Cleaned up commented code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kkizirian 0:ce60014510e5 1 // Class Scheduler
kkizirian 0:ce60014510e5 2 #include "mbed.h"
kkizirian 0:ce60014510e5 3 #include "uLCD_4DGL.h"
kkizirian 0:ce60014510e5 4 #include <string>
kkizirian 0:ce60014510e5 5 #include <vector>
kkizirian 0:ce60014510e5 6 #include "Course.h"
kkizirian 0:ce60014510e5 7 #include "SDFileSystem.h"
kkizirian 0:ce60014510e5 8
kkizirian 0:ce60014510e5 9 SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board
kkizirian 0:ce60014510e5 10 Serial pc(USBTX, USBRX);
kkizirian 0:ce60014510e5 11 Serial esp(p28, p27); // tx, rx
kkizirian 0:ce60014510e5 12 uLCD_4DGL uLCD(p9,p10,p11); // serial tx, serial rx, reset pin;
kkizirian 0:ce60014510e5 13
kkizirian 0:ce60014510e5 14 char ssid[32] = ""; // enter WiFi router ssid inside the quotes
kkizirian 0:ce60014510e5 15 char pwd [32] = ""; // enter WiFi router password inside the quotes
kkizirian 0:ce60014510e5 16
kkizirian 0:ce60014510e5 17 // for sending/receiving data over serial
kkizirian 0:ce60014510e5 18 volatile int tx_in=0;
kkizirian 0:ce60014510e5 19 volatile int tx_out=0;
kkizirian 0:ce60014510e5 20 volatile int rx_in=0;
kkizirian 0:ce60014510e5 21 volatile int rx_out=0;
kkizirian 1:65298ed43a58 22 const int buffer_size = 4095;
kkizirian 0:ce60014510e5 23 char tx_buffer[buffer_size+1];
kkizirian 0:ce60014510e5 24 char rx_buffer[buffer_size+1];
kkizirian 0:ce60014510e5 25 void Tx_interrupt();
kkizirian 0:ce60014510e5 26 void Rx_interrupt();
kkizirian 0:ce60014510e5 27 void read_line();
kkizirian 0:ce60014510e5 28
kkizirian 0:ce60014510e5 29 int DataRX;
kkizirian 0:ce60014510e5 30 int update;
kkizirian 0:ce60014510e5 31 char cmdbuff[512];
kkizirian 0:ce60014510e5 32 char webdata[4096]; // This may need to be bigger depending on WEB browser used
kkizirian 0:ce60014510e5 33 void SendCMD(),read_line(),ReadWebData(),startserver();
kkizirian 0:ce60014510e5 34 int addCourseToVector(vector<Course>& cVec, Course newCourse);
kkizirian 0:ce60014510e5 35 vector<Course> courseVec;
kkizirian 0:ce60014510e5 36 void readClassFile(vector<Course>& cVec), writeClassFile(vector<Course>& cVec);
kkizirian 0:ce60014510e5 37 char rx_line[512];
kkizirian 0:ce60014510e5 38 string ip_line;
kkizirian 0:ce60014510e5 39 bool ip_found = false;
kkizirian 0:ce60014510e5 40 int port =80; // set server port
kkizirian 0:ce60014510e5 41 int SERVtimeout =5; // set server timeout in seconds in case link breaks.
kkizirian 0:ce60014510e5 42
kkizirian 0:ce60014510e5 43 int main()
kkizirian 0:ce60014510e5 44 {
kkizirian 1:65298ed43a58 45
kkizirian 0:ce60014510e5 46 readClassFile(courseVec);
kkizirian 0:ce60014510e5 47
kkizirian 0:ce60014510e5 48 if (courseVec.size() != 0) {
kkizirian 0:ce60014510e5 49 uLCD.cls();
kkizirian 0:ce60014510e5 50 uLCD.locate(0,1);
kkizirian 0:ce60014510e5 51 for (int i = 0; i < courseVec.size(); i++) {
kkizirian 0:ce60014510e5 52 uLCD.locate(0, i);
kkizirian 0:ce60014510e5 53 uLCD.printf("%s", courseVec[i].getDisplayString());
kkizirian 0:ce60014510e5 54 }
kkizirian 0:ce60014510e5 55 }
kkizirian 0:ce60014510e5 56
kkizirian 0:ce60014510e5 57 pc.baud(9600);
kkizirian 0:ce60014510e5 58 esp.baud(9600);
kkizirian 0:ce60014510e5 59 // Setup a serial interrupt function to receive data
kkizirian 0:ce60014510e5 60 esp.attach(&Rx_interrupt, Serial::RxIrq);
kkizirian 0:ce60014510e5 61 // Setup a serial interrupt function to transmit data
kkizirian 0:ce60014510e5 62 esp.attach(&Tx_interrupt, Serial::TxIrq);
kkizirian 0:ce60014510e5 63 startserver();
kkizirian 0:ce60014510e5 64 DataRX=0;
kkizirian 0:ce60014510e5 65
kkizirian 0:ce60014510e5 66 while(1) {
kkizirian 0:ce60014510e5 67 if(DataRX==1) {
kkizirian 0:ce60014510e5 68 ReadWebData();
kkizirian 0:ce60014510e5 69 esp.attach(&Rx_interrupt, Serial::RxIrq);
kkizirian 0:ce60014510e5 70 if (courseVec.size() == 0) {
kkizirian 0:ce60014510e5 71 uLCD.cls();
kkizirian 0:ce60014510e5 72 uLCD.locate(0,0);
kkizirian 0:ce60014510e5 73 uLCD.printf("No classes input!");
kkizirian 0:ce60014510e5 74 uLCD.locate(0,1);
kkizirian 0:ce60014510e5 75 uLCD.printf("To input classes \nGo to:");
kkizirian 0:ce60014510e5 76 uLCD.locate(0,4);
kkizirian 0:ce60014510e5 77 uLCD.printf("%s", ip_line);
kkizirian 0:ce60014510e5 78 }
kkizirian 0:ce60014510e5 79 }
kkizirian 0:ce60014510e5 80 if(update==1)
kkizirian 0:ce60014510e5 81 {
kkizirian 0:ce60014510e5 82 writeClassFile(courseVec);
kkizirian 0:ce60014510e5 83 uLCD.cls();
kkizirian 0:ce60014510e5 84 uLCD.locate(0,1);
kkizirian 0:ce60014510e5 85 for (int i = 0; i < courseVec.size(); i++) {
kkizirian 0:ce60014510e5 86 uLCD.locate(0, i);
kkizirian 0:ce60014510e5 87 uLCD.printf("%s", courseVec[i].getDisplayString());
kkizirian 0:ce60014510e5 88 }
kkizirian 0:ce60014510e5 89 update=0;
kkizirian 0:ce60014510e5 90 }
kkizirian 0:ce60014510e5 91 }
kkizirian 0:ce60014510e5 92 }
kkizirian 0:ce60014510e5 93
kkizirian 0:ce60014510e5 94 // Reads and processes GET and POST web data
kkizirian 0:ce60014510e5 95 void ReadWebData()
kkizirian 0:ce60014510e5 96 {
kkizirian 0:ce60014510e5 97 wait_ms(200);
kkizirian 0:ce60014510e5 98 esp.attach(NULL,Serial::RxIrq);
kkizirian 0:ce60014510e5 99 DataRX=0;
kkizirian 0:ce60014510e5 100
kkizirian 0:ce60014510e5 101 if (!ip_found) {
kkizirian 0:ce60014510e5 102 char* ip_loc = strstr(rx_buffer, "192");
kkizirian 0:ce60014510e5 103 if (ip_loc != NULL) {
kkizirian 0:ce60014510e5 104 char ip_buf[16];
kkizirian 0:ce60014510e5 105 memset(ip_buf, '\0', sizeof(ip_buf));
kkizirian 0:ce60014510e5 106 memcpy(ip_buf, ip_loc, 15);
kkizirian 0:ce60014510e5 107 ip_line = ip_buf;
kkizirian 0:ce60014510e5 108 }
kkizirian 0:ce60014510e5 109 }
kkizirian 0:ce60014510e5 110
kkizirian 0:ce60014510e5 111 memset(webdata, '\0', sizeof(webdata));
kkizirian 0:ce60014510e5 112 strcpy(webdata, rx_buffer);
kkizirian 0:ce60014510e5 113 memset(rx_buffer, '\0', sizeof(rx_buffer));
kkizirian 0:ce60014510e5 114 rx_in = 0;
kkizirian 0:ce60014510e5 115 rx_out = 0;
kkizirian 0:ce60014510e5 116
kkizirian 0:ce60014510e5 117 // check web data for form information
kkizirian 0:ce60014510e5 118 if (strstr(webdata, "building=none") == NULL) {
kkizirian 0:ce60014510e5 119 char buildingBuf[4];
kkizirian 0:ce60014510e5 120 string building;
kkizirian 0:ce60014510e5 121 char* buildingLoc = strstr(webdata, "building=");
kkizirian 0:ce60014510e5 122 if (buildingLoc != NULL) {
kkizirian 0:ce60014510e5 123 memcpy(buildingBuf, &buildingLoc[9], 3);
kkizirian 0:ce60014510e5 124 buildingBuf[3] = '\0';
kkizirian 0:ce60014510e5 125 building = buildingBuf;;
kkizirian 0:ce60014510e5 126 }
kkizirian 0:ce60014510e5 127
kkizirian 0:ce60014510e5 128 char hourBuf[3];
kkizirian 0:ce60014510e5 129 int hour = -1;
kkizirian 0:ce60014510e5 130 char* hourLoc = strstr(webdata, "hour=");
kkizirian 0:ce60014510e5 131 if (hourLoc != NULL) {
kkizirian 0:ce60014510e5 132 memcpy(hourBuf, &hourLoc[5], 2);
kkizirian 0:ce60014510e5 133 hourBuf[2] = '\0';
kkizirian 0:ce60014510e5 134 hour = atoi(hourBuf);
kkizirian 0:ce60014510e5 135 }
kkizirian 0:ce60014510e5 136
kkizirian 0:ce60014510e5 137 char minuteBuf[3];
kkizirian 0:ce60014510e5 138 int minute = -1;
kkizirian 0:ce60014510e5 139 char* minuteLoc = strstr(webdata, "minute=");
kkizirian 0:ce60014510e5 140 if (minuteLoc != NULL) {
kkizirian 0:ce60014510e5 141 memcpy(minuteBuf, &minuteLoc[7], 2);
kkizirian 0:ce60014510e5 142 minuteBuf[2] = '\0';
kkizirian 0:ce60014510e5 143 minute = atoi(minuteBuf);
kkizirian 0:ce60014510e5 144 }
kkizirian 0:ce60014510e5 145
kkizirian 0:ce60014510e5 146 char ampmBuf[3];
kkizirian 0:ce60014510e5 147 string ampm;
kkizirian 0:ce60014510e5 148 char* ampmLoc = strstr(webdata, "AMPM=");
kkizirian 0:ce60014510e5 149 if (ampmLoc != NULL) {
kkizirian 0:ce60014510e5 150 memcpy(ampmBuf, &ampmLoc[5], 2);
kkizirian 0:ce60014510e5 151 ampmBuf[2] = '\0';
kkizirian 0:ce60014510e5 152 ampm = ampmBuf;
kkizirian 0:ce60014510e5 153 }
kkizirian 0:ce60014510e5 154
kkizirian 0:ce60014510e5 155 if (minute != -1) {
kkizirian 0:ce60014510e5 156 Course newCourse(building, hour, minute, ampm);
kkizirian 0:ce60014510e5 157 addCourseToVector(courseVec, newCourse);
kkizirian 0:ce60014510e5 158 update = 1;
kkizirian 0:ce60014510e5 159 }
kkizirian 0:ce60014510e5 160 }
kkizirian 0:ce60014510e5 161 }
kkizirian 0:ce60014510e5 162 // Starts webserver
kkizirian 0:ce60014510e5 163 void startserver()
kkizirian 0:ce60014510e5 164 {
kkizirian 0:ce60014510e5 165 pc.printf("++++++++++ Resetting ESP ++++++++++\r\n");
kkizirian 0:ce60014510e5 166 strcpy(cmdbuff,"node.restart()\r\n");
kkizirian 0:ce60014510e5 167 SendCMD();
kkizirian 0:ce60014510e5 168 wait(2);
kkizirian 0:ce60014510e5 169 read_line();
kkizirian 0:ce60014510e5 170
kkizirian 0:ce60014510e5 171 pc.printf("\n---------- Connecting to AP ----------\r\n");
kkizirian 0:ce60014510e5 172 //pc.printf("ssid = %s pwd = %s\r\n",ssid,pwd);
kkizirian 0:ce60014510e5 173 strcpy(cmdbuff, "wifi.sta.config(\"");
kkizirian 0:ce60014510e5 174 strcat(cmdbuff, ssid);
kkizirian 0:ce60014510e5 175 strcat(cmdbuff, "\",\"");
kkizirian 0:ce60014510e5 176 strcat(cmdbuff, pwd);
kkizirian 0:ce60014510e5 177 strcat(cmdbuff, "\")\r\n");
kkizirian 0:ce60014510e5 178 SendCMD();
kkizirian 0:ce60014510e5 179 wait(3);
kkizirian 0:ce60014510e5 180 read_line();
kkizirian 0:ce60014510e5 181
kkizirian 0:ce60014510e5 182 pc.printf("\n++++++++++ Starting Server ++++++++++\r\n> ");
kkizirian 0:ce60014510e5 183
kkizirian 0:ce60014510e5 184 //create server
kkizirian 0:ce60014510e5 185 sprintf(cmdbuff, "srv=net.createServer(net.TCP,%d)\r\n",SERVtimeout);
kkizirian 0:ce60014510e5 186 SendCMD();
kkizirian 0:ce60014510e5 187 read_line();
kkizirian 0:ce60014510e5 188 wait(0.5);
kkizirian 0:ce60014510e5 189 strcpy(cmdbuff,"srv:listen(80,function(conn)\r\n");
kkizirian 0:ce60014510e5 190 SendCMD();
kkizirian 0:ce60014510e5 191 read_line();
kkizirian 0:ce60014510e5 192 wait(0.3);
kkizirian 0:ce60014510e5 193 strcpy(cmdbuff,"conn:on(\"receive\",function(conn,payload) \r\n");
kkizirian 0:ce60014510e5 194 SendCMD();
kkizirian 0:ce60014510e5 195 read_line();
kkizirian 0:ce60014510e5 196 wait(0.3);
kkizirian 0:ce60014510e5 197
kkizirian 0:ce60014510e5 198 //print data to mbed
kkizirian 0:ce60014510e5 199 strcpy(cmdbuff,"print(payload)\r\n");
kkizirian 0:ce60014510e5 200 SendCMD();
kkizirian 0:ce60014510e5 201 read_line();
kkizirian 0:ce60014510e5 202 wait(0.2);
kkizirian 0:ce60014510e5 203
kkizirian 0:ce60014510e5 204 //web page data
kkizirian 0:ce60014510e5 205 strcpy(cmdbuff,"conn:send('<!DOCTYPE html><html><head><title>Class Scheduler!</title></head><body><h1>Class Scheduler</h1>')\r\n");
kkizirian 0:ce60014510e5 206 SendCMD();
kkizirian 0:ce60014510e5 207 read_line();
kkizirian 0:ce60014510e5 208 wait(0.4);
kkizirian 0:ce60014510e5 209 strcpy(cmdbuff,"conn:send('<p><div class = \"content\">Input your schedule below!</div></p>')\r\n");
kkizirian 0:ce60014510e5 210 SendCMD();
kkizirian 0:ce60014510e5 211 read_line();
kkizirian 0:ce60014510e5 212 wait(0.2);
kkizirian 0:ce60014510e5 213 strcpy(cmdbuff,"conn:send('<form method=\"POST\">')\r\n");
kkizirian 0:ce60014510e5 214 SendCMD();
kkizirian 0:ce60014510e5 215 read_line();
kkizirian 0:ce60014510e5 216 wait(0.3);
kkizirian 0:ce60014510e5 217 strcpy(cmdbuff, "conn:send('<select name=\"building\">')\r\n");
kkizirian 0:ce60014510e5 218 SendCMD();
kkizirian 0:ce60014510e5 219 read_line();
kkizirian 0:ce60014510e5 220 wait(0.3);
kkizirian 0:ce60014510e5 221 strcpy(cmdbuff, "conn:send('<option value =\"none\">None</option>')\r\n");
kkizirian 0:ce60014510e5 222 SendCMD();
kkizirian 0:ce60014510e5 223 read_line();
kkizirian 0:ce60014510e5 224 wait(0.3);
kkizirian 0:ce60014510e5 225 strcpy(cmdbuff,"conn:send('<option value=\"CLH\">Clough Undergraduate Learning Commons</option>')\r\n");
kkizirian 0:ce60014510e5 226 SendCMD();
kkizirian 0:ce60014510e5 227 read_line();
kkizirian 0:ce60014510e5 228 wait(0.3);
kkizirian 0:ce60014510e5 229 strcpy(cmdbuff,"conn:send('<option value=\"COC\">College of Computing</option>')\r\n");
kkizirian 0:ce60014510e5 230 SendCMD();
kkizirian 0:ce60014510e5 231 read_line();
kkizirian 0:ce60014510e5 232 wait(0.3);
kkizirian 0:ce60014510e5 233 strcpy(cmdbuff, "conn:send('<option value=\"KLS\">Klaus Advanced Computing Building</option>')\r\n");
kkizirian 0:ce60014510e5 234 SendCMD();
kkizirian 0:ce60014510e5 235 read_line();
kkizirian 0:ce60014510e5 236 wait(0.3);
kkizirian 0:ce60014510e5 237 strcpy(cmdbuff, "conn:send('<option value=\"VAN\">Van Leer</option></select>')\r\n");
kkizirian 0:ce60014510e5 238 SendCMD();
kkizirian 0:ce60014510e5 239 read_line();
kkizirian 0:ce60014510e5 240 wait(0.3);
kkizirian 0:ce60014510e5 241 strcpy(cmdbuff, "conn:send('<select name=\"hour\"><option value=\"01\">1</option><option value=\"02\">2</option>')\r\n");
kkizirian 0:ce60014510e5 242 SendCMD();
kkizirian 0:ce60014510e5 243 read_line();
kkizirian 0:ce60014510e5 244 wait(.3);
kkizirian 0:ce60014510e5 245 strcpy(cmdbuff, "conn:send('<option value=\"03\">3</option><option value=\"04\">4</option><option value=\"05\">5</option>')\r\n");
kkizirian 0:ce60014510e5 246 SendCMD();
kkizirian 0:ce60014510e5 247 read_line();
kkizirian 0:ce60014510e5 248 wait(.3);
kkizirian 0:ce60014510e5 249 strcpy(cmdbuff, "conn:send('<option value=\"06\">6</option><option value=\"07\">7</option><option value=\"08\">8</option>')\r\n");
kkizirian 0:ce60014510e5 250 SendCMD();
kkizirian 0:ce60014510e5 251 read_line();
kkizirian 0:ce60014510e5 252 wait(0.3);
kkizirian 0:ce60014510e5 253 strcpy(cmdbuff, "conn:send('<option value=\"09\">9</option><option value=\"10\">10</option><option value=\"11\">11</option><option value=\"12\">12</option></select>')\r\n");
kkizirian 0:ce60014510e5 254 SendCMD();
kkizirian 0:ce60014510e5 255 read_line();
kkizirian 0:ce60014510e5 256 wait(0.3);
kkizirian 0:ce60014510e5 257 strcpy(cmdbuff, "conn:send('<select name=\"minute\"><option value=\"00\">00</option><option value=\"05\">05</option>')\r\n");
kkizirian 0:ce60014510e5 258 SendCMD();
kkizirian 0:ce60014510e5 259 read_line();
kkizirian 0:ce60014510e5 260 wait(0.3);
kkizirian 0:ce60014510e5 261 strcpy(cmdbuff, "conn:send('<option value=\"10\">10</option><option value=\"15\">15</option>')\r\n");
kkizirian 0:ce60014510e5 262 SendCMD();
kkizirian 0:ce60014510e5 263 read_line();
kkizirian 0:ce60014510e5 264 wait(0.3);
kkizirian 0:ce60014510e5 265 strcpy(cmdbuff, "conn:send('<option value=\"20\">20</option><option value=\"25\">25</option>')\r\n");
kkizirian 0:ce60014510e5 266 SendCMD();
kkizirian 0:ce60014510e5 267 read_line();
kkizirian 0:ce60014510e5 268 wait(0.3);
kkizirian 0:ce60014510e5 269 strcpy(cmdbuff, "conn:send('<option value=\"30\">30</option><option value=\"35\">35</option>')\r\n");
kkizirian 0:ce60014510e5 270 SendCMD();
kkizirian 0:ce60014510e5 271 read_line();
kkizirian 0:ce60014510e5 272 wait(0.3);
kkizirian 0:ce60014510e5 273 strcpy(cmdbuff, "conn:send('<option value=\"40\">40</option><option value=\"45\">45</option>')\r\n");
kkizirian 0:ce60014510e5 274 SendCMD();
kkizirian 0:ce60014510e5 275 read_line();
kkizirian 0:ce60014510e5 276 wait(0.3);
kkizirian 0:ce60014510e5 277 strcpy(cmdbuff, "conn:send('<option value=\"50\">50</option><option value=\"55\">55</option></select>')\r\n");
kkizirian 0:ce60014510e5 278 SendCMD();
kkizirian 0:ce60014510e5 279 read_line();
kkizirian 0:ce60014510e5 280 wait(0.3);
kkizirian 0:ce60014510e5 281 strcpy(cmdbuff, "conn:send('<select name=\"AMPM\"><option value=\"AM\">AM</option><option value=\"PM\">PM</option></select><br></br>')\r\n");
kkizirian 0:ce60014510e5 282 SendCMD();
kkizirian 0:ce60014510e5 283 read_line();
kkizirian 0:ce60014510e5 284 wait(0.3);
kkizirian 0:ce60014510e5 285
kkizirian 0:ce60014510e5 286 strcpy(cmdbuff,"conn:send('<p><input type=\"submit\" value=\"Add Class\"></form>')\r\n");
kkizirian 0:ce60014510e5 287 SendCMD();
kkizirian 0:ce60014510e5 288 read_line();
kkizirian 0:ce60014510e5 289 wait(0.3);
kkizirian 0:ce60014510e5 290 strcpy(cmdbuff, "conn:send('</body></html>')\r\n");
kkizirian 0:ce60014510e5 291 SendCMD();
kkizirian 0:ce60014510e5 292 read_line();
kkizirian 0:ce60014510e5 293 wait(0.5);
kkizirian 0:ce60014510e5 294 // end web page data
kkizirian 0:ce60014510e5 295 strcpy(cmdbuff, "conn:on(\"sent\",function(conn) conn:close() end)\r\n"); // close current connection
kkizirian 0:ce60014510e5 296 SendCMD();
kkizirian 0:ce60014510e5 297 read_line();
kkizirian 0:ce60014510e5 298 wait(0.3);
kkizirian 0:ce60014510e5 299 strcpy(cmdbuff, "end)\r\n");
kkizirian 0:ce60014510e5 300 SendCMD();
kkizirian 0:ce60014510e5 301 read_line();
kkizirian 0:ce60014510e5 302 wait(0.2);
kkizirian 0:ce60014510e5 303 strcpy(cmdbuff, "end)\r\n");
kkizirian 0:ce60014510e5 304 SendCMD();
kkizirian 0:ce60014510e5 305 read_line();
kkizirian 0:ce60014510e5 306 wait(0.2);
kkizirian 0:ce60014510e5 307
kkizirian 0:ce60014510e5 308 strcpy(cmdbuff, "tmr.alarm(0, 1000, 1, function()\r\n");
kkizirian 0:ce60014510e5 309 SendCMD();
kkizirian 0:ce60014510e5 310 read_line();
kkizirian 0:ce60014510e5 311 wait(0.2);
kkizirian 0:ce60014510e5 312 strcpy(cmdbuff, "if wifi.sta.getip() == nil then\r\n");
kkizirian 0:ce60014510e5 313 SendCMD();
kkizirian 0:ce60014510e5 314 read_line();
kkizirian 0:ce60014510e5 315 wait(0.2);
kkizirian 0:ce60014510e5 316 strcpy(cmdbuff, "print(\"Connecting to AP...\\n\")\r\n");
kkizirian 0:ce60014510e5 317 SendCMD();
kkizirian 0:ce60014510e5 318 read_line();
kkizirian 0:ce60014510e5 319 wait(0.2);
kkizirian 0:ce60014510e5 320 strcpy(cmdbuff, "else\r\n");
kkizirian 0:ce60014510e5 321 SendCMD();
kkizirian 0:ce60014510e5 322 read_line();
kkizirian 0:ce60014510e5 323 wait(0.2);
kkizirian 0:ce60014510e5 324 strcpy(cmdbuff, "ip, nm, gw=wifi.sta.getip()\r\n");
kkizirian 0:ce60014510e5 325 SendCMD();
kkizirian 0:ce60014510e5 326 read_line();
kkizirian 0:ce60014510e5 327 wait(0.2);
kkizirian 0:ce60014510e5 328 strcpy(cmdbuff,"print(\"IP Address: \",ip)\r\n");
kkizirian 0:ce60014510e5 329 SendCMD();
kkizirian 0:ce60014510e5 330 read_line();
kkizirian 0:ce60014510e5 331 wait(0.2);
kkizirian 0:ce60014510e5 332 strcpy(cmdbuff,"tmr.stop(0)\r\n");
kkizirian 0:ce60014510e5 333 SendCMD();
kkizirian 0:ce60014510e5 334 read_line();
kkizirian 0:ce60014510e5 335 wait(0.2);
kkizirian 0:ce60014510e5 336 strcpy(cmdbuff,"end\r\n");
kkizirian 0:ce60014510e5 337 SendCMD();
kkizirian 0:ce60014510e5 338 read_line();
kkizirian 0:ce60014510e5 339 wait(0.2);
kkizirian 0:ce60014510e5 340 strcpy(cmdbuff,"end)\r\n");
kkizirian 0:ce60014510e5 341 SendCMD();
kkizirian 0:ce60014510e5 342 read_line();
kkizirian 0:ce60014510e5 343 wait(0.2);
kkizirian 0:ce60014510e5 344
kkizirian 0:ce60014510e5 345 pc.printf("\n\n++++++++++ Ready ++++++++++\r\n\n");
kkizirian 0:ce60014510e5 346 }
kkizirian 0:ce60014510e5 347
kkizirian 0:ce60014510e5 348 // ESP Command data send
kkizirian 0:ce60014510e5 349 void SendCMD()
kkizirian 0:ce60014510e5 350 {
kkizirian 0:ce60014510e5 351 int i;
kkizirian 0:ce60014510e5 352 char temp_char;
kkizirian 0:ce60014510e5 353 bool empty;
kkizirian 0:ce60014510e5 354 i = 0;
kkizirian 0:ce60014510e5 355 // Start Critical Section - don't interrupt while changing global buffer variables
kkizirian 0:ce60014510e5 356 NVIC_DisableIRQ(UART1_IRQn);
kkizirian 0:ce60014510e5 357 empty = (tx_in == tx_out);
kkizirian 0:ce60014510e5 358 while ((i==0) || (cmdbuff[i-1] != '\n')) {
kkizirian 0:ce60014510e5 359 // Wait if buffer full
kkizirian 0:ce60014510e5 360 if (((tx_in + 1) % buffer_size) == tx_out) {
kkizirian 0:ce60014510e5 361 // End Critical Section - need to let interrupt routine empty buffer by sending
kkizirian 0:ce60014510e5 362 NVIC_EnableIRQ(UART1_IRQn);
kkizirian 0:ce60014510e5 363 while (((tx_in + 1) % buffer_size) == tx_out) {
kkizirian 0:ce60014510e5 364 }
kkizirian 0:ce60014510e5 365 // Start Critical Section - don't interrupt while changing global buffer variables
kkizirian 0:ce60014510e5 366 NVIC_DisableIRQ(UART1_IRQn);
kkizirian 0:ce60014510e5 367 }
kkizirian 0:ce60014510e5 368 tx_buffer[tx_in] = cmdbuff[i];
kkizirian 0:ce60014510e5 369 i++;
kkizirian 0:ce60014510e5 370 tx_in = (tx_in + 1) % buffer_size;
kkizirian 0:ce60014510e5 371 }
kkizirian 0:ce60014510e5 372 if (esp.writeable() && (empty)) {
kkizirian 0:ce60014510e5 373 temp_char = tx_buffer[tx_out];
kkizirian 0:ce60014510e5 374 tx_out = (tx_out + 1) % buffer_size;
kkizirian 0:ce60014510e5 375 // Send first character to start tx interrupts, if stopped
kkizirian 0:ce60014510e5 376 esp.putc(temp_char);
kkizirian 0:ce60014510e5 377 }
kkizirian 0:ce60014510e5 378 // End Critical Section
kkizirian 0:ce60014510e5 379 NVIC_EnableIRQ(UART1_IRQn);
kkizirian 0:ce60014510e5 380 return;
kkizirian 0:ce60014510e5 381 }
kkizirian 0:ce60014510e5 382
kkizirian 0:ce60014510e5 383 // Read a line from the large rx buffer from rx interrupt routine
kkizirian 0:ce60014510e5 384 void read_line() {
kkizirian 0:ce60014510e5 385 int i;
kkizirian 0:ce60014510e5 386 i = 0;
kkizirian 0:ce60014510e5 387 // Start Critical Section - don't interrupt while changing global buffer variables
kkizirian 0:ce60014510e5 388 NVIC_DisableIRQ(UART1_IRQn);
kkizirian 0:ce60014510e5 389 // Loop reading rx buffer characters until end of line character
kkizirian 0:ce60014510e5 390 while ((i==0) || (rx_line[i-1] != '\r')) {
kkizirian 0:ce60014510e5 391 // Wait if buffer empty
kkizirian 0:ce60014510e5 392 if (rx_in == rx_out) {
kkizirian 0:ce60014510e5 393 // End Critical Section - need to allow rx interrupt to get new characters for buffer
kkizirian 0:ce60014510e5 394 NVIC_EnableIRQ(UART1_IRQn);
kkizirian 0:ce60014510e5 395 while (rx_in == rx_out) {
kkizirian 0:ce60014510e5 396 }
kkizirian 0:ce60014510e5 397 // Start Critical Section - don't interrupt while changing global buffer variables
kkizirian 0:ce60014510e5 398 NVIC_DisableIRQ(UART1_IRQn);
kkizirian 0:ce60014510e5 399 }
kkizirian 0:ce60014510e5 400 rx_line[i] = rx_buffer[rx_out];
kkizirian 0:ce60014510e5 401 i++;
kkizirian 0:ce60014510e5 402 rx_out = (rx_out + 1) % buffer_size;
kkizirian 0:ce60014510e5 403 }
kkizirian 0:ce60014510e5 404 // End Critical Section
kkizirian 0:ce60014510e5 405 NVIC_EnableIRQ(UART1_IRQn);
kkizirian 0:ce60014510e5 406 rx_line[i-1] = 0;
kkizirian 0:ce60014510e5 407
kkizirian 0:ce60014510e5 408 return;
kkizirian 0:ce60014510e5 409 }
kkizirian 0:ce60014510e5 410
kkizirian 0:ce60014510e5 411 // Interupt Routine to read in data from serial port
kkizirian 0:ce60014510e5 412 void Rx_interrupt() {
kkizirian 0:ce60014510e5 413 DataRX=1;
kkizirian 0:ce60014510e5 414 // Loop just in case more than one character is in UART's receive FIFO buffer
kkizirian 0:ce60014510e5 415 // Stop if buffer full
kkizirian 0:ce60014510e5 416 while ((esp.readable()) && (((rx_in + 1) % buffer_size) != rx_out)) {
kkizirian 0:ce60014510e5 417 rx_buffer[rx_in] = esp.getc();
kkizirian 0:ce60014510e5 418 // Uncomment to Echo to USB serial to watch data flow
kkizirian 0:ce60014510e5 419 pc.putc(rx_buffer[rx_in]);
kkizirian 0:ce60014510e5 420 rx_in = (rx_in + 1) % buffer_size;
kkizirian 0:ce60014510e5 421 }
kkizirian 0:ce60014510e5 422 return;
kkizirian 0:ce60014510e5 423 }
kkizirian 0:ce60014510e5 424
kkizirian 0:ce60014510e5 425 // Interupt Routine to write out data to serial port
kkizirian 0:ce60014510e5 426 void Tx_interrupt() {
kkizirian 0:ce60014510e5 427 // Loop to fill more than one character in UART's transmit FIFO buffer
kkizirian 0:ce60014510e5 428 // Stop if buffer empty
kkizirian 0:ce60014510e5 429 while ((esp.writeable()) && (tx_in != tx_out)) {
kkizirian 0:ce60014510e5 430 esp.putc(tx_buffer[tx_out]);
kkizirian 0:ce60014510e5 431 tx_out = (tx_out + 1) % buffer_size;
kkizirian 0:ce60014510e5 432 }
kkizirian 0:ce60014510e5 433 return;
kkizirian 0:ce60014510e5 434 }
kkizirian 0:ce60014510e5 435
kkizirian 0:ce60014510e5 436 int addCourseToVector(vector<Course>& cVec, Course newCourse) {
kkizirian 0:ce60014510e5 437 int numIterations = 0;
kkizirian 0:ce60014510e5 438
kkizirian 0:ce60014510e5 439 if (cVec.size() == 0) {
kkizirian 0:ce60014510e5 440 cVec.push_back(newCourse);
kkizirian 0:ce60014510e5 441 return 1;
kkizirian 0:ce60014510e5 442 }
kkizirian 0:ce60014510e5 443
kkizirian 0:ce60014510e5 444 for (int i = 0; i < cVec.size(); i++) {
kkizirian 0:ce60014510e5 445 numIterations++;
kkizirian 0:ce60014510e5 446 if (cVec[i].getAMPM_toInt() < newCourse.getAMPM_toInt())
kkizirian 0:ce60014510e5 447 continue;
kkizirian 0:ce60014510e5 448 else if (newCourse.getAMPM_toInt() < cVec[i].getAMPM_toInt()) {
kkizirian 0:ce60014510e5 449 cVec.insert(cVec.begin()+i, newCourse);
kkizirian 0:ce60014510e5 450 return 1;
kkizirian 0:ce60014510e5 451 }
kkizirian 0:ce60014510e5 452 else if (cVec[i].getAMPM_toInt() == newCourse.getAMPM_toInt()) {
kkizirian 0:ce60014510e5 453 if (cVec[i].getHour_forCompare() < newCourse.getHour_forCompare())
kkizirian 0:ce60014510e5 454 continue;
kkizirian 0:ce60014510e5 455 else if (newCourse.getHour_forCompare() < cVec[i].getHour_forCompare()) {
kkizirian 0:ce60014510e5 456 cVec.insert(cVec.begin()+i, newCourse);
kkizirian 0:ce60014510e5 457 return 1;
kkizirian 0:ce60014510e5 458 }
kkizirian 0:ce60014510e5 459 else if (cVec[i].getHour_forCompare() == newCourse.getHour_forCompare()) {
kkizirian 0:ce60014510e5 460 if (cVec[i].getMinute() < newCourse.getMinute())
kkizirian 0:ce60014510e5 461 continue;
kkizirian 0:ce60014510e5 462 else if (newCourse.getMinute() < cVec[i].getMinute()) {
kkizirian 0:ce60014510e5 463 cVec.insert(cVec.begin()+i, newCourse);
kkizirian 0:ce60014510e5 464 return 1;
kkizirian 0:ce60014510e5 465 }
kkizirian 0:ce60014510e5 466 else if (cVec[i].getMinute() == newCourse.getMinute()) {
kkizirian 0:ce60014510e5 467 uLCD.cls();
kkizirian 0:ce60014510e5 468 uLCD.locate(0,0);
kkizirian 0:ce60014510e5 469 uLCD.printf("Can not add coure!");
kkizirian 0:ce60014510e5 470 uLCD.locate(0,1);
kkizirian 0:ce60014510e5 471 uLCD.printf("Course already at");
kkizirian 0:ce60014510e5 472 uLCD.locate(0,2);
kkizirian 0:ce60014510e5 473 uLCD.printf("%i:%s%s", newCourse.getHour(), newCourse.getMinute_toString(), newCourse.getAMPM());
kkizirian 0:ce60014510e5 474 wait(5);
kkizirian 0:ce60014510e5 475 return 0;
kkizirian 0:ce60014510e5 476 }
kkizirian 0:ce60014510e5 477 }
kkizirian 0:ce60014510e5 478 }
kkizirian 0:ce60014510e5 479 }
kkizirian 0:ce60014510e5 480
kkizirian 0:ce60014510e5 481 if (numIterations == cVec.size()) {
kkizirian 0:ce60014510e5 482 cVec.push_back(newCourse);
kkizirian 0:ce60014510e5 483 return 1;
kkizirian 0:ce60014510e5 484 }
kkizirian 0:ce60014510e5 485
kkizirian 0:ce60014510e5 486 return 0;
kkizirian 0:ce60014510e5 487 }
kkizirian 0:ce60014510e5 488
kkizirian 0:ce60014510e5 489 void writeClassFile(vector<Course>& cVec) {
kkizirian 0:ce60014510e5 490 FILE *writeClass = fopen("/sd/classdir/classes.txt", "w");
kkizirian 0:ce60014510e5 491 if (writeClass != NULL) {
kkizirian 0:ce60014510e5 492 string line = "";
kkizirian 0:ce60014510e5 493 for (int i = 0; i < cVec.size(); i++) {
kkizirian 0:ce60014510e5 494 if (i != (cVec.size() - 1))
kkizirian 0:ce60014510e5 495 line = cVec[i].getFileString() + "\n";
kkizirian 0:ce60014510e5 496 else
kkizirian 0:ce60014510e5 497 line = cVec[i].getFileString();
kkizirian 0:ce60014510e5 498 fprintf(writeClass, line.c_str());
kkizirian 0:ce60014510e5 499 }
kkizirian 0:ce60014510e5 500 fclose(writeClass);
kkizirian 0:ce60014510e5 501 }
kkizirian 0:ce60014510e5 502 }
kkizirian 0:ce60014510e5 503
kkizirian 0:ce60014510e5 504 void readClassFile(vector<Course>& cVec) {
kkizirian 0:ce60014510e5 505 cVec.clear();
kkizirian 0:ce60014510e5 506
kkizirian 0:ce60014510e5 507 FILE *readFp = fopen("/sd/classdir/classes.txt", "r");
kkizirian 0:ce60014510e5 508 char line[15];
kkizirian 0:ce60014510e5 509 char buildingBuf[4];
kkizirian 0:ce60014510e5 510 char hourBuf[3];
kkizirian 0:ce60014510e5 511 int hour;
kkizirian 0:ce60014510e5 512 char minuteBuf[3];
kkizirian 0:ce60014510e5 513 int minute;
kkizirian 0:ce60014510e5 514 char ampmBuf[3];
kkizirian 0:ce60014510e5 515 uLCD.cls();
kkizirian 0:ce60014510e5 516 uLCD.locate(0, 1);
kkizirian 0:ce60014510e5 517 uLCD.printf("Reading class file...");
kkizirian 0:ce60014510e5 518
kkizirian 0:ce60014510e5 519 memset(buildingBuf, 0, sizeof(buildingBuf));
kkizirian 0:ce60014510e5 520 memset(hourBuf, 0, sizeof(hourBuf));
kkizirian 0:ce60014510e5 521 memset(minuteBuf, 0, sizeof(minuteBuf));
kkizirian 0:ce60014510e5 522 memset(ampmBuf, 0, sizeof(ampmBuf));
kkizirian 0:ce60014510e5 523 memset(line, 0, sizeof(line));
kkizirian 0:ce60014510e5 524
kkizirian 0:ce60014510e5 525 if (readFp == NULL)
kkizirian 0:ce60014510e5 526 return;
kkizirian 0:ce60014510e5 527 else {
kkizirian 0:ce60014510e5 528 while (!feof(readFp)) {
kkizirian 0:ce60014510e5 529 fgets(line, 15, readFp);
kkizirian 0:ce60014510e5 530 if(line[8] == NULL)
kkizirian 0:ce60014510e5 531 continue;
kkizirian 0:ce60014510e5 532 memcpy(buildingBuf, line, 3);
kkizirian 0:ce60014510e5 533 memcpy(hourBuf, &line[4], 2);
kkizirian 0:ce60014510e5 534 memcpy(minuteBuf, &line[7], 2);
kkizirian 0:ce60014510e5 535 memcpy(ampmBuf, &line[10], 2);
kkizirian 0:ce60014510e5 536
kkizirian 0:ce60014510e5 537 string building = buildingBuf;
kkizirian 0:ce60014510e5 538 hour = atoi(hourBuf);
kkizirian 0:ce60014510e5 539 minute = atoi(minuteBuf);
kkizirian 0:ce60014510e5 540 string ampm = ampmBuf;
kkizirian 0:ce60014510e5 541
kkizirian 0:ce60014510e5 542 Course temp(building, hour, minute, ampm);
kkizirian 0:ce60014510e5 543 cVec.push_back(temp);
kkizirian 0:ce60014510e5 544 }
kkizirian 0:ce60014510e5 545 }
kkizirian 0:ce60014510e5 546 fclose(readFp);
kkizirian 0:ce60014510e5 547 return;
kkizirian 0:ce60014510e5 548 }