comparison water.c @ 8:c32b5792683a

Rework code to allow multiple relays to be set per job.
author Daniel O'Connor <darius@dons.net.au>
date Sun, 15 Feb 2015 17:47:55 +1030
parents 951277329ee6
children 13903d8343a6
comparison
equal deleted inserted replaced
7:78983502a4e9 8:c32b5792683a
63 63
64 typedef struct { 64 typedef struct {
65 uint8_t valid; 65 uint8_t valid;
66 uint32_t start; 66 uint32_t start;
67 uint16_t length; 67 uint16_t length;
68 uint8_t region; 68 uint8_t relays;
69 } job_t; 69 } job_t;
70 70
71 typedef struct { 71 typedef struct {
72 job_t jobs[NJOBS]; 72 job_t jobs[NJOBS];
73 } settings_t; 73 } settings_t;
82 82
83 /* Local function prototypes */ 83 /* Local function prototypes */
84 static void water_default_settings(void); 84 static void water_default_settings(void);
85 static void water_write_settings(void); 85 static void water_write_settings(void);
86 static void water_load_or_init_settings(void); 86 static void water_load_or_init_settings(void);
87 static void water_addjob(int region, uint16_t delay, uint16_t time); 87 static void water_addjob(uint8_t relays, uint16_t delay, uint16_t time);
88 static void water_deljob(int job);
88 static void water_printjobs(void); 89 static void water_printjobs(void);
89 90
90 void 91 void
91 water_init(void) { 92 water_init(void) {
92 puts_P(PSTR("Initing water stuff\r\n")); 93 puts_P(PSTR("Initing water stuff\r\n"));
126 127
127 /* Parse water related command */ 128 /* Parse water related command */
128 void 129 void
129 water_cmd(int argc, char **argv) { 130 water_cmd(int argc, char **argv) {
130 char *e; 131 char *e;
131 uint16_t delay; 132 uint16_t delay, time;
132 uint16_t time; 133 uint8_t relays;
133 long tmp; 134 long tmp;
134 int i; 135 int region;
135 136
136 if (!strcmp_P(argv[0], PSTR("all"))) { 137 if (!strcasecmp_P(argv[0], PSTR("add"))) {
137 if (argc != 3) { 138 if (argc != 4) {
138 puts_P(PSTR("Wrong number of arguments, usage: wa all delay time\r\n")); 139 puts_P(PSTR("Wrong number of arguments, usage: wa add region delay time\r\n"));
139 return; 140 return;
141 }
142
143 relays = 0;
144 if (!strcasecmp_P(argv[1], PSTR("all"))) {
145 /* all => Or everything together */
146 for (region = 0; region < sizeof(regions) / sizeof(regions[0]); region++)
147 relays |= 1 << regions[region].relay;
148 } else {
149 /* find if it's a valid region */
150 for (region = 0; region < sizeof(regions) / sizeof(regions[0]); region++)
151 if (!strcasecmp(argv[1], regions[region].name))
152 relays |= 1 << regions[region].relay;
153 if (relays == 0) {
154 puts_P(PSTR("Unknown region\r\n"));
155 return;
140 } 156 }
141 157 }
142 tmp = strtol(argv[1], &e, 10); 158 tmp = strtol(argv[2], &e, 10);
143 if (e == argv[1]) { 159 if (e == argv[2]) {
144 puts_P(PSTR("Unable to parse delay\r\n")); 160 puts_P(PSTR("Unable to parse delay\r\n"));
145 return; 161 return;
146 } 162 }
147 if (tmp < 0 || tmp > 65535) { 163 if (tmp < 0 || tmp > 65535) {
148 puts_P(PSTR("delay out of range, must be 0 - 65535\r\n")); 164 puts_P(PSTR("delay out of range, must be 0 - 65535\r\n"));
149 return; 165 return;
150 } 166 }
151 delay = tmp; 167 delay = tmp;
152 168
153 tmp = strtol(argv[2], &e, 10); 169 tmp = strtol(argv[3], &e, 10);
154 if (e == argv[2]) { 170 if (e == argv[3]) {
155 puts_P(PSTR("Unable to parse time\r\n")); 171 puts_P(PSTR("Unable to parse time\r\n"));
156 return; 172 return;
157 } 173 }
158 if (tmp < 1 || tmp > 65535) { 174 if (tmp < 1 || tmp > 65535) {
159 puts_P(PSTR("time out of range, must be 1 - 65535\r\n")); 175 puts_P(PSTR("time out of range, must be 1 - 65535\r\n"));
160 return; 176 return;
161 } 177 }
162 time = tmp; 178 time = tmp;
163 179
164 water_printjobs(); 180 water_addjob(relays, delay, time);
165 181
166 /* add for each region one after the other */ 182 water_printjobs();
167 for (i = 0; i < sizeof(regions) / sizeof(regions[0]); i++) { 183 } else if (!strcasecmp_P(argv[0], PSTR("del"))) {
168 water_addjob(i, delay + time * i, time); 184 if (argc != 2) {
169 } 185 puts_P(PSTR("Wrong number of arguments, usage: wa del job\r\n"));
170 186 return;
171 water_printjobs(); 187 }
188
189 tmp = strtol(argv[1], &e, 10);
190 if (e == argv[1]) {
191 puts_P(PSTR("Unable to parse job\r\n"));
192 return;
193 }
194 water_deljob(tmp);
195 } else if (!strcasecmp_P(argv[0], PSTR("pr"))) {
196 if (argc != 1) {
197 puts_P(PSTR("Wrong number of arguments, usage: wa pr\r\n"));
198 return;
199 }
200 water_printjobs();
201 } else if (!strcasecmp_P(argv[0], PSTR("regions"))) {
202 int i;
203 if (argc != 1) {
204 puts_P(PSTR("Wrong number of arguments, usage: wa regions\r\n"));
205 return;
206 }
207 printf_P(PSTR("%-20S %S\r\n"), PSTR("Region"), PSTR("Relay"));
208 for (i = 0; i < sizeof(regions) / sizeof(regions[0]); i++)
209 printf_P(PSTR("%-20s %d\r\n"), regions[i].name, regions[i].relay);
210 } else if (!strcasecmp_P(argv[0], PSTR("help"))) {
211 puts_P(PSTR(
212 "wa help This help\r\n"
213 "wa add region dly time Water region (or all) for time minutes after dly minutes\r\n"
214 "wa del job Delete job\r\n"
215 "wa pr Print list of jobs\r\n"
216 "wa regions Print list of regions\r\n"
217 ));
172 } else { 218 } else {
173 puts_P(PSTR("Unknown 'wa' sub-command\r\n")); 219 puts_P(PSTR("Unknown 'wa' sub-command. 'wa help' for help\r\n"));
174 return; 220 return;
175 } 221 }
176 } 222 }
177 223
178 static void 224 static void
179 water_addjob(int region, uint16_t delay, uint16_t time) { 225 water_addjob(uint8_t relays, uint16_t delay, uint16_t time) {
180 int i; 226 int i;
181 time_t now; 227 time_t now;
182 228
183 printf_P(PSTR("adding region %d, delay %d, time %d\r\n"), region, delay, time); 229 printf_P(PSTR("adding delays 0x%02x, delay %d, time %d\r\n"), relays, delay, time);
184 230
185 now = ds1307_time(); 231 now = ds1307_time();
186 if (now == -1) { 232 if (now == -1) {
187 puts_P(PSTR("Unable to read time\r\n")); 233 puts_P(PSTR("Unable to read time\r\n"));
188 return; 234 return;
199 return; 245 return;
200 } 246 }
201 247
202 settings.jobs[i].start = now + delay * 60; 248 settings.jobs[i].start = now + delay * 60;
203 settings.jobs[i].length = time * 60; 249 settings.jobs[i].length = time * 60;
204 settings.jobs[i].region = region; 250 settings.jobs[i].relays = relays;
205 settings.jobs[i].valid = 1; 251 settings.jobs[i].valid = 1;
252
253 water_write_settings();
254 }
255
256 static void
257 water_deljob(int job) {
258 if (job < 0 || job >= NJOBS) {
259 puts_P(PSTR("Invalid job number\r\n"));
260 return;
261 }
262
263 if (settings.jobs[job].valid == 0) {
264 puts_P(PSTR("Job already deleted\r\n"));
265 return;
266 }
267
268 settings.jobs[job].valid = 0;
269 printf_P(PSTR("Removed job %d\r\n"), job);
206 270
207 water_write_settings(); 271 water_write_settings();
208 } 272 }
209 273
210 static void 274 static void
212 int i; 276 int i;
213 277
214 for (i = 0; i < NJOBS; i++) { 278 for (i = 0; i < NJOBS; i++) {
215 printf_P(PSTR("%d: "), i); 279 printf_P(PSTR("%d: "), i);
216 if (settings.jobs[i].valid) { 280 if (settings.jobs[i].valid) {
217 printf_P(PSTR("region %d (%s) start %ld length %d\r\n"), settings.jobs[i].region, regions[settings.jobs[i].region].name, 281 printf_P(PSTR("relays 0x%02x start %ld length %d\r\n"), settings.jobs[i].relays,
218 settings.jobs[i].start, settings.jobs[i].length); 282 settings.jobs[i].start, settings.jobs[i].length);
219 } else { 283 } else {
220 printf_P(PSTR("invalid\r\n")); 284 printf_P(PSTR("invalid\r\n"));
221 } 285 }
222 } 286 }
246 relays = 0; 310 relays = 0;
247 311
248 for (i = 0; i < NJOBS; i++) { 312 for (i = 0; i < NJOBS; i++) {
249 if (settings.jobs[i].valid) { 313 if (settings.jobs[i].valid) {
250 if (now > settings.jobs[i].start + settings.jobs[i].length) { 314 if (now > settings.jobs[i].start + settings.jobs[i].length) {
251 printf_P(PSTR("Marking job %d (%s) done\r\n"), i, regions[settings.jobs[i].region].name); 315 printf_P(PSTR("Marking job %d (relays 0x%02x) done\r\n"), i, settings.jobs[i].relays);
252 settings.jobs[i].valid = 0; 316 settings.jobs[i].valid = 0;
253 logged[i] = 0; 317 logged[i] = 0;
254 continue; 318 continue;
255 } 319 }
256 320
257 if (now > settings.jobs[i].start) { 321 if (now > settings.jobs[i].start) {
258 if (logged[i] == 0) { 322 if (logged[i] == 0) {
259 printf_P(PSTR("Job %d (%s) running\r\n"), i, regions[settings.jobs[i].region].name); 323 printf_P(PSTR("Job %d (relays 0x%02x) running\r\n"), i, settings.jobs[i].relays);
260 logged[i] = 1; 324 logged[i] = 1;
261 } 325 }
262 relays |= 1 << regions[settings.jobs[i].region].relay; 326 relays |= 1 << settings.jobs[i].relays;
263 continue; 327 continue;
264 } 328 }
265 } 329 }
266 } 330 }
267 331