Skip to content

Commit 039cd92

Browse files
author
mkuettner97
committed
start and stop sensor logger correctly
1 parent 6a5d961 commit 039cd92

3 files changed

Lines changed: 53 additions & 29 deletions

File tree

src/SD_Card/SDLogger/SDLogger.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,4 +258,8 @@ int SDLogger::end() {
258258
return 0;
259259
}
260260

261+
bool SDLogger::is_active() {
262+
return is_open;
263+
}
264+
261265
SDLogger sdlogger;

src/SD_Card/SDLogger/SDLogger.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ class SDLogger {
9090
*/
9191
int end();
9292

93+
bool is_active();
94+
9395
SDLogger(SDLogger const&) = delete;
9496
SDLogger& operator=(SDLogger const&) = delete;
9597
};

src/SensorManager/SensorManager.cpp

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,18 @@
1717
LOG_MODULE_DECLARE(sensor_manager);
1818
#include "../SD_Card/SDLogger/SDLogger.h"
1919
#include <string>
20+
#include <set>
21+
22+
std::set<int> ble_sensors = {};
23+
std::set<int> sd_sensors = {};
2024

2125
extern struct k_msgq sensor_queue;
2226
// extern k_tid_t sensor_publish;
2327

2428
EdgeMlSensor * get_sensor(enum sensor_id id);
2529

2630
// Track number of active sensors
27-
static int active_sensor_count = 0;
31+
//static int active_sensor_count = 0;
2832

2933
static sensor_manager_state _state;
3034

@@ -90,14 +94,13 @@ void start_sensor_manager() {
9094
k_msgq_purge(&sensor_queue);
9195
k_msgq_purge(&config_queue);
9296

97+
ble_sensors.clear();
98+
sd_sensors.clear();
99+
93100
if (_state == INIT) {
94101
k_thread_start(sensor_pub_id);
95102
}
96103

97-
// Start SDLogger with timestamp-based filename
98-
std::string filename = "sensor_log_" + std::to_string(micros());
99-
sdlogger.begin(filename);
100-
101104
k_poll_signal_raise(&sensor_manager_sig, 0);
102105

103106
_state = RUNNING;
@@ -112,7 +115,7 @@ void stop_sensor_manager() {
112115
Temp::sensor.stop();
113116
BoneConduction::sensor.stop();
114117

115-
active_sensor_count = 0;
118+
active_sensors = 0;
116119

117120
//k_thread_suspend(sensor_pub_id);
118121
k_poll_signal_reset(&sensor_manager_sig);
@@ -142,49 +145,64 @@ EdgeMlSensor * get_sensor(enum sensor_id id) {
142145

143146
// Worker-Funktion für die Sensor-Konfiguration
144147
static void config_work_handler(struct k_work *work) {
148+
int ret;
145149
struct sensor_config config;
146-
147-
int ret = k_msgq_get(&config_queue, &config, K_NO_WAIT);
150+
151+
ret = k_msgq_get(&config_queue, &config, K_NO_WAIT);
148152

149153
float sampleRate = getSampleRateForSensor(config.sensorId, config.sampleRateIndex);
150154
if (sampleRate <= 0) {
151155
LOG_ERR("Invalid sample rate %f for sensor %i", sampleRate, config.sensorId);
152156
return;
153157
}
154158

155-
k_timeout_t t = K_USEC(1e6 / sampleRate);
156-
157159
EdgeMlSensor * sensor = get_sensor((enum sensor_id) config.sensorId);
158160

159-
if (config.storageOptions == 0 || !(config.storageOptions & (DATA_STREAMING | DATA_STORAGE))) {
160-
if (sensor->is_running()) {
161-
sensor->stop();
162-
active_sensors--;
163-
if (active_sensors < 0) {
164-
LOG_WRN("Active sensors is already 0");
165-
active_sensors = 0;
166-
}
167-
168-
if (active_sensor_count == 0) stop_sensor_manager();
169-
}
170-
return;
171-
}
172-
173161
if (sensor->is_running()) {
174162
sensor->stop();
175163
active_sensors--;
164+
165+
if (active_sensors < 0) {
166+
LOG_WRN("Active sensors is already 0");
167+
active_sensors = 0;
168+
}
176169
}
177170

178171
sensor->sd_logging(config.storageOptions & DATA_STORAGE);
179172
sensor->ble_stream(config.storageOptions & DATA_STREAMING);
180173

181-
if (sensor->init(&sensor_queue)) {
182-
if (active_sensors == 0) start_sensor_manager();
183-
sensor->start(config.sampleRateIndex);
184-
if (sensor->is_running()) {
185-
active_sensors++;
174+
if (config.storageOptions & (DATA_STORAGE | DATA_STREAMING)) {
175+
if (sensor->init(&sensor_queue)) {
176+
if (active_sensors == 0) start_sensor_manager();
177+
sensor->start(config.sampleRateIndex);
178+
if (sensor->is_running()) {
179+
active_sensors++;
180+
}
181+
}
182+
}
183+
184+
if (config.storageOptions & DATA_STORAGE) {
185+
sd_sensors.insert(config.sensorId);
186+
187+
if (!sdlogger.is_active()) {
188+
// Start SDLogger with timestamp-based filename
189+
std::string filename = "sensor_log_" + std::to_string(micros());
190+
sdlogger.begin(filename);
186191
}
192+
} else if (sd_sensors.find(config.sensorId) != sd_sensors.end()) {
193+
sd_sensors.erase(config.sensorId);
194+
195+
if (sd_sensors.empty()) sdlogger.end();
196+
}
197+
198+
if (config.storageOptions & DATA_STREAMING) ble_sensors.insert(config.sensorId);
199+
else if (ble_sensors.find(config.sensorId) != ble_sensors.end()) {
200+
ble_sensors.erase(config.sensorId);
201+
202+
// TODO: if (ble_sensors.empty()) ...
187203
}
204+
205+
if (active_sensors == 0) stop_sensor_manager();
188206
}
189207

190208
void config_sensor(struct sensor_config * config) {

0 commit comments

Comments
 (0)