Control Surface
main
MIDI Control Surface library for Arduino
Toggle main menu visibility
Loading...
Searching...
No Matches
src
Submodules
Encoder
AtomicPosition.hpp
Go to the documentation of this file.
1
#pragma once
2
3
#include <
AH/Arduino-Wrapper.h
>
4
#include <
Settings/NamespaceSettings.hpp
>
5
6
#ifdef __AVR__
7
#include <util/atomic.h>
8
#else
9
#include <atomic>
10
#endif
11
#ifdef ARDUINO_ARCH_MBED
12
#include <CriticalSectionLock.h>
13
#endif
14
15
BEGIN_CS_NAMESPACE
16
17
#if defined(ATOMIC_INT_LOCK_FREE) && ATOMIC_INT_LOCK_FREE == 2
18
template
<
class
T>
19
struct
AtomicPosition
{
20
using
type
= T;
21
std::atomic<type>
value
{};
22
23
AtomicPosition
(T t) :
value
{t} {}
24
AtomicPosition
(
const
AtomicPosition
&o) :
AtomicPosition
{o.
get
()} {}
25
AtomicPosition
(
AtomicPosition
&&o) :
AtomicPosition
{o.
get
()} {}
26
AtomicPosition
&
operator=
(
const
AtomicPosition
&o) {
27
this->
set
(o.
get
());
28
return
*
this
;
29
}
30
AtomicPosition
&
operator=
(
AtomicPosition
&&o) {
31
this->
set
(o.get());
32
return
*
this
;
33
}
34
35
constexpr
static
std::memory_order mo_rlx = std::memory_order_relaxed;
36
void
add
(
type
other) {
value
.fetch_add(other, mo_rlx); }
37
type
get
()
const
{
return
value
.load(mo_rlx); }
38
void
set
(
type
other) {
value
.store(other, mo_rlx); }
39
type
xchg
(
type
other) {
return
value
.exchange(other, mo_rlx); }
40
void
add_isr
(
type
other) {
add
(other); }
41
};
42
#elif defined(__AVR__)
43
template
<
class
T>
44
struct
AtomicPosition
{
45
using
type
= T;
46
volatile
type
value
;
47
48
AtomicPosition
(T t) :
value
{t} {}
49
AtomicPosition
(
const
AtomicPosition
&o) :
AtomicPosition
{o.
get
()} {}
50
AtomicPosition
(
AtomicPosition
&&o) :
AtomicPosition
{o.
get
()} {}
51
AtomicPosition
&
operator=
(
const
AtomicPosition
&o) {
52
this->
set
(o.
get
());
53
return
*
this
;
54
}
55
AtomicPosition
&
operator=
(
AtomicPosition
&&o) {
56
this->
set
(o.get());
57
return
*
this
;
58
}
59
60
void
add
(
type
other) {
61
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
value
+= other; }
62
}
63
type
get
()
const
{
64
type
copy;
65
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { copy =
value
; }
66
return
copy;
67
}
68
void
set
(
type
other) {
69
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
value
= other; }
70
}
71
type
xchg
(
type
other) {
72
type
copy;
73
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
74
copy =
value
;
75
value
= other;
76
}
77
return
copy;
78
}
79
void
add_isr
(
type
other) {
value
+= other; }
80
};
81
#elif defined(ARDUINO_ARCH_MBED)
82
template
<
class
T>
83
struct
AtomicPosition
{
84
using
type
= T;
85
volatile
type
value
;
86
87
AtomicPosition
(T t) :
value
{t} {}
88
AtomicPosition
(
const
AtomicPosition
&o) :
AtomicPosition
{o.
get
()} {}
89
AtomicPosition
(
AtomicPosition
&&o) :
AtomicPosition
{o.
get
()} {}
90
AtomicPosition
&
operator=
(
const
AtomicPosition
&o) {
91
this->
set
(o.
get
());
92
return
*
this
;
93
}
94
AtomicPosition
&
operator=
(
AtomicPosition
&&o) {
95
this->
set
(o.get());
96
return
*
this
;
97
}
98
99
void
add
(
type
other) {
100
mbed::CriticalSectionLock lck;
101
value
+= other;
102
}
103
type
get
()
const
{
104
mbed::CriticalSectionLock lck;
105
return
value
;
106
}
107
void
set
(
type
other) {
108
mbed::CriticalSectionLock lck;
109
value
= other;
110
}
111
type
xchg
(
type
other) {
112
mbed::CriticalSectionLock lck;
113
type
copy =
value
;
114
value
= other;
115
return
copy;
116
}
117
void
add_isr
(
type
other) {
value
+= other; }
118
};
119
#else
120
template
<
class
T>
121
struct
AtomicPosition
{
122
using
type
= T;
123
volatile
type
value
;
124
125
AtomicPosition
(T t) :
value
{t} {}
126
AtomicPosition
(
const
AtomicPosition
&o) :
AtomicPosition
{o.
get
()} {}
127
AtomicPosition
(
AtomicPosition
&&o) :
AtomicPosition
{o.
get
()} {}
128
AtomicPosition
&
operator=
(
const
AtomicPosition
&o) {
129
this->
set
(o.
get
());
130
return
*
this
;
131
}
132
AtomicPosition
&
operator=
(
AtomicPosition
&&o) {
133
this->
set
(o.get());
134
return
*
this
;
135
}
136
137
void
add
(
type
other) {
138
noInterrupts();
139
value
+= other;
140
interrupts();
141
}
142
type
get
()
const
{
143
type
copy;
144
noInterrupts();
145
copy =
value
;
146
interrupts();
147
return
copy;
148
}
149
void
set
(
type
other) {
150
noInterrupts();
151
value
= other;
152
interrupts();
153
}
154
type
xchg
(
type
other) {
155
type
copy;
156
noInterrupts();
157
copy =
value
;
158
value
= other;
159
interrupts();
160
return
copy;
161
}
162
void
add_isr
(
type
other) {
value
+= other; }
163
};
164
#endif
165
166
END_CS_NAMESPACE
Arduino-Wrapper.h
NamespaceSettings.hpp
END_CS_NAMESPACE
#define END_CS_NAMESPACE
Definition
Settings/NamespaceSettings.hpp:14
BEGIN_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
Definition
Settings/NamespaceSettings.hpp:11
AtomicPosition
Definition
AtomicPosition.hpp:121
AtomicPosition::operator=
AtomicPosition & operator=(const AtomicPosition &o)
Definition
AtomicPosition.hpp:128
AtomicPosition::value
volatile type value
Definition
AtomicPosition.hpp:123
AtomicPosition::AtomicPosition
AtomicPosition(T t)
Definition
AtomicPosition.hpp:125
AtomicPosition::xchg
type xchg(type other)
Definition
AtomicPosition.hpp:154
AtomicPosition::set
void set(type other)
Definition
AtomicPosition.hpp:149
AtomicPosition::add_isr
void add_isr(type other)
Definition
AtomicPosition.hpp:162
AtomicPosition::type
T type
Definition
AtomicPosition.hpp:122
AtomicPosition::add
void add(type other)
Definition
AtomicPosition.hpp:137
AtomicPosition::AtomicPosition
AtomicPosition(AtomicPosition &&o)
Definition
AtomicPosition.hpp:127
AtomicPosition::AtomicPosition
AtomicPosition(const AtomicPosition &o)
Definition
AtomicPosition.hpp:126
AtomicPosition::operator=
AtomicPosition & operator=(AtomicPosition &&o)
Definition
AtomicPosition.hpp:132
AtomicPosition::get
type get() const
Definition
AtomicPosition.hpp:142
Generated by
1.17.0