Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: TextLCD nRF24L01P mbed
main.cpp@9:53da36425601, 2017-01-17 (annotated)
- Committer:
- jackmax
- Date:
- Tue Jan 17 21:42:42 2017 +0000
- Revision:
- 9:53da36425601
- Parent:
- 8:0de75e0480aa
- Child:
- 10:3417715786be
HTTP GET-y, synchronizacja i alarm - na razie wszystko teoretycznie.; BTW endpoint do alarm?w trzeba zmieni?, w?o?ony tylko prowizorycznie.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| jackmax | 6:19012a85ce35 | 1 | #include "mbed.h" |
| jackmax | 6:19012a85ce35 | 2 | #include "rtos.h" |
| jackmax | 6:19012a85ce35 | 3 | #include "TextLCD.h" |
| jackmax | 6:19012a85ce35 | 4 | #include "custom-chars.h" |
| jackmax | 9:53da36425601 | 5 | #include "melodies.h" |
| jackmax | 8:0de75e0480aa | 6 | #include "nRF24L01P.h" |
| jackmax | 9:53da36425601 | 7 | #include <string> |
| jackmax | 8:0de75e0480aa | 8 | |
| jackmax | 6:19012a85ce35 | 9 | // Host PC Communication channels |
| jackmax | 6:19012a85ce35 | 10 | Serial pc(USBTX, USBRX); // tx, rx |
| jackmax | 6:19012a85ce35 | 11 | |
| jackmax | 6:19012a85ce35 | 12 | I2C i2c_lcd(D5,D7); // SDA, SCL pins. Change if necessary |
| jackmax | 6:19012a85ce35 | 13 | TextLCD_I2C lcd(&i2c_lcd, 0x4E, TextLCD::LCD16x2); // I2C exp: I2C bus, PCF8574 Slaveaddress, LCD Type |
| jackmax | 6:19012a85ce35 | 14 | |
| jackmax | 6:19012a85ce35 | 15 | //Light sensor |
| jackmax | 6:19012a85ce35 | 16 | AnalogIn light_sensor(A1); |
| jackmax | 6:19012a85ce35 | 17 | |
| jackmax | 8:0de75e0480aa | 18 | //2.4 GHz radio |
| jackmax | 8:0de75e0480aa | 19 | #define RX_ADDRESS ((unsigned long long) 0xABCDEF00) |
| jackmax | 8:0de75e0480aa | 20 | #define TX_ADDRESS ((unsigned long long) 0xABCDEF01) |
| jackmax | 8:0de75e0480aa | 21 | #define TRANSFER_SIZE 32 |
| jackmax | 8:0de75e0480aa | 22 | |
| jackmax | 8:0de75e0480aa | 23 | nRF24L01P radio(PB_15, PB_14, PB_13, PB_12, PB_1, PB_2); |
| jackmax | 8:0de75e0480aa | 24 | |
| jackmax | 8:0de75e0480aa | 25 | void initRadio(int channel, int power, int datarate){ |
| jackmax | 8:0de75e0480aa | 26 | radio.powerDown(); |
| jackmax | 8:0de75e0480aa | 27 | radio.powerUp(); |
| jackmax | 8:0de75e0480aa | 28 | |
| jackmax | 8:0de75e0480aa | 29 | radio.setAirDataRate(datarate); |
| jackmax | 8:0de75e0480aa | 30 | radio.setRfOutputPower(power); |
| jackmax | 8:0de75e0480aa | 31 | radio.setRfFrequency(NRF24L01P_MIN_RF_FREQUENCY + 4 * channel); |
| jackmax | 8:0de75e0480aa | 32 | |
| jackmax | 8:0de75e0480aa | 33 | radio.setCrcWidth(NRF24L01P_CRC_8_BIT); |
| jackmax | 8:0de75e0480aa | 34 | radio.enableAutoAcknowledge(NRF24L01P_PIPE_P0); |
| jackmax | 8:0de75e0480aa | 35 | radio.enableAutoAcknowledge(NRF24L01P_PIPE_P1); |
| jackmax | 8:0de75e0480aa | 36 | radio.enableAutoRetransmit(0x0F, 0x0F); |
| jackmax | 8:0de75e0480aa | 37 | |
| jackmax | 8:0de75e0480aa | 38 | radio.setTxAddress(TX_ADDRESS, 4); |
| jackmax | 8:0de75e0480aa | 39 | radio.setRxAddress(TX_ADDRESS, 4, NRF24L01P_PIPE_P0); |
| jackmax | 8:0de75e0480aa | 40 | |
| jackmax | 8:0de75e0480aa | 41 | radio.setRxAddress(RX_ADDRESS, 4, NRF24L01P_PIPE_P1); |
| jackmax | 8:0de75e0480aa | 42 | |
| jackmax | 8:0de75e0480aa | 43 | // Display the (default) setup of the nRF24L01+ chip |
| jackmax | 8:0de75e0480aa | 44 | pc.printf( "nRF24L01+ Frequency : %d MHz\r\n", radio.getRfFrequency() ); |
| jackmax | 8:0de75e0480aa | 45 | pc.printf( "nRF24L01+ Output power : %d dBm\r\n", radio.getRfOutputPower() ); |
| jackmax | 8:0de75e0480aa | 46 | pc.printf( "nRF24L01+ Data Rate : %d kbps\r\n", radio.getAirDataRate() ); |
| jackmax | 8:0de75e0480aa | 47 | pc.printf( "nRF24L01+ TX Address : 0x%010llX\r\n", radio.getTxAddress() ); |
| jackmax | 8:0de75e0480aa | 48 | pc.printf( "nRF24L01+ RX0 Address : 0x%010llX\r\n", radio.getRxAddress(NRF24L01P_PIPE_P0) ); |
| jackmax | 8:0de75e0480aa | 49 | pc.printf( "nRF24L01+ RX1 Address : 0x%010llX\r\n", radio.getRxAddress(NRF24L01P_PIPE_P1) ); |
| jackmax | 8:0de75e0480aa | 50 | |
| jackmax | 8:0de75e0480aa | 51 | |
| jackmax | 8:0de75e0480aa | 52 | radio.setTransferSize(TRANSFER_SIZE, NRF24L01P_PIPE_P0); |
| jackmax | 8:0de75e0480aa | 53 | radio.setTransferSize(TRANSFER_SIZE, NRF24L01P_PIPE_P1); |
| jackmax | 8:0de75e0480aa | 54 | |
| jackmax | 8:0de75e0480aa | 55 | radio.setReceiveMode(); |
| jackmax | 8:0de75e0480aa | 56 | radio.enable(); |
| jackmax | 8:0de75e0480aa | 57 | }; |
| jackmax | 8:0de75e0480aa | 58 | |
| jackmax | 9:53da36425601 | 59 | //TODO: I don't think it needs to be in a separate thread, |
| jackmax | 9:53da36425601 | 60 | //we probably can just put it into the main loop. |
| jackmax | 9:53da36425601 | 61 | void radio_recv(void const* args){ |
| jackmax | 9:53da36425601 | 62 | char rxData[TRANSFER_SIZE+1]; |
| jackmax | 9:53da36425601 | 63 | while (1) { |
| jackmax | 9:53da36425601 | 64 | int rx_bytes=0; |
| jackmax | 9:53da36425601 | 65 | |
| jackmax | 9:53da36425601 | 66 | if(radio.readable(NRF24L01P_PIPE_P1)){ |
| jackmax | 9:53da36425601 | 67 | rx_bytes = radio.read(NRF24L01P_PIPE_P1, rxData, TRANSFER_SIZE); |
| jackmax | 9:53da36425601 | 68 | rxData[TRANSFER_SIZE] = '\0'; |
| jackmax | 9:53da36425601 | 69 | pc.printf("Received %d >>%s<<\r\n",rx_bytes, rxData); |
| jackmax | 9:53da36425601 | 70 | } |
| jackmax | 9:53da36425601 | 71 | |
| jackmax | 9:53da36425601 | 72 | } |
| jackmax | 9:53da36425601 | 73 | } |
| jackmax | 9:53da36425601 | 74 | |
| jackmax | 8:0de75e0480aa | 75 | //Display |
| jackmax | 6:19012a85ce35 | 76 | void backlightTimeout(void const *arg); |
| jackmax | 6:19012a85ce35 | 77 | RtosTimer backlightTimer(&backlightTimeout, osTimerPeriodic, (void*)0); |
| jackmax | 6:19012a85ce35 | 78 | |
| jackmax | 6:19012a85ce35 | 79 | class Display{ |
| jackmax | 6:19012a85ce35 | 80 | public: |
| jackmax | 6:19012a85ce35 | 81 | int wifi_on; |
| jackmax | 6:19012a85ce35 | 82 | int alarm_on; |
| jackmax | 6:19012a85ce35 | 83 | int sync_in_progress; |
| jackmax | 6:19012a85ce35 | 84 | int wireless_in_progress; |
| jackmax | 6:19012a85ce35 | 85 | int frame; |
| jackmax | 6:19012a85ce35 | 86 | |
| jackmax | 6:19012a85ce35 | 87 | time_t seconds; |
| jackmax | 6:19012a85ce35 | 88 | char time_str[9]; |
| jackmax | 6:19012a85ce35 | 89 | char date_str[9]; |
| jackmax | 6:19012a85ce35 | 90 | |
| jackmax | 6:19012a85ce35 | 91 | static const int backlightTime = 5000; |
| jackmax | 6:19012a85ce35 | 92 | int backlightState; |
| jackmax | 6:19012a85ce35 | 93 | Display() |
| jackmax | 6:19012a85ce35 | 94 | { |
| jackmax | 6:19012a85ce35 | 95 | wifi_on = 0; |
| jackmax | 6:19012a85ce35 | 96 | alarm_on = 0; |
| jackmax | 6:19012a85ce35 | 97 | sync_in_progress = 0; |
| jackmax | 6:19012a85ce35 | 98 | wireless_in_progress = 0; |
| jackmax | 6:19012a85ce35 | 99 | frame = 0; |
| jackmax | 6:19012a85ce35 | 100 | backlightState = 0; |
| jackmax | 6:19012a85ce35 | 101 | |
| jackmax | 6:19012a85ce35 | 102 | lcd.setCursor(TextLCD::CurOff_BlkOff); |
| jackmax | 6:19012a85ce35 | 103 | lcd.setUDC(C_ALRM, cc_dzwonek); |
| jackmax | 6:19012a85ce35 | 104 | lcd.setUDC(C_WIFI, cc_wifi); |
| jackmax | 6:19012a85ce35 | 105 | lcd.setUDC(C_WLC , cc_wireless); |
| jackmax | 6:19012a85ce35 | 106 | } |
| jackmax | 6:19012a85ce35 | 107 | |
| jackmax | 6:19012a85ce35 | 108 | void backlightOff(){ |
| jackmax | 6:19012a85ce35 | 109 | lcd.setBacklight(TextLCD::LightOff); |
| jackmax | 6:19012a85ce35 | 110 | backlightState = 0; |
| jackmax | 6:19012a85ce35 | 111 | } |
| jackmax | 6:19012a85ce35 | 112 | |
| jackmax | 6:19012a85ce35 | 113 | void backlightOn(){ |
| jackmax | 6:19012a85ce35 | 114 | lcd.setBacklight(TextLCD::LightOn); |
| jackmax | 6:19012a85ce35 | 115 | backlightState = 1; |
| jackmax | 6:19012a85ce35 | 116 | backlightTimer.start(backlightTime); |
| jackmax | 6:19012a85ce35 | 117 | } |
| jackmax | 6:19012a85ce35 | 118 | |
| jackmax | 6:19012a85ce35 | 119 | void update(){ |
| jackmax | 6:19012a85ce35 | 120 | //Top row of display |
| jackmax | 6:19012a85ce35 | 121 | char ico1 = ' '; |
| jackmax | 6:19012a85ce35 | 122 | char ico2 = wireless_in_progress ? (frame % 2 ? C_WLC : ' ' ) : ' '; |
| jackmax | 6:19012a85ce35 | 123 | char ico3 = C_ALRM; |
| jackmax | 6:19012a85ce35 | 124 | int ah=85, am=84; |
| jackmax | 6:19012a85ce35 | 125 | |
| jackmax | 6:19012a85ce35 | 126 | time_t seconds_now = time(NULL); |
| jackmax | 6:19012a85ce35 | 127 | if (seconds_now != seconds) { |
| jackmax | 6:19012a85ce35 | 128 | seconds = seconds_now; |
| jackmax | 6:19012a85ce35 | 129 | strftime(time_str, 9, "%X", localtime(&seconds)); |
| jackmax | 6:19012a85ce35 | 130 | strftime(date_str, 9, "%x", localtime(&seconds)); |
| jackmax | 6:19012a85ce35 | 131 | } |
| jackmax | 6:19012a85ce35 | 132 | |
| jackmax | 6:19012a85ce35 | 133 | lcd.locate(0,0); //Put in top row |
| jackmax | 6:19012a85ce35 | 134 | lcd.printf("%s%c%c%c%02d:%02d",time_str,ico1,ico2,ico3,ah,am); |
| jackmax | 6:19012a85ce35 | 135 | |
| jackmax | 6:19012a85ce35 | 136 | lcd.locate(0,1); //Put in bottom row |
| jackmax | 6:19012a85ce35 | 137 | if (sync_in_progress) { |
| jackmax | 6:19012a85ce35 | 138 | lcd.printf("Synchronizacja.."); |
| jackmax | 6:19012a85ce35 | 139 | } |
| jackmax | 6:19012a85ce35 | 140 | else { |
| jackmax | 6:19012a85ce35 | 141 | if (frame % 60 < 30) { |
| jackmax | 6:19012a85ce35 | 142 | lcd.printf("SmartAlarm+ Pro "); |
| jackmax | 6:19012a85ce35 | 143 | } |
| jackmax | 6:19012a85ce35 | 144 | else { |
| jackmax | 6:19012a85ce35 | 145 | lcd.printf(" %08s ", date_str); |
| jackmax | 6:19012a85ce35 | 146 | } |
| jackmax | 6:19012a85ce35 | 147 | } |
| jackmax | 6:19012a85ce35 | 148 | frame++; |
| jackmax | 6:19012a85ce35 | 149 | } |
| jackmax | 6:19012a85ce35 | 150 | }; |
| jackmax | 6:19012a85ce35 | 151 | |
| jackmax | 6:19012a85ce35 | 152 | Display disp; |
| jackmax | 6:19012a85ce35 | 153 | |
| jackmax | 6:19012a85ce35 | 154 | //Handling user button presses |
| jackmax | 6:19012a85ce35 | 155 | InterruptIn button(D6); |
| jackmax | 6:19012a85ce35 | 156 | int userButtonLongPress = 300; //Time in ms; threshold for long press |
| jackmax | 6:19012a85ce35 | 157 | Timer userButtonTimer; |
| jackmax | 6:19012a85ce35 | 158 | |
| jackmax | 6:19012a85ce35 | 159 | int userButtonPressed = 0, userButtonReleased = 0, backlightTimedOut = 0; |
| jackmax | 6:19012a85ce35 | 160 | |
| jackmax | 6:19012a85ce35 | 161 | void userButtonPress(){ |
| jackmax | 6:19012a85ce35 | 162 | userButtonPressed = 1; |
| jackmax | 6:19012a85ce35 | 163 | } |
| jackmax | 6:19012a85ce35 | 164 | |
| jackmax | 6:19012a85ce35 | 165 | void userButtonRelease(){ |
| jackmax | 6:19012a85ce35 | 166 | userButtonReleased = 1; |
| jackmax | 6:19012a85ce35 | 167 | } |
| jackmax | 6:19012a85ce35 | 168 | |
| jackmax | 6:19012a85ce35 | 169 | void backlightTimeout(void const *arg){ |
| jackmax | 6:19012a85ce35 | 170 | backlightTimedOut = 1; |
| jackmax | 6:19012a85ce35 | 171 | } |
| jackmax | 6:19012a85ce35 | 172 | |
| jackmax | 9:53da36425601 | 173 | //Wi-Fi communication |
| jackmax | 9:53da36425601 | 174 | #define WIFI_SIZE 1000 |
| jackmax | 9:53da36425601 | 175 | Serial wifi(PA_9, PA_10); |
| jackmax | 9:53da36425601 | 176 | char wifiData[WIFI_SIZE]; |
| jackmax | 9:53da36425601 | 177 | bool wifiDataReady = false; |
| jackmax | 9:53da36425601 | 178 | bool readInProgress = false; |
| jackmax | 9:53da36425601 | 179 | int currentReadIndex = 0; |
| jackmax | 9:53da36425601 | 180 | |
| jackmax | 9:53da36425601 | 181 | void wifiCallback() { |
| jackmax | 9:53da36425601 | 182 | char c = wifi.getc(); |
| jackmax | 9:53da36425601 | 183 | if (c == '*') { |
| jackmax | 9:53da36425601 | 184 | readInProgress = !readInProgress; |
| jackmax | 9:53da36425601 | 185 | wifiData[currentReadIndex] = '\0'; |
| jackmax | 9:53da36425601 | 186 | currentReadIndex = 0; |
| jackmax | 9:53da36425601 | 187 | wifiDataReady = true; |
| jackmax | 9:53da36425601 | 188 | } else if (readInProgress) { |
| jackmax | 9:53da36425601 | 189 | wifiData[currentReadIndex++] = c; |
| jackmax | 9:53da36425601 | 190 | } |
| jackmax | 9:53da36425601 | 191 | } |
| jackmax | 9:53da36425601 | 192 | |
| jackmax | 9:53da36425601 | 193 | std::string wifiGetData(){ |
| jackmax | 9:53da36425601 | 194 | if (!wifiDataReady) |
| jackmax | 9:53da36425601 | 195 | return ""; |
| jackmax | 9:53da36425601 | 196 | |
| jackmax | 9:53da36425601 | 197 | std::string result = wifiData; |
| jackmax | 9:53da36425601 | 198 | wifiDataReady = false; |
| jackmax | 9:53da36425601 | 199 | return result; |
| jackmax | 9:53da36425601 | 200 | } |
| jackmax | 9:53da36425601 | 201 | |
| jackmax | 9:53da36425601 | 202 | std::string httpGETWithRetry(const char* query, float timeout, int retries){ |
| jackmax | 9:53da36425601 | 203 | Timer t; |
| jackmax | 9:53da36425601 | 204 | for (int retry = 0; retry<retries; retry++){ |
| jackmax | 9:53da36425601 | 205 | wifi.printf(query); |
| jackmax | 9:53da36425601 | 206 | t.reset(); |
| jackmax | 9:53da36425601 | 207 | t.start(); |
| jackmax | 9:53da36425601 | 208 | while (!wifiDataReady && t <= timeout){ |
| jackmax | 9:53da36425601 | 209 | wait(0.01f); |
| jackmax | 9:53da36425601 | 210 | } |
| jackmax | 9:53da36425601 | 211 | pc.printf("http GET trying #%d\r\n", retry); |
| jackmax | 9:53da36425601 | 212 | if (wifiDataReady) |
| jackmax | 9:53da36425601 | 213 | break; |
| jackmax | 9:53da36425601 | 214 | } |
| jackmax | 9:53da36425601 | 215 | return wifiGetData(); |
| jackmax | 9:53da36425601 | 216 | } |
| jackmax | 9:53da36425601 | 217 | |
| jackmax | 9:53da36425601 | 218 | //Synchronization |
| jackmax | 9:53da36425601 | 219 | time_t next_sync = 0; |
| jackmax | 9:53da36425601 | 220 | bool sync_in_progress = false; |
| jackmax | 9:53da36425601 | 221 | time_t next_alarm = (time_t)-1; //TODO: bardzo brzydki hack |
| jackmax | 9:53da36425601 | 222 | |
| jackmax | 9:53da36425601 | 223 | time_t getNextSync(){ |
| jackmax | 9:53da36425601 | 224 | //TODO: zmiennej czasu nie powinno sie chyba tak modyfikowac |
| jackmax | 9:53da36425601 | 225 | return time (NULL) + 123; |
| jackmax | 9:53da36425601 | 226 | } |
| jackmax | 8:0de75e0480aa | 227 | |
| jackmax | 9:53da36425601 | 228 | //Reads are blocking now, so we need to put it into a different thread |
| jackmax | 9:53da36425601 | 229 | void syncThreadFunction(void const* args){ |
| jackmax | 9:53da36425601 | 230 | std::string time_string = httpGETWithRetry("GET " |
| jackmax | 9:53da36425601 | 231 | "/time/current/ " |
| jackmax | 9:53da36425601 | 232 | "HTTP/1.1\r\n" |
| jackmax | 9:53da36425601 | 233 | "Host: 10.1.8.202:8080\r\n\r\n", 5.0, 5); |
| jackmax | 9:53da36425601 | 234 | if (time_string == ""){ |
| jackmax | 9:53da36425601 | 235 | lcd.locate(0,1); |
| jackmax | 9:53da36425601 | 236 | lcd.printf("Blad synchroniz."); //TODO: to nie bedzie dzialac, trzeba przekazac te dane do obiektu disp |
| jackmax | 9:53da36425601 | 237 | } |
| jackmax | 9:53da36425601 | 238 | else { |
| jackmax | 9:53da36425601 | 239 | set_time(atoi(time_string.c_str())); |
| jackmax | 9:53da36425601 | 240 | } |
| jackmax | 9:53da36425601 | 241 | |
| jackmax | 9:53da36425601 | 242 | time_string = httpGETWithRetry("GET " |
| jackmax | 9:53da36425601 | 243 | "/alarm/latest/ " |
| jackmax | 9:53da36425601 | 244 | "HTTP/1.1\r\n" |
| jackmax | 9:53da36425601 | 245 | "Host: 10.1.8.202:8080\r\n\r\n", 5.0, 5); //TODO: Jaki endpoint do alarmow? |
| jackmax | 9:53da36425601 | 246 | if (time_string == ""){ |
| jackmax | 9:53da36425601 | 247 | lcd.locate(0,1); |
| jackmax | 9:53da36425601 | 248 | lcd.printf("Blad synchroniz."); |
| jackmax | 9:53da36425601 | 249 | } |
| jackmax | 9:53da36425601 | 250 | else { |
| jackmax | 9:53da36425601 | 251 | next_alarm = atoi(time_string.c_str()); |
| jackmax | 9:53da36425601 | 252 | } |
| jackmax | 9:53da36425601 | 253 | |
| jackmax | 9:53da36425601 | 254 | next_sync = getNextSync(); |
| jackmax | 9:53da36425601 | 255 | sync_in_progress = false; |
| jackmax | 9:53da36425601 | 256 | } |
| jackmax | 9:53da36425601 | 257 | |
| jackmax | 9:53da36425601 | 258 | //Alarm |
| jackmax | 9:53da36425601 | 259 | PwmOut sound(D2); |
| jackmax | 9:53da36425601 | 260 | bool alarm_playing = false; |
| jackmax | 9:53da36425601 | 261 | bool alarm_stopped = false; |
| jackmax | 9:53da36425601 | 262 | |
| jackmax | 9:53da36425601 | 263 | //We probably could do sound playback with timers and whatnot |
| jackmax | 9:53da36425601 | 264 | //but why when we have THREADS! |
| jackmax | 9:53da36425601 | 265 | void alarmThreadFunction(void const* args){ |
| jackmax | 9:53da36425601 | 266 | //TODO: opcja wyboru dzwonka? whatever, i tak tego nie dokonczymy |
| jackmax | 9:53da36425601 | 267 | const int* freq = axelf_freq; |
| jackmax | 9:53da36425601 | 268 | const int* per = axelf_per; |
| jackmax | 9:53da36425601 | 269 | float bpm = 100.0f; //tempo |
| jackmax | 9:53da36425601 | 270 | alarm_stopped = false; |
| jackmax | 9:53da36425601 | 271 | while (!alarm_stopped){ |
| jackmax | 9:53da36425601 | 272 | float base_period = 0.5f; |
| jackmax | 9:53da36425601 | 273 | float base_time = bpm / 60.0f; |
| jackmax | 9:53da36425601 | 274 | for (int i = 0; i < 100; i++){ //TODO: no bounds checking, -1 sentinel at the end required |
| jackmax | 9:53da36425601 | 275 | if (per[i] == -1) |
| jackmax | 9:53da36425601 | 276 | break; |
| jackmax | 9:53da36425601 | 277 | if (freq[i] != 0) { |
| jackmax | 9:53da36425601 | 278 | sound.period(base_period/freq[i]); |
| jackmax | 9:53da36425601 | 279 | sound = 0.5f; |
| jackmax | 9:53da36425601 | 280 | } |
| jackmax | 9:53da36425601 | 281 | else { |
| jackmax | 9:53da36425601 | 282 | sound = 0.0f; |
| jackmax | 9:53da36425601 | 283 | } |
| jackmax | 9:53da36425601 | 284 | wait(base_time/per[i]); |
| jackmax | 8:0de75e0480aa | 285 | } |
| jackmax | 9:53da36425601 | 286 | sound = 0.0f; |
| jackmax | 8:0de75e0480aa | 287 | } |
| jackmax | 9:53da36425601 | 288 | alarm_playing = false; |
| jackmax | 8:0de75e0480aa | 289 | } |
| jackmax | 8:0de75e0480aa | 290 | |
| jackmax | 6:19012a85ce35 | 291 | Ticker display_update_ticker; |
| jackmax | 6:19012a85ce35 | 292 | int main() { |
| jackmax | 6:19012a85ce35 | 293 | set_time(1256729737); //DEBUG: Set RTC time to Wed, 28 Oct 2009 11:35:37 |
| jackmax | 6:19012a85ce35 | 294 | //Initialization |
| jackmax | 6:19012a85ce35 | 295 | disp.backlightOn(); |
| jackmax | 6:19012a85ce35 | 296 | button.rise(&userButtonPress); |
| jackmax | 6:19012a85ce35 | 297 | button.fall(&userButtonRelease); |
| jackmax | 6:19012a85ce35 | 298 | |
| jackmax | 9:53da36425601 | 299 | wifi.baud(115200); |
| jackmax | 9:53da36425601 | 300 | wifi.attach(&wifiCallback); |
| jackmax | 9:53da36425601 | 301 | |
| jackmax | 9:53da36425601 | 302 | pc.printf("Waiting for time\r\n"); |
| jackmax | 9:53da36425601 | 303 | |
| jackmax | 9:53da36425601 | 304 | std::string time_string = httpGETWithRetry("GET " |
| jackmax | 9:53da36425601 | 305 | "/time/current/ " |
| jackmax | 9:53da36425601 | 306 | "HTTP/1.1\r\n" |
| jackmax | 9:53da36425601 | 307 | "Host: 10.1.8.202:8080\r\n\r\n", 5.0, 5); |
| jackmax | 9:53da36425601 | 308 | |
| jackmax | 9:53da36425601 | 309 | pc.printf("Get time returned >>%s<<", time_string.c_str()); |
| jackmax | 9:53da36425601 | 310 | |
| jackmax | 9:53da36425601 | 311 | if (time_string == ""){ |
| jackmax | 9:53da36425601 | 312 | lcd.locate(0,1); //Put in top row |
| jackmax | 9:53da36425601 | 313 | lcd.printf("Blad synchroniz."); |
| jackmax | 9:53da36425601 | 314 | wait(1); |
| jackmax | 9:53da36425601 | 315 | } |
| jackmax | 9:53da36425601 | 316 | else { |
| jackmax | 9:53da36425601 | 317 | set_time(atoi(time_string.c_str())); |
| jackmax | 9:53da36425601 | 318 | } |
| jackmax | 8:0de75e0480aa | 319 | |
| jackmax | 8:0de75e0480aa | 320 | initRadio(6, NRF24L01P_TX_PWR_ZERO_DB, NRF24L01P_DATARATE_250_KBPS); |
| jackmax | 8:0de75e0480aa | 321 | Thread radio_thread(radio_recv); |
| jackmax | 8:0de75e0480aa | 322 | |
| jackmax | 6:19012a85ce35 | 323 | while (1){ |
| jackmax | 6:19012a85ce35 | 324 | pc.printf("ok\r\n"); |
| jackmax | 6:19012a85ce35 | 325 | |
| jackmax | 8:0de75e0480aa | 326 | //Handling button presses |
| jackmax | 6:19012a85ce35 | 327 | if (userButtonPressed) { |
| jackmax | 6:19012a85ce35 | 328 | userButtonPressed = 0; |
| jackmax | 6:19012a85ce35 | 329 | userButtonTimer.reset(); |
| jackmax | 6:19012a85ce35 | 330 | userButtonTimer.start(); |
| jackmax | 6:19012a85ce35 | 331 | } |
| jackmax | 6:19012a85ce35 | 332 | |
| jackmax | 6:19012a85ce35 | 333 | if (userButtonReleased) { |
| jackmax | 6:19012a85ce35 | 334 | userButtonReleased = 0; |
| jackmax | 6:19012a85ce35 | 335 | userButtonTimer.stop(); |
| jackmax | 6:19012a85ce35 | 336 | if (userButtonTimer.read_ms() > userButtonLongPress){ |
| jackmax | 6:19012a85ce35 | 337 | pc.printf("User button long pressed"); |
| jackmax | 9:53da36425601 | 338 | //disp.backlightOff(); //that was just debug code anyway |
| jackmax | 9:53da36425601 | 339 | if (alarm_playing){ |
| jackmax | 9:53da36425601 | 340 | alarm_stopped = true; |
| jackmax | 9:53da36425601 | 341 | } |
| jackmax | 6:19012a85ce35 | 342 | } |
| jackmax | 6:19012a85ce35 | 343 | else { |
| jackmax | 6:19012a85ce35 | 344 | pc.printf("User button short pressed"); |
| jackmax | 6:19012a85ce35 | 345 | disp.backlightOn(); |
| jackmax | 6:19012a85ce35 | 346 | } |
| jackmax | 6:19012a85ce35 | 347 | } |
| jackmax | 6:19012a85ce35 | 348 | |
| jackmax | 8:0de75e0480aa | 349 | //Handling backlight |
| jackmax | 6:19012a85ce35 | 350 | if (backlightTimedOut){ |
| jackmax | 6:19012a85ce35 | 351 | backlightTimedOut = 0; |
| jackmax | 6:19012a85ce35 | 352 | disp.backlightOff(); |
| jackmax | 6:19012a85ce35 | 353 | } |
| jackmax | 6:19012a85ce35 | 354 | |
| jackmax | 9:53da36425601 | 355 | //Synchronize with server |
| jackmax | 9:53da36425601 | 356 | if (time(NULL) >= next_sync && !sync_in_progress){ |
| jackmax | 9:53da36425601 | 357 | sync_in_progress = true; |
| jackmax | 9:53da36425601 | 358 | Thread sync_thread(syncThreadFunction); |
| jackmax | 9:53da36425601 | 359 | } |
| jackmax | 9:53da36425601 | 360 | |
| jackmax | 9:53da36425601 | 361 | //Check if alarm should be played |
| jackmax | 9:53da36425601 | 362 | if (time(NULL) >= next_alarm && !alarm_playing){ |
| jackmax | 9:53da36425601 | 363 | alarm_playing = true; |
| jackmax | 9:53da36425601 | 364 | Thread alarm_thread(alarmThreadFunction); |
| jackmax | 9:53da36425601 | 365 | } |
| jackmax | 9:53da36425601 | 366 | |
| jackmax | 8:0de75e0480aa | 367 | //Updating display |
| jackmax | 6:19012a85ce35 | 368 | disp.update(); |
| jackmax | 8:0de75e0480aa | 369 | |
| jackmax | 6:19012a85ce35 | 370 | Thread::wait(100); |
| jackmax | 6:19012a85ce35 | 371 | }; |
| jackmax | 6:19012a85ce35 | 372 | } |
| jackmax | 6:19012a85ce35 | 373 | |
| jackmax | 6:19012a85ce35 | 374 | |
| jackmax | 6:19012a85ce35 | 375 |

