This commit is contained in:
2025-11-23 09:25:13 +03:00
parent 16aca10f44
commit a234674fac
2 changed files with 338 additions and 86 deletions

View File

@@ -1,37 +1,57 @@
#include "ate0003.h"
static i2c_master_bus_handle_t i2c_bus_handle = NULL;
static httpd_handle_t webserver_handle = NULL;
static i2c_master_bus_handle_t _i2c_bus_handle = NULL;
static httpd_handle_t _webserver_handle = NULL;
static zh_pcf8574_handle_t button_handle = {0};
static zh_pcf8574_handle_t led_handle = {0};
static zh_pcf8574_handle_t relay_handle = {0};
static zh_pcf8574_handle_t _button_handle = {0};
static zh_pcf8574_handle_t _led_1_handle = {0};
static zh_pcf8574_handle_t _led_2_handle = {0};
static zh_pcf8574_handle_t _relay_handle = {0};
static zh_pcf8574_handle_t lcd_handle = {0};
static bool is_ts = true;
static bool is_ret = false;
static bool is_ext = false;
static zh_encoder_handle_t _power_encoder_handle = {0};
static zh_encoder_handle_t _component_encoder_handle = {0};
static void zh_wifi_softap_init(void);
static void zh_webserver_init(void);
static void zh_gpio_init(void);
static void zh_io_expander_init(void);
static void zh_pcf8574_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
volatile static bool _is_work = false;
volatile static bool _is_dmm_enabled = false;
volatile static bool _is_fix_enabled = false;
volatile static uint8_t _permitted_channels = 0;
volatile static uint8_t _used_channels = 0;
volatile static bool _is_initialized = false;
volatile static bool _is_num_1_fixed = false;
volatile static bool _is_num_2_fixed = false;
volatile static bool _is_num_3_fixed = false;
volatile static bool _is_num_4_fixed = false;
static void _zh_wifi_softap_init(void);
static void _zh_webserver_init(void);
static void _zh_encoder_init(void);
static void _zh_ac_dimmer_init(void);
static void _zh_pcf8574_init(void);
static void _zh_pcf8574_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
static void _zh_encoder_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
static void _zh_component_select(uint8_t component);
static void _zh_relay_on(uint8_t relay_number);
static void _zh_relay_off(uint8_t relay_number);
void app_main(void)
{
nvs_flash_init();
esp_event_loop_create_default();
zh_wifi_softap_init();
zh_webserver_init();
zh_gpio_init();
_zh_wifi_softap_init();
_zh_webserver_init();
_zh_encoder_init();
_zh_ac_dimmer_init();
gpio_set_level(TRIAC_GPIO, LOW);
zh_io_expander_init();
_zh_pcf8574_init();
// zh_pcf8574_write_gpio(&led_handle, TS_LED_GREEN, LED_ON);
// zh_pcf8574_write_gpio(&led_handle, RET_LED_BLUE, LED_ON);
// zh_pcf8574_write_gpio(&led_handle, EXT_LED_BLUE, LED_ON);
}
static void zh_wifi_softap_init(void)
static void _zh_wifi_softap_init(void)
{
esp_netif_init();
esp_netif_create_default_wifi_ap();
@@ -50,26 +70,44 @@ static void zh_wifi_softap_init(void)
esp_wifi_start();
}
static void zh_webserver_init(void)
static void _zh_webserver_init(void)
{
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
httpd_start(&webserver_handle, &config);
zh_ota_server_init(webserver_handle);
httpd_start(&_webserver_handle, &config);
zh_ota_server_init(_webserver_handle);
}
static void zh_gpio_init(void)
static void _zh_encoder_init(void)
{
gpio_config_t triac_pin_config = {
.intr_type = GPIO_INTR_DISABLE,
.mode = GPIO_MODE_OUTPUT,
.pin_bit_mask = (1ULL << TRIAC_GPIO),
.pull_down_en = GPIO_PULLDOWN_DISABLE,
.pull_up_en = GPIO_PULLUP_DISABLE,
};
gpio_config(&triac_pin_config);
esp_event_handler_instance_register(ZH_ENCODER, ESP_EVENT_ANY_ID, &_zh_encoder_event_handler, NULL, NULL);
zh_encoder_init_config_t config = ZH_ENCODER_INIT_CONFIG_DEFAULT();
// config.a_gpio_number = PORTC0;
// config.b_gpio_number = PORTC1;
config.encoder_min_value = 0;
config.encoder_max_value = 100;
config.encoder_step = 5;
config.encoder_number = POWER_ENCODER;
zh_encoder_init(&config, &_power_encoder_handle);
zh_encoder_set(&_power_encoder_handle, 100);
// config.a_gpio_number = PORTC2;
// config.b_gpio_number = PORTC3;
config.encoder_min_value = 0;
// config.encoder_max_value = (sizeof(component_cmm) / sizeof(component_cmm[0])) - 1;
config.encoder_step = 1;
config.encoder_number = COMPONENT_ENCODER;
zh_encoder_init(&config, &_component_encoder_handle);
zh_encoder_set(&_component_encoder_handle, 0);
}
static void zh_io_expander_init(void)
static void _zh_ac_dimmer_init(void)
{
zh_ac_dimmer_init_config_t config = ZH_AC_DIMMER_INIT_CONFIG_DEFAULT();
config.zero_cross_gpio = GPIO_NUM_16;
config.triac_gpio = GPIO_NUM_4;
zh_ac_dimmer_init(&config);
}
static void _zh_pcf8574_init(void)
{
i2c_master_bus_config_t i2c_bus_config = {
.clk_source = I2C_CLK_SRC_DEFAULT,
@@ -77,99 +115,273 @@ static void zh_io_expander_init(void)
.sda_io_num = GPIO_NUM_21,
.glitch_ignore_cnt = 7,
};
i2c_new_master_bus(&i2c_bus_config, &i2c_bus_handle);
esp_event_handler_instance_register(ZH_PCF8574, ESP_EVENT_ANY_ID, &zh_pcf8574_event_handler, NULL, NULL);
i2c_new_master_bus(&i2c_bus_config, &_i2c_bus_handle);
esp_event_handler_instance_register(ZH_PCF8574, ESP_EVENT_ANY_ID, &_zh_pcf8574_event_handler, NULL, NULL);
zh_pcf8574_init_config_t config = ZH_PCF8574_INIT_CONFIG_DEFAULT();
config.i2c_handle = i2c_bus_handle;
config.i2c_address = LED_I2C_ADDRESS;
zh_pcf8574_init(&config, &led_handle);
config.i2c_handle = _i2c_bus_handle;
config.i2c_address = LED_1_I2C_ADDRESS;
zh_pcf8574_init(&config, &_led_1_handle);
config.i2c_address = LED_2_I2C_ADDRESS;
zh_pcf8574_init(&config, &_led_2_handle);
config.i2c_address = RELAY_I2C_ADDRESS;
zh_pcf8574_init(&config, &relay_handle);
config.i2c_address = BUTTON_I2C_ADDRESS;
zh_pcf8574_init(&config, &_relay_handle);
config.i2c_address = LCD_I2C_ADDRESS;
zh_pcf8574_init(&config, &lcd_handle);
zh_160x_init(&lcd_handle, ZH_LCD_16X4);
config.p0_gpio_work_mode = true;
config.p1_gpio_work_mode = true;
config.p2_gpio_work_mode = true;
config.p3_gpio_work_mode = true;
config.p4_gpio_work_mode = true;
config.p5_gpio_work_mode = true;
config.interrupt_gpio = GPIO_NUM_17;
zh_pcf8574_init(&config, &button_handle);
config.i2c_address = BUTTON_I2C_ADDRESS;
zh_pcf8574_init(&config, &_button_handle);
}
static void zh_pcf8574_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
static void _zh_pcf8574_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
{
zh_pcf8574_event_on_isr_t *event = event_data;
switch (event->gpio_number)
{
case TS_BUTTON:
if (event->gpio_level == LOW && is_ret == false && is_ext == false)
case DMM_BUTTON:
if (event->gpio_level == LOW && _is_work == false)
{
if (is_ts == true)
if (_is_dmm_enabled == true)
{
zh_pcf8574_write_gpio(&led_handle, TS_LED_RED, LED_ON);
zh_pcf8574_write_gpio(&led_handle, TS_LED_GREEN, LED_OFF);
zh_pcf8574_write_gpio(&relay_handle, TS_RELAY, RELAY_ON);
is_ts = false;
zh_pcf8574_write_gpio(&_led_1_handle, DMM_LED_RED, LED_ON);
zh_pcf8574_write_gpio(&_led_1_handle, DMM_LED_GREEN, LED_OFF);
zh_pcf8574_write_gpio(&_relay_handle, DMM_RELAY, RELAY_ON);
_is_dmm_enabled = false;
}
else
{
zh_pcf8574_write_gpio(&led_handle, TS_LED_RED, LED_OFF);
zh_pcf8574_write_gpio(&led_handle, TS_LED_GREEN, LED_ON);
zh_pcf8574_write_gpio(&relay_handle, TS_RELAY, RELAY_OFF);
is_ts = true;
zh_pcf8574_write_gpio(&_led_1_handle, DMM_LED_RED, LED_OFF);
zh_pcf8574_write_gpio(&_led_1_handle, DMM_LED_GREEN, LED_ON);
zh_pcf8574_write_gpio(&_relay_handle, DMM_RELAY, RELAY_OFF);
_is_dmm_enabled = true;
}
}
break;
case RET_BUTTON:
if (is_ext == true)
{
break;
}
case FIX_BUTTON:
if (event->gpio_level == LOW)
{
is_ret = true;
zh_pcf8574_write_gpio(&led_handle, RET_LED_BLUE, LED_OFF);
zh_pcf8574_write_gpio(&led_handle, RET_LED_GREEN, LED_ON);
zh_pcf8574_write_gpio(&relay_handle, RET_RELAY, RELAY_ON);
zh_pcf8574_write_gpio(&relay_handle, GROUND_RELAY, RELAY_ON);
vTaskDelay(20 / portTICK_PERIOD_MS);
gpio_set_level(TRIAC_GPIO, HIGH);
zh_pcf8574_write_gpio(&_led_1_handle, FIX_LED_BLUE, LED_OFF);
zh_pcf8574_write_gpio(&_led_1_handle, FIX_LED_GREEN, LED_ON);
_is_fix_enabled = true;
}
else
{
gpio_set_level(TRIAC_GPIO, LOW);
vTaskDelay(1 / portTICK_PERIOD_MS);
zh_pcf8574_write_gpio(&led_handle, RET_LED_BLUE, LED_ON);
zh_pcf8574_write_gpio(&led_handle, RET_LED_GREEN, LED_OFF);
zh_pcf8574_write_gpio(&relay_handle, RET_RELAY, RELAY_OFF);
zh_pcf8574_write_gpio(&relay_handle, GROUND_RELAY, RELAY_OFF);
is_ret = false;
zh_pcf8574_write_gpio(&_led_1_handle, FIX_LED_BLUE, LED_ON);
zh_pcf8574_write_gpio(&_led_1_handle, FIX_LED_GREEN, LED_OFF);
_is_fix_enabled = false;
}
break;
case EXT_BUTTON:
if (is_ret == true)
case NUM1_BUTTON:
if ((_permitted_channels & CHANNEL1) == 0)
{
break;
}
if (event->gpio_level == LOW)
{
is_ext = true;
zh_pcf8574_write_gpio(&led_handle, EXT_LED_BLUE, LED_OFF);
zh_pcf8574_write_gpio(&led_handle, EXT_LED_GREEN, LED_ON);
zh_pcf8574_write_gpio(&relay_handle, EXT_RELAY, RELAY_ON);
zh_pcf8574_write_gpio(&relay_handle, GROUND_RELAY, RELAY_ON);
vTaskDelay(20 / portTICK_PERIOD_MS);
gpio_set_level(TRIAC_GPIO, HIGH);
if (_is_num_1_fixed == true)
{
break;
}
zh_pcf8574_write_gpio(&_led_1_handle, NUM1_LED_BLUE, LED_OFF);
zh_pcf8574_write_gpio(&_led_1_handle, NUM1_LED_GREEN, LED_ON);
_zh_relay_on(L1_RELAY);
}
else
{
gpio_set_level(TRIAC_GPIO, LOW);
vTaskDelay(1 / portTICK_PERIOD_MS);
zh_pcf8574_write_gpio(&led_handle, EXT_LED_BLUE, LED_ON);
zh_pcf8574_write_gpio(&led_handle, EXT_LED_GREEN, LED_OFF);
zh_pcf8574_write_gpio(&relay_handle, EXT_RELAY, RELAY_OFF);
zh_pcf8574_write_gpio(&relay_handle, GROUND_RELAY, RELAY_OFF);
is_ext = false;
if (_is_fix_enabled == true)
{
_is_num_1_fixed = true;
break;
}
zh_pcf8574_write_gpio(&_led_1_handle, NUM1_LED_BLUE, LED_ON);
zh_pcf8574_write_gpio(&_led_1_handle, NUM1_LED_GREEN, LED_OFF);
_zh_relay_off(L1_RELAY);
_is_num_1_fixed = false;
}
break;
case NUM2_BUTTON:
if ((_permitted_channels & CHANNEL2) == 0)
{
break;
}
if (event->gpio_level == LOW)
{
if (_is_num_2_fixed == true)
{
break;
}
zh_pcf8574_write_gpio(&_led_1_handle, NUM2_LED_BLUE, LED_OFF);
zh_pcf8574_write_gpio(&_led_1_handle, NUM2_LED_GREEN, LED_ON);
_zh_relay_on(L2_RELAY);
}
else
{
if (_is_fix_enabled == true)
{
_is_num_2_fixed = true;
break;
}
zh_pcf8574_write_gpio(&_led_1_handle, NUM2_LED_BLUE, LED_ON);
zh_pcf8574_write_gpio(&_led_1_handle, NUM2_LED_GREEN, LED_OFF);
_zh_relay_off(L2_RELAY);
_is_num_2_fixed = false;
}
break;
case NUM3_BUTTON:
if ((_permitted_channels & CHANNEL3) == 0)
{
break;
}
if (event->gpio_level == LOW)
{
if (_is_num_3_fixed == true)
{
break;
}
zh_pcf8574_write_gpio(&_led_2_handle, NUM3_LED_BLUE, LED_OFF);
zh_pcf8574_write_gpio(&_led_2_handle, NUM3_LED_GREEN, LED_ON);
_zh_relay_on(L3_RELAY);
}
else
{
if (_is_fix_enabled == true)
{
_is_num_3_fixed = true;
break;
}
zh_pcf8574_write_gpio(&_led_2_handle, NUM3_LED_BLUE, LED_ON);
zh_pcf8574_write_gpio(&_led_2_handle, NUM3_LED_GREEN, LED_OFF);
_zh_relay_off(L3_RELAY);
_is_num_3_fixed = false;
}
break;
case NUM4_BUTTON:
if ((_permitted_channels & CHANNEL4) == 0)
{
break;
}
if (event->gpio_level == LOW)
{
if (_is_num_4_fixed == true)
{
break;
}
zh_pcf8574_write_gpio(&_led_2_handle, NUM4_LED_BLUE, LED_OFF);
zh_pcf8574_write_gpio(&_led_2_handle, NUM4_LED_GREEN, LED_ON);
_zh_relay_on(L4_RELAY);
}
else
{
if (_is_fix_enabled == true)
{
_is_num_4_fixed = true;
break;
}
zh_pcf8574_write_gpio(&_led_2_handle, NUM4_LED_BLUE, LED_ON);
zh_pcf8574_write_gpio(&_led_2_handle, NUM4_LED_GREEN, LED_OFF);
_zh_relay_off(L4_RELAY);
_is_num_4_fixed = false;
}
break;
default:
break;
}
}
static void _zh_encoder_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
{
zh_encoder_event_on_isr_t *event = event_data;
switch (event->encoder_number)
{
case POWER_ENCODER:
zh_160x_set_cursor(&lcd_handle, 0, 11);
zh_160x_print_int(&lcd_handle, (uint8_t)event->encoder_position);
zh_160x_print_char(&lcd_handle, "% ");
zh_ac_dimmer_set(event->encoder_position);
break;
case COMPONENT_ENCODER:
_zh_component_select((uint8_t)event->encoder_position);
break;
default:
break;
}
}
static void _zh_component_select(uint8_t component)
{
// char text_buffer[LCD_TEXT_BUFFER];
// permitted_channels = component_channel[component];
// zh_avr_160x_set_cursor(&lcd_handle, 1, 10);
// strcpy_P(text_buffer, component_cmm[component]);
// zh_avr_160x_print_char(&lcd_handle, text_buffer);
// zh_avr_160x_set_cursor(&lcd_handle, 2, 0);
// strcpy_P(text_buffer, component_line_1[component]);
// zh_avr_160x_print_char(&lcd_handle, text_buffer);
// zh_avr_160x_set_cursor(&lcd_handle, 3, 0);
// strcpy_P(text_buffer, component_line_2[component]);
// zh_avr_160x_print_char(&lcd_handle, text_buffer);
// zh_avr_pcf8574_write(&led1_handle, is_dmm == true ? 0xF9 : 0xFA);
// zh_avr_pcf8574_write(&led2_handle, 0x0F);
if ((_permitted_channels & CHANNEL1) == CHANNEL1)
{
zh_pcf8574_write_gpio(&_led_1_handle, NUM1_LED_BLUE, LED_ON);
}
if ((_permitted_channels & CHANNEL2) == CHANNEL2)
{
zh_pcf8574_write_gpio(&_led_1_handle, NUM2_LED_BLUE, LED_ON);
}
if ((_permitted_channels & CHANNEL3) == CHANNEL3)
{
zh_pcf8574_write_gpio(&_led_2_handle, NUM3_LED_BLUE, LED_ON);
}
if ((_permitted_channels & CHANNEL4) == CHANNEL4)
{
zh_pcf8574_write_gpio(&_led_2_handle, NUM4_LED_BLUE, LED_ON);
}
}
static void _zh_relay_on(uint8_t relay_number)
{
if (++_used_channels > 1)
{
zh_ac_dimmer_stop();
vTaskDelay(5);
zh_pcf8574_write_gpio(&_relay_handle, relay_number, RELAY_ON);
vTaskDelay(20);
zh_ac_dimmer_start();
}
else
{
_is_work = true;
zh_pcf8574_write_gpio(&_relay_handle, relay_number, RELAY_ON);
zh_pcf8574_write_gpio(&_relay_handle, GROUND_RELAY, RELAY_ON);
vTaskDelay(20);
zh_ac_dimmer_start();
}
}
static void _zh_relay_off(uint8_t relay_number)
{
if (--_used_channels > 0)
{
zh_ac_dimmer_stop();
vTaskDelay(5);
zh_pcf8574_write_gpio(&_relay_handle, relay_number, RELAY_OFF);
vTaskDelay(20);
zh_ac_dimmer_start();
}
else
{
zh_ac_dimmer_stop();
vTaskDelay(5);
zh_pcf8574_write_gpio(&_relay_handle, relay_number, RELAY_OFF);
zh_pcf8574_write_gpio(&_relay_handle, GROUND_RELAY, RELAY_OFF);
_is_work = false;
}
}

View File

@@ -1,3 +1,7 @@
/**
* @file ate0003.h
*/
#pragma once
#include "zh_160x_i2c.h"
@@ -29,6 +33,42 @@
#define LED_2_I2C_ADDRESS 0x21 /*!< U7. */
#define RELAY_I2C_ADDRESS 0x23 /*!< U8. */
#define GROUND_RELAY 0x00 /*!< Relay K1. RL1 pin on U8. */
#define DMM_RELAY 0x01 /*!< Relay K2. RL2 pin on U8. */
#define L1_RELAY 0x03 /*!< Relay K4. RL4 pin on U8. */
#define L2_RELAY 0x04 /*!< Relay K3. RL3 pin on U8. */
#define L3_RELAY 0x05 /*!< Relay K6. RL6 pin on U8. */
#define L4_RELAY 0x02 /*!< Relay K5. RL5 pin on U8. */
#define L5_RELAY 0x06 /*!< Relay K7. RL7 pin on U8. */
#define POWER_ENCODER 0x01 /*!< Connector EN2. */
#define COMPONENT_ENCODER 0x02 /*!< Connector EN1. */
#define DMM_BUTTON 0x00 /*!< Connector BT1. BT1-0 on U5. */
#define FIX_BUTTON 0x01 /*!< Connector BT2. BT2-1 on U5. */
#define NUM1_BUTTON 0x02 /*!< Connector BT3. BT3-2 on U5. */
#define NUM2_BUTTON 0x03 /*!< Connector BT4. BT4-3 on U5. */
#define NUM3_BUTTON 0x04 /*!< Connector BT5. BT5-4 on U5. */
#define NUM4_BUTTON 0x05 /*!< Connector BT6. BT6-5 on U5. */
#define DMM_LED_RED 0x00 /*!< Connector LE1. 2 PIN. L1-0 pin on U6. */
#define DMM_LED_GREEN 0x01 /*!< Connector LE1. 1 PIN. L1-1 pin on U6. */
#define FIX_LED_BLUE 0x02 /*!< Connector LE2. 2 PIN. L2-2 pin on U6. */
#define FIX_LED_GREEN 0x03 /*!< Connector LE2. 1 PIN. L2-3 pin on U6. */
#define NUM1_LED_BLUE 0x04 /*!< Connector LE3. 2 PIN. L3-4 pin on U6. */
#define NUM1_LED_GREEN 0x05 /*!< Connector LE3. 1 PIN. L3-5 pin on U6. */
#define NUM2_LED_BLUE 0x06 /*!< Connector LE4. 2 PIN. L4-6 pin on U6. */
#define NUM2_LED_GREEN 0x07 /*!< Connector LE4. 1 PIN. L4-7 pin on U6. */
#define NUM3_LED_BLUE 0x00 /*!< Connector LE5. 2 PIN. L5-0 pin on U7. */
#define NUM3_LED_GREEN 0x01 /*!< Connector LE5. 1 PIN. L5-1 pin on U7. */
#define NUM4_LED_BLUE 0x02 /*!< Connector LE6. 2 PIN. L6-2 pin on U7. */
#define NUM4_LED_GREEN 0x03 /*!< Connector LE6. 1 PIN. L6-3 pin on U7. */
#define CHANNEL1 0x08
#define CHANNEL2 0x04
#define CHANNEL3 0x02
#define CHANNEL4 0x01
// #define GROUND_RELAY 0x00 /*!< Relay K1. RL1 pin on U5. */
// #define TS_RELAY 0x03 /*!< Relay K4. RL4 pin on U5. */
// #define RET_RELAY 0x02 /*!< Relay K3. RL3 pin on U5. */