OVMS3-idf/examples/mesh/manual_networking/main/mesh_light.c
qiyueixa feaa6e8a8f mesh: add manual networking function
1. add mesh_assoc_t to esp_mesh_internal.h file.
2. rename "node" to "device" in esp_mesh.h.
3. add MESH_EVENT_SCAN_DONE event.
4. add APIs esp_mesh_scan_get_ap_record() and esp_mesh_scan_get_ap_ie_len() to get scan results.
5. modify API esp_mesh_set_self_organized() by adding parameter "select_parent".
6. modify API esp_mesh_set_parent() by adding parameter "parent_mesh_id"
7. add manual networking example.
2018-05-23 18:09:12 +08:00

180 lines
5.5 KiB
C

/* Mesh Manual Networking Example
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#include <string.h>
#include "esp_err.h"
#include "esp_mesh.h"
#include "mesh_light.h"
#include "driver/gpio.h"
#include "driver/ledc.h"
/*******************************************************
* Constants
*******************************************************/
/* RGB configuration on ESP-WROVER-KIT board */
#define LEDC_IO_0 (0)
#define LEDC_IO_1 (2)
#define LEDC_IO_2 (4)
#define LEDC_IO_3 (5)
/*******************************************************
* Variable Definitions
*******************************************************/
static bool s_light_inited = false;
/*******************************************************
* Function Definitions
*******************************************************/
esp_err_t mesh_light_init(void)
{
if (s_light_inited == true) {
return ESP_OK;
}
s_light_inited = true;
ledc_timer_config_t ledc_timer = {
.bit_num = LEDC_TIMER_13_BIT,
.freq_hz = 5000,
.speed_mode = LEDC_HIGH_SPEED_MODE,
.timer_num = LEDC_TIMER_0
};
ledc_timer_config(&ledc_timer);
ledc_channel_config_t ledc_channel = {
.channel = LEDC_CHANNEL_0,
.duty = 100,
.gpio_num = LEDC_IO_0,
.intr_type = LEDC_INTR_FADE_END,
.speed_mode = LEDC_HIGH_SPEED_MODE,
.timer_sel = LEDC_TIMER_0
};
ledc_channel_config(&ledc_channel);
ledc_channel.channel = LEDC_CHANNEL_1;
ledc_channel.gpio_num = LEDC_IO_1;
ledc_channel_config(&ledc_channel);
ledc_channel.channel = LEDC_CHANNEL_2;
ledc_channel.gpio_num = LEDC_IO_2;
ledc_channel_config(&ledc_channel);
ledc_channel.channel = LEDC_CHANNEL_3;
ledc_channel.gpio_num = LEDC_IO_3;
ledc_channel_config(&ledc_channel);
ledc_fade_func_install(0);
mesh_light_set(MESH_LIGHT_INIT);
return ESP_OK;
}
esp_err_t mesh_light_set(int color)
{
switch (color) {
case MESH_LIGHT_RED:
/* Red */
ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0, 3000);
ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1, 0);
ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_2, 0);
break;
case MESH_LIGHT_GREEN:
/* Green */
ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0, 0);
ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1, 3000);
ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_2, 0);
break;
case MESH_LIGHT_BLUE:
/* Blue */
ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0, 0);
ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1, 0);
ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_2, 3000);
break;
case MESH_LIGHT_YELLOW:
/* Yellow */
ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0, 3000);
ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1, 3000);
ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_2, 0);
break;
case MESH_LIGHT_PINK:
/* Pink */
ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0, 3000);
ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1, 0);
ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_2, 3000);
break;
case MESH_LIGHT_INIT:
/* can't say */
ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0, 0);
ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1, 3000);
ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_2, 3000);
break;
case MESH_LIGHT_WARNING:
/* warning */
ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0, 3000);
ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1, 3000);
ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_2, 3000);
break;
default:
/* off */
ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0, 0);
ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1, 0);
ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_2, 0);
}
ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0);
ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1);
ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_2);
return ESP_OK;
}
void mesh_connected_indicator(int layer)
{
switch (layer) {
case 1:
mesh_light_set(MESH_LIGHT_PINK);
break;
case 2:
mesh_light_set(MESH_LIGHT_YELLOW);
break;
case 3:
mesh_light_set(MESH_LIGHT_RED);
break;
case 4:
mesh_light_set(MESH_LIGHT_BLUE);
break;
case 5:
mesh_light_set(MESH_LIGHT_GREEN);
break;
case 6:
mesh_light_set(MESH_LIGHT_WARNING);
break;
default:
mesh_light_set(0);
}
}
void mesh_disconnected_indicator(void)
{
mesh_light_set(MESH_LIGHT_WARNING);
}
esp_err_t mesh_light_process(mesh_addr_t *from, uint8_t *buf, uint16_t len)
{
mesh_light_ctl_t *in = (mesh_light_ctl_t *) buf;
if (!from || !buf || len < sizeof(mesh_light_ctl_t)) {
return ESP_FAIL;
}
if (in->token_id != MESH_TOKEN_ID || in->token_value != MESH_TOKEN_VALUE) {
return ESP_FAIL;
}
if (in->cmd == MESH_CONTROL_CMD) {
if (in->on) {
mesh_connected_indicator(esp_mesh_get_layer());
} else {
mesh_light_set(0);
}
}
return ESP_OK;
}