1 /* Mesh Manual Networking Example
2
3 This example code is in the Public Domain (or CC0 licensed, at your option.)
4
5 Unless required by applicable law or agreed to in writing, this
6 software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
7 CONDITIONS OF ANY KIND, either express or implied.
8 */
9
10 #include <string.h>
11 #include "esp_err.h"
12 #include "esp_mesh.h"
13 #include "mesh_light.h"
14 #include "driver/gpio.h"
15 #include "driver/ledc.h"
16
17 /*******************************************************
18 * Constants
19 *******************************************************/
20 /* RGB configuration on ESP-WROVER-KIT board */
21 #define LEDC_IO_0 (0)
22 #define LEDC_IO_1 (2)
23 #define LEDC_IO_2 (4)
24 #define LEDC_IO_3 (5)
25
26 /*******************************************************
27 * Variable Definitions
28 *******************************************************/
29 static bool s_light_inited = false;
30
31 /*******************************************************
32 * Function Definitions
33 *******************************************************/
mesh_light_init(void)34 esp_err_t mesh_light_init(void)
35 {
36 if (s_light_inited == true) {
37 return ESP_OK;
38 }
39 s_light_inited = true;
40
41 ledc_timer_config_t ledc_timer = {
42 .duty_resolution = LEDC_TIMER_13_BIT,
43 .freq_hz = 5000,
44 .speed_mode = LEDC_LOW_SPEED_MODE,
45 .timer_num = LEDC_TIMER_0,
46 .clk_cfg = LEDC_AUTO_CLK,
47 };
48 #ifdef CONFIG_IDF_TARGET_ESP32
49 ledc_timer.speed_mode = LEDC_HIGH_SPEED_MODE;
50 #endif
51 ledc_timer_config(&ledc_timer);
52
53 ledc_channel_config_t ledc_channel = {
54 .channel = LEDC_CHANNEL_0,
55 .duty = 100,
56 .gpio_num = LEDC_IO_0,
57 .intr_type = LEDC_INTR_FADE_END,
58 .speed_mode = LEDC_LOW_SPEED_MODE,
59 .timer_sel = LEDC_TIMER_0,
60 .hpoint = 0,
61 };
62 ledc_channel_config(&ledc_channel);
63 ledc_channel.channel = LEDC_CHANNEL_1;
64 ledc_channel.gpio_num = LEDC_IO_1;
65 ledc_channel_config(&ledc_channel);
66 ledc_channel.channel = LEDC_CHANNEL_2;
67 ledc_channel.gpio_num = LEDC_IO_2;
68 ledc_channel_config(&ledc_channel);
69 ledc_channel.channel = LEDC_CHANNEL_3;
70 ledc_channel.gpio_num = LEDC_IO_3;
71 ledc_channel_config(&ledc_channel);
72 ledc_fade_func_install(0);
73
74 mesh_light_set(MESH_LIGHT_INIT);
75 return ESP_OK;
76 }
77
mesh_light_set(int color)78 esp_err_t mesh_light_set(int color)
79 {
80 switch (color) {
81 case MESH_LIGHT_RED:
82 /* Red */
83 ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, 3000);
84 ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1, 0);
85 ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2, 0);
86 break;
87 case MESH_LIGHT_GREEN:
88 /* Green */
89 ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, 0);
90 ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1, 3000);
91 ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2, 0);
92 break;
93 case MESH_LIGHT_BLUE:
94 /* Blue */
95 ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, 0);
96 ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1, 0);
97 ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2, 3000);
98 break;
99 case MESH_LIGHT_YELLOW:
100 /* Yellow */
101 ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, 3000);
102 ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1, 3000);
103 ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2, 0);
104 break;
105 case MESH_LIGHT_PINK:
106 /* Pink */
107 ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, 3000);
108 ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1, 0);
109 ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2, 3000);
110 break;
111 case MESH_LIGHT_INIT:
112 /* can't say */
113 ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, 0);
114 ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1, 3000);
115 ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2, 3000);
116 break;
117 case MESH_LIGHT_WARNING:
118 /* warning */
119 ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, 3000);
120 ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1, 3000);
121 ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2, 3000);
122 break;
123 default:
124 /* off */
125 ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, 0);
126 ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1, 0);
127 ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2, 0);
128 }
129
130 ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0);
131 ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1);
132 ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2);
133
134 return ESP_OK;
135 }
136
mesh_connected_indicator(int layer)137 void mesh_connected_indicator(int layer)
138 {
139 switch (layer) {
140 case 1:
141 mesh_light_set(MESH_LIGHT_PINK);
142 break;
143 case 2:
144 mesh_light_set(MESH_LIGHT_YELLOW);
145 break;
146 case 3:
147 mesh_light_set(MESH_LIGHT_RED);
148 break;
149 case 4:
150 mesh_light_set(MESH_LIGHT_BLUE);
151 break;
152 case 5:
153 mesh_light_set(MESH_LIGHT_GREEN);
154 break;
155 case 6:
156 mesh_light_set(MESH_LIGHT_WARNING);
157 break;
158 default:
159 mesh_light_set(0);
160 }
161 }
162
mesh_disconnected_indicator(void)163 void mesh_disconnected_indicator(void)
164 {
165 mesh_light_set(MESH_LIGHT_WARNING);
166 }
167
mesh_light_process(mesh_addr_t * from,uint8_t * buf,uint16_t len)168 esp_err_t mesh_light_process(mesh_addr_t *from, uint8_t *buf, uint16_t len)
169 {
170 mesh_light_ctl_t *in = (mesh_light_ctl_t *) buf;
171 if (!from || !buf || len < sizeof(mesh_light_ctl_t)) {
172 return ESP_FAIL;
173 }
174 if (in->token_id != MESH_TOKEN_ID || in->token_value != MESH_TOKEN_VALUE) {
175 return ESP_FAIL;
176 }
177 if (in->cmd == MESH_CONTROL_CMD) {
178 if (in->on) {
179 mesh_connected_indicator(esp_mesh_get_layer());
180 } else {
181 mesh_light_set(0);
182 }
183 }
184 return ESP_OK;
185 }
186