Skip to content

Commit 6104202

Browse files
committed
upd
1 parent 9b2d080 commit 6104202

File tree

8 files changed

+85
-10
lines changed

8 files changed

+85
-10
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ void loop() {
6262
> Это позволяет разделять программу на полностью независимые потоки, а также писать код в событийно-ориентированном стиле
6363
6464
Минусы:
65-
- Каждая задача занимает в оперативной памяти 7 байт
65+
- Каждая задача занимает в оперативной памяти 7 байт (вес можно уменьшить, отключив поддержку ID и событий)
6666
- Проход по списку задач и вызов обработчиков занимает в районе 8us на одну задачу (AVR 16MHz), что равносильно двум вызовам digitalRead
6767

6868
<a id="versions"></a>

docs/0.main.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
- `DataCallback` - функция вида `void f(T* data)`
55
- `LooperCallback` - функция вида `void f(hash_t id)`
66

7+
### Настройки
8+
В файле platform.h можно настроить некоторые параметры библиотеки для уменьшения веса и переноса на отличные от Arduino платформы. Например можно отключить поддержку ID задач для уменьшения веса самих задач и отключить поддержку отправки событий для уменьшения веса всей библиотеки в целом
9+
710
### Макросы
811
```cpp
912
// создать обработчик

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=Looper
2-
version=1.1.0
2+
version=1.1.1
33
author=AlexGyver <[email protected]>
44
maintainer=AlexGyver <[email protected]>
55
sentence=Simple task and event manager for Arduino

src/LooperClass.cpp

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
#include <limits.h>
44

5-
#include "nodes/Timer.h"
65
#include "nodes/Thread.h"
6+
#include "nodes/Timer.h"
77
#include "platform.h"
88

99
#define LP_LIST_AMOUNT 2
@@ -26,14 +26,20 @@ void LooperClass::loop() {
2626
}
2727
if (_removed) _removed = false;
2828
else _thisTask = _thisTask->getPrev();
29+
#if LOOPER_USE_EVENTS
2930
while (_events.length()) _sendEvent(_events.pop());
31+
#endif
3032
}
33+
#if LOOPER_USE_EVENTS
3134
while (_events.length()) _sendEvent(_events.pop());
35+
#endif
3236
_thisState = tState::None;
3337
}
3438

3539
void LooperClass::onEvent(LooperCallback callback) {
40+
#if LOOPER_USE_EVENTS
3641
_event_cb = callback;
42+
#endif
3743
}
3844

3945
void LooperClass::restart() {
@@ -54,7 +60,11 @@ uint32_t LooperClass::nextTimerLeft() {
5460
}
5561

5662
uint16_t LooperClass::length() {
63+
#if LOOPER_USE_EVENTS
5764
return _tasks.length() + _lisns.length();
65+
#else
66+
return _tasks.length();
67+
#endif
5868
}
5969

6070
void LooperClass::delay(uint32_t ms) {
@@ -65,14 +75,26 @@ void LooperClass::delay(uint32_t ms) {
6575

6676
void LooperClass::add(LoopTask* task) {
6777
if (!task) return;
78+
79+
#if LOOPER_USE_EVENTS
6880
task->isListener() ? _lisns.add(task) : _tasks.add(task);
81+
#else
82+
if (!task->isListener()) _tasks.add(task);
83+
#endif
84+
6985
if (!_setup) _tickState(task, tState::Setup);
7086
}
7187

7288
void LooperClass::remove(LoopTask* task) {
7389
if (!task) return;
7490
_tickState(task, tState::Exit);
91+
92+
#if LOOPER_USE_EVENTS
7593
task->isListener() ? _lisns.remove(task) : _tasks.remove(task);
94+
#else
95+
if (!task->isListener()) _tasks.remove(task);
96+
#endif
97+
7698
if (_thisTask == task) {
7799
_thisTask = _thisTask->getPrev();
78100
_removed = true;
@@ -95,8 +117,6 @@ void LooperClass::removeThis() {
95117
remove(thisTask());
96118
}
97119

98-
bool LooperClass::eventBroadcast() { return _broadcast; }
99-
LoopTask* LooperClass::eventSource() { return _source; }
100120
LoopTask* LooperClass::thisTask() { return _removed ? nullptr : _thisTask; }
101121
LoopTimer* LooperClass::thisTimer() { return thisTaskAs<LoopTimer>(); }
102122
LoopThread* LooperClass::thisThread() { return thisTaskAs<LoopThread>(); }
@@ -106,15 +126,35 @@ bool LooperClass::thisExit() { return _thisState == tState::Exit; }
106126
bool LooperClass::thisEvent() { return _thisState == tState::Event; }
107127
bool LooperClass::thisLoop() { return _thisState == tState::Loop; }
108128

129+
bool LooperClass::eventBroadcast() {
130+
#if LOOPER_USE_EVENTS
131+
return _broadcast;
132+
#else
133+
return 0;
134+
#endif
135+
}
136+
137+
LoopTask* LooperClass::eventSource() {
138+
#if LOOPER_USE_EVENTS
139+
return _source;
140+
#else
141+
return nullptr;
142+
#endif
143+
}
109144
void* LooperClass::eventData() {
145+
#if LOOPER_USE_EVENTS
110146
return _thisState == tState::Event ? _data : nullptr;
147+
#else
148+
return nullptr;
149+
#endif
111150
}
112151

113152
void LooperClass::_sendEvent(EventData& evt) {
114153
sendEvent(evt.id, evt.data);
115154
}
116155

117156
void LooperClass::sendEvent(hash_t id, void* data) {
157+
#if LOOPER_USE_EVENTS
118158
LoopTask* source = _thisTask;
119159
tState stateTemp = _thisState;
120160
_thisState = tState::Event;
@@ -126,6 +166,7 @@ void LooperClass::sendEvent(hash_t id, void* data) {
126166
_event_cb(id);
127167
}
128168

169+
#if LOOPER_USE_ID
129170
for (uint8_t i = 0; i < LP_LIST_AMOUNT; i++) {
130171
_thisTask = _getList(i)->getLast();
131172
while (_thisTask) {
@@ -140,26 +181,33 @@ void LooperClass::sendEvent(hash_t id, void* data) {
140181
else _thisTask = _thisTask->getPrev();
141182
}
142183
}
184+
#endif
143185

144186
_broadcast = false;
145187
_source = nullptr;
146188
_thisTask = source;
147189
_thisState = stateTemp;
190+
#endif
148191
}
149192

150193
void LooperClass::sendEvent(const char* id, void* data) {
151194
sendEvent(LPHr(id), data);
152195
}
153196

154197
bool LooperClass::pushEvent(hash_t id, void* data) {
198+
#if LOOPER_USE_EVENTS
155199
return _events.push(EventData{id, data});
200+
#else
201+
return 0;
202+
#endif
156203
}
157204

158205
bool LooperClass::pushEvent(const char* id, void* data) {
159206
return pushEvent(LPHr(id), data);
160207
}
161208

162209
LoopTask* LooperClass::getTask(hash_t id) {
210+
#if LOOPER_USE_EVENTS
163211
if (!id) return nullptr;
164212
for (uint8_t i = 0; i < LP_LIST_AMOUNT; i++) {
165213
LoopTask* p = _getList(i)->getLast();
@@ -168,6 +216,7 @@ LoopTask* LooperClass::getTask(hash_t id) {
168216
p = p->getPrev();
169217
}
170218
}
219+
#endif
171220
return nullptr;
172221
}
173222

@@ -190,6 +239,8 @@ LoopTimer* LooperClass::getTimer(const char* id) {
190239
return getTimer(LPHr(id));
191240
}
192241

242+
#if LOOPER_USE_EVENTS
193243
looper::List<LoopTask>* LooperClass::_getList(uint8_t idx) {
194244
return (looper::List<LoopTask>*[]){&_tasks, &_lisns}[idx];
195-
}
245+
}
246+
#endif

src/LooperClass.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,21 +113,26 @@ class LooperClass {
113113
void* data;
114114
};
115115

116-
LooperCallback _event_cb = nullptr;
117116
LoopTask* _thisTask = nullptr;
117+
looper::List<LoopTask> _tasks;
118+
119+
#if LOOPER_USE_EVENTS
118120
LoopTask* _source = nullptr;
121+
LooperCallback _event_cb = nullptr;
119122
void* _data = nullptr;
120-
looper::List<LoopTask> _tasks;
121123
looper::List<LoopTask> _lisns;
122124
looper::Stack<EventData, LOOPER_QUEUE_SIZE> _events;
125+
126+
looper::List<LoopTask>* _getList(uint8_t idx);
127+
#endif
128+
123129
tState _thisState = tState::None;
124130
bool _setup = true;
125131
bool _removed = false;
126132
bool _broadcast = false;
127133

128134
void _sendEvent(EventData& evt);
129135
void _tickState(LoopTask* task, tState state);
130-
looper::List<LoopTask>* _getList(uint8_t idx);
131136
};
132137

133138
extern LooperClass Looper;

src/nodes/LoopTask.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ void LoopTask::detach() {
2020
}
2121

2222
hash_t LoopTask::id() {
23+
#if LOOPER_USE_ID
2324
return _id;
25+
#else
26+
return 0;
27+
#endif
2428
}
2529

2630
void LoopTask::enable() {

src/nodes/LoopTask.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <inttypes.h>
33
#include <stddef.h>
44

5+
#include "../platform.h"
56
#include "../utils/flags.h"
67
#include "../utils/hash.h"
78
#include "../utils/list.h"
@@ -29,7 +30,10 @@ LP_MAKE_CALLBACK(TaskCallback, void);
2930

3031
class LoopTask : public looper::List<LoopTask>::Node {
3132
public:
32-
LoopTask(hash_t id, TaskCallback callback, uint8_t type, bool states, bool events) : _id(id), _cb(callback) {
33+
LoopTask(hash_t id, TaskCallback callback, uint8_t type, bool states, bool events) : _cb(callback) {
34+
#if LOOPER_USE_ID
35+
_id = id;
36+
#endif
3337
sreg.set(TASK_ENABLED | type);
3438
if (states) enableStates();
3539
if (events) enableEvents();
@@ -102,7 +106,9 @@ class LoopTask : public looper::List<LoopTask>::Node {
102106
uint8_t _tickMask();
103107

104108
private:
109+
#if LOOPER_USE_ID
105110
hash_t _id;
111+
#endif
106112
TaskCallback _cb;
107113
looper::Flags sreg;
108114
};

src/platform.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
// размер стека отложенных событий
55
#define LOOPER_QUEUE_SIZE 3
66

7+
// использовать ID задач для событий
8+
#define LOOPER_USE_ID true
9+
10+
// использовать события
11+
#define LOOPER_USE_EVENTS true
12+
713
namespace looper {
814

915
// миллисекунды со старта МК

0 commit comments

Comments
 (0)