File tree Expand file tree Collapse file tree 1 file changed +12
-0
lines changed Expand file tree Collapse file tree 1 file changed +12
-0
lines changed Original file line number Diff line number Diff line change @@ -114,10 +114,22 @@ int microbit_hal_pin_get_pull(int pin) {
114114}
115115
116116int microbit_hal_pin_set_analog_period_us (int pin, int period) {
117+ // Change the audio virtual-pin period if the pin is the special mixer pin.
117118 if (pin == MICROBIT_HAL_PIN_MIXER) {
118119 uBit.audio .virtualOutputPin .setAnalogPeriodUs (period);
119120 return 0 ;
120121 }
122+
123+ // Calling setAnalogPeriodUs requires the pin to be in analog-out mode. So
124+ // test for this mode by first calling getAnalogPeriodUs, and if it fails then
125+ // attempt to configure the pin in analog-out mode by calling setAnalogValue.
126+ if ((ErrorCode)pin_obj[pin]->getAnalogPeriodUs () == DEVICE_NOT_SUPPORTED) {
127+ if (pin_obj[pin]->setAnalogValue (0 ) != DEVICE_OK) {
128+ return -1 ;
129+ }
130+ }
131+
132+ // Set the analog period.
121133 if (pin_obj[pin]->setAnalogPeriodUs (period) == DEVICE_OK) {
122134 return 0 ;
123135 } else {
You can’t perform that action at this time.
0 commit comments