Skip to content

Commit c1f786d

Browse files
committed
state machine to enable threads
1 parent a04c94d commit c1f786d

File tree

5 files changed

+62
-60
lines changed

5 files changed

+62
-60
lines changed

examples/ScienceJournal/ScienceJournal.ino

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ void setup() {
7575

7676
BLE.addService(service);
7777
BLE.advertise();
78-
science_kit.startAuxiliaryThreads(); // start the BME688 thread
78+
science_kit.startAuxiliaryThreads(); // start the BME688 and External Temperature Probe threads
7979
//_t.start(loop_data);
8080
_tu.start(update);
8181
}
@@ -182,18 +182,18 @@ void updateSubscribedCharacteristics() {
182182
}
183183

184184
if(sndIntensityCharacteristic.subscribed()) {
185-
sndIntensityCharacteristic.writeValue(0);
185+
sndIntensityCharacteristic.writeValue(science_kit.getFrequency1());
186186
}
187187

188188
if(sndPitchCharacteristic.subscribed()) {
189-
sndPitchCharacteristic.writeValue(0);
189+
sndPitchCharacteristic.writeValue(science_kit.getExternalTemperature());
190190
}
191191

192192
if (inputACharacteristic.subscribed()){
193193
inputACharacteristic.writeValue(science_kit.getInputA());
194194
}
195195

196196
if (inputBCharacteristic.subscribed()){
197-
inputBCharacteristic.writeValue(science_kit.getExternalTemperature());
197+
inputBCharacteristic.writeValue(science_kit.getInputB());
198198
}
199199
}

src/Arduino_ScienceKitCarrier.cpp

Lines changed: 46 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ ScienceKitCarrier::ScienceKitCarrier(){
8383
thread_update_bme = new rtos::Thread();
8484
thread_external_temperature = new rtos::Thread();
8585

86+
thread_bme_is_running = false;
87+
thread_ext_temperature_is_running = false;
88+
8689
activity_led_state = ACTIVITY_LED_OFF;
8790
}
8891

@@ -94,7 +97,7 @@ ScienceKitCarrier::ScienceKitCarrier(){
9497
/* Begin */
9598
/********************************************************************/
9699

97-
int ScienceKitCarrier::begin(const bool auxiliary_threads){
100+
int ScienceKitCarrier::begin(const uint8_t auxiliary_threads){
98101
pinMode(LEDR,OUTPUT);
99102
digitalWrite(LEDR,LOW);
100103
pinMode(LEDG,OUTPUT);
@@ -139,9 +142,7 @@ int ScienceKitCarrier::begin(const bool auxiliary_threads){
139142

140143

141144
// let's start bme688 and external ds18b20 probe
142-
if (auxiliary_threads){
143-
startAuxiliaryThreads();
144-
}
145+
startAuxiliaryThreads(auxiliary_threads);
145146
}
146147

147148

@@ -167,21 +168,26 @@ void ScienceKitCarrier::update(const bool roundrobin){
167168
else{
168169
switch (round_robin_index){
169170
case 0:
170-
//updateAnalogInput(); // it is very fast (about 1ms)
171-
updateFrequencyGeneratorData(); // less than 1ms
171+
if (thread_ext_temperature_is_running){
172+
updateAnalogInput(UPDATE_INPUT_B); // it is very fast (about 1ms)
173+
}
174+
else{
175+
updateAnalogInput(); // updated both A and B channel
176+
}
177+
updateFrequencyGeneratorData(); // less than 1ms
172178
break;
173179
case 1:
174-
updateAPDS(); // about 5ms
180+
updateAPDS(); // about 5ms
175181
break;
176182
case 2:
177-
updateINA(); // about 3ms
183+
updateINA(); // about 3ms
178184
break;
179185
case 3:
180-
updateResistance(); // about 1ms
181-
updateUltrasonic(); // requires about 5ms when not connected
186+
updateResistance(); // about 1ms
187+
updateUltrasonic(); // requires about 5ms when not connected
182188
break;
183189
case 4:
184-
updateIMU(); // heavy task, 13ms
190+
updateIMU(); // heavy task, 13ms
185191
break;
186192
default:
187193
break;
@@ -201,21 +207,23 @@ void ScienceKitCarrier::update(const bool roundrobin){
201207
/********************************************************************/
202208

203209
int ScienceKitCarrier::beginAnalogInput(){
204-
/*
205210
pinMode(inputA_pin, INPUT);
206211
pinMode(inputB_pin, INPUT);
207-
*/
208212
return 0;
209213
}
210214

211-
void ScienceKitCarrier::updateAnalogInput(){
212-
if (!getExternalTemperatureIsConnected()){
213-
inputA=analogRead(inputA_pin);
215+
void ScienceKitCarrier::updateAnalogInput(const uint8_t input_to_update){
216+
if ((input_to_update==UPDATE_INPUT_A)||(input_to_update==UPDATE_ALL)){
217+
if (!getExternalTemperatureIsConnected()){
218+
inputA=analogRead(inputA_pin);
219+
}
220+
else{
221+
inputA=ANALOGIN_DISABLED;
222+
}
214223
}
215-
else{
216-
inputA=ANALOGIN_DISABLED;
224+
if ((input_to_update==UPDATE_INPUT_B)||(input_to_update==UPDATE_ALL)){
225+
inputB=analogRead(inputB_pin);
217226
}
218-
inputB=analogRead(inputB_pin);
219227
}
220228

221229
int ScienceKitCarrier::getInputA(){
@@ -718,7 +726,7 @@ bool ScienceKitCarrier::getExternalTemperatureIsConnected(){
718726
void ScienceKitCarrier::threadExternalTemperature(){
719727
beginExternalTemperature();
720728
while(1){
721-
updateAnalogInput();
729+
updateAnalogInput(UPDATE_INPUT_A);
722730
digitalWrite(2,HIGH);
723731
updateExternalTemperature();
724732
digitalWrite(2,LOW);
@@ -734,8 +742,22 @@ void ScienceKitCarrier::threadExternalTemperature(){
734742
/* Threads */
735743
/********************************************************************/
736744

737-
void ScienceKitCarrier::startAuxiliaryThreads(){
738-
//thread_activity_led->start(mbed::callback(this, &ScienceKitCarrier::threadActivityLed));
739-
thread_update_bme->start(mbed::callback(this, &ScienceKitCarrier::threadBME688));
740-
thread_external_temperature->start(mbed::callback(this, &ScienceKitCarrier::threadExternalTemperature));
745+
void ScienceKitCarrier::startAuxiliaryThreads(const uint8_t auxiliary_threads){
746+
//thread_activity_led->start(mbed::callback(this, &ScienceKitCarrier::threadActivityLed)); //left for legacy on prototypes and maybe future implementations
747+
748+
// start bme688 thread
749+
if ((auxiliary_threads==START_AUXILIARY_THREADS)||(auxiliary_threads==START_INTERNAL_AMBIENT_SENSOR)){
750+
if (!thread_bme_is_running){
751+
thread_update_bme->start(mbed::callback(this, &ScienceKitCarrier::threadBME688));
752+
}
753+
thread_bme_is_running=true;
754+
}
755+
756+
// start ds18b20 thread
757+
if ((auxiliary_threads==START_AUXILIARY_THREADS)||(auxiliary_threads==START_EXTERNAL_AMBIENT_SENSOR)){
758+
if (!thread_ext_temperature_is_running){
759+
thread_external_temperature->start(mbed::callback(this, &ScienceKitCarrier::threadExternalTemperature));
760+
}
761+
thread_ext_temperature_is_running=true;
762+
}
741763
}

src/Arduino_ScienceKitCarrier.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,17 @@ class ScienceKitCarrier{
8686
rtos::Thread * thread_update_bme;
8787
rtos::Thread * thread_external_temperature;
8888

89+
bool thread_bme_is_running;
90+
bool thread_ext_temperature_is_running;
91+
8992
uint8_t activity_led_state;
9093

9194
public:
9295
ScienceKitCarrier();
9396

94-
int begin(const bool auxiliary_threads=true);
97+
int begin(const uint8_t auxiliary_threads=START_AUXILIARY_THREADS);
9598
void update(const bool roundrobin=false); // this makes update on: analog in, imu, apds, ina, resistance, round robin enables one sensor update
96-
void startAuxiliaryThreads();
99+
void startAuxiliaryThreads(const uint8_t auxiliary_threads=START_AUXILIARY_THREADS);
97100

98101

99102

@@ -112,7 +115,7 @@ class ScienceKitCarrier{
112115

113116
/* Analog input connected to Grove connectors A and B */
114117
int beginAnalogInput();
115-
void updateAnalogInput();
118+
void updateAnalogInput(const uint8_t input_to_update=UPDATE_ALL);
116119
int getInputA(); // 0-1024
117120
int getInputB(); // 0-1024
118121

src/utils/Arduino_ScienceKitCarrier_ble_config.h

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/utils/Arduino_ScienceKitCarrier_definitions.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
#define INPUTA_PIN A0
2525
#define INPUTB_PIN A1
2626
#define ANALOGIN_DISABLED 0
27+
#define UPDATE_ALL 0
28+
#define UPDATE_INPUT_A 1
29+
#define UPDATE_INPUT_B 2
2730

2831
// APDS9960
2932
#define INT_APDS9960 9
@@ -72,8 +75,10 @@ const uint16_t MAXIMUM_AMPS{1}; // 1A
7275
#define ROUND_ROBIN_ENABLED 1
7376
#define ROUND_ROBIN_DISABLED 0
7477

75-
#define START_AUXILIARY_THREADS 1
7678
#define NO_AUXILIARY_THREADS 0
79+
#define START_AUXILIARY_THREADS 1
80+
#define START_INTERNAL_AMBIENT_SENSOR 2 // bme688
81+
#define START_EXTERNAL_AMBIENT_SENSOR 3 // ds18b20
7782

7883

7984

0 commit comments

Comments
 (0)