ESPHome 2026.5.0
Loading...
Searching...
No Matches
LwRx.h
Go to the documentation of this file.
1#pragma once
2
4#include "esphome/core/hal.h"
5
6namespace esphome::lightwaverf {
7
8// LwRx.h
9//
10// LightwaveRF 434MHz receiver for Arduino
11//
12// Author: Bob Tidey (robert@tideys.net)
13
14static const uint8_t RX_STAT_HIGH_AVE = 0;
15static const uint8_t RX_STAT_HIGH_MAX = 1;
16static const uint8_t RX_STAT_HIGH_MIN = 2;
17static const uint8_t RX_STAT_LOW0_AVE = 3;
18static const uint8_t RX_STAT_LOW0_MAX = 4;
19static const uint8_t RX_STAT_LOW0_MIN = 5;
20static const uint8_t RX_STAT_LOW1_AVE = 6;
21static const uint8_t RX_STAT_LOW1_MAX = 7;
22static const uint8_t RX_STAT_LOW1_MIN = 8;
23static const uint8_t RX_STAT_COUNT = 9;
24
25// sets maximum number of pairings which can be held
26static const uint8_t RX_MAXPAIRS = 10;
27
28static const uint8_t RX_NIBBLE[] = {0xF6, 0xEE, 0xED, 0xEB, 0xDE, 0xDD, 0xDB, 0xBE,
29 0xBD, 0xBB, 0xB7, 0x7E, 0x7D, 0x7B, 0x77, 0x6F};
30static const uint8_t RX_CMD_OFF = 0xF6; // raw 0
31static const uint8_t RX_CMD_ON = 0xEE; // raw 1
32static const uint8_t RX_CMD_MOOD = 0xED; // raw 2
33static const uint8_t RX_PAR0_ALLOFF = 0x7D; // param 192-255 all off (12 in msb)
34static const uint8_t RX_DEV_15 = 0x6F; // device 15
35
36static const uint8_t RX_MSGLEN = 10; // expected length of rx message
37
38static const uint8_t RX_STATE_IDLE = 0;
39static const uint8_t RX_STATE_MSGSTARTFOUND = 1;
40static const uint8_t RX_STATE_BYTESTARTFOUND = 2;
41static const uint8_t RX_STATE_GETBYTE = 3;
42
43// Gather stats for pulse widths (ave is x 16)
44static const uint16_t LWRX_STATSDFLT[RX_STAT_COUNT] = {5000, 0, 5000, 20000, 0, 2500, 4000, 0, 500}; // usigned int
45
46class LwRx {
47 public:
48 // Seup must be called once, set up pin used to receive data
49 void lwrx_setup(InternalGPIOPin *pin);
50
51 // Set translate to determine whether translating from nibbles to bytes in message
52 // Translate off only applies to 10char message returns
53 void lwrx_settranslate(bool translate);
54
55 // Check to see whether message available
56 bool lwrx_message();
57
58 // Get a message, len controls format (2 cmd+param, 4 cmd+param+room+device),10 full message
59 bool lwrx_getmessage(uint8_t *buf, uint8_t len);
60
61 // Setup repeat filter
62 void lwrx_setfilter(uint8_t repeats, uint8_t timeout);
63
64 // Add pair, if no pairing set then all messages are received, returns number of pairs
65 uint8_t lwrx_addpair(const uint8_t *pairdata);
66
67 // Get pair data into buffer for the pairnumber. Returns current paircount
68 // Use pairnumber 255 to just get current paircount
69 uint8_t lwrx_getpair(uint8_t *pairdata, uint8_t pairnumber);
70
71 // Make a pair from next message received within timeout 100mSec
72 // This call returns immediately whilst message checking continues
73 void lwrx_makepair(uint8_t timeout);
74
75 // Set pair mode controls
76 void lwrx_set_pair_mode(bool pair_enforce, bool pair_base_only);
77
78 // Returns time from last packet received in msec
79 // Can be used to determine if Rx may be still receiving repeats
81
82 static void rx_process_bits(LwRx *arg);
83
84 // Pairing data
85 uint8_t rx_paircount = 0;
86 uint8_t rx_pairs[RX_MAXPAIRS][8];
87 // set false to responds to all messages if no pairs set up
88 bool rx_pairEnforce = false;
89 // set false to use Address, Room and Device in pairs, true just the Address part
90 bool rx_pairBaseOnly = false;
91
92 uint8_t rx_pairtimeout = 0; // 100msec units
93
94 // Repeat filters
95 uint8_t rx_repeats = 2; // msg must be repeated at least this number of times
96 uint8_t rx_repeatcount = 0;
97 uint8_t rx_timeout = 20; // reset repeat window after this in 100mSecs
98 uint32_t rx_prevpkttime = 0; // last packet time in milliseconds
99 uint32_t rx_pairstarttime = 0; // last msg time in milliseconds
100
101 // Receive mode constants and variables
102 uint8_t rx_msg[RX_MSGLEN]; // raw message received
103 uint8_t rx_buf[RX_MSGLEN]; // message buffer during reception
104
105 uint32_t rx_prev; // time of previous interrupt in microseconds
106
107 volatile bool rx_msgcomplete = false; // set high when message available
108 bool rx_translate = true; // Set false to get raw data
109
110 uint8_t rx_state = 0;
111
112 uint8_t rx_num_bits = 0; // number of bits in the current uint8_t
113 uint8_t rx_num_bytes = 0; // number of bytes received
114
115 uint16_t lwrx_stats[RX_STAT_COUNT]; // unsigned int
116
117 bool lwrx_stats_enable = true;
118
119 protected:
120 void lwrx_clearpairing_();
121
122 // Return stats on pulse timings
123 bool lwrx_getstats_(uint16_t *stats);
124
125 // Enable collection of stats on pulse timings
126 void lwrx_setstatsenable_(bool rx_stats_enable);
127
128 // internal support functions
129 bool rx_report_message_();
130 int16_t rx_find_nibble_(uint8_t data); // int
131 void rx_addpairfrommsg_();
132 void rx_paircommit_();
133 void rx_remove_pair_(uint8_t *buf);
134 int16_t rx_check_pairs_(const uint8_t *buf, bool all_devices); // int
135
138};
139
140} // namespace esphome::lightwaverf
Copy of GPIOPin that is safe to use from ISRs (with no virtual functions)
Definition gpio.h:92
int16_t rx_find_nibble_(uint8_t data)
Find nibble from byte returns -1 if none found.
Definition LwRx.cpp:348
volatile bool rx_msgcomplete
Definition LwRx.h:107
uint8_t rx_buf[RX_MSGLEN]
Definition LwRx.h:103
bool rx_report_message_()
Check a message to see if it should be reported under pairing / mood / all off rules returns -1 if no...
Definition LwRx.cpp:333
void rx_paircommit_()
check and commit pair
Definition LwRx.cpp:371
uint16_t lwrx_stats[RX_STAT_COUNT]
Definition LwRx.h:115
int16_t rx_check_pairs_(const uint8_t *buf, bool all_devices)
Check to see if message matches one of the pairs if mode is pairBase only then ignore device and room...
Definition LwRx.cpp:383
bool lwrx_getstats_(uint16_t *stats)
Return stats on high and low pulses.
Definition LwRx.cpp:288
uint32_t lwrx_packetinterval()
Return time in milliseconds since last packet received.
Definition LwRx.cpp:232
ISRInternalGPIOPin rx_pin_isr_
Definition LwRx.h:136
void lwrx_set_pair_mode(bool pair_enforce, bool pair_base_only)
Set pairs behaviour.
Definition LwRx.cpp:310
void lwrx_setup(InternalGPIOPin *pin)
Set things up to receive LightWaveRF 434Mhz messages pin must be 2 or 3 to trigger interrupts !...
Definition LwRx.cpp:320
void rx_addpairfrommsg_()
add pair from message buffer
Definition LwRx.cpp:361
uint8_t lwrx_getpair(uint8_t *pairdata, uint8_t pairnumber)
Get pair data (translated back to nibble form.
Definition LwRx.cpp:268
InternalGPIOPin * rx_pin_
Definition LwRx.h:137
uint32_t rx_pairstarttime
Definition LwRx.h:99
uint8_t rx_msg[RX_MSGLEN]
Definition LwRx.h:102
void lwrx_setstatsenable_(bool rx_stats_enable)
Set stats mode.
Definition LwRx.cpp:300
uint8_t lwrx_addpair(const uint8_t *pairdata)
Add a pair to filter received messages pairdata is device,dummy,5*addr,room pairdata is held in trans...
Definition LwRx.cpp:247
bool lwrx_message()
Test if a message has arrived.
Definition LwRx.cpp:175
uint32_t rx_prevpkttime
Definition LwRx.h:98
void rx_remove_pair_(uint8_t *buf)
Remove an existing pair matching the buffer.
Definition LwRx.cpp:419
uint8_t rx_pairs[RX_MAXPAIRS][8]
Definition LwRx.h:86
static void rx_process_bits(LwRx *arg)
Pin change interrupt routine that identifies 1 and 0 LightwaveRF bits and constructs a message when a...
Definition LwRx.cpp:20
void lwrx_setfilter(uint8_t repeats, uint8_t timeout)
Set up repeat filtering of received messages.
Definition LwRx.cpp:237
void lwrx_settranslate(bool translate)
Set translate mode.
Definition LwRx.cpp:180
void lwrx_makepair(uint8_t timeout)
Make a pair from next message successfully received.
Definition LwRx.cpp:260
void lwrx_clearpairing_()
Clear all pairing.
Definition LwRx.cpp:283
bool lwrx_getmessage(uint8_t *buf, uint8_t len)
Transfer a message to user buffer.
Definition LwRx.cpp:184
std::string size_t len
static void uint32_t