Erriez Oregon THN128 433MHz temperature sensor library for Arduino 1.1.1
This is an Oregon THN128 433MHz temperature sensor transmit/receive library for Arduino.
Loading...
Searching...
No Matches
ErriezOregonTHN128Transmit.c
Go to the documentation of this file.
1/*
2 * MIT License
3 *
4 * Copyright (c) 2020-2026 Erriez
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
33#include <Arduino.h>
35
36/* Function prototypes */
37void delay100ms(void) __attribute__((weak));
38extern void delay100ms(void);
39
40/* Static variables */
41#if defined(ARDUINO_ARCH_AVR)
42#include <util/delay.h>
43
44static int8_t _rfTxPort = -1;
45static int8_t _rfTxBit = -1;
46
60#define RF_TX_PIN_INIT(rfTxPin) { \
61 _rfTxPort = digitalPinToPort(rfTxPin); \
62 _rfTxBit = digitalPinToBitMask(rfTxPin); \
63 *portModeRegister(_rfTxPort) |= _rfTxBit; \
64}
65
70#define RF_TX_PIN_DISABLE() { \
71 if ((_rfTxPort >= 0) && (_rfTxBit >= 0)) { \
72 *portModeRegister(_rfTxPort) &= ~_rfTxBit; \
73 } \
74}
75
81#define IS_RF_TX_PIN_INITIALIZED() ((_rfTxPort >= 0) && (_rfTxBit >= 0))
82
87#define RF_TX_PIN_HIGH() { *portOutputRegister(_rfTxPort) |= _rfTxBit; }
88
93#define RF_TX_PIN_LOW() { *portOutputRegister(_rfTxPort) &= ~_rfTxBit; }
94
99#define RF_TX_DELAY_US(us) _delay_us(us)
100
105#define RF_TX_DELAY_MS(ms) _delay_ms(ms)
106
107#elif defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
108static int8_t _rfTxPin = -1;
109
116#define RF_TX_PIN_INIT(rfTxPin) { \
117 _rfTxPin= rfTxPin; \
118 pinMode(_rfTxPin, OUTPUT); \
119}
120
125#define RF_TX_PIN_DISABLE() { \
126 if (_rfTxPin >= 0) { \
127 pinMode(_rfTxPin, INPUT); \
128 } \
129}
130
136#define IS_RF_TX_PIN_INITIALIZED() (_rfTxPin >= 0)
137
142#define RF_TX_PIN_HIGH() { digitalWrite(_rfTxPin, HIGH); }
143
148#define RF_TX_PIN_LOW() { digitalWrite(_rfTxPin, LOW); }
149
154#define RF_TX_DELAY_US(us) delayMicroseconds(us)
155
160#define RF_TX_DELAY_MS(ms) delay(ms)
161#else
162#error "May work, but not tested on this target"
163#endif
164
171static void txSync()
172{
173 /* Transmit sync pulse */
174 RF_TX_PIN_HIGH();
175 RF_TX_DELAY_US(T_SYNC_US);
176 RF_TX_PIN_LOW();
177 RF_TX_DELAY_US(T_SYNC_US);
178}
179
183static void txBit0()
184{
185 /* Transmit data bit 0 pulse */
186 RF_TX_PIN_LOW();
187 RF_TX_DELAY_US(T_BIT_US);
188 RF_TX_PIN_HIGH();
189 RF_TX_DELAY_US(T_BIT_US);
190}
191
195static void txBit1()
196{
197 /* Transmit data bit 1 pulse */
198 RF_TX_PIN_HIGH();
199 RF_TX_DELAY_US(T_BIT_US);
200 RF_TX_PIN_LOW();
201 RF_TX_DELAY_US(T_BIT_US);
202}
203
207static void txDisable()
208{
209 /* Transmit pin low */
210 RF_TX_PIN_LOW();
211}
212
216static void txPreamble()
217{
218 /* Transmit 12 preamble bits 1 */
219 for (uint8_t i = 0; i < 12; i++) {
220 txBit1();
221 }
222 RF_TX_DELAY_US(T_PREAMBLE_SPACE_US);
223}
224
225/* Transmit 32-bit data */
226static void txData(uint32_t data)
227{
228 /* Transmit 32 data bits */
229 for (uint8_t i = 0; i < 32; i++) {
230 if (data & (1UL << i)) {
231 txBit1();
232 } else {
233 txBit0();
234 }
235 }
236}
237
238/*------------------------------------------------------------------------------------------------*/
239/* Public functions */
240/*------------------------------------------------------------------------------------------------*/
248void OregonTHN128_TxBegin(uint8_t rfTxPin)
249{
250 /* Set RF transmit pin output */
251 RF_TX_PIN_INIT(rfTxPin);
252}
253
260{
261 /* Set RF transmit pin input */
262 RF_TX_PIN_DISABLE();
263}
264
270void OregonTHN128_TxRawData(uint32_t rawData)
271{
272 /* Check RF transmit pin initialized */
273 if (!IS_RF_TX_PIN_INITIALIZED()) {
274 return;
275 }
276
277 /* Transmit */
278 txPreamble();
279 txSync();
280 txData(rawData);
281 txDisable();
282}
283
293{
294 // Convert data structure to 32-bit raw data;
295 data->rawData = OregonTHN128_DataToRaw(data);
296
297 // Send raw data
299}
uint32_t OregonTHN128_DataToRaw(OregonTHN128Data_t *data)
Convert data structure to 32-bit raw data.
void OregonTHN128_TxRawData(uint32_t rawData)
Transmit data.
void OregonTHN128_Transmit(OregonTHN128Data_t *data)
Transmit Transmit data.
void OregonTHN128_TxBegin(uint8_t rfTxPin)
Transmit begin.
void OregonTHN128_TxEnd(void)
Disable transmit.
Oregon THN128 433MHz temperature transmit library for Arduino.