1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
| /*
Cath Copyright Cyrob 2021
Cyrob Arduino Task helper by Philippe Demerliac
See my presentation video in French : https://youtu.be/aGwHYCcQ3Io
See also for v1.3 : https://youtu.be/ph57EpJPs5E
=====================================================================================
========================== OPEN SOURCE LICENCE ==================================
=====================================================================================
Copyright 2021 Philippe Demerliac Cyrob.org
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
................................................................................................................
Release history
................................................................................................................
Version Date Author Comment
1.0 30/08/2021 Phildem First version tested ok
1.1 05/09/2021 Phildem Misc fixes, better comments and presentation
1.2 06/09/2021 Phildem Remove unnecessary Cath:: in Cath class definition, (Warning removed)
1.3 08/09/2021 Phildem Misc comments/name fixes, Memory optimised, __CathOpt_SmallCounter__ option added
1.4 13/09/2021 Soif Fixes english comments & indentation
*/
//____________________________________________________________________________________________________________
// Start of Cath definition__________________________________________________________________________________
#define kMaxCathTask 9 // Max Number of task instances. MUST BE >= to tasks instancied
#define __CathOpt_SmallCounter__ // Comment this line to allow 32 bit delay. If not, max period is 65536 ms
#ifdef __CathOpt_SmallCounter__
typedef uint16_t CathCnt;
#else
typedef uint32_t CathCnt;
#endif
class Cath{
public:
// Derived class MUST implement these 2 methods
virtual void SetUp() =0; // Called at setup
virtual void Loop() =0; // Called periodically
CathCnt m_CurCounter; // Curent number of ms before next Loop call
CathCnt m_LoopDelay; // Default period of Loop call (in ms)
static uint8_t S_NbTask; // Actual number of task instances
static Cath* S_CathTasks[kMaxCathTask]; // Array of task object pointers
static uint8_t S_LastMilli; // Used to call every ms (a byte is enought to detect change)
//..............................................................
// Must be called in task constructors to register in the task list
// WARNING : think to set kMaxCathTask as needed
// Task : Pointer to the derivated task to register
// Period : Loop call Period (in ms). WARNING do not pass 0!
// Offset : Delay of the first call in ms (1 def). WARNING do not pass 0!
static void S_Register(Cath* Task,CathCnt Period,CathCnt Offset=1){
Task->m_LoopDelay=Period;
Task->m_CurCounter= Offset;
Cath::S_CathTasks[Cath::S_NbTask++]=Task;
}
//..............................................................
// Must be called once in Arduino setup to call all the task setups
static void S_SetUp(){
for(int T=0;T<S_NbTask;T++)
Cath::S_CathTasks[T]->SetUp();
}
//..............................................................
// Must be called once in Arduino Loop to call all the task loop if needed
static void S_Loop(){
uint8_t CurMilli=millis();
if (CurMilli!=S_LastMilli) {
S_LastMilli=CurMilli;
for(int T=0;T<S_NbTask;T++)
if ( Cath::S_CathTasks[T]->m_CurCounter--==0) {
Cath::S_CathTasks[T]->m_CurCounter=Cath::S_CathTasks[T]->m_LoopDelay;
Cath::S_CathTasks[T]->Loop();
}
}
}
};
//Cath static variables definitions
//(Note set to 0 for code clarity but done by default anyway because they are static)
uint8_t Cath::S_NbTask=0;
Cath* Cath::S_CathTasks[kMaxCathTask];
uint8_t Cath::S_LastMilli=0;
// End of Cath definition ___________________________________________________________________________________
//___________________________________________________________________________________________________________
//****************************************************************************************************************
// I/O Abstraction
#define kOutPinSlowBlink 4 // Output pins where different leds are connected
#define kOutPinFastBlink 5
#define kOutPinAssyBlink 6
#define kOutPinAorB 7
#define kOutPinAandB 8
#define kOutPinAxorB 9
#define kInPinA 2 // Input pins where pushbuttons are connected (Internal pullup required)
#define kInPinB 3
//****************************************************************************************************************
// Globals
bool gPushA=false; // Memory state of button A, true if pushed, debounced
bool gPushB=false; // Memory state of button B, true if pushed, debounced
//Exemple task ...........................................................................................
//Blinker ..........................................................................................
class Blinker: public Cath{
public:
Blinker(uint8_t Pin,unsigned long Period,unsigned long Offset=1){
m_Pin=Pin;
Cath::S_Register(this,Period,Offset);
}
void SetUp(){
pinMode(m_Pin,OUTPUT);
digitalWrite(m_Pin, LOW);
}
void Loop(){
digitalWrite(m_Pin,!digitalRead(m_Pin));
}
uint8_t m_Pin; // Number id of the output pin to blink
};
//Assy Blinker ..........................................................................................
class ABlinker: public Cath{
public:
ABlinker(){
Cath::S_Register(this,2000,666);
}
void SetUp(){
pinMode(kOutPinAssyBlink,OUTPUT);
digitalWrite(kOutPinAssyBlink, LOW);
m_State=0;
}
void Loop(){
if (m_State++ & 1){
digitalWrite(kOutPinAssyBlink,HIGH);
m_CurCounter=50; // Here we force the next call to a shorter value
}
else {
digitalWrite(kOutPinAssyBlink,LOW);
}
}
unsigned char m_State; // State counter, short pulse if Odd
};
//PushBut ..........................................................................................
class PushBut: public Cath{
public:
PushBut(uint8_t Pin, bool* Store){
m_Pin=Pin;
m_Store=Store;
Cath::S_Register(this,20); // Here 20ms is for switch deboudcing
}
void SetUp(){
pinMode(m_Pin,INPUT_PULLUP);
}
void Loop(){
*m_Store=!digitalRead(m_Pin); // We invert the value because the switch in inverted (connected to ground)
}
uint8_t m_Pin; // Number Id of input Pin to test
bool* m_Store; // Boolean value to store the result
};
//PinAorB ..........................................................................................
class PinAorB: public Cath{
public:
PinAorB(){
Cath::S_Register(this,20);
}
void SetUp(){
pinMode(kOutPinAorB,OUTPUT);
}
void Loop(){
digitalWrite(kOutPinAorB, gPushA || gPushB);
}
};
//PinAandB ..........................................................................................
class PinAandB: public Cath{
public:
PinAandB(){
Cath::S_Register(this,20);
}
void SetUp(){
pinMode(kOutPinAandB,OUTPUT);
}
void Loop(){
digitalWrite(kOutPinAandB, gPushA && gPushB);
}
};
//PinAxorB ..........................................................................................
class PinAxorB: public Cath{
public:
PinAxorB(){
Cath::S_Register(this,60);
}
void SetUp(){
pinMode(kOutPinAxorB,OUTPUT);
}
void Loop(){
digitalWrite(kOutPinAxorB, !gPushA != !gPushB && !digitalRead(kOutPinAxorB));
}
};
//****************************************************************************************************************
// Global tasks instanciation
// 3 Instances of the blinker task
Blinker BuiltIn(LED_BUILTIN,100,50);
Blinker Blinker1(kOutPinSlowBlink,500);
Blinker Blinker2(kOutPinFastBlink,1500,500);
// 2 Instances of the PushBut task
PushBut PushA(kInPinA,&gPushA);
PushBut PushB(kInPinB,&gPushB);
// 1 instance by tasks for these one
ABlinker Assy;
PinAorB AorB;
PinAandB AandB;
PinAxorB AxorB;
//-----------------------------------------------------------------------------------------------------------------
void setup() {
Cath::S_SetUp(); // Just ask Cath to call the task's setup
}
//-----------------------------------------------------------------------------------------------------------------
void loop() {
Cath::S_Loop(); // Just ask Cath to call the task's loop
} |
Partager