-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwpispiplayback.cc
More file actions
228 lines (181 loc) · 4.43 KB
/
wpispiplayback.cc
File metadata and controls
228 lines (181 loc) · 4.43 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
Sends custom codes out over the SPI at a controlled rate to LG A/C device via an infrared diode.
g++ wpispiplayback.cc -lwiringPi -o LGcontrol
parameters: <command>
command = AC_OFF, AC_ONHEAT76, AC_ONAC74
CPU performance scaling is handled by the code itself.
Requires wiringPi library to be installed. See wiringpi.com or type:
wget https://project-downloads.drogon.net/wiringpi-latest.deb
sudo dpkg -i wiringpi-latest.deb
*/
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <string>
#include <wiringPi.h>
#include <wiringPiSPI.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <cstring>
using namespace std;
#define DELAYOFFSET 0
#define SPICHANNEL 1
// SPISPEED = 38000 * 2
#define SPISPEED 76000
#define REPEAT_COUNT 1
unsigned int bytePtr = 0;
unsigned int bitPtr = 0;
#define DATABUFSIZE 4096
#define usecPerBit (26.31578947/2)
unsigned char * databuf;
bool exit_condition = false;
void ctrlc_handler(int s){
exit_condition = true;
}
void clearSig() {
memset(databuf, 0x00, DATABUFSIZE);
bytePtr = 0;
bitPtr = 0;
}
void filldata(int usec, unsigned char fill){
if (bytePtr>=DATABUFSIZE) return;
int cnt = usec / usecPerBit / 2; // total number of bit cycles represented by usec
// Fill in any previous hole
if (bitPtr > 0) {
while ((bitPtr < 8) && (cnt > 0)){
databuf[bytePtr] |= (fill & 0x3) << (8-bitPtr);
bitPtr += 2;
cnt -= 1;
}
if (bitPtr==8){
bytePtr++;
bitPtr = 0;
}
}
int cnt8 = cnt / 4;
int cntbit = cnt % 4;
for (int i=0; i < cnt8; i++) {
databuf[bytePtr++] = fill; // 10101010
if (bytePtr>=DATABUFSIZE) return;
}
for (int i=0; i < cntbit; i++) {
databuf[bytePtr] |= (fill & 0x03) << (8-bitPtr);
bitPtr += 2;
}
}
void pulseIR(int usec){
filldata(usec, 0xAA);
}
void spacer(int usec){
filldata(usec, 0x00);
}
void LGCode(int num) {
const int ontime = 450;
const int offtime = 750;
const int longofftime = 1500;
for (int i=0; i < (num-1); i++){
pulseIR(ontime);
spacer(offtime);
}
pulseIR(ontime); spacer(longofftime);
}
void LGHeader() {
clearSig();
pulseIR(9300); spacer(4500);
LGCode(1);
LGCode(4);
}
void AC_OFF() {
LGHeader();
LGCode(4);
LGCode(1);
LGCode(12);
LGCode(2);
LGCode(4);
LGCode(1);
}
void AC_ONHEAT76() {
LGHeader();
LGCode(9);
LGCode(3);
LGCode(3);
LGCode(3);
LGCode(3);
LGCode(2);
LGCode(1);
LGCode(1);
LGCode(1);
LGCode(1);
}
void AC_ONAC74() {
LGHeader();
LGCode(12);
LGCode(5);
LGCode(3);
LGCode(1);
LGCode(3);
}
void setCPUMode(string mode) {
int fd = open("/sys/devices/system/cpu/cpufreq/policy0/scaling_governor", O_WRONLY);
if (fd == -1) {
perror("Unable to open /sys/devices/system/cpu/cpufreq/policy0/scaling_governor");
exit(1);
}
if (write(fd, mode.c_str(), mode.length()) != mode.length()) {
perror("Error writing to /sys/devices/system/cpu/cpufreq/policy0/scaling_governor");
exit(1);
}
close(fd);
}
int main(int argc, char **argv)
{
void (*remoteFunction)() = NULL;
if (argc < 2) {
printf("No remote control signal function specified.\n");
return 1;
}
string command = argv[1];
if (command=="AC_ONHEAT76") {
printf("Turning on AC - Heat 76 Degrees\n");
remoteFunction = AC_ONHEAT76;
}
else if (command=="AC_OFF") {
printf("Turning off AC\n");
remoteFunction = AC_OFF;
}
else if (command=="AC_ONAC74") {
printf("Turning on AC - AC 74 Degrees\n");
remoteFunction = AC_ONAC74;
}
else {
printf("Invalid remote control signal function specified\n");
return 1;
}
setCPUMode("performance");
struct sigaction sigIntHandler;
sigIntHandler.sa_handler = ctrlc_handler;
sigemptyset(&sigIntHandler.sa_mask);
sigIntHandler.sa_flags = 0;
sigaction(SIGINT, &sigIntHandler, NULL);
databuf = (unsigned char *)malloc(DATABUFSIZE);
piHiPri(0);
wiringPiSetup();
wiringPiSPISetup (SPICHANNEL, SPISPEED);
for (int n=0; n < REPEAT_COUNT; n++) {
remoteFunction();
wiringPiSPIDataRW (SPICHANNEL, databuf, DATABUFSIZE) ;
sleep(1);
if (exit_condition) break;
}
printf("Freeing memory...\n");
free(databuf);
setCPUMode("ondemand");
printf("Goodbye\n");
return 0;
}