Skip to content

Commit 4641f91

Browse files
committed
Split thread
1 parent 574057e commit 4641f91

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

src/main.cpp

+24-11
Original file line numberDiff line numberDiff line change
@@ -17,36 +17,49 @@ int main() {
1717
constexpr uint32_t TRIGGER_INITIALIZE_FLAG = 0b0001;
1818
constexpr uint32_t RECEIVED_INPUT_FLAG = 0b0010;
1919
constexpr uint32_t TRIGGER_SUSPEND_FLAG = 0b0100;
20+
constexpr size_t INITIALIZE_THREAD_STACK_SIZE = 512;
21+
constexpr size_t SUSPEND_THREAD_STACK_SIZE = 512;
2022
constexpr size_t INPUTS_THREAD_STACK_SIZE = 1024;
21-
constexpr size_t WATCH_THREAD_STACK_SIZE = 1024;
2223

2324
CachedInputs inputs{};
2425
OutputMachine output{};
2526
mbed::BufferedSerial pc(USBTX, USBRX);
2627

2728
rtos::EventFlags flags{};
28-
unsigned char watch_flags_thread_stack[WATCH_THREAD_STACK_SIZE] = {};
29+
unsigned char initialize_thread_stack[INITIALIZE_THREAD_STACK_SIZE] = {};
30+
unsigned char suspend_thread_stack[SUSPEND_THREAD_STACK_SIZE] = {};
2931
unsigned char inputs_thread_stack[INPUTS_THREAD_STACK_SIZE] = {};
3032

31-
rtos::Thread watch_flags_thread(
32-
osPriorityBelowNormal, WATCH_THREAD_STACK_SIZE, watch_flags_thread_stack
33+
rtos::Thread initialize_thread(
34+
osPriorityBelowNormal1, INITIALIZE_THREAD_STACK_SIZE, initialize_thread_stack
35+
);
36+
rtos::Thread suspend_thread(
37+
osPriorityBelowNormal2, SUSPEND_THREAD_STACK_SIZE, suspend_thread_stack
3338
);
3439
rtos::Thread inputs_thread(
35-
osPriorityBelowNormal, INPUTS_THREAD_STACK_SIZE, inputs_thread_stack
40+
osPriorityBelowNormal3, INPUTS_THREAD_STACK_SIZE, inputs_thread_stack
3641
);
3742
// TODO: handle osStatus
38-
watch_flags_thread.start([&output, &flags]() {
39-
constexpr uint32_t WATCH_FLAGS = TRIGGER_INITIALIZE_FLAG | RECEIVED_INPUT_FLAG | TRIGGER_SUSPEND_FLAG;
43+
initialize_thread.start([&output, &flags]() {
4044
while (true) {
41-
const uint32_t res = flags.wait_any_for(WATCH_FLAGS, 1s);
42-
if ((res & TRIGGER_INITIALIZE_FLAG) != 0) {
45+
const uint32_t res = flags.wait_any_for(TRIGGER_INITIALIZE_FLAG, 1s, false);
46+
if (res & TRIGGER_INITIALIZE_FLAG) {
4347
output.initialize();
44-
} else if ((res & RECEIVED_INPUT_FLAG) != 0) {
48+
}
49+
flags.clear(TRIGGER_INITIALIZE_FLAG);
50+
}
51+
});
52+
suspend_thread.start([&output, &flags]() {
53+
constexpr uint32_t WATCH_FLAGS = TRIGGER_SUSPEND_FLAG | RECEIVED_INPUT_FLAG;
54+
while (true) {
55+
const uint32_t res = flags.wait_any_for(WATCH_FLAGS, 1s, false);
56+
if (res & RECEIVED_INPUT_FLAG) {
4557
// do nothing
4658
} else {
47-
// trigger suspend; or received no inputs for 1s
59+
// trigger suspend or timeout
4860
output.suspend();
4961
}
62+
flags.clear(WATCH_FLAGS);
5063
}
5164
});
5265
osStatus inputs_thread_status = inputs_thread.start([&inputs]() {

0 commit comments

Comments
 (0)