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