-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.c
267 lines (212 loc) · 6.99 KB
/
main.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
MIT No Attribution
Copyright (c) 2021-2023 Mika Tuupola
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.
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.
-cut-
SPDX-License-Identifier: MIT-0
*/
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>
#include <wchar.h>
#include <pico/stdlib.h>
#include <hardware/clocks.h>
#include <sys/time.h>
#include <hagl_hal.h>
#include <hagl.h>
#include <font6x9.h>
#include <fps.h>
#include <aps.h>
#include "metaballs.h"
#include "plasma.h"
#include "rotozoom.h"
#include "deform.h"
static uint8_t effect = 1;
volatile bool fps_flag = false;
volatile bool switch_flag = true;
static fps_instance_t fps;
static aps_instance_t bps;
static uint8_t *buffer;
static hagl_backend_t backend;
static hagl_backend_t *display;
wchar_t message[32];
static const uint64_t US_PER_FRAME_60_FPS = 1000000 / 60;
static const uint64_t US_PER_FRAME_30_FPS = 1000000 / 30;
static const uint64_t US_PER_FRAME_25_FPS = 1000000 / 25;
static char demo[4][32] = {
"METABALLS",
"PLASMA",
"ROTOZOOM",
"DEFORM",
};
size_t
total_heap()
{
extern char __StackLimit, __bss_end__;
return &__StackLimit - &__bss_end__;
}
size_t
free_heap(void)
{
struct mallinfo m = mallinfo();
return total_heap() - m.uordblks;
}
bool
switch_timer_callback(struct repeating_timer *t)
{
switch_flag = true;
return true;
}
bool
show_timer_callback(struct repeating_timer *t)
{
fps_flag = true;
return true;
}
void static inline
switch_demo()
{
switch_flag = false;
switch(effect) {
case 0:
//metaballs_close();
break;
case 1:
plasma_close();
break;
case 2:
//rotozoom_close();
break;
case 3:
deform_close();
break;
}
effect = (effect + 1) % 4;
switch(effect) {
case 0:
metaballs_init(display);
printf("[main] Initialized metaballs, %d free heap \r\n", free_heap());
break;
case 1:
plasma_init(display);
printf("[main] Initialized plasma, %d free heap \r\n", free_heap());
break;
case 2:
rotozoom_init(display);
printf("[main] Initialized rotozoom, %d free heap \r\n", free_heap());
break;
case 3:
deform_init(display);
printf("[main] Initialized deform, %d free heap \r\n", free_heap());
break;
}
fps_init(&fps);
aps_init(&bps);
}
void static inline
show_fps()
{
hagl_color_t green = hagl_color(display, 0, 255, 0);
fps_flag = 0;
/* Set clip window to full screen so we can display the messages. */
hagl_set_clip(display, 0, 0, display->width - 1, display->height - 1);
/* Print the message on top left corner. */
swprintf(message, sizeof(message), L"%s ", demo[effect]);
hagl_put_text(display, message, 4, 4, green, font6x9);
/* Print the message on lower left corner. */
swprintf(message, sizeof(message), L"%.*f FPS ", 0, fps.current);
hagl_put_text(display, message, 4, display->height - 14, green, font6x9);
/* Print the message on lower right corner. */
swprintf(message, sizeof(message), L"%.*f KBPS ", 0, bps.current / 1024);
hagl_put_text(display, message, display->width - 60, display->height - 14, green, font6x9);
/* Set clip window back to smaller so effects do not mess the messages. */
hagl_set_clip(display, 0, 20, display->width - 1, display->height - 21);
}
int
main()
{
size_t bytes = 0;
struct repeating_timer switch_timer;
struct repeating_timer show_timer;
// set_sys_clock_khz(133000, true);
// clock_configure(
// clk_peri,
// 0,
// CLOCKS_CLK_PERI_CTRL_AUXSRC_VALUE_CLKSRC_PLL_SYS,
// 133000 * 1000,
// 133000 * 1000
// );
stdio_init_all();
/* Sleep so that we have time to open the serial console. */
sleep_ms(5000);
printf("[main] %d total heap \r\n", total_heap());
printf("[main] %d free heap \r\n", free_heap());
fps_init(&fps);
// memset(&backend, 0, sizeof(hagl_backend_t));
// backend.buffer = malloc(MIPI_display->width * MIPI_display->height * (DISPLAY_DEPTH / 8));
// backend.buffer2 = malloc(MIPI_display->width * MIPI_display->height * (DISPLAY_DEPTH / 8));
// hagl_hal_init(&backend);
// display = &backend;
// memset(&backend, 0, sizeof(hagl_backend_t));
// backend.buffer = malloc(MIPI_display->width * MIPI_display->height * (DISPLAY_DEPTH / 8));
// hagl_hal_init(&backend);
// display = &backend;
display = hagl_init();
hagl_clear(display);
hagl_set_clip(display, 0, 20, display->width - 1, display->height - 21);
/* Change demo every 10 seconds. */
add_repeating_timer_ms(10000, switch_timer_callback, NULL, &switch_timer);
/* Update displayed FPS counter every 250 ms. */
add_repeating_timer_ms(250, show_timer_callback, NULL, &show_timer);
while (1) {
uint64_t start = time_us_64();
switch(effect) {
case 0:
metaballs_animate(display);
metaballs_render(display);
break;
case 1:
plasma_animate(display);
plasma_render(display);
break;
case 2:
rotozoom_animate();
rotozoom_render(display);
break;
case 3:
deform_animate();
deform_render(display);
break;
}
/* Update the displayed fps if requested. */
if (fps_flag) {
show_fps();
}
/* Flush back buffer contents to display. NOP if single buffering. */
bytes = hagl_flush(display);
aps_update(&bps, bytes);
fps_update(&fps);
/* Print the message in console and switch to next demo. */
if (switch_flag) {
printf("[main] %s at %d fps / %d kBps\r\n", demo[effect], (uint32_t)fps.current, (uint32_t)(bps.current / 1024));
switch_demo();
}
/* Cap the demos to 60 fps. This is mostly to accommodate to smaller */
/* screens where plasma will run too fast. */
busy_wait_until(start + US_PER_FRAME_60_FPS);
};
return 0;
}