forked from geeekpi/pico_breadboard_kit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
300 lines (233 loc) · 7.88 KB
/
main.c
File metadata and controls
300 lines (233 loc) · 7.88 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#include <stdio.h>
#include "pico/stdlib.h"
#include "pico/mutex.h"
#include "pico/sem.h"
#include "FreeRTOS.h" /* Must come first. */
#include "task.h" /* RTOS task related API prototypes. */
#include "queue.h" /* RTOS queue related API prototypes. */
#include "timers.h" /* Software timer related API prototypes. */
#include "semphr.h" /* Semaphore related API prototypes. */
#include "lvgl.h"
#include "lv_port_disp.h"
#include "lv_port_indev.h"
#include "keypad_encoder/lv_demo_keypad_encoder.h"
#include "hardware/pio.h"
#include "hardware/clocks.h"
#include "hardware/adc.h"
#include "ws2812.pio.h"
void vApplicationTickHook(void)
{
lv_tick_inc(1);
}
lv_obj_t *img1 = NULL; // 开机图片
lv_obj_t *led1 = NULL;
lv_obj_t *led2 = NULL;
lv_obj_t *jy_label = NULL;
uint8_t adc_en = 0;
static void keypad_handler(lv_event_t *e)
{
lv_event_code_t code = lv_event_get_code(e);
if (code == LV_EVENT_CLICKED)
{
lv_obj_del(img1);
lv_obj_clean(lv_scr_act());
vTaskDelay(100 / portTICK_PERIOD_MS);
lv_demo_keypad_encoder();
}
}
static void beep_handler(lv_event_t *e)
{
lv_event_code_t code = lv_event_get_code(e);
if (code == LV_EVENT_VALUE_CHANGED)
{
gpio_xor_mask(0x2000);
}
}
static inline void put_pixel(uint32_t pixel_grb)
{
pio_sm_put_blocking(pio0, 0, pixel_grb << 8u);
}
static inline uint32_t urgb_u32(uint8_t r, uint8_t g, uint8_t b)
{
return ((uint32_t)(r) << 8) |
((uint32_t)(g) << 16) |
(uint32_t)(b);
}
static void slider_event_cb(lv_event_t *e)
{
lv_event_code_t code = lv_event_get_code(e);
lv_obj_t *obj = lv_event_get_target(e);
if (code == LV_EVENT_VALUE_CHANGED)
{
lv_color_t color = lv_colorwheel_get_rgb(obj);
put_pixel(urgb_u32(color.ch.red << 5, ((color.ch.green_h << 2) + color.ch.green_h) << 2, (color.ch.blue << 3)));
}
}
static void clr_rgb_handler(lv_event_t *e)
{
lv_event_code_t code = lv_event_get_code(e);
if (code == LV_EVENT_CLICKED)
{
put_pixel(urgb_u32(0, 0, 0));
}
}
void gpio_callback(uint gpio, uint32_t events)
{
switch (gpio)
{
case 15:
lv_led_toggle(led1);
gpio_xor_mask(1ul << 16);
break;
case 14:
lv_led_toggle(led2);
gpio_xor_mask(1ul << 17);
break;
}
}
static void hw_handler(lv_event_t *e)
{
lv_obj_t *label;
lv_event_code_t code = lv_event_get_code(e);
if (code == LV_EVENT_CLICKED)
{
lv_obj_del(img1);
lv_obj_clean(lv_scr_act());
vTaskDelay(100 / portTICK_PERIOD_MS);
// 蜂鸣器
gpio_init(13);
gpio_set_dir(13, GPIO_OUT);
lv_obj_t *beep_btn = lv_btn_create(lv_scr_act());
lv_obj_add_event_cb(beep_btn, beep_handler, LV_EVENT_ALL, NULL);
lv_obj_align(beep_btn, LV_ALIGN_TOP_MID, 0, 40);
lv_obj_add_flag(beep_btn, LV_OBJ_FLAG_CHECKABLE);
lv_obj_set_height(beep_btn, LV_SIZE_CONTENT);
label = lv_label_create(beep_btn);
lv_label_set_text(label, "Beep");
lv_obj_center(label);
// 清除颜色
lv_obj_t *clr_rgb_btn = lv_btn_create(lv_scr_act());
lv_obj_add_event_cb(clr_rgb_btn, clr_rgb_handler, LV_EVENT_ALL, NULL);
lv_obj_align(clr_rgb_btn, LV_ALIGN_TOP_MID, 0, 80);
label = lv_label_create(clr_rgb_btn);
lv_label_set_text(label, "Turn off RGB");
lv_obj_center(label);
// RGB 灯
/*Create a slider in the center of the display*/
lv_obj_t *lv_colorwheel = lv_colorwheel_create(lv_scr_act(), true);
lv_obj_set_size(lv_colorwheel, 200, 200);
lv_obj_align(lv_colorwheel, LV_ALIGN_TOP_MID, 100, 0);
lv_obj_center(lv_colorwheel);
lv_obj_add_event_cb(lv_colorwheel, slider_event_cb, LV_EVENT_VALUE_CHANGED, NULL);
// todo get free sm
PIO pio = pio0;
int sm = 0;
uint offset = pio_add_program(pio, &ws2812_program);
ws2812_program_init(pio, sm, offset, 12, 800000, true);
put_pixel(urgb_u32(0, 0, 0));
gpio_set_irq_enabled_with_callback(14, GPIO_IRQ_EDGE_RISE | GPIO_IRQ_EDGE_FALL, true, &gpio_callback);
gpio_set_irq_enabled_with_callback(15, GPIO_IRQ_EDGE_RISE | GPIO_IRQ_EDGE_FALL, true, &gpio_callback);
gpio_set_irq_enabled_with_callback(22, GPIO_IRQ_EDGE_RISE | GPIO_IRQ_EDGE_FALL, true, &gpio_callback);
led1 = lv_led_create(lv_scr_act());
lv_obj_align(led1, LV_ALIGN_TOP_MID, -30, 400);
lv_led_set_color(led1, lv_palette_main(LV_PALETTE_RED));
lv_led_off(led1);
led2 = lv_led_create(lv_scr_act());
lv_obj_align(led2, LV_ALIGN_TOP_MID, 30, 400);
lv_led_set_color(led2, lv_palette_main(LV_PALETTE_GREEN));
lv_led_off(led2);
gpio_init(16);
gpio_init(17);
gpio_set_dir(16, GPIO_OUT);
gpio_set_dir(17, GPIO_OUT);
gpio_put(16, 0);
gpio_put(17, 0);
adc_en = 1;
jy_label = lv_label_create(lv_scr_act());
lv_label_set_text(jy_label, "X = 0 Y = 0");
lv_obj_set_style_text_align(jy_label, LV_TEXT_ALIGN_CENTER, 0);
lv_obj_set_width(jy_label, 50);
lv_obj_align(jy_label, LV_ALIGN_CENTER, 0, 0);
lv_obj_t *btn_label = lv_label_create(lv_scr_act());
lv_label_set_text(btn_label, "Press Button to Toggle LED!");
lv_obj_set_style_text_align(btn_label, LV_TEXT_ALIGN_CENTER, 0);
lv_obj_align(btn_label, LV_ALIGN_BOTTOM_MID, 0, -20);
}
}
void lv_example_btn_1(void)
{
lv_obj_t *label;
lv_obj_t *btn1 = lv_btn_create(lv_scr_act());
lv_obj_add_event_cb(btn1, keypad_handler, LV_EVENT_ALL, NULL);
lv_obj_align(btn1, LV_ALIGN_TOP_MID, 0, 40);
label = lv_label_create(btn1);
lv_label_set_text(label, "Keypad Demo");
lv_obj_center(label);
lv_obj_t *btn2 = lv_btn_create(lv_scr_act());
lv_obj_add_event_cb(btn2, hw_handler, LV_EVENT_ALL, NULL);
lv_obj_align(btn2, LV_ALIGN_TOP_MID, 0, 80);
label = lv_label_create(btn2);
lv_label_set_text(label, "Hardware Demo");
lv_obj_center(label);
}
void task0(void *pvParam)
{
lv_obj_clean(lv_scr_act());
vTaskDelay(100 / portTICK_PERIOD_MS);
img1 = lv_img_create(lv_scr_act());
LV_IMG_DECLARE(ai);
lv_img_set_src(img1, &ai);
lv_obj_align(img1, LV_ALIGN_DEFAULT, 0, 0);
lv_example_btn_1();
for (;;)
{
if (adc_en)
{
adc_init();
// Make sure GPIO is high-impedance, no pullups etc
adc_gpio_init(26);
adc_gpio_init(27);
for (;;)
{
char buf[50];
adc_select_input(0);
uint adc_x_raw = adc_read();
adc_select_input(1);
uint adc_y_raw = adc_read();
const uint bar_width = 40;
const uint adc_max = (1 << 12) - 1;
int bar_x_pos = (adc_x_raw * bar_width / adc_max) - 19;
int bar_y_pos = (adc_y_raw * bar_width / adc_max) - 19;
vTaskDelay(200 / portTICK_PERIOD_MS);
sprintf(buf,"X = %d Y = %d",bar_x_pos,bar_y_pos);
lv_label_set_text(jy_label, buf);
}
}
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
void task1(void *pvParam)
{
for (;;)
{
lv_task_handler();
vTaskDelay(5 / portTICK_PERIOD_MS);
}
}
int main()
{
stdio_init_all();
lv_init();
lv_port_disp_init();
lv_port_indev_init();
UBaseType_t task0_CoreAffinityMask = (1 << 0);
UBaseType_t task1_CoreAffinityMask = (1 << 1);
TaskHandle_t task0_Handle = NULL;
xTaskCreate(task0, "task0", 2048, NULL, 1, &task0_Handle);
vTaskCoreAffinitySet(task0_Handle, task0_CoreAffinityMask);
TaskHandle_t task1_Handle = NULL;
xTaskCreate(task1, "task1", 2048, NULL, 2, &task1_Handle);
vTaskCoreAffinitySet(task1_Handle, task1_CoreAffinityMask);
vTaskStartScheduler();
return 0;
}