Initial commit
Dependencies: MODSERIAL Terminal TextLCD mbed
main.cpp@1:a6b0e0ce3e7a, 2014-12-05 (annotated)
- Committer:
- anmar
- Date:
- Fri Dec 05 10:27:17 2014 +0000
- Revision:
- 1:a6b0e0ce3e7a
- Parent:
- 0:0903545c0460
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
anmar | 0:0903545c0460 | 1 | /* |
anmar | 0:0903545c0460 | 2 | * Anders Markland, Knightec AB |
anmar | 0:0903545c0460 | 3 | * 2013-11-15 |
anmar | 0:0903545c0460 | 4 | * |
anmar | 0:0903545c0460 | 5 | * |
anmar | 0:0903545c0460 | 6 | */ |
anmar | 0:0903545c0460 | 7 | |
anmar | 0:0903545c0460 | 8 | #include "mbed.h" |
anmar | 0:0903545c0460 | 9 | |
anmar | 0:0903545c0460 | 10 | #include "Terminal.h" |
anmar | 0:0903545c0460 | 11 | #include "MODSERIAL.h" |
anmar | 0:0903545c0460 | 12 | #include "TextLCD.h" |
anmar | 0:0903545c0460 | 13 | #include "defines.h" |
anmar | 0:0903545c0460 | 14 | #include "mytimer.h" |
anmar | 0:0903545c0460 | 15 | //#include "presence.h" |
anmar | 0:0903545c0460 | 16 | //#include "thermal.h" |
anmar | 0:0903545c0460 | 17 | |
anmar | 0:0903545c0460 | 18 | |
anmar | 0:0903545c0460 | 19 | |
anmar | 0:0903545c0460 | 20 | #define C_ae (0xE4) |
anmar | 0:0903545c0460 | 21 | #define C_aa (0xE5) |
anmar | 0:0903545c0460 | 22 | #define C_oe (0xF6) |
anmar | 0:0903545c0460 | 23 | |
anmar | 0:0903545c0460 | 24 | #define C_AE (0xC4) |
anmar | 0:0903545c0460 | 25 | #define C_AA (0xC5) |
anmar | 0:0903545c0460 | 26 | #define C_OE (0xD6) |
anmar | 0:0903545c0460 | 27 | |
anmar | 0:0903545c0460 | 28 | |
anmar | 0:0903545c0460 | 29 | #define L_ae (0x84) |
anmar | 0:0903545c0460 | 30 | #define L_aa (0x94) |
anmar | 0:0903545c0460 | 31 | #define L_oe (0xF6) |
anmar | 0:0903545c0460 | 32 | |
anmar | 0:0903545c0460 | 33 | #define L_AE (0x8E) |
anmar | 0:0903545c0460 | 34 | #define L_AA (0x8F) |
anmar | 0:0903545c0460 | 35 | #define l_OE (0x99) |
anmar | 0:0903545c0460 | 36 | |
anmar | 0:0903545c0460 | 37 | |
anmar | 0:0903545c0460 | 38 | |
anmar | 0:0903545c0460 | 39 | Terminal *term; |
anmar | 0:0903545c0460 | 40 | DigitalInOut reset(p9); |
anmar | 0:0903545c0460 | 41 | DigitalOut led1(LED1); |
anmar | 0:0903545c0460 | 42 | DigitalOut led2(LED2); |
anmar | 0:0903545c0460 | 43 | DigitalOut led3(LED3); |
anmar | 0:0903545c0460 | 44 | DigitalOut led4(LED4); |
anmar | 0:0903545c0460 | 45 | |
anmar | 0:0903545c0460 | 46 | //Presence *presence; |
anmar | 0:0903545c0460 | 47 | //I2C i2c(p28, p27); |
anmar | 0:0903545c0460 | 48 | //I2C *i2c; |
anmar | 0:0903545c0460 | 49 | |
anmar | 0:0903545c0460 | 50 | |
anmar | 0:0903545c0460 | 51 | SPI spi_lcd(p5, NC, p7); // MOSI, MISO, SCLK |
anmar | 0:0903545c0460 | 52 | TextLCD lcd(&spi_lcd, p15, p8, TextLCD::LCD16x2); // SPI bus, CS pin, LCD Type ok |
anmar | 0:0903545c0460 | 53 | |
anmar | 0:0903545c0460 | 54 | int presence = 0; |
anmar | 0:0903545c0460 | 55 | |
anmar | 0:0903545c0460 | 56 | int seconds_since_movement = 0; |
anmar | 0:0903545c0460 | 57 | int last_inactive_period = 0; |
anmar | 0:0903545c0460 | 58 | int seconds_since_last_update = 0; |
anmar | 0:0903545c0460 | 59 | |
anmar | 0:0903545c0460 | 60 | MODSERIAL radio(p9, p10); // tx, rx |
anmar | 0:0903545c0460 | 61 | //Serial uart(p13, p14); |
anmar | 0:0903545c0460 | 62 | |
anmar | 0:0903545c0460 | 63 | int receiver = 0; |
anmar | 0:0903545c0460 | 64 | |
anmar | 0:0903545c0460 | 65 | double dt[16]; |
anmar | 0:0903545c0460 | 66 | double d_PTAT; |
anmar | 0:0903545c0460 | 67 | |
anmar | 0:0903545c0460 | 68 | |
anmar | 0:0903545c0460 | 69 | void pad(char *ptr, int l); |
anmar | 0:0903545c0460 | 70 | |
anmar | 0:0903545c0460 | 71 | int D6T_getvalue(); |
anmar | 0:0903545c0460 | 72 | int D6T_checkPEC( uint8_t buf[] , int pPEC ); |
anmar | 0:0903545c0460 | 73 | void measure(); |
anmar | 0:0903545c0460 | 74 | uint8_t calc_crc( uint8_t data ); |
anmar | 0:0903545c0460 | 75 | char readbuff[35]; |
anmar | 0:0903545c0460 | 76 | int tPTAT; |
anmar | 0:0903545c0460 | 77 | int tP[16]; |
anmar | 0:0903545c0460 | 78 | int tPEC; |
anmar | 0:0903545c0460 | 79 | int time_since_ping; |
anmar | 0:0903545c0460 | 80 | |
anmar | 0:0903545c0460 | 81 | uint8_t showoutput = 0; |
anmar | 0:0903545c0460 | 82 | |
anmar | 0:0903545c0460 | 83 | |
anmar | 0:0903545c0460 | 84 | |
anmar | 0:0903545c0460 | 85 | // bool newline_detected = false; |
anmar | 0:0903545c0460 | 86 | |
anmar | 0:0903545c0460 | 87 | // Called everytime a new character goes into |
anmar | 0:0903545c0460 | 88 | // the RX buffer. Test that character for \n |
anmar | 0:0903545c0460 | 89 | // Note, rxGetLastChar() gets the last char that |
anmar | 0:0903545c0460 | 90 | // we received but it does NOT remove it from |
anmar | 0:0903545c0460 | 91 | // the RX buffer. |
anmar | 0:0903545c0460 | 92 | /*void rxCallback(MODSERIAL_IRQ_INFO *q) { |
anmar | 0:0903545c0460 | 93 | MODSERIAL *serial = q->serial; |
anmar | 0:0903545c0460 | 94 | if ( serial->rxGetLastChar() == '\n') { |
anmar | 0:0903545c0460 | 95 | newline_detected = true; |
anmar | 0:0903545c0460 | 96 | } |
anmar | 0:0903545c0460 | 97 | }*/ |
anmar | 0:0903545c0460 | 98 | |
anmar | 0:0903545c0460 | 99 | |
anmar | 0:0903545c0460 | 100 | |
anmar | 0:0903545c0460 | 101 | Ticker flipper; |
anmar | 0:0903545c0460 | 102 | Ticker msflip; |
anmar | 0:0903545c0460 | 103 | Ticker sflip; |
anmar | 0:0903545c0460 | 104 | |
anmar | 0:0903545c0460 | 105 | uint16_t msticks; |
anmar | 0:0903545c0460 | 106 | uint8_t print_tick; |
anmar | 0:0903545c0460 | 107 | |
anmar | 0:0903545c0460 | 108 | void flip() |
anmar | 0:0903545c0460 | 109 | { |
anmar | 0:0903545c0460 | 110 | time_since_ping++; |
anmar | 0:0903545c0460 | 111 | |
anmar | 0:0903545c0460 | 112 | if ( receiver ) |
anmar | 0:0903545c0460 | 113 | if ( time_since_ping > 10 ) |
anmar | 0:0903545c0460 | 114 | { |
anmar | 0:0903545c0460 | 115 | led2 = 1; |
anmar | 0:0903545c0460 | 116 | led3 = 1; |
anmar | 0:0903545c0460 | 117 | led4 = 1; |
anmar | 0:0903545c0460 | 118 | } |
anmar | 0:0903545c0460 | 119 | else |
anmar | 0:0903545c0460 | 120 | { |
anmar | 0:0903545c0460 | 121 | led2 = 0; |
anmar | 0:0903545c0460 | 122 | led3 = 0; |
anmar | 0:0903545c0460 | 123 | led4 = 0; |
anmar | 0:0903545c0460 | 124 | } |
anmar | 0:0903545c0460 | 125 | } |
anmar | 0:0903545c0460 | 126 | |
anmar | 0:0903545c0460 | 127 | void ms() |
anmar | 0:0903545c0460 | 128 | { |
anmar | 0:0903545c0460 | 129 | msticks++; |
anmar | 0:0903545c0460 | 130 | } |
anmar | 0:0903545c0460 | 131 | |
anmar | 0:0903545c0460 | 132 | |
anmar | 0:0903545c0460 | 133 | void s() |
anmar | 0:0903545c0460 | 134 | { |
anmar | 0:0903545c0460 | 135 | seconds_since_last_update++; |
anmar | 0:0903545c0460 | 136 | print_tick++; |
anmar | 0:0903545c0460 | 137 | } |
anmar | 0:0903545c0460 | 138 | |
anmar | 0:0903545c0460 | 139 | |
anmar | 0:0903545c0460 | 140 | enum StateType { Init, PresenceDetected, LastPresenceDetected, CommunicationFault } displayState; |
anmar | 0:0903545c0460 | 141 | |
anmar | 0:0903545c0460 | 142 | |
anmar | 0:0903545c0460 | 143 | |
anmar | 0:0903545c0460 | 144 | |
anmar | 0:0903545c0460 | 145 | /* |
anmar | 0:0903545c0460 | 146 | |
anmar | 0:0903545c0460 | 147 | ------------------ |
anmar | 0:0903545c0460 | 148 | " Scaniasatellit " |
anmar | 0:0903545c0460 | 149 | " bemannad " |
anmar | 0:0903545c0460 | 150 | ------------------ |
anmar | 0:0903545c0460 | 151 | |
anmar | 0:0903545c0460 | 152 | ------------------ |
anmar | 0:0903545c0460 | 153 | " Inaktivitet " |
anmar | 0:0903545c0460 | 154 | " 00:03:22 " |
anmar | 0:0903545c0460 | 155 | ------------------ |
anmar | 0:0903545c0460 | 156 | |
anmar | 0:0903545c0460 | 157 | ------------------ |
anmar | 0:0903545c0460 | 158 | " Fel: " |
anmar | 0:0903545c0460 | 159 | " Kommunikation " |
anmar | 0:0903545c0460 | 160 | ------------------ |
anmar | 0:0903545c0460 | 161 | */ |
anmar | 0:0903545c0460 | 162 | |
anmar | 0:0903545c0460 | 163 | |
anmar | 0:0903545c0460 | 164 | |
anmar | 0:0903545c0460 | 165 | void showPresence(int s) |
anmar | 0:0903545c0460 | 166 | { |
anmar | 0:0903545c0460 | 167 | time_t seconds = s; |
anmar | 0:0903545c0460 | 168 | char str[32]; |
anmar | 0:0903545c0460 | 169 | char buf[30]; |
anmar | 0:0903545c0460 | 170 | |
anmar | 0:0903545c0460 | 171 | |
anmar | 0:0903545c0460 | 172 | struct tm *timeinfo; |
anmar | 0:0903545c0460 | 173 | timeinfo = localtime(&seconds); |
anmar | 0:0903545c0460 | 174 | |
anmar | 0:0903545c0460 | 175 | |
anmar | 0:0903545c0460 | 176 | |
anmar | 0:0903545c0460 | 177 | strftime(str, 32, "%T", localtime(&seconds)); |
anmar | 0:0903545c0460 | 178 | |
anmar | 0:0903545c0460 | 179 | |
anmar | 0:0903545c0460 | 180 | //lcd.cls(); |
anmar | 0:0903545c0460 | 181 | lcd.locate(0,0); |
anmar | 0:0903545c0460 | 182 | |
anmar | 0:0903545c0460 | 183 | char pr[10]; |
anmar | 0:0903545c0460 | 184 | |
anmar | 0:0903545c0460 | 185 | if ( presence ) |
anmar | 0:0903545c0460 | 186 | { |
anmar | 0:0903545c0460 | 187 | strcpy(pr, "Ja"); |
anmar | 0:0903545c0460 | 188 | sprintf(buf, " N%crvaro: %s", L_ae, pr); |
anmar | 0:0903545c0460 | 189 | pad(buf, 16); |
anmar | 0:0903545c0460 | 190 | lcd.printf("%s", buf); |
anmar | 0:0903545c0460 | 191 | |
anmar | 0:0903545c0460 | 192 | if ( s > 30 ) |
anmar | 0:0903545c0460 | 193 | { |
anmar | 0:0903545c0460 | 194 | sprintf(buf, " %dd %s", timeinfo->tm_yday, str); |
anmar | 0:0903545c0460 | 195 | } |
anmar | 0:0903545c0460 | 196 | else |
anmar | 0:0903545c0460 | 197 | { |
anmar | 0:0903545c0460 | 198 | strcpy(buf, " "); |
anmar | 0:0903545c0460 | 199 | } |
anmar | 0:0903545c0460 | 200 | pad(buf, 16); |
anmar | 0:0903545c0460 | 201 | lcd.printf("%s", buf); |
anmar | 0:0903545c0460 | 202 | |
anmar | 0:0903545c0460 | 203 | } |
anmar | 0:0903545c0460 | 204 | else |
anmar | 0:0903545c0460 | 205 | { |
anmar | 0:0903545c0460 | 206 | if ( s > 180 * 60 ) // No movement for 3 hours --> probably false trigger |
anmar | 0:0903545c0460 | 207 | strcpy(pr, "Nej"); |
anmar | 0:0903545c0460 | 208 | else |
anmar | 0:0903545c0460 | 209 | strcpy(pr, "?"); |
anmar | 0:0903545c0460 | 210 | sprintf(buf, " N%crvaro: %s", L_ae, pr); |
anmar | 0:0903545c0460 | 211 | pad(buf, 16); |
anmar | 0:0903545c0460 | 212 | lcd.printf("%s", buf); |
anmar | 0:0903545c0460 | 213 | |
anmar | 0:0903545c0460 | 214 | |
anmar | 0:0903545c0460 | 215 | sprintf(buf, " %dd %s", timeinfo->tm_yday, str); |
anmar | 0:0903545c0460 | 216 | pad(buf, 16); |
anmar | 0:0903545c0460 | 217 | lcd.printf("%s", buf); |
anmar | 0:0903545c0460 | 218 | } |
anmar | 0:0903545c0460 | 219 | } |
anmar | 0:0903545c0460 | 220 | |
anmar | 0:0903545c0460 | 221 | void showAbsence(int s) |
anmar | 0:0903545c0460 | 222 | { |
anmar | 0:0903545c0460 | 223 | time_t seconds = s; |
anmar | 0:0903545c0460 | 224 | char str[32]; |
anmar | 0:0903545c0460 | 225 | |
anmar | 0:0903545c0460 | 226 | strftime(str, 32, "%T", localtime(&seconds)); |
anmar | 0:0903545c0460 | 227 | |
anmar | 0:0903545c0460 | 228 | lcd.cls(); |
anmar | 0:0903545c0460 | 229 | //int inactiveTime; |
anmar | 0:0903545c0460 | 230 | lcd.printf(" Inaktivitet "); |
anmar | 0:0903545c0460 | 231 | lcd.printf(" [%c] %s", presence?'X':' ', str); |
anmar | 0:0903545c0460 | 232 | } |
anmar | 0:0903545c0460 | 233 | |
anmar | 0:0903545c0460 | 234 | void showError() |
anmar | 0:0903545c0460 | 235 | { |
anmar | 0:0903545c0460 | 236 | lcd.cls(); |
anmar | 0:0903545c0460 | 237 | lcd.printf(" Fel: "); |
anmar | 0:0903545c0460 | 238 | lcd.printf(" Kommunikation "); |
anmar | 0:0903545c0460 | 239 | } |
anmar | 0:0903545c0460 | 240 | |
anmar | 0:0903545c0460 | 241 | |
anmar | 0:0903545c0460 | 242 | void pad(char *ptr, int l) |
anmar | 0:0903545c0460 | 243 | { |
anmar | 0:0903545c0460 | 244 | int len = strlen (ptr); |
anmar | 0:0903545c0460 | 245 | while ( len < l ) |
anmar | 0:0903545c0460 | 246 | { |
anmar | 0:0903545c0460 | 247 | ptr[len++] = ' '; |
anmar | 0:0903545c0460 | 248 | ptr[len] = '\0'; |
anmar | 0:0903545c0460 | 249 | } |
anmar | 0:0903545c0460 | 250 | } |
anmar | 0:0903545c0460 | 251 | |
anmar | 0:0903545c0460 | 252 | |
anmar | 0:0903545c0460 | 253 | |
anmar | 0:0903545c0460 | 254 | int main() |
anmar | 0:0903545c0460 | 255 | { |
anmar | 0:0903545c0460 | 256 | int last_elapsed_time = 0; |
anmar | 0:0903545c0460 | 257 | |
anmar | 0:0903545c0460 | 258 | displayState = Init; |
anmar | 0:0903545c0460 | 259 | |
anmar | 0:0903545c0460 | 260 | //set_time(0); // Set RTC time to Wed, 28 Oct 2009 11:35:37 |
anmar | 0:0903545c0460 | 261 | |
anmar | 0:0903545c0460 | 262 | receiver = 1; //DEBUG |
anmar | 0:0903545c0460 | 263 | |
anmar | 0:0903545c0460 | 264 | term = new Terminal(USBTX, USBRX); |
anmar | 0:0903545c0460 | 265 | //presence = new Presence(term); |
anmar | 0:0903545c0460 | 266 | |
anmar | 0:0903545c0460 | 267 | flipper.attach(&flip, 1.0); |
anmar | 0:0903545c0460 | 268 | msflip.attach_us(&ms, 1000); |
anmar | 0:0903545c0460 | 269 | sflip.attach(&s, 1); |
anmar | 0:0903545c0460 | 270 | |
anmar | 0:0903545c0460 | 271 | |
anmar | 0:0903545c0460 | 272 | term->baud(115200); |
anmar | 0:0903545c0460 | 273 | |
anmar | 0:0903545c0460 | 274 | radio.baud(9600); |
anmar | 0:0903545c0460 | 275 | //radio.attach(&rxCallback, MODSERIAL::RxIrq); |
anmar | 0:0903545c0460 | 276 | |
anmar | 0:0903545c0460 | 277 | //uart.baud(9600); |
anmar | 0:0903545c0460 | 278 | |
anmar | 0:0903545c0460 | 279 | term->cls(); |
anmar | 0:0903545c0460 | 280 | term->locate(0, 0); |
anmar | 0:0903545c0460 | 281 | term->foreground(RED); |
anmar | 0:0903545c0460 | 282 | term->printf("\n\rConnected to presence detector...\n\r"); |
anmar | 0:0903545c0460 | 283 | //term->printf("\nPress 'o' to activate output\n\n\r"); |
anmar | 0:0903545c0460 | 284 | |
anmar | 0:0903545c0460 | 285 | term->foreground(WHITE); |
anmar | 0:0903545c0460 | 286 | term->printf("Delay\r\n"); |
anmar | 0:0903545c0460 | 287 | |
anmar | 0:0903545c0460 | 288 | //lcd.cls(); |
anmar | 0:0903545c0460 | 289 | |
anmar | 0:0903545c0460 | 290 | |
anmar | 0:0903545c0460 | 291 | lcd.printf("Hello"); |
anmar | 0:0903545c0460 | 292 | |
anmar | 0:0903545c0460 | 293 | char buffer[32]; |
anmar | 0:0903545c0460 | 294 | time_t seconds = time(NULL); |
anmar | 0:0903545c0460 | 295 | |
anmar | 0:0903545c0460 | 296 | |
anmar | 0:0903545c0460 | 297 | strftime(buffer, 32, "%F %T\n", localtime(&seconds)); |
anmar | 0:0903545c0460 | 298 | term->printf("Current time %s", buffer); |
anmar | 0:0903545c0460 | 299 | |
anmar | 0:0903545c0460 | 300 | |
anmar | 0:0903545c0460 | 301 | while (1) |
anmar | 0:0903545c0460 | 302 | { |
anmar | 0:0903545c0460 | 303 | char ch; |
anmar | 0:0903545c0460 | 304 | char buffer[50]; |
anmar | 0:0903545c0460 | 305 | int pos = 0; |
anmar | 0:0903545c0460 | 306 | char rx; |
anmar | 0:0903545c0460 | 307 | |
anmar | 0:0903545c0460 | 308 | msticks = 0; |
anmar | 0:0903545c0460 | 309 | /*while ( uart.readable() ) |
anmar | 0:0903545c0460 | 310 | { |
anmar | 0:0903545c0460 | 311 | term->printf("%c", uart.getc()); |
anmar | 0:0903545c0460 | 312 | } */ |
anmar | 0:0903545c0460 | 313 | |
anmar | 0:0903545c0460 | 314 | if ( radio.readable() ) |
anmar | 0:0903545c0460 | 315 | { |
anmar | 0:0903545c0460 | 316 | while ( msticks < 100 ) |
anmar | 0:0903545c0460 | 317 | { |
anmar | 0:0903545c0460 | 318 | if ( radio.readable() ) |
anmar | 0:0903545c0460 | 319 | { |
anmar | 0:0903545c0460 | 320 | rx = radio.getc(); |
anmar | 0:0903545c0460 | 321 | if ( rx == '!' ) // Look for start of package |
anmar | 0:0903545c0460 | 322 | { |
anmar | 0:0903545c0460 | 323 | pos = 0; |
anmar | 0:0903545c0460 | 324 | } |
anmar | 0:0903545c0460 | 325 | //term->printf("%c", rx); |
anmar | 0:0903545c0460 | 326 | buffer[pos++] = rx; |
anmar | 0:0903545c0460 | 327 | |
anmar | 0:0903545c0460 | 328 | if ( rx == '\n' || pos > 40 ) // Prevent garbage from causing buffer overflow |
anmar | 0:0903545c0460 | 329 | { |
anmar | 0:0903545c0460 | 330 | buffer[pos] = '\0'; |
anmar | 0:0903545c0460 | 331 | break; |
anmar | 0:0903545c0460 | 332 | } |
anmar | 0:0903545c0460 | 333 | //term->printf(","); |
anmar | 0:0903545c0460 | 334 | } |
anmar | 0:0903545c0460 | 335 | } |
anmar | 0:0903545c0460 | 336 | } |
anmar | 0:0903545c0460 | 337 | |
anmar | 0:0903545c0460 | 338 | |
anmar | 0:0903545c0460 | 339 | if ( buffer[0] == '!' && (buffer[1] == 'M' || buffer[1] == 'P') && buffer[9] == '\n' ) //&& strlen(buffer) == 10 ) |
anmar | 0:0903545c0460 | 340 | { // format: "!M00000pp\n" Where "00000" are number of second since last movement and 'pp' is the ASCII hex coded xor parity byte |
anmar | 0:0903545c0460 | 341 | uint8_t p = 0; |
anmar | 0:0903545c0460 | 342 | char *ptr; |
anmar | 0:0903545c0460 | 343 | int parity; |
anmar | 0:0903545c0460 | 344 | char text[10]; |
anmar | 0:0903545c0460 | 345 | |
anmar | 0:0903545c0460 | 346 | |
anmar | 0:0903545c0460 | 347 | if ( buffer[1] == 'P' ) |
anmar | 0:0903545c0460 | 348 | { |
anmar | 0:0903545c0460 | 349 | presence = 1; |
anmar | 0:0903545c0460 | 350 | } |
anmar | 0:0903545c0460 | 351 | else |
anmar | 0:0903545c0460 | 352 | { |
anmar | 0:0903545c0460 | 353 | presence = 0; |
anmar | 0:0903545c0460 | 354 | } |
anmar | 0:0903545c0460 | 355 | |
anmar | 0:0903545c0460 | 356 | for ( uint8_t x = 0; x < 7; x++ ) |
anmar | 0:0903545c0460 | 357 | { |
anmar | 0:0903545c0460 | 358 | p ^= buffer[x]; |
anmar | 0:0903545c0460 | 359 | } |
anmar | 0:0903545c0460 | 360 | |
anmar | 0:0903545c0460 | 361 | ptr = buffer + 7; |
anmar | 0:0903545c0460 | 362 | sscanf(ptr, "%x", &parity); |
anmar | 0:0903545c0460 | 363 | |
anmar | 0:0903545c0460 | 364 | //printf("%d %d\r\n", p, parity); |
anmar | 0:0903545c0460 | 365 | |
anmar | 0:0903545c0460 | 366 | if ( p == parity ) |
anmar | 0:0903545c0460 | 367 | { |
anmar | 0:0903545c0460 | 368 | strncpy(text, buffer + 2, 5); |
anmar | 0:0903545c0460 | 369 | text[5] = '\0'; |
anmar | 0:0903545c0460 | 370 | |
anmar | 0:0903545c0460 | 371 | sscanf(text, "%d", &seconds_since_movement); |
anmar | 0:0903545c0460 | 372 | seconds_since_last_update = 0; |
anmar | 0:0903545c0460 | 373 | |
anmar | 0:0903545c0460 | 374 | /*if ( seconds_since_movement < 10 ) |
anmar | 0:0903545c0460 | 375 | { |
anmar | 0:0903545c0460 | 376 | //printf("\xe2\x88\x9e\n"); |
anmar | 0:0903545c0460 | 377 | if ( displayState != PresenceDetected ) |
anmar | 0:0903545c0460 | 378 | { |
anmar | 0:0903545c0460 | 379 | displayState = PresenceDetected; |
anmar | 0:0903545c0460 | 380 | //printf("\r\nN%crvaro detekterad \r\n", C_ae); |
anmar | 0:0903545c0460 | 381 | //printf("%d\r\n", last_elapsed_time); |
anmar | 0:0903545c0460 | 382 | } |
anmar | 0:0903545c0460 | 383 | } |
anmar | 0:0903545c0460 | 384 | else |
anmar | 0:0903545c0460 | 385 | { |
anmar | 0:0903545c0460 | 386 | displayState = LastPresenceDetected; |
anmar | 0:0903545c0460 | 387 | // printf("Seconds since last movement: %d \r", seconds_since_movement); |
anmar | 0:0903545c0460 | 388 | } */ |
anmar | 0:0903545c0460 | 389 | } |
anmar | 0:0903545c0460 | 390 | } |
anmar | 0:0903545c0460 | 391 | /*else if ( !strcmp(buffer, "!P1\n") ) |
anmar | 0:0903545c0460 | 392 | { // format: "!P1\n" Confirmed presence |
anmar | 0:0903545c0460 | 393 | seconds_since_last_update = 0; |
anmar | 0:0903545c0460 | 394 | seconds_since_movement = 0; |
anmar | 0:0903545c0460 | 395 | } */ |
anmar | 0:0903545c0460 | 396 | |
anmar | 0:0903545c0460 | 397 | memset(buffer, '\0',10); // Clear buffer until next packet is received |
anmar | 0:0903545c0460 | 398 | |
anmar | 0:0903545c0460 | 399 | //Presence, LastPresenceDetected, CommunicationFault } displayState; |
anmar | 0:0903545c0460 | 400 | |
anmar | 0:0903545c0460 | 401 | if ( print_tick ) |
anmar | 0:0903545c0460 | 402 | { |
anmar | 0:0903545c0460 | 403 | time_t seconds = time(NULL); |
anmar | 0:0903545c0460 | 404 | char str[32]; |
anmar | 0:0903545c0460 | 405 | strftime(str, 32, "[%F %T]", localtime(&seconds)); |
anmar | 0:0903545c0460 | 406 | |
anmar | 0:0903545c0460 | 407 | |
anmar | 0:0903545c0460 | 408 | showPresence(seconds_since_movement); |
anmar | 0:0903545c0460 | 409 | |
anmar | 0:0903545c0460 | 410 | if ( seconds_since_movement ) |
anmar | 0:0903545c0460 | 411 | { |
anmar | 0:0903545c0460 | 412 | //showAbsence(seconds_since_movement); |
anmar | 0:0903545c0460 | 413 | if ( displayState != LastPresenceDetected ) |
anmar | 0:0903545c0460 | 414 | { |
anmar | 0:0903545c0460 | 415 | term->printf("%s D%crr %cppnad/st%cngd\r\n", str, C_oe, C_oe, C_ae); |
anmar | 0:0903545c0460 | 416 | displayState = LastPresenceDetected; |
anmar | 0:0903545c0460 | 417 | //showAbsence(); |
anmar | 0:0903545c0460 | 418 | } |
anmar | 0:0903545c0460 | 419 | if ( seconds_since_movement > last_inactive_period ) |
anmar | 0:0903545c0460 | 420 | { |
anmar | 0:0903545c0460 | 421 | last_inactive_period = seconds_since_movement; |
anmar | 0:0903545c0460 | 422 | } |
anmar | 0:0903545c0460 | 423 | } |
anmar | 0:0903545c0460 | 424 | else |
anmar | 0:0903545c0460 | 425 | { |
anmar | 0:0903545c0460 | 426 | // showPresence(seconds_since_movement); |
anmar | 0:0903545c0460 | 427 | |
anmar | 0:0903545c0460 | 428 | if ( displayState != PresenceDetected ) |
anmar | 0:0903545c0460 | 429 | { |
anmar | 0:0903545c0460 | 430 | term->printf("Idle: %d\r\n", last_inactive_period); |
anmar | 0:0903545c0460 | 431 | term->printf("%s N%crvaro detekterad\r\n", str, C_ae); |
anmar | 0:0903545c0460 | 432 | |
anmar | 0:0903545c0460 | 433 | displayState = PresenceDetected; |
anmar | 0:0903545c0460 | 434 | last_inactive_period = 0; |
anmar | 0:0903545c0460 | 435 | } |
anmar | 0:0903545c0460 | 436 | } |
anmar | 0:0903545c0460 | 437 | |
anmar | 0:0903545c0460 | 438 | if ( seconds_since_movement < last_elapsed_time ) |
anmar | 0:0903545c0460 | 439 | { |
anmar | 0:0903545c0460 | 440 | //printf("%s %d\r\n", str, last_elapsed_time); |
anmar | 0:0903545c0460 | 441 | } |
anmar | 0:0903545c0460 | 442 | |
anmar | 0:0903545c0460 | 443 | //printf("%d %d\r\n", seconds_since_movement, last_elapsed_time); |
anmar | 0:0903545c0460 | 444 | |
anmar | 0:0903545c0460 | 445 | last_elapsed_time = seconds_since_movement; |
anmar | 0:0903545c0460 | 446 | if ( seconds_since_last_update > 10 ) |
anmar | 0:0903545c0460 | 447 | { |
anmar | 0:0903545c0460 | 448 | //if ( displayState != CommunicationFault ) |
anmar | 0:0903545c0460 | 449 | { |
anmar | 0:0903545c0460 | 450 | displayState = CommunicationFault; |
anmar | 0:0903545c0460 | 451 | term->printf("%s Kommunikationsfel %d\r\n", str, seconds_since_last_update); |
anmar | 0:0903545c0460 | 452 | } |
anmar | 0:0903545c0460 | 453 | } |
anmar | 0:0903545c0460 | 454 | print_tick = 0; |
anmar | 0:0903545c0460 | 455 | } |
anmar | 0:0903545c0460 | 456 | |
anmar | 0:0903545c0460 | 457 | |
anmar | 0:0903545c0460 | 458 | if ( term->readable()) |
anmar | 0:0903545c0460 | 459 | { |
anmar | 0:0903545c0460 | 460 | ch = term->getc(); |
anmar | 0:0903545c0460 | 461 | |
anmar | 0:0903545c0460 | 462 | switch(ch) |
anmar | 0:0903545c0460 | 463 | { |
anmar | 0:0903545c0460 | 464 | case 'r': |
anmar | 0:0903545c0460 | 465 | reset.output(); |
anmar | 0:0903545c0460 | 466 | break; |
anmar | 0:0903545c0460 | 467 | |
anmar | 0:0903545c0460 | 468 | case 't': |
anmar | 0:0903545c0460 | 469 | int y; |
anmar | 0:0903545c0460 | 470 | int m; |
anmar | 0:0903545c0460 | 471 | int d; |
anmar | 0:0903545c0460 | 472 | int h; |
anmar | 0:0903545c0460 | 473 | int mi; |
anmar | 0:0903545c0460 | 474 | int s; |
anmar | 0:0903545c0460 | 475 | char buf[100]; |
anmar | 0:0903545c0460 | 476 | char *ptr; |
anmar | 0:0903545c0460 | 477 | term->printf("Enter new time: YYYY-MM-DD-HH-MM-SS\r\n"); |
anmar | 0:0903545c0460 | 478 | term->scanf("%s", buf); |
anmar | 0:0903545c0460 | 479 | sscanf(buf, "%4d-%2d-%2d-%2d-%2d-%2d", &y, &m, &d, &h, &mi, &s); |
anmar | 0:0903545c0460 | 480 | |
anmar | 0:0903545c0460 | 481 | /* term->printf("Entered string: %s\r\n", buf); |
anmar | 0:0903545c0460 | 482 | term->printf("%4d-%2d-%2d %2d:%2d:%2d", y, m, d, h, mi, s); */ |
anmar | 0:0903545c0460 | 483 | |
anmar | 0:0903545c0460 | 484 | |
anmar | 0:0903545c0460 | 485 | |
anmar | 0:0903545c0460 | 486 | time_t rawtime; |
anmar | 0:0903545c0460 | 487 | struct tm * timeinfo; |
anmar | 0:0903545c0460 | 488 | //int year, month ,day; |
anmar | 0:0903545c0460 | 489 | const char * weekday[] = { "Sunday", "Monday", |
anmar | 0:0903545c0460 | 490 | "Tuesday", "Wednesday", |
anmar | 0:0903545c0460 | 491 | "Thursday", "Friday", "Saturday"}; |
anmar | 0:0903545c0460 | 492 | |
anmar | 0:0903545c0460 | 493 | /* get current timeinfo and modify it to the user's choice */ |
anmar | 0:0903545c0460 | 494 | time ( &rawtime ); |
anmar | 0:0903545c0460 | 495 | timeinfo = localtime ( &rawtime ); |
anmar | 0:0903545c0460 | 496 | timeinfo->tm_year = y - 1900; |
anmar | 0:0903545c0460 | 497 | timeinfo->tm_mon = m - 1; |
anmar | 0:0903545c0460 | 498 | timeinfo->tm_mday = d; |
anmar | 0:0903545c0460 | 499 | |
anmar | 0:0903545c0460 | 500 | timeinfo->tm_hour = h; |
anmar | 0:0903545c0460 | 501 | timeinfo->tm_min = mi; |
anmar | 0:0903545c0460 | 502 | timeinfo->tm_sec = s; |
anmar | 0:0903545c0460 | 503 | |
anmar | 0:0903545c0460 | 504 | |
anmar | 0:0903545c0460 | 505 | /* call mktime: timeinfo->tm_wday will be set */ |
anmar | 0:0903545c0460 | 506 | set_time(mktime ( timeinfo )); |
anmar | 0:0903545c0460 | 507 | |
anmar | 0:0903545c0460 | 508 | |
anmar | 0:0903545c0460 | 509 | time_t seconds = time(NULL); |
anmar | 0:0903545c0460 | 510 | char str[32]; |
anmar | 0:0903545c0460 | 511 | strftime(str, 32, "[%F %T]", localtime(&seconds)); |
anmar | 0:0903545c0460 | 512 | term->printf("New time: %s\r\n", str); |
anmar | 0:0903545c0460 | 513 | |
anmar | 0:0903545c0460 | 514 | break; |
anmar | 0:0903545c0460 | 515 | case 'o': |
anmar | 0:0903545c0460 | 516 | printf("Toggle output\r\n"); |
anmar | 0:0903545c0460 | 517 | showoutput = !showoutput; |
anmar | 0:0903545c0460 | 518 | term->cls(); |
anmar | 0:0903545c0460 | 519 | |
anmar | 0:0903545c0460 | 520 | break; |
anmar | 0:0903545c0460 | 521 | |
anmar | 0:0903545c0460 | 522 | default: |
anmar | 0:0903545c0460 | 523 | break; |
anmar | 0:0903545c0460 | 524 | } |
anmar | 0:0903545c0460 | 525 | } |
anmar | 0:0903545c0460 | 526 | |
anmar | 0:0903545c0460 | 527 | // led1 = !led1; |
anmar | 0:0903545c0460 | 528 | |
anmar | 0:0903545c0460 | 529 | if ( showoutput ) |
anmar | 0:0903545c0460 | 530 | { |
anmar | 0:0903545c0460 | 531 | /*clear_screen++; |
anmar | 0:0903545c0460 | 532 | if ( clear_screen > 50 ) |
anmar | 0:0903545c0460 | 533 | { |
anmar | 0:0903545c0460 | 534 | term->cls(); |
anmar | 0:0903545c0460 | 535 | clear_screen = 0; |
anmar | 0:0903545c0460 | 536 | }*/ |
anmar | 0:0903545c0460 | 537 | /*term->locate(0, 0); |
anmar | 0:0903545c0460 | 538 | |
anmar | 0:0903545c0460 | 539 | term->printf("Sensor output:\r\n"); |
anmar | 0:0903545c0460 | 540 | term->printf("--------------------------------------------------\r\n"); */ |
anmar | 0:0903545c0460 | 541 | //presence->thermal->measure(); |
anmar | 0:0903545c0460 | 542 | |
anmar | 0:0903545c0460 | 543 | for ( int x = 0; x < 4; x++ ) |
anmar | 0:0903545c0460 | 544 | { |
anmar | 0:0903545c0460 | 545 | for ( int y = 0; y < 4; y++ ) |
anmar | 0:0903545c0460 | 546 | { |
anmar | 0:0903545c0460 | 547 | |
anmar | 0:0903545c0460 | 548 | /* term->foreground(RED); |
anmar | 0:0903545c0460 | 549 | term->foreground(YELLOW); |
anmar | 0:0903545c0460 | 550 | term->foreground(GREEN); |
anmar | 0:0903545c0460 | 551 | term->printf("%1.3fV ", voltage); |
anmar | 0:0903545c0460 | 552 | term->foreground(WHITE);*/ |
anmar | 0:0903545c0460 | 553 | } |
anmar | 0:0903545c0460 | 554 | // term->printf("\r\n"); |
anmar | 0:0903545c0460 | 555 | } |
anmar | 0:0903545c0460 | 556 | } |
anmar | 0:0903545c0460 | 557 | |
anmar | 0:0903545c0460 | 558 | //wait(0.2); |
anmar | 0:0903545c0460 | 559 | } |
anmar | 0:0903545c0460 | 560 | } |
anmar | 0:0903545c0460 | 561 | |
anmar | 0:0903545c0460 | 562 | |
anmar | 0:0903545c0460 | 563 | |
anmar | 0:0903545c0460 | 564 |