create main.py for OLT device

This commit is contained in:
Tomas Krejci 2024-05-22 12:24:03 +02:00
parent a726377d78
commit 62e5396cd1

87
main.py
View File

@ -1,8 +1,8 @@
# ir_tx.test Test for nonblocking OpenLaserTag/SONY IR transmitter.
# Sony SIRC protocol.
# OpenLaserTag device
# OLT protocol.
# Tomas Krejci [Njord]
# Implements a 2-button remote control on a Pyboard with auto repeat.
from sys import platform
ESP32 = platform == 'esp32'
RP2 = platform == 'rp2'
@ -17,74 +17,21 @@ from primitives.delay_ms import Delay_ms
# Import all implemented classes
from olt_lib.ir_tx.olt import LT_24, SONY_12, SONY_15, SONY_20
loop = asyncio.get_event_loop()
# If button is held down normal behaviour is to retransmit
# but most NEC models send a REPEAT code
class Rbutton:
toggle = 1 # toggle is ignored in NEC mode
def __init__(self, irb, pin, addr, data, proto):
self.irb = irb
self.sw = Switch(pin)
self.addr = addr
self.data = data
self.proto = proto
self.sw.close_func(self.cfunc)
self.sw.open_func(self.ofunc)
self.tim = Delay_ms(self.repeat)
def cfunc(self): # Button push: send data
tog = 0 # NEC, sony 12, 15: toggle==0
self.irb.transmit(self.addr, self.data, tog, True) # Test validation
# Auto repeat. The Sony protocol specifies 45ms but this is tight.
# In 20 bit mode a data burst can be upto 39ms long.
self.tim.trigger(108)
def ofunc(self): # Button release: cancel repeat timer
self.tim.stop()
Rbutton.toggle ^= 1 # Toggle control
async def repeat(self):
await asyncio.sleep(0) # Let timer stop before retriggering
if not self.sw(): # Button is still pressed: retrigger
self.tim.trigger(108)
tog = 0 # NEC, sony 12, 15: toggle==0
self.irb.transmit(self.addr, self.data, tog, True) # Test validation
async def main(proto):
# Test uses a 56KHz carrier.
if ESP32: # Pins for IR LED gate
pin = Pin(23, Pin.OUT, value = 0)
elif RP2:
pin = Pin(17, Pin.OUT, value = 0)
else:
pin = Pin('X1')
classes = (LT_24, SONY_12, SONY_15, SONY_20)
irb = classes[proto](pin, 56000) # My decoder chip is 56KHz
# Uncomment the following to print transmit timing
irb.timeit = True
b = [] # Rbutton instances
px3 = Pin('X3', Pin.IN, Pin.PULL_UP) if PYBOARD else Pin(18, Pin.IN, Pin.PULL_UP)
px4 = Pin('X4', Pin.IN, Pin.PULL_UP) if PYBOARD else Pin(19, Pin.IN, Pin.PULL_UP)
b.append(Rbutton(irb, px3, 0x1, 0x7, proto))
b.append(Rbutton(irb, px4, 0x10, 0xb, proto))
if ESP32:
while True:
print('Running')
await asyncio.sleep(5)
elif RP2:
led = Pin(25, Pin.OUT)
while True:
await asyncio.sleep_ms(500) # Obligatory flashing LED.
led(not led())
else:
led = LED(1)
while True:
await asyncio.sleep_ms(500) # Obligatory flashing LED.
led.toggle()
while True:
print("main while running")
await asyncio.sleep(3)
asyncio.run(main(0))
try:
asyncio.run(main(0))
except KeyboardInterrupt:
print("Interrupted")
finally:
asyncio.new_event_loop()
print('End')