Skip to content

Commit 1797e49

Browse files
committed
mod: external temperature sensor and threads on ESP32
1 parent 2bd4b44 commit 1797e49

File tree

3 files changed

+68
-26
lines changed

3 files changed

+68
-26
lines changed

src/Arduino_ScienceKitCarrier.cpp

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ int ScienceKitCarrier::begin(const uint8_t auxiliary_threads){
172172
*/
173173

174174
// let's start bme688 and external ds18b20 probe
175-
//WIP startAuxiliaryThreads(auxiliary_threads);
175+
startAuxiliaryThreads(auxiliary_threads);
176176
}
177177

178178

@@ -248,14 +248,17 @@ int ScienceKitCarrier::beginAnalogInput(){
248248

249249
void ScienceKitCarrier::updateAnalogInput(const uint8_t input_to_update){
250250
if ((input_to_update==UPDATE_INPUT_A)||(input_to_update==UPDATE_ALL)){
251-
/* WIP
251+
252252
if (!getExternalTemperatureIsConnected()){
253253
inputA=analogRead(inputA_pin);
254+
#ifdef ESP32
255+
beginExternalTemperature();
256+
#endif
254257
}
255258
else{
256259
inputA=ANALOGIN_DISABLED;
257260
}
258-
*/
261+
259262
}
260263
if ((input_to_update==UPDATE_INPUT_B)||(input_to_update==UPDATE_ALL)){
261264
inputB=analogRead(inputB_pin);
@@ -472,14 +475,23 @@ float ScienceKitCarrier::getAirQuality(){
472475
return airquality;
473476
}
474477

475-
#ifdef ARDUINO_NANO_RP2040_CONNECT
476478
void ScienceKitCarrier::threadBME688(){
477479
beginBME688();
478480
while(1){
479481
updateBME688();
482+
#ifdef ARDUINO_NANO_RP2040_CONNECT
480483
rtos::ThisThread::sleep_for(1000);
484+
#endif
485+
#ifdef ESP32
486+
delay(1000);
487+
#endif
481488
}
482489
}
490+
491+
#ifdef ESP32
492+
void ScienceKitCarrier::freeRTOSInternalTemperature(void * pvParameters){
493+
((ScienceKitCarrier*) pvParameters)->threadBME688();
494+
}
483495
#endif
484496

485497

@@ -580,12 +592,11 @@ float ScienceKitCarrier::getMagneticFieldZ(){
580592

581593
#ifdef ARDUINO_NANO_RP2040_CONNECT
582594
void ScienceKitCarrier::delay(unsigned long t){
583-
rtos::ThisThread::sleep_for(t);
595+
rtos::ThisThread::sleep_for(t);
584596
}
585597
#endif
586598

587599

588-
589600
/********************************************************************/
590601
/* LEDs: errors and activity */
591602
/********************************************************************/
@@ -745,7 +756,6 @@ bool ScienceKitCarrier::getUltrasonicIsConnected(){
745756
/********************************************************************/
746757
//WIP
747758

748-
#ifdef ARDUINO_NANO_RP2040_CONNECT
749759
int ScienceKitCarrier::beginExternalTemperature(){
750760
new (&ow) OneWireNg_CurrentPlatform(OW_PIN, false);
751761
DSTherm drv(ow);
@@ -754,8 +764,14 @@ int ScienceKitCarrier::beginExternalTemperature(){
754764

755765
void ScienceKitCarrier::updateExternalTemperature(){
756766
float temperature;
757-
pinMode(OW_PIN,INPUT);
758767

768+
#ifdef ARDUINO_NANO_RP2040_CONNECT
769+
pinMode(OW_PIN,INPUT);
770+
#endif
771+
#ifdef ESP32
772+
pinMode(INPUTA_PIN,INPUT);
773+
#endif
774+
759775
DSTherm drv(ow);
760776
drv.convertTempAll(DSTherm::MAX_CONV_TIME, false);
761777

@@ -795,11 +811,19 @@ void ScienceKitCarrier::threadExternalTemperature(){
795811
while(1){
796812
updateExternalTemperature();
797813
updateAnalogInput(UPDATE_INPUT_A);
798-
rtos::ThisThread::sleep_for(1000);
814+
815+
#ifdef ARDUINO_NANO_RP2040_CONNECT
816+
rtos::ThisThread::sleep_for(1000);
817+
#endif
818+
#ifdef ESP32
819+
delay(1000);
820+
#endif
799821
}
800822
}
801-
#endif
802823

824+
void ScienceKitCarrier::freeRTOSExternalTemperature(void * pvParameters){
825+
((ScienceKitCarrier*) pvParameters)->threadExternalTemperature();
826+
}
803827

804828

805829
/********************************************************************/
@@ -845,30 +869,35 @@ uint ScienceKitCarrier::getMicrophoneRMS(){
845869
/********************************************************************/
846870
/* Threads */
847871
/********************************************************************/
848-
#ifdef ARDUINO_NANO_RP2040_CONNECT
849872

850873
void ScienceKitCarrier::startAuxiliaryThreads(const uint8_t auxiliary_threads){
851874
//thread_activity_led->start(mbed::callback(this, &ScienceKitCarrier::threadActivityLed)); //left for legacy on prototypes and maybe future implementations
852-
853875
// start bme688 thread
854876
if ((auxiliary_threads==START_AUXILIARY_THREADS)||(auxiliary_threads==START_INTERNAL_AMBIENT_SENSOR)){
855877
if (!thread_bme_is_running){
856-
thread_update_bme->start(mbed::callback(this, &ScienceKitCarrier::threadBME688));
878+
#ifdef ARDUINO_NANO_RP2040_CONNECT
879+
thread_update_bme->start(mbed::callback(this, &ScienceKitCarrier::threadBME688));
880+
#endif
881+
#ifdef ESP32
882+
xTaskCreatePinnedToCore(this->freeRTOSInternalTemperature, "update_internal_temperature", 10000, this, 1, &thread_internal_temperature, INTERNAL_TEMPERATURE_CORE);
883+
#endif
857884
}
858885
thread_bme_is_running=true;
859886
}
860-
861887
// start ds18b20 thread
862888
if ((auxiliary_threads==START_AUXILIARY_THREADS)||(auxiliary_threads==START_EXTERNAL_AMBIENT_SENSOR)){
863889
if (!thread_ext_temperature_is_running){
864-
thread_external_temperature->start(mbed::callback(this, &ScienceKitCarrier::threadExternalTemperature));
890+
#ifdef ARDUINO_NANO_RP2040_CONNECT
891+
thread_external_temperature->start(mbed::callback(this, &ScienceKitCarrier::threadExternalTemperature));
892+
#endif
893+
#ifdef ESP32
894+
xTaskCreatePinnedToCore(this->freeRTOSExternalTemperature, "update_external_temperature", 10000, this, 1, &thread_external_temperature, EXTERNAL_TEMPERATURE_CORE);
895+
#endif
865896
}
866897
thread_ext_temperature_is_running=true;
867898
}
868899
}
869900

870-
#endif
871-
872901

873902
/***
874903
* _ _

src/Arduino_ScienceKitCarrier.h

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,18 @@
4141
#ifdef ARDUINO_NANO_RP2040_CONNECT
4242
#include "../../OneWireNg/src/platform/OneWireNg_PicoRP2040.h" // forces to use gpio instead PIO hw
4343
#define OneWireNg_CurrentPlatform OneWireNg_PicoRP2040
44+
#endif
45+
#ifdef ESP32
46+
#include "OneWireNg_CurrentPlatform.h"
47+
#endif
4448
#include "drivers/DSTherm.h"
4549
#include "utils/Placeholder.h"
46-
#endif
4750

4851

4952
#include "./utils/function_generator_controller.h"
5053
#include "./utils/Arduino_ScienceKitCarrier_definitions.h"
5154

52-
#ifdef ARDUINO_NANO_RP2040_CONNECT
5355
static Placeholder<OneWireNg_CurrentPlatform> ow;
54-
#endif
5556

5657

5758
class ScienceKitCarrier{
@@ -101,6 +102,11 @@ class ScienceKitCarrier{
101102
rtos::Thread * thread_external_temperature;
102103
#endif
103104

105+
#ifdef ESP32
106+
TaskHandle_t thread_internal_temperature;
107+
TaskHandle_t thread_external_temperature;
108+
#endif
109+
104110
bool thread_bme_is_running;
105111
bool thread_ext_temperature_is_running;
106112

@@ -111,16 +117,13 @@ class ScienceKitCarrier{
111117

112118
int begin(const uint8_t auxiliary_threads=START_AUXILIARY_THREADS);
113119
void update(const bool roundrobin=false); // this makes update on: analog in, imu, apds, ina, resistance, round robin enables one sensor update
114-
#ifdef ARDUINO_NANO_RP2040_CONNECT
120+
115121
void startAuxiliaryThreads(const uint8_t auxiliary_threads=START_AUXILIARY_THREADS);
116-
#endif
117-
118122

119123
#ifdef ARDUINO_NANO_RP2040_CONNECT
120124
void delay(unsigned long t); // you must use this instead delay, due threads usage
121125
#endif
122126

123-
124127
/* Blink red alert */
125128
void errorTrap(const int error_code=0);
126129

@@ -174,8 +177,9 @@ class ScienceKitCarrier{
174177
float getPressure(); // hPascal
175178
float getHumidity(); // Percentage
176179
float getAirQuality(); // index, if good it is 25.0
177-
#ifdef ARDUINO_NANO_RP2040_CONNECT
178180
void threadBME688(); // thread used to update BME688 automatically in multithread mode
181+
#ifdef ESP32
182+
static void freeRTOSInternalTemperature(void * pvParameters);
179183
#endif
180184

181185

@@ -222,12 +226,13 @@ class ScienceKitCarrier{
222226

223227

224228
/* External temperature probe - Dallas DS18B20 */
225-
#ifdef ARDUINO_NANO_RP2040_CONNECT
226229
int beginExternalTemperature();
227230
void updateExternalTemperature();
228231
float getExternalTemperature(); // celsius
229232
bool getExternalTemperatureIsConnected();
230233
void threadExternalTemperature();
234+
#ifdef ESP32
235+
static void freeRTOSExternalTemperature(void * pvParameters);
231236
#endif
232237

233238
#ifdef ARDUINO_NANO_RP2040_CONNECT

src/utils/Arduino_ScienceKitCarrier_definitions.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,12 @@ const uint16_t MAXIMUM_AMPS{1}; // 1A
6060
#define BME688_CS 10
6161

6262
// External temperature connected on input A
63+
#ifdef ARDUINO_NANO_RP2040_CONNECT
6364
#define OW_PIN digitalPinToPinName(INPUTA_PIN)
65+
#endif
66+
#ifdef ESP32
67+
#define OW_PIN digitalPinToGPIONumber(INPUTA_PIN)
68+
#endif
6469
#define EXTERNAL_TEMPERATURE_DISABLED -273.15; // absolute zero xD
6570

6671
// Microphone - PDM on Arduino Nano RP2040 Connect
@@ -100,6 +105,9 @@ const uint16_t MAXIMUM_AMPS{1}; // 1A
100105
#define START_EXTERNAL_AMBIENT_SENSOR 3 // ds18b20
101106

102107

108+
#define EXTERNAL_TEMPERATURE_CORE 1 // user
109+
#define INTERNAL_TEMPERATURE_CORE 0 // other
110+
103111

104112

105113

0 commit comments

Comments
 (0)