From 1b20f3d49c8b9006ad44fc1eb49aa41076810693 Mon Sep 17 00:00:00 2001 From: Tomas Krejci Date: Mon, 27 May 2024 01:48:39 +0200 Subject: [PATCH] up ir tx to valid LT packet --- lib/olt_lib/ir_tx/__init__.py | 36 ++++++++-------------- lib/olt_lib/ir_tx/olt.py | 57 ++++++++++++++++------------------- main.py | 10 +++--- 3 files changed, 44 insertions(+), 59 deletions(-) diff --git a/lib/olt_lib/ir_tx/__init__.py b/lib/olt_lib/ir_tx/__init__.py index 3e28ec6..f1dc0a3 100644 --- a/lib/olt_lib/ir_tx/__init__.py +++ b/lib/olt_lib/ir_tx/__init__.py @@ -15,11 +15,12 @@ else: from micropython import const from array import array from time import ticks_us, ticks_diff, sleep_ms +import uasyncio as asyncio # import micropython # micropython.alloc_emergency_exception_buf(100) -# Shared by NEC + STOP = const(0) # End of data # IR abstract base class. Array holds periods in μs between toggling 36/38KHz @@ -84,24 +85,26 @@ class IR: # Public interface # Before populating array, zero pointer, set notional carrier state (off). - def transmit(self, addr, data, toggle=0, validate=False): # NEC: toggle is unused + def transmit(self, tx1, tx2, tx3, validate=False): # NEC: toggle is unused while self.busy(): pass t = ticks_us() if validate: - if addr > self.valid[0] or addr < 0: - raise ValueError('Address out of range', addr) - if data > self.valid[1] or data < 0: - raise ValueError('Data out of range', data) - if toggle > self.valid[2] or toggle < 0: - raise ValueError('Toggle out of range', toggle) + if tx1 > self.valid[0] or tx1 < 0: + raise ValueError('Address out of range', tx1) + if tx2 > self.valid[1] or tx2 < 0: + raise ValueError('Data out of range', tx2) + if tx3 > self.valid[2] or tx3 < 0: + raise ValueError('Toggle out of range', tx3) self.aptr = 0 # Inital conditions for tx: index into array self.carrier = False - self.tx(addr, data, toggle) # Subclass populates ._arr + self.tx(tx1, tx2, tx3) # Subclass populates ._arr self.trigger() # Initiate transmission if self.timeit: dt = ticks_diff(ticks_us(), t) print('Time = {}μs'.format(dt)) + + #asyncio.sleep_ms(2) sleep_ms(1) # Ensure ._busy is set prior to return # Subclass interface @@ -127,17 +130,4 @@ class IR: assert t > 0 self.verbose and print('add', t) # .carrier unaffected - self._arr[self.aptr - 1] += t - - -# Given an iterable (e.g. list or tuple) of times, emit it as an IR stream. -class Player(IR): - - def __init__(self, pin, freq=56000, verbose=False, asize=68): # NEC specifies 38KHz - super().__init__(pin, freq, asize, 33, verbose) # Measured duty ratio 33% - - def play(self, lst): - for x, t in enumerate(lst): - self._arr[x] = t - self.aptr = x + 1 - self.trigger() + self._arr[self.aptr - 1] += t \ No newline at end of file diff --git a/lib/olt_lib/ir_tx/olt.py b/lib/olt_lib/ir_tx/olt.py index ca0c4c4..b243f89 100644 --- a/lib/olt_lib/ir_tx/olt.py +++ b/lib/olt_lib/ir_tx/olt.py @@ -5,46 +5,41 @@ from micropython import const from olt_lib.ir_tx import IR -class SONY_ABC(IR): +# Bit reverse a 32 bit value +def rbit32(v): + v = (v & 0x0000ffff) << 16 | (v & 0xffff0000) >> 16 + v = (v & 0x00ff00ff) << 8 | (v & 0xff00ff00) >> 8 + v = (v & 0x0f0f0f0f) << 4 | (v & 0xf0f0f0f0) >> 4 + v = (v & 0x33333333) << 2 | (v & 0xcccccccc) >> 2 + return (v & 0x55555555) << 1 | (v & 0xaaaaaaaa) >> 1 +class LT_ABC(IR): def __init__(self, pin, bits, freq, verbose): - super().__init__(pin, freq, 3 + bits * 2, 30, verbose) - if bits not in (12, 15, 20, 24): - raise ValueError('bits must be 12, 15, 20 or 24.') + super().__init__(pin, freq, 3 + bits * 2, 40, verbose) # 30 -> 40 + if bits != 24: + raise ValueError('OLT only support 24 bits.') + #if bits not in (20, 24): + # raise ValueError('bits must be 20 or 24.') self.bits = bits + - def tx(self, addr, data, ext): + + def tx(self, tx1, tx2, tx3): self.append(2400, 600) bits = self.bits - v = data & 0x7f - if bits == 12: - v |= (addr & 0x1f) << 7 - elif bits == 15: - v |= (addr & 0xff) << 7 - else: - v |= (addr & 0x1f) << 7 - v |= (ext & 0xff) << 12 + v = tx3 & 0xff + v |= (tx2 & 0xff) << 8 + v |= (tx1 & 0xff) << 16 + + v = rbit32(v) + v = v >> 8 + for _ in range(bits): self.append(1200 if v & 1 else 600, 600) v >>= 1 -# Sony LT specifies 56KHz -class SONY_12(SONY_ABC): - valid = (0x1f, 0x7f, 0) # Max addr, data, toggle - def __init__(self, pin, freq=56000, verbose=False): - super().__init__(pin, 12, freq, verbose) - -class SONY_15(SONY_ABC): - valid = (0xff, 0x7f, 0) # Max addr, data, toggle - def __init__(self, pin, freq=56000, verbose=False): - super().__init__(pin, 15, freq, verbose) - -class SONY_20(SONY_ABC): - valid = (0x1f, 0x7f, 0xff) # Max addr, data, toggle - def __init__(self, pin, freq=56000, verbose=False): - super().__init__(pin, 20, freq, verbose) - -class LT_24(SONY_ABC): - valid = (0x1f, 0x7f, 0xff) # Max addr, data, toggle +# OLT specifies 56KHz +class LT_24(LT_ABC): + valid = (0xff, 0xff, 0xff) # Max tx1, tx2, tx3 def __init__(self, pin, freq=56000, verbose=False): super().__init__(pin, 24, freq, verbose) diff --git a/main.py b/main.py index 755b5ff..d95ff64 100644 --- a/main.py +++ b/main.py @@ -94,9 +94,9 @@ async def start_game_comand(btn, rgb, ir_tx): await rgb.set_red() btn.press_func(None) btn.release_func(None) - addr = 0x80 - data = 0x81 - tog = 0x82 + tx1 = 0x83 + tx2 = 0x05 + tx3 = 0xE8 while True: btn.press.clear() @@ -104,9 +104,9 @@ async def start_game_comand(btn, rgb, ir_tx): await btn.press.wait() logger.debug("Start game button pressed") await rgb.set_green() - # add IR TX here + # IR TX send logger.debug("Start game IR TX transmit") - ir_tx.transmit(addr, data, tog, False) + ir_tx.transmit(tx1, tx2, tx3, False) await btn.release.wait() logger.debug("Start game button released") await rgb.set_red()