annotate water.c @ 11:13903d8343a6 default tip

Catch up properly with settings change.
author Daniel O'Connor <darius@dons.net.au>
date Sun, 15 Feb 2015 22:33:40 +1030
parents c32b5792683a
children
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;
8
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
68 uint8_t relays;
3
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);
8
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
87 static void water_addjob(uint8_t relays, uint16_t delay, uint16_t time);
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
88 static void water_deljob(int job);
3
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
89 static void water_printjobs(void);
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
90
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
91 void
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
92 water_init(void) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
93 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
94
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
95 water_load_or_init_settings();
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
96
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
97 /* Setup timer */
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
98 /* 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
99
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
100 /* 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
101 TCCR2A = _BV(WGM21);
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
102 TCCR2B = _BV(CS22) | _BV(CS21);
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
103
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
104 /* Compare with ... */
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
105 OCR2A = 250;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
106
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
107 /* Enable interrupt for match on A */
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
108 TIMSK2 = _BV(OCIE2A);
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
109
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
110 nowtm.sec = 0;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
111 nowtm.usec = 0;
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 /*
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
115 * Timer 2 Compare IRQ
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 * Update time counter
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
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
120 ISR(TIMER2_COMPA_vect) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
121 nowtm.usec += 8000;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
122 while (nowtm.usec > 1000000) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
123 nowtm.usec -= 1000000;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
124 nowtm.sec++;
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
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
5
951277329ee6 Use splitargv instead of rolling our own.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
130 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
131 char *e;
8
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
132 uint16_t delay, time;
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
133 uint8_t relays;
3
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
134 long tmp;
8
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
135 int region;
3
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
136
8
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
137 if (!strcasecmp_P(argv[0], PSTR("add"))) {
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
138 if (argc != 4) {
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
139 puts_P(PSTR("Wrong number of arguments, usage: wa add region delay time\r\n"));
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
140 return;
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
141 }
3
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
142
8
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
143 relays = 0;
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
144 if (!strcasecmp_P(argv[1], PSTR("all"))) {
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
145 /* all => Or everything together */
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
146 for (region = 0; region < sizeof(regions) / sizeof(regions[0]); region++)
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
147 relays |= 1 << regions[region].relay;
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
148 } else {
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
149 /* find if it's a valid region */
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
150 for (region = 0; region < sizeof(regions) / sizeof(regions[0]); region++)
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
151 if (!strcasecmp(argv[1], regions[region].name))
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
152 relays |= 1 << regions[region].relay;
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
153 if (relays == 0) {
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
154 puts_P(PSTR("Unknown region\r\n"));
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
155 return;
3
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
156 }
8
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
157 }
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
158 tmp = strtol(argv[2], &e, 10);
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
159 if (e == argv[2]) {
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
160 puts_P(PSTR("Unable to parse delay\r\n"));
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
161 return;
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
162 }
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
163 if (tmp < 0 || tmp > 65535) {
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
164 puts_P(PSTR("delay out of range, must be 0 - 65535\r\n"));
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
165 return;
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
166 }
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
167 delay = tmp;
3
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
168
8
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
169 tmp = strtol(argv[3], &e, 10);
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
170 if (e == argv[3]) {
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
171 puts_P(PSTR("Unable to parse time\r\n"));
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
172 return;
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
173 }
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
174 if (tmp < 1 || tmp > 65535) {
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
175 puts_P(PSTR("time out of range, must be 1 - 65535\r\n"));
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
176 return;
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
177 }
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
178 time = tmp;
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
179
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
180 water_addjob(relays, delay, time);
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
181
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
182 water_printjobs();
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
183 } else if (!strcasecmp_P(argv[0], PSTR("del"))) {
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
184 if (argc != 2) {
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
185 puts_P(PSTR("Wrong number of arguments, usage: wa del job\r\n"));
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
186 return;
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
187 }
3
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
188
8
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
189 tmp = strtol(argv[1], &e, 10);
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
190 if (e == argv[1]) {
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
191 puts_P(PSTR("Unable to parse job\r\n"));
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
192 return;
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
193 }
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
194 water_deljob(tmp);
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
195 } else if (!strcasecmp_P(argv[0], PSTR("pr"))) {
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
196 if (argc != 1) {
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
197 puts_P(PSTR("Wrong number of arguments, usage: wa pr\r\n"));
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
198 return;
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
199 }
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
200 water_printjobs();
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
201 } else if (!strcasecmp_P(argv[0], PSTR("regions"))) {
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
202 int i;
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
203 if (argc != 1) {
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
204 puts_P(PSTR("Wrong number of arguments, usage: wa regions\r\n"));
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
205 return;
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
206 }
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
207 printf_P(PSTR("%-20S %S\r\n"), PSTR("Region"), PSTR("Relay"));
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
208 for (i = 0; i < sizeof(regions) / sizeof(regions[0]); i++)
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
209 printf_P(PSTR("%-20s %d\r\n"), regions[i].name, regions[i].relay);
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
210 } else if (!strcasecmp_P(argv[0], PSTR("help"))) {
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
211 puts_P(PSTR(
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
212 "wa help This help\r\n"
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
213 "wa add region dly time Water region (or all) for time minutes after dly minutes\r\n"
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
214 "wa del job Delete job\r\n"
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
215 "wa pr Print list of jobs\r\n"
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
216 "wa regions Print list of regions\r\n"
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
217 ));
3
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
218 } else {
8
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
219 puts_P(PSTR("Unknown 'wa' sub-command. 'wa help' for help\r\n"));
3
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
220 return;
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 static void
8
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
225 water_addjob(uint8_t relays, uint16_t delay, uint16_t time) {
3
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
226 int i;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
227 time_t now;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
228
8
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
229 printf_P(PSTR("adding delays 0x%02x, delay %d, time %d\r\n"), relays, delay, time);
3
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
230
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
231 now = ds1307_time();
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
232 if (now == -1) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
233 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
234 return;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
235 }
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
236
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
237 /* Find an open job slot */
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
238 for (i = 0; i < NJOBS; i++) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
239 if (settings.jobs[i].valid == 0)
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
240 break;
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 if (i == NJOBS) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
244 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
245 return;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
246 }
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 settings.jobs[i].start = now + delay * 60;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
249 settings.jobs[i].length = time * 60;
8
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
250 settings.jobs[i].relays = relays;
3
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
251 settings.jobs[i].valid = 1;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
252
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
253 water_write_settings();
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
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
256 static void
8
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
257 water_deljob(int job) {
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
258 if (job < 0 || job >= NJOBS) {
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
259 puts_P(PSTR("Invalid job number\r\n"));
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
260 return;
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
261 }
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
262
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
263 if (settings.jobs[job].valid == 0) {
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
264 puts_P(PSTR("Job already deleted\r\n"));
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
265 return;
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
266 }
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
267
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
268 settings.jobs[job].valid = 0;
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
269 printf_P(PSTR("Removed job %d\r\n"), job);
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
270
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
271 water_write_settings();
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
272 }
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
273
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
274 static void
3
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
275 water_printjobs(void) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
276 int i;
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 for (i = 0; i < NJOBS; i++) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
279 printf_P(PSTR("%d: "), i);
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
280 if (settings.jobs[i].valid) {
8
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
281 printf_P(PSTR("relays 0x%02x start %ld length %d\r\n"), settings.jobs[i].relays,
3
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
282 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
283 } else {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
284 printf_P(PSTR("invalid\r\n"));
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
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
289 /* Update water related state machine */
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
290 void
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
291 water_update(void) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
292 static time_t lastcheck = 0;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
293 int i;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
294 uint8_t relays;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
295 static int16_t lastrelays = -1;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
296 time_t now;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
297 static uint8_t logged[NJOBS] = {0};
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 if (lastcheck + 5 > nowtm.sec)
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
300 return;
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 lastcheck = nowtm.sec;
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 now = ds1307_time();
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
305 if (now == -1) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
306 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
307 return;
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
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
310 relays = 0;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
311
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
312 for (i = 0; i < NJOBS; i++) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
313 if (settings.jobs[i].valid) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
314 if (now > settings.jobs[i].start + settings.jobs[i].length) {
8
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
315 printf_P(PSTR("Marking job %d (relays 0x%02x) done\r\n"), i, settings.jobs[i].relays);
3
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
316 settings.jobs[i].valid = 0;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
317 logged[i] = 0;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
318 continue;
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
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
321 if (now > settings.jobs[i].start) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
322 if (logged[i] == 0) {
8
c32b5792683a Rework code to allow multiple relays to be set per job.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
323 printf_P(PSTR("Job %d (relays 0x%02x) running\r\n"), i, settings.jobs[i].relays);
3
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
324 logged[i] = 1;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
325 }
11
13903d8343a6 Catch up properly with settings change.
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
326 relays |= settings.jobs[i].relays;
3
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
327 continue;
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 }
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
330 }
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
331
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
332 /* Update EEPROM with jobs */
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
333 water_write_settings();
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
334
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
335 if (lastrelays == -1 || lastrelays != relays) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
336 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
337 lastrelays = relays;
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 PORTB = (PORTB & ~RELAY_MASK) | relays;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
341 }
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
342
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
343 /* Read the settings from EEPROM
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
344 * 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
345 */
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
346 static void
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
347 water_load_or_init_settings(void) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
348 uint8_t *dptr;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
349 uint16_t crc, strcrc;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
350 int i;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
351
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
352 puts_P(PSTR("Loading settings\r\n"));
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
353
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 eeprom_busy_wait();
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
356 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
357 strcrc = eeprom_read_word(&ee_area.crc);
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
358
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
359 dptr = (uint8_t *)&settings;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
360
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
361 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
362 crc = _crc16_update(crc, dptr[i]);
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
363
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
364 /* All OK? */
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
365 if (crc == strcrc)
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
366 return;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
367
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
368 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
369 water_default_settings();
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
370 water_write_settings();
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
371 }
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
372
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
373 /* Set defaults */
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
374 static void
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
375 water_default_settings(void) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
376 int i;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
377
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
378 puts_P(PSTR("Defaulting settings\r\n"));
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
379
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
380 for (i = 0; i < NJOBS; i++)
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
381 settings.jobs[i].valid = 0;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
382 }
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
383
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
384 /* Write the current settings out to EEPROM */
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
385 static void
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
386 water_write_settings(void) {
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
387 uint16_t crc;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
388 uint8_t *dptr;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
389 int i;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
390
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
391 //puts_P(PSTR("Saving settings\r\n"));
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
392
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
393 eeprom_busy_wait();
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
394 /* Use update variant to save EEPROM cycles */
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
395 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
396
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
397 dptr = (uint8_t *)&settings;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
398 crc = 0;
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
399 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
400 crc = _crc16_update(crc, dptr[i]);
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
401
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
402 eeprom_update_word(&ee_area.crc, crc);
e75ecd162da3 Add watering logic ressurected from 2013.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
403 }