ESPHome  2024.10.2
lightwaverf.cpp
Go to the documentation of this file.
1 #include "esphome/core/log.h"
2 
3 #ifdef USE_ESP8266
4 
5 #include "lightwaverf.h"
6 
7 namespace esphome {
8 namespace lightwaverf {
9 
10 static const char *const TAG = "lightwaverf.sensor";
11 
12 static const uint8_t DEFAULT_REPEAT = 10;
13 static const bool DEFAULT_INVERT = false;
14 static const uint32_t DEFAULT_TICK = 330;
15 
17  ESP_LOGCONFIG(TAG, "Setting up Lightwave RF...");
18 
19  this->lwtx_.lwtx_setup(pin_tx_, DEFAULT_REPEAT, DEFAULT_INVERT, DEFAULT_TICK);
20  this->lwrx_.lwrx_setup(pin_rx_);
21 }
22 
23 void LightWaveRF::update() { this->read_tx(); }
24 
26  if (this->lwrx_.lwrx_message()) {
29  }
30 }
31 
32 void LightWaveRF::send_rx(const std::vector<uint8_t> &msg, uint8_t repeats, bool inverted, int u_sec) {
33  this->lwtx_.lwtx_setup(pin_tx_, repeats, inverted, u_sec);
34 
35  uint32_t timeout = 0;
36  if (this->lwtx_.lwtx_free()) {
37  this->lwtx_.lwtx_send(msg);
38  timeout = millis();
39  ESP_LOGD(TAG, "[%i] msg start", timeout);
40  }
41  while (!this->lwtx_.lwtx_free() && millis() < (timeout + 1000)) {
42  delay(10);
43  }
44  timeout = millis() - timeout;
45  ESP_LOGD(TAG, "[%u] msg sent: %i", millis(), timeout);
46 }
47 
48 void LightWaveRF::print_msg_(uint8_t *msg, uint8_t len) {
49  char buffer[65];
50  ESP_LOGD(TAG, " Received code (len:%i): ", len);
51 
52  for (int i = 0; i < len; i++) {
53  sprintf(&buffer[i * 6], "0x%02x, ", msg[i]);
54  }
55  ESP_LOGD(TAG, "[%s]", buffer);
56 }
57 
59  ESP_LOGCONFIG(TAG, "Lightwave RF:");
60  LOG_PIN(" Pin TX: ", this->pin_tx_);
61  LOG_PIN(" Pin RX: ", this->pin_rx_);
62  LOG_UPDATE_INTERVAL(this);
63 }
64 } // namespace lightwaverf
65 } // namespace esphome
66 
67 #endif
bool lwrx_getmessage(uint8_t *buf, uint8_t len)
Transfer a message to user buffer.
Definition: LwRx.cpp:184
uint32_t IRAM_ATTR HOT millis()
Definition: core.cpp:25
void send_rx(const std::vector< uint8_t > &msg, uint8_t repeats, bool inverted, int u_sec)
Definition: lightwaverf.cpp:32
bool lwrx_message()
Test if a message has arrived.
Definition: LwRx.cpp:175
void print_msg_(uint8_t *msg, uint8_t len)
Definition: lightwaverf.cpp:48
std::string size_t len
Definition: helpers.h:292
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void lwtx_setup(InternalGPIOPin *pin, uint8_t repeats, bool inverted, int u_sec)
Set things up to transmit LightWaveRF 434Mhz messages.
Definition: LwTx.cpp:149
void lwrx_setup(InternalGPIOPin *pin)
Set things up to receive LightWaveRF 434Mhz messages pin must be 2 or 3 to trigger interrupts !!! For...
Definition: LwRx.cpp:313
bool lwtx_free()
Check for send free.
Definition: LwTx.cpp:105
void lwtx_send(const std::vector< uint8_t > &msg)
Send a LightwaveRF message (10 nibbles in bytes)
Definition: LwTx.cpp:110
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:26