Skip to content

Commit 6cd57f7

Browse files
committed
Introducing init() method for initializing tasks.
1 parent 57fdcc3 commit 6cd57f7

23 files changed

+127
-45
lines changed

examples/SoftTimer6Debouncer1/SoftTimer6Debouncer1.ino

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Debouncer debouncer(INPUT_PIN, MODE_CLOSE_ON_PUSH, onPressed, onReleased);
1414

1515
void setup() {
1616
Serial.begin(9800);
17+
debouncer.init();
1718
PciManager.registerListener(INPUT_PIN, &debouncer);
1819
Serial.println("Ready.");
1920
}
@@ -24,4 +25,4 @@ void onPressed() {
2425
void onReleased(unsigned long pressTimespan) {
2526
Serial.print(pressTimespan);
2627
Serial.println(" - released");
27-
}
28+
}

examples/SoftTimer6Debouncer2/SoftTimer6Debouncer2.ino

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Debouncer debouncer(INPUT_PIN, MODE_CLOSE_ON_PUSH, onPressed, onReleased);
2121

2222
void setup() {
2323
Serial.begin(9800);
24+
debouncer.init();
2425
// -- Setup external pin change interrupt for the pin.
2526
setupInterrupts(INPUT_PIN);
2627
Serial.println("Ready.");

examples/SoftTimer6Rotary/SoftTimer6Rotary.ino

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ Debouncer rotPushDebouncer(ROT_PUSH, MODE_CLOSE_ON_PUSH, onRotPushPress, onRotPu
1616

1717
void setup() {
1818
Serial.begin(9600);
19+
rotPushDebouncer.init();
20+
r.init();
1921
PciManager.registerListener(ROT_PUSH, &rotPushDebouncer);
2022

2123
Serial.println("Ready.");

examples/SoftTimer8Dimmer/SoftTimer8Dimmer.ino

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ SoftPwmTask pwm(OUT_PIN);
2121
Dimmer dimmer(&pwm, 1500);
2222

2323
void setup() {
24+
dimmer.init();
2425
PciManager.registerListener(INPUT_PIN, &debouncer);
2526
}
2627

@@ -38,4 +39,4 @@ void onReleased(unsigned long pressTimespan) {
3839
}
3940
}
4041

41-
42+

examples/SoftTimer9FrequencyTask/SoftTimer9FrequencyTask.ino

+4-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ Task stateStepTask(3000, stateStep);
1919
int state = 0;
2020

2121
void setup() {
22+
f1.init();
23+
f2.init();
24+
f3.init();
2225
SoftTimer.add(&stateStepTask);
2326
}
2427

@@ -48,4 +51,4 @@ void stateStep(Task* task) {
4851
SoftTimer.remove(&f1);
4952
state = 0;
5053
}
51-
}
54+
}

library.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=SoftTimer
2-
version=3.1.5
2+
version=3.2.0
33
author=Balazs Kelemen <[email protected]>
44
maintainer=Balazs Kelemen <[email protected]>
55
sentence=SoftTimer is a lightweight pseudo multitasking solution for Arduino.

src/BlinkTask.cpp

+22-11
Original file line numberDiff line numberDiff line change
@@ -31,37 +31,48 @@
3131
#define BLINK_STATE_ON 1
3232
#define BLINK_STATE_WAIT 2
3333

34-
void BlinkTask::init(byte outPin, unsigned long onMs, unsigned long offMs, byte count, unsigned long delayMs) {
34+
void BlinkTask::setupProperties(byte outPin, unsigned long onMs, unsigned long offMs, byte count, unsigned long delayMs) {
35+
this->_outPin = outPin;
3536
this->onMs = onMs;
3637
this->offMs = offMs;
3738
this->count = count;
3839
this->delayMs = delayMs;
40+
this->onLevel = HIGH;
41+
}
3942

40-
pinMode(outPin, OUTPUT);
41-
this->_bitMask = digitalPinToBitMask(outPin);
42-
this->_portRegister = portOutputRegister(digitalPinToPort(outPin));
43+
void BlinkTask::init() {
44+
pinMode(this->_outPin, OUTPUT);
45+
this->_bitMask = digitalPinToBitMask(this->_outPin);
46+
this->_portRegister = portOutputRegister(digitalPinToPort(this->_outPin));
4347

44-
this->onLevel = HIGH;
48+
// -- Turn off.
49+
if(this->onLevel == HIGH) {
50+
*this->_portRegister &= ~this->_bitMask;
51+
} else {
52+
*this->_portRegister |= this->_bitMask;
53+
}
54+
Task::init();
4555
}
4656

4757
BlinkTask::BlinkTask(byte outPin, unsigned long onOffMs) : Task(onOffMs, &(BlinkTask::stepState)) {
48-
this->init(outPin, onOffMs, onOffMs, 0, 0);
58+
this->setupProperties(outPin, onOffMs, onOffMs, 0, 0);
4959
}
5060

5161
BlinkTask::BlinkTask(byte outPin, unsigned long onMs, unsigned long offMs) : Task(onMs, &(BlinkTask::stepState)) {
52-
this->init(outPin, onMs, offMs, 0, 0);
62+
this->setupProperties(outPin, onMs, offMs, 0, 0);
5363
}
5464

5565
BlinkTask::BlinkTask(byte outPin, unsigned long onMs,unsigned long offMs, byte count) : Task(onMs, &(BlinkTask::stepState)) {
56-
this->init(outPin, onMs, offMs, count, 0);
57-
this->stop();
66+
this->setupProperties(outPin, onMs, offMs, count, 0);
5867
}
5968

60-
BlinkTask::BlinkTask(byte outPin, unsigned long onMs, unsigned long offMs, byte count, unsigned long delayMs) : Task(onMs, &(BlinkTask::stepState)) {
61-
this->init(outPin, onMs, offMs, count, delayMs);
69+
BlinkTask::BlinkTask(byte outPin, unsigned long onMs, unsigned long offMs, byte count, unsigned long delayMs)
70+
: Task(onMs, &(BlinkTask::stepState)) {
71+
this->setupProperties(outPin, onMs, offMs, count, delayMs);
6272
}
6373

6474
void BlinkTask::start() {
75+
this->init();
6576
this->_state = BLINK_STATE_OFF;
6677
this->_counter = 0;
6778
this->setPeriodMs(0);

src/BlinkTask.h

+5-3
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@
2727
#ifndef BLINKTASK_H
2828
#define BLINKTASK_H
2929

30-
#include <inttypes.h>
31-
#include "Arduino.h"
30+
#include <Arduino.h>
3231
#include "Task.h"
3332

3433
class BlinkTask : public Task
@@ -69,6 +68,8 @@ class BlinkTask : public Task
6968
*/
7069
BlinkTask(byte outPin, unsigned long onMs, unsigned long offMs, byte count, unsigned long delayMs);
7170

71+
void init() override;
72+
7273
/**
7374
* Register the blink Task to the Timer Manager: start blinking.
7475
*/
@@ -102,12 +103,13 @@ class BlinkTask : public Task
102103
byte onLevel;
103104

104105
private:
105-
void init(byte outPin, unsigned long onMs, unsigned long offMs, byte count, unsigned long delayMs);
106+
void setupProperties(byte outPin, unsigned long onMs, unsigned long offMs, byte count, unsigned long delayMs);
106107
static void stepState(Task* me);
107108
byte _counter;
108109
/** Can be STATE_OFF, STATE_ON, STATE_WAIT */
109110
byte _state;
110111

112+
byte _outPin;
111113
uint8_t _bitMask;
112114
#ifndef ESP8266
113115
volatile uint8_t *_portRegister;

src/Debouncer.cpp

+10-4
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,20 @@ Debouncer::Debouncer(int pin, int pushMode, void (*onPressed)(), void (*onReleas
3636
this->_onLevel = pushMode;
3737
this->_onPressed = onPressed;
3838
this->_onReleased = onReleased;
39-
40-
if(pullUp) {
41-
pinMode(pin, INPUT_PULLUP);
39+
this->_pullUp = pullUp;
40+
}
41+
42+
void Debouncer::init()
43+
{
44+
if(this->_pullUp) {
45+
pinMode(this->_pin, INPUT_PULLUP);
4246
} else {
43-
pinMode(pin, INPUT);
47+
pinMode(this->_pin, INPUT);
4448
}
4549
this->_state = digitalRead(this->_pin) == this->_onLevel ? STATE_ON : STATE_OFF;
4650

51+
Task::init();
52+
4753
SoftTimer.add(this);
4854
}
4955

src/Debouncer.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ class Debouncer : public PciListener, public Task
6060
* it was released.
6161
*/
6262
Debouncer(int pin, int pushMode, void (*onPressed)(), void (*onReleased)(unsigned long pressTimespan), bool pullUp=false);
63+
64+
void init() override;
6365

6466
/**
6567
* Please call this function on interrupt.
@@ -74,8 +76,9 @@ class Debouncer : public PciListener, public Task
7476
private:
7577
int _pin;
7678
int _onLevel;
79+
bool _pullUp;
7780
volatile int _state; // 0=off, 1=bouncing, 2=pressing
78-
unsigned long _pressStart;
81+
volatile unsigned long _pressStart;
7982
void (*_onPressed)();
8083
void (*_onReleased)(unsigned long pressTimespan);
8184
static void step(Task* me);

src/Dimmer.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,16 @@ Dimmer::Dimmer(SoftPwmTask* pwm, int frequencyMs, byte stepCount) : Task(10, &(D
3232
this->_pwm = pwm;
3333
this->direction = DIMMER_DIRECTION_HIGH;
3434
this->value = 0;
35-
this->_pwm->analogWrite((byte)this->value);
3635
this->stepCount = stepCount;
3736

3837
this->setFrequency(frequencyMs);
3938
}
4039

40+
void Dimmer::init()
41+
{
42+
this->_pwm->analogWrite((byte)this->value);
43+
Task::init();
44+
}
4145

4246
void Dimmer::start(boolean stopOnLimit) {
4347
this->stopOnLimit = stopOnLimit;

src/Dimmer.h

+2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ class Dimmer : public Task
4747
*/
4848
Dimmer(SoftPwmTask* pwm, int frequencyMs, byte stepCount = DEFAULT_STEP_COUNT);
4949

50+
void init() override;
51+
5052
/**
5153
* Start an unlimited pulsation from the current value on, in the current direction.
5254
*/

src/FrequencyTask.cpp

+9-3
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,20 @@
2929

3030
FrequencyTask::FrequencyTask(int outPin, float freq) : Task(0, &(FrequencyTask::step))
3131
{
32-
pinMode(outPin, OUTPUT);
33-
32+
this->_outPin = outPin;
33+
3434
_bitMask = digitalPinToBitMask(outPin);
3535
_portRegister = portOutputRegister(digitalPinToPort(outPin));
3636

3737
this->setFrequency(freq);
3838
}
3939

40+
void FrequencyTask::init()
41+
{
42+
pinMode(this->_outPin, OUTPUT);
43+
44+
Task::init();
45+
}
4046

4147
void FrequencyTask::setFrequency(float freq) {
4248
this->periodMicros = 500000.0 / freq;
@@ -55,4 +61,4 @@ void FrequencyTask::step(Task* task)
5561

5662
ft->_stateOn = !ft->_stateOn;
5763
}
58-
64+

src/FrequencyTask.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,15 @@ class FrequencyTask : public Task
4242
*/
4343
FrequencyTask(int outPin, float freq);
4444

45+
void init() override;
46+
4547
/**
4648
* Adjust the frequency.
4749
*/
4850
void setFrequency(float freq);
4951

5052
private:
53+
int _outPin;
5154
boolean _stateOn;
5255
uint8_t _bitMask;
5356
#ifndef ESP8266
@@ -59,4 +62,4 @@ class FrequencyTask : public Task
5962
};
6063

6164
#endif
62-
65+

src/Rotary.cpp

+13-5
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,24 @@
3838

3939
Rotary::Rotary(int pinA, int pinB, void (*onRotation)(short direction, Rotary* rotary), bool pullUp)
4040
: Task(IDDLE_TIME_MICROS, &(Rotary::step)) {
41-
this->_listenerA.init(pinA, this, pullUp);
42-
this->_listenerB.init(pinB, this, pullUp);
41+
this->_pinA = pinA;
42+
this->_pinB = pinB;
43+
this->_pullUp = pullUp;
4344
this->_stateCW = EVENT_NOTIFIED;
4445
this->_stateCCW = EVENT_NOTIFIED;
4546
this->_onRotation = onRotation;
46-
47-
PciManager.registerListener(pinA, &this->_listenerA);
48-
PciManager.registerListener(pinB, &this->_listenerB);
47+
}
48+
49+
void Rotary::init()
50+
{
51+
this->_listenerA.init(this->_pinA, this, this->_pullUp);
52+
this->_listenerB.init(this->_pinB, this, this->_pullUp);
53+
54+
PciManager.registerListener(this->_pinA, &this->_listenerA);
55+
PciManager.registerListener(this->_pinB, &this->_listenerB);
4956

5057
SoftTimer.add(this);
58+
Task::init();
5159
}
5260

5361
void Rotary::pciHandleChange(byte changedTo, PciListenerImp2* listener) {

src/Rotary.h

+6
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ class Rotary : public Task, public IPciChangeHandler
5252
* it was released.
5353
*/
5454
Rotary(int pinA, int pinB, void (*onRotation)(short direction, Rotary* rotary), bool pullUp=false);
55+
56+
void init() override;
5557

5658
/**
5759
* Please call this function on interrupt.
@@ -63,6 +65,10 @@ class Rotary : public Task, public IPciChangeHandler
6365
PciListenerImp2 _listenerB = PciListenerImp2();
6466
volatile int _stateCW;
6567
volatile int _stateCCW;
68+
int _pinA;
69+
int _pinB;
70+
boolean _pullUp;
71+
6672
void (*_onRotation)(short direction, Rotary* rotary);
6773
static void step(Task* me);
6874
};

src/SoftPwmTask.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,16 @@ SoftPwmTask::SoftPwmTask(int pin) : Task(0, &(SoftPwmTask::step))
3333
_value = 0;
3434
_counter = 0;
3535
upperLimit = 255;
36-
pinMode(_outPin, OUTPUT);
3736
this->periodMicros = 30;
3837

3938
_bitMask = digitalPinToBitMask(pin);
4039
_portRegister = portOutputRegister(digitalPinToPort(pin));
4140
}
4241

42+
void SoftPwmTask::init() {
43+
pinMode(this->_outPin, OUTPUT);
44+
Task::init();
45+
}
4346

4447
void SoftPwmTask::analogWrite(byte value) {
4548
this->_value = value;

src/SoftPwmTask.h

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ class SoftPwmTask : public Task
3737
* We would like to do pwn on the given pin.
3838
*/
3939
SoftPwmTask(int pin);
40+
41+
void init() override;
4042

4143
/**
4244
* Just like in the Arduino implementation this method will set the duty level of the pin.

src/SoftTimer.cpp

+6-3
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,25 @@
2424
2525
*/
2626

27-
#include "Arduino.h"
2827
#include "SoftTimer.h"
2928

3029
/**
3130
* The main loop is implemented here. You do not ever need to implement this function
3231
* if you think in event driven programing.
3332
*/
3433
void loop() {
35-
SoftTimer.run(); // -- run() will never return, if PREVENT_LOOP_ITERATION macro is defined.
34+
SoftTimer.run(); // -- run() will never return, unless ENABLE_LOOP_ITERATION macro is defined.
3635
}
3736

38-
3937
/**
4038
* Register a task in the timer manager.
4139
*/
4240
void SoftTimerClass::add(Task* task) {
41+
// -- If a task is not initialized, we should do in now.
42+
if (!task->initialized)
43+
{
44+
task->init();
45+
}
4346

4447
// -- A task should be registered only once.
4548
this->remove(task);

0 commit comments

Comments
 (0)