comparison main.c @ 116:9afff8f29844

set fridge params separatelly
author Matt Johnston <matt@ucc.asn.au>
date Fri, 05 Oct 2012 22:24:47 +0800
parents 90d1ebb941ab
children eecd2612a68e
comparison
equal deleted inserted replaced
115:90d1ebb941ab 116:9afff8f29844
1 #include <stdio.h> 1 #include <stdio.h>
2 #include <string.h> 2 #include <string.h>
3 #include <stddef.h> 3 #include <stddef.h>
4 #include <stdbool.h>
5 #include <stdlib.h>
4 #include <avr/io.h> 6 #include <avr/io.h>
5 #include <avr/interrupt.h> 7 #include <avr/interrupt.h>
6 #include <avr/sleep.h> 8 #include <avr/sleep.h>
7 #include <util/delay.h> 9 #include <util/delay.h>
8 #include <avr/pgmspace.h> 10 #include <avr/pgmspace.h>
479 { 481 {
480 printf_P(PSTR("measure %hu\n"), measure_wake); 482 printf_P(PSTR("measure %hu\n"), measure_wake);
481 printf_P(PSTR("comms %hu\n"), comms_wake); 483 printf_P(PSTR("comms %hu\n"), comms_wake);
482 printf_P(PSTR("wake %hhu\n"), wake_secs); 484 printf_P(PSTR("wake %hhu\n"), wake_secs);
483 printf_P(PSTR("tick %d\n"), TICK); 485 printf_P(PSTR("tick %d\n"), TICK);
484 printf_P(PSTR("fridge %.1fº\n"), fridge_setpoint / 10.0); 486 printf_P(PSTR("fridge %.1fº\n"), fridge_setpoint / 10.0f);
485 printf_P(PSTR("fridge difference %.1fº\n"), fridge_difference / 10.0); 487 printf_P(PSTR("fridge difference %.1fº\n"), fridge_difference / 10.0f);
486 printf_P(PSTR("fridge_delay %hu\n"), fridge_delay); 488 printf_P(PSTR("fridge_delay %hu\n"), fridge_delay);
487 printf_P(PSTR("sensors %hhu (%hhu)\n"), 489 printf_P(PSTR("sensors %hhu (%hhu)\n"),
488 n_sensors, MAX_SENSORS); 490 n_sensors, MAX_SENSORS);
489 printf_P(PSTR("meas %hu (%hu)\n"), 491 printf_P(PSTR("meas %hu (%hu)\n"),
490 max_measurements, TOTAL_MEASUREMENTS); 492 max_measurements, TOTAL_MEASUREMENTS);
494 cmd_set_params(const char *params) 496 cmd_set_params(const char *params)
495 { 497 {
496 uint16_t new_measure_wake; 498 uint16_t new_measure_wake;
497 uint16_t new_comms_wake; 499 uint16_t new_comms_wake;
498 uint8_t new_wake_secs; 500 uint8_t new_wake_secs;
499 int16_t new_fridge_setpoint; 501 int ret = sscanf_P(params, PSTR("%hu %hu %hhu"),
500 int16_t new_fridge_difference; 502 &new_measure_wake, &new_comms_wake, &new_wake_secs);
501 uint16_t new_fridge_delay; 503
502 int ret = sscanf_P(params, PSTR("%hu %hu %hhu %hd %hd %hu"), 504 if (ret != 3)
503 &new_measure_wake, &new_comms_wake, &new_wake_secs,
504 &new_fridge_setpoint, &new_fridge_difference, &new_fridge_delay);
505
506 if (ret != 6)
507 { 505 {
508 printf_P(PSTR("Bad values\n")); 506 printf_P(PSTR("Bad values\n"));
509 } 507 }
510 else 508 else
511 { 509 {
512 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) 510 ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
513 { 511 {
514 eeprom_write(new_measure_wake, measure_wake); 512 eeprom_write(new_measure_wake, measure_wake);
515 eeprom_write(new_comms_wake, comms_wake); 513 eeprom_write(new_comms_wake, comms_wake);
516 eeprom_write(new_wake_secs, wake_secs); 514 eeprom_write(new_wake_secs, wake_secs);
517 eeprom_write(new_fridge_setpoint, fridge_setpoint);
518 eeprom_write(new_fridge_difference, fridge_difference);
519 eeprom_write(new_fridge_delay, fridge_delay);
520 uint16_t magic = EXPECT_MAGIC; 515 uint16_t magic = EXPECT_MAGIC;
521 eeprom_write(magic, magic); 516 eeprom_write(magic, magic);
522 } 517 }
523 printf_P(PSTR("set_params for next boot\n")); 518 printf_P(PSTR("set_params for next boot\n"));
524 printf_P(PSTR("measure %hu comms %hu wake %hhu fridge %.1fº diff %.1f delay %hu\n"), 519 printf_P(PSTR("measure %hu comms %hu wake %hhu\n"),
525 new_measure_wake, new_comms_wake, new_wake_secs, 520 new_measure_wake, new_comms_wake, new_wake_secs);
526 new_fridge_setpoint / 10.0, new_fridge_difference / 10.0, 521 }
527 new_fridge_delay 522 }
528 ); 523
529 524 // returns true if eeprom was written
530 // fridge parameters can safely be updated immediately, and avoids 525 static bool
531 // resetting the delay with a reset. 526 set_initial_eeprom()
532 fridge_setpoint = new_fridge_setpoint; 527 {
533 fridge_difference = new_fridge_difference; 528 uint16_t magic;
534 fridge_delay = new_fridge_delay; 529 eeprom_read(magic, magic);
530 if (magic == EXPECT_MAGIC)
531 {
532 return false;
533 }
534
535 ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
536 {
537 eeprom_write(measure_wake, measure_wake);
538 eeprom_write(comms_wake, comms_wake);
539 eeprom_write(wake_secs, wake_secs);
540 eeprom_write(fridge_setpoint, fridge_setpoint);
541 eeprom_write(fridge_difference, fridge_difference);
542 eeprom_write(fridge_delay, fridge_delay);
543 magic = EXPECT_MAGIC;
544 eeprom_write(magic, magic);
545 }
546
547 return true;
548 }
549
550 static void
551 cmd_set_fridge_setpoint(char *params)
552 {
553 float new_f = atof(params);
554 if (new_f < 2 || new_f > 30)
555 {
556 printf_P(PSTR("Bad fridge value %f\n"), new_f);
557 return;
558 }
559
560 fridge_setpoint = new_f * 10;
561 bool written = set_initial_eeprom();
562 if (!written)
563 {
564 ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
565 {
566 eeprom_write(fridge_setpoint, fridge_setpoint);
567 }
568 }
569 }
570
571 static void
572 cmd_set_fridge_difference(char *params)
573 {
574 float new_f = atof(params);
575 if (new_f < 0 || new_f > 30)
576 {
577 printf_P(PSTR("Bad fridge value %f\n"), new_f);
578 return;
579 }
580
581 fridge_difference = new_f * 10;
582 bool written = set_initial_eeprom();
583 if (!written)
584 {
585 ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
586 {
587 eeprom_write(fridge_difference, fridge_difference);
588 }
589 }
590 }
591
592 static void
593 cmd_set_fridge_delay(char *params)
594 {
595 uint16_t new_delay = atoi(params);
596 if (new_delay < 5)
597 {
598 printf_P(PSTR("Bad fridge delay %d\n"), new_delay);
599 return;
600 }
601
602 fridge_delay = new_delay;
603 bool written = set_initial_eeprom();
604 if (!written)
605 {
606 ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
607 {
608 eeprom_write(fridge_delay, fridge_delay);
609 }
535 } 610 }
536 } 611 }
537 612
538 static void 613 static void
539 cmd_awake() 614 cmd_awake()
574 cmd_set_params(&readbuf[11]); 649 cmd_set_params(&readbuf[11]);
575 } 650 }
576 else if (strcmp_P(readbuf, PSTR("awake")) == 0) 651 else if (strcmp_P(readbuf, PSTR("awake")) == 0)
577 { 652 {
578 cmd_awake(); 653 cmd_awake();
654 }
655 else if (strncmp_P(readbuf, PSTR("fridge_setpoint "), 16) == 0)
656 {
657 cmd_set_fridge_setpoint(&readbuf[16]);
658 }
659 else if (strncmp_P(readbuf, PSTR("fridge_diff "), 12) == 0)
660 {
661 cmd_set_fridge_difference(&readbuf[12]);
662 }
663 else if (strncmp_P(readbuf, PSTR("fridge_delay "), 13) == 0)
664 {
665 cmd_set_fridge_delay(&readbuf[13]);
579 } 666 }
580 else if (strcmp_P(readbuf, PSTR("reset")) == 0) 667 else if (strcmp_P(readbuf, PSTR("reset")) == 0)
581 { 668 {
582 cmd_reset(); 669 cmd_reset();
583 } 670 }