2
2
3
3
#include < algorithm>
4
4
#include < atomic>
5
+ #include < mutex>
5
6
#include < optional>
6
7
#include < unordered_map>
7
8
@@ -76,6 +77,7 @@ class OneshotAdc : public BaseComponent {
76
77
std::vector<int > read_all_mv () {
77
78
std::vector<int > values;
78
79
values.reserve (configs_.size ());
80
+ std::lock_guard<std::recursive_mutex> lock (mutex_);
79
81
for (const auto &config : configs_) {
80
82
int raw = 0 ;
81
83
auto err = adc_oneshot_read (adc_handle_, config.channel , &raw);
@@ -96,6 +98,7 @@ class OneshotAdc : public BaseComponent {
96
98
* it was configured).
97
99
*/
98
100
std::optional<int > read_raw (const AdcConfig &config) {
101
+ std::lock_guard<std::recursive_mutex> lock (mutex_);
99
102
if (std::find (configs_.begin (), configs_.end (), config) != configs_.end ()) {
100
103
int raw;
101
104
auto err = adc_oneshot_read (adc_handle_, config.channel , &raw);
@@ -120,6 +123,7 @@ class OneshotAdc : public BaseComponent {
120
123
* (if it was configured).
121
124
*/
122
125
std::optional<int > read_mv (const AdcConfig &config) {
126
+ std::lock_guard<std::recursive_mutex> lock (mutex_);
123
127
auto maybe_raw = read_raw (config);
124
128
if (maybe_raw.has_value ()) {
125
129
return raw_to_mv (maybe_raw.value ());
@@ -205,6 +209,7 @@ class OneshotAdc : public BaseComponent {
205
209
}
206
210
}
207
211
212
+ std::recursive_mutex mutex_;
208
213
std::vector<AdcConfig> configs_;
209
214
adc_oneshot_unit_handle_t adc_handle_;
210
215
adc_cali_handle_t adc_cali_handle_;
0 commit comments