ir-remote/arduino/arduino.ino
2022-07-01 19:05:00 -04:00

94 lines
2.1 KiB
C++

#include <IRremote.hpp>
#include <Arduino.h>
// define IR_SEND_PIN 4
int IR_RECEIVE_PIN = 11;
int IR_SEND_PIN = 4;
String delim = ",";
uint16_t sAddress = 0x0000;
uint8_t sCommand = 0x00;
uint8_t sRepeats = 0x00;
bool readyToSend = false;
void setup() {
Serial.begin(9600);
while (!Serial) {
;
}
// Receiver
Serial.print("Setting up receiver pin ");
Serial.println(IR_RECEIVE_PIN);
IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK);
// Sender
Serial.print("Setting up sender pin ");
Serial.println(IR_SEND_PIN);
pinMode(IR_SEND_PIN, OUTPUT);
IrSender.begin(IR_SEND_PIN);
Serial.flush();
}
void loop() {
if (IrReceiver.decode()) {
IrReceiver.printIRResultShort(&Serial);
IrReceiver.resume();
}
/*
if (readyToSend) {
// Send out an actual result
sendData();
// TODO: Don't need to have this variable in the future probably
readyToSend = false;
} else {
// If we aren't sending data, then check for serial data
// Try to parse serial, and if you can, set readyToSend to true
readyToSend = parseSerial();
}
*/
if (parseSerial()) {
sendData();
}
delay(1000); // delay must be greater than 5 ms (RECORD_GAP_MICROS), otherwise the receiver sees it as one long signal
}
bool parseSerial() {
if (Serial.available() == 0) {
// There is no data for us
return false;
}
String input = Serial.readStringUntil('\n');
Serial.print("Got serial message: ");
Serial.println(input);
Serial.flush();
sAddress = atoi(strtok(input.c_str(), delim.c_str()));
sCommand = atoi(strtok(NULL, delim.c_str()));
sRepeats = atoi(strtok(NULL, delim.c_str()));
// If we have a real address and command, we successfully parsed
return sAddress && sCommand;
}
void sendData() {
Serial.print("Send now: protocol: Samsung address=0x");
Serial.print(sAddress, HEX);
Serial.print(" command=0x");
Serial.print(sCommand, HEX);
Serial.print(" repeats=");
Serial.println(sRepeats);
Serial.flush();
IrSender.sendSamsung(sAddress, sCommand, sRepeats);
}