-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
246 lines (202 loc) · 9.52 KB
/
Copy pathmain.cpp
File metadata and controls
246 lines (202 loc) · 9.52 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/**
* pico-sunrise - A sunrise clock for RP2040 based microcontrollers
*
* @file
* @copyright
* @parblock
* SPDX-License-Identifier: MIT
*
* SPDX-FileCopyrightText: Copyright (c) 2025 Ian Hangartner <icrashstuff at outlook dot com>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
* @endparblock
*
* @brief Entry point of pico-sunrise
*/
/**
* @mainpage
* A sunrise clock for RP2040 based microcontrollers
*
* @par Parts required
* - RP2040 based microcontroller (Raspberry Pi Pico) - Such as https://www.adafruit.com/product/4883\n
* - PA1616S GPS Module - MTK3339 Chipset - Such as https://www.adafruit.com/product/746\n
* - SK6812RGBW individually addressable LED strip - Such as https://www.adafruit.com/product/4913\n
*
* @par Configuration
* @ref config.h
*/
#include "hardware/watchdog.h"
#include "pico/multicore.h"
#include "pico/stdlib.h"
#include <stdint.h>
#include <stdio.h>
#include "config.h"
#include "datetime.h"
#include "gps.h"
#include "led.h"
#include "license_text.h"
#include "sunrise.h"
#define arraysizeof(array) (sizeof(array) / sizeof(array[0]))
static void verify_time(const datetime_t& a, const datetime_t& b)
{
if (a == b)
return;
printf("Time mismatch\n");
a.print_to_stdout("a: ");
b.print_to_stdout("b: ");
}
/**
* Check that timezone correct is working
*/
static void check_dst()
{
const timespan_t offset_dt = timespan_t(0, -8, 0, 0);
const timespan_t offset_st = timespan_t(0, -9, 0, 0);
verify_time(datetime_t((1762077599) * MICROSECONDS_PER_SECOND), datetime_t(2025, 11, 2, 9, 59, 59));
verify_time(datetime_t((1762077600) * MICROSECONDS_PER_SECOND), datetime_t(2025, 11, 2, 10, 0, 0));
verify_time(datetime_t((1762077599) * MICROSECONDS_PER_SECOND).get_tz_corrected(offset_st, offset_dt), datetime_t(2025, 11, 2, 1, 59, 59));
verify_time(datetime_t((1762077600) * MICROSECONDS_PER_SECOND).get_tz_corrected(offset_st, offset_dt), datetime_t(2025, 11, 2, 1, 0, 0));
verify_time(datetime_t((1741517999) * MICROSECONDS_PER_SECOND), datetime_t(2025, 3, 9, 10, 59, 59));
verify_time(datetime_t((1741518000) * MICROSECONDS_PER_SECOND), datetime_t(2025, 3, 9, 11, 0, 0));
verify_time(datetime_t((1741517999) * MICROSECONDS_PER_SECOND).get_tz_corrected(offset_st, offset_dt), datetime_t(2025, 3, 9, 1, 59, 59));
verify_time(datetime_t((1741518000) * MICROSECONDS_PER_SECOND).get_tz_corrected(offset_st, offset_dt), datetime_t(2025, 3, 9, 3, 0, 0));
}
static int status_impl_dummy_func(const char*, va_list) { return 0; }
static int (*status_impl)(const char* fmt, va_list vlist) = vprintf;
#if defined(__GNUC__) || defined(__clang__)
#define formatting_attribute(fmtargnumber) __attribute__((format(__printf__, fmtargnumber, fmtargnumber + 1)))
#endif
static formatting_attribute(1) int status(const char* fmt, ...)
{
va_list args;
va_start(args, fmt);
int r = status_impl(fmt, args);
va_end(args);
return r;
}
int main()
{
watchdog_enable(WATCHDOG_INIT_TIME, 1);
init_unix_time();
stdio_init_all();
const timespan_t offset_dt = TIMEZONE_OFFSET_DT;
const timespan_t offset_st = TIMEZONE_OFFSET_ST;
/* Reset to midnight 1970-1-11 (local time zone) */
set_unix_time(MICROSECONDS_PER_DAY * 10 - offset_st.to_microseconds_since_1970());
led_init(LED_IS_RGBW, LED_FREQUENCY, LED_GPIO);
#if SUNRISE_TESTING == 0
multicore_launch_core1(gps_thread_func);
#endif
uint64_t last_status_time = 0;
loop_measure_t perf = {};
watchdog_disable();
watchdog_enable(WATCHDOG_LOOP_TIME, 1);
uint32_t dither_count = 0;
while (true)
{
#if SUNRISE_TESTING == 0
if (!time_reached(gps_data.watchdog_expiry_time))
watchdog_update();
#else
watchdog_update();
#endif
const uint64_t loop_start_time = time_us_64();
if (loop_start_time / STATUS_PRINT_INTERVAL != last_status_time / STATUS_PRINT_INTERVAL)
{
status_impl = vprintf;
last_status_time = loop_start_time;
}
else
status_impl = status_impl_dummy_func;
check_dst();
const datetime_t now = datetime_t::get_current_utc().get_tz_corrected(offset_st, offset_dt);
const datetime_t midnight = datetime_t(now.year, now.month, now.day);
#if SUNRISE_TESTING
// Negative offset from full_power_time
const timespan_t rise_time = timespan_t(0, 0, 1, 0);
// Positive offsets from midnight
const datetime_t full_power_time = midnight + rise_time + timespan_t(0, 0, 0, 10);
const datetime_t start_time = full_power_time - rise_time;
const datetime_t off_allowed_time = full_power_time + timespan_t(0, 0, 0, 10);
const datetime_t off_forced_time = off_allowed_time + timespan_t(0, 0, 0, 10);
#else
// Negative offset from full_power_time
const timespan_t rise_time = timespan_t(0, 0, 30, 0);
// Positive offsets from midnight
const datetime_t full_power_time = midnight + timespan_t(0, 6, 0, 0);
const datetime_t start_time = full_power_time - rise_time;
const datetime_t off_allowed_time = midnight + timespan_t(0, 7, 0, 0);
const datetime_t off_forced_time = midnight + timespan_t(0, 7, 30, 0);
#endif
char buf[64];
status("\n\n\n======> Program info\n");
status("Name: pico-sunrise\n");
status("\n======> License text (pico-sunrise)\n");
status("%s", license_text_pico_sunrise);
status("\n======> License text (pico-sdk and pico-examples)\n");
status("%s", license_text_pico_sdk_and_pico_examples);
status("\n======> GPS Status\n");
status("Firmware release: %s\n", gps_data.firmware_release_str);
status("Firmware build id: %s\n", gps_data.firmware_build_id);
status("Firmware internal 1: %s\n", gps_data.firmware_internal_1);
status("Firmware internal 2: %s\n", gps_data.firmware_internal_2);
status("Avg. loop time: %lld us\n", gps_data.perf.average_loop_time);
status("loops_per_second: %.3f\n", gps_data.perf.loops_per_second);
status("Satellites used: %d\n", gps_data.satellites_used);
status("Fix status: %d\n", gps_data.fix_status);
status("NMEA Parsing: %s\n", gps_data.nmea_in_progress);
status("NMEA Last: %s\n", gps_data.nmea_last_full);
float sunrise_factor = -1.0;
if (start_time <= now && now < full_power_time)
{
double time_elapsed = now.to_microseconds_since_1970() - start_time.to_microseconds_since_1970();
double time_to_full = full_power_time.to_microseconds_since_1970() - start_time.to_microseconds_since_1970();
sunrise_factor = time_elapsed / time_to_full;
}
if (full_power_time <= now && now < off_forced_time)
sunrise_factor = 1.0;
int dither_steps_possible = perf.loops_per_second / float(DITHER_NOISE_TARGET_FREQUENCY);
dither_steps_possible = dither_steps_possible < 1 ? 1 : dither_steps_possible;
status("\n======> Sunrise status\n");
status("Current time: %s\n", now.print_to_buffer(buf, arraysizeof(buf)));
status("Midnight: %s\n", midnight.print_to_buffer(buf, arraysizeof(buf)));
status("start_time: %s\n", start_time.print_to_buffer(buf, arraysizeof(buf)));
status("full_power_time: %s\n", full_power_time.print_to_buffer(buf, arraysizeof(buf)));
status("off_allowed_time: %s\n", off_allowed_time.print_to_buffer(buf, arraysizeof(buf)));
status("off_forced_time: %s\n", off_forced_time.print_to_buffer(buf, arraysizeof(buf)));
status("sunrise_factor: %f\n", sunrise_factor);
status("Avg. loop time: %lld us\n", perf.average_loop_time);
status("loops_per_second: %.3f\n", perf.loops_per_second);
status("Dither loops/sec: %.3f\n", perf.loops_per_second / float(dither_steps_possible));
status("Dither cycles: %d\n", dither_steps_possible);
led_color_t* colors = (led_color_t*)calloc(LED_PIXEL_COUNT, sizeof(led_color_t));
++dither_count %= dither_steps_possible;
sunrise_apply(float(dither_count) / float(dither_steps_possible), sunrise_factor, LED_WHITE_COLOR_TEMP, colors, LED_PIXEL_COUNT);
led_swizzle_config_t led_config = {};
led_config.byte_pos_r = LED_BYTE_POS_R;
led_config.byte_pos_g = LED_BYTE_POS_G;
led_config.byte_pos_b = LED_BYTE_POS_B;
led_config.byte_pos_w = LED_BYTE_POS_W;
led_push(colors, LED_PIXEL_COUNT, led_config);
free(colors);
perf.end_loop();
}
led_shutdown();
return 0;
}