Skip to content

Commit d33c750

Browse files
committed
Impl timeout to suspend
1 parent bc55b88 commit d33c750

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

src/main.cpp

+22-14
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,38 @@
1414
using namespace std::chrono_literals;
1515

1616
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;
1921

2022
CachedInputs inputs{};
2123
OutputMachine output{};
2224
mbed::BufferedSerial pc(USBTX, USBRX);
2325

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] = {};
2628
unsigned char inputs_thread_stack[INPUTS_THREAD_STACK_SIZE] = {};
2729

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
3032
);
3133
rtos::Thread inputs_thread(
3234
osPriorityBelowNormal, INPUTS_THREAD_STACK_SIZE, inputs_thread_stack
3335
);
34-
3536
// 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;
3739
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+
}
4149
}
4250
});
4351
osStatus inputs_thread_status = inputs_thread.start([&inputs]() {
@@ -57,11 +65,11 @@ int main() {
5765
DeferedDelay _delay(10ms);
5866
pc.sync();
5967
uint8_t header = 0;
60-
// TODO: timeout
6168
ssize_t read = pc.read(&header, 1);
6269
if (read < 1) {
6370
continue;
6471
}
72+
flags.set(RECEIVED_INPUT_FLAG);
6573
// なぜかこれがないと動かない
6674
rtos::ThisThread::sleep_for(20ms);
6775
switch (header) {
@@ -74,7 +82,7 @@ int main() {
7482
// bldc
7583
uint16_t pulsewidth_us_lsb = static_cast<uint16_t>(buffer[i * 2 + 0]);
7684
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)
7886
| (pulsewidth_us_msb << 8);
7987
// servo
8088
pulsewidth_us_lsb
@@ -101,7 +109,7 @@ int main() {
101109
if (output.state() == State::INITIALIZING) {
102110
continue;
103111
}
104-
trigger_setup.set(1);
112+
flags.set(1);
105113
} break;
106114
case 0xFF:
107115
// suspend

0 commit comments

Comments
 (0)