14
14
using namespace std ::chrono_literals;
15
15
16
16
int main () {
17
- constexpr size_t INPUTS_THREAD_STACK_SIZE = 1024 ;
18
- constexpr size_t SETUP_THREAD_STACK_SIZE = 512 ;
17
+ constexpr uint32_t TRIGGER_INITIALIZE_FLAG = 0b0001 ;
18
+ constexpr uint32_t RECEIVED_INPUT_FLAG = 0b0010 ;
19
+ constexpr size_t INPUTS_THREAD_STACK_SIZE = 1024 ;
20
+ constexpr size_t WATCH_THREAD_STACK_SIZE = 1024 ;
19
21
20
22
CachedInputs inputs{};
21
23
OutputMachine output{};
22
24
mbed::BufferedSerial pc (USBTX, USBRX);
23
25
24
- rtos::EventFlags trigger_setup {};
25
- unsigned char setup_thread_stack[SETUP_THREAD_STACK_SIZE] = {};
26
+ rtos::EventFlags flags {};
27
+ unsigned char watch_flags_thread_stack[WATCH_THREAD_STACK_SIZE] = {};
26
28
unsigned char inputs_thread_stack[INPUTS_THREAD_STACK_SIZE] = {};
27
29
28
- rtos::Thread setup_thread (
29
- osPriorityBelowNormal, SETUP_THREAD_STACK_SIZE, setup_thread_stack
30
+ rtos::Thread watch_flags_thread (
31
+ osPriorityBelowNormal, WATCH_THREAD_STACK_SIZE, watch_flags_thread_stack
30
32
);
31
33
rtos::Thread inputs_thread (
32
34
osPriorityBelowNormal, INPUTS_THREAD_STACK_SIZE, inputs_thread_stack
33
35
);
34
-
35
36
// TODO: handle osStatus
36
- setup_thread.start ([&output, &trigger_setup]() {
37
+ watch_flags_thread.start ([&output, &flags]() {
38
+ constexpr uint32_t WATCH_FLAGS = TRIGGER_INITIALIZE_FLAG | RECEIVED_INPUT_FLAG;
37
39
while (true ) {
38
- trigger_setup.wait_any (1 , osWaitForever, false );
39
- output.initialize ();
40
- trigger_setup.clear ();
40
+ const uint32_t res = flags.wait_any_for (WATCH_FLAGS, 5s);
41
+ if ((res & TRIGGER_INITIALIZE_FLAG) != 0 ) {
42
+ output.initialize ();
43
+ } else if ((res & RECEIVED_INPUT_FLAG) != 0 ) {
44
+ // do nothing
45
+ } else {
46
+ // received no inputs for 5s
47
+ output.suspend ();
48
+ }
41
49
}
42
50
});
43
51
osStatus inputs_thread_status = inputs_thread.start ([&inputs]() {
@@ -57,11 +65,11 @@ int main() {
57
65
DeferedDelay _delay (10ms);
58
66
pc.sync ();
59
67
uint8_t header = 0 ;
60
- // TODO: timeout
61
68
ssize_t read = pc.read (&header, 1 );
62
69
if (read < 1 ) {
63
70
continue ;
64
71
}
72
+ flags.set (RECEIVED_INPUT_FLAG);
65
73
// なぜかこれがないと動かない
66
74
rtos::ThisThread::sleep_for (20ms);
67
75
switch (header) {
@@ -74,7 +82,7 @@ int main() {
74
82
// bldc
75
83
uint16_t pulsewidth_us_lsb = static_cast <uint16_t >(buffer[i * 2 + 0 ]);
76
84
uint16_t pulsewidth_us_msb = static_cast <uint16_t >(buffer[i * 2 + 1 ]);
77
- pulsewidths_us[i].first = (pulsewidth_us_lsb << 0 )
85
+ pulsewidths_us[i].first = (pulsewidth_us_lsb << 0 )
78
86
| (pulsewidth_us_msb << 8 );
79
87
// servo
80
88
pulsewidth_us_lsb
@@ -101,7 +109,7 @@ int main() {
101
109
if (output.state () == State::INITIALIZING) {
102
110
continue ;
103
111
}
104
- trigger_setup .set (1 );
112
+ flags .set (1 );
105
113
} break ;
106
114
case 0xFF :
107
115
// suspend
0 commit comments