-
Notifications
You must be signed in to change notification settings - Fork 0
/
morningtown.c
182 lines (153 loc) · 4.36 KB
/
morningtown.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
* morningtown.c
*
* Silent alarm clock
*
* Copyright © 2023 Thomas White <[email protected]>
*
* This file is part of MorningTown
*
* MorningTown is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MorningTown is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MorningTown. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <pico/stdlib.h>
#include <hardware/rtc.h>
#include <hardware/watchdog.h>
#include <pico/cyw43_arch.h>
#include "ntp_client.h"
#define LED_GREEN 21
#define LED_RED 22
#define TEST_BUTTON 16
/* Hard-coded calculations because I can't be bothered to work out all the
* special cases (leap years etc). There's a decent chance that DST will have
* been abolished in Europe by the time this table runs out, anyway.
*/
static int last_sunday_in_march(time_t year)
{
switch ( year ) {
case 2024: return 31;
case 2025: return 30;
case 2026: return 29;
case 2027: return 28;
case 2028: return 26;
case 2029: return 25;
case 2030: return 31;
default: return 28; /* Guess! */
}
}
static int last_sunday_in_october(time_t year)
{
switch ( year ) {
case 2024: return 27;
case 2025: return 26;
case 2026: return 25;
case 2027: return 31;
case 2028: return 29;
case 2029: return 28;
case 2030: return 27;
default: return 27; /* Guess! */
}
}
/* This is about the simplest possible case of local time handling, and it
* still makes me want to hurl. Ugh.
*
* Note that we don't care about the *time* of switching to/from DST (01:00 in
* Europe), because the wakeup time is usually much later.
*/
static int dst(datetime_t t)
{
/* April to September inclusive. Note Jan=0 */
if ( (t.month >= 3) && (t.month <= 8) ) return 1;
if ( (t.month == 2) && (t.day >= last_sunday_in_march(t.year)) ) return 1;
if ( (t.month == 9) && (t.day < last_sunday_in_october(t.year)) ) return 1;
return 0;
}
static void check_clock(int *pre_wake, int *wake_now)
{
time_t dstoffs;
datetime_t t = {0};
rtc_get_datetime(&t);
/* Time offsets for CET/CEST (Europe) */
dstoffs = dst(t) ? 2 : 1;
/* Set wakeup times here */
if ( (t.hour == 7-dstoffs) && (t.min >= 15) ) {
*pre_wake = 1;
*wake_now = 0;
} else if ( (t.hour >= 8-dstoffs) && (t.hour < 12) ) {
*pre_wake = 0;
*wake_now = 1;
} else {
*pre_wake = 0;
*wake_now = 0;
}
}
int main()
{
NTP_T *ntp_state;
int last_conn;
int pre_wake = 0;
int wake_now = 0;
int initial_sync;
gpio_init(LED_GREEN);
gpio_init(LED_RED);
gpio_init(TEST_BUTTON);
gpio_set_dir(LED_GREEN, GPIO_OUT);
gpio_set_dir(LED_RED, GPIO_OUT);
gpio_set_dir(TEST_BUTTON, GPIO_IN);
gpio_pull_up(TEST_BUTTON);
cyw43_arch_init();
cyw43_arch_enable_sta_mode();
stdio_init_all();
watchdog_enable(0x7fffff, 1);
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 1);
gpio_put(LED_RED, 1);
gpio_put(LED_GREEN, 1);
sleep_ms(2000);
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 0);
gpio_put(LED_RED, 0);
gpio_put(LED_GREEN, 0);
rtc_init();
ntp_state = ntp_init();
last_conn = 20000;
initial_sync = 0;
while (1) {
watchdog_update();
int st = cyw43_wifi_link_status(&cyw43_state, CYW43_ITF_STA);
if ( (st != CYW43_LINK_JOIN) && (last_conn > 10000) ) {
cyw43_arch_wifi_connect_async(WIFI_SSID,
WIFI_PASSWORD,
CYW43_AUTH_WPA2_AES_PSK);
debug_print("connecting to wifi...\n");
last_conn = 0;
}
if ( ntp_ok(ntp_state) ) initial_sync = 1;
if ( initial_sync ) check_clock(&pre_wake, &wake_now);
/* Determine the LED status */
if ( gpio_get(TEST_BUTTON) == 0 ) {
/* Button pressed */
gpio_put(LED_GREEN, ntp_ok(ntp_state));
gpio_put(LED_RED, ntp_err(ntp_state));
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN,
(st == CYW43_LINK_JOIN));
} else {
/* Normal operation */
gpio_put(LED_GREEN, pre_wake || wake_now);
gpio_put(LED_RED, wake_now);
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 0);
}
last_conn += 1;
cyw43_arch_poll();
sleep_ms(100);
}
}