example code for ir remote receiver
Adding CP example code, product demo code and Arduino code for the IR remote receiver
This commit is contained in:
parent
9142c83df3
commit
3d2da42319
3 changed files with 141 additions and 0 deletions
|
|
@ -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 <Arduino.h>
|
||||||
|
|
||||||
|
#include <IRremote.hpp> // 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
28
STEMMA_IR_Remote_Receiver_Examples/CircuitPython/code.py
Normal file
28
STEMMA_IR_Remote_Receiver_Examples/CircuitPython/code.py
Normal file
|
|
@ -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")
|
||||||
|
|
@ -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"
|
||||||
Loading…
Reference in a new issue