To input courses and save to SD card

Dependencies:   4DGL-uLCD-SE Course PinDetect SDFileSystem mbed

Committer:
kkizirian
Date:
Thu Dec 08 15:32:03 2016 +0000
Revision:
0:209f4db6ca27
To input courses

Who changed what in which revision?

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