19
19
20
20
#include " Arduino_ScienceKitCarrier.h"
21
21
22
+ // static members must be initialized externally the class definition
23
+ short ScienceKitCarrier::sampleBuffer[MICROPHONE_BUFFER_SIZE];
24
+ volatile int ScienceKitCarrier::samplesRead;
25
+
22
26
23
27
ScienceKitCarrier::ScienceKitCarrier (){
24
28
round_robin_index=0 ;
@@ -77,7 +81,10 @@ ScienceKitCarrier::ScienceKitCarrier(){
77
81
external_temperature=EXTERNAL_TEMPERATURE_DISABLED;
78
82
external_temperature_is_connected=false ;
79
83
80
- round_robin_index = 0 ;
84
+ microphone_rms=0 ;
85
+ rms=0 ;
86
+
87
+ round_robin_index=0 ;
81
88
82
89
thread_activity_led = new rtos::Thread ();
83
90
thread_update_bme = new rtos::Thread ();
@@ -140,6 +147,10 @@ int ScienceKitCarrier::begin(const uint8_t auxiliary_threads){
140
147
return ERR_BEGIN_ULTRASONIC;
141
148
}
142
149
150
+ // let's start microphone (PDM on Arduino Nano RP2040 Connect)
151
+ if (beginMicrophone ()!=0 ){
152
+ return ERR_BEGIN_MICROPHONE;
153
+ }
143
154
144
155
// let's start bme688 and external ds18b20 probe
145
156
startAuxiliaryThreads (auxiliary_threads);
@@ -154,6 +165,7 @@ int ScienceKitCarrier::begin(const uint8_t auxiliary_threads){
154
165
/* *******************************************************************/
155
166
156
167
void ScienceKitCarrier::update (const bool roundrobin){
168
+ updateMicrophone (); // about 1ms when MICROPHONE_BUFFER_SIZE is 512
157
169
if (!roundrobin){
158
170
updateAnalogInput ();
159
171
updateAPDS ();
@@ -608,6 +620,8 @@ void ScienceKitCarrier::setActivityLed(const int led_state){
608
620
activity_led_state=led_state;
609
621
}
610
622
623
+
624
+
611
625
/* *******************************************************************/
612
626
/* Function Generator Controller */
613
627
/* *******************************************************************/
@@ -646,6 +660,8 @@ uint8_t ScienceKitCarrier::getRange2(){
646
660
return range2;
647
661
}
648
662
663
+
664
+
649
665
/* *******************************************************************/
650
666
/* Ultrasonic Sensor */
651
667
/* *******************************************************************/
@@ -678,6 +694,8 @@ bool ScienceKitCarrier::getUltrasonicIsConnected(){
678
694
return ultrasonic_is_connected;
679
695
}
680
696
697
+
698
+
681
699
/* *******************************************************************/
682
700
/* External Temperature Probe */
683
701
/* *******************************************************************/
@@ -691,13 +709,13 @@ int ScienceKitCarrier::beginExternalTemperature(){
691
709
void ScienceKitCarrier::updateExternalTemperature (){
692
710
float temperature;
693
711
pinMode (OW_PIN,INPUT);
712
+
694
713
DSTherm drv (ow);
695
-
696
- drv.convertTempAll (DSTherm::MAX_CONV_TIME, false );
714
+ drv.convertTempAll (DSTherm::MAX_CONV_TIME, false );
697
715
698
716
static Placeholder<DSTherm::Scratchpad> scrpd;
699
-
700
717
OneWireNg::ErrorCode ec = drv.readScratchpadSingle (scrpd);
718
+
701
719
if (ec == OneWireNg::EC_SUCCESS) {
702
720
if (scrpd->getAddr ()!=15 ){
703
721
external_temperature_is_connected=false ;
@@ -717,7 +735,6 @@ void ScienceKitCarrier::updateExternalTemperature(){
717
735
}
718
736
}
719
737
720
-
721
738
float ScienceKitCarrier::getExternalTemperature (){
722
739
return external_temperature;
723
740
}
@@ -729,7 +746,6 @@ bool ScienceKitCarrier::getExternalTemperatureIsConnected(){
729
746
void ScienceKitCarrier::threadExternalTemperature (){
730
747
beginExternalTemperature ();
731
748
while (1 ){
732
- // updateAnalogInput(UPDATE_INPUT_A);
733
749
updateExternalTemperature ();
734
750
updateAnalogInput (UPDATE_INPUT_A);
735
751
rtos::ThisThread::sleep_for (1000 );
@@ -738,6 +754,43 @@ void ScienceKitCarrier::threadExternalTemperature(){
738
754
739
755
740
756
757
+ /* *******************************************************************/
758
+ /* Microphone */
759
+ /* *******************************************************************/
760
+
761
+ int ScienceKitCarrier::beginMicrophone (){
762
+ PDM.setGain (50 );
763
+ PDM.onReceive (updateMicrophoneDataBuffer);
764
+ if (!PDM.begin (channels, frequency)){
765
+ return ERR_BEGIN_MICROPHONE;
766
+ }
767
+ return 0 ;
768
+ }
769
+
770
+ void ScienceKitCarrier::updateMicrophone (){
771
+ if (samplesRead) {
772
+ // Calculate the RMS of data buffer
773
+ rms=0 ;
774
+ for (int i=0 ; i<samplesRead; i++){
775
+ rms=rms+(sampleBuffer[i]*sampleBuffer[i]);
776
+ }
777
+ rms=rms/samplesRead;
778
+ microphone_rms=sqrt (rms);
779
+ samplesRead = 0 ;
780
+ }
781
+ }
782
+
783
+ void ScienceKitCarrier::updateMicrophoneDataBuffer (){
784
+ int bytesAvailable = PDM.available ();
785
+ PDM.read (sampleBuffer, bytesAvailable);
786
+ samplesRead = bytesAvailable / 2 ;
787
+ }
788
+
789
+ uint ScienceKitCarrier::getMicrophoneRMS (){
790
+ return microphone_rms;
791
+ }
792
+
793
+
741
794
742
795
743
796
/* *******************************************************************/
0 commit comments