Improve performance
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
#include "dmx512.h"
|
||||
#include "esphome/core/log.h"
|
||||
#include "esp32-hal-matrix.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace dmx512 {
|
||||
@@ -8,29 +7,37 @@ namespace dmx512 {
|
||||
static const char *TAG = "dmx512";
|
||||
|
||||
void DMX512::loop() {
|
||||
bool update = false;
|
||||
if(this->update_ || (this->last_update_ + UPDATE_INTERVAL_MS < millis())) {
|
||||
update = true;
|
||||
}
|
||||
if(update) {
|
||||
this->uart_->flush();
|
||||
this->sendBreak();
|
||||
device_values[0] = 0;
|
||||
this->uart_->write_array(device_values, 513);
|
||||
this->device_values_[0] = 0;
|
||||
this->uart_->write_array(this->device_values_, this->max_chan_ + 1);
|
||||
this->update_ = false;
|
||||
this->last_update_ = millis();
|
||||
}
|
||||
}
|
||||
|
||||
void DMX512::sendBreak() {
|
||||
pinMatrixOutDetach(this->tx_pin_, false, false);
|
||||
pinMode(this->tx_pin_, OUTPUT);
|
||||
digitalWrite(this->tx_pin_, LOW);
|
||||
delayMicroseconds(88);
|
||||
digitalWrite(this->tx_pin_, HIGH);
|
||||
delayMicroseconds(1);
|
||||
pinMatrixOutAttach(this->tx_pin_, this->uart_idx_, false, false);
|
||||
pinMatrixOutDetach(this->tx_pin_, false, false);
|
||||
pinMode(this->tx_pin_, OUTPUT);
|
||||
digitalWrite(this->tx_pin_, LOW);
|
||||
delayMicroseconds(DMX_BREAK_LEN);
|
||||
digitalWrite(this->tx_pin_, HIGH);
|
||||
delayMicroseconds(1);
|
||||
pinMatrixOutAttach(this->tx_pin_, this->uart_idx_, false, false);
|
||||
}
|
||||
|
||||
void DMX512::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, "Setting up DMX512...");
|
||||
ESP_LOGCONFIG(TAG, "Setting up DMX512...");
|
||||
}
|
||||
|
||||
void DMX512::setup() {
|
||||
for(int i = 0; i < 513; i++)
|
||||
this->device_values[i] = 0;
|
||||
for(int i = 0; i < DMX_MSG_SIZE; i++)
|
||||
this->device_values_[i] = 0;
|
||||
if(this->pin_enable_) {
|
||||
ESP_LOGD(TAG, "Enabling RS485 module");
|
||||
this->pin_enable_->setup();
|
||||
@@ -38,14 +45,28 @@ void DMX512::setup() {
|
||||
}
|
||||
}
|
||||
|
||||
void DMX512::set_channel_used(uint16_t channel) {
|
||||
if(channel > this->max_chan_)
|
||||
this->max_chan_ = channel;
|
||||
}
|
||||
|
||||
void DMX512::write_channel(uint16_t channel, uint8_t value) {
|
||||
ESP_LOGD(TAG, "write_channel %d: %d", channel, value);
|
||||
this->device_values[channel] = value;
|
||||
this->device_values_[channel] = value;
|
||||
this->update_ = true;
|
||||
}
|
||||
|
||||
void DMX512Output::set_channel(uint16_t channel) {
|
||||
this->channel_ = channel;
|
||||
if(this->universe_) {
|
||||
this->universe_->set_channel_used(channel);
|
||||
}
|
||||
}
|
||||
|
||||
void DMX512Output::write_state(float state) {
|
||||
uint16_t value = state * 0xffff;
|
||||
this->universe_->write_channel(this->channel_, (value >> 8));
|
||||
if(this->universe_)
|
||||
this->universe_->write_channel(this->channel_, (value >> 8));
|
||||
}
|
||||
|
||||
} // namespace dmx512
|
||||
|
||||
@@ -5,6 +5,11 @@
|
||||
#include "esphome/components/output/float_output.h"
|
||||
#include "esp32-hal-matrix.h"
|
||||
|
||||
#define UPDATE_INTERVAL_MS 500
|
||||
#define DMX_MAX_CHANNEL 512
|
||||
#define DMX_MSG_SIZE DMX_MAX_CHANNEL + 1
|
||||
#define DMX_BREAK_LEN 88
|
||||
|
||||
namespace esphome {
|
||||
namespace dmx512 {
|
||||
|
||||
@@ -27,6 +32,8 @@ class DMX512 : public Component {
|
||||
|
||||
void set_uart_tx_pin(int tx_pin) { tx_pin_ = tx_pin; }
|
||||
|
||||
void set_channel_used(uint16_t channel);
|
||||
|
||||
void set_uart_num(int num) {
|
||||
if(num == 0) {
|
||||
this->uart_idx_ = U0TXD_OUT_IDX;
|
||||
@@ -46,21 +53,24 @@ class DMX512 : public Component {
|
||||
esphome::uart::UARTComponent *uart_{nullptr};
|
||||
std::vector<uint8_t> rx_buffer_;
|
||||
uint32_t last_dmx512_transmission_{0};
|
||||
uint8_t device_values[513];
|
||||
uint8_t device_values_[DMX_MSG_SIZE];
|
||||
int uart_idx_{0};
|
||||
int tx_pin_{0};
|
||||
uint16_t max_chan_{0};
|
||||
bool update_{true};
|
||||
unsigned long last_update_{0};
|
||||
GPIOPin *pin_enable_{nullptr};
|
||||
};
|
||||
|
||||
class DMX512Output : public output::FloatOutput, public Component {
|
||||
public:
|
||||
void set_universe(DMX512 *universe) { this->universe_ = universe; }
|
||||
void set_channel(uint16_t channel) { this->channel_ = channel; }
|
||||
void set_channel(uint16_t channel);
|
||||
void write_state(float state);
|
||||
|
||||
protected:
|
||||
uint16_t channel_;
|
||||
DMX512 *universe_;
|
||||
uint16_t channel_{0};
|
||||
DMX512 *universe_{nullptr};
|
||||
};
|
||||
|
||||
} // namespace dmx512
|
||||
|
||||
Reference in New Issue
Block a user