@@ -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
600597void 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
908927void 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
0 commit comments