-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset_ac.py
269 lines (218 loc) · 6.49 KB
/
set_ac.py
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
from enum import Enum
import pigpio
import time
import argparse
from config import Config
GPIO = 18
FREQ_KHZ = 38.0
GAP_S = 100 / 1000
PULSE_SHORT = 482
PULSE_LONG = 3486
GAP_SHORT = 388
GAP_LONG = 1257
GAP_LEADING = 1698
if not Config.DRY_RUN:
pi = pigpio.pi()
def get_checksum(cmd):
checksum = 0
for c in cmd:
checksum = checksum + c
return checksum % 256
class AcModes(Enum):
auto = 0
dry = 2
cool = 3
heat = 4
fan = 6
class FanModes(Enum):
undefined = 0
l1 = 0x3
l2 = 0x4
l3 = 0x5
l4 = 0x6
l5 = 0x7
auto = 0xA
silent = 0xB
def get_ac_command(is_ac_on, temperature, mode, fan_mode, is_swing_on, is_economy, is_powerful, on_timer, off_timer):
print("Creating command with the following settings")
print("AC on: %d" % is_ac_on)
print("Temperature: %d" % temperature)
print("Mode: %s" % mode)
print("Fan: %s" % fan_mode)
print("Swing: %d" % is_swing_on)
print("Economy mode: %d" % is_economy)
print("Power mode: %d" % is_powerful)
print("On timer: %d" % on_timer)
print("Off timer: %d" % off_timer)
preamble = [0x11, 0xDA, 0x27, 0x00]
ret = [0] * 14
# byte 1 = 0
ret[1] = ret[1] + 0x08
# AC on
ret[1] = ret[1] + (0x01 * is_ac_on)
# Mode
ret[1] = ret[1] + ((mode.value << 4) & 0xf0)
# Temperature
if mode == AcModes.dry:
ret[2] = 0xC0
else:
ret[2] = temperature * 2
# byte 3 = 0
# Swing
ret[4] = 0x0f * is_swing_on
ret[4] = ret[4] + (fan_mode.value << 4)
# byte 5 = 0
# byte 6 = 0 # TODO: tOn/tOff
if on_timer:
if on_timer > 12:
on_timer = 12
on_timer = on_timer * 15
ret[6] = (on_timer & 0x3f) << 2
ret[7] = (on_timer & 0xa0) >> 6
ret[1] = ret[1] + 0x02
else:
ret[7] = 0x06
if off_timer:
if off_timer > 9:
off_timer = 9
off_timer = off_timer * 15
ret[7] = ret[7] | (off_timer & 0x03) << 6
ret[8] = (off_timer & 0xfc) >> 2
ret[1] = ret[1] + 0x04
else:
ret[8] = 0x60
ret[9] = 0x01 * is_powerful
ret[11] = 0xC1
ret[12] = 0x80 + 0x4 * is_economy
ret = preamble + ret
checksum = get_checksum(ret)
return ret + [checksum]
def get_raw_values(cmd):
raw_values = list()
raw_values.append(PULSE_LONG)
raw_values.append(GAP_LEADING)
for c in cmd:
for i in range(8):
if c & (1 << i):
raw_values.append(PULSE_SHORT)
raw_values.append(GAP_LONG)
else:
raw_values.append(PULSE_SHORT)
raw_values.append(GAP_SHORT)
raw_values.append(PULSE_SHORT)
return raw_values
def carrier(gpio, frequency, micros):
"""
Generate carrier square wave.
"""
wf = []
cycle = 1000.0 / frequency
cycles = int(round(micros/cycle))
on = int(round(cycle / 2.0))
sofar = 0
for c in range(cycles):
target = int(round((c+1)*cycle))
sofar += on
off = target - sofar
sofar += off
wf.append(pigpio.pulse(1<<gpio, 0, on))
wf.append(pigpio.pulse(0, 1<<gpio, off))
return wf
def ir_transmit(code):
raw_code = get_raw_values(code)
print("Sending code")
for code_byte in code:
print("%02X" % code_byte, end=' ')
print()
if Config.DRY_RUN:
print("DRY RUN - not sending code")
return
pi.set_mode(GPIO, pigpio.OUTPUT) # IR TX connected to this GPIO.
pi.wave_add_new()
emit_time = time.time()
# Create wave
marks_wid = {}
spaces_wid = {}
wave = [0] * len(raw_code)
for i in range(0, len(raw_code)):
ci = raw_code[i]
if i & 1: # Space
if ci not in spaces_wid:
pi.wave_add_generic([pigpio.pulse(0, 0, ci)])
spaces_wid[ci] = pi.wave_create()
wave[i] = spaces_wid[ci]
else: # Mark
if ci not in marks_wid:
wf = carrier(GPIO, FREQ_KHZ, ci)
pi.wave_add_generic(wf)
marks_wid[ci] = pi.wave_create()
wave[i] = marks_wid[ci]
delay = emit_time - time.time()
if delay > 0.0:
time.sleep(delay)
pi.wave_chain(wave)
while pi.wave_tx_busy():
time.sleep(0.002)
emit_time = time.time() + GAP_S
for i in marks_wid:
pi.wave_delete(marks_wid[i])
marks_wid = {}
for i in spaces_wid:
pi.wave_delete(spaces_wid[i])
spaces_wid = {}
def set_ac(on, temp, mode, fan, swing, economy, power, comfort, on_timer, off_timer):
on = int(on)
temp = int(temp)
mode = AcModes[mode.lower()]
fan = FanModes[fan.lower()]
swing = int(swing)
economy = int(economy)
power = int(power)
comfort = int(comfort)
if mode == AcModes.dry:
fan = FanModes.auto
cmd1 = [0x11, 0xDA, 0x27, 0x00, 0xC5, 0x00, 0x00]
if comfort:
cmd1[6] = 0x10
fan = FanModes.auto
swing = 0
cmd1 = cmd1 + [get_checksum(cmd1)]
cmd2 = [0x11, 0xDA, 0x27, 0x00, 0x42, 0x00, 0x00]
cmd2 = cmd2 + [get_checksum(cmd2)]
cmd3 = get_ac_command(on, temp, mode, fan, swing, economy, power, on_timer, off_timer)
ir_transmit(cmd1)
ir_transmit(cmd2)
ir_transmit(cmd3)
if __name__=='__main__':
p = argparse.ArgumentParser()
p.add_argument("on", help="AC on [0,1]", type=int)
p.add_argument("mode", help="mode [auto, cool, dry, heat, fan]", type=str)
p.add_argument("temperature", help="temperature[18-30]", type=int)
p.add_argument("swing", help="swing [0,1]", type=int)
p.add_argument("fan", help="fan mode [l1-l5,auto,silent]", type=str)
p.add_argument("economy", help="economy mode [0,1]", type=int)
p.add_argument("power", help="power mode [0,1]", type=int)
p.add_argument("comfort", help="comfort mode [0,1]", type=int)
p.add_argument("on_timer", help="on timer [0-12]", type=int)
p.add_argument("off_timer", help="off timer [0-9]", type=int)
args = p.parse_args()
cmd1 = [0x11, 0xDA, 0x27, 0x00, 0xC5, 0x00, 0x00]
if args.comfort:
cmd1[6] = 0x10
cmd1 = cmd1 + [get_checksum(cmd1)]
cmd2 = [0x11, 0xDA, 0x27, 0x00, 0x42, 0x00, 0x00]
cmd2 = cmd2 + [get_checksum(cmd2)]
cmd3 = get_ac_command(
args.on,
args.temperature,
AcModes[args.mode.lower()],
FanModes[args.fan.lower()],
args.swing,
args.economy,
args.power,
args.on_timer,
args.off_timer
)
ir_transmit(cmd1)
ir_transmit(cmd2)
ir_transmit(cmd3)