Allows my home wiz lights to be timed on and off according to a schedule.

Dependents:   heating

Committer:
andrewboyson
Date:
Wed Jun 09 09:23:54 2021 +0000
Revision:
7:3035a540ef65
Parent:
4:65f000627fb0
Changed WizSchedMinutesUtcToLocal function so that local midnight in summer is 00h00 rather than 24h00.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewboyson 0:9af80a39adcc 1 #include <time.h>
andrewboyson 0:9af80a39adcc 2
andrewboyson 4:65f000627fb0 3 #include "tm.h"
andrewboyson 0:9af80a39adcc 4 #include "clk.h"
andrewboyson 0:9af80a39adcc 5 #include "log.h"
andrewboyson 0:9af80a39adcc 6
andrewboyson 0:9af80a39adcc 7 // J F M A+1 M+1 J+1 J+1 A+1 S+1 O+1 N D
andrewboyson 4:65f000627fb0 8 static uint16_t sunrises[] = { 8*60+45, 8*60+ 6, 7*60+ 1, 5*60+38, 4*60+23, 3*60+28, 3*60+26, 4*60+14, 5*60+16, 6*60+16, 7*60+21, 8*60+22 };
andrewboyson 4:65f000627fb0 9 static uint16_t sunsets [] = { 15*60+45, 16*60+43, 17*60+45, 18*60+51, 19*60+53, 20*60+49, 21*60+ 3, 20*60+20, 19*60+ 5, 17*60+45, 16*60+28, 15*60+38 };
andrewboyson 0:9af80a39adcc 10
andrewboyson 0:9af80a39adcc 11 int WizSunSetMinutes ()
andrewboyson 0:9af80a39adcc 12 {
andrewboyson 0:9af80a39adcc 13 struct tm tmUtc;
andrewboyson 0:9af80a39adcc 14 ClkNowTmUtc (&tmUtc);
andrewboyson 0:9af80a39adcc 15
andrewboyson 0:9af80a39adcc 16 int start = sunsets[tmUtc.tm_mon];
andrewboyson 0:9af80a39adcc 17 int finish = tmUtc.tm_mon == 11 ? sunsets[0] : sunsets[tmUtc.tm_mon+1];
andrewboyson 4:65f000627fb0 18 int extra = (finish - start) * (tmUtc.tm_mday-1) / TmMonthLength(tmUtc.tm_year, tmUtc.tm_mon);
andrewboyson 0:9af80a39adcc 19
andrewboyson 0:9af80a39adcc 20 return start + extra;
andrewboyson 0:9af80a39adcc 21 }
andrewboyson 0:9af80a39adcc 22 int WizSunRiseMinutes()
andrewboyson 0:9af80a39adcc 23 {
andrewboyson 0:9af80a39adcc 24 struct tm tmUtc;
andrewboyson 0:9af80a39adcc 25 ClkNowTmUtc (&tmUtc);
andrewboyson 0:9af80a39adcc 26
andrewboyson 0:9af80a39adcc 27 int start = sunrises[tmUtc.tm_mon];
andrewboyson 0:9af80a39adcc 28 int finish = tmUtc.tm_mon == 11 ? sunrises[0] : sunrises[tmUtc.tm_mon+1];
andrewboyson 4:65f000627fb0 29 int extra = (finish - start) * (tmUtc.tm_mday-1) / TmMonthLength(tmUtc.tm_year, tmUtc.tm_mon);
andrewboyson 0:9af80a39adcc 30
andrewboyson 0:9af80a39adcc 31 return start + extra;
andrewboyson 0:9af80a39adcc 32 }