Skip to content

Commit a412b2e

Browse files
committed
add: status led for esp32
1 parent d74aef8 commit a412b2e

File tree

3 files changed

+111
-74
lines changed

3 files changed

+111
-74
lines changed

src/Arduino_ScienceKitCarrier.cpp

+88-60
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ ScienceKitCarrier::ScienceKitCarrier(){
9090
#endif
9191

9292
#ifdef ARDUINO_NANO_RP2040_CONNECT
93-
thread_activity_led = new rtos::Thread();
93+
thread_status_led = new rtos::Thread();
9494
thread_update_bme = new rtos::Thread();
9595
thread_external_temperature = new rtos::Thread();
9696
thread_ultrasonic = new rtos::Thread();
@@ -103,8 +103,13 @@ ScienceKitCarrier::ScienceKitCarrier(){
103103
thread_bme_is_running = false;
104104
thread_ext_temperature_is_running = false;
105105
thread_ultrasonic_is_running = false;
106+
thread_led_is_running = false;
106107

107-
activity_led_state = ACTIVITY_LED_OFF;
108+
status_led_state = STATUS_LED_OFF;
109+
enable_led_red = false;
110+
enable_led_green = false;
111+
enable_led_blue = false;
112+
led_time_base = 20;
108113
}
109114

110115

@@ -582,19 +587,11 @@ float ScienceKitCarrier::getMagneticFieldZ(){
582587
return magnetic_field[2];
583588
}
584589

585-
/********************************************************************/
586-
/* delay */
587-
/********************************************************************/
588590

589-
#ifdef ARDUINO_NANO_RP2040_CONNECT
590-
void ScienceKitCarrier::delay(unsigned long t){
591-
rtos::ThisThread::sleep_for(t);
592-
}
593-
#endif
594591

595592

596593
/********************************************************************/
597-
/* LEDs: errors and activity */
594+
/* LEDs: errors and status */
598595
/********************************************************************/
599596

600597
void ScienceKitCarrier::errorTrap(const int error_code){
@@ -619,59 +616,69 @@ void ScienceKitCarrier::errorTrap(const int error_code){
619616
}
620617
}
621618

622-
#ifdef ARDUINO_NANO_RP2040_CONNECT
623-
void ScienceKitCarrier::threadActivityLed(){
619+
#ifdef ESP32
620+
void ScienceKitCarrier::setStatusLed(const int led_state){
621+
switch (led_state){
622+
case STATUS_LED_OFF:
623+
enable_led_red = false;
624+
enable_led_green = false;
625+
enable_led_blue = false;
626+
break;
627+
case STATUS_LED_BLE:
628+
enable_led_red = false;
629+
enable_led_green = false;
630+
enable_led_blue = true;
631+
led_time_base = 20;
632+
break;
633+
case STATUS_LED_PAIRING:
634+
enable_led_red = false;
635+
enable_led_green = false;
636+
enable_led_blue = true;
637+
led_time_base = 5;
638+
break;
639+
case STATUS_LED_ADD_EXT_TEMP:
640+
enable_led_red = true;
641+
break;
642+
case STATUS_LED_ADD_ULTRASONIC:
643+
enable_led_green = true;
644+
break;
645+
case STATUS_LED_RM_EXT_TEMP:
646+
enable_led_red = false;
647+
break;
648+
case STATUS_LED_RM_ULTRASONIC:
649+
enable_led_green = false;
650+
break;
651+
default:
652+
enable_led_red = false;
653+
enable_led_green = false;
654+
enable_led_blue = false;
655+
}
656+
}
657+
658+
void ScienceKitCarrier::threadStatusLed(){
659+
unsigned long animation_time = millis();
660+
int brightness = 0;
661+
int fadeAmount = 5;
624662
while(1){
625-
switch (activity_led_state){
626-
case ACTIVITY_LED_OFF:
627-
digitalWrite(LEDB,LOW);
628-
digitalWrite(LEDG,LOW);
629-
break;
630-
case ACTIVITY_LED_BLE: // blue breathing effect
631-
digitalWrite(LEDG, LOW);
632-
for(int i=255; i>0; i--){
633-
analogWrite(LEDB, i);
634-
rtos::ThisThread::sleep_for(10);
635-
}
636-
for(int i=0; i<255; i++){
637-
analogWrite(LEDB, i);
638-
rtos::ThisThread::sleep_for(10);
639-
}
640-
rtos::ThisThread::sleep_for(100);
641-
break;
642-
case ACTIVITY_LED_PAIRING: // blue-green flashing
643-
for(int i = 255; i>0; i=i-2){
644-
analogWrite(LEDG,i);
645-
rtos::ThisThread::sleep_for(1);
646-
}
647-
for(int i = 0; i<255; i=i+2){
648-
analogWrite(LEDG,i);
649-
rtos::ThisThread::sleep_for(1);
650-
}
651-
for(int i = 255; i>0; i=i-2){
652-
analogWrite(LEDB,i);
653-
rtos::ThisThread::sleep_for(1);
654-
}
655-
for(int i = 0; i<255; i=i+2){
656-
analogWrite(LEDB,i);
657-
rtos::ThisThread::sleep_for(1);
658-
}
659-
digitalWrite(LEDG, LOW);
660-
digitalWrite(LEDB, LOW);
661-
break;
662-
default: // any other value turns off leds
663-
digitalWrite(LEDB,LOW);
664-
digitalWrite(LEDG,LOW);
665-
}
663+
while(millis()-animation_time>led_time_base){
664+
animation_time = millis();
665+
analogWrite(LED_RED, 255-(0.125*brightness)*enable_led_red);
666+
analogWrite(LED_GREEN, 255-brightness*enable_led_green);
667+
analogWrite(LED_BLUE, 255-brightness*enable_led_blue);
668+
brightness = brightness + fadeAmount;
669+
if (brightness <= 0 || brightness >= 255) {
670+
fadeAmount = -fadeAmount;
671+
}
672+
}
673+
delay(5);
666674
}
667675
}
668-
#endif
669676

670-
void ScienceKitCarrier::setActivityLed(const int led_state){
671-
activity_led_state=led_state;
677+
void ScienceKitCarrier::freeRTOSStatusLed(void * pvParameters){
678+
((ScienceKitCarrier*) pvParameters)->threadStatusLed();
672679
}
673-
674-
680+
#endif
681+
675682

676683
/********************************************************************/
677684
/* Function Generator Controller */
@@ -726,13 +733,19 @@ void ScienceKitCarrier::updateUltrasonic(){
726733
if (ultrasonic_data==4294967295){
727734
ultrasonic_measure = -1.0;
728735
ultrasonic_is_connected = false;
736+
#ifdef ESP32
737+
setStatusLed(STATUS_LED_RM_ULTRASONIC);
738+
#endif
729739
}
730740
else{
731741
ultrasonic_measure = float(ultrasonic_data) / 1000.0;
732742
if (ultrasonic_measure>4500.0){
733743
ultrasonic_measure = 4500.0;
734744
}
735745
ultrasonic_is_connected = true;
746+
#ifdef ESP32
747+
setStatusLed(STATUS_LED_ADD_ULTRASONIC);
748+
#endif
736749
}
737750

738751
if (ultrasonic_is_connected){
@@ -819,10 +832,16 @@ void ScienceKitCarrier::updateExternalTemperature(){
819832
if (ec == OneWireNg::EC_SUCCESS) {
820833
if (scrpd->getAddr()!=15){
821834
external_temperature_is_connected=false;
835+
#ifdef ESP32
836+
setStatusLed(STATUS_LED_RM_EXT_TEMP);
837+
#endif
822838
external_temperature = EXTERNAL_TEMPERATURE_DISABLED;
823839
}
824840
else{
825841
external_temperature_is_connected=true;
842+
#ifdef ESP32
843+
setStatusLed(STATUS_LED_ADD_EXT_TEMP);
844+
#endif
826845
long temp = scrpd->getTemp();
827846
int sign=1;
828847
if (temp < 0) {
@@ -906,7 +925,6 @@ uint ScienceKitCarrier::getMicrophoneRMS(){
906925
/********************************************************************/
907926

908927
void ScienceKitCarrier::startAuxiliaryThreads(const uint8_t auxiliary_threads){
909-
//thread_activity_led->start(mbed::callback(this, &ScienceKitCarrier::threadActivityLed)); //left for legacy on prototypes and maybe future implementations
910928
// start bme688 thread
911929
if ((auxiliary_threads==START_AUXILIARY_THREADS)||(auxiliary_threads==START_INTERNAL_AMBIENT_SENSOR)){
912930
if (!thread_bme_is_running){
@@ -945,6 +963,16 @@ void ScienceKitCarrier::startAuxiliaryThreads(const uint8_t auxiliary_threads){
945963
}
946964
thread_ultrasonic_is_running = true;
947965
}
966+
967+
// start status led
968+
#ifdef ESP32
969+
if ((auxiliary_threads==START_AUXILIARY_THREADS)||(auxiliary_threads==START_STATUS_LED)){
970+
if (!thread_led_is_running){
971+
xTaskCreatePinnedToCore(this->freeRTOSStatusLed, "update_led", 10000, this, 1, &thread_led, LED_CORE);
972+
}
973+
thread_led_is_running = true;
974+
}
975+
#endif
948976
}
949977

950978

src/Arduino_ScienceKitCarrier.h

+11-10
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ class ScienceKitCarrier{
107107
static const char channels = MICROPHONE_CHANNELS;
108108
static const int frequency = MICROPHONE_FREQUENCY;
109109

110-
rtos::Thread * thread_activity_led;
110+
rtos::Thread * thread_status_led;
111111
rtos::Thread * thread_update_bme;
112112
rtos::Thread * thread_external_temperature;
113113
rtos::Thread * thread_ultrasonic;
@@ -118,14 +118,18 @@ class ScienceKitCarrier{
118118
TaskHandle_t thread_internal_temperature;
119119
TaskHandle_t thread_external_temperature;
120120
TaskHandle_t thread_ultrasonic;
121+
TaskHandle_t thread_led;
121122
SemaphoreHandle_t wire_semaphore;
122123
#endif
123124

124125
bool thread_bme_is_running;
125126
bool thread_ext_temperature_is_running;
126127
bool thread_ultrasonic_is_running;
128+
bool thread_led_is_running;
127129

128-
uint8_t activity_led_state;
130+
uint8_t status_led_state;
131+
bool enable_led_red, enable_led_green, enable_led_blue;
132+
unsigned long led_time_base;
129133

130134
void requestUltrasonicUpdate();
131135
void retriveUltrasonicUpdate();
@@ -138,18 +142,15 @@ class ScienceKitCarrier{
138142

139143
void startAuxiliaryThreads(const uint8_t auxiliary_threads=START_AUXILIARY_THREADS);
140144

141-
#ifdef ARDUINO_NANO_RP2040_CONNECT
142-
void delay(unsigned long t); // you must use this instead delay, due threads usage
143-
#endif
144-
145145
/* Blink red alert */
146146
void errorTrap(const int error_code=0);
147147

148-
/* Activity led */
149-
#ifdef ARDUINO_NANO_RP2040_CONNECT
150-
void threadActivityLed();
148+
/* Status led */
149+
#ifdef ESP32
150+
void setStatusLed(const int led_state=STATUS_LED_OFF);
151+
void threadStatusLed();
152+
static void freeRTOSStatusLed(void * pvParameters);
151153
#endif
152-
void setActivityLed(const int led_state=ACTIVITY_LED_OFF);
153154

154155

155156

src/utils/Arduino_ScienceKitCarrier_definitions.h

+12-4
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,14 @@ const uint16_t MAXIMUM_AMPS{1}; // 1A
9494

9595

9696
// Led
97-
#define ACTIVITY_LED_OFF 0
98-
#define ACTIVITY_LED_BLE 1
99-
#define ACTIVITY_LED_PAIRING 2
97+
#define STATUS_LED_OFF 0
98+
#define STATUS_LED_BLE 1
99+
#define STATUS_LED_PAIRING 2
100+
#define STATUS_LED_ADD_EXT_TEMP 3
101+
#define STATUS_LED_ADD_ULTRASONIC 4
102+
#define STATUS_LED_RM_EXT_TEMP 5
103+
#define STATUS_LED_RM_ULTRASONIC 6
104+
100105

101106
// Update
102107
#define ROUND_ROBIN_ENABLED 1
@@ -107,11 +112,14 @@ const uint16_t MAXIMUM_AMPS{1}; // 1A
107112
#define START_INTERNAL_AMBIENT_SENSOR 2 // bme688
108113
#define START_EXTERNAL_AMBIENT_SENSOR 3 // ds18b20
109114
#define START_ULTRASONIC 4 // grove I2C ultrasonic sensor
115+
#define START_STATUS_LED 5
116+
#
110117

111118
#ifdef ESP32 // 1 user, 0 secondary core
112119
#define EXTERNAL_TEMPERATURE_CORE 1
113120
#define INTERNAL_TEMPERATURE_CORE 0
114-
#define ULTRASONIC_CORE 1
121+
#define ULTRASONIC_CORE 0
122+
#define LED_CORE 0
115123
#endif
116124

117125

0 commit comments

Comments
 (0)