TonyGo

Untitled

Mar 27th, 2026
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.38 KB | None | 0 0
  1. # https://docs.sunfounder.com/projects/pico-2w-kit/en/latest/pyproject/py_irremote.html
  2. # Modified for CROSS remote 31 July 2025 by Tony Goodhew
  3. import time
  4. from machine import Pin
  5. from ir_rx.nec import NEC_8  # Adjust based on your remote's protocol
  6. from ir_rx.print_error import print_error
  7.  
  8. # Initialize the IR receiver pin
  9. ir_pin = Pin(17, Pin.IN)
  10.  
  11. # Callback function to handle received data
  12. def ir_callback(data, addr, ctrl):
  13.     if data < 0:  # Repeat code or error
  14.         pass
  15.     else:
  16. #        print(data)
  17.         key = decode_key(data)
  18.         print("Received Key:", key)
  19.  
  20. # Function to decode the received data into key presses
  21. def decode_key(data):
  22.     key_codes = {
  23.         69: "1",
  24.         70: "2",
  25.         71: "3",
  26.         68: "4",
  27.         64: "5",
  28.         67: "6",
  29.         7: "7",
  30.         21: "8",
  31.         9: "9",
  32.         22: "*",
  33.         25: "0",
  34.         13: "#",
  35.         24: "U",
  36.         8: "L",
  37.         28: "K",
  38.         90: "R",
  39.         82: "D",
  40.         0x0: "ERROR"
  41.         # Add more key codes based on your remote
  42.     }
  43.     return key_codes.get(data, "UNKNOWN")
  44.  
  45. # Instantiate the IR receiver
  46. ir = NEC_8(ir_pin, ir_callback)
  47. ir.error_function(print_error)  # Optional: to print errors
  48.  
  49. try:
  50.     while True:
  51.         time.sleep(1)  # Keep the main thread alive
  52. except KeyboardInterrupt:
  53.     ir.close()
  54.     print("Program terminated")
Advertisement
Add Comment
Please, Sign In to add comment