-
Notifications
You must be signed in to change notification settings - Fork 57
/
RF.h
53 lines (49 loc) · 1.01 KB
/
RF.h
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
/*
Released under Creative Commons Attribution 4.0
by bitluni 2016
https://creativecommons.org/licenses/by/4.0/
Attribution means you can use it however you like as long you
mention that it's base on my stuff.
I'll be pleased if you'd do it by sharing http://youtube.com/bitlunislab
*/
//Works with BH9938 with t=200
inline void rfPreamble(int pin, int t)
{
int m = micros();
digitalWrite(pin, 1);
while(micros() - m < t);
digitalWrite(pin, 0);
while(micros() - m < t * 32);
}
inline void rfWriteBit(int pin, int t, int b)
{
int m = micros();
if(b)
{
digitalWrite(pin, 1);
while(micros() - m < t * 3);
digitalWrite(pin, 0);
while(micros() - m < t * 4);
}
else
{
digitalWrite(pin, 1);
while(micros() - m < t);
digitalWrite(pin, 0);
while(micros() - m < t * 4);
}
}
void rfWriteCode(int pin, int t, int code, int data)
{
rfPreamble(pin, t);
for(int i = 0; i < 20; i++)
{
rfWriteBit(pin, t, code & 1);
code >>= 1;
}
for(int i = 0; i < 4; i++)
{
rfWriteBit(pin, t, data & 1);
data >>= 1;
}
}