Skip to content

Commit 54a094c

Browse files
committed
Cleanup
1 parent e39be22 commit 54a094c

File tree

1 file changed

+23
-32
lines changed

1 file changed

+23
-32
lines changed

src/main.cpp

+23-32
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
#include <mutex>
55

66
#include "BufferedSerial.h"
7-
#include "events/Event.h"
87
#include "events/EventQueue.h"
8+
#include "events/UserAllocatedEvent.h"
99
#include "ThisThread.h"
1010
#include "Thread.h"
1111

@@ -15,25 +15,8 @@
1515

1616
using namespace std::chrono_literals;
1717

18-
class WatchDog {
19-
private:
20-
std::atomic_bool* received_input;
21-
events::Event<void()>* suspend_event;
22-
23-
public:
24-
WatchDog(std::atomic_bool* received_input, events::Event<void()>* suspend_event) :
25-
received_input(received_input),
26-
suspend_event(suspend_event) {}
27-
28-
void check() {
29-
const bool received = this->received_input->load(std::memory_order_acquire);
30-
if (!received) {
31-
this->suspend_event->post();
32-
}
33-
}
34-
};
35-
3618
int main() {
19+
constexpr size_t EVENT_QUEUE_SIZE = 32 * EVENTS_EVENT_SIZE;
3720
constexpr size_t EVENT_LOOP_THREAD_STACK_SIZE = 1024;
3821
constexpr size_t INPUTS_THREAD_STACK_SIZE = 1024;
3922

@@ -42,13 +25,14 @@ int main() {
4225
mbed::BufferedSerial pc(USBTX, USBRX);
4326
std::atomic_bool received_input;
4427

45-
unsigned char equeue_buffer[1024] = {};
28+
unsigned char event_queue_buffer[EVENT_QUEUE_SIZE] = {};
4629
unsigned char event_loop_thread_stack[EVENT_LOOP_THREAD_STACK_SIZE] = {};
4730
unsigned char inputs_thread_stack[INPUTS_THREAD_STACK_SIZE] = {};
4831

49-
events::EventQueue equeue(1024, equeue_buffer);
50-
rtos::Thread event_loop_thread(
51-
osPriorityNormal,
32+
events::EventQueue equeue(EVENT_QUEUE_SIZE, event_queue_buffer);
33+
34+
rtos::Thread event_loop_thread(
35+
osPriorityBelowNormal,
5236
EVENT_LOOP_THREAD_STACK_SIZE,
5337
event_loop_thread_stack,
5438
"event_loop"
@@ -57,8 +41,9 @@ int main() {
5741
osPriorityBelowNormal3, INPUTS_THREAD_STACK_SIZE, inputs_thread_stack, "inputs"
5842
);
5943
// TODO: handle osStatus
60-
event_loop_thread.start(mbed::callback(&equeue, &events::EventQueue::dispatch_forever)
61-
);
44+
event_loop_thread.start([&equeue]() {
45+
equeue.dispatch_forever();
46+
});
6247
osStatus inputs_thread_status = inputs_thread.start([&inputs]() {
6348
while (true) {
6449
inputs.read();
@@ -72,17 +57,23 @@ int main() {
7257
// 得られる値を見て実行状況を判断すること
7358
inputs.read();
7459
}
75-
auto initialize_event = equeue.event(&output, &OutputMachine::initialize);
76-
auto suspend_event = equeue.event(&output, &OutputMachine::suspend);
77-
WatchDog watchdog(&received_input, &suspend_event);
78-
equeue.call_every(1s, &watchdog, &WatchDog::check);
60+
auto initialize_event
61+
= equeue.make_user_allocated_event(&output, &OutputMachine::initialize);
62+
auto suspend_event
63+
= equeue.make_user_allocated_event(&output, &OutputMachine::suspend);
64+
equeue.call_every(1s, [&suspend_event, &received_input]() {
65+
const bool received = received_input.load(std::memory_order_acquire);
66+
if (!received) {
67+
suspend_event.call();
68+
}
69+
});
7970
while (true) {
8071
DeferedDelay _delay(10ms);
8172
pc.sync();
8273
uint8_t header = 0;
8374
ssize_t read = pc.read(&header, 1);
8475
if (read < 1) {
85-
initialize_event.post();
76+
initialize_event.call();
8677
continue;
8778
}
8879
received_input.store(true, std::memory_order_release);
@@ -125,11 +116,11 @@ int main() {
125116
if (output.state() == State::INITIALIZING) {
126117
continue;
127118
}
128-
initialize_event.post();
119+
initialize_event.call();
129120
} break;
130121
case 0xFF:
131122
// suspend
132-
suspend_event.post();
123+
suspend_event.call();
133124
break;
134125
default:
135126
// unexpected

0 commit comments

Comments
 (0)