-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathled_ctrl.cpp
More file actions
115 lines (99 loc) · 2.55 KB
/
Copy pathled_ctrl.cpp
File metadata and controls
115 lines (99 loc) · 2.55 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
#include "led_ctrl.h"
#define blueLed 14
#define whiteLed 27
#define MAX_LED_BRIGHTNESS 64 // 64 steps
#define MIN_LED_BRIGHTNESS 0 //
RGBW_LED status_led;
int fade_step, duty, keep_fade_min;
unsigned long led_interval;
void set_status_led(RGBW_LED led) {
status_led = led;
duty = MIN_LED_BRIGHTNESS;
fade_step = 1;
keep_fade_min = 0;
ledcWrite(blueLed, MIN_LED_BRIGHTNESS); // off
ledcWrite(whiteLed, MIN_LED_BRIGHTNESS); // off
switch (led) {
case LED_B_ON:
ledcWrite(blueLed, MAX_LED_BRIGHTNESS); // on
break;
case LED_W_ON:
ledcWrite(whiteLed, MAX_LED_BRIGHTNESS); // on
break;
case LED_B_FADE:
led_interval = 80; // fade cycle = 80ms * 64 steps * 2 = 10.24sec
break;
case LED_W_FADE:
led_interval = 80;
break;
case LED_B_BLINK:
led_interval = 500;
break;
case LED_W_BLINK:
led_interval = 500;
break;
case LED_OFF:
default:
break;
}
}
RGBW_LED get_status_led() {
return status_led;
}
void led_refresh() {
static unsigned long led_tick;
uint8_t pin;
if (millis() - led_tick > led_interval) {
led_tick = millis();
switch (status_led) {
case LED_B_FADE:
pin = blueLed;
goto FADE;
break;
case LED_W_FADE:
pin = whiteLed;
goto FADE;
break;
case LED_B_BLINK:
pin = blueLed;
goto BLINK;
break;
case LED_W_BLINK:
pin = whiteLed;
goto BLINK;
break;
default:
return;
break;
}
FADE:
duty += fade_step;
if ((duty < MIN_LED_BRIGHTNESS) || (duty > MAX_LED_BRIGHTNESS))
log_e("invalid pwm duty %d", duty);
ledcWrite(pin, duty);
if ((duty == MIN_LED_BRIGHTNESS) && keep_fade_min) {
fade_step = 0;
keep_fade_min--;
} else if (duty <= MIN_LED_BRIGHTNESS) {
fade_step = 1;
} else if (duty >= MAX_LED_BRIGHTNESS) {
fade_step = -1;
keep_fade_min = 25; // 25 * 80ms = 2 sec
}
return;
BLINK:
duty = (duty == MIN_LED_BRIGHTNESS) ? MAX_LED_BRIGHTNESS : MIN_LED_BRIGHTNESS;
ledcWrite(pin, duty);
return;
}
}
void led_init() {
ledcAttach(blueLed, 12000, 8); // 12 kHz PWM, 8-bit resolution
ledcOutputInvert(blueLed, true);
ledcWrite(blueLed, MIN_LED_BRIGHTNESS); // off
delay(1);
ledcAttach(whiteLed, 12000, 8);
ledcOutputInvert(whiteLed, true);
ledcWrite(whiteLed, MIN_LED_BRIGHTNESS); // off
delay(1);
}