-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNexusTX.cpp
101 lines (86 loc) · 1.72 KB
/
NexusTX.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include "NexusTX.h"
NexusTX::NexusTX(byte tr_pin)
{
SendBuffer = new bool[buffer_size];
TX_PIN = tr_pin;
pinMode(TX_PIN, OUTPUT);
digitalWrite(TX_PIN, LOW);
SendBuffer[9] = 0;
SendBuffer[24] = 1;
SendBuffer[25] = 1;
SendBuffer[26] = 1;
SendBuffer[27] = 1;
}
void NexusTX::tx_bit(bool b)
{
digitalWrite(TX_PIN, HIGH);
delayMicroseconds(PULSE_HIGH);
digitalWrite(TX_PIN, LOW);
if (b == true)
delayMicroseconds(PULSE_ONE);
else
delayMicroseconds(PULSE_ZERO);
}
void NexusTX::setBatteryFlag(bool level)
{
SendBuffer[8] = level;
}
void NexusTX::setHumidity(int h)
{
uint8_t h8 = (uint8_t)h;
for (idx = 0; idx <= 7; idx++)
{
SendBuffer[35 - idx] = (h8 >> idx) & 0x1;
}
}
void NexusTX::setChannel(byte dev_ch)
{
SendBuffer[10] = dev_ch & 0x2;
SendBuffer[11] = dev_ch & 0x1;
}
void NexusTX::setId(byte dev_id)
{
int array_idx = 0;
for (idx = 7; idx >= 0; idx--)
{
SendBuffer[array_idx++] = dev_id & (0x1 << idx);
}
}
void NexusTX::setTemperature(float t)
{
int16_t t12 = t * 10.0f;
for (idx = 0; idx <= 11; idx++)
{
SendBuffer[23 - idx] = (t12 >> idx) & 0x1;
}
}
void NexusTX::SendNexus()
{
for (int i=0; i <buffer_size; i++)
{
tx_bit(SendBuffer[i]);
}
}
void NexusTX::SendPacket()
{
for (idx = 1; idx < repeat; idx++)
{
SendNexus();
if (idx + 1 == repeat) {break;} // do not send sync after last TX
// sync bit
digitalWrite(TX_PIN, HIGH);
delayMicroseconds(PULSE_HIGH);
digitalWrite(TX_PIN, LOW);
delayMicroseconds(PULSE_SYNC);
}
}
bool NexusTX::transmit()
{
if (millis() >= time_marker_send && send_time)
{
time_marker_send = millis() + send_time;
SendPacket();
return true;
}
else return false;
}