diff --git a/components/dmx/CMakeLists.txt b/components/dmx/CMakeLists.txt index 7b3abdc..18becaa 100644 --- a/components/dmx/CMakeLists.txt +++ b/components/dmx/CMakeLists.txt @@ -1 +1,8 @@ -idf_component_register(SRCS "src/dmx.c" INCLUDE_DIRS "include") +idf_component_register( + SRCS + "src/dmx.c" + INCLUDE_DIRS + "include" + REQUIRES + logger + esp_dmx) diff --git a/components/dmx/include/dmx.h b/components/dmx/include/dmx.h index cd74574..dac038d 100644 --- a/components/dmx/include/dmx.h +++ b/components/dmx/include/dmx.h @@ -1,9 +1,25 @@ #pragma once +#include "esp_dmx.h" + #ifdef __cplusplus extern "C" { #endif +/** + * @brief Initialize the DMX interface. + * @param port The DMX port to initialize. + * @param pin_tx The pin for TX of the RS485. + * @param pin_rx The pin for RX of the RS485. + */ +void init_dmx(dmx_port_t port, int pin_tx, int pin_rx); + +/** + * @brief Send some test DMX data on the specified port. + * @param port The DMX port to send data on. + */ +void send_dmx(dmx_port_t port); + #ifdef __cplusplus } #endif diff --git a/components/dmx/src/dmx.c b/components/dmx/src/dmx.c index e69de29..3dd1c12 100644 --- a/components/dmx/src/dmx.c +++ b/components/dmx/src/dmx.c @@ -0,0 +1,43 @@ +#define LOG_TAG "DMX" ///< "DMX" log tag for this file + +#include + +#include "dmx.h" +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "logger.h" + +static void dmx_sender_task(void *pv_parameters) { + dmx_port_t port = (dmx_port_t)(intptr_t)pv_parameters; + TickType_t last_wake_time = xTaskGetTickCount(); + + while (1) { + send_dmx(port); + vTaskDelayUntil(&last_wake_time, pdMS_TO_TICKS(1000)); + } +} + +void init_dmx(dmx_port_t port, int pin_tx, int pin_rx) { + LOGI("Initializing DMX port %d on pin %d (TX) and %d (RX)", port, pin_tx, + pin_rx); + + dmx_config_t config = DMX_CONFIG_DEFAULT; + dmx_personality_t personalities[] = {}; + dmx_driver_install(port, &config, personalities, 0); + + dmx_set_pin(port, pin_tx, pin_rx, DMX_PIN_NO_CHANGE); // RTS pin is not used + + BaseType_t task_created = xTaskCreate(dmx_sender_task, "dmx_sender", 2048, + (void *)(intptr_t)port, 5, NULL); + if (task_created != pdPASS) { + LOGE("Failed to create DMX sender task for port %d", port); + } +} + +void send_dmx(dmx_port_t port) { + LOGD("Sending DMX data on port %d", port); + + uint8_t dmx_data[DMX_PACKET_SIZE] = {DMX_SC, 0, 0, 0, 255, 100, 0, 100, 0}; + dmx_write(port, dmx_data, DMX_PACKET_SIZE); + dmx_send(port); +} diff --git a/include/README b/include/README new file mode 100644 index 0000000..49819c0 --- /dev/null +++ b/include/README @@ -0,0 +1,37 @@ + +This directory is intended for project header files. + +A header file is a file containing C declarations and macro definitions +to be shared between several project source files. You request the use of a +header file in your project source file (C, C++, etc) located in `src` folder +by including it, with the C preprocessing directive `#include'. + +```src/main.c + +#include "header.h" + +int main (void) +{ + ... +} +``` + +Including a header file produces the same results as copying the header file +into each source file that needs it. Such copying would be time-consuming +and error-prone. With a header file, the related declarations appear +in only one place. If they need to be changed, they can be changed in one +place, and programs that include the header file will automatically use the +new version when next recompiled. The header file eliminates the labor of +finding and changing all the copies as well as the risk that a failure to +find one copy will result in inconsistencies within a program. + +In C, the convention is to give header files names that end with `.h'. + +Read more about using header files in official GCC documentation: + +* Include Syntax +* Include Operation +* Once-Only Headers +* Computed Includes + +https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 07d74e6..5b7abd5 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -4,5 +4,6 @@ idf_component_register( INCLUDE_DIRS "." REQUIRES - web_server - logger) + dmx + logger + web_server) diff --git a/main/dmx-interface.c b/main/dmx-interface.c index fb94c1b..c7e6570 100644 --- a/main/dmx-interface.c +++ b/main/dmx-interface.c @@ -2,6 +2,7 @@ #include +#include "dmx.h" #include "esp_err.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" @@ -34,6 +35,8 @@ void app_main(void) { LOGI("Web server started successfully"); LOGI("Open http://192.168.4.1 in your browser"); + init_dmx(DMX_NUM_1, 21, 33); + // Keep the app running while (1) { vTaskDelay(pdMS_TO_TICKS(1000));