Guides the user to their classes
Dependencies: 4DGL-uLCD-SE Course SDFileSystem mbed PinDetect LSM9DS1_Library_cal MBed_Adafruit-GPS-Library
main.cpp@3:0ca91f8afec5, 2016-12-08 (annotated)
- Committer:
- kkizirian
- Date:
- Thu Dec 08 14:15:25 2016 +0000
- Revision:
- 3:0ca91f8afec5
- Parent:
- 2:c708e2027970
- Child:
- 4:93a4b415fe6c
Added compass functionality
Who changed what in which revision?
User | Revision | Line number | New 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 | 3:0ca91f8afec5 | 8 | #include "LSM9DS1.h" |
kkizirian | 2:c708e2027970 | 9 | #include "PinDetect.h" |
kkizirian | 3:0ca91f8afec5 | 10 | #define PI 3.14159 |
kkizirian | 3:0ca91f8afec5 | 11 | #define DECLINATION -4.94 // Declination (degrees) in Atlanta,GA. |
kkizirian | 0:ce60014510e5 | 12 | |
kkizirian | 0:ce60014510e5 | 13 | SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board |
kkizirian | 0:ce60014510e5 | 14 | Serial pc(USBTX, USBRX); |
kkizirian | 0:ce60014510e5 | 15 | Serial esp(p28, p27); // tx, rx |
kkizirian | 3:0ca91f8afec5 | 16 | uLCD_4DGL uLCD(p13,p14,p15); // serial tx, serial rx, reset pin; |
kkizirian | 3:0ca91f8afec5 | 17 | PinDetect left(p20); |
kkizirian | 3:0ca91f8afec5 | 18 | PinDetect right(p18); |
kkizirian | 3:0ca91f8afec5 | 19 | PinDetect center(p19); |
kkizirian | 0:ce60014510e5 | 20 | |
kkizirian | 3:0ca91f8afec5 | 21 | char ssid[32] = ""; // enter WiFi router ssid inside the quotes |
kkizirian | 3:0ca91f8afec5 | 22 | char pwd [32] = ""; // enter WiFi router password inside the quotes |
kkizirian | 0:ce60014510e5 | 23 | |
kkizirian | 0:ce60014510e5 | 24 | // for sending/receiving data over serial |
kkizirian | 0:ce60014510e5 | 25 | volatile int tx_in=0; |
kkizirian | 0:ce60014510e5 | 26 | volatile int tx_out=0; |
kkizirian | 0:ce60014510e5 | 27 | volatile int rx_in=0; |
kkizirian | 0:ce60014510e5 | 28 | volatile int rx_out=0; |
kkizirian | 2:c708e2027970 | 29 | const int buffer_size = 4095; |
kkizirian | 0:ce60014510e5 | 30 | char tx_buffer[buffer_size+1]; |
kkizirian | 0:ce60014510e5 | 31 | char rx_buffer[buffer_size+1]; |
kkizirian | 0:ce60014510e5 | 32 | void Tx_interrupt(); |
kkizirian | 0:ce60014510e5 | 33 | void Rx_interrupt(); |
kkizirian | 0:ce60014510e5 | 34 | void read_line(); |
kkizirian | 0:ce60014510e5 | 35 | |
kkizirian | 0:ce60014510e5 | 36 | int DataRX; |
kkizirian | 0:ce60014510e5 | 37 | int update; |
kkizirian | 0:ce60014510e5 | 38 | char cmdbuff[512]; |
kkizirian | 0:ce60014510e5 | 39 | char webdata[4096]; // This may need to be bigger depending on WEB browser used |
kkizirian | 0:ce60014510e5 | 40 | void SendCMD(),read_line(),ReadWebData(),startserver(); |
kkizirian | 0:ce60014510e5 | 41 | int addCourseToVector(vector<Course>& cVec, Course newCourse); |
kkizirian | 0:ce60014510e5 | 42 | vector<Course> courseVec; |
kkizirian | 0:ce60014510e5 | 43 | void readClassFile(vector<Course>& cVec), writeClassFile(vector<Course>& cVec); |
kkizirian | 2:c708e2027970 | 44 | void displayCourseVec(); |
kkizirian | 3:0ca91f8afec5 | 45 | float calculateHeading(float mx, float my), computeAngleToDestination(float diffLat, float diffLong); |
kkizirian | 3:0ca91f8afec5 | 46 | int xEnd, yEnd, lastXEnd, lastYEnd = 0.0; |
kkizirian | 0:ce60014510e5 | 47 | char rx_line[512]; |
kkizirian | 0:ce60014510e5 | 48 | string ip_line; |
kkizirian | 0:ce60014510e5 | 49 | bool ip_found = false; |
kkizirian | 3:0ca91f8afec5 | 50 | bool screen_refreshed = false; |
kkizirian | 2:c708e2027970 | 51 | int volatile current_screen = 0; |
kkizirian | 2:c708e2027970 | 52 | bool volatile screen_change = false; |
kkizirian | 0:ce60014510e5 | 53 | int port =80; // set server port |
kkizirian | 0:ce60014510e5 | 54 | int SERVtimeout =5; // set server timeout in seconds in case link breaks. |
kkizirian | 0:ce60014510e5 | 55 | |
kkizirian | 2:c708e2027970 | 56 | void left_callback(void) |
kkizirian | 2:c708e2027970 | 57 | { |
kkizirian | 3:0ca91f8afec5 | 58 | if (courseVec.size() != 0) { |
kkizirian | 3:0ca91f8afec5 | 59 | current_screen = (3 + current_screen - 1)%3; |
kkizirian | 3:0ca91f8afec5 | 60 | screen_change = true; |
kkizirian | 3:0ca91f8afec5 | 61 | } else |
kkizirian | 3:0ca91f8afec5 | 62 | current_screen = 0; |
kkizirian | 2:c708e2027970 | 63 | } |
kkizirian | 2:c708e2027970 | 64 | |
kkizirian | 2:c708e2027970 | 65 | void right_callback(void) |
kkizirian | 2:c708e2027970 | 66 | { |
kkizirian | 3:0ca91f8afec5 | 67 | if (courseVec.size() != 0) { |
kkizirian | 3:0ca91f8afec5 | 68 | current_screen = (3 + current_screen + 1)%3; |
kkizirian | 3:0ca91f8afec5 | 69 | screen_change = true; |
kkizirian | 3:0ca91f8afec5 | 70 | } else |
kkizirian | 3:0ca91f8afec5 | 71 | current_screen = 0; |
kkizirian | 2:c708e2027970 | 72 | } |
kkizirian | 2:c708e2027970 | 73 | |
kkizirian | 2:c708e2027970 | 74 | void center_callback(void) |
kkizirian | 2:c708e2027970 | 75 | { |
kkizirian | 3:0ca91f8afec5 | 76 | if (current_screen == 0) { |
kkizirian | 3:0ca91f8afec5 | 77 | courseVec.clear(); |
kkizirian | 3:0ca91f8afec5 | 78 | update = 1; |
kkizirian | 3:0ca91f8afec5 | 79 | } |
kkizirian | 2:c708e2027970 | 80 | } |
kkizirian | 2:c708e2027970 | 81 | |
kkizirian | 0:ce60014510e5 | 82 | int main() |
kkizirian | 0:ce60014510e5 | 83 | { |
kkizirian | 3:0ca91f8afec5 | 84 | LSM9DS1 IMU(p9, p10, 0xD6, 0x3C); |
kkizirian | 3:0ca91f8afec5 | 85 | float h = 0.0; |
kkizirian | 3:0ca91f8afec5 | 86 | float angleToDest = 0.0; |
kkizirian | 3:0ca91f8afec5 | 87 | IMU.begin(); |
kkizirian | 3:0ca91f8afec5 | 88 | if (!IMU.begin()) { |
kkizirian | 3:0ca91f8afec5 | 89 | pc.printf("Failed to communicate with LSM9DS1.\n"); |
kkizirian | 3:0ca91f8afec5 | 90 | } |
kkizirian | 3:0ca91f8afec5 | 91 | uLCD.cls(); |
kkizirian | 3:0ca91f8afec5 | 92 | uLCD.printf("Place IMU flat"); |
kkizirian | 3:0ca91f8afec5 | 93 | IMU.calibrate(1); |
kkizirian | 3:0ca91f8afec5 | 94 | uLCD.cls(); |
kkizirian | 3:0ca91f8afec5 | 95 | uLCD.printf("Rotate IMU 360\ndegrees in \nhorizontal plane"); |
kkizirian | 3:0ca91f8afec5 | 96 | IMU.calibrateMag(0); |
kkizirian | 3:0ca91f8afec5 | 97 | |
kkizirian | 3:0ca91f8afec5 | 98 | float myLat = 33.776346; |
kkizirian | 3:0ca91f8afec5 | 99 | float myLong = -84.378927; |
kkizirian | 3:0ca91f8afec5 | 100 | |
kkizirian | 3:0ca91f8afec5 | 101 | float destinationLat = 33.772778; |
kkizirian | 3:0ca91f8afec5 | 102 | float destinationLong = -84.379685; |
kkizirian | 3:0ca91f8afec5 | 103 | |
kkizirian | 2:c708e2027970 | 104 | left.mode(PullUp); |
kkizirian | 2:c708e2027970 | 105 | right.mode(PullUp); |
kkizirian | 2:c708e2027970 | 106 | center.mode(PullUp); |
kkizirian | 2:c708e2027970 | 107 | |
kkizirian | 2:c708e2027970 | 108 | left.attach_deasserted(&left_callback); |
kkizirian | 2:c708e2027970 | 109 | right.attach_deasserted(&right_callback); |
kkizirian | 2:c708e2027970 | 110 | center.attach_deasserted(¢er_callback); |
kkizirian | 2:c708e2027970 | 111 | left.setSampleFrequency(); |
kkizirian | 2:c708e2027970 | 112 | right.setSampleFrequency(); |
kkizirian | 2:c708e2027970 | 113 | center.setSampleFrequency(); |
kkizirian | 2:c708e2027970 | 114 | |
kkizirian | 0:ce60014510e5 | 115 | readClassFile(courseVec); |
kkizirian | 2:c708e2027970 | 116 | |
kkizirian | 0:ce60014510e5 | 117 | pc.baud(9600); |
kkizirian | 0:ce60014510e5 | 118 | esp.baud(9600); |
kkizirian | 0:ce60014510e5 | 119 | // Setup a serial interrupt function to receive data |
kkizirian | 0:ce60014510e5 | 120 | esp.attach(&Rx_interrupt, Serial::RxIrq); |
kkizirian | 0:ce60014510e5 | 121 | // Setup a serial interrupt function to transmit data |
kkizirian | 0:ce60014510e5 | 122 | esp.attach(&Tx_interrupt, Serial::TxIrq); |
kkizirian | 0:ce60014510e5 | 123 | startserver(); |
kkizirian | 0:ce60014510e5 | 124 | DataRX=0; |
kkizirian | 2:c708e2027970 | 125 | |
kkizirian | 0:ce60014510e5 | 126 | while(1) { |
kkizirian | 2:c708e2027970 | 127 | switch(current_screen) { |
kkizirian | 2:c708e2027970 | 128 | case 0: |
kkizirian | 2:c708e2027970 | 129 | if (screen_change) { |
kkizirian | 2:c708e2027970 | 130 | displayCourseVec(); |
kkizirian | 3:0ca91f8afec5 | 131 | screen_refreshed = true; |
kkizirian | 2:c708e2027970 | 132 | screen_change = false; |
kkizirian | 2:c708e2027970 | 133 | } |
kkizirian | 2:c708e2027970 | 134 | if(DataRX==1) { |
kkizirian | 2:c708e2027970 | 135 | ReadWebData(); |
kkizirian | 2:c708e2027970 | 136 | esp.attach(&Rx_interrupt, Serial::RxIrq); |
kkizirian | 3:0ca91f8afec5 | 137 | if (!screen_refreshed) { |
kkizirian | 3:0ca91f8afec5 | 138 | displayCourseVec(); |
kkizirian | 3:0ca91f8afec5 | 139 | screen_refreshed = true; |
kkizirian | 3:0ca91f8afec5 | 140 | } |
kkizirian | 2:c708e2027970 | 141 | } |
kkizirian | 2:c708e2027970 | 142 | if(update==1) { |
kkizirian | 2:c708e2027970 | 143 | writeClassFile(courseVec); |
kkizirian | 3:0ca91f8afec5 | 144 | if (!screen_refreshed) { |
kkizirian | 3:0ca91f8afec5 | 145 | displayCourseVec(); |
kkizirian | 3:0ca91f8afec5 | 146 | } |
kkizirian | 2:c708e2027970 | 147 | update=0; |
kkizirian | 2:c708e2027970 | 148 | } |
kkizirian | 3:0ca91f8afec5 | 149 | screen_refreshed = false; |
kkizirian | 2:c708e2027970 | 150 | break; |
kkizirian | 2:c708e2027970 | 151 | case 1: |
kkizirian | 2:c708e2027970 | 152 | if (screen_change) { |
kkizirian | 2:c708e2027970 | 153 | uLCD.printf("Compass screen"); |
kkizirian | 2:c708e2027970 | 154 | screen_change = false; |
kkizirian | 2:c708e2027970 | 155 | } |
kkizirian | 3:0ca91f8afec5 | 156 | |
kkizirian | 3:0ca91f8afec5 | 157 | IMU.readMag(); |
kkizirian | 3:0ca91f8afec5 | 158 | h = calculateHeading(IMU.calcMag(IMU.mx), IMU.calcMag(IMU.my)); |
kkizirian | 3:0ca91f8afec5 | 159 | destinationLat = courseVec[0].getLat(); |
kkizirian | 3:0ca91f8afec5 | 160 | destinationLong = courseVec[0].getLong(); |
kkizirian | 3:0ca91f8afec5 | 161 | float diffLat = destinationLat - myLat; |
kkizirian | 3:0ca91f8afec5 | 162 | float diffLong = destinationLong - myLong; |
kkizirian | 3:0ca91f8afec5 | 163 | angleToDest = computeAngleToDestination(diffLat, diffLong); |
kkizirian | 3:0ca91f8afec5 | 164 | h = angleToDest - h; |
kkizirian | 3:0ca91f8afec5 | 165 | h = h - 90; |
kkizirian | 3:0ca91f8afec5 | 166 | if (h < 0) |
kkizirian | 3:0ca91f8afec5 | 167 | h = h + 360; |
kkizirian | 3:0ca91f8afec5 | 168 | if (h > 360) |
kkizirian | 3:0ca91f8afec5 | 169 | h = h - 360; |
kkizirian | 3:0ca91f8afec5 | 170 | xEnd = 0; |
kkizirian | 3:0ca91f8afec5 | 171 | yEnd = 0; |
kkizirian | 3:0ca91f8afec5 | 172 | float rads = 0.0; |
kkizirian | 3:0ca91f8afec5 | 173 | //uLCD.cls(); |
kkizirian | 3:0ca91f8afec5 | 174 | if (h < 90) { |
kkizirian | 3:0ca91f8afec5 | 175 | rads = h * PI / 180; |
kkizirian | 3:0ca91f8afec5 | 176 | xEnd = floor(63 * sin(rads) + .5); |
kkizirian | 3:0ca91f8afec5 | 177 | yEnd = floor(63 * cos(rads) + .5); |
kkizirian | 3:0ca91f8afec5 | 178 | xEnd = 63 + xEnd; |
kkizirian | 3:0ca91f8afec5 | 179 | yEnd = 63 + yEnd; |
kkizirian | 3:0ca91f8afec5 | 180 | } else if (90 < h < 180) { |
kkizirian | 3:0ca91f8afec5 | 181 | h = h - 90; |
kkizirian | 3:0ca91f8afec5 | 182 | rads = h * PI / 180; |
kkizirian | 3:0ca91f8afec5 | 183 | xEnd = floor(63 * cos(rads) + .5); |
kkizirian | 3:0ca91f8afec5 | 184 | yEnd = floor(63 * sin(rads) + .5); |
kkizirian | 3:0ca91f8afec5 | 185 | xEnd = 63 + xEnd; |
kkizirian | 3:0ca91f8afec5 | 186 | yEnd = 63 - yEnd; |
kkizirian | 3:0ca91f8afec5 | 187 | } else if (180 < h < 270) { |
kkizirian | 3:0ca91f8afec5 | 188 | h = h - 180; |
kkizirian | 3:0ca91f8afec5 | 189 | rads = h * PI / 180; |
kkizirian | 3:0ca91f8afec5 | 190 | xEnd = floor(63 * sin(rads) + .5); |
kkizirian | 3:0ca91f8afec5 | 191 | yEnd = floor(63 * cos(rads) + .5); |
kkizirian | 3:0ca91f8afec5 | 192 | xEnd = 63 - xEnd; |
kkizirian | 3:0ca91f8afec5 | 193 | yEnd = 63 - yEnd; |
kkizirian | 3:0ca91f8afec5 | 194 | } else if (270 < h) { |
kkizirian | 3:0ca91f8afec5 | 195 | h = h - 270; |
kkizirian | 3:0ca91f8afec5 | 196 | rads = h * PI / 180; |
kkizirian | 3:0ca91f8afec5 | 197 | xEnd = floor(63 * cos(rads) + .5); |
kkizirian | 3:0ca91f8afec5 | 198 | yEnd = floor(63 * sin(rads) + .5); |
kkizirian | 3:0ca91f8afec5 | 199 | xEnd = 63 - xEnd; |
kkizirian | 3:0ca91f8afec5 | 200 | yEnd = 63 + yEnd; |
kkizirian | 3:0ca91f8afec5 | 201 | } |
kkizirian | 3:0ca91f8afec5 | 202 | //uLCD.line(63, 63, lastXEnd, lastYEnd, BLACK); |
kkizirian | 3:0ca91f8afec5 | 203 | uLCD.cls(); |
kkizirian | 3:0ca91f8afec5 | 204 | uLCD.line(63, 63, xEnd, yEnd, WHITE); |
kkizirian | 3:0ca91f8afec5 | 205 | lastXEnd = xEnd; lastYEnd = yEnd; |
kkizirian | 3:0ca91f8afec5 | 206 | wait(.1); |
kkizirian | 2:c708e2027970 | 207 | break; |
kkizirian | 2:c708e2027970 | 208 | case 2: |
kkizirian | 2:c708e2027970 | 209 | if (screen_change) { |
kkizirian | 2:c708e2027970 | 210 | uLCD.cls(); |
kkizirian | 2:c708e2027970 | 211 | uLCD.printf("Distance screen"); |
kkizirian | 2:c708e2027970 | 212 | screen_change = false; |
kkizirian | 2:c708e2027970 | 213 | } |
kkizirian | 2:c708e2027970 | 214 | break; |
kkizirian | 2:c708e2027970 | 215 | |
kkizirian | 0:ce60014510e5 | 216 | } |
kkizirian | 0:ce60014510e5 | 217 | } |
kkizirian | 0:ce60014510e5 | 218 | } |
kkizirian | 0:ce60014510e5 | 219 | |
kkizirian | 0:ce60014510e5 | 220 | // Reads and processes GET and POST web data |
kkizirian | 0:ce60014510e5 | 221 | void ReadWebData() |
kkizirian | 0:ce60014510e5 | 222 | { |
kkizirian | 0:ce60014510e5 | 223 | wait_ms(200); |
kkizirian | 0:ce60014510e5 | 224 | esp.attach(NULL,Serial::RxIrq); |
kkizirian | 0:ce60014510e5 | 225 | DataRX=0; |
kkizirian | 2:c708e2027970 | 226 | |
kkizirian | 0:ce60014510e5 | 227 | if (!ip_found) { |
kkizirian | 3:0ca91f8afec5 | 228 | char* ip_loc = strstr(rx_buffer, "IP Address:"); |
kkizirian | 3:0ca91f8afec5 | 229 | if ((ip_loc != NULL)) { |
kkizirian | 3:0ca91f8afec5 | 230 | ip_loc = strstr(&ip_loc[10], "IP Address:"); |
kkizirian | 3:0ca91f8afec5 | 231 | if (ip_loc != NULL) { |
kkizirian | 3:0ca91f8afec5 | 232 | char ip_buf[16]; |
kkizirian | 3:0ca91f8afec5 | 233 | memset(ip_buf, '\0', sizeof(ip_buf)); |
kkizirian | 3:0ca91f8afec5 | 234 | memcpy(ip_buf, &ip_loc[12], 15); |
kkizirian | 3:0ca91f8afec5 | 235 | ip_line = ip_buf; |
kkizirian | 3:0ca91f8afec5 | 236 | ip_found = true; |
kkizirian | 3:0ca91f8afec5 | 237 | } |
kkizirian | 0:ce60014510e5 | 238 | } |
kkizirian | 0:ce60014510e5 | 239 | } |
kkizirian | 0:ce60014510e5 | 240 | |
kkizirian | 0:ce60014510e5 | 241 | memset(webdata, '\0', sizeof(webdata)); |
kkizirian | 0:ce60014510e5 | 242 | strcpy(webdata, rx_buffer); |
kkizirian | 0:ce60014510e5 | 243 | memset(rx_buffer, '\0', sizeof(rx_buffer)); |
kkizirian | 0:ce60014510e5 | 244 | rx_in = 0; |
kkizirian | 0:ce60014510e5 | 245 | rx_out = 0; |
kkizirian | 0:ce60014510e5 | 246 | |
kkizirian | 0:ce60014510e5 | 247 | // check web data for form information |
kkizirian | 0:ce60014510e5 | 248 | if (strstr(webdata, "building=none") == NULL) { |
kkizirian | 0:ce60014510e5 | 249 | char buildingBuf[4]; |
kkizirian | 0:ce60014510e5 | 250 | string building; |
kkizirian | 0:ce60014510e5 | 251 | char* buildingLoc = strstr(webdata, "building="); |
kkizirian | 0:ce60014510e5 | 252 | if (buildingLoc != NULL) { |
kkizirian | 0:ce60014510e5 | 253 | memcpy(buildingBuf, &buildingLoc[9], 3); |
kkizirian | 0:ce60014510e5 | 254 | buildingBuf[3] = '\0'; |
kkizirian | 0:ce60014510e5 | 255 | building = buildingBuf;; |
kkizirian | 0:ce60014510e5 | 256 | } |
kkizirian | 0:ce60014510e5 | 257 | |
kkizirian | 0:ce60014510e5 | 258 | char hourBuf[3]; |
kkizirian | 0:ce60014510e5 | 259 | int hour = -1; |
kkizirian | 0:ce60014510e5 | 260 | char* hourLoc = strstr(webdata, "hour="); |
kkizirian | 0:ce60014510e5 | 261 | if (hourLoc != NULL) { |
kkizirian | 0:ce60014510e5 | 262 | memcpy(hourBuf, &hourLoc[5], 2); |
kkizirian | 0:ce60014510e5 | 263 | hourBuf[2] = '\0'; |
kkizirian | 0:ce60014510e5 | 264 | hour = atoi(hourBuf); |
kkizirian | 0:ce60014510e5 | 265 | } |
kkizirian | 0:ce60014510e5 | 266 | |
kkizirian | 0:ce60014510e5 | 267 | char minuteBuf[3]; |
kkizirian | 0:ce60014510e5 | 268 | int minute = -1; |
kkizirian | 0:ce60014510e5 | 269 | char* minuteLoc = strstr(webdata, "minute="); |
kkizirian | 0:ce60014510e5 | 270 | if (minuteLoc != NULL) { |
kkizirian | 0:ce60014510e5 | 271 | memcpy(minuteBuf, &minuteLoc[7], 2); |
kkizirian | 0:ce60014510e5 | 272 | minuteBuf[2] = '\0'; |
kkizirian | 0:ce60014510e5 | 273 | minute = atoi(minuteBuf); |
kkizirian | 0:ce60014510e5 | 274 | } |
kkizirian | 0:ce60014510e5 | 275 | |
kkizirian | 0:ce60014510e5 | 276 | char ampmBuf[3]; |
kkizirian | 0:ce60014510e5 | 277 | string ampm; |
kkizirian | 0:ce60014510e5 | 278 | char* ampmLoc = strstr(webdata, "AMPM="); |
kkizirian | 0:ce60014510e5 | 279 | if (ampmLoc != NULL) { |
kkizirian | 0:ce60014510e5 | 280 | memcpy(ampmBuf, &mLoc[5], 2); |
kkizirian | 0:ce60014510e5 | 281 | ampmBuf[2] = '\0'; |
kkizirian | 0:ce60014510e5 | 282 | ampm = ampmBuf; |
kkizirian | 0:ce60014510e5 | 283 | } |
kkizirian | 0:ce60014510e5 | 284 | |
kkizirian | 0:ce60014510e5 | 285 | if (minute != -1) { |
kkizirian | 0:ce60014510e5 | 286 | Course newCourse(building, hour, minute, ampm); |
kkizirian | 0:ce60014510e5 | 287 | addCourseToVector(courseVec, newCourse); |
kkizirian | 0:ce60014510e5 | 288 | update = 1; |
kkizirian | 0:ce60014510e5 | 289 | } |
kkizirian | 0:ce60014510e5 | 290 | } |
kkizirian | 0:ce60014510e5 | 291 | } |
kkizirian | 0:ce60014510e5 | 292 | // Starts webserver |
kkizirian | 0:ce60014510e5 | 293 | void startserver() |
kkizirian | 0:ce60014510e5 | 294 | { |
kkizirian | 3:0ca91f8afec5 | 295 | uLCD.cls(); |
kkizirian | 3:0ca91f8afec5 | 296 | uLCD.locate(0,0); |
kkizirian | 3:0ca91f8afec5 | 297 | uLCD.printf("Starting server..."); |
kkizirian | 0:ce60014510e5 | 298 | pc.printf("++++++++++ Resetting ESP ++++++++++\r\n"); |
kkizirian | 0:ce60014510e5 | 299 | strcpy(cmdbuff,"node.restart()\r\n"); |
kkizirian | 0:ce60014510e5 | 300 | SendCMD(); |
kkizirian | 0:ce60014510e5 | 301 | wait(2); |
kkizirian | 0:ce60014510e5 | 302 | read_line(); |
kkizirian | 2:c708e2027970 | 303 | |
kkizirian | 0:ce60014510e5 | 304 | pc.printf("\n---------- Connecting to AP ----------\r\n"); |
kkizirian | 0:ce60014510e5 | 305 | //pc.printf("ssid = %s pwd = %s\r\n",ssid,pwd); |
kkizirian | 0:ce60014510e5 | 306 | strcpy(cmdbuff, "wifi.sta.config(\""); |
kkizirian | 0:ce60014510e5 | 307 | strcat(cmdbuff, ssid); |
kkizirian | 0:ce60014510e5 | 308 | strcat(cmdbuff, "\",\""); |
kkizirian | 0:ce60014510e5 | 309 | strcat(cmdbuff, pwd); |
kkizirian | 0:ce60014510e5 | 310 | strcat(cmdbuff, "\")\r\n"); |
kkizirian | 0:ce60014510e5 | 311 | SendCMD(); |
kkizirian | 0:ce60014510e5 | 312 | wait(3); |
kkizirian | 0:ce60014510e5 | 313 | read_line(); |
kkizirian | 2:c708e2027970 | 314 | |
kkizirian | 0:ce60014510e5 | 315 | pc.printf("\n++++++++++ Starting Server ++++++++++\r\n> "); |
kkizirian | 0:ce60014510e5 | 316 | |
kkizirian | 0:ce60014510e5 | 317 | //create server |
kkizirian | 0:ce60014510e5 | 318 | sprintf(cmdbuff, "srv=net.createServer(net.TCP,%d)\r\n",SERVtimeout); |
kkizirian | 0:ce60014510e5 | 319 | SendCMD(); |
kkizirian | 0:ce60014510e5 | 320 | read_line(); |
kkizirian | 0:ce60014510e5 | 321 | wait(0.5); |
kkizirian | 0:ce60014510e5 | 322 | strcpy(cmdbuff,"srv:listen(80,function(conn)\r\n"); |
kkizirian | 0:ce60014510e5 | 323 | SendCMD(); |
kkizirian | 0:ce60014510e5 | 324 | read_line(); |
kkizirian | 0:ce60014510e5 | 325 | wait(0.3); |
kkizirian | 2:c708e2027970 | 326 | strcpy(cmdbuff,"conn:on(\"receive\",function(conn,payload) \r\n"); |
kkizirian | 2:c708e2027970 | 327 | SendCMD(); |
kkizirian | 2:c708e2027970 | 328 | read_line(); |
kkizirian | 2:c708e2027970 | 329 | wait(0.3); |
kkizirian | 0:ce60014510e5 | 330 | |
kkizirian | 2:c708e2027970 | 331 | //print data to mbed |
kkizirian | 2:c708e2027970 | 332 | strcpy(cmdbuff,"print(payload)\r\n"); |
kkizirian | 2:c708e2027970 | 333 | SendCMD(); |
kkizirian | 2:c708e2027970 | 334 | read_line(); |
kkizirian | 2:c708e2027970 | 335 | wait(0.2); |
kkizirian | 0:ce60014510e5 | 336 | |
kkizirian | 2:c708e2027970 | 337 | //web page data |
kkizirian | 2:c708e2027970 | 338 | strcpy(cmdbuff,"conn:send('<!DOCTYPE html><html><head><title>Class Scheduler!</title></head><body><h1>Class Scheduler</h1>')\r\n"); |
kkizirian | 2:c708e2027970 | 339 | SendCMD(); |
kkizirian | 2:c708e2027970 | 340 | read_line(); |
kkizirian | 2:c708e2027970 | 341 | wait(0.4); |
kkizirian | 2:c708e2027970 | 342 | strcpy(cmdbuff,"conn:send('<p><div class = \"content\">Input your schedule below!</div></p>')\r\n"); |
kkizirian | 2:c708e2027970 | 343 | SendCMD(); |
kkizirian | 2:c708e2027970 | 344 | read_line(); |
kkizirian | 2:c708e2027970 | 345 | wait(0.2); |
kkizirian | 2:c708e2027970 | 346 | strcpy(cmdbuff,"conn:send('<form method=\"POST\">')\r\n"); |
kkizirian | 2:c708e2027970 | 347 | SendCMD(); |
kkizirian | 2:c708e2027970 | 348 | read_line(); |
kkizirian | 2:c708e2027970 | 349 | wait(0.3); |
kkizirian | 2:c708e2027970 | 350 | strcpy(cmdbuff, "conn:send('<select name=\"building\">')\r\n"); |
kkizirian | 2:c708e2027970 | 351 | SendCMD(); |
kkizirian | 2:c708e2027970 | 352 | read_line(); |
kkizirian | 2:c708e2027970 | 353 | wait(0.3); |
kkizirian | 2:c708e2027970 | 354 | strcpy(cmdbuff, "conn:send('<option value =\"none\">None</option>')\r\n"); |
kkizirian | 2:c708e2027970 | 355 | SendCMD(); |
kkizirian | 2:c708e2027970 | 356 | read_line(); |
kkizirian | 2:c708e2027970 | 357 | wait(0.3); |
kkizirian | 2:c708e2027970 | 358 | strcpy(cmdbuff,"conn:send('<option value=\"CLH\">Clough Undergraduate Learning Commons</option>')\r\n"); |
kkizirian | 2:c708e2027970 | 359 | SendCMD(); |
kkizirian | 2:c708e2027970 | 360 | read_line(); |
kkizirian | 2:c708e2027970 | 361 | wait(0.3); |
kkizirian | 2:c708e2027970 | 362 | strcpy(cmdbuff,"conn:send('<option value=\"COC\">College of Computing</option>')\r\n"); |
kkizirian | 2:c708e2027970 | 363 | SendCMD(); |
kkizirian | 2:c708e2027970 | 364 | read_line(); |
kkizirian | 2:c708e2027970 | 365 | wait(0.3); |
kkizirian | 2:c708e2027970 | 366 | strcpy(cmdbuff, "conn:send('<option value=\"KLS\">Klaus Advanced Computing Building</option>')\r\n"); |
kkizirian | 2:c708e2027970 | 367 | SendCMD(); |
kkizirian | 2:c708e2027970 | 368 | read_line(); |
kkizirian | 2:c708e2027970 | 369 | wait(0.3); |
kkizirian | 2:c708e2027970 | 370 | strcpy(cmdbuff, "conn:send('<option value=\"VAN\">Van Leer</option></select>')\r\n"); |
kkizirian | 2:c708e2027970 | 371 | SendCMD(); |
kkizirian | 2:c708e2027970 | 372 | read_line(); |
kkizirian | 2:c708e2027970 | 373 | wait(0.3); |
kkizirian | 2:c708e2027970 | 374 | strcpy(cmdbuff, "conn:send('<select name=\"hour\"><option value=\"01\">1</option><option value=\"02\">2</option>')\r\n"); |
kkizirian | 2:c708e2027970 | 375 | SendCMD(); |
kkizirian | 2:c708e2027970 | 376 | read_line(); |
kkizirian | 2:c708e2027970 | 377 | wait(.3); |
kkizirian | 2:c708e2027970 | 378 | strcpy(cmdbuff, "conn:send('<option value=\"03\">3</option><option value=\"04\">4</option><option value=\"05\">5</option>')\r\n"); |
kkizirian | 2:c708e2027970 | 379 | SendCMD(); |
kkizirian | 2:c708e2027970 | 380 | read_line(); |
kkizirian | 2:c708e2027970 | 381 | wait(.3); |
kkizirian | 2:c708e2027970 | 382 | strcpy(cmdbuff, "conn:send('<option value=\"06\">6</option><option value=\"07\">7</option><option value=\"08\">8</option>')\r\n"); |
kkizirian | 2:c708e2027970 | 383 | SendCMD(); |
kkizirian | 2:c708e2027970 | 384 | read_line(); |
kkizirian | 2:c708e2027970 | 385 | wait(0.3); |
kkizirian | 2:c708e2027970 | 386 | 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 | 2:c708e2027970 | 387 | SendCMD(); |
kkizirian | 2:c708e2027970 | 388 | read_line(); |
kkizirian | 2:c708e2027970 | 389 | wait(0.3); |
kkizirian | 2:c708e2027970 | 390 | strcpy(cmdbuff, "conn:send('<select name=\"minute\"><option value=\"00\">00</option><option value=\"05\">05</option>')\r\n"); |
kkizirian | 2:c708e2027970 | 391 | SendCMD(); |
kkizirian | 2:c708e2027970 | 392 | read_line(); |
kkizirian | 2:c708e2027970 | 393 | wait(0.3); |
kkizirian | 2:c708e2027970 | 394 | strcpy(cmdbuff, "conn:send('<option value=\"10\">10</option><option value=\"15\">15</option>')\r\n"); |
kkizirian | 2:c708e2027970 | 395 | SendCMD(); |
kkizirian | 2:c708e2027970 | 396 | read_line(); |
kkizirian | 2:c708e2027970 | 397 | wait(0.3); |
kkizirian | 2:c708e2027970 | 398 | strcpy(cmdbuff, "conn:send('<option value=\"20\">20</option><option value=\"25\">25</option>')\r\n"); |
kkizirian | 2:c708e2027970 | 399 | SendCMD(); |
kkizirian | 2:c708e2027970 | 400 | read_line(); |
kkizirian | 2:c708e2027970 | 401 | wait(0.3); |
kkizirian | 2:c708e2027970 | 402 | strcpy(cmdbuff, "conn:send('<option value=\"30\">30</option><option value=\"35\">35</option>')\r\n"); |
kkizirian | 2:c708e2027970 | 403 | SendCMD(); |
kkizirian | 2:c708e2027970 | 404 | read_line(); |
kkizirian | 2:c708e2027970 | 405 | wait(0.3); |
kkizirian | 2:c708e2027970 | 406 | strcpy(cmdbuff, "conn:send('<option value=\"40\">40</option><option value=\"45\">45</option>')\r\n"); |
kkizirian | 2:c708e2027970 | 407 | SendCMD(); |
kkizirian | 2:c708e2027970 | 408 | read_line(); |
kkizirian | 2:c708e2027970 | 409 | wait(0.3); |
kkizirian | 2:c708e2027970 | 410 | strcpy(cmdbuff, "conn:send('<option value=\"50\">50</option><option value=\"55\">55</option></select>')\r\n"); |
kkizirian | 2:c708e2027970 | 411 | SendCMD(); |
kkizirian | 2:c708e2027970 | 412 | read_line(); |
kkizirian | 2:c708e2027970 | 413 | wait(0.3); |
kkizirian | 2:c708e2027970 | 414 | strcpy(cmdbuff, "conn:send('<select name=\"AMPM\"><option value=\"AM\">AM</option><option value=\"PM\">PM</option></select><br></br>')\r\n"); |
kkizirian | 2:c708e2027970 | 415 | SendCMD(); |
kkizirian | 2:c708e2027970 | 416 | read_line(); |
kkizirian | 2:c708e2027970 | 417 | wait(0.3); |
kkizirian | 0:ce60014510e5 | 418 | |
kkizirian | 2:c708e2027970 | 419 | strcpy(cmdbuff,"conn:send('<p><input type=\"submit\" value=\"Add Class\"></form>')\r\n"); |
kkizirian | 2:c708e2027970 | 420 | SendCMD(); |
kkizirian | 2:c708e2027970 | 421 | read_line(); |
kkizirian | 2:c708e2027970 | 422 | wait(0.3); |
kkizirian | 2:c708e2027970 | 423 | strcpy(cmdbuff, "conn:send('</body></html>')\r\n"); |
kkizirian | 2:c708e2027970 | 424 | SendCMD(); |
kkizirian | 2:c708e2027970 | 425 | read_line(); |
kkizirian | 2:c708e2027970 | 426 | wait(0.5); |
kkizirian | 2:c708e2027970 | 427 | // end web page data |
kkizirian | 2:c708e2027970 | 428 | strcpy(cmdbuff, "conn:on(\"sent\",function(conn) conn:close() end)\r\n"); // close current connection |
kkizirian | 2:c708e2027970 | 429 | SendCMD(); |
kkizirian | 2:c708e2027970 | 430 | read_line(); |
kkizirian | 2:c708e2027970 | 431 | wait(0.3); |
kkizirian | 2:c708e2027970 | 432 | strcpy(cmdbuff, "end)\r\n"); |
kkizirian | 2:c708e2027970 | 433 | SendCMD(); |
kkizirian | 2:c708e2027970 | 434 | read_line(); |
kkizirian | 2:c708e2027970 | 435 | wait(0.2); |
kkizirian | 0:ce60014510e5 | 436 | strcpy(cmdbuff, "end)\r\n"); |
kkizirian | 0:ce60014510e5 | 437 | SendCMD(); |
kkizirian | 0:ce60014510e5 | 438 | read_line(); |
kkizirian | 0:ce60014510e5 | 439 | wait(0.2); |
kkizirian | 0:ce60014510e5 | 440 | |
kkizirian | 0:ce60014510e5 | 441 | strcpy(cmdbuff, "tmr.alarm(0, 1000, 1, function()\r\n"); |
kkizirian | 0:ce60014510e5 | 442 | SendCMD(); |
kkizirian | 0:ce60014510e5 | 443 | read_line(); |
kkizirian | 0:ce60014510e5 | 444 | wait(0.2); |
kkizirian | 0:ce60014510e5 | 445 | strcpy(cmdbuff, "if wifi.sta.getip() == nil then\r\n"); |
kkizirian | 0:ce60014510e5 | 446 | SendCMD(); |
kkizirian | 0:ce60014510e5 | 447 | read_line(); |
kkizirian | 0:ce60014510e5 | 448 | wait(0.2); |
kkizirian | 0:ce60014510e5 | 449 | strcpy(cmdbuff, "print(\"Connecting to AP...\\n\")\r\n"); |
kkizirian | 0:ce60014510e5 | 450 | SendCMD(); |
kkizirian | 0:ce60014510e5 | 451 | read_line(); |
kkizirian | 0:ce60014510e5 | 452 | wait(0.2); |
kkizirian | 0:ce60014510e5 | 453 | strcpy(cmdbuff, "else\r\n"); |
kkizirian | 0:ce60014510e5 | 454 | SendCMD(); |
kkizirian | 0:ce60014510e5 | 455 | read_line(); |
kkizirian | 0:ce60014510e5 | 456 | wait(0.2); |
kkizirian | 0:ce60014510e5 | 457 | strcpy(cmdbuff, "ip, nm, gw=wifi.sta.getip()\r\n"); |
kkizirian | 0:ce60014510e5 | 458 | SendCMD(); |
kkizirian | 0:ce60014510e5 | 459 | read_line(); |
kkizirian | 0:ce60014510e5 | 460 | wait(0.2); |
kkizirian | 0:ce60014510e5 | 461 | strcpy(cmdbuff,"print(\"IP Address: \",ip)\r\n"); |
kkizirian | 0:ce60014510e5 | 462 | SendCMD(); |
kkizirian | 0:ce60014510e5 | 463 | read_line(); |
kkizirian | 0:ce60014510e5 | 464 | wait(0.2); |
kkizirian | 0:ce60014510e5 | 465 | strcpy(cmdbuff,"tmr.stop(0)\r\n"); |
kkizirian | 0:ce60014510e5 | 466 | SendCMD(); |
kkizirian | 0:ce60014510e5 | 467 | read_line(); |
kkizirian | 0:ce60014510e5 | 468 | wait(0.2); |
kkizirian | 0:ce60014510e5 | 469 | strcpy(cmdbuff,"end\r\n"); |
kkizirian | 0:ce60014510e5 | 470 | SendCMD(); |
kkizirian | 0:ce60014510e5 | 471 | read_line(); |
kkizirian | 0:ce60014510e5 | 472 | wait(0.2); |
kkizirian | 0:ce60014510e5 | 473 | strcpy(cmdbuff,"end)\r\n"); |
kkizirian | 0:ce60014510e5 | 474 | SendCMD(); |
kkizirian | 0:ce60014510e5 | 475 | read_line(); |
kkizirian | 0:ce60014510e5 | 476 | wait(0.2); |
kkizirian | 0:ce60014510e5 | 477 | |
kkizirian | 0:ce60014510e5 | 478 | pc.printf("\n\n++++++++++ Ready ++++++++++\r\n\n"); |
kkizirian | 0:ce60014510e5 | 479 | } |
kkizirian | 0:ce60014510e5 | 480 | |
kkizirian | 0:ce60014510e5 | 481 | // ESP Command data send |
kkizirian | 0:ce60014510e5 | 482 | void SendCMD() |
kkizirian | 0:ce60014510e5 | 483 | { |
kkizirian | 0:ce60014510e5 | 484 | int i; |
kkizirian | 0:ce60014510e5 | 485 | char temp_char; |
kkizirian | 0:ce60014510e5 | 486 | bool empty; |
kkizirian | 0:ce60014510e5 | 487 | i = 0; |
kkizirian | 0:ce60014510e5 | 488 | // Start Critical Section - don't interrupt while changing global buffer variables |
kkizirian | 0:ce60014510e5 | 489 | NVIC_DisableIRQ(UART1_IRQn); |
kkizirian | 0:ce60014510e5 | 490 | empty = (tx_in == tx_out); |
kkizirian | 0:ce60014510e5 | 491 | while ((i==0) || (cmdbuff[i-1] != '\n')) { |
kkizirian | 0:ce60014510e5 | 492 | // Wait if buffer full |
kkizirian | 0:ce60014510e5 | 493 | if (((tx_in + 1) % buffer_size) == tx_out) { |
kkizirian | 0:ce60014510e5 | 494 | // End Critical Section - need to let interrupt routine empty buffer by sending |
kkizirian | 0:ce60014510e5 | 495 | NVIC_EnableIRQ(UART1_IRQn); |
kkizirian | 0:ce60014510e5 | 496 | while (((tx_in + 1) % buffer_size) == tx_out) { |
kkizirian | 0:ce60014510e5 | 497 | } |
kkizirian | 0:ce60014510e5 | 498 | // Start Critical Section - don't interrupt while changing global buffer variables |
kkizirian | 0:ce60014510e5 | 499 | NVIC_DisableIRQ(UART1_IRQn); |
kkizirian | 0:ce60014510e5 | 500 | } |
kkizirian | 0:ce60014510e5 | 501 | tx_buffer[tx_in] = cmdbuff[i]; |
kkizirian | 0:ce60014510e5 | 502 | i++; |
kkizirian | 0:ce60014510e5 | 503 | tx_in = (tx_in + 1) % buffer_size; |
kkizirian | 0:ce60014510e5 | 504 | } |
kkizirian | 0:ce60014510e5 | 505 | if (esp.writeable() && (empty)) { |
kkizirian | 0:ce60014510e5 | 506 | temp_char = tx_buffer[tx_out]; |
kkizirian | 0:ce60014510e5 | 507 | tx_out = (tx_out + 1) % buffer_size; |
kkizirian | 0:ce60014510e5 | 508 | // Send first character to start tx interrupts, if stopped |
kkizirian | 0:ce60014510e5 | 509 | esp.putc(temp_char); |
kkizirian | 0:ce60014510e5 | 510 | } |
kkizirian | 0:ce60014510e5 | 511 | // End Critical Section |
kkizirian | 0:ce60014510e5 | 512 | NVIC_EnableIRQ(UART1_IRQn); |
kkizirian | 0:ce60014510e5 | 513 | return; |
kkizirian | 0:ce60014510e5 | 514 | } |
kkizirian | 0:ce60014510e5 | 515 | |
kkizirian | 0:ce60014510e5 | 516 | // Read a line from the large rx buffer from rx interrupt routine |
kkizirian | 2:c708e2027970 | 517 | void read_line() |
kkizirian | 2:c708e2027970 | 518 | { |
kkizirian | 0:ce60014510e5 | 519 | int i; |
kkizirian | 0:ce60014510e5 | 520 | i = 0; |
kkizirian | 0:ce60014510e5 | 521 | // Start Critical Section - don't interrupt while changing global buffer variables |
kkizirian | 0:ce60014510e5 | 522 | NVIC_DisableIRQ(UART1_IRQn); |
kkizirian | 0:ce60014510e5 | 523 | // Loop reading rx buffer characters until end of line character |
kkizirian | 0:ce60014510e5 | 524 | while ((i==0) || (rx_line[i-1] != '\r')) { |
kkizirian | 0:ce60014510e5 | 525 | // Wait if buffer empty |
kkizirian | 0:ce60014510e5 | 526 | if (rx_in == rx_out) { |
kkizirian | 0:ce60014510e5 | 527 | // End Critical Section - need to allow rx interrupt to get new characters for buffer |
kkizirian | 0:ce60014510e5 | 528 | NVIC_EnableIRQ(UART1_IRQn); |
kkizirian | 0:ce60014510e5 | 529 | while (rx_in == rx_out) { |
kkizirian | 0:ce60014510e5 | 530 | } |
kkizirian | 0:ce60014510e5 | 531 | // Start Critical Section - don't interrupt while changing global buffer variables |
kkizirian | 0:ce60014510e5 | 532 | NVIC_DisableIRQ(UART1_IRQn); |
kkizirian | 0:ce60014510e5 | 533 | } |
kkizirian | 0:ce60014510e5 | 534 | rx_line[i] = rx_buffer[rx_out]; |
kkizirian | 0:ce60014510e5 | 535 | i++; |
kkizirian | 0:ce60014510e5 | 536 | rx_out = (rx_out + 1) % buffer_size; |
kkizirian | 0:ce60014510e5 | 537 | } |
kkizirian | 0:ce60014510e5 | 538 | // End Critical Section |
kkizirian | 0:ce60014510e5 | 539 | NVIC_EnableIRQ(UART1_IRQn); |
kkizirian | 0:ce60014510e5 | 540 | rx_line[i-1] = 0; |
kkizirian | 0:ce60014510e5 | 541 | |
kkizirian | 0:ce60014510e5 | 542 | return; |
kkizirian | 0:ce60014510e5 | 543 | } |
kkizirian | 0:ce60014510e5 | 544 | |
kkizirian | 0:ce60014510e5 | 545 | // Interupt Routine to read in data from serial port |
kkizirian | 2:c708e2027970 | 546 | void Rx_interrupt() |
kkizirian | 2:c708e2027970 | 547 | { |
kkizirian | 0:ce60014510e5 | 548 | DataRX=1; |
kkizirian | 0:ce60014510e5 | 549 | // Loop just in case more than one character is in UART's receive FIFO buffer |
kkizirian | 0:ce60014510e5 | 550 | // Stop if buffer full |
kkizirian | 0:ce60014510e5 | 551 | while ((esp.readable()) && (((rx_in + 1) % buffer_size) != rx_out)) { |
kkizirian | 0:ce60014510e5 | 552 | rx_buffer[rx_in] = esp.getc(); |
kkizirian | 0:ce60014510e5 | 553 | // Uncomment to Echo to USB serial to watch data flow |
kkizirian | 0:ce60014510e5 | 554 | pc.putc(rx_buffer[rx_in]); |
kkizirian | 0:ce60014510e5 | 555 | rx_in = (rx_in + 1) % buffer_size; |
kkizirian | 0:ce60014510e5 | 556 | } |
kkizirian | 0:ce60014510e5 | 557 | return; |
kkizirian | 0:ce60014510e5 | 558 | } |
kkizirian | 0:ce60014510e5 | 559 | |
kkizirian | 0:ce60014510e5 | 560 | // Interupt Routine to write out data to serial port |
kkizirian | 2:c708e2027970 | 561 | void Tx_interrupt() |
kkizirian | 2:c708e2027970 | 562 | { |
kkizirian | 0:ce60014510e5 | 563 | // Loop to fill more than one character in UART's transmit FIFO buffer |
kkizirian | 0:ce60014510e5 | 564 | // Stop if buffer empty |
kkizirian | 0:ce60014510e5 | 565 | while ((esp.writeable()) && (tx_in != tx_out)) { |
kkizirian | 0:ce60014510e5 | 566 | esp.putc(tx_buffer[tx_out]); |
kkizirian | 0:ce60014510e5 | 567 | tx_out = (tx_out + 1) % buffer_size; |
kkizirian | 0:ce60014510e5 | 568 | } |
kkizirian | 0:ce60014510e5 | 569 | return; |
kkizirian | 0:ce60014510e5 | 570 | } |
kkizirian | 0:ce60014510e5 | 571 | |
kkizirian | 2:c708e2027970 | 572 | int addCourseToVector(vector<Course>& cVec, Course newCourse) |
kkizirian | 2:c708e2027970 | 573 | { |
kkizirian | 0:ce60014510e5 | 574 | int numIterations = 0; |
kkizirian | 0:ce60014510e5 | 575 | |
kkizirian | 0:ce60014510e5 | 576 | if (cVec.size() == 0) { |
kkizirian | 0:ce60014510e5 | 577 | cVec.push_back(newCourse); |
kkizirian | 0:ce60014510e5 | 578 | return 1; |
kkizirian | 0:ce60014510e5 | 579 | } |
kkizirian | 0:ce60014510e5 | 580 | |
kkizirian | 0:ce60014510e5 | 581 | for (int i = 0; i < cVec.size(); i++) { |
kkizirian | 0:ce60014510e5 | 582 | numIterations++; |
kkizirian | 0:ce60014510e5 | 583 | if (cVec[i].getAMPM_toInt() < newCourse.getAMPM_toInt()) |
kkizirian | 0:ce60014510e5 | 584 | continue; |
kkizirian | 0:ce60014510e5 | 585 | else if (newCourse.getAMPM_toInt() < cVec[i].getAMPM_toInt()) { |
kkizirian | 0:ce60014510e5 | 586 | cVec.insert(cVec.begin()+i, newCourse); |
kkizirian | 0:ce60014510e5 | 587 | return 1; |
kkizirian | 2:c708e2027970 | 588 | } else if (cVec[i].getAMPM_toInt() == newCourse.getAMPM_toInt()) { |
kkizirian | 0:ce60014510e5 | 589 | if (cVec[i].getHour_forCompare() < newCourse.getHour_forCompare()) |
kkizirian | 0:ce60014510e5 | 590 | continue; |
kkizirian | 0:ce60014510e5 | 591 | else if (newCourse.getHour_forCompare() < cVec[i].getHour_forCompare()) { |
kkizirian | 0:ce60014510e5 | 592 | cVec.insert(cVec.begin()+i, newCourse); |
kkizirian | 0:ce60014510e5 | 593 | return 1; |
kkizirian | 2:c708e2027970 | 594 | } else if (cVec[i].getHour_forCompare() == newCourse.getHour_forCompare()) { |
kkizirian | 0:ce60014510e5 | 595 | if (cVec[i].getMinute() < newCourse.getMinute()) |
kkizirian | 0:ce60014510e5 | 596 | continue; |
kkizirian | 0:ce60014510e5 | 597 | else if (newCourse.getMinute() < cVec[i].getMinute()) { |
kkizirian | 0:ce60014510e5 | 598 | cVec.insert(cVec.begin()+i, newCourse); |
kkizirian | 0:ce60014510e5 | 599 | return 1; |
kkizirian | 2:c708e2027970 | 600 | } else if (cVec[i].getMinute() == newCourse.getMinute()) { |
kkizirian | 0:ce60014510e5 | 601 | uLCD.cls(); |
kkizirian | 0:ce60014510e5 | 602 | uLCD.locate(0,0); |
kkizirian | 0:ce60014510e5 | 603 | uLCD.printf("Can not add coure!"); |
kkizirian | 0:ce60014510e5 | 604 | uLCD.locate(0,1); |
kkizirian | 0:ce60014510e5 | 605 | uLCD.printf("Course already at"); |
kkizirian | 0:ce60014510e5 | 606 | uLCD.locate(0,2); |
kkizirian | 0:ce60014510e5 | 607 | uLCD.printf("%i:%s%s", newCourse.getHour(), newCourse.getMinute_toString(), newCourse.getAMPM()); |
kkizirian | 0:ce60014510e5 | 608 | wait(5); |
kkizirian | 0:ce60014510e5 | 609 | return 0; |
kkizirian | 0:ce60014510e5 | 610 | } |
kkizirian | 0:ce60014510e5 | 611 | } |
kkizirian | 0:ce60014510e5 | 612 | } |
kkizirian | 0:ce60014510e5 | 613 | } |
kkizirian | 0:ce60014510e5 | 614 | |
kkizirian | 0:ce60014510e5 | 615 | if (numIterations == cVec.size()) { |
kkizirian | 0:ce60014510e5 | 616 | cVec.push_back(newCourse); |
kkizirian | 0:ce60014510e5 | 617 | return 1; |
kkizirian | 0:ce60014510e5 | 618 | } |
kkizirian | 0:ce60014510e5 | 619 | |
kkizirian | 0:ce60014510e5 | 620 | return 0; |
kkizirian | 0:ce60014510e5 | 621 | } |
kkizirian | 0:ce60014510e5 | 622 | |
kkizirian | 2:c708e2027970 | 623 | void writeClassFile(vector<Course>& cVec) |
kkizirian | 2:c708e2027970 | 624 | { |
kkizirian | 0:ce60014510e5 | 625 | FILE *writeClass = fopen("/sd/classdir/classes.txt", "w"); |
kkizirian | 0:ce60014510e5 | 626 | if (writeClass != NULL) { |
kkizirian | 0:ce60014510e5 | 627 | string line = ""; |
kkizirian | 0:ce60014510e5 | 628 | for (int i = 0; i < cVec.size(); i++) { |
kkizirian | 0:ce60014510e5 | 629 | if (i != (cVec.size() - 1)) |
kkizirian | 0:ce60014510e5 | 630 | line = cVec[i].getFileString() + "\n"; |
kkizirian | 0:ce60014510e5 | 631 | else |
kkizirian | 0:ce60014510e5 | 632 | line = cVec[i].getFileString(); |
kkizirian | 0:ce60014510e5 | 633 | fprintf(writeClass, line.c_str()); |
kkizirian | 0:ce60014510e5 | 634 | } |
kkizirian | 0:ce60014510e5 | 635 | fclose(writeClass); |
kkizirian | 0:ce60014510e5 | 636 | } |
kkizirian | 0:ce60014510e5 | 637 | } |
kkizirian | 0:ce60014510e5 | 638 | |
kkizirian | 2:c708e2027970 | 639 | void readClassFile(vector<Course>& cVec) |
kkizirian | 2:c708e2027970 | 640 | { |
kkizirian | 0:ce60014510e5 | 641 | cVec.clear(); |
kkizirian | 0:ce60014510e5 | 642 | |
kkizirian | 0:ce60014510e5 | 643 | FILE *readFp = fopen("/sd/classdir/classes.txt", "r"); |
kkizirian | 0:ce60014510e5 | 644 | char line[15]; |
kkizirian | 0:ce60014510e5 | 645 | char buildingBuf[4]; |
kkizirian | 0:ce60014510e5 | 646 | char hourBuf[3]; |
kkizirian | 0:ce60014510e5 | 647 | int hour; |
kkizirian | 0:ce60014510e5 | 648 | char minuteBuf[3]; |
kkizirian | 0:ce60014510e5 | 649 | int minute; |
kkizirian | 0:ce60014510e5 | 650 | char ampmBuf[3]; |
kkizirian | 0:ce60014510e5 | 651 | uLCD.cls(); |
kkizirian | 0:ce60014510e5 | 652 | uLCD.locate(0, 1); |
kkizirian | 0:ce60014510e5 | 653 | uLCD.printf("Reading class file..."); |
kkizirian | 2:c708e2027970 | 654 | |
kkizirian | 0:ce60014510e5 | 655 | memset(buildingBuf, 0, sizeof(buildingBuf)); |
kkizirian | 0:ce60014510e5 | 656 | memset(hourBuf, 0, sizeof(hourBuf)); |
kkizirian | 0:ce60014510e5 | 657 | memset(minuteBuf, 0, sizeof(minuteBuf)); |
kkizirian | 0:ce60014510e5 | 658 | memset(ampmBuf, 0, sizeof(ampmBuf)); |
kkizirian | 0:ce60014510e5 | 659 | memset(line, 0, sizeof(line)); |
kkizirian | 0:ce60014510e5 | 660 | |
kkizirian | 0:ce60014510e5 | 661 | if (readFp == NULL) |
kkizirian | 0:ce60014510e5 | 662 | return; |
kkizirian | 0:ce60014510e5 | 663 | else { |
kkizirian | 0:ce60014510e5 | 664 | while (!feof(readFp)) { |
kkizirian | 0:ce60014510e5 | 665 | fgets(line, 15, readFp); |
kkizirian | 0:ce60014510e5 | 666 | if(line[8] == NULL) |
kkizirian | 0:ce60014510e5 | 667 | continue; |
kkizirian | 0:ce60014510e5 | 668 | memcpy(buildingBuf, line, 3); |
kkizirian | 0:ce60014510e5 | 669 | memcpy(hourBuf, &line[4], 2); |
kkizirian | 0:ce60014510e5 | 670 | memcpy(minuteBuf, &line[7], 2); |
kkizirian | 0:ce60014510e5 | 671 | memcpy(ampmBuf, &line[10], 2); |
kkizirian | 0:ce60014510e5 | 672 | |
kkizirian | 0:ce60014510e5 | 673 | string building = buildingBuf; |
kkizirian | 0:ce60014510e5 | 674 | hour = atoi(hourBuf); |
kkizirian | 0:ce60014510e5 | 675 | minute = atoi(minuteBuf); |
kkizirian | 0:ce60014510e5 | 676 | string ampm = ampmBuf; |
kkizirian | 0:ce60014510e5 | 677 | |
kkizirian | 0:ce60014510e5 | 678 | Course temp(building, hour, minute, ampm); |
kkizirian | 0:ce60014510e5 | 679 | cVec.push_back(temp); |
kkizirian | 0:ce60014510e5 | 680 | } |
kkizirian | 0:ce60014510e5 | 681 | } |
kkizirian | 0:ce60014510e5 | 682 | fclose(readFp); |
kkizirian | 0:ce60014510e5 | 683 | return; |
kkizirian | 2:c708e2027970 | 684 | } |
kkizirian | 2:c708e2027970 | 685 | |
kkizirian | 2:c708e2027970 | 686 | void displayCourseVec() |
kkizirian | 2:c708e2027970 | 687 | { |
kkizirian | 2:c708e2027970 | 688 | if (courseVec.size() == 0) { |
kkizirian | 2:c708e2027970 | 689 | uLCD.cls(); |
kkizirian | 2:c708e2027970 | 690 | uLCD.locate(0,0); |
kkizirian | 2:c708e2027970 | 691 | uLCD.printf("No classes input!"); |
kkizirian | 2:c708e2027970 | 692 | uLCD.locate(0,1); |
kkizirian | 2:c708e2027970 | 693 | uLCD.printf("To input classes \nGo to:"); |
kkizirian | 2:c708e2027970 | 694 | uLCD.locate(0,4); |
kkizirian | 2:c708e2027970 | 695 | uLCD.printf("%s", ip_line); |
kkizirian | 2:c708e2027970 | 696 | } else { |
kkizirian | 2:c708e2027970 | 697 | uLCD.cls(); |
kkizirian | 2:c708e2027970 | 698 | uLCD.locate(0,1); |
kkizirian | 2:c708e2027970 | 699 | for (int i = 0; i < courseVec.size(); i++) { |
kkizirian | 2:c708e2027970 | 700 | uLCD.locate(0, i); |
kkizirian | 2:c708e2027970 | 701 | uLCD.printf("%s", courseVec[i].getDisplayString()); |
kkizirian | 2:c708e2027970 | 702 | } |
kkizirian | 3:0ca91f8afec5 | 703 | uLCD.locate(0, 8); |
kkizirian | 3:0ca91f8afec5 | 704 | uLCD.printf("To add courses \ngo to"); |
kkizirian | 3:0ca91f8afec5 | 705 | uLCD.locate(0,10); |
kkizirian | 3:0ca91f8afec5 | 706 | uLCD.printf("%s", ip_line); |
kkizirian | 3:0ca91f8afec5 | 707 | uLCD.locate(0,12); |
kkizirian | 3:0ca91f8afec5 | 708 | uLCD.printf("To reset courses\npress center"); |
kkizirian | 2:c708e2027970 | 709 | } |
kkizirian | 3:0ca91f8afec5 | 710 | } |
kkizirian | 3:0ca91f8afec5 | 711 | |
kkizirian | 3:0ca91f8afec5 | 712 | float calculateHeading(float mx, float my) |
kkizirian | 3:0ca91f8afec5 | 713 | { |
kkizirian | 3:0ca91f8afec5 | 714 | float heading = 0.0; |
kkizirian | 3:0ca91f8afec5 | 715 | if (my == 0.0) |
kkizirian | 3:0ca91f8afec5 | 716 | heading = (mx < 0.0) ? 180.0 : 0.0; |
kkizirian | 3:0ca91f8afec5 | 717 | else |
kkizirian | 3:0ca91f8afec5 | 718 | heading = atan2(mx, my)*360.0/(2.0*PI); |
kkizirian | 3:0ca91f8afec5 | 719 | //pc.printf("heading atan=%f \n\r",heading); |
kkizirian | 3:0ca91f8afec5 | 720 | heading -= DECLINATION; //correct for geo location |
kkizirian | 3:0ca91f8afec5 | 721 | if(heading>180.0) heading = heading - 360.0; |
kkizirian | 3:0ca91f8afec5 | 722 | else if(heading<-180.0) heading = 360.0 + heading; |
kkizirian | 3:0ca91f8afec5 | 723 | else if(heading<0.0) heading = 360.0 + heading; |
kkizirian | 3:0ca91f8afec5 | 724 | |
kkizirian | 3:0ca91f8afec5 | 725 | // Convert everything from radians to degrees: |
kkizirian | 3:0ca91f8afec5 | 726 | //heading *= 180.0 / PI; |
kkizirian | 3:0ca91f8afec5 | 727 | |
kkizirian | 3:0ca91f8afec5 | 728 | //pc.printf("Magnetic Heading: %f degress\n\r",heading); |
kkizirian | 3:0ca91f8afec5 | 729 | return heading; |
kkizirian | 3:0ca91f8afec5 | 730 | } |
kkizirian | 3:0ca91f8afec5 | 731 | |
kkizirian | 3:0ca91f8afec5 | 732 | float computeAngleToDestination(float diffLat, float diffLong) |
kkizirian | 3:0ca91f8afec5 | 733 | { |
kkizirian | 3:0ca91f8afec5 | 734 | float angle = 0.0; |
kkizirian | 3:0ca91f8afec5 | 735 | |
kkizirian | 3:0ca91f8afec5 | 736 | if (diffLat > 0) { |
kkizirian | 3:0ca91f8afec5 | 737 | if (diffLong > 0) { |
kkizirian | 3:0ca91f8afec5 | 738 | // in quadrant 1 |
kkizirian | 3:0ca91f8afec5 | 739 | angle = 180*atan2(diffLat,diffLong)/PI; |
kkizirian | 3:0ca91f8afec5 | 740 | } else { |
kkizirian | 3:0ca91f8afec5 | 741 | // in quadrant 2 |
kkizirian | 3:0ca91f8afec5 | 742 | angle = 180*atan2(diffLat,-1 * diffLong)/PI; |
kkizirian | 3:0ca91f8afec5 | 743 | angle = 180 - angle; |
kkizirian | 3:0ca91f8afec5 | 744 | } |
kkizirian | 3:0ca91f8afec5 | 745 | } else { |
kkizirian | 3:0ca91f8afec5 | 746 | if (diffLong > 0) { |
kkizirian | 3:0ca91f8afec5 | 747 | // in quadrant 4 |
kkizirian | 3:0ca91f8afec5 | 748 | angle = 180*atan2(-1*diffLat, diffLong)/PI; |
kkizirian | 3:0ca91f8afec5 | 749 | angle = 360 - angle; |
kkizirian | 3:0ca91f8afec5 | 750 | } else { |
kkizirian | 3:0ca91f8afec5 | 751 | // in quadrant 3 |
kkizirian | 3:0ca91f8afec5 | 752 | angle = 180*atan2(-1*diffLat, -1*diffLong)/PI; |
kkizirian | 3:0ca91f8afec5 | 753 | angle = 180 + angle; |
kkizirian | 3:0ca91f8afec5 | 754 | } |
kkizirian | 3:0ca91f8afec5 | 755 | } |
kkizirian | 3:0ca91f8afec5 | 756 | |
kkizirian | 3:0ca91f8afec5 | 757 | //pc.printf("Angle to Destination: %f degress\n\r",angle); |
kkizirian | 3:0ca91f8afec5 | 758 | return angle; |
kkizirian | 0:ce60014510e5 | 759 | } |