-
Notifications
You must be signed in to change notification settings - Fork 0
/
Proove.cpp
129 lines (108 loc) · 2.26 KB
/
Proove.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include "Proove.h"
#include <math.h>
#include <bitset>
using namespace std;
Proove::Proove(int gpio_pin, int transmitter_id_dec)
{
this->gpio_pin = gpio_pin;
int maxid = pow(2,26)-1;
if(transmitter_id_dec > maxid)
{
throw "transmitter_id can not be larger than 2^26-1";
}
this->transmitter_id = std::bitset<26>(transmitter_id_dec).to_string();
if(wiringPiSetupGpio() == -1)
{
printf("%s \n", "Failed to setup wiringPi");
}
pinMode(this->gpio_pin, OUTPUT);
}
Proove::~Proove()
{
}
void Proove::channel_on(int channel_id)
{
this->trigger(this->off, this->on, this->channel_id[channel_id]);
}
void Proove::channel_off(int channel_id)
{
this->trigger(this->off, this->off, this->channel_id[channel_id]);
}
void Proove::group_on()
{
this->trigger(this->on, this->on, this->channel_id[0]);
}
void Proove::group_off()
{
this->trigger(this->on, this->off, this->channel_id[0]);
}
void Proove::trigger(string group, string state, string channel)
{
stringstream ss;
ss << this->transmitter_id;
ss << group;
ss << state;
ss << this->channel_id[0];
ss << channel;
string data = ss.str();
string packet = this->encode(data);
for(int i = 0; i < this->tx_repeat; i++)
{
this->tx_packet(packet);
}
}
string Proove::encode(string data)
{
stringstream ss;
for(char& c : data)
{
ss << c;
if(c == '0')
{
ss << '1';
}
else
{
ss << '0';
}
}
return ss.str();
}
void Proove::tx_packet(string packet)
{
this->tx_sync();
for(char& c : packet)
{
if(c == '0')
{
this->tx_l0();
}
else
{
this->tx_l1();
}
}
}
void Proove::tx_sync()
{
this->tx_waveform(this->tSyncHigh, this->tSyncLow);
}
void Proove::tx_l0()
{
this->tx_waveform(this->tZeroHigh, this->tZeroLow);
}
void Proove::tx_l1()
{
this->tx_waveform(this->tOneHigh, this->tOneLow);
}
void Proove::tx_pause()
{
this->tx_waveform(this->tPauseHigh, this->tPauseLow);
}
void Proove::tx_waveform(int high_pulse, int low_pulse)
{
digitalWrite(this->gpio_pin, HIGH);
usleep(high_pulse);
digitalWrite(this->gpio_pin, LOW);
usleep(low_pulse);
}