-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathArduino_Threads.h
118 lines (90 loc) · 3.41 KB
/
Arduino_Threads.h
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
/*
* 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
*/
#ifndef ARDUINO_THREADS_H_
#define ARDUINO_THREADS_H_
/**************************************************************************************
* INCLUDE
**************************************************************************************/
#include <mbed.h>
#include <SharedPtr.h>
#include "threading/Sink.hpp"
#include "threading/Source.hpp"
#include "threading/Shared.hpp"
#include "io/BusDevice.h"
#include "io/spi/SpiBusDevice.h"
#include "io/wire/WireBusDevice.h"
#include "io/serial/SerialDispatcher.h"
/**************************************************************************************
* DEFINE
**************************************************************************************/
#define SOURCE(name, type) \
public: \
Source<type> name; \
private:
#define SINK(name, type) \
public: \
SinkBlocking<type> name; \
private:
// we need to call the Sink<T>(int size) non-default constructor using size as parameter.
// This is done by writing
// Sink<type> name{size};
// instead of:
// Sink<type> name(size);
// otherwise the compiler will read it as a declaration of a method called "name" and we
// get a syntax error.
// This is called "C++11 uniform init" (using "{}" instead of "()" without "="... yikes!)
// https://chromium.googlesource.com/chromium/src/+/master/styleguide/c++/c++-dos-and-donts.md
#define SHARED(name, type) \
Shared<type> name;
/**************************************************************************************
* CLASS DECLARATION
**************************************************************************************/
#define CONCAT2(x,y) x##y
#define CONCAT(x,y) CONCAT2(x,y)
#define INCF(F) INCF_(F)
#define INCF_(F) #F
#define _macroToString(sequence) #sequence
class Arduino_Threads
{
public:
Arduino_Threads();
virtual ~Arduino_Threads();
void start (int const stack_size = 4096, uint32_t const start_flags = 0, uint32_t const stop_flags = 0);
void terminate ();
void setLoopDelay(uint32_t const delay);
void sendEvent (uint32_t const event);
static void broadcastEvent(uint32_t event);
protected:
char * _tabname;
virtual void setup() = 0;
virtual void loop () = 0;
private:
static rtos::EventFlags _global_events;
mbed::SharedPtr<rtos::Thread> _thread;
uint32_t _start_flags, _stop_flags;
uint32_t _loop_delay_ms;
void threadFunc();
};
#define THD_ENTER(tabname) class CONCAT(tabname, Class) : public Arduino_Threads { \
public: \
CONCAT(tabname, Class)() { _tabname = _macroToString(tabname); } \
private: \
#define THD_DONE(tabname) \
}; \
CONCAT(tabname,Class) tabname;
#endif /* ARDUINO_THREADS_H_ */