mirror of
https://github.com/mjg59/python-broadlink.git
synced 2024-11-21 22:51:41 +01:00
Expose IR/RF conversion functions (#788)
* Move IR duration<->Broadlink conversion down from CLI * Fix --learn base64 to not crash with --durations Also remove its b'...' wrapping. * Fix IR/RF conversions --------- Co-authored-by: William Grant <me@williamgrant.id.au>
This commit is contained in:
parent
84af992dcc
commit
247be74c33
@ -6,6 +6,47 @@ from . import exceptions as e
|
|||||||
from .device import Device
|
from .device import Device
|
||||||
|
|
||||||
|
|
||||||
|
def pulses_to_data(pulses: t.List[int], tick: int = 32.84) -> None:
|
||||||
|
"""Convert a microsecond duration sequence into a Broadlink IR packet."""
|
||||||
|
result = bytearray(4)
|
||||||
|
result[0x00] = 0x26
|
||||||
|
|
||||||
|
for pulse in pulses:
|
||||||
|
div, mod = divmod(int(pulse // tick), 256)
|
||||||
|
if div:
|
||||||
|
result.append(0)
|
||||||
|
result.append(div)
|
||||||
|
result.append(mod)
|
||||||
|
|
||||||
|
data_len = len(result) - 4
|
||||||
|
result[0x02] = data_len & 0xFF
|
||||||
|
result[0x03] = data_len >> 8
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
def data_to_pulses(data: bytes, tick: int = 32.84) -> t.List[int]:
|
||||||
|
"""Parse a Broadlink packet into a microsecond duration sequence."""
|
||||||
|
result = []
|
||||||
|
index = 4
|
||||||
|
end = min(256 * data[0x03] + data[0x02] + 4, len(data))
|
||||||
|
|
||||||
|
while index < end:
|
||||||
|
chunk = data[index]
|
||||||
|
index += 1
|
||||||
|
|
||||||
|
if chunk == 0:
|
||||||
|
try:
|
||||||
|
chunk = 256 * data[index] + data[index + 1]
|
||||||
|
except IndexError:
|
||||||
|
raise ValueError("Malformed data.")
|
||||||
|
index += 2
|
||||||
|
|
||||||
|
result.append(int(chunk * tick))
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
class rmmini(Device):
|
class rmmini(Device):
|
||||||
"""Controls a Broadlink RM mini 3."""
|
"""Controls a Broadlink RM mini 3."""
|
||||||
|
|
||||||
|
@ -1,68 +1,32 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
import argparse
|
import argparse
|
||||||
import base64
|
import base64
|
||||||
import codecs
|
|
||||||
import time
|
import time
|
||||||
|
import typing as t
|
||||||
|
|
||||||
import broadlink
|
import broadlink
|
||||||
from broadlink.const import DEFAULT_PORT
|
from broadlink.const import DEFAULT_PORT
|
||||||
from broadlink.exceptions import ReadError, StorageError
|
from broadlink.exceptions import ReadError, StorageError
|
||||||
|
from broadlink.remote import data_to_pulses, pulses_to_data
|
||||||
|
|
||||||
TICK = 32.84
|
|
||||||
TIMEOUT = 30
|
TIMEOUT = 30
|
||||||
IR_TOKEN = 0x26
|
|
||||||
|
|
||||||
|
|
||||||
def auto_int(x):
|
def auto_int(x):
|
||||||
return int(x, 0)
|
return int(x, 0)
|
||||||
|
|
||||||
|
|
||||||
def to_microseconds(bytes):
|
def format_pulses(pulses: t.List[int]) -> str:
|
||||||
result = []
|
"""Format pulses."""
|
||||||
# print bytes[0] # 0x26 = 38for IR
|
return " ".join(
|
||||||
index = 4
|
f"+{pulse}" if i % 2 == 0 else f"-{pulse}"
|
||||||
while index < len(bytes):
|
for i, pulse in enumerate(pulses)
|
||||||
chunk = bytes[index]
|
)
|
||||||
index += 1
|
|
||||||
if chunk == 0:
|
|
||||||
chunk = bytes[index]
|
|
||||||
chunk = 256 * chunk + bytes[index + 1]
|
|
||||||
index += 2
|
|
||||||
result.append(int(round(chunk * TICK)))
|
|
||||||
if chunk == 0x0d05:
|
|
||||||
break
|
|
||||||
return result
|
|
||||||
|
|
||||||
|
|
||||||
def durations_to_broadlink(durations):
|
def parse_pulses(data: t.List[str]) -> t.List[int]:
|
||||||
result = bytearray()
|
"""Parse pulses."""
|
||||||
result.append(IR_TOKEN)
|
return [abs(int(s)) for s in data]
|
||||||
result.append(0)
|
|
||||||
result.append(len(durations) % 256)
|
|
||||||
result.append(len(durations) / 256)
|
|
||||||
for dur in durations:
|
|
||||||
num = int(round(dur / TICK))
|
|
||||||
if num > 255:
|
|
||||||
result.append(0)
|
|
||||||
result.append(num / 256)
|
|
||||||
result.append(num % 256)
|
|
||||||
return result
|
|
||||||
|
|
||||||
|
|
||||||
def format_durations(data):
|
|
||||||
result = ''
|
|
||||||
for i in range(0, len(data)):
|
|
||||||
if len(result) > 0:
|
|
||||||
result += ' '
|
|
||||||
result += ('+' if i % 2 == 0 else '-') + str(data[i])
|
|
||||||
return result
|
|
||||||
|
|
||||||
|
|
||||||
def parse_durations(str):
|
|
||||||
result = []
|
|
||||||
for s in str.split():
|
|
||||||
result.append(abs(int(s)))
|
|
||||||
return result
|
|
||||||
|
|
||||||
|
|
||||||
parser = argparse.ArgumentParser(fromfile_prefix_chars='@')
|
parser = argparse.ArgumentParser(fromfile_prefix_chars='@')
|
||||||
@ -112,8 +76,8 @@ if args.joinwifi:
|
|||||||
|
|
||||||
if args.convert:
|
if args.convert:
|
||||||
data = bytearray.fromhex(''.join(args.data))
|
data = bytearray.fromhex(''.join(args.data))
|
||||||
durations = to_microseconds(data)
|
pulses = data_to_pulses(data)
|
||||||
print(format_durations(durations))
|
print(format_pulses(pulses))
|
||||||
if args.temperature:
|
if args.temperature:
|
||||||
print(dev.check_temperature())
|
print(dev.check_temperature())
|
||||||
if args.humidity:
|
if args.humidity:
|
||||||
@ -125,8 +89,11 @@ if args.sensors:
|
|||||||
for key in data:
|
for key in data:
|
||||||
print("{} {}".format(key, data[key]))
|
print("{} {}".format(key, data[key]))
|
||||||
if args.send:
|
if args.send:
|
||||||
data = durations_to_broadlink(parse_durations(' '.join(args.data))) \
|
data = (
|
||||||
if args.durations else bytearray.fromhex(''.join(args.data))
|
pulses_to_data(parse_pulses(args.data))
|
||||||
|
if args.durations
|
||||||
|
else bytes.fromhex(''.join(args.data))
|
||||||
|
)
|
||||||
dev.send_data(data)
|
dev.send_data(data)
|
||||||
if args.learn or (args.learnfile and not args.rflearn):
|
if args.learn or (args.learnfile and not args.rflearn):
|
||||||
dev.enter_learning()
|
dev.enter_learning()
|
||||||
@ -144,17 +111,19 @@ if args.learn or (args.learnfile and not args.rflearn):
|
|||||||
print("No data received...")
|
print("No data received...")
|
||||||
exit(1)
|
exit(1)
|
||||||
|
|
||||||
learned = format_durations(to_microseconds(bytearray(data))) \
|
print("Packet found!")
|
||||||
if args.durations \
|
raw_fmt = data.hex()
|
||||||
else ''.join(format(x, '02x') for x in bytearray(data))
|
base64_fmt = base64.b64encode(data).decode('ascii')
|
||||||
if args.learn:
|
pulse_fmt = format_pulses(data_to_pulses(data))
|
||||||
print(learned)
|
|
||||||
decode_hex = codecs.getdecoder("hex_codec")
|
print("Raw:", raw_fmt)
|
||||||
print("Base64: " + str(base64.b64encode(decode_hex(learned)[0])))
|
print("Base64:", base64_fmt)
|
||||||
|
print("Pulses:", pulse_fmt)
|
||||||
|
|
||||||
if args.learnfile:
|
if args.learnfile:
|
||||||
print("Saving to {}".format(args.learnfile))
|
print("Saving to {}".format(args.learnfile))
|
||||||
with open(args.learnfile, "w") as text_file:
|
with open(args.learnfile, "w") as text_file:
|
||||||
text_file.write(learned)
|
text_file.write(pulse_fmt if args.durations else raw_fmt)
|
||||||
if args.check:
|
if args.check:
|
||||||
if dev.check_power():
|
if dev.check_power():
|
||||||
print('* ON *')
|
print('* ON *')
|
||||||
@ -238,14 +207,15 @@ if args.rflearn:
|
|||||||
exit(1)
|
exit(1)
|
||||||
|
|
||||||
print("Packet found!")
|
print("Packet found!")
|
||||||
learned = format_durations(to_microseconds(bytearray(data))) \
|
raw_fmt = data.hex()
|
||||||
if args.durations \
|
base64_fmt = base64.b64encode(data).decode('ascii')
|
||||||
else ''.join(format(x, '02x') for x in bytearray(data))
|
pulse_fmt = format_pulses(data_to_pulses(data))
|
||||||
if args.learnfile is None:
|
|
||||||
print(learned)
|
print("Raw:", raw_fmt)
|
||||||
decode_hex = codecs.getdecoder("hex_codec")
|
print("Base64:", base64_fmt)
|
||||||
print("Base64: {}".format(str(base64.b64encode(decode_hex(learned)[0]))))
|
print("Pulses:", pulse_fmt)
|
||||||
if args.learnfile is not None:
|
|
||||||
|
if args.learnfile:
|
||||||
print("Saving to {}".format(args.learnfile))
|
print("Saving to {}".format(args.learnfile))
|
||||||
with open(args.learnfile, "w") as text_file:
|
with open(args.learnfile, "w") as text_file:
|
||||||
text_file.write(learned)
|
text_file.write(pulse_fmt if args.durations else raw_fmt)
|
||||||
|
Loading…
Reference in New Issue
Block a user