MakeBlock Drive Updated Updated library for MakeBlock Ranger

Loading...
Searching...
No Matches
MeIR.h
Go to the documentation of this file.
1
58#ifndef MeIR_h
59#define MeIR_h
60
61/* Includes ------------------------------------------------------------------*/
62#include <stdint.h>
63#include <stdbool.h>
64#include <Arduino.h>
65#include "MeConfig.h"
66#ifdef ME_PORT_DEFINED
67#include "MePort.h"
68#endif // ME_PORT_DEFINED
69
70#ifndef __AVR_ATmega32U4__
71#define MARK 0
72#define SPACE 1
73#define NEC_BITS 32
74
75#define USECPERTICK 50 // microseconds per clock interrupt tick
76#define RAWBUF 100 // Length of raw duration buffer
77
78 typedef enum {ERROR = 0, SUCCESS = !ERROR} ErrorStatus;
79
80#define NEC_HDR_MARK 9000
81#define NEC_HDR_SPACE 4500
82#define NEC_BIT_MARK 560
83#define NEC_ONE_SPACE 1600
84#define NEC_ZERO_SPACE 560
85#define NEC_RPT_SPACE 2250
86#define NEC_RPT_PERIOD 110000
87
88
89// #define TOLERANCE 25 // percent tolerance in measurements
90// #define LTOL (1.0 - TOLERANCE/100.)
91// #define UTOL (1.0 + TOLERANCE/100.)
92
93#define _GAP 5000 // Minimum map between transmissions
94// #define GAP_TICKS 5000//(_GAP/USECPERTICK)
95
96// #define TICKS_LOW(us) (int) (((us)*LTOL/USECPERTICK))
97// #define TICKS_HIGH(us) (int) (((us)*UTOL/USECPERTICK + 1))
98
99// receiver states
100#define STATE_IDLE 2
101#define STATE_MARK 3
102#define STATE_SPACE 4
103#define STATE_STOP 5
104
105
106// Values for decode_type
107#define NEC 1
108#define SONY 2
109#define RC5 3
110#define RC6 4
111#define DISH 5
112#define SHARP 6
113#define PANASONIC 7
114#define JVC 8
115#define SANYO 9
116#define MITSUBISHI 10
117#define SAMSUNG 11
118#define LG 12
119#define UNKNOWN -1
120
121#define TOPBIT 0x80000000
122
123
124#ifdef F_CPU
125#define SYSCLOCK F_CPU // main Arduino clock
126#else
127#define SYSCLOCK 16000000 // main Arduino clock
128#endif
129
130
131#define _GAP 5000 // Minimum map between transmissions
132#define GAP_TICKS (_GAP/USECPERTICK)
133
134
135#define TIMER_DISABLE_INTR (TIMSK2 = 0)
136#define TIMER_ENABLE_PWM (TCCR2A |= _BV(COM2B1))
137#define TIMER_DISABLE_PWM (TCCR2A &= ~(_BV(COM2B1)))
138#define TIMER_ENABLE_INTR (TIMSK2 = _BV(OCIE2A))
139#define TIMER_DISABLE_INTR (TIMSK2 = 0)
140#define TIMER_INTR_NAME TIMER2_COMPA_vect
141#define TIMER_CONFIG_KHZ(val) ({ \
142 const uint8_t pwmval = F_CPU / 2000 / (val); \
143 TCCR2A = _BV(WGM20); \
144 TCCR2B = _BV(WGM22) | _BV(CS20); \
145 OCR2A = pwmval; \
146 OCR2B = pwmval / 3; \
147})
148
149#define TIMER_COUNT_TOP (SYSCLOCK * USECPERTICK / 1000000)
150#if (TIMER_COUNT_TOP < 256)
151#define TIMER_CONFIG_NORMAL() ({ \
152 TCCR2A = _BV(WGM21); \
153 TCCR2B = _BV(CS20); \
154 OCR2A = TIMER_COUNT_TOP; \
155 TCNT2 = 0; \
156})
157#else
158#define TIMER_CONFIG_NORMAL() ({ \
159 TCCR2A = _BV(WGM21); \
160 TCCR2B = _BV(CS21); \
161 OCR2A = TIMER_COUNT_TOP / 8; \
162 TCNT2 = 0; \
163})
164#endif
165
166// information for the interrupt handler
167 typedef struct {
168 uint8_t recvpin; // pin for IR data from detector
169 volatile uint8_t rcvstate; // state machine
170 volatile uint32_t lastTime;
171 unsigned int timer; //
172 volatile uint8_t rawbuf[RAWBUF]; // raw data
173 volatile uint8_t rawlen; // counter of entries in rawbuf
174}
175 irparams_t;
176
177// main class for receiving IR
183 class MeIR
184{
185 public:
192 MeIR();
193
208 ErrorStatus decode();
209
224 void begin();
225
240 void end();
241
256 void loop();
257
272 boolean keyPressed(unsigned char r);
273 // void resume();
274
275 int8_t decode_type; // NEC, SONY, RC5, UNKNOWN
276 unsigned long value; // Decoded value
277 uint8_t bits; // Number of bits in decoded value
278 volatile uint8_t *rawbuf; // Raw intervals in .5 us ticks
279 int rawlen; // Number of records in rawbuf.
280
295 String getString();
296
311 unsigned char getCode();
312
327 void sendString(String s);
328
343 void sendString(float v);
344
361 void sendNEC(unsigned long data, int nbits);
362
381 void sendRaw(unsigned int buf[], int len, uint8_t hz);
382
397 void enableIROut(uint8_t khz);
398
413 void enableIRIn();
414
429 void mark(uint16_t us);
430
445 void space(uint16_t us);
446
447 private:
448 // These are called by decode
463 ErrorStatus decodeNEC();
464
465 int16_t irIndex;
466 char irRead;
467 char floatString[5];
468 boolean irReady;
469 boolean irPressed;
470 String irBuffer;
471 String Pre_Str;
472 double irDelayTime;
473};
474#endif // !__AVR_ATmega32U4__
475#endif
476
MeConfig.h
Configuration file of library.
MePort.h
Header for MePort.cpp module.
MeIR
Driver for Me IR module.
Definition MeIR.h:184
MeIR::sendRaw
void sendRaw(unsigned int buf[], int len, uint8_t hz)
MeIR::decode
ErrorStatus decode()
MeIR::sendString
void sendString(float v)
MeIR::mark
void mark(uint16_t us)
MeIR::enableIROut
void enableIROut(uint8_t khz)
MeIR::end
void end()
MeIR::keyPressed
boolean keyPressed(unsigned char r)
MeIR::MeIR
MeIR()
MeIR::begin
void begin()
MeIR::sendNEC
void sendNEC(unsigned long data, int nbits)
MeIR::sendString
void sendString(String s)
MeIR::getString
String getString()
MeIR::getCode
unsigned char getCode()
MeIR::enableIRIn
void enableIRIn()
MeIR::space
void space(uint16_t us)
MeIR::loop
void loop()
irparams_t
Definition MeIR.h:167