-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathArduino_Threads.cpp
125 lines (106 loc) · 3.79 KB
/
Arduino_Threads.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
* This file is part of the Arduino_ThreadsafeIO library.
* Copyright (c) 2021 Arduino SA.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**************************************************************************************
* INCLUDE
**************************************************************************************/
#include "Arduino_Threads.h"
/**************************************************************************************
* STATIC MEMBER DECLARATION
**************************************************************************************/
rtos::EventFlags Arduino_Threads::_global_events;
/**************************************************************************************
* CTOR/DTOR
**************************************************************************************/
Arduino_Threads::Arduino_Threads()
: _start_flags{0}
, _stop_flags{0}
, _loop_delay_ms{0}
{
}
Arduino_Threads::~Arduino_Threads()
{
terminate();
}
/**************************************************************************************
* PUBLIC MEMBER FUNCTIONS
**************************************************************************************/
void Arduino_Threads::start(int const stack_size, uint32_t const start_flags, uint32_t const stop_flags)
{
_start_flags = start_flags;
_stop_flags = stop_flags;
_thread.reset(new rtos::Thread(osPriorityNormal, stack_size, nullptr, _tabname));
_thread->start(mbed::callback(this, &Arduino_Threads::threadFunc));
}
void Arduino_Threads::terminate()
{
_thread->terminate();
_thread->join();
}
void Arduino_Threads::sendEvent(uint32_t const event)
{
_thread->flags_set(event);
}
void Arduino_Threads::setLoopDelay(uint32_t const delay)
{
_loop_delay_ms = delay;
}
void Arduino_Threads::broadcastEvent(uint32_t const event)
{
_global_events.set(event);
}
/**************************************************************************************
* PRIVATE MEMBER FUNCTIONS
**************************************************************************************/
void Arduino_Threads::threadFunc()
{
setup();
/* If _start_flags have been passed then wait until all the flags are set
* before starting the loop. this is used to synchronize loops from multiple
* sketches.
*/
if (_start_flags != 0)
_global_events.wait_all(_start_flags);
/* if _stop_flags have been passed stop when all the flags are set
* otherwise loop forever
*/
for (;;)
{
loop();
/* On exit clear the flags that have forced us to stop.
* note that if two groups of sketches stop on common flags
* the first group will clear them so the second group may never
* exit.
*/
if (_stop_flags!=0)
{
if ((_global_events.get() & _stop_flags) != _stop_flags)
{
_global_events.clear(_stop_flags);
return;
}
if ((rtos::ThisThread::flags_get() & _stop_flags) != _stop_flags)
{
rtos::ThisThread::flags_clear(_stop_flags);
return;
}
}
/* Sleep for the time we've been asked to insert between loops.
*/
rtos::ThisThread::sleep_for(rtos::Kernel::Clock::duration_u32(_loop_delay_ms));
}
}