-
Notifications
You must be signed in to change notification settings - Fork 1
/
webthing_rgbw_funs.c
136 lines (117 loc) · 3.96 KB
/
webthing_rgbw_funs.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
/* *********************************************************
* LED RGB+White controller - additional functions
* Compatible with Web Thing API
*
* Created on: Apr 5, 2021
* Last update: Apr 5, 2021
* Author: Krzysztof Zurek
* E-mail: [email protected]
www: www.alfa46.com
*
************************************************************/
#include <inttypes.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/timers.h"
#include "esp_system.h"
#include "driver/ledc.h"
#include "nvs_flash.h"
#include "esp_log.h"
#include "simple_web_thing_server.h"
#include "webthing_rgbw.h"
/****************************************************
*
* jsonize model of RGB color property
*
* **************************************************/
char *color_model_jsonize(property_t *p){
char *buff;
//only unit printed in model
buff = malloc(12 + strlen(p -> unit));
sprintf(buff, "\"unit\":\"%s\",", p -> unit);
return buff;
}
/****************************************************
*
* RGB color jsonize
*
* **************************************************/
char *color_value_jsonize(property_t *p){
char *buff;
char col_str[] = "\"%s\":\"%s\"";
buff = malloc(32);
memset(buff, 0 , 32);
sprintf(buff, col_str, p -> id, p -> value);
return buff;
}
/*******************************************************************
*
* initialize GPIOs for channel A and B, both switch OFF
*
* ******************************************************************/
void init_ledc(void){
//timer configuration
ledc_timer_config_t ledc_timer = {
.duty_resolution = LEDC_TIMER_13_BIT, // resolution of PWM duty
.freq_hz = 1000, // frequency of PWM signal
.speed_mode = LEDC_HIGH_SPEED_MODE, // timer mode
.timer_num = LEDC_TIMER_0, // timer index
.clk_cfg = LEDC_AUTO_CLK, // Auto select the source clock
};
// Set configuration of timer0 for high speed channels
ledc_timer_config(&ledc_timer);
//channel configuration
//---- channel RED
ledc_channel_config_t channel_red = {
.channel = LEDC_CH_RED,
.duty = 0,
.gpio_num = GPIO_CH_RED,
.speed_mode = LEDC_HIGH_SPEED_MODE,
.hpoint = 0,
.timer_sel = LEDC_TIMER_0,
.intr_type = LEDC_INTR_DISABLE,
};
// Set LED Controller with previously prepared configuration
ledc_channel_config(&channel_red);
//---- channel GREEN
ledc_channel_config_t channel_green = {
.channel = LEDC_CH_GREEN,
.duty = 0,
.gpio_num = GPIO_CH_GREEN,
.speed_mode = LEDC_HIGH_SPEED_MODE,
.hpoint = 0,
.timer_sel = LEDC_TIMER_0,
.intr_type = LEDC_INTR_DISABLE,
};
// Set LED Controller with previously prepared configuration
ledc_channel_config(&channel_green);
//---- channel BLUE
ledc_channel_config_t channel_blue = {
.channel = LEDC_CH_BLUE,
.duty = 0,
.gpio_num = GPIO_CH_BLUE,
.speed_mode = LEDC_HIGH_SPEED_MODE,
.hpoint = 0,
.timer_sel = LEDC_TIMER_0,
.intr_type = LEDC_INTR_DISABLE,
};
// Set LED Controller with previously prepared configuration
ledc_channel_config(&channel_blue);
//---- channel BLUE
ledc_channel_config_t channel_white = {
.channel = LEDC_CH_WHITE,
.duty = 0,
.gpio_num = GPIO_CH_WHITE,
.speed_mode = LEDC_HIGH_SPEED_MODE,
.hpoint = 0,
.timer_sel = LEDC_TIMER_0,
.intr_type = LEDC_INTR_DISABLE,
};
// Set LED Controller with previously prepared configuration
ledc_channel_config(&channel_white);
// Initialize fade service.
ledc_fade_func_install(ESP_INTR_FLAG_IRAM);
}