This commit is contained in:
@@ -61,27 +61,29 @@
|
||||
* In IrSqrutinizer, recognition of repeating signals will therefore not work.
|
||||
* The size of the data is platform dependent ("unsigned int", which is 16 bit on AVR boards, 32 bits on 32 bit boards).
|
||||
*
|
||||
* For minimal footprint, undefine all DECODE* and SEND_* in IRremote.h.
|
||||
*
|
||||
* For optimal results, try for example to set _GAP to 100000
|
||||
* (what a brilliant variable name!!) and RAW_BUFFER_LENGTH to 251
|
||||
* in IRremoteInt.h.
|
||||
*/
|
||||
#include <Arduino.h>
|
||||
|
||||
// Change the following two entries if desired
|
||||
#include "PinDefinitionsAndMore.h" // Define macros for input and output pin etc.
|
||||
|
||||
/**
|
||||
* Input Pin used by the receiver, can be arbitrary (almost...)
|
||||
*/
|
||||
#define INPUTPIN 5
|
||||
#if !defined(RAW_BUFFER_LENGTH)
|
||||
# if RAMEND <= 0x4FF || RAMSIZE < 0x4FF
|
||||
#define RAW_BUFFER_LENGTH 180 // 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 500 // 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
|
||||
|
||||
/**
|
||||
* Baud rate for the serial/USB connection.
|
||||
* (115200 is the default for IrScrutinizer and Lirc.)
|
||||
*/
|
||||
#define BAUDRATE 115200
|
||||
#define NO_DECODER
|
||||
|
||||
#include <IRremote.h>
|
||||
#include "IRremote.hpp"
|
||||
#include <limits.h>
|
||||
|
||||
/**
|
||||
@@ -107,14 +109,14 @@
|
||||
/**
|
||||
* The modules supported, as given by the "modules" command.
|
||||
*/
|
||||
#ifdef TRANSMIT
|
||||
#ifdef RECEIVE
|
||||
#if defined(TRANSMIT)
|
||||
#if defined(RECEIVE)
|
||||
#define modulesSupported "base transmit receive"
|
||||
#else // ! RECEIVE
|
||||
#define modulesSupported "base transmit"
|
||||
#endif
|
||||
#else // !TRANSMIT
|
||||
#ifdef RECEIVE
|
||||
#if defined(RECEIVE)
|
||||
#define modulesSupported "base receive"
|
||||
#else // ! RECETVE
|
||||
#error At lease one of TRANSMIT and RECEIVE must be defined
|
||||
@@ -146,26 +148,12 @@ typedef unsigned frequency_t; // max 65535, unless 32-bit
|
||||
/**
|
||||
* Type used for durations in micro seconds.
|
||||
*/
|
||||
typedef unsigned microseconds_t; // max 65535, unless 32-bit
|
||||
typedef uint16_t microseconds_t; // max 65535
|
||||
|
||||
static const microseconds_t DUMMYENDING = 40000U;
|
||||
static const frequency_t FREQUENCY_T_MAX = UINT16_MAX;
|
||||
static const frequency_t MICROSECONDS_T_MAX = UINT16_MAX;
|
||||
|
||||
#ifdef RECEIVE
|
||||
/**
|
||||
* Instance of the IRremote class.
|
||||
*/
|
||||
IRrecv irRecv(INPUTPIN);
|
||||
#endif
|
||||
|
||||
#ifdef TRANSMIT
|
||||
/*}
|
||||
* Instance of the IRremote class.
|
||||
*/
|
||||
IRsend irSend;
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Our own tokenizer class. Breaks the command line into tokens.
|
||||
* Usage outside of this package is discouraged.
|
||||
@@ -175,12 +163,11 @@ private:
|
||||
static const int invalidIndex = -1;
|
||||
|
||||
int index; // signed since invalidIndex is possible
|
||||
const String& payload;
|
||||
const String &payload;
|
||||
void trim();
|
||||
|
||||
public:
|
||||
Tokenizer(const String &str);
|
||||
virtual ~Tokenizer();
|
||||
|
||||
String getToken();
|
||||
String getRest();
|
||||
@@ -192,10 +179,8 @@ public:
|
||||
static const int invalid = INT_MAX;
|
||||
};
|
||||
|
||||
Tokenizer::Tokenizer(const String& str) : index(0), payload(str) {
|
||||
}
|
||||
|
||||
Tokenizer::~Tokenizer() {
|
||||
Tokenizer::Tokenizer(const String &str) :
|
||||
index(0), payload(str) {
|
||||
}
|
||||
|
||||
String Tokenizer::getRest() {
|
||||
@@ -210,7 +195,7 @@ String Tokenizer::getLine() {
|
||||
|
||||
int i = payload.indexOf('\n', index);
|
||||
String s = (i > 0) ? payload.substring(index, i) : payload.substring(index);
|
||||
index = (i > 0) ? i+1 : invalidIndex;
|
||||
index = (i > 0) ? i + 1 : invalidIndex;
|
||||
return s;
|
||||
}
|
||||
|
||||
@@ -222,9 +207,9 @@ String Tokenizer::getToken() {
|
||||
String s = (i > 0) ? payload.substring(index, i) : payload.substring(index);
|
||||
index = (i > 0) ? i : invalidIndex;
|
||||
if (index != invalidIndex)
|
||||
if (index != invalidIndex)
|
||||
while (payload.charAt(index) == ' ')
|
||||
index++;
|
||||
if (index != invalidIndex)
|
||||
while (payload.charAt(index) == ' ')
|
||||
index++;
|
||||
return s;
|
||||
}
|
||||
|
||||
@@ -244,9 +229,9 @@ frequency_t Tokenizer::getFrequency() {
|
||||
}
|
||||
///////////////// end Tokenizer /////////////////////////////////
|
||||
|
||||
#ifdef TRANSMIT
|
||||
#if defined(TRANSMIT)
|
||||
static inline unsigned hz2khz(frequency_t hz) {
|
||||
return (hz + 500)/1000;
|
||||
return (hz + 500) / 1000;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -266,49 +251,48 @@ static inline unsigned hz2khz(frequency_t hz) {
|
||||
* @param frequency Modulation frequency, in Hz (not kHz as normally in IRremote)
|
||||
* @param times Number of times to send the signal, in the sense above.
|
||||
*/
|
||||
static void sendRaw(const microseconds_t intro[], unsigned lengthIntro,
|
||||
const microseconds_t repeat[], unsigned lengthRepeat,
|
||||
const microseconds_t ending[], unsigned lengthEnding,
|
||||
frequency_t frequency, unsigned times) {
|
||||
static void sendRaw(const microseconds_t intro[], unsigned lengthIntro, const microseconds_t repeat[], unsigned lengthRepeat,
|
||||
const microseconds_t ending[], unsigned lengthEnding, frequency_t frequency, unsigned times) {
|
||||
if (lengthIntro > 0U)
|
||||
irSend.sendRaw(intro, lengthIntro, hz2khz(frequency));
|
||||
IrSender.sendRaw(intro, lengthIntro, hz2khz(frequency));
|
||||
if (lengthRepeat > 0U)
|
||||
for (unsigned i = 0U; i < times - (lengthIntro > 0U); i++)
|
||||
irSend.sendRaw(repeat, lengthRepeat, hz2khz(frequency));
|
||||
IrSender.sendRaw(repeat, lengthRepeat, hz2khz(frequency));
|
||||
if (lengthEnding > 0U)
|
||||
irSend.sendRaw(ending, lengthEnding, hz2khz(frequency));
|
||||
IrSender.sendRaw(ending, lengthEnding, hz2khz(frequency));
|
||||
}
|
||||
#endif // TRANSMIT
|
||||
|
||||
#ifdef RECEIVE
|
||||
/**
|
||||
* Reads a command from the stream given as argument.
|
||||
* @param stream Stream to read from, typically Serial.
|
||||
*/
|
||||
static void receive(Stream& stream) {
|
||||
irRecv.enableIRIn();
|
||||
irRecv.resume(); // Receive the next value
|
||||
#if defined(RECEIVE)
|
||||
|
||||
while (!irRecv.decode()) {
|
||||
}
|
||||
irRecv.disableIRIn();
|
||||
|
||||
dump(stream);
|
||||
}
|
||||
|
||||
static void dump(Stream& stream) {
|
||||
unsigned int count = irRecv.results.rawlen;
|
||||
static void dump(Stream &stream) {
|
||||
unsigned int count = IrReceiver.decodedIRData.rawDataPtr->rawlen;
|
||||
// If buffer gets full, count = RAW_BUFFER_LENGTH, which is odd,
|
||||
// and IrScrutinizer does not like that.
|
||||
count &= ~1;
|
||||
count &= ~1;
|
||||
for (unsigned int i = 1; i < count; i++) {
|
||||
stream.write(i & 1 ? '+' : '-');
|
||||
stream.print(irRecv.results.rawbuf[i] * MICROS_PER_TICK, DEC);
|
||||
stream.print(IrReceiver.decodedIRData.rawDataPtr->rawbuf[i] * MICROS_PER_TICK, DEC);
|
||||
stream.print(" ");
|
||||
}
|
||||
stream.print('-');
|
||||
stream.println(DUMMYENDING);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads a command from the stream given as argument.
|
||||
* @param stream Stream to read from, typically Serial.
|
||||
*/
|
||||
static void receive(Stream &stream) {
|
||||
IrReceiver.start();
|
||||
|
||||
while (!IrReceiver.decode()) {
|
||||
}
|
||||
IrReceiver.stop();
|
||||
|
||||
dump(stream);
|
||||
}
|
||||
|
||||
#endif // RECEIVE
|
||||
|
||||
/**
|
||||
@@ -320,19 +304,27 @@ void setup() {
|
||||
; // wait for serial port to connect.
|
||||
|
||||
Serial.println(F(PROGNAME " " VERSION));
|
||||
//Serial.setTimeout(SERIALTIMEOUT);
|
||||
/*
|
||||
#ifdef RECEIVE
|
||||
// There is unfortunately no disableIRIn in IRremote.
|
||||
// Therefore, turn it on, and leave it on.
|
||||
// We _hope_ that it will not interfere with sending.
|
||||
irRecv.enableIRIn();
|
||||
// Just to know which program is running on my Arduino
|
||||
Serial.println(F("START " __FILE__ " from " __DATE__ "\r\nUsing library version " VERSION_IRREMOTE));
|
||||
|
||||
#if defined(RECEIVE)
|
||||
// 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);
|
||||
Serial.print(F("at pin "));
|
||||
#endif
|
||||
*/
|
||||
|
||||
#if defined(IR_SEND_PIN)
|
||||
IrSender.begin(); // Start with IR_SEND_PIN -which is defined in PinDefinitionsAndMore.h- as send pin and enable feedback LED at default feedback LED pin
|
||||
#else
|
||||
IrSender.begin(3, ENABLE_LED_FEEDBACK, USE_DEFAULT_FEEDBACK_LED_PIN); // Specify send pin and enable feedback LED at default feedback LED pin
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
|
||||
static String readCommand(Stream& stream) {
|
||||
static String readCommand(Stream &stream) {
|
||||
while (stream.available() == 0) {
|
||||
}
|
||||
|
||||
@@ -341,7 +333,7 @@ static String readCommand(Stream& stream) {
|
||||
return line;
|
||||
}
|
||||
|
||||
static void processCommand(const String& line, Stream& stream) {
|
||||
static void processCommand(const String &line, Stream &stream) {
|
||||
Tokenizer tokenizer(line);
|
||||
String cmd = tokenizer.getToken();
|
||||
|
||||
@@ -353,47 +345,47 @@ static void processCommand(const String& line, Stream& stream) {
|
||||
}
|
||||
|
||||
switch (cmd[0]) {
|
||||
case 'm':
|
||||
stream.println(F(modulesSupported));
|
||||
break;
|
||||
case 'm':
|
||||
stream.println(F(modulesSupported));
|
||||
break;
|
||||
|
||||
#ifdef RECEIVE
|
||||
case 'r': // receive
|
||||
#if defined(RECEIVE)
|
||||
case 'r': // receive
|
||||
//case 'a':
|
||||
//case 'c':
|
||||
receive(stream);
|
||||
break;
|
||||
receive(stream);
|
||||
break;
|
||||
#endif // RECEIVE
|
||||
|
||||
#ifdef TRANSMIT
|
||||
case 's': // send
|
||||
{
|
||||
// TODO: handle unparsable data gracefully
|
||||
unsigned noSends = (unsigned) tokenizer.getInt();
|
||||
frequency_t frequency = tokenizer.getFrequency();
|
||||
unsigned introLength = (unsigned) tokenizer.getInt();
|
||||
unsigned repeatLength = (unsigned) tokenizer.getInt();
|
||||
unsigned endingLength = (unsigned) tokenizer.getInt();
|
||||
microseconds_t intro[introLength];
|
||||
microseconds_t repeat[repeatLength];
|
||||
microseconds_t ending[endingLength];
|
||||
for (unsigned i = 0; i < introLength; i++)
|
||||
intro[i] = tokenizer.getMicroseconds();
|
||||
for (unsigned i = 0; i < repeatLength; i++)
|
||||
repeat[i] = tokenizer.getMicroseconds();
|
||||
for (unsigned i = 0; i < endingLength; i++)
|
||||
ending[i] = tokenizer.getMicroseconds();
|
||||
sendRaw(intro, introLength, repeat, repeatLength, ending, endingLength, frequency, noSends);
|
||||
stream.println(F(okString));
|
||||
}
|
||||
break;
|
||||
#if defined(TRANSMIT)
|
||||
case 's': // send
|
||||
{
|
||||
// TODO: handle unparsable data gracefully
|
||||
unsigned noSends = (unsigned) tokenizer.getInt();
|
||||
frequency_t frequency = tokenizer.getFrequency();
|
||||
unsigned introLength = (unsigned) tokenizer.getInt();
|
||||
unsigned repeatLength = (unsigned) tokenizer.getInt();
|
||||
unsigned endingLength = (unsigned) tokenizer.getInt();
|
||||
microseconds_t intro[introLength];
|
||||
microseconds_t repeat[repeatLength];
|
||||
microseconds_t ending[endingLength];
|
||||
for (unsigned i = 0; i < introLength; i++)
|
||||
intro[i] = tokenizer.getMicroseconds();
|
||||
for (unsigned i = 0; i < repeatLength; i++)
|
||||
repeat[i] = tokenizer.getMicroseconds();
|
||||
for (unsigned i = 0; i < endingLength; i++)
|
||||
ending[i] = tokenizer.getMicroseconds();
|
||||
sendRaw(intro, introLength, repeat, repeatLength, ending, endingLength, frequency, noSends);
|
||||
stream.println(F(okString));
|
||||
}
|
||||
break;
|
||||
#endif // TRANSMIT
|
||||
|
||||
case 'v': // version
|
||||
stream.println(F(PROGNAME " " VERSION));
|
||||
break;
|
||||
default:
|
||||
stream.println(F(errorString));
|
||||
case 'v': // version
|
||||
stream.println(F(PROGNAME " " VERSION));
|
||||
break;
|
||||
default:
|
||||
stream.println(F(errorString));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user