Skip to content

Commit 63afe7f

Browse files
committed
Merge branch 'functional-shouldbe-lib'
2 parents d33b987 + d2ec37c commit 63afe7f

File tree

4 files changed

+82
-57
lines changed

4 files changed

+82
-57
lines changed

cores/esp32/Schedule.cpp

+23-13
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,22 @@ static scheduled_fn_t* sLastUnused = 0;
1414

1515
static int sCount = 0;
1616

17-
static scheduled_fn_t* get_fn() {
17+
static scheduled_fn_t* get_fn()
18+
{
1819
scheduled_fn_t* result = NULL;
1920
// try to get an item from unused items list
20-
if (sFirstUnused) {
21+
if (sFirstUnused)
22+
{
2123
result = sFirstUnused;
2224
sFirstUnused = result->mNext;
23-
if (sFirstUnused == NULL) {
25+
if (sFirstUnused == NULL)
26+
{
2427
sLastUnused = NULL;
2528
}
2629
}
2730
// if no unused items, and count not too high, allocate a new one
28-
else if (sCount != SCHEDULED_FN_MAX_COUNT) {
31+
else if (sCount != SCHEDULED_FN_MAX_COUNT)
32+
{
2933
result = new scheduled_fn_t;
3034
result->mNext = NULL;
3135
++sCount;
@@ -35,10 +39,12 @@ static scheduled_fn_t* get_fn() {
3539

3640
static void recycle_fn(scheduled_fn_t* fn)
3741
{
38-
if (!sLastUnused) {
42+
if (!sLastUnused)
43+
{
3944
sFirstUnused = fn;
4045
}
41-
else {
46+
else
47+
{
4248
sLastUnused->mNext = fn;
4349
}
4450
fn->mNext = NULL;
@@ -48,15 +54,18 @@ static void recycle_fn(scheduled_fn_t* fn)
4854
bool schedule_function(std::function<void(void)> fn)
4955
{
5056
scheduled_fn_t* item = get_fn();
51-
if (!item) {
57+
if (!item)
58+
{
5259
return false;
5360
}
5461
item->mFunc = fn;
5562
item->mNext = NULL;
56-
if (!sFirst) {
63+
if (!sFirst)
64+
{
5765
sFirst = item;
5866
}
59-
else {
67+
else
68+
{
6069
sLast->mNext = item;
6170
}
6271
sLast = item;
@@ -65,10 +74,11 @@ bool schedule_function(std::function<void(void)> fn)
6574

6675
void run_scheduled_functions()
6776
{
68-
scheduled_fn_t* rFirst = sFirst;
69-
sFirst = NULL;
70-
sLast = NULL;
71-
while (rFirst) {
77+
scheduled_fn_t* rFirst = sFirst;
78+
sFirst = NULL;
79+
sLast = NULL;
80+
while (rFirst)
81+
{
7282
scheduled_fn_t* item = rFirst;
7383
rFirst = item->mNext;
7484
item->mFunc();

cores/esp32/Schedule.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,20 @@
66
#define SCHEDULED_FN_MAX_COUNT 32
77
#define SCHEDULED_FN_INITIAL_COUNT 4
88

9-
// Warning
10-
// This API is not considered stable.
9+
// Warning
10+
// This API is not considered stable.
1111
// Function signatures will change.
1212
// You have been warned.
1313

14-
// Run given function next time `loop` function returns,
14+
// Run given function next time `loop` function returns,
1515
// or `run_scheduled_functions` is called.
1616
// Use std::bind to pass arguments to a function, or call a class member function.
1717
// Note: there is no mechanism for cancelling scheduled functions.
1818
// Keep that in mind when binding functions to objects which may have short lifetime.
1919
// Returns false if the number of scheduled functions exceeds SCHEDULED_FN_MAX_COUNT.
2020
bool schedule_function(std::function<void(void)> fn);
2121

22-
// Run all scheduled functions.
22+
// Run all scheduled functions.
2323
// Use this function if your are not using `loop`, or `loop` does not return
2424
// on a regular basis.
2525
void run_scheduled_functions();

libraries/ESP32/examples/GPIO/FunctionalInterrupt/FunctionalInterrupts.cpp

+49-37
Original file line numberDiff line numberDiff line change
@@ -3,65 +3,77 @@
33

44
void ICACHE_RAM_ATTR interruptFunctional(void* arg)
55
{
6-
ArgStructure* localArg = static_cast<ArgStructure*>(arg);
7-
if (localArg->interruptInfo)
8-
{
9-
localArg->interruptInfo->value = digitalRead(localArg->interruptInfo->pin);
10-
localArg->interruptInfo->micro = micros();
11-
}
12-
if (localArg->functionInfo->reqScheduledFunction)
13-
{
14-
schedule_function(
15-
[reqScheduledFunction = localArg->functionInfo->reqScheduledFunction,
16-
interruptInfo = *localArg->interruptInfo]() { reqScheduledFunction(interruptInfo); });
17-
}
18-
else if (localArg->functionInfo->reqFunction)
19-
{
20-
localArg->functionInfo->reqFunction();
21-
}
6+
ArgStructure* localArg = static_cast<ArgStructure*>(arg);
7+
if (localArg->interruptInfo)
8+
{
9+
localArg->interruptInfo->value = digitalRead(localArg->interruptInfo->pin);
10+
localArg->interruptInfo->micro = micros();
11+
}
12+
if (localArg->functionInfo->reqScheduledFunction)
13+
{
14+
schedule_function(
15+
[reqScheduledFunction = localArg->functionInfo->reqScheduledFunction,
16+
interruptInfo = *localArg->interruptInfo]()
17+
{
18+
reqScheduledFunction(interruptInfo);
19+
});
20+
}
21+
else if (localArg->functionInfo->reqFunction)
22+
{
23+
localArg->functionInfo->reqFunction();
24+
}
2225
}
2326

2427
void cleanupFunctional(void* arg)
2528
{
26-
ArgStructure* localArg = static_cast<ArgStructure*>(arg);
27-
delete localArg;
29+
ArgStructure* localArg = static_cast<ArgStructure*>(arg);
30+
delete localArg;
2831
}
2932

3033
void attachInterrupt(uint8_t pin, std::function<void(void)> intRoutine, int mode)
3134
{
32-
// use the local interrupt routine which takes the ArgStructure as argument
35+
// use the local interrupt routine which takes the ArgStructure as argument
3336

34-
void* localArg = detachInterruptArg(pin);
35-
if (localArg) cleanupFunctional(localArg);
37+
void* localArg = detachInterruptArg(pin);
38+
if (localArg)
39+
{
40+
cleanupFunctional(localArg);
41+
}
3642

37-
FunctionInfo* fi = new FunctionInfo;
38-
fi->reqFunction = intRoutine;
43+
FunctionInfo* fi = new FunctionInfo;
44+
fi->reqFunction = intRoutine;
3945

40-
ArgStructure* as = new ArgStructure;
41-
as->functionInfo = fi;
46+
ArgStructure* as = new ArgStructure;
47+
as->functionInfo = fi;
4248

43-
attachInterruptArg (pin, interruptFunctional, as, mode);
49+
attachInterruptArg(pin, interruptFunctional, as, mode);
4450
}
4551

4652
void attachScheduledInterrupt(uint8_t pin, std::function<void(InterruptInfo)> scheduledIntRoutine, int mode)
4753
{
48-
void* localArg = detachInterruptArg(pin);
49-
if (localArg) cleanupFunctional(localArg);
54+
void* localArg = detachInterruptArg(pin);
55+
if (localArg)
56+
{
57+
cleanupFunctional(localArg);
58+
}
5059

51-
InterruptInfo* ii = new InterruptInfo(pin);
60+
InterruptInfo* ii = new InterruptInfo(pin);
5261

53-
FunctionInfo* fi = new FunctionInfo;
54-
fi->reqScheduledFunction = scheduledIntRoutine;
62+
FunctionInfo* fi = new FunctionInfo;
63+
fi->reqScheduledFunction = scheduledIntRoutine;
5564

56-
ArgStructure* as = new ArgStructure;
57-
as->interruptInfo = ii;
58-
as->functionInfo = fi;
65+
ArgStructure* as = new ArgStructure;
66+
as->interruptInfo = ii;
67+
as->functionInfo = fi;
5968

60-
attachInterruptArg(pin, interruptFunctional, as, mode);
69+
attachInterruptArg(pin, interruptFunctional, as, mode);
6170
}
6271

6372
void detachFunctionalInterrupt(uint8_t pin)
6473
{
65-
void* localArg = detachInterruptArg(pin);
66-
if (localArg) cleanupFunctional(localArg);
74+
void* localArg = detachInterruptArg(pin);
75+
if (localArg)
76+
{
77+
cleanupFunctional(localArg);
78+
}
6779
}

libraries/ESP32/examples/GPIO/FunctionalInterrupt/FunctionalInterrupts.h

+6-3
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,22 @@
55

66
// Structures for communication
77

8-
struct InterruptInfo {
8+
struct InterruptInfo
9+
{
910
InterruptInfo(uint8_t _pin) : pin(_pin) {}
1011
const uint8_t pin;
1112
uint8_t value = 0;
1213
uint32_t micro = 0;
1314
};
1415

15-
struct FunctionInfo {
16+
struct FunctionInfo
17+
{
1618
std::function<void(void)> reqFunction = nullptr;
1719
std::function<void(InterruptInfo)> reqScheduledFunction = nullptr;
1820
};
1921

20-
struct ArgStructure {
22+
struct ArgStructure
23+
{
2124
~ArgStructure()
2225
{
2326
delete functionInfo;

0 commit comments

Comments
 (0)