generated from Nigh/pico-scheduler-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
138 lines (115 loc) · 2.78 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
#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/adc.h"
#include "hardware/sync.h"
#include "scheduler/uevent.h"
#include "scheduler/scheduler.h"
#include "hardware/pio.h"
#include "hardware/clocks.h"
#include "ws2812/ws2812.pio.h"
#include "platform.h"
#define IS_RGBW false
#define NUM_PIXELS 1
#define WS2812_PIN 16
static inline void put_pixel(uint32_t pixel_grb) {
pio_sm_put_blocking(pio0, 0, pixel_grb << 8u);
}
#include <math.h>
typedef struct RGBColor {
uint8_t r;
uint8_t g;
uint8_t b;
} RGBColor;
RGBColor hsv2rgb(float h, float s, float v) {
float r, g, b;
int i = floor(h * 6);
float f = h * 6 - i;
float p = v * (1 - s);
float q = v * (1 - f * s);
float t = v * (1 - (1 - f) * s);
switch(i % 6) {
case 0: r = v, g = t, b = p; break;
case 1: r = q, g = v, b = p; break;
case 2: r = p, g = v, b = t; break;
case 3: r = p, g = q, b = v; break;
case 4: r = t, g = p, b = v; break;
case 5: r = v, g = p, b = q; break;
}
RGBColor color;
color.r = r * 255;
color.g = g * 255;
color.b = b * 255;
return color;
}
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);
}
volatile uint32_t int_cache;
static __inline void CRITICAL_REGION_ENTER(void) {
int_cache = save_and_disable_interrupts();
}
static __inline void CRITICAL_REGION_EXIT(void) {
restore_interrupts(int_cache);
}
bool timer_100hz_callback(struct repeating_timer* t) {
uevt_bc_e(UEVT_TIMER_100HZ);
return true;
}
void led_blink_routine(void) {
static uint8_t _tick = 0;
_tick += 1;
if(_tick == 64) {
RGBColor color = hsv2rgb(0.7, 0.99, 0.2);
put_pixel(urgb_u32(color.r, color.g, color.b));
}
if(_tick == 74) {
put_pixel(0);
}
if(_tick == 100) {
_tick = 0;
}
}
void temperature_routine(void) {
static uint16_t tick = 0;
static float _t = 0;
if(tick > 200) {
tick = 0;
int32_t t_adc = adc_read();
_t = 27.0 - ((t_adc - 876) / 2.136f);
uevt_bc(UEVT_ADC_TEMPERATURE_RESULT, &_t);
}
tick += 1;
}
void main_handler(uevt_t* evt) {
switch(evt->evt_id) {
case UEVT_TIMER_100HZ:
led_blink_routine();
temperature_routine();
break;
case UEVT_ADC_TEMPERATURE_RESULT:
printf("Temperature is %0.2f\n", *((float*)(evt->content)));
break;
}
}
int main() {
app_sched_init();
user_event_init();
user_event_handler_regist(main_handler);
stdio_init_all();
PIO pio = pio0;
uint offset = pio_add_program(pio, &ws2812_program);
ws2812_program_init(pio, 0, offset, WS2812_PIN, 800000, IS_RGBW);
adc_init();
adc_gpio_init(26);
adc_gpio_init(27);
adc_gpio_init(28);
adc_gpio_init(29);
adc_set_temp_sensor_enabled(true);
adc_select_input(4);
struct repeating_timer timer;
add_repeating_timer_ms(10, timer_100hz_callback, NULL, &timer);
while(true) {
app_sched_execute();
__wfi();
}
}