annotate water.c @ 5:951277329ee6

Use splitargv instead of rolling our own. Fix WDT tripping continuously on self reset (disable WDT on startup). Pretty up startup message.
author Daniel O'Connor <darius@dons.net.au>
date Sun, 15 Feb 2015 16:16:54 +1030
parents e75ecd162da3
children c32b5792683a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1 /*
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2 * Watering logic
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
3 *
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
4 * Copyright (c) 2015
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
5 * Daniel O'Connor <darius@dons.net.au>. All rights reserved.
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
6 *
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
7 * Redistribution and use in source and binary forms, with or without
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
8 * modification, are permitted provided that the following conditions
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
9 * are met:
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
10 * 1. Redistributions of source code must retain the above copyright
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
11 * notice, this list of conditions and the following disclaimer.
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
15 *
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
26 * SUCH DAMAGE.
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
27 */
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
28
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
29 #include <stdio.h>
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
30 #include <stdlib.h>
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
31 #include <string.h>
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
32 #include <avr/io.h>
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
33 #include <avr/interrupt.h>
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
34 #include <avr/pgmspace.h>
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
35 #include <avr/eeprom.h>
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
36 #include <util/crc16.h>
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
37
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
38 #include "ds1307.h"
5
951277329ee6 Use splitargv instead of rolling our own.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
39 #include "splitargv.h"
3
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
40 #include "water.h"
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
41
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
42 struct tm {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
43 int32_t sec;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
44 int32_t usec;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
45 };
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
46
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
47 volatile struct tm nowtm;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
48
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
49 #define RELAY_MASK 0xf0
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
50
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
51 typedef struct {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
52 char *name;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
53 uint8_t relay;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
54 } region_t;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
55
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
56 static region_t regions[] = {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
57 { "front", 4 },
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
58 { "beds", 5 },
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
59 // { "side", 6 },
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
60 };
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
61
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
62 #define NJOBS 6
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
63
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
64 typedef struct {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
65 uint8_t valid;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
66 uint32_t start;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
67 uint16_t length;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
68 uint8_t region;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
69 } job_t;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
70
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
71 typedef struct {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
72 job_t jobs[NJOBS];
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
73 } settings_t;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
74
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
75 /* Current settings in RAM */
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
76 static settings_t settings;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
77 /* EEPROM copy of settings */
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
78 struct {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
79 settings_t settings;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
80 uint16_t crc;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
81 } ee_area __attribute__((section(".eeprom")));
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
82
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
83 /* Local function prototypes */
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
84 static void water_default_settings(void);
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
85 static void water_write_settings(void);
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
86 static void water_load_or_init_settings(void);
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
87 static void water_addjob(int region, uint16_t delay, uint16_t time);
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
88 static void water_printjobs(void);
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
89
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
90 void
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
91 water_init(void) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
92 puts_P(PSTR("Initing water stuff\r\n"));
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
93
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
94 water_load_or_init_settings();
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
95
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
96 /* Setup timer */
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
97 /* 8Mhz / 256 = 31250 Hz / 250 = 125 Hz = IRQ every 8 ms */
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
98
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
99 /* CTC mode, no output on pin, Divide clock by 256 */
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
100 TCCR2A = _BV(WGM21);
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
101 TCCR2B = _BV(CS22) | _BV(CS21);
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
102
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
103 /* Compare with ... */
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
104 OCR2A = 250;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
105
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
106 /* Enable interrupt for match on A */
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
107 TIMSK2 = _BV(OCIE2A);
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
108
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
109 nowtm.sec = 0;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
110 nowtm.usec = 0;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
111 }
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
112
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
113 /*
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
114 * Timer 2 Compare IRQ
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
115 *
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
116 * Update time counter
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
117 */
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
118
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
119 ISR(TIMER2_COMPA_vect) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
120 nowtm.usec += 8000;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
121 while (nowtm.usec > 1000000) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
122 nowtm.usec -= 1000000;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
123 nowtm.sec++;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
124 }
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
125 }
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
126
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
127 /* Parse water related command */
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
128 void
5
951277329ee6 Use splitargv instead of rolling our own.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
129 water_cmd(int argc, char **argv) {
951277329ee6 Use splitargv instead of rolling our own.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
130 char *e;
3
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
131 uint16_t delay;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
132 uint16_t time;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
133 long tmp;
5
951277329ee6 Use splitargv instead of rolling our own.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
134 int i;
3
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
135
5
951277329ee6 Use splitargv instead of rolling our own.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
136 if (!strcmp_P(argv[0], PSTR("all"))) {
3
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
137 if (argc != 3) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
138 puts_P(PSTR("Wrong number of arguments, usage: wa all delay time\r\n"));
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
139 return;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
140 }
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
141
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
142 tmp = strtol(argv[1], &e, 10);
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
143 if (e == argv[1]) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
144 puts_P(PSTR("Unable to parse delay\r\n"));
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
145 return;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
146 }
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
147 if (tmp < 0 || tmp > 65535) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
148 puts_P(PSTR("delay out of range, must be 0 - 65535\r\n"));
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
149 return;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
150 }
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
151 delay = tmp;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
152
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
153 tmp = strtol(argv[2], &e, 10);
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
154 if (e == argv[2]) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
155 puts_P(PSTR("Unable to parse time\r\n"));
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
156 return;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
157 }
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
158 if (tmp < 1 || tmp > 65535) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
159 puts_P(PSTR("time out of range, must be 1 - 65535\r\n"));
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
160 return;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
161 }
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
162 time = tmp;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
163
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
164 water_printjobs();
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
165
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
166 /* add for each region one after the other */
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
167 for (i = 0; i < sizeof(regions) / sizeof(regions[0]); i++) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
168 water_addjob(i, delay + time * i, time);
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
169 }
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
170
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
171 water_printjobs();
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
172 } else {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
173 puts_P(PSTR("Unknown 'wa' sub-command\r\n"));
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
174 return;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
175 }
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
176 }
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
177
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
178 static void
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
179 water_addjob(int region, uint16_t delay, uint16_t time) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
180 int i;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
181 time_t now;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
182
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
183 printf_P(PSTR("adding region %d, delay %d, time %d\r\n"), region, delay, time);
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
184
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
185 now = ds1307_time();
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
186 if (now == -1) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
187 puts_P(PSTR("Unable to read time\r\n"));
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
188 return;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
189 }
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
190
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
191 /* Find an open job slot */
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
192 for (i = 0; i < NJOBS; i++) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
193 if (settings.jobs[i].valid == 0)
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
194 break;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
195 }
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
196
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
197 if (i == NJOBS) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
198 puts_P(PSTR("Couldn't find free slot\r\n"));
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
199 return;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
200 }
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
201
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
202 settings.jobs[i].start = now + delay * 60;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
203 settings.jobs[i].length = time * 60;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
204 settings.jobs[i].region = region;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
205 settings.jobs[i].valid = 1;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
206
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
207 water_write_settings();
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
208 }
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
209
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
210 static void
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
211 water_printjobs(void) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
212 int i;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
213
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
214 for (i = 0; i < NJOBS; i++) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
215 printf_P(PSTR("%d: "), i);
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
216 if (settings.jobs[i].valid) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
217 printf_P(PSTR("region %d (%s) start %ld length %d\r\n"), settings.jobs[i].region, regions[settings.jobs[i].region].name,
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
218 settings.jobs[i].start, settings.jobs[i].length);
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
219 } else {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
220 printf_P(PSTR("invalid\r\n"));
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
221 }
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
222 }
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
223 }
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
224
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
225 /* Update water related state machine */
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
226 void
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
227 water_update(void) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
228 static time_t lastcheck = 0;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
229 int i;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
230 uint8_t relays;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
231 static int16_t lastrelays = -1;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
232 time_t now;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
233 static uint8_t logged[NJOBS] = {0};
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
234
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
235 if (lastcheck + 5 > nowtm.sec)
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
236 return;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
237
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
238 lastcheck = nowtm.sec;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
239
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
240 now = ds1307_time();
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
241 if (now == -1) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
242 puts_P(PSTR("Unable to read time\r\n"));
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
243 return;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
244 }
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
245
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
246 relays = 0;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
247
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
248 for (i = 0; i < NJOBS; i++) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
249 if (settings.jobs[i].valid) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
250 if (now > settings.jobs[i].start + settings.jobs[i].length) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
251 printf_P(PSTR("Marking job %d (%s) done\r\n"), i, regions[settings.jobs[i].region].name);
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
252 settings.jobs[i].valid = 0;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
253 logged[i] = 0;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
254 continue;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
255 }
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
256
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
257 if (now > settings.jobs[i].start) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
258 if (logged[i] == 0) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
259 printf_P(PSTR("Job %d (%s) running\r\n"), i, regions[settings.jobs[i].region].name);
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
260 logged[i] = 1;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
261 }
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
262 relays |= 1 << regions[settings.jobs[i].region].relay;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
263 continue;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
264 }
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
265 }
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
266 }
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
267
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
268 /* Update EEPROM with jobs */
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
269 water_write_settings();
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
270
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
271 if (lastrelays == -1 || lastrelays != relays) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
272 printf_P(PSTR("[%ld] Setting relays to 0x%02x\r\n"), now, relays);
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
273 lastrelays = relays;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
274 }
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
275
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
276 PORTB = (PORTB & ~RELAY_MASK) | relays;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
277 }
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
278
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
279 /* Read the settings from EEPROM
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
280 * If the CRC fails then reload from flash
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
281 */
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
282 static void
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
283 water_load_or_init_settings(void) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
284 uint8_t *dptr;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
285 uint16_t crc, strcrc;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
286 int i;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
287
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
288 puts_P(PSTR("Loading settings\r\n"));
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
289
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
290 crc = 0;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
291 eeprom_busy_wait();
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
292 eeprom_read_block(&settings, &ee_area.settings, sizeof(settings_t));
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
293 strcrc = eeprom_read_word(&ee_area.crc);
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
294
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
295 dptr = (uint8_t *)&settings;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
296
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
297 for (i = 0; i < sizeof(settings_t); i++)
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
298 crc = _crc16_update(crc, dptr[i]);
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
299
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
300 /* All OK? */
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
301 if (crc == strcrc)
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
302 return;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
303
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
304 printf_P(PSTR("CRC mismatch got 0x%04x vs 0x%04x, setting defaults\r\n"), crc, strcrc);
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
305 water_default_settings();
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
306 water_write_settings();
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
307 }
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
308
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
309 /* Set defaults */
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
310 static void
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
311 water_default_settings(void) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
312 int i;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
313
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
314 puts_P(PSTR("Defaulting settings\r\n"));
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
315
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
316 for (i = 0; i < NJOBS; i++)
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
317 settings.jobs[i].valid = 0;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
318 }
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
319
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
320 /* Write the current settings out to EEPROM */
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
321 static void
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
322 water_write_settings(void) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
323 uint16_t crc;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
324 uint8_t *dptr;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
325 int i;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
326
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
327 //puts_P(PSTR("Saving settings\r\n"));
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
328
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
329 eeprom_busy_wait();
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
330 /* Use update variant to save EEPROM cycles */
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
331 eeprom_update_block(&settings, &ee_area.settings, sizeof(settings_t));
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
332
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
333 dptr = (uint8_t *)&settings;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
334 crc = 0;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
335 for (i = 0; i < sizeof(settings_t); i++)
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
336 crc = _crc16_update(crc, dptr[i]);
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
337
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
338 eeprom_update_word(&ee_area.crc, crc);
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
339 }