-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRgbRfWirelessTransmitter.cpp
55 lines (48 loc) · 1.32 KB
/
RgbRfWirelessTransmitter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
* RGB RF Wireless library v1.0.1 (20190603) made by Rob Bogie
*
* License: GNU LGPLv3. See license.txt
*/
#include "RgbRfWirelessTransmitter.h"
RgbRfTransmitter::RgbRfTransmitter(unsigned long address, byte pin, unsigned int periodusec, byte repeats) {
_address = address;
_pin = pin;
_periodusec = periodusec;
_repeats = (1 << repeats) - 1; // I.e. _repeats = 2^repeats - 1
pinMode(_pin, OUTPUT);
}
void RgbRfTransmitter::sendButton(RgbRfButton button) {
for (int8_t i = _repeats; i >= 0; i--) {
_sendAddress();
for (int8_t i=7; i>=0; i--) {
_sendBit(((uint8_t)button >> i) & 1);
}
_sendStopPulse();
}
}
void RgbRfTransmitter::_sendAddress() {
for (int8_t i=15; i>=0; i--) {
_sendBit((_address >> i) & 1);
}
}
void RgbRfTransmitter::_sendStopPulse() {
digitalWrite(_pin, HIGH);
delayMicroseconds(_periodusec);
digitalWrite(_pin, LOW);
delayMicroseconds(_periodusec * 30);
}
void RgbRfTransmitter::_sendBit(boolean isBitOne) {
if (isBitOne) {
// Send '1' (3T, T)
digitalWrite(_pin, HIGH);
delayMicroseconds(_periodusec * 3);
digitalWrite(_pin, LOW);
delayMicroseconds(_periodusec);
} else {
// Send '0' (T, 3T)
digitalWrite(_pin, HIGH);
delayMicroseconds(_periodusec);
digitalWrite(_pin, LOW);
delayMicroseconds(_periodusec * 3);
}
}