-
Notifications
You must be signed in to change notification settings - Fork 776
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2846 from adafruit/ir_transceiver
adding examples for the stemma ir transceiver
- Loading branch information
Showing
3 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
64 changes: 64 additions & 0 deletions
64
...A_IR_Transceiver_Examples/Arduino_STEMMA_IR_Transceiver/Arduino_STEMMA_IR_Transceiver.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// SPDX-FileCopyrightText: 2024 Liz Clark for Adafruit Industries | ||
// | ||
// SPDX-License-Identifier: MIT | ||
|
||
/* | ||
* Based on the SimpleReceiver.cpp and SimpleSender.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 | ||
#define IR_SEND_PIN 6 | ||
|
||
void setup() { | ||
Serial.begin(115200); | ||
while (!Serial) | ||
; | ||
Serial.println("Adafruit STEMMA IR Transceiver Demo"); | ||
IrReceiver.begin(IR_RECEIVE_PIN); | ||
IrSender.begin(IR_SEND_PIN); // Start with IR_SEND_PIN -which is defined in PinDefinitionsAndMore.h- as send pin and enable feedback LED at default feedback LED pin | ||
Serial.print("IRin on pin "); | ||
Serial.print(IR_RECEIVE_PIN); | ||
Serial.print(", IRout on pin "); | ||
Serial.println(IR_SEND_PIN); | ||
} | ||
|
||
uint8_t sCommand = 0x34; | ||
uint8_t sRepeats = 0; | ||
|
||
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); | ||
delay(1000); | ||
Serial.println("Sending received command.."); | ||
IrSender.sendNEC(IrReceiver.lastDecodedProtocol, IrReceiver.lastDecodedCommand, IrReceiver.repeatCount); | ||
delay(1000); | ||
Serial.print("Sent!"); | ||
//Serial.println(IrReceiver.lastDecodedProtocol, IrReceiver.lastDecodedCommand, IrReceiver.repeatCount); | ||
} | ||
Serial.println(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# SPDX-FileCopyrightText: 2024 Liz Clark for Adafruit Industries | ||
# SPDX-License-Identifier: MIT | ||
|
||
import time | ||
import array | ||
import pulseio | ||
import board | ||
import adafruit_irremote | ||
|
||
# Create a 'PulseOut' to send infrared signals on the IR transmitter @ 38KHz | ||
pulseout = pulseio.PulseOut(board.D6, frequency=38000, duty_cycle=2**15) | ||
# Create an encoder that will take numbers and turn them into NEC IR pulses | ||
encoder = adafruit_irremote.GenericTransmit(header=[9000, 4500], | ||
one=[560, 1700], | ||
zero=[560, 560], | ||
trail=0) | ||
# IR receiver setup | ||
ir_receiver = pulseio.PulseIn(board.D5, maxlen=120, idle_state=True) | ||
decoder = adafruit_irremote.GenericDecode() | ||
|
||
while True: | ||
pulses = decoder.read_pulses(ir_receiver) | ||
try: | ||
# Attempt to decode the received pulses | ||
received_code = decoder.decode_bits(pulses) | ||
if received_code: | ||
hex_code = ''.join(["%02X" % x for x in received_code]) | ||
print(f"Received: {hex_code}") | ||
# Convert pulses to an array of type 'H' (unsigned short) | ||
pulse_array = array.array('H', pulses) | ||
# send code back using original pulses | ||
pulseout.send(pulse_array) | ||
print(f"Sent: {pulse_array}") | ||
except adafruit_irremote.IRNECRepeatException: # Signal was repeated, ignore | ||
pass | ||
except adafruit_irremote.IRDecodeException: # Failed to decode signal | ||
print("Error decoding") | ||
ir_receiver.clear() # Clear the receiver buffer | ||
time.sleep(1) # Delay to allow the receiver to settle | ||
print() |