mirror of
https://git.beihong.wang/wangbeihong/iot-bedroom-environment-controller.git
synced 2026-04-23 15:13:05 +08:00
- Rename component directory from lvgl_st7735s_use to lvgl_st7789_use - Update CMakeLists.txt to register new source files - Add comprehensive README documentation for ST7789 configuration - Add time_alarm module with SNTP synchronization and alarm management - Add sensors header for sensor abstraction layer
205 lines
5.1 KiB
C
205 lines
5.1 KiB
C
#include "peripherals.h"
|
|
#include "esp_log.h"
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
#include "freertos/task.h"
|
|
#include "freertos/semphr.h"
|
|
#include "driver/gpio.h"
|
|
#include "driver/ledc.h"
|
|
#include "iot_servo.h"
|
|
#include "app_state.h"
|
|
#include "lvgl_st7789_use.h"
|
|
#include "mqtt_manager.h"
|
|
#include "serial_mcu.h"
|
|
|
|
static const char *TAG = "peripherals";
|
|
|
|
void init_gpio_output(void)
|
|
{
|
|
gpio_config_t io_conf = {
|
|
.pin_bit_mask = 1ULL << GPIO_NUM_1,
|
|
.mode = GPIO_MODE_OUTPUT,
|
|
.pull_up_en = 0,
|
|
.pull_down_en = 1,
|
|
.intr_type = GPIO_INTR_DISABLE};
|
|
gpio_config(&io_conf);
|
|
}
|
|
|
|
static const uint16_t calibration_value_0 = 0;
|
|
static const uint16_t calibration_value_180 = 180;
|
|
|
|
void servo_init(void)
|
|
{
|
|
ESP_LOGI(TAG, "初始化舵机控制");
|
|
servo_config_t servo_cfg = {
|
|
.max_angle = 180,
|
|
.min_width_us = 500,
|
|
.max_width_us = 2500,
|
|
.freq = 50,
|
|
.timer_number = LEDC_TIMER_0,
|
|
.channels = {
|
|
.servo_pin = {
|
|
SERVO_GPIO,
|
|
},
|
|
.ch = {
|
|
LEDC_CHANNEL_0,
|
|
},
|
|
},
|
|
.channel_number = 1,
|
|
};
|
|
|
|
esp_err_t ret = iot_servo_init(LEDC_LOW_SPEED_MODE, &servo_cfg);
|
|
if (ret != ESP_OK)
|
|
{
|
|
ESP_LOGE(TAG, "舵机初始化失败: %s", esp_err_to_name(ret));
|
|
}
|
|
else
|
|
{
|
|
ESP_LOGI(TAG, "舵机初始化成功");
|
|
}
|
|
}
|
|
|
|
void servo_set_angle(uint16_t angle)
|
|
{
|
|
if (angle < calibration_value_0)
|
|
{
|
|
angle = calibration_value_0;
|
|
}
|
|
else if (angle > calibration_value_180)
|
|
{
|
|
angle = calibration_value_180;
|
|
}
|
|
iot_servo_write_angle(LEDC_LOW_SPEED_MODE, 0, angle);
|
|
ESP_LOGI(TAG, "舵机角度设置为: %d", angle);
|
|
}
|
|
|
|
void peripheral_control_task(void *pvParameters)
|
|
{
|
|
(void)pvParameters;
|
|
for (;;)
|
|
{
|
|
|
|
static bool last_led_backlight_on = false;
|
|
static bool backlight_first_run = true;
|
|
|
|
if (backlight_first_run)
|
|
{
|
|
last_led_backlight_on = !led_backlight_on;
|
|
backlight_first_run = false;
|
|
}
|
|
|
|
if (led_backlight_on != last_led_backlight_on)
|
|
{
|
|
if (led_backlight_on == false)
|
|
{
|
|
gpio_set_level(EXAMPLE_LCD_GPIO_BL, 0);
|
|
}
|
|
else
|
|
{
|
|
gpio_set_level(EXAMPLE_LCD_GPIO_BL, 1);
|
|
}
|
|
last_led_backlight_on = led_backlight_on;
|
|
}
|
|
|
|
static bool last_servo_control_flag = false;
|
|
static bool first_run = true;
|
|
|
|
if (first_run)
|
|
{
|
|
last_servo_control_flag = !servo_control_flag;
|
|
first_run = false;
|
|
}
|
|
|
|
if (servo_control_flag != last_servo_control_flag)
|
|
{
|
|
if (servo_control_flag == false)
|
|
{
|
|
servo_set_angle(0);
|
|
}
|
|
else
|
|
{
|
|
servo_set_angle(180);
|
|
}
|
|
mqtt_manager_publish_feedback();
|
|
last_servo_control_flag = servo_control_flag;
|
|
}
|
|
|
|
static bool last_fan_control_flag = false;
|
|
static bool fan_first_run = true;
|
|
|
|
if (fan_first_run)
|
|
{
|
|
last_fan_control_flag = !fan_control_flag;
|
|
fan_first_run = false;
|
|
}
|
|
|
|
if (fan_control_flag != last_fan_control_flag)
|
|
{
|
|
if (fan_control_flag == false)
|
|
{
|
|
gpio_set_level(GPIO_NUM_1, 0);
|
|
}
|
|
else
|
|
{
|
|
gpio_set_level(GPIO_NUM_1, 1);
|
|
}
|
|
mqtt_manager_publish_feedback();
|
|
last_fan_control_flag = fan_control_flag;
|
|
}
|
|
|
|
static bool last_buzzer_control_flag = false;
|
|
static bool buzzer_first_run = true;
|
|
|
|
if (buzzer_first_run)
|
|
{
|
|
last_buzzer_control_flag = !buzzer_control_flag;
|
|
buzzer_first_run = false;
|
|
}
|
|
|
|
if (buzzer_control_flag != last_buzzer_control_flag)
|
|
{
|
|
if (buzzer_control_flag == false)
|
|
{
|
|
sendControlFrame(0x02, 0);
|
|
}
|
|
else
|
|
{
|
|
sendControlFrame(0x02, 1);
|
|
}
|
|
mqtt_manager_publish_feedback();
|
|
last_buzzer_control_flag = buzzer_control_flag;
|
|
}
|
|
|
|
static uint8_t last_led_brightness_value = 0;
|
|
static bool led_first_run = true;
|
|
|
|
if (led_first_run)
|
|
{
|
|
last_led_brightness_value = !led_brightness_value;
|
|
led_first_run = false;
|
|
}
|
|
|
|
if (led_brightness_value != last_led_brightness_value)
|
|
{
|
|
sendControlFrame(0x01, led_brightness_value);
|
|
ESP_LOGI(TAG, "LED brightness updated to %d", led_brightness_value);
|
|
mqtt_manager_publish_feedback();
|
|
last_led_brightness_value = led_brightness_value;
|
|
}
|
|
|
|
vTaskDelay(pdMS_TO_TICKS(100));
|
|
}
|
|
}
|
|
|
|
esp_err_t peripherals_init(void)
|
|
{
|
|
ESP_LOGI(TAG, "peripherals_init");
|
|
|
|
/* 初始化 GPIO 输出(风扇、背光等)和舵机 */
|
|
init_gpio_output();
|
|
servo_init();
|
|
|
|
/* 未来可在此添加其他外设初始化(如 ADC、PWM、RMT 等) */
|
|
return ESP_OK;
|
|
}
|