annotate water.c @ 3:e75ecd162da3

Add watering logic ressurected from 2013. Handles basic "water all regions for X minutes in X minutes time" Tolerates power failures.
author Daniel O'Connor <darius@dons.net.au>
date Tue, 27 Jan 2015 23:40:01 +1030
parents
children 951277329ee6
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"
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
39 #include "water.h"
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
40
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
41 struct tm {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
42 int32_t sec;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
43 int32_t usec;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
44 };
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 volatile struct tm nowtm;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
47
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
48 #define RELAY_MASK 0xf0
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
49
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
50 typedef struct {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
51 char *name;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
52 uint8_t relay;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
53 } region_t;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
54
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
55 static region_t regions[] = {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
56 { "front", 4 },
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
57 { "beds", 5 },
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
58 // { "side", 6 },
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
59 };
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 #define NJOBS 6
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
62
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
63 typedef struct {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
64 uint8_t valid;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
65 uint32_t start;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
66 uint16_t length;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
67 uint8_t region;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
68 } job_t;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
69
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
70 typedef struct {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
71 job_t jobs[NJOBS];
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
72 } settings_t;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
73
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
74 /* Current settings in RAM */
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
75 static settings_t settings;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
76 /* EEPROM copy of settings */
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
77 struct {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
78 settings_t settings;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
79 uint16_t crc;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
80 } ee_area __attribute__((section(".eeprom")));
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
81
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
82 /* Local function prototypes */
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
83 static void water_default_settings(void);
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
84 static void water_write_settings(void);
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
85 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
86 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
87 static void water_printjobs(void);
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
88
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
89 void
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
90 water_init(void) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
91 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
92
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
93 water_load_or_init_settings();
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
94
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
95 /* Setup timer */
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
96 /* 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
97
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
98 /* 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
99 TCCR2A = _BV(WGM21);
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
100 TCCR2B = _BV(CS22) | _BV(CS21);
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
101
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
102 /* Compare with ... */
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
103 OCR2A = 250;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
104
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
105 /* Enable interrupt for match on A */
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
106 TIMSK2 = _BV(OCIE2A);
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
107
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
108 nowtm.sec = 0;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
109 nowtm.usec = 0;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
110 }
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 * Timer 2 Compare IRQ
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
114 *
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
115 * Update time counter
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
116 */
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 ISR(TIMER2_COMPA_vect) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
119 nowtm.usec += 8000;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
120 while (nowtm.usec > 1000000) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
121 nowtm.usec -= 1000000;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
122 nowtm.sec++;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
123 }
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 #define MAXARGS 10
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
127
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
128 /* Parse water related command */
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
129 void
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
130 water_cmd(char *buf) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
131 char *e, **btmp;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
132 uint16_t delay;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
133 uint16_t time;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
134 long tmp;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
135 int i, argc, skip;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
136 char *argv[MAXARGS];
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
137
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
138 /* Split command string on space/tab boundaries into argv/argc */
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
139 argc = 0;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
140 btmp = &buf;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
141 skip = 1;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
142 while (1) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
143 argv[argc] = strsep(btmp, " \t");
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
144 if (argv[argc] == '\0')
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
145 break;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
146 /* Skip first arg (ie 'wa') */
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
147 if (skip) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
148 skip = 0;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
149 continue;
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 argc++;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
152 if (argc == MAXARGS)
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
153 break;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
154 }
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
155
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
156 if (!strcmp(argv[0], "all")) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
157 if (argc != 3) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
158 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
159 return;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
160 }
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 tmp = strtol(argv[1], &e, 10);
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
163 if (e == argv[1]) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
164 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
165 return;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
166 }
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
167 if (tmp < 0 || tmp > 65535) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
168 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
169 return;
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 delay = tmp;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
172
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
173 tmp = strtol(argv[2], &e, 10);
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
174 if (e == argv[2]) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
175 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
176 return;
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 if (tmp < 1 || tmp > 65535) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
179 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
180 return;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
181 }
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
182 time = tmp;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
183
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
184 water_printjobs();
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
185
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
186 /* 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
187 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
188 water_addjob(i, delay + time * i, time);
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 water_printjobs();
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
192 } else {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
193 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
194 return;
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
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
198 static void
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
199 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
200 int i;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
201 time_t now;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
202
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
203 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
204
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
205 now = ds1307_time();
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
206 if (now == -1) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
207 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
208 return;
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
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
211 /* Find an open job slot */
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
212 for (i = 0; i < NJOBS; i++) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
213 if (settings.jobs[i].valid == 0)
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
214 break;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
215 }
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
216
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
217 if (i == NJOBS) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
218 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
219 return;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
220 }
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 settings.jobs[i].start = now + delay * 60;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
223 settings.jobs[i].length = time * 60;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
224 settings.jobs[i].region = region;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
225 settings.jobs[i].valid = 1;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
226
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
227 water_write_settings();
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
228 }
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
229
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
230 static void
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
231 water_printjobs(void) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
232 int i;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
233
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
234 for (i = 0; i < NJOBS; i++) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
235 printf_P(PSTR("%d: "), i);
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
236 if (settings.jobs[i].valid) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
237 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
238 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
239 } else {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
240 printf_P(PSTR("invalid\r\n"));
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
241 }
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
242 }
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
243 }
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 /* Update water related state machine */
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
246 void
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
247 water_update(void) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
248 static time_t lastcheck = 0;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
249 int i;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
250 uint8_t relays;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
251 static int16_t lastrelays = -1;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
252 time_t now;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
253 static uint8_t logged[NJOBS] = {0};
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
254
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
255 if (lastcheck + 5 > nowtm.sec)
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
256 return;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
257
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
258 lastcheck = nowtm.sec;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
259
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
260 now = ds1307_time();
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
261 if (now == -1) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
262 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
263 return;
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 relays = 0;
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 for (i = 0; i < NJOBS; i++) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
269 if (settings.jobs[i].valid) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
270 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
271 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
272 settings.jobs[i].valid = 0;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
273 logged[i] = 0;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
274 continue;
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
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
277 if (now > settings.jobs[i].start) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
278 if (logged[i] == 0) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
279 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
280 logged[i] = 1;
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 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
283 continue;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
284 }
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
285 }
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
286 }
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 /* Update EEPROM with jobs */
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
289 water_write_settings();
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
290
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
291 if (lastrelays == -1 || lastrelays != relays) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
292 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
293 lastrelays = relays;
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
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
296 PORTB = (PORTB & ~RELAY_MASK) | relays;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
297 }
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
298
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
299 /* Read the settings from EEPROM
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
300 * 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
301 */
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
302 static void
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
303 water_load_or_init_settings(void) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
304 uint8_t *dptr;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
305 uint16_t crc, strcrc;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
306 int i;
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 puts_P(PSTR("Loading settings\r\n"));
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
309
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
310 crc = 0;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
311 eeprom_busy_wait();
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
312 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
313 strcrc = eeprom_read_word(&ee_area.crc);
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
314
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
315 dptr = (uint8_t *)&settings;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
316
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
317 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
318 crc = _crc16_update(crc, dptr[i]);
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 /* All OK? */
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
321 if (crc == strcrc)
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
322 return;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
323
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
324 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
325 water_default_settings();
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
326 water_write_settings();
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
327 }
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 /* Set defaults */
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
330 static void
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
331 water_default_settings(void) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
332 int i;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
333
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
334 puts_P(PSTR("Defaulting settings\r\n"));
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
335
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
336 for (i = 0; i < NJOBS; i++)
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
337 settings.jobs[i].valid = 0;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
338 }
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
339
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
340 /* Write the current settings out to EEPROM */
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
341 static void
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
342 water_write_settings(void) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
343 uint16_t crc;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
344 uint8_t *dptr;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
345 int i;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
346
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
347 //puts_P(PSTR("Saving settings\r\n"));
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
348
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
349 eeprom_busy_wait();
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
350 /* Use update variant to save EEPROM cycles */
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
351 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
352
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
353 dptr = (uint8_t *)&settings;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
354 crc = 0;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
355 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
356 crc = _crc16_update(crc, dptr[i]);
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
357
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
358 eeprom_update_word(&ee_area.crc, crc);
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
359 }