Compare commits
6 Commits
esp-idf-4.
...
0f5e2a9664
Author | SHA1 | Date | |
---|---|---|---|
0f5e2a9664 | |||
05660afc54 | |||
101103de03 | |||
7bff8e2c17 | |||
2da0f224a1 | |||
f0a1dae657 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -2,3 +2,4 @@ build/
|
|||||||
sdkconfig
|
sdkconfig
|
||||||
sdkconfig.old
|
sdkconfig.old
|
||||||
.vscode/
|
.vscode/
|
||||||
|
managed_components/
|
@@ -4,7 +4,5 @@
|
|||||||
# CMakeLists in this exact order for cmake to work correctly
|
# CMakeLists in this exact order for cmake to work correctly
|
||||||
cmake_minimum_required(VERSION 3.5)
|
cmake_minimum_required(VERSION 3.5)
|
||||||
|
|
||||||
set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/led_strip)
|
|
||||||
|
|
||||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||||
project(display-ambient-light-board)
|
project(display-ambient-light-board)
|
@@ -1,9 +1,15 @@
|
|||||||
dependencies:
|
dependencies:
|
||||||
|
espressif/mdns:
|
||||||
|
component_hash: 3f3bf8ff67ec99dde4e935637af087ebc899b9fee396ffa1c9138d3775f1aca4
|
||||||
|
source:
|
||||||
|
service_url: https://api.components.espressif.com/
|
||||||
|
type: service
|
||||||
|
version: 1.0.9
|
||||||
idf:
|
idf:
|
||||||
component_hash: null
|
component_hash: null
|
||||||
source:
|
source:
|
||||||
type: idf
|
type: idf
|
||||||
version: 4.4.4
|
version: 5.0.1
|
||||||
manifest_hash: dcf4d39b94252de130019eadceb989d72b0dbc26b552cfdcbb50f6da531d2b92
|
manifest_hash: d23f3b15e8b95dfe5da6cf83768d24158a3a0222f95694b1fa09c3ca8602f79f
|
||||||
target: esp32c3
|
target: esp32c3
|
||||||
version: 1.0.0
|
version: 1.0.0
|
||||||
|
17
docs/udp.md
Normal file
17
docs/udp.md
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
# 基于 UDP 的上、下位机通讯规则
|
||||||
|
|
||||||
|
## 报文格式
|
||||||
|
|
||||||
|
开始的一个字节是命令,剩余字节是数据。缓冲区长度设为 1024 字节,所以单个 UDP 报文的数据最多为 1023 字节。
|
||||||
|
|
||||||
|
## 命令
|
||||||
|
|
||||||
|
### 上位机在线 `1`
|
||||||
|
|
||||||
|
### 更新灯带颜色 `2`
|
||||||
|
|
||||||
|
| 数据 | 长度(字节) | 说明 |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| 起始位置 | 2 | 0~65535 |
|
||||||
|
| 长度 | 2 | 0~65535 |
|
||||||
|
| 颜色 | 3 | RGB 顺序,$2^3 * 2^3 * 2^3 = 65535$ 真彩色 |
|
@@ -1,2 +1,2 @@
|
|||||||
idf_component_register(SRCS "hw-ms03.c" "app_nvs.c" "apds_9960.c" "pca9555.c" "i2c.c" "asr_pro.c" "ci_03t.c" "ui_input.c" "ambient_light.c" "temperature.c" "embedded_display.c" "mqtt.c" "main.c" "wifi.c" "light.c" "mqtt.c"
|
idf_component_register(SRCS "udp_server.c" "service_discovery.c" "app_nvs.c" "apds_9960.c" "pca9555.c" "i2c.c" "asr_pro.c" "ci_03t.c" "ui_input.c" "ambient_light.c" "temperature.c" "embedded_display.c" "mqtt.c" "main.c" "wifi.c" "light.c" "mqtt.c" "led_strip_encoder/led_strip_encoder.c"
|
||||||
INCLUDE_DIRS ".")
|
INCLUDE_DIRS ".")
|
@@ -120,9 +120,9 @@ void ambient_light_fetch(void* arg) {
|
|||||||
uint16_t als_ch0_raw;
|
uint16_t als_ch0_raw;
|
||||||
uint16_t als_ch1_raw;
|
uint16_t als_ch1_raw;
|
||||||
uint16_t proximity_raw;
|
uint16_t proximity_raw;
|
||||||
char als_ch0_str[10];
|
char als_ch0_str[20];
|
||||||
char als_ch1_str[10];
|
char als_ch1_str[20];
|
||||||
char proximity_str[10];
|
char proximity_str[20];
|
||||||
uint8_t als_ch0_buffer[] = {0, 0};
|
uint8_t als_ch0_buffer[] = {0, 0};
|
||||||
uint8_t als_ch1_buffer[] = {0, 0};
|
uint8_t als_ch1_buffer[] = {0, 0};
|
||||||
uint8_t proximity_buffer[] = {0, 0};
|
uint8_t proximity_buffer[] = {0, 0};
|
||||||
|
@@ -6,6 +6,7 @@
|
|||||||
#include "driver/i2c.h"
|
#include "driver/i2c.h"
|
||||||
#include "embedded_display.c"
|
#include "embedded_display.c"
|
||||||
#include "esp_log.h"
|
#include "esp_log.h"
|
||||||
|
#include "esp_timer.h"
|
||||||
#include "freertos/FreeRTOS.h"
|
#include "freertos/FreeRTOS.h"
|
||||||
#include "i2c.c"
|
#include "i2c.c"
|
||||||
|
|
||||||
@@ -128,7 +129,7 @@
|
|||||||
|
|
||||||
#define APDS_9960_TAG "APDS-9960"
|
#define APDS_9960_TAG "APDS-9960"
|
||||||
|
|
||||||
static xQueueHandle apds_9960_int_evt_queue = NULL;
|
static QueueHandle_t apds_9960_int_evt_queue = NULL;
|
||||||
|
|
||||||
static int64_t last_apds_9960_int_time = 0;
|
static int64_t last_apds_9960_int_time = 0;
|
||||||
|
|
||||||
|
@@ -1,27 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
#include "driver/gpio.h"
|
|
||||||
#include "freeRTOS/FreeRTOS.h"
|
|
||||||
|
|
||||||
#define HW_MS03_INT_GPIO 6
|
|
||||||
#define BEEP_GPIO 7
|
|
||||||
|
|
||||||
void hw_ms03_int_handler(void *arg) {
|
|
||||||
gpio_set_level(BEEP_GPIO, gpio_get_level(HW_MS03_INT_GPIO));
|
|
||||||
}
|
|
||||||
|
|
||||||
void hw_ms03_init() {
|
|
||||||
gpio_config_t io_conf = {};
|
|
||||||
|
|
||||||
io_conf.mode = GPIO_MODE_INPUT;
|
|
||||||
io_conf.pull_up_en = 0;
|
|
||||||
io_conf.pull_down_en = 1;
|
|
||||||
io_conf.intr_type = GPIO_INTR_ANYEDGE;
|
|
||||||
io_conf.pin_bit_mask = 1ULL << HW_MS03_INT_GPIO;
|
|
||||||
gpio_config(&io_conf);
|
|
||||||
io_conf.mode = GPIO_MODE_OUTPUT;
|
|
||||||
io_conf.pin_bit_mask = 1ULL << BEEP_GPIO;
|
|
||||||
io_conf.pull_down_en = 0;
|
|
||||||
gpio_config(&io_conf);
|
|
||||||
|
|
||||||
gpio_isr_handler_add(HW_MS03_INT_GPIO, hw_ms03_int_handler, NULL);
|
|
||||||
}
|
|
@@ -54,7 +54,7 @@ uint8_t i2c_check_slave_exists(uint8_t address) {
|
|||||||
i2c_master_start(cmd);
|
i2c_master_start(cmd);
|
||||||
i2c_master_write_byte(cmd, (address << 1) | I2C_MASTER_WRITE, ACK_CHECK_EN);
|
i2c_master_write_byte(cmd, (address << 1) | I2C_MASTER_WRITE, ACK_CHECK_EN);
|
||||||
i2c_master_stop(cmd);
|
i2c_master_stop(cmd);
|
||||||
esp_err_t ret = i2c_master_cmd_begin(I2C_NUM_0, cmd, 50 / portTICK_RATE_MS);
|
esp_err_t ret = i2c_master_cmd_begin(I2C_NUM_0, cmd, 50 / portTICK_PERIOD_MS);
|
||||||
i2c_cmd_link_delete(cmd);
|
i2c_cmd_link_delete(cmd);
|
||||||
|
|
||||||
if (ret == ESP_OK) {
|
if (ret == ESP_OK) {
|
||||||
|
17
main/idf_component.yml
Normal file
17
main/idf_component.yml
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
## IDF Component Manager Manifest File
|
||||||
|
dependencies:
|
||||||
|
espressif/mdns: "^1.0.9"
|
||||||
|
## Required IDF version
|
||||||
|
idf:
|
||||||
|
version: ">=5.0.1"
|
||||||
|
# # Put list of dependencies here
|
||||||
|
# # For components maintained by Espressif:
|
||||||
|
# component: "~1.0.0"
|
||||||
|
# # For 3rd party components:
|
||||||
|
# username/component: ">=1.0.0,<2.0.0"
|
||||||
|
# username2/component2:
|
||||||
|
# version: "~1.0.0"
|
||||||
|
# # For transient dependencies `public` flag can be set.
|
||||||
|
# # `public` flag doesn't have an effect dependencies of the `main` component.
|
||||||
|
# # All dependencies of `main` are public by default.
|
||||||
|
# public: true
|
141
main/led_strip_encoder/led_strip_encoder.c
Normal file
141
main/led_strip_encoder/led_strip_encoder.c
Normal file
@@ -0,0 +1,141 @@
|
|||||||
|
/*
|
||||||
|
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "led_strip_encoder.h"
|
||||||
|
|
||||||
|
#include "esp_check.h"
|
||||||
|
|
||||||
|
static const char *TAG = "led_encoder";
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
rmt_encoder_t base;
|
||||||
|
rmt_encoder_t *bytes_encoder;
|
||||||
|
rmt_encoder_t *copy_encoder;
|
||||||
|
int state;
|
||||||
|
rmt_symbol_word_t reset_code;
|
||||||
|
} rmt_led_strip_encoder_t;
|
||||||
|
|
||||||
|
static size_t rmt_encode_led_strip(rmt_encoder_t *encoder,
|
||||||
|
rmt_channel_handle_t channel,
|
||||||
|
const void *primary_data, size_t data_size,
|
||||||
|
rmt_encode_state_t *ret_state) {
|
||||||
|
rmt_led_strip_encoder_t *led_encoder =
|
||||||
|
__containerof(encoder, rmt_led_strip_encoder_t, base);
|
||||||
|
rmt_encoder_handle_t bytes_encoder = led_encoder->bytes_encoder;
|
||||||
|
rmt_encoder_handle_t copy_encoder = led_encoder->copy_encoder;
|
||||||
|
rmt_encode_state_t session_state = 0;
|
||||||
|
rmt_encode_state_t state = 0;
|
||||||
|
size_t encoded_symbols = 0;
|
||||||
|
switch (led_encoder->state) {
|
||||||
|
case 0: // send RGB data
|
||||||
|
encoded_symbols += bytes_encoder->encode(
|
||||||
|
bytes_encoder, channel, primary_data, data_size, &session_state);
|
||||||
|
if (session_state & RMT_ENCODING_COMPLETE) {
|
||||||
|
led_encoder->state =
|
||||||
|
1; // switch to next state when current encoding session finished
|
||||||
|
}
|
||||||
|
if (session_state & RMT_ENCODING_MEM_FULL) {
|
||||||
|
state |= RMT_ENCODING_MEM_FULL;
|
||||||
|
goto out; // yield if there's no free space for encoding artifacts
|
||||||
|
}
|
||||||
|
// fall-through
|
||||||
|
case 1: // send reset code
|
||||||
|
encoded_symbols +=
|
||||||
|
copy_encoder->encode(copy_encoder, channel, &led_encoder->reset_code,
|
||||||
|
sizeof(led_encoder->reset_code), &session_state);
|
||||||
|
if (session_state & RMT_ENCODING_COMPLETE) {
|
||||||
|
led_encoder->state = 0; // back to the initial encoding session
|
||||||
|
state |= RMT_ENCODING_COMPLETE;
|
||||||
|
}
|
||||||
|
if (session_state & RMT_ENCODING_MEM_FULL) {
|
||||||
|
state |= RMT_ENCODING_MEM_FULL;
|
||||||
|
goto out; // yield if there's no free space for encoding artifacts
|
||||||
|
}
|
||||||
|
}
|
||||||
|
out:
|
||||||
|
*ret_state = state;
|
||||||
|
return encoded_symbols;
|
||||||
|
}
|
||||||
|
|
||||||
|
static esp_err_t rmt_del_led_strip_encoder(rmt_encoder_t *encoder) {
|
||||||
|
rmt_led_strip_encoder_t *led_encoder =
|
||||||
|
__containerof(encoder, rmt_led_strip_encoder_t, base);
|
||||||
|
rmt_del_encoder(led_encoder->bytes_encoder);
|
||||||
|
rmt_del_encoder(led_encoder->copy_encoder);
|
||||||
|
free(led_encoder);
|
||||||
|
return ESP_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
static esp_err_t rmt_led_strip_encoder_reset(rmt_encoder_t *encoder) {
|
||||||
|
rmt_led_strip_encoder_t *led_encoder =
|
||||||
|
__containerof(encoder, rmt_led_strip_encoder_t, base);
|
||||||
|
rmt_encoder_reset(led_encoder->bytes_encoder);
|
||||||
|
rmt_encoder_reset(led_encoder->copy_encoder);
|
||||||
|
led_encoder->state = 0;
|
||||||
|
return ESP_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
esp_err_t rmt_new_led_strip_encoder(const led_strip_encoder_config_t *config,
|
||||||
|
rmt_encoder_handle_t *ret_encoder) {
|
||||||
|
esp_err_t ret = ESP_OK;
|
||||||
|
rmt_led_strip_encoder_t *led_encoder = NULL;
|
||||||
|
ESP_GOTO_ON_FALSE(config && ret_encoder, ESP_ERR_INVALID_ARG, err, TAG,
|
||||||
|
"invalid argument");
|
||||||
|
led_encoder = calloc(1, sizeof(rmt_led_strip_encoder_t));
|
||||||
|
ESP_GOTO_ON_FALSE(led_encoder, ESP_ERR_NO_MEM, err, TAG,
|
||||||
|
"no mem for led strip encoder");
|
||||||
|
led_encoder->base.encode = rmt_encode_led_strip;
|
||||||
|
led_encoder->base.del = rmt_del_led_strip_encoder;
|
||||||
|
led_encoder->base.reset = rmt_led_strip_encoder_reset;
|
||||||
|
// different led strip might have its own timing requirements, following
|
||||||
|
// parameter is for WS2812
|
||||||
|
rmt_bytes_encoder_config_t bytes_encoder_config = {
|
||||||
|
.bit0 =
|
||||||
|
{
|
||||||
|
.level0 = 1,
|
||||||
|
.duration0 = 0.3 * config->resolution / 1000000, // T0H=0.3us
|
||||||
|
.level1 = 0,
|
||||||
|
.duration1 = 0.9 * config->resolution / 1000000, // T0L=0.9us
|
||||||
|
},
|
||||||
|
.bit1 =
|
||||||
|
{
|
||||||
|
.level0 = 1,
|
||||||
|
.duration0 = 0.9 * config->resolution / 1000000, // T1H=0.9us
|
||||||
|
.level1 = 0,
|
||||||
|
.duration1 = 0.3 * config->resolution / 1000000, // T1L=0.3us
|
||||||
|
},
|
||||||
|
.flags.msb_first = 1 // WS2812 transfer bit order: G7...G0R7...R0B7...B0
|
||||||
|
};
|
||||||
|
ESP_GOTO_ON_ERROR(
|
||||||
|
rmt_new_bytes_encoder(&bytes_encoder_config, &led_encoder->bytes_encoder),
|
||||||
|
err, TAG, "create bytes encoder failed");
|
||||||
|
rmt_copy_encoder_config_t copy_encoder_config = {};
|
||||||
|
ESP_GOTO_ON_ERROR(
|
||||||
|
rmt_new_copy_encoder(©_encoder_config, &led_encoder->copy_encoder),
|
||||||
|
err, TAG, "create copy encoder failed");
|
||||||
|
|
||||||
|
uint32_t reset_ticks = config->resolution / 1000000 * 50 /
|
||||||
|
2; // reset code duration defaults to 50us
|
||||||
|
led_encoder->reset_code = (rmt_symbol_word_t){
|
||||||
|
.level0 = 0,
|
||||||
|
.duration0 = reset_ticks,
|
||||||
|
.level1 = 0,
|
||||||
|
.duration1 = reset_ticks,
|
||||||
|
};
|
||||||
|
*ret_encoder = &led_encoder->base;
|
||||||
|
return ESP_OK;
|
||||||
|
err:
|
||||||
|
if (led_encoder) {
|
||||||
|
if (led_encoder->bytes_encoder) {
|
||||||
|
rmt_del_encoder(led_encoder->bytes_encoder);
|
||||||
|
}
|
||||||
|
if (led_encoder->copy_encoder) {
|
||||||
|
rmt_del_encoder(led_encoder->copy_encoder);
|
||||||
|
}
|
||||||
|
free(led_encoder);
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
38
main/led_strip_encoder/led_strip_encoder.h
Normal file
38
main/led_strip_encoder/led_strip_encoder.h
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
/*
|
||||||
|
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*/
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#include "driver/rmt_encoder.h"
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Type of led strip encoder configuration
|
||||||
|
*/
|
||||||
|
typedef struct {
|
||||||
|
uint32_t resolution; /*!< Encoder resolution, in Hz */
|
||||||
|
} led_strip_encoder_config_t;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Create RMT encoder for encoding LED strip pixels into RMT symbols
|
||||||
|
*
|
||||||
|
* @param[in] config Encoder configuration
|
||||||
|
* @param[out] ret_encoder Returned encoder handle
|
||||||
|
* @return
|
||||||
|
* - ESP_ERR_INVALID_ARG for any invalid arguments
|
||||||
|
* - ESP_ERR_NO_MEM out of memory when creating led strip encoder
|
||||||
|
* - ESP_OK if creating encoder successfully
|
||||||
|
*/
|
||||||
|
esp_err_t rmt_new_led_strip_encoder(const led_strip_encoder_config_t *config,
|
||||||
|
rmt_encoder_handle_t *ret_encoder);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
319
main/light.c
319
main/light.c
@@ -1,28 +1,22 @@
|
|||||||
/* RMT example -- RGB LED Strip
|
|
||||||
|
|
||||||
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.
|
|
||||||
*/
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "driver/rmt.h"
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "driver/rmt_tx.h"
|
||||||
#include "esp_log.h"
|
#include "esp_log.h"
|
||||||
#include "esp_system.h"
|
#include "esp_system.h"
|
||||||
#include "freertos/FreeRTOS.h"
|
#include "freertos/FreeRTOS.h"
|
||||||
#include "freertos/task.h"
|
#include "freertos/task.h"
|
||||||
#include "led_strip.h"
|
#include "led_strip_encoder/led_strip_encoder.h"
|
||||||
#include "nvs.h"
|
#include "nvs.h"
|
||||||
#include "nvs_flash.h"
|
#include "nvs_flash.h"
|
||||||
#include "sdkconfig.h"
|
#include "sdkconfig.h"
|
||||||
|
|
||||||
static const char *LIGHT_TAG = "DisplayAmbientLight_Light";
|
static const char *LIGHT_TAG = "DisplayAmbientLight_Light";
|
||||||
|
|
||||||
#define RMT_TX_CHANNEL RMT_CHANNEL_0
|
|
||||||
#define RMT_TX_GPIO 1
|
#define RMT_TX_GPIO 1
|
||||||
#define STRIP_LED_NUMBER CONFIG_NUMBER_OF_LEDS
|
#define STRIP_LED_NUMBER CONFIG_NUMBER_OF_LEDS
|
||||||
#define EXAMPLE_CHASE_SPEED_MS (10)
|
#define EXAMPLE_CHASE_SPEED_MS (10)
|
||||||
|
#define RMT_LED_STRIP_RESOLUTION_HZ 10000000
|
||||||
|
|
||||||
typedef enum light_mode_e {
|
typedef enum light_mode_e {
|
||||||
light_mode_init = 0,
|
light_mode_init = 0,
|
||||||
@@ -34,7 +28,13 @@ typedef enum light_mode_e {
|
|||||||
light_mode_off = 6,
|
light_mode_off = 6,
|
||||||
} light_mode_t;
|
} light_mode_t;
|
||||||
|
|
||||||
led_strip_t *light_led_strip;
|
rmt_channel_handle_t led_chan = NULL;
|
||||||
|
static uint8_t led_strip_pixels[STRIP_LED_NUMBER * 3];
|
||||||
|
rmt_encoder_handle_t led_encoder = NULL;
|
||||||
|
rmt_transmit_config_t tx_config = {
|
||||||
|
.loop_count = 0, // no transfer loop
|
||||||
|
};
|
||||||
|
|
||||||
light_mode_t light_mode;
|
light_mode_t light_mode;
|
||||||
float display_ambient_light_brightness = 1;
|
float display_ambient_light_brightness = 1;
|
||||||
uint8_t display_ambient_lighting_level = 255;
|
uint8_t display_ambient_lighting_level = 255;
|
||||||
@@ -85,6 +85,8 @@ void led_strip_set_color_calibration(float red, float green, float blue) {
|
|||||||
led_strip_green_calibration = green;
|
led_strip_green_calibration = green;
|
||||||
led_strip_blue_calibration = blue;
|
led_strip_blue_calibration = blue;
|
||||||
|
|
||||||
|
ESP_LOGI(LIGHT_TAG, "Calibration: %f %f %f", red, green, blue);
|
||||||
|
|
||||||
nvs_handle_t local_nvs_handle;
|
nvs_handle_t local_nvs_handle;
|
||||||
|
|
||||||
esp_err_t err = nvs_open("storage", NVS_READWRITE, &local_nvs_handle);
|
esp_err_t err = nvs_open("storage", NVS_READWRITE, &local_nvs_handle);
|
||||||
@@ -126,6 +128,14 @@ void led_strip_set_color_calibration(float red, float green, float blue) {
|
|||||||
nvs_close(local_nvs_handle);
|
nvs_close(local_nvs_handle);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void light_strip_transmit_task(void *pvParameter) {
|
||||||
|
while (1) {
|
||||||
|
ESP_ERROR_CHECK(rmt_transmit(led_chan, led_encoder, led_strip_pixels,
|
||||||
|
sizeof(led_strip_pixels), &tx_config));
|
||||||
|
vTaskDelay(pdMS_TO_TICKS(10));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Simple helper function, converting HSV color space to RGB color
|
* @brief Simple helper function, converting HSV color space to RGB color
|
||||||
* space
|
* space
|
||||||
@@ -179,52 +189,51 @@ void led_strip_hsv2rgb(uint32_t h, uint32_t s, uint32_t v, uint32_t *r,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void update_desktop_connection_state() {
|
// void update_desktop_connection_state() {
|
||||||
static uint8_t tick = 0;
|
// static uint8_t tick = 0;
|
||||||
|
|
||||||
bool beat = tick / 10 % 2 ? 1 : 0;
|
// bool beat = tick / 10 % 2 ? 1 : 0;
|
||||||
|
|
||||||
switch (light_mode) {
|
// switch (light_mode) {
|
||||||
case light_mode_desktop_online:
|
// case light_mode_desktop_online:
|
||||||
if (beat) {
|
// if (beat) {
|
||||||
ESP_ERROR_CHECK(
|
// led_strip_pixels[0] = 0;
|
||||||
light_led_strip->set_pixel(light_led_strip, 0, 10, 10, 10));
|
// led_strip_pixels[1] = 0;
|
||||||
}
|
// led_strip_pixels[2] = 0;
|
||||||
ESP_ERROR_CHECK(
|
// }
|
||||||
light_led_strip->set_pixel(light_led_strip, 1, 10, 10, 10));
|
// led_strip_pixels[3] = 10
|
||||||
ESP_ERROR_CHECK(
|
// break;
|
||||||
light_led_strip->set_pixel(light_led_strip, 2, 10, 10, 10));
|
// case light_mode_mqtt_connected:
|
||||||
break;
|
// if (beat) {
|
||||||
case light_mode_mqtt_connected:
|
// ESP_ERROR_CHECK(
|
||||||
if (beat) {
|
// light_led_strip->set_pixel(light_led_strip, 0, 10, 10, 10));
|
||||||
ESP_ERROR_CHECK(
|
// ESP_ERROR_CHECK(
|
||||||
light_led_strip->set_pixel(light_led_strip, 0, 10, 10, 10));
|
// light_led_strip->set_pixel(light_led_strip, 1, 10, 10, 10));
|
||||||
ESP_ERROR_CHECK(
|
// }
|
||||||
light_led_strip->set_pixel(light_led_strip, 1, 10, 10, 10));
|
// ESP_ERROR_CHECK(
|
||||||
}
|
// light_led_strip->set_pixel(light_led_strip, 2, 22, 22, 22));
|
||||||
ESP_ERROR_CHECK(
|
// break;
|
||||||
light_led_strip->set_pixel(light_led_strip, 2, 22, 22, 22));
|
// case light_mode_idle:
|
||||||
break;
|
// if (beat) {
|
||||||
case light_mode_idle:
|
// ESP_ERROR_CHECK(
|
||||||
if (beat) {
|
// light_led_strip->set_pixel(light_led_strip, 0, 77, 77, 77));
|
||||||
ESP_ERROR_CHECK(
|
// ESP_ERROR_CHECK(
|
||||||
light_led_strip->set_pixel(light_led_strip, 0, 77, 77, 77));
|
// light_led_strip->set_pixel(light_led_strip, 1, 77, 77, 77));
|
||||||
ESP_ERROR_CHECK(
|
// ESP_ERROR_CHECK(
|
||||||
light_led_strip->set_pixel(light_led_strip, 1, 77, 77, 77));
|
// light_led_strip->set_pixel(light_led_strip, 2, 77, 77, 77));
|
||||||
ESP_ERROR_CHECK(
|
// }
|
||||||
light_led_strip->set_pixel(light_led_strip, 2, 77, 77, 77));
|
// break;
|
||||||
}
|
// default:
|
||||||
break;
|
// break;
|
||||||
default:
|
// }
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
tick++;
|
// tick++;
|
||||||
}
|
// }
|
||||||
|
|
||||||
void light_for_init() {
|
void light_for_init() {
|
||||||
ESP_LOGI(LIGHT_TAG, "light_for_init");
|
ESP_LOGI(LIGHT_TAG, "light_for_init");
|
||||||
ESP_ERROR_CHECK(light_led_strip->clear(light_led_strip, 100));
|
|
||||||
|
memset(led_strip_pixels, 0, sizeof(led_strip_pixels));
|
||||||
|
|
||||||
nvs_handle local_nvs_handle;
|
nvs_handle local_nvs_handle;
|
||||||
esp_err_t err = nvs_open("storage", NVS_READWRITE, &local_nvs_handle);
|
esp_err_t err = nvs_open("storage", NVS_READWRITE, &local_nvs_handle);
|
||||||
@@ -267,16 +276,17 @@ void light_for_init() {
|
|||||||
uint8_t init_r, init_g, init_b;
|
uint8_t init_r, init_g, init_b;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
for (uint8_t i = 0; i < 50; i++) {
|
for (uint8_t i = 0; i < 200; i++) {
|
||||||
init_r = (uint8_t)(r_f * (float)i);
|
init_r = (uint8_t)(r_f * (float)i);
|
||||||
init_g = (uint8_t)(g_f * (float)i);
|
init_g = (uint8_t)(g_f * (float)i);
|
||||||
init_b = (uint8_t)(b_f * (float)i);
|
init_b = (uint8_t)(b_f * (float)i);
|
||||||
|
|
||||||
for (int j = 0; j < STRIP_LED_NUMBER; j++) {
|
for (int j = 0; j < STRIP_LED_NUMBER; j++) {
|
||||||
ESP_ERROR_CHECK(light_led_strip->set_pixel(light_led_strip, j, init_r,
|
led_strip_pixels[j * 3 + 0] = init_g;
|
||||||
init_g, init_b));
|
led_strip_pixels[j * 3 + 1] = init_r;
|
||||||
|
led_strip_pixels[j * 3 + 2] = init_b;
|
||||||
}
|
}
|
||||||
ESP_ERROR_CHECK(light_led_strip->refresh(light_led_strip, 100));
|
|
||||||
vTaskDelay(pdMS_TO_TICKS(20));
|
vTaskDelay(pdMS_TO_TICKS(20));
|
||||||
}
|
}
|
||||||
vTaskDelay(pdMS_TO_TICKS(100));
|
vTaskDelay(pdMS_TO_TICKS(100));
|
||||||
@@ -292,11 +302,21 @@ void light_for_connecting_wifi() {
|
|||||||
int8_t tick_tock = 0;
|
int8_t tick_tock = 0;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
ESP_ERROR_CHECK(
|
if (tick_tock) {
|
||||||
light_led_strip->set_pixel(light_led_strip, tick_tock, 150, 150, 0));
|
led_strip_pixels[0] = 150;
|
||||||
ESP_ERROR_CHECK(light_led_strip->set_pixel(light_led_strip,
|
led_strip_pixels[1] = 150;
|
||||||
(tick_tock + 1) % 2, 0, 200, 0));
|
led_strip_pixels[2] = 0;
|
||||||
ESP_ERROR_CHECK(light_led_strip->refresh(light_led_strip, 100));
|
led_strip_pixels[3] = 200;
|
||||||
|
led_strip_pixels[4] = 0;
|
||||||
|
led_strip_pixels[5] = 0;
|
||||||
|
} else {
|
||||||
|
led_strip_pixels[0] = 200;
|
||||||
|
led_strip_pixels[1] = 0;
|
||||||
|
led_strip_pixels[2] = 0;
|
||||||
|
led_strip_pixels[3] = 150;
|
||||||
|
led_strip_pixels[4] = 150;
|
||||||
|
led_strip_pixels[5] = 0;
|
||||||
|
}
|
||||||
tick_tock = !tick_tock;
|
tick_tock = !tick_tock;
|
||||||
vTaskDelay(pdMS_TO_TICKS(200));
|
vTaskDelay(pdMS_TO_TICKS(200));
|
||||||
} while (light_mode == light_mode_connection_wifi);
|
} while (light_mode == light_mode_connection_wifi);
|
||||||
@@ -304,7 +324,10 @@ void light_for_connecting_wifi() {
|
|||||||
|
|
||||||
void light_for_idle() {
|
void light_for_idle() {
|
||||||
ESP_LOGI(LIGHT_TAG, "light_for_idle");
|
ESP_LOGI(LIGHT_TAG, "light_for_idle");
|
||||||
ESP_ERROR_CHECK(light_led_strip->clear(light_led_strip, 100));
|
|
||||||
|
memset(led_strip_pixels, 0, sizeof(led_strip_pixels));
|
||||||
|
ESP_ERROR_CHECK(rmt_transmit(led_chan, led_encoder, led_strip_pixels,
|
||||||
|
sizeof(led_strip_pixels), &tx_config));
|
||||||
|
|
||||||
uint32_t red = 0, green = 0, blue = 0;
|
uint32_t red = 0, green = 0, blue = 0;
|
||||||
uint16_t step_length = 360 / STRIP_LED_NUMBER;
|
uint16_t step_length = 360 / STRIP_LED_NUMBER;
|
||||||
@@ -318,24 +341,21 @@ void light_for_idle() {
|
|||||||
// Build RGB values
|
// Build RGB values
|
||||||
led_strip_hsv2rgb(hue, 50, 30, &red, &green, &blue);
|
led_strip_hsv2rgb(hue, 50, 30, &red, &green, &blue);
|
||||||
|
|
||||||
red = red * display_ambient_light_brightness * led_strip_red_calibration;
|
led_strip_pixels[j * 3 + 0] = green * display_ambient_light_brightness *
|
||||||
green = green * display_ambient_light_brightness *
|
|
||||||
led_strip_green_calibration;
|
led_strip_green_calibration;
|
||||||
blue =
|
led_strip_pixels[j * 3 + 1] =
|
||||||
|
red * display_ambient_light_brightness * led_strip_red_calibration;
|
||||||
|
led_strip_pixels[j * 3 + 2] =
|
||||||
blue * display_ambient_light_brightness * led_strip_blue_calibration;
|
blue * display_ambient_light_brightness * led_strip_blue_calibration;
|
||||||
// Write RGB values to strip driver
|
|
||||||
ESP_ERROR_CHECK(
|
|
||||||
light_led_strip->set_pixel(light_led_strip, j, red, green, blue));
|
|
||||||
}
|
}
|
||||||
update_desktop_connection_state();
|
// update_desktop_connection_state();
|
||||||
ESP_ERROR_CHECK(light_led_strip->refresh(light_led_strip, 100));
|
|
||||||
vTaskDelay(pdMS_TO_TICKS(10));
|
vTaskDelay(pdMS_TO_TICKS(10));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void light_strip_running_task(void *pv_parameters) {
|
void light_strip_running_task(void *pv_parameters) {
|
||||||
while (true) {
|
while (true) {
|
||||||
if (!light_led_strip) {
|
if (!led_chan) {
|
||||||
ESP_LOGE(LIGHT_TAG, "install WS2812 driver failed 2");
|
ESP_LOGE(LIGHT_TAG, "install WS2812 driver failed 2");
|
||||||
}
|
}
|
||||||
switch (light_mode) {
|
switch (light_mode) {
|
||||||
@@ -359,69 +379,76 @@ void light_strip_running_task(void *pv_parameters) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void light_init_strip() {
|
void light_init_strip() {
|
||||||
rmt_config_t config = RMT_DEFAULT_CONFIG_TX(RMT_TX_GPIO, RMT_TX_CHANNEL);
|
rmt_tx_channel_config_t tx_chan_config = {
|
||||||
// set counter clock to 40MHz
|
.clk_src = RMT_CLK_SRC_DEFAULT, // select source clock
|
||||||
config.clk_div = 2;
|
.gpio_num = RMT_TX_GPIO,
|
||||||
|
.mem_block_symbols =
|
||||||
|
64, // increase the block size can make the LED less flickering
|
||||||
|
.resolution_hz = RMT_LED_STRIP_RESOLUTION_HZ,
|
||||||
|
.trans_queue_depth = 4, // set the number of transactions that can be
|
||||||
|
// pending in the background
|
||||||
|
};
|
||||||
|
ESP_ERROR_CHECK(rmt_new_tx_channel(&tx_chan_config, &led_chan));
|
||||||
|
|
||||||
ESP_ERROR_CHECK(rmt_config(&config));
|
ESP_LOGI(LIGHT_TAG, "Install led strip encoder");
|
||||||
ESP_ERROR_CHECK(rmt_driver_install(config.channel, 0, 0));
|
|
||||||
|
|
||||||
// install ws2812 driver
|
led_strip_encoder_config_t encoder_config = {
|
||||||
led_strip_config_t strip_config = LED_STRIP_DEFAULT_CONFIG(
|
.resolution = RMT_LED_STRIP_RESOLUTION_HZ,
|
||||||
STRIP_LED_NUMBER, (led_strip_dev_t)config.channel);
|
};
|
||||||
light_led_strip = led_strip_new_rmt_ws2812(&strip_config);
|
ESP_ERROR_CHECK(rmt_new_led_strip_encoder(&encoder_config, &led_encoder));
|
||||||
if (!light_led_strip) {
|
|
||||||
ESP_LOGE(LIGHT_TAG, "install WS2812 driver failed");
|
ESP_LOGI(LIGHT_TAG, "Enable RMT TX channel");
|
||||||
}
|
ESP_ERROR_CHECK(rmt_enable(led_chan));
|
||||||
// Clear LED strip (turn off all LEDs)
|
|
||||||
ESP_ERROR_CHECK(light_led_strip->clear(light_led_strip, 100));
|
ESP_LOGI(LIGHT_TAG, "Start LED rainbow chase");
|
||||||
// Show simple rainbow chasing pattern
|
|
||||||
ESP_LOGI(LIGHT_TAG, "LED Rainbow Chase Start");
|
|
||||||
|
|
||||||
light_mode = light_mode_init;
|
light_mode = light_mode_init;
|
||||||
|
|
||||||
xTaskCreate(light_strip_running_task, "LIGHT_STRIP_RUNNING_TASK", 4096, NULL,
|
xTaskCreate(light_strip_running_task, "LIGHT_STRIP_RUNNING_TASK", 4096, NULL,
|
||||||
1, NULL);
|
1, NULL);
|
||||||
|
|
||||||
|
xTaskCreate(light_strip_transmit_task, "LIGHT_STRIP_TRANSMIT_TASK", 4096,
|
||||||
|
NULL, 1, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
void light_play_colors(uint16_t len, uint8_t *buffer) {
|
void light_play_colors(uint16_t len, uint8_t *buffer) {
|
||||||
light_mode = light_mode_desktop_sending_colors;
|
// light_mode = light_mode_desktop_sending_colors;
|
||||||
|
|
||||||
uint16_t black_count = 0; // count of black pixels. r/g/b <= 10
|
// uint16_t black_count = 0; // count of black pixels. r/g/b <= 10
|
||||||
|
|
||||||
for (uint16_t led_index = 0, buffer_cursor = 0;
|
// for (uint16_t led_index = 0, buffer_cursor = 0;
|
||||||
led_index < STRIP_LED_NUMBER && buffer_cursor < len;
|
// led_index < STRIP_LED_NUMBER && buffer_cursor < len;
|
||||||
led_index++, buffer_cursor += 3) {
|
// led_index++, buffer_cursor += 3) {
|
||||||
uint8_t r = (uint8_t)((float)buffer[buffer_cursor] *
|
// uint8_t r = (uint8_t)((float)buffer[buffer_cursor] *
|
||||||
display_ambient_light_brightness *
|
// display_ambient_light_brightness *
|
||||||
led_strip_red_calibration),
|
// led_strip_red_calibration),
|
||||||
g = (uint8_t)((float)buffer[buffer_cursor + 1] *
|
// g = (uint8_t)((float)buffer[buffer_cursor + 1] *
|
||||||
display_ambient_light_brightness *
|
// display_ambient_light_brightness *
|
||||||
led_strip_green_calibration),
|
// led_strip_green_calibration),
|
||||||
b = (uint8_t)((float)buffer[buffer_cursor + 2] *
|
// b = (uint8_t)((float)buffer[buffer_cursor + 2] *
|
||||||
display_ambient_light_brightness *
|
// display_ambient_light_brightness *
|
||||||
led_strip_blue_calibration);
|
// led_strip_blue_calibration);
|
||||||
if (r <= 7 && g <= 7 && b <= 7) {
|
// if (r <= 7 && g <= 7 && b <= 7) {
|
||||||
black_count++;
|
// black_count++;
|
||||||
}
|
// }
|
||||||
ESP_ERROR_CHECK(
|
// led_strip_pixels[led_index * 3 + 0] = g;
|
||||||
light_led_strip->set_pixel(light_led_strip, led_index, r, g, b));
|
// led_strip_pixels[led_index * 3 + 1] = r;
|
||||||
}
|
// led_strip_pixels[led_index * 3 + 2] = b;
|
||||||
|
// }
|
||||||
|
|
||||||
if (black_count > STRIP_LED_NUMBER / 5 * 4) {
|
// if (black_count > STRIP_LED_NUMBER / 5 * 4) {
|
||||||
uint8_t r = (uint8_t)((float)50 * display_ambient_light_brightness *
|
// uint8_t r = (uint8_t)((float)50 * display_ambient_light_brightness *
|
||||||
led_strip_red_calibration),
|
// led_strip_red_calibration),
|
||||||
g = (uint8_t)((float)40 * display_ambient_light_brightness *
|
// g = (uint8_t)((float)40 * display_ambient_light_brightness *
|
||||||
led_strip_green_calibration),
|
// led_strip_green_calibration),
|
||||||
b = (uint8_t)((float)20 * display_ambient_light_brightness *
|
// b = (uint8_t)((float)20 * display_ambient_light_brightness *
|
||||||
led_strip_blue_calibration);
|
// led_strip_blue_calibration);
|
||||||
for (uint16_t led_index = 0; led_index < STRIP_LED_NUMBER; led_index++) {
|
// for (uint16_t led_index = 0; led_index < STRIP_LED_NUMBER; led_index++) {
|
||||||
ESP_ERROR_CHECK(
|
// led_strip_pixels[led_index * 3 + 0] = g;
|
||||||
light_led_strip->set_pixel(light_led_strip, led_index, r, g, b));
|
// led_strip_pixels[led_index * 3 + 1] = r;
|
||||||
}
|
// led_strip_pixels[led_index * 3 + 2] = b;
|
||||||
} else {
|
// }
|
||||||
}
|
// }
|
||||||
ESP_ERROR_CHECK(light_led_strip->refresh(light_led_strip, 100));
|
|
||||||
vTaskDelay(pdMS_TO_TICKS(10));
|
vTaskDelay(pdMS_TO_TICKS(10));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -429,3 +456,53 @@ void light_play(light_mode_t mode) {
|
|||||||
ESP_LOGI(LIGHT_TAG, "light_play: %d", mode);
|
ESP_LOGI(LIGHT_TAG, "light_play: %d", mode);
|
||||||
light_mode = mode;
|
light_mode = mode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void set_display_ambient_light_colors(uint16_t offset, uint8_t *sub_pixels,
|
||||||
|
uint16_t len) {
|
||||||
|
// ESP_LOGI(LIGHT_TAG, "set_display_ambient_light_colors: offset: %d\t len:
|
||||||
|
// %d",
|
||||||
|
// offset, len);
|
||||||
|
light_mode = light_mode_desktop_sending_colors;
|
||||||
|
|
||||||
|
uint16_t black_count = 0; // count of black pixels. r/g/b <= 7
|
||||||
|
|
||||||
|
uint16_t global_end = len + offset * 3;
|
||||||
|
|
||||||
|
if (global_end > STRIP_LED_NUMBER * 3) {
|
||||||
|
global_end = STRIP_LED_NUMBER * 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (uint16_t global_index = offset * 3, local_index = 0;
|
||||||
|
global_index < global_end; global_index += 3, local_index += 3) {
|
||||||
|
uint8_t r = (uint8_t)((float)sub_pixels[local_index + 0] *
|
||||||
|
display_ambient_light_brightness *
|
||||||
|
led_strip_red_calibration),
|
||||||
|
g = (uint8_t)((float)sub_pixels[local_index + 1] *
|
||||||
|
display_ambient_light_brightness *
|
||||||
|
led_strip_green_calibration),
|
||||||
|
b = (uint8_t)((float)sub_pixels[local_index + 2] *
|
||||||
|
display_ambient_light_brightness *
|
||||||
|
led_strip_blue_calibration);
|
||||||
|
if (r <= 7 && g <= 7 && b <= 7) {
|
||||||
|
black_count++;
|
||||||
|
}
|
||||||
|
led_strip_pixels[global_index + 0] = g;
|
||||||
|
led_strip_pixels[global_index + 1] = r;
|
||||||
|
led_strip_pixels[global_index + 2] = b;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (black_count == len / 3) {
|
||||||
|
uint8_t r = (uint8_t)((float)10 * display_ambient_light_brightness *
|
||||||
|
led_strip_red_calibration),
|
||||||
|
g = (uint8_t)((float)7 * display_ambient_light_brightness *
|
||||||
|
led_strip_green_calibration),
|
||||||
|
b = (uint8_t)((float)7 * display_ambient_light_brightness *
|
||||||
|
led_strip_blue_calibration);
|
||||||
|
for (uint16_t global_index = offset * 3; global_index < global_end;
|
||||||
|
global_index += 3) {
|
||||||
|
led_strip_pixels[global_index + 0] = g;
|
||||||
|
led_strip_pixels[global_index + 1] = r;
|
||||||
|
led_strip_pixels[global_index + 2] = b;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
21
main/main.c
21
main/main.c
@@ -5,17 +5,18 @@
|
|||||||
#include "esp_log.h"
|
#include "esp_log.h"
|
||||||
#include "freertos/FreeRTOS.h"
|
#include "freertos/FreeRTOS.h"
|
||||||
#include "freertos/task.h"
|
#include "freertos/task.h"
|
||||||
#include "hw-ms03.c"
|
|
||||||
#include "i2c.c"
|
#include "i2c.c"
|
||||||
#include "light.c"
|
#include "light.c"
|
||||||
#include "mqtt.c"
|
#include "mqtt.c"
|
||||||
#include "pca9555.c"
|
#include "pca9555.c"
|
||||||
#include "sdkconfig.h"
|
#include "sdkconfig.h"
|
||||||
|
#include "service_discovery.c"
|
||||||
#include "temperature.c"
|
#include "temperature.c"
|
||||||
|
#include "udp_server.c"
|
||||||
#include "ui_input.c"
|
#include "ui_input.c"
|
||||||
#include "wifi.c"
|
#include "wifi.c"
|
||||||
|
|
||||||
static const char *TAG = "DisplayAmbientLight";
|
static const char *APP_TAG = "DisplayAmbientLight";
|
||||||
|
|
||||||
void app_main(void) {
|
void app_main(void) {
|
||||||
app_nvs_init();
|
app_nvs_init();
|
||||||
@@ -27,7 +28,7 @@ void app_main(void) {
|
|||||||
|
|
||||||
init_display();
|
init_display();
|
||||||
display_print8_str(0, 0, "Ambient Light");
|
display_print8_str(0, 0, "Ambient Light");
|
||||||
// hw_ms03_init();
|
|
||||||
ci_03t_init();
|
ci_03t_init();
|
||||||
apds_9960_init();
|
apds_9960_init();
|
||||||
apds_9960_auto_fetch();
|
apds_9960_auto_fetch();
|
||||||
@@ -41,14 +42,16 @@ void app_main(void) {
|
|||||||
if (connect_wifi()) {
|
if (connect_wifi()) {
|
||||||
light_play(light_mode_idle);
|
light_play(light_mode_idle);
|
||||||
}
|
}
|
||||||
|
udp_server_init();
|
||||||
|
service_discovery_init();
|
||||||
vTaskDelay(pdMS_TO_TICKS(1000));
|
vTaskDelay(pdMS_TO_TICKS(1000));
|
||||||
mqtt_app_start();
|
mqtt_app_start();
|
||||||
if (waiting_for_mqtt_connected()) {
|
// if (waiting_for_mqtt_connected()) {
|
||||||
light_play(light_mode_mqtt_connected);
|
// light_play(light_mode_mqtt_connected);
|
||||||
}
|
// }
|
||||||
if (waiting_for_desktop_online()) {
|
// if (waiting_for_desktop_online()) {
|
||||||
light_play(light_mode_desktop_online);
|
// light_play(light_mode_desktop_online);
|
||||||
}
|
// }
|
||||||
while (waiting_and_get_colors()) {
|
while (waiting_and_get_colors()) {
|
||||||
light_play_colors(NUMBER_OF_LEDS * 3, mqtt_colors_buffer);
|
light_play_colors(NUMBER_OF_LEDS * 3, mqtt_colors_buffer);
|
||||||
}
|
}
|
||||||
|
@@ -52,7 +52,7 @@
|
|||||||
static const char *MQTT_TAG = "DisplayAmbientLight_MQTT";
|
static const char *MQTT_TAG = "DisplayAmbientLight_MQTT";
|
||||||
|
|
||||||
static EventGroupHandle_t s_mqtt_event_group;
|
static EventGroupHandle_t s_mqtt_event_group;
|
||||||
static xQueueHandle mqtt_cmd_event = NULL;
|
static QueueHandle_t mqtt_cmd_event = NULL;
|
||||||
static esp_mqtt_client_handle_t client = NULL;
|
static esp_mqtt_client_handle_t client = NULL;
|
||||||
|
|
||||||
typedef struct colors {
|
typedef struct colors {
|
||||||
@@ -69,8 +69,8 @@ static void log_error_if_nonzero(const char *message, int error_code) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void mqtt_event_handler(void *handler_args, esp_event_base_t base,
|
static void mqtt_event_handler(void *handler_args, esp_event_base_t base,
|
||||||
int32_t event_id, void *event_data) {
|
long event_id, void *event_data) {
|
||||||
ESP_LOGD(MQTT_TAG, "Event dispatched from event loop base=%s, event_id=%d",
|
ESP_LOGD(MQTT_TAG, "Event dispatched from event loop base=%s, event_id=%ld",
|
||||||
base, event_id);
|
base, event_id);
|
||||||
esp_mqtt_event_handle_t event = event_data;
|
esp_mqtt_event_handle_t event = event_data;
|
||||||
client = event->client;
|
client = event->client;
|
||||||
@@ -195,7 +195,7 @@ static void mqtt_app_start() {
|
|||||||
xTaskCreate(mqtt_cmd_event_handler, "mqtt_cmd_event", 2048, NULL, 10, NULL);
|
xTaskCreate(mqtt_cmd_event_handler, "mqtt_cmd_event", 2048, NULL, 10, NULL);
|
||||||
|
|
||||||
const esp_mqtt_client_config_t mqtt_cfg = {
|
const esp_mqtt_client_config_t mqtt_cfg = {
|
||||||
.uri = MQTT_BROKER_URL,
|
.broker.address.uri = MQTT_BROKER_URL,
|
||||||
};
|
};
|
||||||
esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg);
|
esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg);
|
||||||
esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler,
|
esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler,
|
||||||
|
28
main/service_discovery.c
Normal file
28
main/service_discovery.c
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "esp_err.h"
|
||||||
|
#include "esp_log.h"
|
||||||
|
#include "mdns.h"
|
||||||
|
|
||||||
|
static const char *SERVICE_DISCOVERY_TAG = "SERVICE_DISCOVERY_TAG";
|
||||||
|
|
||||||
|
static void service_discovery_init() { // 初始化 mDNS 服务
|
||||||
|
esp_err_t err = mdns_init();
|
||||||
|
if (err) {
|
||||||
|
ESP_LOGE(SERVICE_DISCOVERY_TAG, "MDNS Init failed: %d\n", err);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 设置 hostname
|
||||||
|
mdns_hostname_set("ambient-light-board");
|
||||||
|
// 设置默认实例
|
||||||
|
mdns_instance_name_set("ivan");
|
||||||
|
// 添加服务
|
||||||
|
mdns_service_add(NULL, "_ambient_light", "_udp", 23042, NULL, 0);
|
||||||
|
mdns_service_add(NULL, "_http", "_tcp", 80, NULL, 0);
|
||||||
|
|
||||||
|
// 注意:必须先添加服务,然后才能设置其属性
|
||||||
|
// web 服务器使用自定义的实例名
|
||||||
|
mdns_service_instance_name_set("_ambientlight_web", "_tcp",
|
||||||
|
"Ambient Light Board Web Server");
|
||||||
|
}
|
@@ -22,7 +22,7 @@
|
|||||||
void fetch_temperature(void* arg) {
|
void fetch_temperature(void* arg) {
|
||||||
esp_err_t error;
|
esp_err_t error;
|
||||||
float temperature = DEFAULT_TEMPERATURE;
|
float temperature = DEFAULT_TEMPERATURE;
|
||||||
char temperature_str[10];
|
char temperature_str[20];
|
||||||
uint8_t temperature_buffer[] = {0, 0};
|
uint8_t temperature_buffer[] = {0, 0};
|
||||||
display_fill_rect(0, 0, 128, 2, 0x00);
|
display_fill_rect(0, 0, 128, 2, 0x00);
|
||||||
for (;;) {
|
for (;;) {
|
||||||
|
134
main/udp_server.c
Normal file
134
main/udp_server.c
Normal file
@@ -0,0 +1,134 @@
|
|||||||
|
#include <lwip/netdb.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/param.h>
|
||||||
|
|
||||||
|
#include "esp_err.h"
|
||||||
|
#include "esp_event.h"
|
||||||
|
#include "esp_log.h"
|
||||||
|
#include "esp_netif.h"
|
||||||
|
#include "esp_system.h"
|
||||||
|
#include "esp_wifi.h"
|
||||||
|
#include "freertos/FreeRTOS.h"
|
||||||
|
#include "freertos/task.h"
|
||||||
|
#include "light.c"
|
||||||
|
#include "lwip/err.h"
|
||||||
|
#include "lwip/sockets.h"
|
||||||
|
#include "lwip/sys.h"
|
||||||
|
#include "nvs_flash.h"
|
||||||
|
|
||||||
|
#define UDP_PORT 23042
|
||||||
|
|
||||||
|
static const char *UDP_SERVER_TAG = "UDP_SERVER";
|
||||||
|
|
||||||
|
static void udp_server_task(void *pvParameters) {
|
||||||
|
char rx_buffer[1024];
|
||||||
|
char addr_str[128];
|
||||||
|
int addr_family = (int)pvParameters;
|
||||||
|
int ip_protocol = 0;
|
||||||
|
struct sockaddr_in6 dest_addr;
|
||||||
|
|
||||||
|
while (1) {
|
||||||
|
if (addr_family == AF_INET) {
|
||||||
|
struct sockaddr_in *dest_addr_ip4 = (struct sockaddr_in *)&dest_addr;
|
||||||
|
dest_addr_ip4->sin_addr.s_addr = htonl(INADDR_ANY);
|
||||||
|
dest_addr_ip4->sin_family = AF_INET;
|
||||||
|
dest_addr_ip4->sin_port = htons(UDP_PORT);
|
||||||
|
ip_protocol = IPPROTO_IP;
|
||||||
|
}
|
||||||
|
|
||||||
|
int sock = socket(addr_family, SOCK_DGRAM, ip_protocol);
|
||||||
|
if (sock < 0) {
|
||||||
|
ESP_LOGE(UDP_SERVER_TAG, "Unable to create socket: errno %d. sock: %d",
|
||||||
|
errno, sock);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
ESP_LOGI(UDP_SERVER_TAG, "Socket created");
|
||||||
|
|
||||||
|
// Set timeout
|
||||||
|
struct timeval timeout;
|
||||||
|
timeout.tv_sec = 10;
|
||||||
|
timeout.tv_usec = 0;
|
||||||
|
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof timeout);
|
||||||
|
|
||||||
|
int err = bind(sock, (struct sockaddr *)&dest_addr, sizeof(dest_addr));
|
||||||
|
if (err < 0) {
|
||||||
|
ESP_LOGE(UDP_SERVER_TAG, "Socket unable to bind: errno %d. sock: %d",
|
||||||
|
errno, sock);
|
||||||
|
}
|
||||||
|
ESP_LOGI(UDP_SERVER_TAG, "Socket bound, port %d", UDP_PORT);
|
||||||
|
|
||||||
|
struct sockaddr_storage source_addr; // Large enough for both IPv4 or IPv6
|
||||||
|
socklen_t socklen = sizeof(source_addr);
|
||||||
|
|
||||||
|
while (1) {
|
||||||
|
// ESP_LOGI(UDP_SERVER_TAG, "Waiting for data");
|
||||||
|
int len = recvfrom(sock, rx_buffer, sizeof(rx_buffer) - 1, 0,
|
||||||
|
(struct sockaddr *)&source_addr, &socklen);
|
||||||
|
// Error occurred during receiving
|
||||||
|
if (len < 0) {
|
||||||
|
if (errno == EAGAIN) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
ESP_LOGE(UDP_SERVER_TAG, "recvfrom failed: errno %d. len: %d", errno,
|
||||||
|
len);
|
||||||
|
}
|
||||||
|
// Data received
|
||||||
|
else {
|
||||||
|
// Get the sender's ip address as string
|
||||||
|
if (source_addr.ss_family == PF_INET) {
|
||||||
|
inet_ntoa_r(((struct sockaddr_in *)&source_addr)->sin_addr, addr_str,
|
||||||
|
sizeof(addr_str) - 1);
|
||||||
|
|
||||||
|
rx_buffer[len] = 0; // Null-terminate whatever we received and treat
|
||||||
|
// like a string...
|
||||||
|
|
||||||
|
switch (rx_buffer[0]) {
|
||||||
|
case 1:
|
||||||
|
sendto(sock, rx_buffer, 1, 0, (struct sockaddr *)&source_addr,
|
||||||
|
sizeof(source_addr));
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 2:
|
||||||
|
set_display_ambient_light_colors(
|
||||||
|
((uint16_t)rx_buffer[1] << 8 | (uint16_t)rx_buffer[2]),
|
||||||
|
(uint8_t *)&(rx_buffer[3]), len - 3);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
|
||||||
|
ESP_LOGI(UDP_SERVER_TAG, "%s", rx_buffer);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
int err =
|
||||||
|
sendto(sock, rx_buffer, len, 0, (struct sockaddr *)&source_addr,
|
||||||
|
sizeof(source_addr));
|
||||||
|
if (err < 0) {
|
||||||
|
ESP_LOGI(UDP_SERVER_TAG, "Received %d bytes from %s:", len,
|
||||||
|
addr_str);
|
||||||
|
ESP_LOGE(UDP_SERVER_TAG,
|
||||||
|
"Error occurred during sending: errno %d. sock: %d", errno,
|
||||||
|
sock);
|
||||||
|
shutdown(sock, 0);
|
||||||
|
close(sock);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sock != -1 && len < 0) {
|
||||||
|
ESP_LOGE(UDP_SERVER_TAG, "Shutting down socket and restarting...");
|
||||||
|
shutdown(sock, 0);
|
||||||
|
close(sock);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
vTaskDelete(NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
void udp_server_init(void) {
|
||||||
|
ESP_ERROR_CHECK(nvs_flash_init());
|
||||||
|
ESP_ERROR_CHECK(esp_netif_init());
|
||||||
|
xTaskCreate(udp_server_task, "udp_server", 4096, (void *)AF_INET, 5, NULL);
|
||||||
|
}
|
@@ -34,8 +34,8 @@
|
|||||||
|
|
||||||
static const char *UI_INPUT_TAG = "UiInput";
|
static const char *UI_INPUT_TAG = "UiInput";
|
||||||
|
|
||||||
static xQueueHandle ui_input_event = NULL;
|
static QueueHandle_t ui_input_event = NULL;
|
||||||
static xQueueHandle ui_input_raw_event = NULL;
|
static QueueHandle_t ui_input_raw_event = NULL;
|
||||||
|
|
||||||
typedef struct encoder_state {
|
typedef struct encoder_state {
|
||||||
e_ui_input_raw_key_t key;
|
e_ui_input_raw_key_t key;
|
||||||
@@ -51,7 +51,6 @@ static encoder_state_t encoder_1_state = {.key = ui_input_raw_key_encoder_1,
|
|||||||
|
|
||||||
uint8_t level_byte;
|
uint8_t level_byte;
|
||||||
int8_t delta = 0;
|
int8_t delta = 0;
|
||||||
char changing_str[12] = "NC";
|
|
||||||
|
|
||||||
static void IRAM_ATTR gpio_isr_handler(void *arg) {
|
static void IRAM_ATTR gpio_isr_handler(void *arg) {
|
||||||
xQueueSendFromISR(ui_input_raw_event, NULL, NULL);
|
xQueueSendFromISR(ui_input_raw_event, NULL, NULL);
|
||||||
@@ -59,7 +58,7 @@ static void IRAM_ATTR gpio_isr_handler(void *arg) {
|
|||||||
|
|
||||||
static void ui_input_update_embedded_display(void *arg) {
|
static void ui_input_update_embedded_display(void *arg) {
|
||||||
s_ui_input_t input;
|
s_ui_input_t input;
|
||||||
char changing_str[12] = "NC";
|
char changing_str[20] = "NC";
|
||||||
for (;;) {
|
for (;;) {
|
||||||
if (xQueueReceive(ui_input_event, &input, portMAX_DELAY)) {
|
if (xQueueReceive(ui_input_event, &input, portMAX_DELAY)) {
|
||||||
switch (input.key) {
|
switch (input.key) {
|
||||||
|
Reference in New Issue
Block a user