Skip to content

Commit 1b108a3

Browse files
committed
Split thread
1 parent 4d5c8dd commit 1b108a3

File tree

1 file changed

+27
-13
lines changed

1 file changed

+27
-13
lines changed

src/main.cpp

+27-13
Original file line numberDiff line numberDiff line change
@@ -17,36 +17,50 @@ 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) {
45-
// do nothing
46-
} else {
47-
// trigger suspend; or received no inputs for 1s
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+
const bool trigger_suspend = (res & TRIGGER_SUSPEND_FLAG) != 0;
57+
const bool received_input = (res & RECEIVED_INPUT_FLAG) != 0;
58+
if (trigger_suspend || !received_input) {
4859
output.suspend();
60+
} else {
61+
// do nothing
4962
}
63+
flags.clear(WATCH_FLAGS);
5064
}
5165
});
5266
osStatus inputs_thread_status = inputs_thread.start([&inputs]() {

0 commit comments

Comments
 (0)