1 /* Mesh Internal Communication 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 ledc_timer_config(&ledc_timer);
49
50 ledc_channel_config_t ledc_channel = {
51 .channel = LEDC_CHANNEL_0,
52 .duty = 100,
53 .gpio_num = LEDC_IO_0,
54 .intr_type = LEDC_INTR_FADE_END,
55 .speed_mode = LEDC_LOW_SPEED_MODE,
56 .timer_sel = LEDC_TIMER_0,
57 .hpoint = 0,
58 };
59 ledc_channel_config(&ledc_channel);
60 ledc_channel.channel = LEDC_CHANNEL_1;
61 ledc_channel.gpio_num = LEDC_IO_1;
62 ledc_channel_config(&ledc_channel);
63 ledc_channel.channel = LEDC_CHANNEL_2;
64 ledc_channel.gpio_num = LEDC_IO_2;
65 ledc_channel_config(&ledc_channel);
66 ledc_channel.channel = LEDC_CHANNEL_3;
67 ledc_channel.gpio_num = LEDC_IO_3;
68 ledc_channel_config(&ledc_channel);
69 ledc_fade_func_install(0);
70
71 mesh_light_set(MESH_LIGHT_INIT);
72 return ESP_OK;
73 }
74
mesh_light_set(int color)75 esp_err_t mesh_light_set(int color)
76 {
77 switch (color) {
78 case MESH_LIGHT_RED:
79 /* Red */
80 ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, 3000);
81 ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1, 0);
82 ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2, 0);
83 break;
84 case MESH_LIGHT_GREEN:
85 /* Green */
86 ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, 0);
87 ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1, 3000);
88 ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2, 0);
89 break;
90 case MESH_LIGHT_BLUE:
91 /* Blue */
92 ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, 0);
93 ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1, 0);
94 ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2, 3000);
95 break;
96 case MESH_LIGHT_YELLOW:
97 /* Yellow */
98 ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, 3000);
99 ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1, 3000);
100 ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2, 0);
101 break;
102 case MESH_LIGHT_PINK:
103 /* Pink */
104 ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, 3000);
105 ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1, 0);
106 ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2, 3000);
107 break;
108 case MESH_LIGHT_INIT:
109 /* can't say */
110 ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, 0);
111 ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1, 3000);
112 ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2, 3000);
113 break;
114 case MESH_LIGHT_WARNING:
115 /* warning */
116 ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, 3000);
117 ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1, 3000);
118 ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2, 3000);
119 break;
120 default:
121 /* off */
122 ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, 0);
123 ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1, 0);
124 ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2, 0);
125 }
126
127 ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0);
128 ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1);
129 ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2);
130
131 return ESP_OK;
132 }
133
mesh_connected_indicator(int layer)134 void mesh_connected_indicator(int layer)
135 {
136 switch (layer) {
137 case 1:
138 mesh_light_set(MESH_LIGHT_PINK);
139 break;
140 case 2:
141 mesh_light_set(MESH_LIGHT_YELLOW);
142 break;
143 case 3:
144 mesh_light_set(MESH_LIGHT_RED);
145 break;
146 case 4:
147 mesh_light_set(MESH_LIGHT_BLUE);
148 break;
149 case 5:
150 mesh_light_set(MESH_LIGHT_GREEN);
151 break;
152 case 6:
153 mesh_light_set(MESH_LIGHT_WARNING);
154 break;
155 default:
156 mesh_light_set(0);
157 }
158 }
159
mesh_disconnected_indicator(void)160 void mesh_disconnected_indicator(void)
161 {
162 mesh_light_set(MESH_LIGHT_WARNING);
163 }
164
mesh_light_process(mesh_addr_t * from,uint8_t * buf,uint16_t len)165 esp_err_t mesh_light_process(mesh_addr_t *from, uint8_t *buf, uint16_t len)
166 {
167 mesh_light_ctl_t *in = (mesh_light_ctl_t *) buf;
168 if (!from || !buf || len < sizeof(mesh_light_ctl_t)) {
169 return ESP_FAIL;
170 }
171 if (in->token_id != MESH_TOKEN_ID || in->token_value != MESH_TOKEN_VALUE) {
172 return ESP_FAIL;
173 }
174 if (in->cmd == MESH_CONTROL_CMD) {
175 if (in->on) {
176 mesh_connected_indicator(esp_mesh_get_layer());
177 } else {
178 mesh_light_set(0);
179 }
180 }
181 return ESP_OK;
182 }
183