From 3d2da423196bc76b353e676aad36a57fafdfa694 Mon Sep 17 00:00:00 2001 From: Liz Date: Tue, 16 Apr 2024 12:09:38 -0400 Subject: [PATCH] example code for ir remote receiver Adding CP example code, product demo code and Arduino code for the IR remote receiver --- .../Arduino_STEMMA_IR_Remote_Receiver.ino | 46 +++++++++++++ .../CircuitPython/code.py | 28 ++++++++ .../CircuitPython_ProductDemo/code.py | 67 +++++++++++++++++++ 3 files changed, 141 insertions(+) create mode 100644 STEMMA_IR_Remote_Receiver_Examples/Arduino_STEMMA_IR_Remote_Receiver/Arduino_STEMMA_IR_Remote_Receiver.ino create mode 100644 STEMMA_IR_Remote_Receiver_Examples/CircuitPython/code.py create mode 100644 STEMMA_IR_Remote_Receiver_Examples/CircuitPython_ProductDemo/code.py diff --git a/STEMMA_IR_Remote_Receiver_Examples/Arduino_STEMMA_IR_Remote_Receiver/Arduino_STEMMA_IR_Remote_Receiver.ino b/STEMMA_IR_Remote_Receiver_Examples/Arduino_STEMMA_IR_Remote_Receiver/Arduino_STEMMA_IR_Remote_Receiver.ino new file mode 100644 index 000000000..855904fa9 --- /dev/null +++ b/STEMMA_IR_Remote_Receiver_Examples/Arduino_STEMMA_IR_Remote_Receiver/Arduino_STEMMA_IR_Remote_Receiver.ino @@ -0,0 +1,46 @@ +// SPDX-FileCopyrightText: 2024 Liz Clark for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +/* + * Based on the SimpleReceiver.cpp from the + * Arduino-IRremote https://github.com/Arduino-IRremote/Arduino-IRremote. + * by Armin Joachimsmeyer + ************************************************************************************ + * MIT License + * + * Copyright (c) 2020-2023 Armin Joachimsmeyer + * + */ + +#include + +#include // include the library +#define IR_RECEIVE_PIN 5 + +void setup() { + Serial.begin(115200); + IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK); +} + +void loop() { + /* + * Check if received data is available and if yes, try to decode it. + * Decoded result is in the IrReceiver.decodedIRData structure. + * + * E.g. command is in IrReceiver.decodedIRData.command + * address is in command is in IrReceiver.decodedIRData.address + * and up to 32 bit raw data in IrReceiver.decodedIRData.decodedRawData + */ + if (IrReceiver.decode()) { + if (IrReceiver.decodedIRData.protocol == UNKNOWN) { + IrReceiver.printIRResultRawFormatted(&Serial, true); + IrReceiver.resume(); + } else { + IrReceiver.resume(); + IrReceiver.printIRResultShort(&Serial); + IrReceiver.printIRSendUsage(&Serial); + } + Serial.println(); + } +} diff --git a/STEMMA_IR_Remote_Receiver_Examples/CircuitPython/code.py b/STEMMA_IR_Remote_Receiver_Examples/CircuitPython/code.py new file mode 100644 index 000000000..2c16476d0 --- /dev/null +++ b/STEMMA_IR_Remote_Receiver_Examples/CircuitPython/code.py @@ -0,0 +1,28 @@ +# SPDX-FileCopyrightText: Copyright (c) 2024 ladyada for Adafruit Industries +# +# SPDX-License-Identifier: MIT + +import board +import pulseio +import adafruit_irremote + +# IR receiver setup +ir_receiver = pulseio.PulseIn(board.D5, maxlen=120, idle_state=True) +decoder = adafruit_irremote.GenericDecode() + +def decode_ir_signals(p): + codes = decoder.decode_bits(p) + return codes + +while True: + pulses = decoder.read_pulses(ir_receiver) + try: + # Attempt to decode the received pulses + received_code = decode_ir_signals(pulses) + if received_code: + hex_code = ''.join(["%02X" % x for x in received_code]) + print(f"Received: {hex_code}") + except adafruit_irremote.IRNECRepeatException: # Signal was repeated, ignore + pass + except adafruit_irremote.IRDecodeException: # Failed to decode signal + print("Error decoding") diff --git a/STEMMA_IR_Remote_Receiver_Examples/CircuitPython_ProductDemo/code.py b/STEMMA_IR_Remote_Receiver_Examples/CircuitPython_ProductDemo/code.py new file mode 100644 index 000000000..b0a55dd36 --- /dev/null +++ b/STEMMA_IR_Remote_Receiver_Examples/CircuitPython_ProductDemo/code.py @@ -0,0 +1,67 @@ +# SPDX-FileCopyrightText: Copyright (c) 2024 ladyada for Adafruit Industries +# +# SPDX-License-Identifier: MIT +# +# Product demo for the IR Remote Receiver with an ESP32-S2 TFT + +import board +import terminalio +import displayio +import digitalio +import pulseio +import adafruit_irremote +try: + from fourwire import FourWire +except ImportError: + from displayio import FourWire +from adafruit_display_text import label +from adafruit_st7789 import ST7789 + +displayio.release_displays() + +# TFT display setup +spi = board.SPI() +tft_cs = board.TFT_CS # Adjust to your CS pin +tft_dc = board.TFT_DC # Adjust to your DC pin +display_bus = FourWire(spi, command=tft_dc, chip_select=tft_cs) +display = ST7789( + display_bus, rotation=270, width=240, height=135, rowstart=40, colstart=53 +) + +# Enable the display backlight +backlight = board.TFT_BACKLIGHT # Adjust to your backlight pin +backlight = digitalio.DigitalInOut(backlight) +backlight.switch_to_output() +backlight.value = True + +# Display initial text +text_area = label.Label(terminalio.FONT, text="Waiting\nfor IR...", color=0xFFFFFF) +text_area.anchor_point = (0.5, 0.5) +text_area.anchored_position = (240 / 2, 135 / 2) # Assuming display size is 240x135 +text_area.scale = 3 # Scale the text size up by 3 times + +display.root_group = text_area + +# IR receiver setup +# Adjust to your IR receiver pin +ir_receiver = pulseio.PulseIn(board.D5, maxlen=120, idle_state=True) +decoder = adafruit_irremote.GenericDecode() + +def decode_ir_signals(p): + codes = decoder.decode_bits(p) + return codes + +while True: + pulses = decoder.read_pulses(ir_receiver) + try: + # Attempt to decode the received pulses + received_code = decode_ir_signals(pulses) + # Update display with the received code + if received_code: + hex_code = ''.join(["%02X" % x for x in received_code]) + text_area.text = f"Received:\n{hex_code}" + print(f"Received: {hex_code}") + except adafruit_irremote.IRNECRepeatException: # Signal was repeated, ignore + pass + except adafruit_irremote.IRDecodeException: # Failed to decode signal + text_area.text = "Error decoding"