@@ -90,7 +90,7 @@ ScienceKitCarrier::ScienceKitCarrier(){
90
90
#endif
91
91
92
92
#ifdef ARDUINO_NANO_RP2040_CONNECT
93
- thread_activity_led = new rtos::Thread ();
93
+ thread_status_led = new rtos::Thread ();
94
94
thread_update_bme = new rtos::Thread ();
95
95
thread_external_temperature = new rtos::Thread ();
96
96
thread_ultrasonic = new rtos::Thread ();
@@ -103,8 +103,13 @@ ScienceKitCarrier::ScienceKitCarrier(){
103
103
thread_bme_is_running = false ;
104
104
thread_ext_temperature_is_running = false ;
105
105
thread_ultrasonic_is_running = false ;
106
+ thread_led_is_running = false ;
106
107
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 ;
108
113
}
109
114
110
115
@@ -582,19 +587,11 @@ float ScienceKitCarrier::getMagneticFieldZ(){
582
587
return magnetic_field[2 ];
583
588
}
584
589
585
- /* *******************************************************************/
586
- /* delay */
587
- /* *******************************************************************/
588
590
589
- #ifdef ARDUINO_NANO_RP2040_CONNECT
590
- void ScienceKitCarrier::delay (unsigned long t){
591
- rtos::ThisThread::sleep_for (t);
592
- }
593
- #endif
594
591
595
592
596
593
/* *******************************************************************/
597
- /* LEDs: errors and activity */
594
+ /* LEDs: errors and status */
598
595
/* *******************************************************************/
599
596
600
597
void ScienceKitCarrier::errorTrap (const int error_code){
@@ -619,59 +616,69 @@ void ScienceKitCarrier::errorTrap(const int error_code){
619
616
}
620
617
}
621
618
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 ;
624
662
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 );
666
674
}
667
675
}
668
- #endif
669
676
670
- void ScienceKitCarrier::setActivityLed ( const int led_state ){
671
- activity_led_state=led_state ;
677
+ void ScienceKitCarrier::freeRTOSStatusLed ( void * pvParameters ){
678
+ ((ScienceKitCarrier*) pvParameters)-> threadStatusLed () ;
672
679
}
673
-
674
-
680
+ # endif
681
+
675
682
676
683
/* *******************************************************************/
677
684
/* Function Generator Controller */
@@ -726,13 +733,19 @@ void ScienceKitCarrier::updateUltrasonic(){
726
733
if (ultrasonic_data==4294967295 ){
727
734
ultrasonic_measure = -1.0 ;
728
735
ultrasonic_is_connected = false ;
736
+ #ifdef ESP32
737
+ setStatusLed (STATUS_LED_RM_ULTRASONIC);
738
+ #endif
729
739
}
730
740
else {
731
741
ultrasonic_measure = float (ultrasonic_data) / 1000.0 ;
732
742
if (ultrasonic_measure>4500.0 ){
733
743
ultrasonic_measure = 4500.0 ;
734
744
}
735
745
ultrasonic_is_connected = true ;
746
+ #ifdef ESP32
747
+ setStatusLed (STATUS_LED_ADD_ULTRASONIC);
748
+ #endif
736
749
}
737
750
738
751
if (ultrasonic_is_connected){
@@ -819,10 +832,16 @@ void ScienceKitCarrier::updateExternalTemperature(){
819
832
if (ec == OneWireNg::EC_SUCCESS) {
820
833
if (scrpd->getAddr ()!=15 ){
821
834
external_temperature_is_connected=false ;
835
+ #ifdef ESP32
836
+ setStatusLed (STATUS_LED_RM_EXT_TEMP);
837
+ #endif
822
838
external_temperature = EXTERNAL_TEMPERATURE_DISABLED;
823
839
}
824
840
else {
825
841
external_temperature_is_connected=true ;
842
+ #ifdef ESP32
843
+ setStatusLed (STATUS_LED_ADD_EXT_TEMP);
844
+ #endif
826
845
long temp = scrpd->getTemp ();
827
846
int sign=1 ;
828
847
if (temp < 0 ) {
@@ -906,7 +925,6 @@ uint ScienceKitCarrier::getMicrophoneRMS(){
906
925
/* *******************************************************************/
907
926
908
927
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
910
928
// start bme688 thread
911
929
if ((auxiliary_threads==START_AUXILIARY_THREADS)||(auxiliary_threads==START_INTERNAL_AMBIENT_SENSOR)){
912
930
if (!thread_bme_is_running){
@@ -945,6 +963,16 @@ void ScienceKitCarrier::startAuxiliaryThreads(const uint8_t auxiliary_threads){
945
963
}
946
964
thread_ultrasonic_is_running = true ;
947
965
}
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
948
976
}
949
977
950
978
0 commit comments