Infrared4Arduino 1.2.3
Loading...
Searching...
No Matches
Board.h
Go to the documentation of this file.
1/*
2Copyright (C) 2020 Bengt Martensson.
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation; either version 3 of the License, or (at
7your option) any later version.
8
9This program is distributed in the hope that it will be useful, but
10WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12General Public License for more details.
13
14You should have received a copy of the GNU General Public License along with
15this program. If not, see http://www.gnu.org/licenses/.
16 */
17
28#pragma once
29
30#include "IrSignal.h"
31#include "PinModeStatus.h"
32
33class Board {
34protected:
37 };
38
39public:
41
42 virtual void writeLow();
43 virtual void writeHigh();
44
45 virtual void writeLow(pin_t pin) { digitalWrite(pin, LOW); };
46 virtual void writeHigh(pin_t pin) { digitalWrite(pin, HIGH); };
47
48 void setPinMode(pin_t pin, PinMode mode) { pinMode(pin, mode); };
49 bool readDigital(pin_t pin) { return digitalRead(pin); };
50
51 virtual pin_t getPwmPin() const;
52
53 static Board* getInstance() {
54 return instance;
55 };
56
57 static const unsigned long microsPerTick = 50UL; // was USECPERTICK
58
59 virtual void checkValidSendPin(pin_t pin __attribute__((unused))) {/* TODO */};
60
64 static constexpr pin_t NO_PIN = 255U;
65
69 static constexpr dutycycle_t defaultDutyCycle = 40;
70
74 virtual void enableSampler(pin_t pin __attribute__((unused))) {
77 timerReset();
78 }
79
83 virtual void disableSampler() {
85 }
86
93 void enablePwm(pin_t pin, frequency_t frequency, dutycycle_t dutyCycle) {
95 //pinMode(getPin(), OUTPUT);
96 //writeLow();
97 timerConfigHz(frequency, dutyCycle);
98 }
99
103 void disablePwm() {
104 }
105
107 timerEnablePwm(); // supposed to turn on
108 delayMicroseconds(time);
110 }
111
115 virtual void timerReset() {};
116
117protected:
121 virtual void timerEnableIntr() = 0;
122
126 virtual void timerDisableIntr() = 0;
127
131 virtual void timerConfigHz(frequency_t hz, dutycycle_t dutyCycle = defaultDutyCycle) = 0;
132
136 virtual void timerConfigNormal() = 0;
137
141 virtual void timerEnablePwm() = 0;
142
146 virtual void timerDisablePwm() = 0;
147
148public:
149 // Function defined later in this file
150 static constexpr pin_t defaultPwmPin();
151
152 static constexpr bool hasHardwarePwm();
153
154 static void debugPinHigh();
155
156 static void debugPinLow();
157
158 static void setupDebugPin();
159
160private:
161 static Board* instance;
162};
163
165//------------------------------------------------------------------------------
166// CPU Frequency
167//
168#ifndef F_CPU
169#error F_CPU not defined. This must be fixed.
170#endif
171
172#if ! defined(ARDUINO)
173
174// Assume that we compile a test version, to be executed on the host, not on a board.
175#include "boards/NoBoard.h"
176
177#elif defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__)
179//#define IR_USE_TIMER1 // tx = pin 9
180#define IR_USE_TIMER2 // tx = pin 3
181
182#include "boards/ATmega328P.h"
183
184#elif defined(__AVR_ATmega2560__)
186//#define IR_USE_TIMER1 // tx = pin 11
187#define IR_USE_TIMER2 // tx = pin 9
188//#define IR_USE_TIMER3 // tx = pin 5
189//#define IR_USE_TIMER4 // tx = pin 6
190//#define IR_USE_TIMER5 // tx = pin 46
191
192#include "boards/ATmega2560.h"
193
194#elif defined(__AVR_ATmega32U4__)
195// Can be either Leonardo, Micro, or Teensy 2.
196#if defined(ARDUINO_AVR_LEONARDO) || defined(ARDUINO_AVR_MICRO)
197#define IR_USE_TIMER1 // tx = 9
198//#define IR_USE_TIMER2 // tx = 3, currently broken
199
200#include "boards/ATmega32U4.h"
201
202#else
204#error Unsupported board. Please fix boarddefs.h.
205#endif
206
207#elif defined(__AVR_ATmega4809__)
208// ATMega4809, like Uno WiFi Rev 2, Nano Every
209#define IR_USE_TIMER1 // tx = pin 6
210//#define IR_USE_TIMER2 // Not yet implemented tx = ?
211
212#include "boards/ATmega4809.h"
213
214#elif defined(__SAM3X8E__) || defined(__SAM3X8H__)
215// Arduino Due
216
217 //#define IR_USE_PWM0 // tx = pin 34
218 //#define IR_USE_PWM1 // tx = pin 36
219 //#define IR_USE_PWM2 // tx = pin 38
220 //#define IR_USE_PWM3 // tx = pin 40
221 //#define IR_USE_PWM4 // tx = pin 9
222 //#define IR_USE_PWM5 // tx = pin 8
223 #define IR_USE_PWM6 // tx = pin 7
224 //#define IR_USE_PWM7 // tx = pin 6
225
226 #define IR_USE_TC3 // Use timer clock 3.
227 //#define IR_USE_TC4 // Use timer clock 4.
228 //#define IR_USE_TC5 // Use timer clock 5.
229
230// #define IR_USE_SAM // Used to correct code where needed to be compatible with the Due.
231// #define IR_USE_DUE // Used to correctly map pins. (The idea being there might be more than one Arduino model based on SAM cores.)
232
233#include "boards/Due.h"
234
235//#elif defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__)
236#elif defined(ARDUINO_TEENSY30) || defined(ARDUINO_TEENSY32)
237// Teensy 3.0 / Teensy 3.1 / 3.2
238
239#include "boards/Teensy3x.h"
240
241#elif defined(ARDUINO_ARCH_SAM) || defined(ARDUINO_ARCH_SAMD)
242
243#include "boards/Sam.h"
244
246#elif defined(ESP32)
247
248#include "boards/Esp32.h"
249
251// This board is not supported, see
252// https://github.com/bengtmartensson/Infrared4Arduino/issues/5
253// for the reasons.
254
255#else
256
257//#error Your board is currently not supported. Please add it to boarddefs.h.
258#warning The present board is either unknown, or does not support (Hardware) PWM \
259or equidistant timer sampling. The classes IrReceiverSampler and \
260IrSenderPwmHard will not be available.
261
262#include "boards/NoBoard.h"
263
264#endif
265
266//#define DEBUG_PIN 2
267
268inline void Board::setupDebugPin() {
269#ifdef DEBUG_PIN
270 instance->setPinMode(DEBUG_PIN, OUTPUT);
271#endif
272}
273
274inline void Board::debugPinHigh() {
275#ifdef DEBUG_PIN
276 digitalWrite(DEBUG_PIN, HIGH);
277#endif
278}
279
280inline void Board::debugPinLow() {
281#ifdef DEBUG_PIN
282 digitalWrite(DEBUG_PIN, LOW);
283#endif
284}
285
286#if !HAS_FLASH_READ
287// Dummy definition for allowing compiling some stuff
288typedef void * uint_farptr_t;
289#endif
290
291#ifdef HAS_HARDWARE_PWM
292
293inline constexpr bool Board::hasHardwarePwm() {
294 return true;
295}
296inline constexpr pin_t Board::defaultPwmPin() { return PWM_PIN; }
297inline pin_t Board::getPwmPin() const { return PWM_PIN; }
298inline void Board::writeLow() { digitalWrite(getPwmPin(), LOW); }
299inline void Board::writeHigh() { digitalWrite(getPwmPin(), HIGH); }
300
301#else
302
303inline constexpr bool Board::hasHardwarePwm() { return false; };
304inline constexpr pin_t Board::defaultPwmPin() { return NO_PIN; };
305inline pin_t Board::getPwmPin() const { return NO_PIN; };
306inline void Board::writeLow() { digitalWrite(getPwmPin(), LOW); };
307inline void Board::writeHigh() { digitalWrite(getPwmPin(), HIGH); };
308
309#endif
Hardware dependent definitions for boards based on the ATMega 2560.
Hardware dependent definitions for boards based on the ATMega 328P.
#define PWM_PIN
Definition: ATmega328P.h:108
Hardware dependent definitions for Leonardo like boards like Leonardo and Micro.
Hardware dependent definitions for boards based on the ATMega 4809.
void * uint_farptr_t
Definition: Board.h:288
Hardware dependent definitions for Esp32 boards.
int8_t dutycycle_t
Type for duty cycle in percent.
Definition: InfraredTypes.h:36
uint16_t microseconds_t
Type for durations in micro seconds.
Definition: InfraredTypes.h:15
uint32_t frequency_t
Type for modulation frequency in Hz.
Definition: InfraredTypes.h:31
uint8_t pin_t
Type for GPIO pin, compatible with Arduino libs.
Definition: InfraredTypes.h:41
This file make sure that the types PinMode and PinStatus, as well as the values HIGH and LOW are avai...
int PinMode
Different operating modes of a GPIO pin.
Definition: PinModeStatus.h:55
Hardware dependent definitions for Teensy 3.1/3.2 and probably more.
This class serves as an HAL (Hardware Abstraction Layer).
Definition: Board.h:33
virtual void timerEnableIntr()=0
Start periodic sampling routine.
virtual void enableSampler(pin_t pin __attribute__((unused)))
Start the periodic ISR sampler routine.
Definition: Board.h:74
virtual void writeHigh(pin_t pin)
Definition: Board.h:46
void sendPwmMark(microseconds_t time)
Definition: Board.h:106
virtual void disableSampler()
Turn off sampler routine.
Definition: Board.h:83
static void debugPinHigh()
Definition: Board.h:274
bool readDigital(pin_t pin)
Definition: Board.h:49
void enablePwm(pin_t pin, frequency_t frequency, dutycycle_t dutyCycle)
Start PWM, making output active.
Definition: Board.h:93
static Board * getInstance()
Definition: Board.h:53
virtual void checkValidSendPin(pin_t pin __attribute__((unused)))
Definition: Board.h:59
static void setupDebugPin()
Definition: Board.h:268
static void debugPinLow()
Definition: Board.h:280
virtual void timerDisablePwm()=0
Turn off PWM output.
virtual void timerConfigHz(frequency_t hz, dutycycle_t dutyCycle=defaultDutyCycle)=0
Configure hardware PWM, but do not enable it.
static const unsigned long microsPerTick
Definition: Board.h:57
static constexpr bool hasHardwarePwm()
Definition: Board.h:293
static constexpr pin_t defaultPwmPin()
Definition: Board.h:296
virtual void timerDisableIntr()=0
Turn off periodic interrupts.
virtual void writeLow(pin_t pin)
Definition: Board.h:45
void disablePwm()
Turn off PWM.
Definition: Board.h:103
void setPinMode(pin_t pin, PinMode mode)
Definition: Board.h:48
virtual void timerEnablePwm()=0
Start PWM output.
Board()
Definition: Board.h:35
virtual void writeHigh()
Definition: Board.h:299
static void delayMicroseconds(microseconds_t)
Version of delayMicroseconds able to grok more than 16383 micros.
Definition: Board.cpp:25
virtual void writeLow()
Definition: Board.h:298
virtual void timerConfigNormal()=0
Disables the PWM configuration.
static constexpr dutycycle_t defaultDutyCycle
Default duty cycle to use.
Definition: Board.h:69
static constexpr pin_t NO_PIN
Constant indicating no or invalid pin.
Definition: Board.h:64
virtual void timerReset()
TODO.
Definition: Board.h:115
virtual pin_t getPwmPin() const
Definition: Board.h:297