Mercurial > ~darius > hgwebdir.cgi > tempctrl
comparison testavr.c @ 51:cb184206344d
Rejig command parsing and assume the compiler isn't dumb (eg it can reuse
stack variables).
author | darius@Inchoate |
---|---|
date | Wed, 29 Oct 2008 16:09:55 +1030 |
parents | 13a68734348b |
children | 3217e93b28a3 |
comparison
equal
deleted
inserted
replaced
50:a13e0ccc1d2d | 51:cb184206344d |
---|---|
168 | 168 |
169 /* User just pressed enter */ | 169 /* User just pressed enter */ |
170 if (cmd.len == 0) | 170 if (cmd.len == 0) |
171 return; | 171 return; |
172 | 172 |
173 if (cmd.buf[0] == '?') { | 173 if (!strcasecmp_P((char *)cmd.buf, PSTR("?")) || |
174 !strcasecmp_P((char *)cmd.buf, PSTR("help"))) { | |
174 printf_P(PSTR("rs Reset and check for presence\r\n" | 175 printf_P(PSTR("rs Reset and check for presence\r\n" |
175 "sr Search the bus for ROMs\r\n" | 176 "sr Search the bus for ROMs\r\n" |
176 "re Read a bit\r\n" | 177 "re Read a bit\r\n" |
177 "rb Read a byte\r\n" | 178 "rb Read a byte\r\n" |
178 "wr bit Write a bit\r\n" | 179 "wr bit Write a bit\r\n" |
179 "wb byte Write a byte (hex)\r\n" | 180 "wb byte Write a byte (hex)\r\n" |
180 "wc cmd [ROMID] Write command\r\n" | 181 "wc cmd [ROMID] Write command\r\n" |
181 "te ROMID Read the temperature from a DS1820\r\n" | 182 "te ROMID Read the temperature from a DS1820\r\n" |
182 "in port Read from a port\r\n" | 183 "in port Read from a port\r\n" |
183 "out port val Write to a port\r\n" | 184 "ou port val Write to a port (val in hex)\r\n" |
184 "ddr port [val] Read/write DDR for a port\r\n" | 185 "dd port [val] Read/write DDR for a port (val in hex)\r\n" |
185 "tc ... Temperature control related (tc help for more)\r\n")); | 186 "rt ROMID Read DS2502 status page\r\n" |
187 "we ROMID adr val Write data into a DS2502 PROM (adr & val in hex)\r\n" | |
188 "rr ROMID Read DS2502 PROM\r\n" | |
189 "zz Reset MCU\r\n" | |
190 #ifdef WITHUSB | |
191 "us Generate USB data\r\n" | |
192 #endif | |
193 "tc ... Temperature control related (tc help for more)\r\n")); | |
186 | 194 |
187 return; | 195 return; |
188 } | 196 } else if (!strncasecmp_P((char *)cmd.buf, PSTR("zz"), 2)) { |
189 | 197 cli(); |
190 i = strlen((char *)cmd.buf); | 198 wdt_enable(WDTO_15MS); |
191 if (cmd.len < 2) | 199 for (;;) |
192 goto badcmd; | 200 ; |
193 | 201 } else if (!strncasecmp_P((char *)cmd.buf, PSTR("rs"), 2)) { |
194 if (cmd.buf[0] == 'r' && cmd.buf[1] == 's') { | |
195 printf_P(PSTR("Resetting... ")); | 202 printf_P(PSTR("Resetting... ")); |
196 | 203 |
197 if (OWTouchReset() == 1) | 204 if (OWTouchReset() == 1) |
198 printf_P(PSTR("No presence pulse found\r\n")); | 205 printf_P(PSTR("No presence pulse found\r\n")); |
199 else | 206 else |
200 printf_P(PSTR("Presence pulse found\r\n")); | 207 printf_P(PSTR("Presence pulse found\r\n")); |
201 } else if (cmd.buf[0] == 'r' && cmd.buf[1] == 'e') { | 208 } else if (!strncasecmp_P((char *)cmd.buf, PSTR("re"), 2)) { |
202 if (OWReadBit()) | 209 if (OWReadBit()) |
203 printf_P(PSTR("Read a 1\r\n")); | 210 printf_P(PSTR("Read a 1\r\n")); |
204 else | 211 else |
205 printf_P(PSTR("Read a 0\r\n")); | 212 printf_P(PSTR("Read a 0\r\n")); |
206 } else if (cmd.buf[0] == 'r' && cmd.buf[1] == 'b') { | 213 } else if (!strncasecmp_P((char *)cmd.buf, PSTR("rb"), 2)) { |
207 printf_P(PSTR("Read a 0x%02x\r\n"), OWReadByte()); | 214 printf_P(PSTR("Read a 0x%02x\r\n"), OWReadByte()); |
208 } else if (cmd.buf[0] == 'w' && cmd.buf[1] == 'r') { | 215 } else if (!strncasecmp_P((char *)cmd.buf, PSTR("wr"), 2)) { |
209 arg = strtol((char *)cmd.buf + 3, (char **)NULL, 10); | 216 arg = strtol((char *)cmd.buf + 3, (char **)NULL, 10); |
210 OWWriteBit(arg); | 217 OWWriteBit(arg); |
211 printf_P(PSTR("Wrote a %c\r\n"), arg ? '1' : '0'); | 218 printf_P(PSTR("Wrote a %c\r\n"), arg ? '1' : '0'); |
212 } else if (cmd.buf[0] == 'w' && cmd.buf[1] == 'b') { | 219 } else if (!strncasecmp_P((char *)cmd.buf, PSTR("wb"), 2)) { |
213 arg = (int)strtol((char *)cmd.buf + 3, (char **)NULL, 16); | 220 arg = (int)strtol((char *)cmd.buf + 3, (char **)NULL, 16); |
214 OWWriteByte(arg); | 221 OWWriteByte(arg); |
215 } else if (cmd.buf[0] == 'r' && cmd.buf[1] == 't') { | 222 } else if (!strncasecmp_P((char *)cmd.buf, PSTR("rt"), 2)) { |
216 if (cmd.len < 26) { | 223 if (sscanf_P((char *)cmd.buf, PSTR("rt %hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhx"), |
224 &ROM[0], &ROM[1], &ROM[2], &ROM[3], | |
225 &ROM[4], &ROM[5], &ROM[6], &ROM[7]) != 8) { | |
217 printf_P(PSTR("Unable to parse ROM ID\r\n")); | 226 printf_P(PSTR("Unable to parse ROM ID\r\n")); |
218 return; | 227 return; |
219 } | 228 } |
220 | 229 |
221 if (OWTouchReset() != 0) { | |
222 printf_P(PSTR("No presence\r\n")); | |
223 return; | |
224 } | |
225 | |
226 for (i = 0; i < 8; i++) | |
227 ROM[i] = (int)strtol((char *)cmd.buf + 3 * (i + 1), (char **)NULL, 16); | |
228 | |
229 if (ROM[0] != OW_FAMILY_ROM) { | 230 if (ROM[0] != OW_FAMILY_ROM) { |
230 printf_P(PSTR("ROM specified isn't a DS2502\r\n")); | 231 printf_P(PSTR("ROM specified isn't a DS2502\r\n")); |
231 return; | 232 return; |
232 } | 233 } |
233 | 234 |
261 printf_P(PSTR("\r\n")); | 262 printf_P(PSTR("\r\n")); |
262 if (crc != OWReadByte()) { | 263 if (crc != OWReadByte()) { |
263 printf_P(PSTR("CRC mismatch on data\r\n")); | 264 printf_P(PSTR("CRC mismatch on data\r\n")); |
264 return; | 265 return; |
265 } | 266 } |
266 } else if (cmd.buf[0] == 'w' && cmd.buf[1] == 'e') { | 267 } else if (!strncasecmp_P((char *)cmd.buf, PSTR("we"), 2)) { |
267 if (cmd.len < 26) { | 268 uint8_t adr, data; |
269 | |
270 if (sscanf_P((char *)cmd.buf, PSTR("we %hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhx %hhx %hhx"), | |
271 &ROM[0], &ROM[1], &ROM[2], &ROM[3], | |
272 &ROM[4], &ROM[5], &ROM[6], &ROM[7], | |
273 &adr, &data) != 10) { | |
268 printf_P(PSTR("Unable to parse ROM ID\r\n")); | 274 printf_P(PSTR("Unable to parse ROM ID\r\n")); |
269 return; | 275 return; |
270 } | 276 } |
271 | 277 |
272 for (i = 0; i < 8; i++) | |
273 ROM[i] = (int)strtol((char *)cmd.buf + 3 * (i + 1), (char **)NULL, 16); | |
274 | |
275 if (ROM[0] != OW_FAMILY_ROM) { | 278 if (ROM[0] != OW_FAMILY_ROM) { |
276 printf_P(PSTR("ROM specified isn't a ROM\r\n")); | 279 printf_P(PSTR("ID specified isn't a ROM\r\n")); |
277 return; | 280 return; |
278 } | 281 } |
279 | 282 |
280 buf[0] = (int)strtol((char *)cmd.buf + 27, (char **)NULL, 16); /* Address */ | |
281 buf[1] = (int)strtol((char *)cmd.buf + 30, (char **)NULL, 16); /* Data .. */ | |
282 buf[2] = (int)strtol((char *)cmd.buf + 33, (char **)NULL, 16); | |
283 | |
284 if (OWTouchReset() != 0) { | 283 if (OWTouchReset() != 0) { |
285 printf_P(PSTR("No presence\r\n")); | 284 printf_P(PSTR("No presence\r\n")); |
286 return; | 285 return; |
287 } | 286 } |
288 | 287 |
289 i = OWProgROM(ROM, buf[0], 2, &buf[1], 0, 0); | 288 printf_P(PSTR("OWProgROM returned %S\r\n"), OWProgROM_Status[OWProgROM(ROM, buf[0], 2, &buf[1], 0, 0)]); |
290 printf_P(PSTR("OWProgROM returned %d\r\n"), i); | 289 } else if (!strncasecmp_P((char *)cmd.buf, PSTR("rr"), 2)) { |
291 } else if (cmd.buf[0] == 'r' && cmd.buf[1] == 'r') { | 290 if (sscanf_P((char *)cmd.buf, PSTR("rr %hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhx"), |
292 if (cmd.len < 26) { | 291 &ROM[0], &ROM[1], &ROM[2], &ROM[3], |
292 &ROM[4], &ROM[5], &ROM[6], &ROM[7]) != 8) { | |
293 printf_P(PSTR("Unable to parse ROM ID\r\n")); | 293 printf_P(PSTR("Unable to parse ROM ID\r\n")); |
294 return; | 294 return; |
295 } | 295 } |
296 | |
297 for (i = 0; i < 8; i++) | |
298 ROM[i] = (int)strtol((char *)cmd.buf + 3 * (i + 1), (char **)NULL, 16); | |
299 | 296 |
300 if (ROM[0] != OW_FAMILY_ROM) { | 297 if (ROM[0] != OW_FAMILY_ROM) { |
301 printf_P(PSTR("ROM specified isn't a ROM\r\n")); | 298 printf_P(PSTR("ROM specified isn't a ROM\r\n")); |
302 return; | 299 return; |
303 } | 300 } |
334 if (crc != OWReadByte()) { | 331 if (crc != OWReadByte()) { |
335 printf_P(PSTR("CRC mismatch on data\r\n")); | 332 printf_P(PSTR("CRC mismatch on data\r\n")); |
336 return; | 333 return; |
337 } | 334 } |
338 | 335 |
339 } else if (cmd.buf[0] == 'w' && cmd.buf[1] == 'c') { | 336 } else if (!strncasecmp_P((char *)cmd.buf, PSTR("wc"), 2)) { |
340 if (cmd.len < 5) { | 337 uint8_t c; |
341 printf_P(PSTR("No arguments\r\n")); | 338 |
339 i = sscanf_P((char *)cmd.buf, PSTR("wc %hhx %hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhx"), | |
340 &ROM[0], &ROM[1], &ROM[2], &ROM[3], | |
341 &ROM[4], &ROM[5], &ROM[6], &ROM[7], | |
342 &c); | |
343 | |
344 if (i != 1 && i != 9) { | |
345 printf_P(PSTR("Incorrect usage\r\n")); | |
342 return; | 346 return; |
343 } | 347 } |
344 | 348 |
345 arg = (int)strtol((char *)cmd.buf + 3, (char **)NULL, 16); | 349 if (i == 1) { |
346 if (arg == 0) { | 350 OWSendCmd(i == 1 ? NULL : ROM, c); |
347 printf_P(PSTR("Unparseable command\r\n")); | 351 return; |
348 return; | 352 } |
349 } | 353 } else if (!strncasecmp_P((char *)cmd.buf, PSTR("te"), 2)) { |
350 | 354 if (sscanf_P((char *)cmd.buf, PSTR("te %hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhx"), |
351 if (i == 5) { | 355 &ROM[0], &ROM[1], &ROM[2], &ROM[3], |
352 OWSendCmd(NULL, arg); | 356 &ROM[4], &ROM[5], &ROM[6], &ROM[7]) != 8) { |
353 return; | |
354 } | |
355 | |
356 if (i < 29) { | |
357 printf_P(PSTR("Can't parse ROM ID\r\n")); | |
358 return; | |
359 } | |
360 for (i = 0; i < 8; i++) | |
361 ROM[i] = (int)strtol((char *)cmd.buf + 6 + (3 * i), (char **)NULL, 16); | |
362 | |
363 OWSendCmd(ROM, arg); | |
364 } else if (cmd.buf[0] == 't' && cmd.buf[1] == 'e') { | |
365 if (cmd.len < 26) { | |
366 printf_P(PSTR("Unable to parse ROM ID\r\n")); | 357 printf_P(PSTR("Unable to parse ROM ID\r\n")); |
367 return; | 358 return; |
368 } | 359 } |
369 | 360 |
370 for (i = 0; i < 8; i++) | 361 t = OWGetTemp(ROM); |
371 ROM[i] = (int)strtol((char *)cmd.buf + 3 * (i + 1), (char **)NULL, 16); | 362 switch (t) { |
372 | 363 case OW_TEMP_WRONG_FAM: |
373 if (ROM[0] != OW_FAMILY_TEMP) { | 364 printf_P(PSTR("ROM specified isn't a temperature sensor\r\n")); |
374 printf_P(PSTR("ROM specified isn't a temperature sensor\r\n")); | 365 break; |
375 return; | 366 |
376 } | 367 case OW_TEMP_CRC_ERR: |
377 | 368 printf_P(PSTR("CRC mismatch\r\n")); |
378 OWSendCmd(ROM, OW_CONVERTT_CMD); | 369 break; |
379 i = 0; | 370 |
380 /* Wait for the conversion */ | 371 case OW_TEMP_NO_ROM: |
381 while (OWReadBit() == 0) | 372 printf_P(PSTR("No ROM found\r\n")); |
382 i = 1; | 373 break; |
383 | 374 |
384 | 375 default: |
385 OWSendCmd(ROM, OW_RD_SCR_CMD); | 376 printf_P(PSTR("%d.%02d\r\n"), GETWHOLE(t), GETFRAC(t)); |
386 crc = 0; | 377 break; |
387 for (i = 0; i < 9; i++) { | 378 } |
388 buf[i] = OWReadByte(); | 379 } else if (!strncasecmp_P((char *)cmd.buf, PSTR("sr"), 2)) { |
389 if (i < 8) | |
390 OWCRC(buf[i], &crc); | |
391 } | |
392 | |
393 if (crc != buf[8]) { | |
394 printf_P(PSTR("CRC mismatch\r\n")); | |
395 return; | |
396 } | |
397 | |
398 /* 0 Temperature LSB | |
399 * 1 Temperature MSB | |
400 * 2 Th | |
401 * 3 Tl | |
402 * 4 Reserved | |
403 * 5 Reserved | |
404 * 6 Count Remain | |
405 * 7 Count per C | |
406 * 8 CRC | |
407 */ | |
408 #if 0 | |
409 for (i = 0; i < 9; i++) | |
410 printf_P(PSTR("%d\r\n"), buf[i]); | |
411 #endif | |
412 temp = buf[0]; | |
413 if (buf[1] & 0x80) | |
414 temp -= 256; | |
415 temp >>= 1; | |
416 | |
417 tfrac = buf[7] - buf[6]; | |
418 tfrac *= (uint16_t)100; | |
419 tfrac /= buf[7]; | |
420 tfrac += 75; | |
421 if (tfrac < 100) { | |
422 temp--; | |
423 } else { | |
424 tfrac -= 100; | |
425 } | |
426 | |
427 printf_P(PSTR("%d.%02d\r\n"), temp, tfrac); | |
428 } else if (cmd.buf[0] == 's' && cmd.buf[1] == 'r') { | |
429 memset(ROM, 0, 8); | 380 memset(ROM, 0, 8); |
430 | 381 |
431 i = OWFirst(ROM, 1, 0); | 382 i = OWFirst(ROM, 1, 0); |
432 do { | 383 do { |
433 switch (i) { | 384 switch (i) { |
458 for (i = 0; i < 8; i++) | 409 for (i = 0; i < 8; i++) |
459 printf_P(PSTR("%02x%S"), ROM[i], i == 7 ? PSTR("\r\n") : PSTR(":")); | 410 printf_P(PSTR("%02x%S"), ROM[i], i == 7 ? PSTR("\r\n") : PSTR(":")); |
460 | 411 |
461 i = OWNext(ROM, 1, 0); | 412 i = OWNext(ROM, 1, 0); |
462 } while (1); | 413 } while (1); |
463 } else if (cmd.buf[0] == 'i' && cmd.buf[1] == 'n') { | 414 } else if (!strncasecmp_P((char *)cmd.buf, PSTR("in"), 2)) { |
415 uint8_t inp; | |
416 | |
464 switch (tolower(cmd.buf[3])) { | 417 switch (tolower(cmd.buf[3])) { |
465 case 'a': | 418 case 'a': |
466 crc = PINA; | 419 inp = PINA; |
467 break; | 420 break; |
468 | 421 |
469 case 'b': | 422 case 'b': |
470 crc = PINB; | 423 inp = PINB; |
471 break; | 424 break; |
472 | 425 |
473 case 'c': | 426 case 'c': |
474 crc = PINC; | 427 inp = PINC; |
475 break; | 428 break; |
476 | 429 |
477 case 'd': | 430 case 'd': |
478 crc = PIND; | 431 inp = PIND; |
479 break; | 432 break; |
480 | 433 |
481 default: | 434 default: |
482 printf_P(PSTR("Unknown port\r\n")); | 435 printf_P(PSTR("Unknown port\r\n")); |
483 return; | 436 return; |
484 } | 437 } |
485 printf_P(PSTR("0x%02x\r\n"), crc); | 438 printf_P(PSTR("0x%02x\r\n"), inp); |
486 } else if (cmd.buf[0] == 'o' && cmd.buf[1] == 'u') { | 439 } else if (!strncasecmp_P((char *)cmd.buf, PSTR("ou"), 2)) { |
487 crc = strtol((char *)cmd.buf + 8, (char **)NULL, 16); | 440 char port; |
488 switch (tolower(cmd.buf[4])) { | 441 int val; |
442 | |
443 if (sscanf_P((char *)cmd.buf, PSTR("ou %c %x"), &port, &val) != 2) { | |
444 printf_P(PSTR("Unable to parse ou arguments\r\n")); | |
445 return; | |
446 } | |
447 | |
448 switch (port) { | |
489 case 'a': | 449 case 'a': |
490 PORTA = crc; | 450 PORTA = val & 0xff; |
491 break; | 451 break; |
492 | 452 |
493 case 'b': | 453 case 'b': |
494 PORTB = crc; | 454 PORTB = val & 0xff; |
495 break; | 455 break; |
496 | 456 |
497 case 'c': | 457 case 'c': |
498 PORTC = crc; | 458 PORTC = val & 0xff; |
499 break; | 459 break; |
500 | 460 |
501 case 'd': | 461 case 'd': |
502 PORTD = crc; | 462 PORTD = val & 0xff; |
503 break; | 463 break; |
504 | 464 |
505 default: | 465 default: |
506 printf_P(PSTR("Unknown port\r\n")); | 466 printf_P(PSTR("Unknown port\r\n")); |
507 return; | 467 return; |
508 } | 468 } |
509 printf_P(PSTR("PORT%c <= 0x%02x\r\n"), toupper(cmd.buf[4]), crc); | 469 printf_P(PSTR("PORT%c <= 0x%02x\r\n"), toupper(port), val); |
510 } else if (cmd.buf[0] == 'd' && cmd.buf[1] == 'd') { | 470 } else if (!strncasecmp_P((char *)cmd.buf, PSTR("dd"), 2)) { |
511 crc = strtol((char *)cmd.buf + 8, (char **)NULL, 16); | 471 char port; |
512 switch (tolower(cmd.buf[4])) { | 472 uint8_t val; |
513 case 'a': | 473 int num; |
514 DDRA = crc; | 474 |
515 break; | 475 num = sscanf_P((char *)cmd.buf, PSTR("dd %c %x"), &port, &val); |
516 | 476 |
517 case 'b': | 477 if (num != 2 && num != 3) { |
518 DDRB = crc; | 478 printf_P(PSTR("Unable to parse dd arguments\r\n")); |
519 break; | 479 return; |
520 | 480 } |
521 case 'c': | 481 |
522 DDRC = crc; | 482 if (num == 2) { |
523 break; | 483 switch (port) { |
524 | 484 case 'a': |
525 case 'd': | 485 val = DDRA; |
526 DDRD = crc; | 486 break; |
527 break; | 487 |
528 | 488 case 'b': |
529 default: | 489 val = DDRB; |
530 printf_P(PSTR("Unknown port\r\n")); | 490 break; |
531 return; | 491 |
532 } | 492 case 'c': |
533 printf_P(PSTR("0x%02x\r\n"), crc); | 493 val = DDRC; |
534 } else if (cmd.buf[0] == 't' && cmd.buf[1] == 'c') { | 494 break; |
495 | |
496 case 'd': | |
497 val = DDRD; | |
498 break; | |
499 | |
500 default: | |
501 printf_P(PSTR("Unknown port\r\n")); | |
502 return; | |
503 } | |
504 printf_P(PSTR("DDR%c => 0x%02x\r\n"), toupper(port), val); | |
505 } else { | |
506 switch (port) { | |
507 case 'a': | |
508 DDRA = val & 0xff; | |
509 break; | |
510 | |
511 case 'b': | |
512 DDRB = val & 0xff; | |
513 break; | |
514 | |
515 case 'c': | |
516 DDRC = val & 0xff; | |
517 break; | |
518 | |
519 case 'd': | |
520 DDRD = val & 0xff; | |
521 break; | |
522 | |
523 default: | |
524 printf_P(PSTR("Unknown port\r\n")); | |
525 return; | |
526 } | |
527 printf_P(PSTR("DDR%c <= 0x%02x\r\n"), toupper(port), val); | |
528 } | |
529 } else if (!strncasecmp_P((char *)cmd.buf, PSTR("tc"), 2)) { | |
535 tempctrl_cmd((char *)cmd.buf); | 530 tempctrl_cmd((char *)cmd.buf); |
536 #ifdef WITHUSB | 531 #ifdef WITHUSB |
537 } else if (cmd.buf[0] == 'u' && cmd.buf[1] == 's') { | 532 } else if (!strncasecmp_P((char *)cmd.buf, PSTR("us"), 2)) { |
538 usb_gendata(); | 533 usb_gendata(); |
539 #endif | 534 #endif |
540 } else { | 535 } else { |
541 badcmd: | 536 printf_P(PSTR("Unknown command, help for a list\r\n")); |
542 printf_P(PSTR("Unknown command, ? for a list\r\n")); | |
543 } | 537 } |
544 } | 538 } |
545 | 539 |
546 | 540 |