@@ -17,36 +17,50 @@ int main() {
17
17
constexpr uint32_t TRIGGER_INITIALIZE_FLAG = 0b0001 ;
18
18
constexpr uint32_t RECEIVED_INPUT_FLAG = 0b0010 ;
19
19
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 ;
20
22
constexpr size_t INPUTS_THREAD_STACK_SIZE = 1024 ;
21
- constexpr size_t WATCH_THREAD_STACK_SIZE = 1024 ;
22
23
23
24
CachedInputs inputs{};
24
25
OutputMachine output{};
25
26
mbed::BufferedSerial pc (USBTX, USBRX);
26
27
27
28
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] = {};
29
31
unsigned char inputs_thread_stack[INPUTS_THREAD_STACK_SIZE] = {};
30
32
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
33
38
);
34
39
rtos::Thread inputs_thread (
35
- osPriorityBelowNormal , INPUTS_THREAD_STACK_SIZE, inputs_thread_stack
40
+ osPriorityBelowNormal3 , INPUTS_THREAD_STACK_SIZE, inputs_thread_stack
36
41
);
37
42
// 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]() {
40
44
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) {
43
47
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) {
48
59
output.suspend ();
60
+ } else {
61
+ // do nothing
49
62
}
63
+ flags.clear (WATCH_FLAGS);
50
64
}
51
65
});
52
66
osStatus inputs_thread_status = inputs_thread.start ([&inputs]() {
0 commit comments