-
Notifications
You must be signed in to change notification settings - Fork 0
/
clock_render.c
235 lines (217 loc) · 7.63 KB
/
clock_render.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
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
#include "clock_render.h"
#include "buttons.h"
#include "render/blank.h"
#include "render/bounce.h"
#include "render/fade.h"
#include "render/drops.h"
#include "render/matrix.h"
#include "render/matrix_with_numbers.h"
#include "render/number_cascade.h"
#include "render/number_cascade_hires.h"
#include "render/waveform.h"
// This is the "render multiplexer" file. It registers a table
// of rendering options (called DisplayMode) and provides logic to
// call the "current" one and select between them with the
// INCREMENT_BUTTON.
//
// You can easily customize display_modes[], removing, rearranging
// or adding your own new entries. Anything you add should be put
// in the render/ directory for consistency. Using the existing
// modes as a starting point (such as render/blank.c) should make
// the process relatively straight-forwrd for those who are
// already familiar with C.
//
// This file also supports the concept of sleep_time and wake_time
// which is used to automatically change the display mode to "off"
// when sleep_time is encoundered and change it back when
// wake_time is encountered.. This only happens if sleep_time != wake_time
struct DisplayMode {
const char* name;
void (*render)(
uint32_t* led,
uint32_t frame_index,
uint16_t time_hhmm,
const struct ClockSettings* settings);
};
#if defined(led_matrix_dotstar)
struct DisplayMode display_modes[] = {
{"matrix", matrix_render}, // This entry will be the default power-on mode
{"number_cascade", number_cascade_render},
{"number_fade", fade_render},
{"off", blank_render}, // always put this entry at the end of the list
};
#elif defined(led_matrix_64x32)
struct DisplayMode display_modes[] = {
{"matrix_with_numbers", matrix_with_numbers_render}, // This entry will be the default power-on mode
{"matrix", matrix_render}, // Note: MATRIX_ONLY_MODE_INDEX in clock_render.h points to this index
{"bounce", bounce_render},
{"number_cascade", number_cascade_hires_render},
{"waveform", waveform_render},
{"drops", drops_render},
{"off", blank_render}, // always put this entry at the end of the list
};
#else
#error Unknown LED_MATRIX_SOURCE
#endif
#define NUM_DISPLAY_MODES (sizeof(display_modes) / sizeof(display_modes[0]))
#define OFF_DISPLAY_MODE_INDEX (NUM_DISPLAY_MODES - 1)
static uint16_t last_sleep_or_wake_hhmm; // used to create an edge trigger
static uint16_t next_mode_change_hhmm;
// this is so initilization can happen when switching modes
static uint32_t frame_index_delta = 0;
static uint8_t display_mode; // currently active index
static uint8_t wake_display_mode; // what mode to wake up to
static uint8_t display_mode_intitialized = 0;
// interface for display modes
uint8_t clock_render_num_display_modes(void) {
return NUM_DISPLAY_MODES;
}
void clock_render_set_display_mode(uint8_t mode) {
if (mode >= NUM_DISPLAY_MODES) {
return;
}
display_mode = mode;
}
uint8_t clock_render_get_display_mode(void) {
return display_mode;
}
uint16_t clock_render_next_mode_change(void) {
return next_mode_change_hhmm;
}
// interface for getting names. Used by clock_settings.c
const char* clock_render_display_mode_name(uint8_t mode) {
if (mode < NUM_DISPLAY_MODES) {
return display_modes[mode].name;
}
return "unknown";
}
static void increment_display_mode(uint32_t frame_index) {
++display_mode;
if (display_mode >= NUM_DISPLAY_MODES) {
display_mode = 0;
}
// the mode to return to when waking up
wake_display_mode = display_mode;
// This is done to show numbers right away in DISPLAY_NUMBERS mode
// +1 because check_buttons is called at the end of the loop
frame_index_delta = frame_index + 1;
clock_render_reset_mode_change();
}
// logic that checks button state and changed the display mode if needed.
static uint8_t check_buttons(uint8_t button_pressed, uint32_t frame_index) {
if (button_pressed & INCREMENT_BUTTON) {
increment_display_mode(frame_index);
}
if (button_pressed & SELECT_BUTTON) {
// The user asked to go into set time mode (whether they realize it or not).
return 1;
}
return 0;
}
void clock_render_reset_mode_change(void) {
next_mode_change_hhmm = 0xFFFF;
}
// Initializes data the first time it is called.
static void maybe_clock_init(
uint32_t frame_index,
uint8_t time_hhmm,
const struct ClockSettings* settings) {
if (!display_mode_intitialized) {
last_sleep_or_wake_hhmm = 0xFFFF;
clock_render_reset_mode_change();
display_mode = settings->startup_display_mode;
wake_display_mode = display_mode;
display_mode_intitialized = 1;
}
if (frame_index == 0) {
// reset the delta so that display_modes that need to
// do something when they are first selected can
// get an effective frame_index = 0
frame_index_delta = 0;
}
}
// This logic implements sleep_time and wake_time
static void check_for_sleep_and_wake(
uint16_t time_hhmm,
const struct ClockSettings* settings) {
if (settings->sleep_time == settings->wake_time) {
return;
}
// last_sleep_or_wake_hhmm could be removed. The penalty for
// doing so is that if the user wants to change modes and
// the time happens to be sleep_time or wake_time, then
// the clock will continuously override the user setting until
// the minute has passed.
if (last_sleep_or_wake_hhmm == time_hhmm) {
return;
}
if (time_hhmm == settings->sleep_time) {
display_mode = OFF_DISPLAY_MODE_INDEX;
last_sleep_or_wake_hhmm = time_hhmm;
} else if (time_hhmm == settings->wake_time) {
display_mode = wake_display_mode;
last_sleep_or_wake_hhmm = time_hhmm;
}
}
static uint16_t add_minutes(uint16_t time_hhmm, uint16_t delta) {
uint16_t minutes = (time_hhmm % 100) + delta;
uint16_t hours = time_hhmm / 100;
while (minutes >= 60) {
++hours;
minutes -= 60;
}
return (hours % 24) * 100 + minutes;
}
static void check_for_auto_mode_change(
uint16_t time_hhmm,
const struct ClockSettings* settings,
uint32_t frame_index) {
if (settings->mode_change_minutes == 0) {
// disabled
return;
}
if (display_mode == OFF_DISPLAY_MODE_INDEX) {
return;
}
if (time_hhmm == next_mode_change_hhmm) {
do {
increment_display_mode(frame_index);
} while ((settings->enabled_modes & (1 << display_mode)) == 0);
}
if (next_mode_change_hhmm == 0xFFFF) {
next_mode_change_hhmm = add_minutes(time_hhmm, settings->mode_change_minutes);
// Odd observation. With this previous code:
// framerates took a huge hit on the matrix mode. This
// happened even when this function was not called at all.
// It seems like having random() here is changing something
// fundamental with the build
/*
next_mode_change_hhmm =
time_hhmm +
settings->mode_change_min_minutes +
(random() % (settings->mode_change_max_minutes -
settings->mode_change_min_minutes));
*/
}
}
// public interface. Called to render all particles and update their state.
uint8_t clock_render(
uint32_t* led,
uint8_t button_pressed,
uint32_t frame_index,
uint16_t time_hhmm,
const struct ClockSettings* settings) {
maybe_clock_init(frame_index, time_hhmm, settings);
check_for_sleep_and_wake(time_hhmm, settings);
display_modes[display_mode].render(
led,
frame_index - frame_index_delta,
time_hhmm,
settings);
// Note: anything that might call increment_display_mode() (including
// the two calls below) must be called after dislay_modes[i].render
// Otherwise the passed frame index will not be zero which can
// lead to initilization problems.
check_for_auto_mode_change(time_hhmm, settings, frame_index);
return check_buttons(button_pressed, frame_index);
}