ir tx on button fire, ir rx decode, error LT packet on tx site

This commit is contained in:
Tomas Krejci 2024-05-26 23:37:31 +02:00
parent 1d50af9125
commit 1159bead74

78
main.py
View File

@ -20,17 +20,41 @@ import time
import gc
from machine import Pin, freq
from olt_lib.ir_rx.print_error import print_error # Optional print of error codes
from olt_lib.ir_rx.olt import LT_24 # Import all implemented classes
from olt_lib.ir_rx.olt import LT_24 as LT_24_RX # Import all implemented classes
# Define IR RX pin according to platform
if platform == "pyboard":
p = Pin("X3", Pin.IN)
ir_rx_pin = Pin("X3", Pin.IN)
elif platform == "esp8266":
freq(160000000)
p = Pin(13, Pin.IN)
ir_rx_pin = Pin(13, Pin.IN)
elif platform == "esp32" or platform == "esp32_LoBo":
p = Pin(23, Pin.IN)
ir_rx_pin = Pin(23, Pin.IN)
elif platform == "rp2":
p = Pin(16, Pin.IN)
ir_rx_pin = Pin(16, Pin.IN)
#### OLT IR TX imports ###
from sys import platform
ESP32 = platform == 'esp32'
RP2 = platform == 'rp2'
PYBOARD = platform == 'pyboard'
if ESP32 or RP2:
from machine import Pin
else:
from pyb import Pin, LED
import uasyncio as asyncio
from primitives.switch import Switch
from primitives.delay_ms import Delay_ms
# Import all implemented classes
from olt_lib.ir_tx.olt import LT_24 as LT_24_TX
# Define IR TX pin according to platform
if ESP32: # Pins for IR LED gate
ir_tx_pin = Pin(23, Pin.OUT, value = 0)
elif RP2:
ir_tx_pin = Pin(17, Pin.OUT, value = 0)
else:
ir_tx_pin = Pin('X1')
#### Other imports ###
from primitives.delay_ms import Delay_ms
@ -42,8 +66,10 @@ from rgb import RGB
import logging
global logger
init_ticks = time.ticks_ms()
# change logging level here
logging.basicConfig(level=logging.ERROR, format='%(name)s - %(levelname)s - %(message)s')
# change logging level here, logging.ERROR is default, logging.DEBUG for more info, and reset device
log_level = logging.DEBUG
#log_level = logging.ERROR
logging.basicConfig(level=log_level, format='%(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
@ -63,11 +89,15 @@ async def alive_check():
await asyncio.sleep_ms(900)
### OLT Start game command functions ###
async def start_game_comand(btn, rgb):
async def start_game_comand(btn, rgb, ir_tx):
logger.debug("Start game command")
await rgb.set_red()
btn.press_func(None)
btn.release_func(None)
addr = 0x80
data = 0x81
tog = 0x82
while True:
btn.press.clear()
btn.release.clear()
@ -75,6 +105,8 @@ async def start_game_comand(btn, rgb):
logger.debug("Start game button pressed")
await rgb.set_green()
# add IR TX here
logger.debug("Start game IR TX transmit")
ir_tx.transmit(addr, data, tog, False)
await btn.release.wait()
logger.debug("Start game button released")
await rgb.set_red()
@ -93,6 +125,24 @@ async def main(proto):
rgb1 = RGB()
### OLT IR RX ###
logger.debug("OLT IR RX init")
# User callback
def cb(byte1, byte2, byte3, packet):
print(f"byte1 0x{byte1:02x} byte2 0x{byte2:02x} byte3 0x{byte3:02x} packet 0x{packet:06x}")
ir_rx = LT_24_RX(ir_rx_pin, cb) # Instantiate receiver
ir_rx.error_function(print_error) # Show debug information
### OLT IR TX ###
logger.debug("OLT IR TX init")
ir_tx = LT_24_TX(ir_tx_pin, 56000) # My decoder chip is 56KHz
# Uncomment the following to print transmit timing
ir_tx.timeit = True
### Buttons ###
logger.debug("Button init")
pin3 = machine.Pin(18, machine.Pin.IN, machine.Pin.PULL_UP) # Button 3 on membrane switch
@ -101,17 +151,9 @@ async def main(proto):
btn3.release_func(None)
btn3.double_func(None)
btn3.long_func(None)
asyncio.create_task(start_game_comand(btn3, rgb1))
### OLT IR RX ###
logger.debug("OLT IR RX init")
# User callback
def cb(byte1, byte2, byte3, packet):
print(f"byte1 0x{byte1:02x} byte2 0x{byte2:02x} byte3 0x{byte3:02x} packet 0x{packet:06x}")
ir = LT_24(p, cb) # Instantiate receiver
ir.error_function(print_error) # Show debug information
asyncio.create_task(start_game_comand(btn3, rgb1, ir_tx))
### Main loop ###
while True:
await asyncio.sleep(1)