IRremote
IRCommandDispatcher.h
Go to the documentation of this file.
1 /*
2  * IRCommandDispatcher.h
3  *
4  * Library to process IR commands by calling functions specified in a mapping array.
5  *
6  * To run this example you need to install the "IRremote" or "IRMP" library under "Tools -> Manage Libraries..." or "Ctrl+Shift+I"
7  *
8  * Copyright (C) 2019-2026 Armin Joachimsmeyer
9  * armin.joachimsmeyer@gmail.com
10  *
11  * This file is part of ServoEasing https://github.com/ArminJo/ServoEasing.
12  * This file is part of IRMP https://github.com/IRMP-org/IRMP.
13  * This file is part of Arduino-IRremote https://github.com/Arduino-IRremote/Arduino-IRremote.
14  *
15  * IRCommandDispatcher is free software: you can redistribute it and/or modify
16  * it under the terms of the GNU General Public License as published by
17  * the Free Software Foundation, either version 3 of the License, or
18  * (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
23  * See the GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program. If not, see <http://www.gnu.org/licenses/gpl.html>.
27  */
28 
29 #ifndef _IR_COMMAND_DISPATCHER_H
30 #define _IR_COMMAND_DISPATCHER_H
31 
32 #include <stdint.h>
33 
34 //#define DISPATCHER_IR_COMMAND_HAS_MORE_THAN_8_BIT // Enables mapping and dispatching of IR commands consisting of more than 8 bits. Saves up to 160 bytes program memory and 5 bytes RAM + 1 byte RAM per mapping entry.
35 //#define USE_DISPATCHER_COMMAND_STRINGS // Enables the printing of command strings. Requires additional 2 bytes RAM for each command mapping. Requires program memory for strings, but saves snprintf() code (1.5k) if INFO or DEBUG is activated, which has no effect if snprintf() is also used in other parts of your program / libraries.
36 #if defined(USE_DISPATCHER_COMMAND_STRINGS)
37 #define COMMAND_STRING(anyString) anyString
38 #else
39 #define COMMAND_STRING(anyString)
40 #endif
41 /*
42  * For command mapping file
43  */
44 #define IR_COMMAND_FLAG_BLOCKING 0x00 // default - blocking command, repeat not accepted, only one command at a time. Stops an already running command.
45 #define IR_COMMAND_FLAG_REPEATABLE 0x01 // repeat accepted
46 #define IR_COMMAND_FLAG_NON_BLOCKING 0x02 // Non blocking (short) command that can be processed any time and may interrupt other IR commands - used for stop, set direction etc.
47 #define IR_COMMAND_FLAG_REPEATABLE_NON_BLOCKING (IR_COMMAND_FLAG_REPEATABLE | IR_COMMAND_FLAG_NON_BLOCKING)
48 #define IR_COMMAND_FLAG_BEEP 0x04 // Do a single short beep before executing command. May not be useful for short or repeating commands.
49 #define IR_COMMAND_FLAG_BLOCKING_BEEP (IR_COMMAND_FLAG_BLOCKING | IR_COMMAND_FLAG_BEEP)
50 
51 #if !defined(IS_STOP_REQUESTED)
52 #define IS_STOP_REQUESTED IRDispatcher.requestToStopReceived
53 #endif
54 #if !defined(RETURN_IF_STOP)
55 #define RETURN_IF_STOP if (IRDispatcher.requestToStopReceived) return
56 #endif
57 #if !defined(BREAK_IF_STOP)
58 #define BREAK_IF_STOP if (IRDispatcher.requestToStopReceived) break
59 #endif
60 #if !defined(DELAY_AND_RETURN_IF_STOP)
61 #define DELAY_AND_RETURN_IF_STOP(aDurationMillis) if (IRDispatcher.delayAndCheckForStop(aDurationMillis)) return
62 #endif
63 
64 /*
65  * Define as COMMAND_EMPTY a code which is not sent by the remote - otherwise please redefine it here
66  */
67 #if defined(DISPATCHER_IR_COMMAND_HAS_MORE_THAN_8_BIT)
68 #define COMMAND_EMPTY __UINT_FAST16_MAX__ // 0xFFFF code no command
69 typedef uint_fast16_t IRCommandType;
70 #else
71 typedef uint_fast8_t IRCommandType;
72 #define COMMAND_EMPTY __UINT_FAST8_MAX__ // 0xFF code no command
73 #endif
74 
75 // Basic mapping structure
78  uint8_t Flags;
79  void (*CommandToCall)();
80 #if defined(USE_DISPATCHER_COMMAND_STRINGS)
81  const char *CommandString;
82 #endif
83 };
84 
86  uint16_t address; // to distinguish between multiple senders
88  bool isRepeat;
89  volatile uint32_t MillisOfLastCode; // millis() of last IR command -including repeats!- received - for timeouts etc.
90  volatile bool isAvailable; // flag for a polling interpreting function, that a new command has arrived. Is set true by library and set false by main loop.
91 };
92 
94 public:
95  void init();
96  void printIRInfo(Print *aSerial);
97 
100  void setNextBlockingCommand(IRCommandType aBlockingCommandToRunNext);
101  bool delayAndCheckForStop(uint16_t aDelayMillis);
102 
103  // The main dispatcher function
104  void checkAndCallCommand(bool aCallBlockingCommandImmediately);
105 
106  void printIRCommandString(Print *aSerial, IRCommandType aCommand);
107  void printIRCommandStringForArrayIndex(Print *aSerial, uint_fast8_t aMappingArrayIndex);
108  void setRequestToStopReceived(bool aRequestToStopReceived = true);
109 
110  IRCommandType currentBlockingCommandCalled = COMMAND_EMPTY; // The code for the current called command
111  IRCommandType lastBlockingCommandCalled = COMMAND_EMPTY; // The code for the last called command. Can be evaluated by main loop
112  IRCommandType BlockingCommandToRunNext = COMMAND_EMPTY; // Storage for command currently suspended to allow the current command to end, before it is called by main loop
113  bool justCalledBlockingCommand = false; // Flag that a blocking command was received and called - is set before call of command
114  /*
115  * Flag for running blocking commands to terminate. To check, you can use "if (IRDispatcher.requestToStopReceived) return;" (available as macro RETURN_IF_STOP).
116  * It is set if a blocking IR command received, which cannot be executed directly. Can be reset by main loop, if command has stopped.
117  * It is reset before executing a blocking command.
118  */
119  volatile bool requestToStopReceived;
120  /*
121  * This flag must be true, if we have a function, which want to interpret the IR codes by itself e.g. the calibrate function of QuadrupedControl
122  */
123  bool doNotUseDispatcher = false;
124 
126 
127 };
128 
130 
131 #endif // _IR_COMMAND_DISPATCHER_H
IRCommandDispatcher::requestToStopReceived
volatile bool requestToStopReceived
Definition: IRCommandDispatcher.h:119
IRCommandDispatcher::currentBlockingCommandCalled
IRCommandType currentBlockingCommandCalled
Definition: IRCommandDispatcher.h:110
IRCommandDispatcher::printIRInfo
void printIRInfo(Print *aSerial)
Definition: IRCommandDispatcher.hpp:271
IRCommandDispatcher::init
void init()
Definition: IRCommandDispatcher.hpp:76
IRDataForCommandDispatcherStruct::isAvailable
volatile bool isAvailable
Definition: IRCommandDispatcher.h:90
IRCommandDispatcher::BlockingCommandToRunNext
IRCommandType BlockingCommandToRunNext
Definition: IRCommandDispatcher.h:112
IRCommandDispatcher::setNextBlockingCommand
void setNextBlockingCommand(IRCommandType aBlockingCommandToRunNext)
Definition: IRCommandDispatcher.hpp:449
IRToCommandMappingStruct::Flags
uint8_t Flags
Definition: IRCommandDispatcher.h:78
IRDataForCommandDispatcherStruct::MillisOfLastCode
volatile uint32_t MillisOfLastCode
Definition: IRCommandDispatcher.h:89
IRCommandDispatcher::printIRCommandStringForArrayIndex
void printIRCommandStringForArrayIndex(Print *aSerial, uint_fast8_t aMappingArrayIndex)
Definition: IRCommandDispatcher.hpp:479
IRDataForCommandDispatcherStruct::isRepeat
bool isRepeat
Definition: IRCommandDispatcher.h:88
IRCommandDispatcher
Definition: IRCommandDispatcher.h:93
IRCommandDispatcher::lastBlockingCommandCalled
IRCommandType lastBlockingCommandCalled
Definition: IRCommandDispatcher.h:111
IRDataForCommandDispatcherStruct::address
uint16_t address
Definition: IRCommandDispatcher.h:86
IRCommandDispatcher::printIRCommandString
void printIRCommandString(Print *aSerial, IRCommandType aCommand)
Definition: IRCommandDispatcher.hpp:497
IRToCommandMappingStruct::CommandToCall
void(* CommandToCall)()
Definition: IRCommandDispatcher.h:79
IRCommandDispatcher::delayAndCheckForStop
bool delayAndCheckForStop(uint16_t aDelayMillis)
Definition: IRCommandDispatcher.hpp:469
IRCommandType
uint_fast8_t IRCommandType
Definition: IRCommandDispatcher.h:71
IRToCommandMappingStruct
Definition: IRCommandDispatcher.h:76
IRCommandDispatcher::setRequestToStopReceived
void setRequestToStopReceived(bool aRequestToStopReceived=true)
Definition: IRCommandDispatcher.hpp:507
IRDispatcher
IRCommandDispatcher IRDispatcher
Definition: IRCommandDispatcher.hpp:58
IRDataForCommandDispatcherStruct::command
IRCommandType command
Definition: IRCommandDispatcher.h:87
IRCommandDispatcher::IRReceivedData
struct IRDataForCommandDispatcherStruct IRReceivedData
Definition: IRCommandDispatcher.h:125
IRDataForCommandDispatcherStruct
Definition: IRCommandDispatcher.h:85
IRCommandDispatcher::doNotUseDispatcher
bool doNotUseDispatcher
Definition: IRCommandDispatcher.h:123
IRCommandDispatcher::checkAndCallCommand
void checkAndCallCommand(bool aCallBlockingCommandImmediately)
Definition: IRCommandDispatcher.hpp:301
IRToCommandMappingStruct::IRCode
IRCommandType IRCode
Definition: IRCommandDispatcher.h:77
IRCommandDispatcher::checkAndRunNonBlockingCommands
bool checkAndRunNonBlockingCommands()
IRCommandDispatcher::justCalledBlockingCommand
bool justCalledBlockingCommand
Definition: IRCommandDispatcher.h:113
IRCommandDispatcher::checkAndRunSuspendedBlockingCommands
bool checkAndRunSuspendedBlockingCommands()
Definition: IRCommandDispatcher.hpp:427
COMMAND_EMPTY
#define COMMAND_EMPTY
Definition: IRCommandDispatcher.h:72