ESPHome  2023.5.5
remote_base.cpp
Go to the documentation of this file.
1 #include "remote_base.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace remote_base {
6 
7 static const char *const TAG = "remote_base";
8 
9 #ifdef USE_ESP32
10 RemoteRMTChannel::RemoteRMTChannel(uint8_t mem_block_num) : mem_block_num_(mem_block_num) {
11  static rmt_channel_t next_rmt_channel = RMT_CHANNEL_0;
12  this->channel_ = next_rmt_channel;
13  next_rmt_channel = rmt_channel_t(int(next_rmt_channel) + mem_block_num);
14 }
15 
16 void RemoteRMTChannel::config_rmt(rmt_config_t &rmt) {
17  if (rmt_channel_t(int(this->channel_) + this->mem_block_num_) >= RMT_CHANNEL_MAX) {
18  this->mem_block_num_ = int(RMT_CHANNEL_MAX) - int(this->channel_);
19  ESP_LOGW(TAG, "Not enough RMT memory blocks available, reduced to %i blocks.", this->mem_block_num_);
20  }
21  rmt.channel = this->channel_;
22  rmt.clk_div = this->clock_divider_;
23  rmt.mem_block_num = this->mem_block_num_;
24 }
25 #endif
26 
27 void RemoteReceiverBinarySensorBase::dump_config() { LOG_BINARY_SENSOR("", "Remote Receiver Binary Sensor", this); }
28 
29 void RemoteTransmitterBase::send_(uint32_t send_times, uint32_t send_wait) {
30 #ifdef ESPHOME_LOG_HAS_VERY_VERBOSE
31  const std::vector<int32_t> &vec = this->temp_.get_data();
32  char buffer[256];
33  uint32_t buffer_offset = 0;
34  buffer_offset += sprintf(buffer, "Sending times=%u wait=%ums: ", send_times, send_wait);
35 
36  for (size_t i = 0; i < vec.size(); i++) {
37  const int32_t value = vec[i];
38  const uint32_t remaining_length = sizeof(buffer) - buffer_offset;
39  int written;
40 
41  if (i + 1 < vec.size()) {
42  written = snprintf(buffer + buffer_offset, remaining_length, "%d, ", value);
43  } else {
44  written = snprintf(buffer + buffer_offset, remaining_length, "%d", value);
45  }
46 
47  if (written < 0 || written >= int(remaining_length)) {
48  // write failed, flush...
49  buffer[buffer_offset] = '\0';
50  ESP_LOGVV(TAG, "%s", buffer);
51  buffer_offset = 0;
52  written = sprintf(buffer, " ");
53  if (i + 1 < vec.size()) {
54  written += sprintf(buffer + written, "%d, ", value);
55  } else {
56  written += sprintf(buffer + written, "%d", value);
57  }
58  }
59 
60  buffer_offset += written;
61  }
62  if (buffer_offset != 0) {
63  ESP_LOGVV(TAG, "%s", buffer);
64  }
65 #endif
66  this->send_internal(send_times, send_wait);
67 }
68 } // namespace remote_base
69 } // namespace esphome
void config_rmt(rmt_config_t &rmt)
Definition: remote_base.cpp:16
RemoteRMTChannel(uint8_t mem_block_num=1)
Definition: remote_base.cpp:10
Definition: a4988.cpp:4
void send_(uint32_t send_times, uint32_t send_wait)
Definition: remote_base.cpp:29