Created
July 23, 2023 10:21
-
-
Save paul-ridgway/4082025e597d0077b7cd87ab27224aea to your computer and use it in GitHub Desktop.
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
#include <string> | |
#include <Arduino.h> | |
#define IR_RECEIVE_PIN 10 | |
#if !defined(RAW_BUFFER_LENGTH) | |
#if RAMEND <= 0x4FF || RAMSIZE < 0x4FF | |
#define RAW_BUFFER_LENGTH 200 // 750 (600 if we have only 2k RAM) is the value for air condition remotes. Default is 112 if DECODE_MAGIQUEST is enabled, otherwise 100. | |
#elif RAMEND <= 0x8FF || RAMSIZE < 0x8FF | |
#define RAW_BUFFER_LENGTH 600 // 750 (600 if we have only 2k RAM) is the value for air condition remotes. Default is 112 if DECODE_MAGIQUEST is enabled, otherwise 100. | |
#else | |
#define RAW_BUFFER_LENGTH 750 // 750 (600 if we have only 2k RAM) is the value for air condition remotes. Default is 112 if DECODE_MAGIQUEST is enabled, otherwise 100. | |
#endif | |
#endif | |
// #define MICROS_PER_TICK 25L // must be with L to get 32 bit results if multiplied with rawbuf[] content. | |
/* | |
* MARK_EXCESS_MICROS is subtracted from all marks and added to all spaces before decoding, | |
* to compensate for the signal forming of different IR receiver modules. See also IRremote.hpp line 142. | |
* | |
* You can change this value accordingly to the receiver module you use. | |
* The required value can be derived from the timings printed here. | |
* Keep in mind that the timings may change with the distance | |
* between sender and receiver as well as with the ambient light intensity. | |
*/ | |
#define MARK_EXCESS_MICROS 20 // Adapt it to your IR receiver module. 20 is recommended for the cheap VS1838 modules. | |
// #define RECORD_GAP_MICROS 12000 // Default is 5000. Activate it for some LG air conditioner protocols | |
// #define DEBUG // Activate this for lots of lovely debug output from the decoders. | |
#include <IRremote.hpp> | |
//+============================================================================= | |
// Configure the Arduino | |
// | |
void setup() | |
{ | |
Serial.begin(115200); // Status message will be sent to PC at 9600 baud | |
#if defined(__AVR_ATmega32U4__) || defined(SERIAL_PORT_USBVIRTUAL) || defined(SERIAL_USB) /*stm32duino*/ || defined(USBCON) /*STM32_stm32*/ || defined(SERIALUSB_PID) || defined(ARDUINO_attiny3217) | |
delay(4000); // To be able to connect Serial monitor after reset or power up and before first print out. Do not wait for an attached Serial Monitor! | |
#endif | |
// Just to know which program is running on my Arduino | |
Serial.println(F("START " __FILE__ " from " __DATE__ "\r\nUsing library version " VERSION_IRREMOTE)); | |
// Start the receiver and if not 3. parameter specified, take LED_BUILTIN pin from the internal boards definition as default feedback LED | |
IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK); | |
Serial.print(F("Ready to receive IR signals of protocols: ")); | |
printActiveIRProtocols(&Serial); | |
// infos for receive | |
Serial.print(RECORD_GAP_MICROS); | |
Serial.println(F(" us is the (minimum) gap, after which the start of a new IR packet is assumed")); | |
Serial.print(MARK_EXCESS_MICROS); | |
Serial.println(); | |
Serial.println(F("Because of the verbose output (>200 ms at 115200), repeats are probably not dumped correctly!")); | |
Serial.println(); | |
} | |
std::string PadToDoubleDigit(int num) | |
{ | |
if (num < 10) | |
{ | |
return "0" + std::to_string(num); | |
} | |
else | |
{ | |
return std::to_string(num); | |
} | |
} | |
void loop() | |
{ | |
if (IrReceiver.decode()) | |
{ | |
if (IrReceiver.decodedIRData.flags & IRDATA_FLAGS_WAS_OVERFLOW) | |
{ | |
Serial.print("Try to increase the \"RAW_BUFFER_LENGTH\" value of: "); | |
Serial.println(RAW_BUFFER_LENGTH); | |
} | |
else | |
{ | |
auto decodedIRData = IrReceiver.decodedIRData; | |
uint8_t tRawlen = decodedIRData.rawDataPtr->rawlen; // Get it once here in order to print quite consistent data, even if ISR is running | |
for (int i = 1; i < tRawlen; i += 2) | |
{ | |
auto rawMark = decodedIRData.rawDataPtr->rawbuf[i]; | |
auto rawVal = decodedIRData.rawDataPtr->rawbuf[i + 1]; | |
int mark = round(((float)rawMark) / 2.5f); | |
int val = round(((float)rawVal) / 2.5f); | |
if (mark > 5) | |
{ | |
Serial.println("Timing mark above 5!"); | |
break; | |
} | |
if (val < 10) | |
{ | |
Serial.print("0 "); | |
} | |
else | |
{ | |
Serial.print("1 "); | |
} | |
if (((i + 1) / 2) % 10 == 0) { | |
Serial.print(" | "); | |
} | |
} | |
Serial.println(); | |
} | |
IrReceiver.resume(); // Prepare for the next value | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment