-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathled.c
More file actions
executable file
·84 lines (74 loc) · 1.93 KB
/
led.c
File metadata and controls
executable file
·84 lines (74 loc) · 1.93 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
#include <stdbool.h>
#include "osapi.h"
#include "os_type.h"
#include "ets_sys.h"
#include "eagle_soc.h"
#include "led.h"
#include "gpio.h"
#include "espmissingincludes.h"
#include "pwm.h"
/*
* Sobre PWM: el período se setea en microsegundos. Los ciclos de trabajo se
* setean en unidades de 40 ns (no en porcentajes), pero la implementación de
* Espressif tiene una limitación que hace que todo ciclo de trabajo mayor a 90%
* sea 100%.
*/
#define CHANNEL_COUNT 3
#define PWM_PERIOD 8000
#define MAX_DUTY (PWM_PERIOD * 1000 / 40)
#define RED_CHANNEL 0
#define GREEN_CHANNEL 1
#define BLUE_CHANNEL 2
static bool on;
static struct color color;
static os_timer_t blink_timer;
static uint32_t pwm_conf[CHANNEL_COUNT][3] = {
{PERIPHS_IO_MUX_GPIO4_U, FUNC_GPIO4, 4}, /* rojo */
{PERIPHS_IO_MUX_GPIO5_U, FUNC_GPIO5, 5}, /* verde */
{PERIPHS_IO_MUX_GPIO2_U, FUNC_GPIO2, 2} /* azul */
};
static uint32_t duty_cycles[CHANNEL_COUNT] = {MAX_DUTY, MAX_DUTY, MAX_DUTY};
static void blink_callback(void *arg);
void ICACHE_FLASH_ATTR led_init(void)
{
color.blue = 0xF;
pwm_init(PWM_PERIOD, duty_cycles, CHANNEL_COUNT, pwm_conf);
led_turn_off();
}
void ICACHE_FLASH_ATTR led_set_color(struct color col)
{
color = col;
if (on) {
led_turn_on();
}
}
/* Configura y prende */
void ICACHE_FLASH_ATTR led_turn_on()
{
pwm_set_duty(MAX_DUTY * color.red, RED_CHANNEL);
pwm_set_duty(MAX_DUTY * color.green, GREEN_CHANNEL);
pwm_set_duty(MAX_DUTY * color.blue, BLUE_CHANNEL);
pwm_start();
on = true;
}
void ICACHE_FLASH_ATTR led_turn_off()
{
pwm_set_duty(0, RED_CHANNEL);
pwm_set_duty(0, GREEN_CHANNEL);
pwm_set_duty(0, BLUE_CHANNEL);
pwm_start();
on = false;
}
void ICACHE_FLASH_ATTR led_set_blink(uint16_t period)
{
if (period == 0) {
os_timer_disarm(&blink_timer);
} else {
os_timer_setfn(&blink_timer, blink_callback, NULL);
os_timer_arm(&blink_timer, period, true);
}
}
static void blink_callback(void *arg)
{
on ? led_turn_off() : led_turn_on();
}