@@ -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
203209int 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
221229int ScienceKitCarrier::getInputA (){
@@ -718,7 +726,7 @@ bool ScienceKitCarrier::getExternalTemperatureIsConnected(){
718726void 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}
0 commit comments