4
4
#include < mutex>
5
5
6
6
#include " BufferedSerial.h"
7
- #include " events/Event.h"
8
7
#include " events/EventQueue.h"
8
+ #include " events/UserAllocatedEvent.h"
9
9
#include " ThisThread.h"
10
10
#include " Thread.h"
11
11
15
15
16
16
using namespace std ::chrono_literals;
17
17
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
-
36
18
int main () {
19
+ constexpr size_t EVENT_QUEUE_SIZE = 32 * EVENTS_EVENT_SIZE;
37
20
constexpr size_t EVENT_LOOP_THREAD_STACK_SIZE = 1024 ;
38
21
constexpr size_t INPUTS_THREAD_STACK_SIZE = 1024 ;
39
22
@@ -42,13 +25,14 @@ int main() {
42
25
mbed::BufferedSerial pc (USBTX, USBRX);
43
26
std::atomic_bool received_input;
44
27
45
- unsigned char equeue_buffer[ 1024 ] = {};
28
+ unsigned char event_queue_buffer[EVENT_QUEUE_SIZE ] = {};
46
29
unsigned char event_loop_thread_stack[EVENT_LOOP_THREAD_STACK_SIZE] = {};
47
30
unsigned char inputs_thread_stack[INPUTS_THREAD_STACK_SIZE] = {};
48
31
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,
52
36
EVENT_LOOP_THREAD_STACK_SIZE,
53
37
event_loop_thread_stack,
54
38
" event_loop"
@@ -57,8 +41,9 @@ int main() {
57
41
osPriorityBelowNormal3, INPUTS_THREAD_STACK_SIZE, inputs_thread_stack, " inputs"
58
42
);
59
43
// 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
+ });
62
47
osStatus inputs_thread_status = inputs_thread.start ([&inputs]() {
63
48
while (true ) {
64
49
inputs.read ();
@@ -72,17 +57,23 @@ int main() {
72
57
// 得られる値を見て実行状況を判断すること
73
58
inputs.read ();
74
59
}
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
+ });
79
70
while (true ) {
80
71
DeferedDelay _delay (10ms);
81
72
pc.sync ();
82
73
uint8_t header = 0 ;
83
74
ssize_t read = pc.read (&header, 1 );
84
75
if (read < 1 ) {
85
- initialize_event.post ();
76
+ initialize_event.call ();
86
77
continue ;
87
78
}
88
79
received_input.store (true , std::memory_order_release);
@@ -125,11 +116,11 @@ int main() {
125
116
if (output.state () == State::INITIALIZING) {
126
117
continue ;
127
118
}
128
- initialize_event.post ();
119
+ initialize_event.call ();
129
120
} break ;
130
121
case 0xFF :
131
122
// suspend
132
- suspend_event.post ();
123
+ suspend_event.call ();
133
124
break ;
134
125
default :
135
126
// unexpected
0 commit comments