1
0
mirror of https://github.com/esphome/esphome.git synced 2025-11-09 19:41:49 +00:00

Compare commits

..

1 Commits

Author SHA1 Message Date
J. Nick Koston
c5014321a6 Expand uart.write tests 2025-11-08 21:15:28 -06:00
4 changed files with 32 additions and 40 deletions

View File

@@ -3,7 +3,7 @@ import esphome.codegen as cg
from esphome.components import audio, audio_dac
import esphome.config_validation as cv
from esphome.const import CONF_DATA, CONF_ID, CONF_VOLUME
from esphome.core import CORE, ID
from esphome.core import CORE
from esphome.coroutine import CoroPriority, coroutine_with_priority
AUTO_LOAD = ["audio"]
@@ -90,10 +90,7 @@ async def speaker_play_action(config, action_id, template_arg, args):
templ = await cg.templatable(data, args, cg.std_vector.template(cg.uint8))
cg.add(var.set_data_template(templ))
else:
# Generate static array in flash to avoid RAM copy
arr_id = ID(f"{action_id}_data", is_declaration=True, type=cg.uint8)
arr = cg.static_const_array(arr_id, cg.ArrayInitializer(*data))
cg.add(var.set_data_static(arr, len(data)))
cg.add(var.set_data_static(data))
return var

View File

@@ -10,33 +10,28 @@ namespace speaker {
template<typename... Ts> class PlayAction : public Action<Ts...>, public Parented<Speaker> {
public:
void set_data_template(std::vector<uint8_t> (*func)(Ts...)) {
this->data_.func = func;
this->len_ = -1; // Sentinel value indicates template mode
void set_data_template(std::function<std::vector<uint8_t>(Ts...)> func) {
this->data_func_ = func;
this->static_ = false;
}
void set_data_static(const uint8_t *data, size_t len) {
this->data_.data = data;
this->len_ = len; // Length >= 0 indicates static mode
void set_data_static(const std::vector<uint8_t> &data) {
this->data_static_ = data;
this->static_ = true;
}
void play(const Ts &...x) override {
if (this->len_ >= 0) {
// Static mode: pass pointer directly to play(const uint8_t *, size_t)
this->parent_->play(this->data_.data, static_cast<size_t>(this->len_));
if (this->static_) {
this->parent_->play(this->data_static_);
} else {
// Template mode: call function and pass vector to play(const std::vector<uint8_t> &)
auto val = this->data_.func(x...);
auto val = this->data_func_(x...);
this->parent_->play(val);
}
}
protected:
ssize_t len_{-1}; // -1 = template mode, >=0 = static mode with length
union Data {
std::vector<uint8_t> (*func)(Ts...); // Function pointer (stateless lambdas)
const uint8_t *data; // Pointer to static data in flash
} data_;
bool static_{false};
std::function<std::vector<uint8_t>(Ts...)> data_func_{};
std::vector<uint8_t> data_static_{};
};
template<typename... Ts> class VolumeSetAction : public Action<Ts...>, public Parented<Speaker> {

View File

@@ -1,12 +1,3 @@
number:
- platform: template
name: "Speaker Number"
id: my_number
optimistic: true
min_value: 0
max_value: 100
step: 1
esphome:
on_boot:
then:
@@ -23,15 +14,6 @@ esphome:
- speaker.finish:
- speaker.stop:
button:
- platform: template
name: "Speaker Button"
on_press:
then:
- speaker.play: [0x10, 0x20, 0x30, 0x40]
- speaker.play: !lambda |-
return {0x01, 0x02, (uint8_t)id(my_number).state};
i2s_audio:
i2s_lrclk_pin: ${i2s_bclk_pin}
i2s_bclk_pin: ${i2s_lrclk_pin}

View File

@@ -3,6 +3,8 @@ esphome:
then:
- uart.write: 'Hello World'
- uart.write: [0x00, 0x20, 0x42]
- uart.write: !lambda |-
return {0xAA, 0xBB, 0xCC};
uart:
- id: uart_uart
@@ -46,6 +48,15 @@ switch:
turn_on: "TURN_ON"
turn_off: "TURN_OFF"
number:
- platform: template
name: "Test Number"
id: test_number
optimistic: true
min_value: 0
max_value: 100
step: 1
button:
# Test uart button with array data
- platform: uart
@@ -57,3 +68,10 @@ button:
name: "UART Button String"
uart_id: uart_uart
data: "BUTTON_PRESS"
# Test uart button with lambda (function pointer)
- platform: template
name: "UART Lambda Test"
on_press:
- uart.write: !lambda |-
std::string cmd = "VALUE=" + str_sprintf("%.0f", id(test_number).state) + "\r\n";
return std::vector<uint8_t>(cmd.begin(), cmd.end());