IRSendRev Class
IRSendRev Class
Description
A class used for managing, sending, and receiving data using IR.
Syntax
class IRSendRev
Members
Public Constructors | |
The public constructor should not be used as this class is intended to be a singleton class. Access member functions using the object instance named IR. |
Public Methods | |
IRSendRecv::Init | Initialize the IRSendRecv class |
IRSendRecv::Send | Send data using IR |
IRSendRecv::Recv | Receive data using IR |
IRSendRecv::IsDta | Check if received data is present |
IRSendRecv::Clear | Clear received data buffer |
IRSendRev::Init
Description
Initialize the IRSendRecv class.
Syntax
void Init(int revPin);
void Init(void);
Parameters
revPin: Pin used for receiving IR data.
Returns
The function returns nothing.
Example Code
Example: recv
Notes and Warnings
revPin should be a pin that has interrupt functionality.
IRSendRev::Send
Description
Send data using IR.
Syntax
void Send(unsigned char *idata, unsigned char ifreq);
Parameters
idata: pointer to data to send
ifreq: IR modulation frequency in kilohertz (kHz)
Returns
The function returns nothing.
Example Code
Example: send
The code in send.ino indicates how does the Infrared Emitter sends the infrared data. Must connect the IR send pins to D3 for this demo.
#include "IRSendRev.h" #define BIT_LEN 0 #define BIT_START_H 1 #define BIT_START_L 2 #define BIT_DATA_H 3 #define BIT_DATA_L 4 #define BIT_DATA_LEN 5 #define BIT_DATA 6 const int ir_freq = 38; // 38k unsigned char dtaSend[20]; void dtaInit() { dtaSend[BIT_LEN] = 11; // all data that needs to be sent dtaSend[BIT_START_H] = 179; // the logic high duration of "Start" dtaSend[BIT_START_L] = 90; // the logic low duration of "Start" dtaSend[BIT_DATA_H] = 11; // the logic "long" duration in the communication dtaSend[BIT_DATA_L] = 33; // the logic "short" duration in the communication dtaSend[BIT_DATA_LEN] = 6; // Number of data which will sent. If the number is other, you should increase or reduce dtaSend[BIT_DATA+x]. dtaSend[BIT_DATA+0] = 128; // data that will sent dtaSend[BIT_DATA+1] = 127; dtaSend[BIT_DATA+2] = 192; dtaSend[BIT_DATA+3] = 63; dtaSend[BIT_DATA+4] = 192; dtaSend[BIT_DATA+5] = 63; } void setup() { /* Connect D1 to data line of IR transmitter */ dtaInit(); } void loop() { IR.Send(dtaSend, 38); delay(2000); }
Notes and Warnings
Include “IRSendRev.h” to use the class function.
IRSendRev::Recv
Description
Receive data using IR.
Syntax
unsigned char Recv(unsigned char *revData);
Parameters
revData: pointer to the location for storing received data.
Returns
The total number of received bytes.
Example Code
Example: recv
Connect the IR receiver pins to D2 for this demo. You can see the remote control’s infrared data that received through a serial port terminal, then write the received infrared data into sending.ino and downloaded to the board with Infrared Emitter Grove, so you can send the same data with remote control’s button.
#include "IRSendRev.h" #define BIT_LEN 0 #define BIT_START_H 1 #define BIT_START_L 2 #define BIT_DATA_H 3 #define BIT_DATA_L 4 #define BIT_DATA_LEN 5 #define BIT_DATA 6 // Ameba need using pin that has interrupt #if defined(BOARD_RTL8195A) const int pinRecv = 3; // ir receiver connect to D3 #elif defined(BOARD_RTL8710) const int pinRecv = 12; // ir receiver connect to D13 #else const int pinRecv = 3; // ir receiver connect to D3 #endif void setup() { Serial.begin(115200); IR.Init(pinRecv); Serial.println("init over"); } unsigned char dta[20]; void loop() { if(IR.IsDta()) // get IR data { IR.Recv(dta); // receive data to dta Serial.println("+------------------------------------------------------+"); Serial.print("LEN = "); Serial.println(dta[BIT_LEN]); Serial.print("START_H: "); Serial.print(dta[BIT_START_H]); Serial.print("\tSTART_L: "); Serial.println(dta[BIT_START_L]); Serial.print("DATA_H: "); Serial.print(dta[BIT_DATA_H]); Serial.print("\tDATA_L: "); Serial.println(dta[BIT_DATA_L]); Serial.print("\r\nDATA_LEN = "); Serial.println(dta[BIT_DATA_LEN]); Serial.print("DATA: "); for(int i=0; i<dta[BIT_DATA_LEN]; i++) { Serial.print("0x"); Serial.print(dta[i+BIT_DATA], HEX); Serial.print("\t"); } Serial.println(); Serial.print("DATA: "); for(int i=0; i<dta[BIT_DATA_LEN]; i++) { Serial.print(dta[i+BIT_DATA], DEC); Serial.print("\t"); } Serial.println(); Serial.println("+------------------------------------------------------+\r\n\r\n"); } }
Notes and Warnings
Include “IRSendRev.h” to use the class function.
IRSendRev::IsDta
Description
Check if received data is present.
Syntax
unsigned char IsDta(void);
Parameters
The function requires no input parameter.
Returns
The function returns “0” if no data received or received data is incomplete.
Example Code
Example: recv
Details of the code can be found in the previous section of IRSendRev::Recv.
Notes and Warnings
Include “IRSendRev.h” to use the class function.
IRSendRev::Clear
Description
Clear received data buffer.
Syntax
void Clear(void);
Parameters
The function requires no input parameter.
Returns
The function returns nothing.
Example Code
NA
Notes and Warnings
Include “IRSendRev.h” to use the class function.