1#include <main-header.h>
5#define LED_WHITE_MIN (250 * 0xFF)
17# define LED_TIMER_FREQUENCY 130
21static uint8_t do_mosfet;
23static int lastled = 0;
24static uint16_t fadetarget[3];
25static uint16_t curled[3];
26static uint16_t ledoffval[3];
27static uint16_t blink_colour[3];
38struct leddef intled = { .pins = { 106, 107, 108 }, .timers = { 3, 3, 3 } };
39struct leddef extled = { .pins = { 7, 110, 111 }, .timers = { 2, 1, 1 } };
41static struct leddef *curpins;
43static byte led_managed_by;
44static byte lastledconfig;
56static void mosfet(uint8_t number, uint8_t state) {
57 mculib_pin_set(MAIN_MCULIBHANDLE, number + 100, state);
59static int isSameRGB(uint16_t src[3], uint16_t dst[3]) {
60 if (src[0] != dst[0]) {
63 if (src[1] != dst[1]) {
66 if (src[2] != dst[2]) {
72static void copy_colour(uint16_t src[3], uint16_t dst[3]) {
77static struct ledcolour *get_led_colour() {
78 if (led_managed_by == LED_USER) {
81 return &machinecolour;
87static void colour_to_array(uint16_t *dest, uint32_t colour, uint8_t brightness) {
88 dest[0] = (colour >> 16) & 0xFF;
89 dest[1] = (colour >> 8) & 0xFF;
90 dest[2] = (colour) & 0xFF;
93 for (i = 0; i < 3; i++) {
94 dest[i] = dest[i] * (uint16_t)brightness;
97static void set_blink_colour(uint32_t colour,
int brightness) {
98 colour_to_array(blink_colour, colour, brightness);
101static void set_led(uint16_t rgb[3]) {
103 copy_colour(rgb, curled);
104 if (config_get_flag(CONFIG_FLAGS_INTERNAL_LED_INVERTED)) {
105 mculib_timer_set_pwm(MAIN_MCULIBHANDLE, curpins->timers[0], curpins->pins[0], rgb[0]);
106 mculib_timer_set_pwm(MAIN_MCULIBHANDLE, curpins->timers[1], curpins->pins[1], rgb[1]);
107 mculib_timer_set_pwm(MAIN_MCULIBHANDLE, curpins->timers[2], curpins->pins[2], rgb[2]);
109 mculib_timer_set_pwm(MAIN_MCULIBHANDLE, curpins->timers[0], curpins->pins[0], 0xFFFF - rgb[0]);
110 mculib_timer_set_pwm(MAIN_MCULIBHANDLE, curpins->timers[1], curpins->pins[1], 0xFFFF - rgb[1]);
111 mculib_timer_set_pwm(MAIN_MCULIBHANDLE, curpins->timers[2], curpins->pins[2], 0xFFFF - rgb[2]);
116 if ((rgb[0] > LED_WHITE_MIN) && (rgb[1] > LED_WHITE_MIN) && (rgb[2] > LED_WHITE_MIN)) {
118 if (config_get_flag(CONFIG_FLAGS_WHITE_MOSFET0)) {
121 if (config_get_flag(CONFIG_FLAGS_WHITE_MOSFET1)) {
126 if (config_get_flag(CONFIG_FLAGS_WHITE_MOSFET0)) {
129 if (config_get_flag(CONFIG_FLAGS_WHITE_MOSFET1)) {
139static int inline cnt(
int idx) {
140 if (fadetarget[idx] > curled[idx]) {
141 if ((fadetarget[idx] - curled[idx]) < 256) {
142 curled[idx] = fadetarget[idx];
144 curled[idx] = curled[idx] + 255;
149 if (fadetarget[idx] < curled[idx]) {
150 if ((curled[idx] - fadetarget[idx]) < 256) {
151 curled[idx] = fadetarget[idx];
153 curled[idx] = curled[idx] - 256;
164static void led_timer_irq() {
167 if (operation == 1) {
169 }
else if (operation == 2) {
170 if (ctr > (lc->off + lc->on)) {
174 set_blink_colour(lc->colour, lc->brightness);
175 set_led(blink_colour);
176 }
else if (ctr == lc->on) {
185 }
else if (operation == 3) {
187 set_led(blink_colour);
188 }
else if (ctr == lc->on) {
193 }
else if (operation == 4) {
194 if (isSameRGB(fadetarget, curled)) {
204static void initpin(
int pinno) {
206 int pin = curpins->pins[pinno];
207 int timer = curpins->timers[pinno];
209 r = mculib_pin_out(MAIN_MCULIBHANDLE, pin, HAL_PIN_FASTEST);
210 printf(
"LED: Setting pin %i to timer %i\r\n", pin, timer);
212 printf(
"Failed to config pin %i to timer: %\r\n", pin, r);
215 r = mculib_timer_attach_pin_pwm(MAIN_MCULIBHANDLE, timer, pin);
217 printf(
"Failed to attach pin %i to timer: %\r\n", pin, r);
223 lastledconfig = config_get_flag(CONFIG_FLAGS_LEDEXTERN);
227 printf(
"Initializing LEDs\r\n");
232 mculib_timer_disable(MAIN_MCULIBHANDLE, 3);
234 r = mculib_timer_enable_simple(MAIN_MCULIBHANDLE, 2, LED_TIMER_FREQUENCY, &led_timer_irq);
236 printf(
"Failed to init timer for led: %\r\n", r);
239 r = mculib_timer_enable_simple(MAIN_MCULIBHANDLE, 1, LED_TIMER_FREQUENCY, &led_timer_irq);
241 printf(
"Failed to init timer for led: %\r\n", r);
245 mculib_timer_disable(MAIN_MCULIBHANDLE, 1);
246 mculib_timer_disable(MAIN_MCULIBHANDLE, 2);
247 r = mculib_timer_enable_simple(MAIN_MCULIBHANDLE, 3, 300, &led_timer_irq);
249 printf(
"Failed to init timer for led: %\r\n", r);
254 led_set_machine_managed();
260 if ((r = mculib_pin_out(MAIN_MCULIBHANDLE, 100, HAL_PIN_FASTEST))) {
261 printf(
"led mosfetinit: failed to set pin %i to out: %i\r\n", 100, r);
264 if ((r = mculib_pin_out(MAIN_MCULIBHANDLE, 101, HAL_PIN_FASTEST))) {
265 printf(
"led mosfetinit: failed to set pin %i to out: %i\r\n", 101, r);
273void led_flash(LED_USAGE_TYPE lut, uint32_t colour, uint32_t duration) {
274 if (lut != led_managed_by) {
278 set_blink_colour(colour, 256);
279 get_led_colour()->on = duration;
288void led_blink(LED_USAGE_TYPE lut, uint32_t colour, uint8_t brightness, uint32_t on, uint32_t off) {
289 led_blink_flags(lut, colour, brightness, on, off, 0);
291void led_blink_flags(LED_USAGE_TYPE lut, uint32_t colour, uint8_t brightness, uint32_t on, uint32_t off, uint32_t flags) {
292 if (lut != led_managed_by) {
296 if (flags & LED_FLAG_FADE_TO) {
298 colour_to_array((uint16_t *)&fadetarget, colour, brightness);
306 lc->brightness = brightness;
309 set_blink_colour(lc->colour, lc->brightness);
314void led_print_status() {
315 if (operation == 2) {
316 printf(
"R=%p, G=%p, B=%p\r\n", (
int)blink_colour[0], (
int)blink_colour[1], (
int)blink_colour[2]);
318 printf(
"Unprintable operation %i\r\n", operation);
323void led_off(LED_USAGE_TYPE lut) {
324 if (lut != led_managed_by) {
331 do_mosfet = config_get_flag(CONFIG_FLAGS_WHITE_MOSFET0) | config_get_flag(CONFIG_FLAGS_WHITE_MOSFET1);
333 if (config_get_flag(CONFIG_FLAGS_LEDEXTERN) != lastledconfig) {
334 led_off(led_managed_by);
346void led_set_machine_managed() {
347 led_managed_by = LED_MACHINE;
349void led_set_user_managed() {
350 led_managed_by = LED_USER;
352void led_set_wifi_managed() {
353 led_managed_by = LED_WIFI;
356void led_indicate(LED_INDICATOR a) {
357 if (a == LED_POWERUP) {
358 led_blink(a, (uint32_t)0xFFFFFFFF, (uint8_t)50, (uint32_t)50, (uint32_t)100);
362 if (config_get_flag(CONFIG_FLAGS_LEDEXTERN)) {
365 if (a == LED_FATALERROR) {
366 led_blink(a, (uint32_t)0xFF0000, (uint8_t)50, (uint32_t)50, (uint32_t)100);
367 }
else if (a == LED_POWERUP) {
368 led_blink(a, (uint32_t)0x0000FF, (uint8_t)50, (uint32_t)50, (uint32_t)100);
369 }
else if (a == LED_RESET) {
370 led_blink(a, (uint32_t)0x010101, (uint8_t)50, (uint32_t)100, (uint32_t)100);
371 }
else if (a == LED_FLASHEND) {
372 led_blink(a, (uint32_t)0xFF0000, (uint8_t)255, (uint32_t)10, (uint32_t)300);
373 }
else if (a == LED_WIFI_BRIDGE) {
374 led_blink(a, (uint32_t)0xFF0000, (uint8_t)256, (uint32_t)5, (uint32_t)150);
375 }
else if (a == LED_WIFI_BRIDGE2) {
376 led_blink(a, (uint32_t)0x00FF00, (uint8_t)256, (uint32_t)5, (uint32_t)150);
377 }
else if (a == LED_WIFI_STATION) {
379 }
else if (a == LED_WIFI_APMODE) {
381 }
else if (a == LED_WIFI_CONNECTED) {
382 led_off(LED_MACHINE);
383 }
else if (a == LED_POWERSAVE) {
384 led_blink(LED_MACHINE, 0xFFFFFF, 10, 5, 1000);
void led_flash(LED_USAGE_TYPE lut, uint32_t colour, uint32_t duration)
will flash led once
void led_blink(LED_USAGE_TYPE lut, uint32_t colour, uint8_t brightness, uint32_t on, uint32_t off)
will keep blinking until told otherwise will repeat endlessly.