mirror of
https://github.com/esphome/esphome.git
synced 2025-09-19 03:32:20 +01:00
[nextion] Adds a command pacer with command_spacing
attribute (#7948)
Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
@@ -35,8 +35,54 @@ using nextion_writer_t = std::function<void(Nextion &)>;
|
||||
|
||||
static const std::string COMMAND_DELIMITER{static_cast<char>(255), static_cast<char>(255), static_cast<char>(255)};
|
||||
|
||||
#ifdef USE_NEXTION_COMMAND_SPACING
|
||||
class NextionCommandPacer {
|
||||
public:
|
||||
/**
|
||||
* @brief Creates command pacer with initial spacing
|
||||
* @param initial_spacing Initial time between commands in milliseconds
|
||||
*/
|
||||
explicit NextionCommandPacer(uint8_t initial_spacing = 0) : spacing_ms_(initial_spacing) {}
|
||||
|
||||
/**
|
||||
* @brief Set the minimum time between commands
|
||||
* @param spacing_ms Spacing in milliseconds
|
||||
*/
|
||||
void set_spacing(uint8_t spacing_ms) { spacing_ms_ = spacing_ms; }
|
||||
|
||||
/**
|
||||
* @brief Get current command spacing
|
||||
* @return Current spacing in milliseconds
|
||||
*/
|
||||
uint8_t get_spacing() const { return spacing_ms_; }
|
||||
|
||||
/**
|
||||
* @brief Check if enough time has passed to send next command
|
||||
* @return true if enough time has passed since last command
|
||||
*/
|
||||
bool can_send() const { return (millis() - last_command_time_) >= spacing_ms_; }
|
||||
|
||||
/**
|
||||
* @brief Mark a command as sent, updating the timing
|
||||
*/
|
||||
void mark_sent() { last_command_time_ = millis(); }
|
||||
|
||||
private:
|
||||
uint8_t spacing_ms_;
|
||||
uint32_t last_command_time_{0};
|
||||
};
|
||||
#endif // USE_NEXTION_COMMAND_SPACING
|
||||
|
||||
class Nextion : public NextionBase, public PollingComponent, public uart::UARTDevice {
|
||||
public:
|
||||
#ifdef USE_NEXTION_COMMAND_SPACING
|
||||
/**
|
||||
* @brief Set the command spacing for the display
|
||||
* @param spacing_ms Time in milliseconds between commands
|
||||
*/
|
||||
void set_command_spacing(uint32_t spacing_ms) { this->command_pacer_.set_spacing(spacing_ms); }
|
||||
#endif // USE_NEXTION_COMMAND_SPACING
|
||||
|
||||
/**
|
||||
* Set the text of a component to a static string.
|
||||
* @param component The component name.
|
||||
@@ -1227,6 +1273,9 @@ class Nextion : public NextionBase, public PollingComponent, public uart::UARTDe
|
||||
bool is_connected() { return this->is_connected_; }
|
||||
|
||||
protected:
|
||||
#ifdef USE_NEXTION_COMMAND_SPACING
|
||||
NextionCommandPacer command_pacer_{0};
|
||||
#endif // USE_NEXTION_COMMAND_SPACING
|
||||
std::deque<NextionQueue *> nextion_queue_;
|
||||
std::deque<NextionQueue *> waveform_queue_;
|
||||
uint16_t recv_ret_string_(std::string &response, uint32_t timeout, bool recv_flag);
|
||||
@@ -1360,5 +1409,6 @@ class Nextion : public NextionBase, public PollingComponent, public uart::UARTDe
|
||||
uint32_t started_ms_ = 0;
|
||||
bool sent_setup_commands_ = false;
|
||||
};
|
||||
|
||||
} // namespace nextion
|
||||
} // namespace esphome
|
||||
|
Reference in New Issue
Block a user