up ir tx async

This commit is contained in:
Tomas Krejci 2024-05-27 17:33:09 +02:00
parent 7ff3652e29
commit 85ca755700
3 changed files with 19 additions and 11 deletions

View File

@ -85,7 +85,7 @@ class IR:
# Public interface
# Before populating array, zero pointer, set notional carrier state (off).
def transmit(self, tx1, tx2, tx3, validate=False): # NEC: toggle is unused
async def transmit(self, tx1, tx2, tx3, validate=False): # NEC: toggle is unused
while self.busy():
pass
t = ticks_us()
@ -98,17 +98,18 @@ class IR:
raise ValueError('Toggle out of range', tx3)
self.aptr = 0 # Inital conditions for tx: index into array
self.carrier = False
self.tx(tx1, tx2, tx3) # Subclass populates ._arr
self.trigger() # Initiate transmission
await self.tx(tx1, tx2, tx3) # Subclass populates ._arr
asyncio.create_task(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
while self.busy():
await asyncio.sleep_ms(1)
#sleep_ms(1) # Ensure ._busy is set prior to return
# Subclass interface
def trigger(self): # Used by NEC to initiate a repeat frame
async def trigger(self): # Used by NEC to initiate a repeat frame
if ESP32:
self._rmt.write_pulses(tuple(self._mva[0 : self.aptr]))
elif RP2:

View File

@ -24,7 +24,7 @@ class LT_ABC(IR):
def tx(self, tx1, tx2, tx3):
async def tx(self, tx1, tx2, tx3):
bits = self.bits
v = tx3 & 0xff
v |= (tx2 & 0xff) << 8

15
main.py
View File

@ -71,7 +71,7 @@ log_level = logging.DEBUG
#log_level = logging.ERROR
logging.basicConfig(level=log_level, format='%(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
#PYTHONASYNCIODEBUG=1
### alive check ###
async def alive_check():
@ -90,7 +90,7 @@ async def alive_check():
### OLT Start game command functions ###
async def start_game_comand(btn, rgb, ir_tx):
logger.debug("Start game command")
logger.debug("Start game command on button press")
await rgb.set_red()
btn.press_func(None)
btn.release_func(None)
@ -106,7 +106,10 @@ async def start_game_comand(btn, rgb, ir_tx):
await rgb.set_green()
# IR TX send
logger.debug("Start game IR TX transmit")
ir_tx.transmit(tx1, tx2, tx3, False)
asyncio.create_task(ir_tx.transmit(tx1, tx2, tx3, False))
#loop = asyncio.get_running_loop()
#loop.set_exception_handler(exception_handler)
await asyncio.sleep_ms(50)
await btn.release.wait()
logger.debug("Start game button released")
await rgb.set_red()
@ -157,11 +160,15 @@ async def main(proto):
while True:
await asyncio.sleep(1)
def exception_handler(loop, context):
# log exception
print(context['exception'])
### asyncio start main ###
try:
asyncio.run(main(0))
except KeyboardInterrupt:
logger.exception("App Interrupted")