@@ -750,7 +750,7 @@ JshPinState jshPinGetState(Pin pin) {
750
750
#if JSH_PORTV_COUNT > 0
751
751
// handle virtual ports (eg. pins on an IO Expander)
752
752
if ((pinInfo [pin ].port & JSH_PORT_MASK )== JSH_PORTV )
753
- return JSHPINSTATE_UNDEFINED ;
753
+ return jshVirtualPinGetState ( pin ) ;
754
754
#endif
755
755
uint32_t ipin = (uint32_t )pinInfo [pin ].pin ;
756
756
#if NRF_SD_BLE_API_VERSION > 5
@@ -818,39 +818,48 @@ nrf_saadc_value_t nrf_analog_read() {
818
818
819
819
return result ;
820
820
}
821
+
822
+ JsVarFloat nrf_analog_read_pin (int channel /*0..7*/ ) {
823
+ // sanity checks for channel
824
+ assert (NRF_SAADC_INPUT_AIN0 == 1 );
825
+ assert (NRF_SAADC_INPUT_AIN1 == 2 );
826
+ assert (NRF_SAADC_INPUT_AIN2 == 3 );
827
+
828
+ nrf_saadc_input_t ain = channel + 1 ;
829
+ nrf_saadc_channel_config_t config ;
830
+ config .acq_time = NRF_SAADC_ACQTIME_3US ;
831
+ config .gain = NRF_SAADC_GAIN1_4 ; // 1/4 of input volts
832
+ config .mode = NRF_SAADC_MODE_SINGLE_ENDED ;
833
+ config .pin_p = ain ;
834
+ config .pin_n = ain ;
835
+ config .reference = NRF_SAADC_REFERENCE_VDD4 ; // VDD/4 as reference.
836
+ config .resistor_p = NRF_SAADC_RESISTOR_DISABLED ;
837
+ config .resistor_n = NRF_SAADC_RESISTOR_DISABLED ;
838
+
839
+ // make reading
840
+ nrf_saadc_enable ();
841
+ nrf_saadc_resolution_set (NRF_SAADC_RESOLUTION_14BIT );
842
+ nrf_saadc_channel_init (0 , & config );
843
+
844
+ JsVarFloat f = nrf_analog_read () / 16384.0 ;
845
+ nrf_saadc_channel_input_set (0 , NRF_SAADC_INPUT_DISABLED , NRF_SAADC_INPUT_DISABLED ); // give us back our pin!
846
+ nrf_saadc_disable ();
847
+ return f ;
848
+ }
821
849
#endif
822
850
823
851
// Returns an analog value between 0 and 1
824
852
JsVarFloat jshPinAnalog (Pin pin ) {
853
+ #if JSH_PORTV_COUNT > 0
854
+ // handle virtual ports (eg. pins on an IO Expander)
855
+ if ((pinInfo [pin ].port & JSH_PORT_MASK )== JSH_PORTV )
856
+ return jshVirtualPinGetAnalogValue (pin );
857
+ #endif
825
858
if (pinInfo [pin ].analog == JSH_ANALOG_NONE ) return NAN ;
826
859
if (!jshGetPinStateIsManual (pin ))
827
860
jshPinSetState (pin , JSHPINSTATE_ADC_IN );
828
861
#ifdef NRF52
829
- // sanity checks for channel
830
- assert (NRF_SAADC_INPUT_AIN0 == 1 );
831
- assert (NRF_SAADC_INPUT_AIN1 == 2 );
832
- assert (NRF_SAADC_INPUT_AIN2 == 3 );
833
- nrf_saadc_input_t ain = 1 + (pinInfo [pin ].analog & JSH_MASK_ANALOG_CH );
834
-
835
- nrf_saadc_channel_config_t config ;
836
- config .acq_time = NRF_SAADC_ACQTIME_3US ;
837
- config .gain = NRF_SAADC_GAIN1_4 ; // 1/4 of input volts
838
- config .mode = NRF_SAADC_MODE_SINGLE_ENDED ;
839
- config .pin_p = ain ;
840
- config .pin_n = ain ;
841
- config .reference = NRF_SAADC_REFERENCE_VDD4 ; // VDD/4 as reference.
842
- config .resistor_p = NRF_SAADC_RESISTOR_DISABLED ;
843
- config .resistor_n = NRF_SAADC_RESISTOR_DISABLED ;
844
-
845
- // make reading
846
- nrf_saadc_enable ();
847
- nrf_saadc_resolution_set (NRF_SAADC_RESOLUTION_14BIT );
848
- nrf_saadc_channel_init (0 , & config );
849
-
850
- JsVarFloat f = nrf_analog_read () / 16384.0 ;
851
- nrf_saadc_channel_input_set (0 , NRF_SAADC_INPUT_DISABLED , NRF_SAADC_INPUT_DISABLED ); // give us back our pin!
852
- nrf_saadc_disable ();
853
- return f ;
862
+ return nrf_analog_read_pin (pinInfo [pin ].analog & JSH_MASK_ANALOG_CH );
854
863
#else
855
864
const nrf_adc_config_t nrf_adc_config = {
856
865
NRF_ADC_CONFIG_RES_10BIT ,
0 commit comments