Initial commit

This commit is contained in:
Tony
2026-04-29 18:53:26 +08:00
commit f4756ce816
45 changed files with 14318 additions and 0 deletions
+6
View File
@@ -0,0 +1,6 @@
idf_component_register(
SRCS "app_main.cpp"
REQUIRES gateway_core dali_domain gateway_runtime log
)
set_property(TARGET ${COMPONENT_LIB} PROPERTY CXX_STANDARD 17)
+237
View File
@@ -0,0 +1,237 @@
menu "Gateway App"
config GATEWAY_CHANNEL_COUNT
int "Gateway channel count"
range 1 2
default 2
help
Number of logical DALI gateway channels exposed by the native gateway.
menu "Gateway Channel 1"
config GATEWAY_CHANNEL1_GW_ID
int "Gateway id"
range 0 255
default 3
help
Lua-compatible gateway id for the first DALI channel.
choice GATEWAY_CHANNEL1_PHY
prompt "Channel 1 PHY"
default GATEWAY_CHANNEL1_PHY_DISABLED
help
Select the physical transport used by channel 1.
config GATEWAY_CHANNEL1_PHY_DISABLED
bool "Disabled"
config GATEWAY_CHANNEL1_PHY_NATIVE
bool "Native DALI GPIO"
config GATEWAY_CHANNEL1_PHY_UART1
bool "Serial PHY on UART1"
config GATEWAY_CHANNEL1_PHY_UART2
bool "Serial PHY on UART2"
endchoice
config GATEWAY_CHANNEL1_NATIVE_BUS_ID
int "Native DALI bus id"
depends on GATEWAY_CHANNEL1_PHY_NATIVE
range 0 15
default 0
help
Logical DALI HAL bus index for channel 1.
config GATEWAY_CHANNEL1_NATIVE_TX_PIN
int "Native DALI TX pin"
depends on GATEWAY_CHANNEL1_PHY_NATIVE
range 0 48
default 18
help
ESP32-S3 GPIO used for channel 1 native DALI transmit.
config GATEWAY_CHANNEL1_NATIVE_RX_PIN
int "Native DALI RX pin"
depends on GATEWAY_CHANNEL1_PHY_NATIVE
range 0 48
default 8
help
ESP32-S3 GPIO used for channel 1 native DALI receive.
config GATEWAY_CHANNEL1_NATIVE_BAUDRATE
int "Native DALI baudrate"
depends on GATEWAY_CHANNEL1_PHY_NATIVE
range 400 2400
default 1200
help
Runtime baudrate used for channel 1 native DALI timing.
config GATEWAY_CHANNEL1_SERIAL_TX_PIN
int "Serial PHY TX pin"
depends on GATEWAY_CHANNEL1_PHY_UART1 || GATEWAY_CHANNEL1_PHY_UART2
range 0 48
default 1
help
ESP32-S3 GPIO used by the channel 1 serial PHY transmit pin.
config GATEWAY_CHANNEL1_SERIAL_RX_PIN
int "Serial PHY RX pin"
depends on GATEWAY_CHANNEL1_PHY_UART1 || GATEWAY_CHANNEL1_PHY_UART2
range 0 48
default 2
help
ESP32-S3 GPIO used by the channel 1 serial PHY receive pin.
config GATEWAY_CHANNEL1_SERIAL_BAUDRATE
int "Serial PHY baudrate"
depends on GATEWAY_CHANNEL1_PHY_UART1 || GATEWAY_CHANNEL1_PHY_UART2
range 1200 921600
default 9600
help
UART baudrate used by the channel 1 serial PHY.
config GATEWAY_CHANNEL1_SERIAL_RX_BUFFER
int "Serial PHY RX buffer bytes"
depends on GATEWAY_CHANNEL1_PHY_UART1 || GATEWAY_CHANNEL1_PHY_UART2
range 128 4096
default 512
endmenu
menu "Gateway Channel 2"
config GATEWAY_CHANNEL2_GW_ID
int "Gateway id"
depends on GATEWAY_CHANNEL_COUNT >= 2
range 0 255
default 4
help
Lua-compatible gateway id for the second DALI channel.
choice GATEWAY_CHANNEL2_PHY
prompt "Channel 2 PHY"
depends on GATEWAY_CHANNEL_COUNT >= 2
default GATEWAY_CHANNEL2_PHY_DISABLED
help
Select the physical transport used by channel 2.
config GATEWAY_CHANNEL2_PHY_DISABLED
bool "Disabled"
config GATEWAY_CHANNEL2_PHY_NATIVE
bool "Native DALI GPIO"
config GATEWAY_CHANNEL2_PHY_UART1
bool "Serial PHY on UART1"
config GATEWAY_CHANNEL2_PHY_UART2
bool "Serial PHY on UART2"
endchoice
config GATEWAY_CHANNEL2_NATIVE_BUS_ID
int "Native DALI bus id"
depends on GATEWAY_CHANNEL_COUNT >= 2 && GATEWAY_CHANNEL2_PHY_NATIVE
range 0 15
default 1
help
Logical DALI HAL bus index for channel 2.
config GATEWAY_CHANNEL2_NATIVE_TX_PIN
int "Native DALI TX pin"
depends on GATEWAY_CHANNEL_COUNT >= 2 && GATEWAY_CHANNEL2_PHY_NATIVE
range 0 48
default 19
help
ESP32-S3 GPIO used for channel 2 native DALI transmit.
config GATEWAY_CHANNEL2_NATIVE_RX_PIN
int "Native DALI RX pin"
depends on GATEWAY_CHANNEL_COUNT >= 2 && GATEWAY_CHANNEL2_PHY_NATIVE
range 0 48
default 9
help
ESP32-S3 GPIO used for channel 2 native DALI receive.
config GATEWAY_CHANNEL2_NATIVE_BAUDRATE
int "Native DALI baudrate"
depends on GATEWAY_CHANNEL_COUNT >= 2 && GATEWAY_CHANNEL2_PHY_NATIVE
range 400 2400
default 1200
help
Runtime baudrate used for channel 2 native DALI timing.
config GATEWAY_CHANNEL2_SERIAL_TX_PIN
int "Serial PHY TX pin"
depends on GATEWAY_CHANNEL_COUNT >= 2 && (GATEWAY_CHANNEL2_PHY_UART1 || GATEWAY_CHANNEL2_PHY_UART2)
range 0 48
default 6
help
ESP32-S3 GPIO used by the channel 2 serial PHY transmit pin.
config GATEWAY_CHANNEL2_SERIAL_RX_PIN
int "Serial PHY RX pin"
depends on GATEWAY_CHANNEL_COUNT >= 2 && (GATEWAY_CHANNEL2_PHY_UART1 || GATEWAY_CHANNEL2_PHY_UART2)
range 0 48
default 7
help
ESP32-S3 GPIO used by the channel 2 serial PHY receive pin.
config GATEWAY_CHANNEL2_SERIAL_BAUDRATE
int "Serial PHY baudrate"
depends on GATEWAY_CHANNEL_COUNT >= 2 && (GATEWAY_CHANNEL2_PHY_UART1 || GATEWAY_CHANNEL2_PHY_UART2)
range 1200 921600
default 9600
help
UART baudrate used by the channel 2 serial PHY.
config GATEWAY_CHANNEL2_SERIAL_RX_BUFFER
int "Serial PHY RX buffer bytes"
depends on GATEWAY_CHANNEL_COUNT >= 2 && (GATEWAY_CHANNEL2_PHY_UART1 || GATEWAY_CHANNEL2_PHY_UART2)
range 128 4096
default 512
endmenu
config GATEWAY_ENABLE_DALI_BUS
bool "Legacy single local DALI bus switch"
default n
help
Legacy compatibility option retained for existing sdkconfig files. New code uses the
per-channel PHY selectors above.
config GATEWAY_DALI_BUS_ID
int "DALI bus id"
depends on GATEWAY_ENABLE_DALI_BUS
range 0 15
default 0
help
Logical DALI bus index used by the HAL queue set.
config GATEWAY_DALI_TX_PIN
int "DALI TX pin"
depends on GATEWAY_ENABLE_DALI_BUS
range 0 48
default 18
help
ESP32-S3 GPIO used for the DALI transmit pin.
config GATEWAY_DALI_RX_PIN
int "DALI RX pin"
depends on GATEWAY_ENABLE_DALI_BUS
range 0 48
default 8
help
ESP32-S3 GPIO used for the DALI receive pin.
config GATEWAY_DALI_BAUDRATE
int "DALI baudrate"
depends on GATEWAY_ENABLE_DALI_BUS
range 400 2400
default 1200
help
Runtime baudrate used when initializing the local DALI bus.
endmenu
+126
View File
@@ -0,0 +1,126 @@
#include "dali_domain.hpp"
#include "gateway_core.hpp"
#include "gateway_runtime.hpp"
#include "sdkconfig.h"
#include <cstdio>
#ifndef CONFIG_GATEWAY_CHANNEL_COUNT
#define CONFIG_GATEWAY_CHANNEL_COUNT 2
#endif
namespace {
constexpr const char* kProjectName = "DALI_485_Gateway";
constexpr const char* kProjectVersion = "0.1.0";
[[maybe_unused]] void LogBindError(const char* channel_name, esp_err_t err) {
if (err != ESP_OK) {
std::printf("gateway_main: failed to bind %s err=%d\n", channel_name, err);
}
}
void BindConfiguredChannels(gateway::DaliDomainService& dali_domain,
const gateway::GatewayRuntime& runtime) {
#if CONFIG_GATEWAY_CHANNEL1_PHY_NATIVE
gateway::DaliHardwareBusConfig channel1{};
channel1.channel_index = 0;
channel1.gateway_id = static_cast<uint8_t>(CONFIG_GATEWAY_CHANNEL1_GW_ID);
channel1.bus_id = static_cast<uint8_t>(CONFIG_GATEWAY_CHANNEL1_NATIVE_BUS_ID);
channel1.tx_pin = static_cast<uint8_t>(CONFIG_GATEWAY_CHANNEL1_NATIVE_TX_PIN);
channel1.rx_pin = static_cast<uint8_t>(CONFIG_GATEWAY_CHANNEL1_NATIVE_RX_PIN);
channel1.baudrate = static_cast<uint32_t>(CONFIG_GATEWAY_CHANNEL1_NATIVE_BAUDRATE);
channel1.name = runtime.gatewayName(channel1.gateway_id);
LogBindError("channel1 native DALI", dali_domain.bindHardwareBus(channel1));
#elif CONFIG_GATEWAY_CHANNEL1_PHY_UART1 || CONFIG_GATEWAY_CHANNEL1_PHY_UART2
gateway::DaliSerialBusConfig channel1{};
channel1.channel_index = 0;
channel1.gateway_id = static_cast<uint8_t>(CONFIG_GATEWAY_CHANNEL1_GW_ID);
#if CONFIG_GATEWAY_CHANNEL1_PHY_UART1
channel1.uart_port = 1;
#else
channel1.uart_port = 2;
#endif
channel1.tx_pin = CONFIG_GATEWAY_CHANNEL1_SERIAL_TX_PIN;
channel1.rx_pin = CONFIG_GATEWAY_CHANNEL1_SERIAL_RX_PIN;
channel1.baudrate = static_cast<uint32_t>(CONFIG_GATEWAY_CHANNEL1_SERIAL_BAUDRATE);
channel1.rx_buffer_size = static_cast<size_t>(CONFIG_GATEWAY_CHANNEL1_SERIAL_RX_BUFFER);
channel1.tx_buffer_size = static_cast<size_t>(CONFIG_GATEWAY_CHANNEL1_SERIAL_RX_BUFFER);
channel1.name = runtime.gatewayName(channel1.gateway_id);
LogBindError("channel1 serial DALI", dali_domain.bindSerialBus(channel1));
#endif
#if CONFIG_GATEWAY_CHANNEL_COUNT >= 2
#if CONFIG_GATEWAY_CHANNEL2_PHY_NATIVE
gateway::DaliHardwareBusConfig channel2{};
channel2.channel_index = 1;
channel2.gateway_id = static_cast<uint8_t>(CONFIG_GATEWAY_CHANNEL2_GW_ID);
channel2.bus_id = static_cast<uint8_t>(CONFIG_GATEWAY_CHANNEL2_NATIVE_BUS_ID);
channel2.tx_pin = static_cast<uint8_t>(CONFIG_GATEWAY_CHANNEL2_NATIVE_TX_PIN);
channel2.rx_pin = static_cast<uint8_t>(CONFIG_GATEWAY_CHANNEL2_NATIVE_RX_PIN);
channel2.baudrate = static_cast<uint32_t>(CONFIG_GATEWAY_CHANNEL2_NATIVE_BAUDRATE);
channel2.name = runtime.gatewayName(channel2.gateway_id);
LogBindError("channel2 native DALI", dali_domain.bindHardwareBus(channel2));
#elif CONFIG_GATEWAY_CHANNEL2_PHY_UART1 || CONFIG_GATEWAY_CHANNEL2_PHY_UART2
gateway::DaliSerialBusConfig channel2{};
channel2.channel_index = 1;
channel2.gateway_id = static_cast<uint8_t>(CONFIG_GATEWAY_CHANNEL2_GW_ID);
#if CONFIG_GATEWAY_CHANNEL2_PHY_UART1
channel2.uart_port = 1;
#else
channel2.uart_port = 2;
#endif
channel2.tx_pin = CONFIG_GATEWAY_CHANNEL2_SERIAL_TX_PIN;
channel2.rx_pin = CONFIG_GATEWAY_CHANNEL2_SERIAL_RX_PIN;
channel2.baudrate = static_cast<uint32_t>(CONFIG_GATEWAY_CHANNEL2_SERIAL_BAUDRATE);
channel2.rx_buffer_size = static_cast<size_t>(CONFIG_GATEWAY_CHANNEL2_SERIAL_RX_BUFFER);
channel2.tx_buffer_size = static_cast<size_t>(CONFIG_GATEWAY_CHANNEL2_SERIAL_RX_BUFFER);
channel2.name = runtime.gatewayName(channel2.gateway_id);
LogBindError("channel2 serial DALI", dali_domain.bindSerialBus(channel2));
#endif
#endif
}
} // namespace
extern "C" void app_main(void) {
ESP_ERROR_CHECK(gateway::InitializeRuntimeNvs());
const gateway::BootProfile profile{
gateway::AppRole::kGateway,
"gateway",
true,
true,
true,
true,
true,
};
gateway::GatewayCore core(profile);
core.start();
gateway::DaliDomainService dali_domain;
gateway::GatewayRuntime runtime(
profile,
gateway::GatewayRuntimeConfig{
kProjectName,
kProjectVersion,
gateway::ReadRuntimeSerialId(),
},
&dali_domain);
ESP_ERROR_CHECK(runtime.start());
runtime.setGatewayCount(CONFIG_GATEWAY_CHANNEL_COUNT);
BindConfiguredChannels(dali_domain, runtime);
const auto device_info = runtime.deviceInfo();
std::printf("gateway_main: dali domain implementation=%s bound=%d channels=%u\n",
dali_domain.implementationName(), dali_domain.isBound(),
static_cast<unsigned>(dali_domain.channelCount()));
for (const auto& channel : dali_domain.channelInfo()) {
std::printf("gateway_main: channel=%u gateway=%u name=%s\n", channel.channel_index,
channel.gateway_id, channel.name.c_str());
}
std::printf("gateway_main: runtime device type=%s serial=%s project=%s version=%s\n",
device_info.type.c_str(), device_info.serial_id.c_str(),
device_info.project.c_str(), device_info.version.c_str());
}