Page 1 of 1

get_mode for micro:bit pins with analog read and write

Posted: Sat Aug 22, 2020 4:32 pm
by kjw
I can't find the documentation for the BBC micro:bit MicroPython's get_mode() method on the pins. It appears to be set all the time to a useful value apart from for read_analog() where it returns unused for some reason?

Code: Select all

MicroPython v1.9.2-34-gd64154c73 on 2017-09-01; micro:bit v1.0.1 with nRF51822
Type "help()" for more information.
>>> 
>>> from microbit import *
>>> pin2.get_mode()
'read_digital'
>>> pin2.write_digital(1)
>>> pin2.get_mode()
'write_digital'
>>> pin2.write_analog(123)
>>> pin2.get_mode()       
'write_analog'
>>> pin2.is_touched()     
False
>>> pin2.get_mode()  
'touch'
>>> pin2.read_analog()
223
>>> pin2.get_mode()   
'unused'
There are some tests in https://github.com/bbcmicrobit/micropyt ... st_pins.py which expect this behaviour but they do not document why that result is expected. They also suggest that write_analog mode varies with the value which I've just confirmed too:

Code: Select all

>>> pin2.write_analog(123)
>>> pin2.get_mode()
'write_analog'
>>> pin2.write_analog(0)  
>>> pin2.get_mode()     
'unused'

Re: get_mode for micro:bit pins with analog read and write

Posted: Thu Sep 03, 2020 5:44 am
by jimmo
My guess would be that after a successful analog read, the pin is in high-impedance mode. i.e. the pin isn't in any mode, so there's no need for it to do any cleanup when transitioning to another mode. (This is mostly what the "pin mode" feature is for -- it internally tracks the mode so it knows when it's allowed to change mode and how to leave the current mode).

Note that this is different to read digital, which might have a pull-up/down/none enabled (also you can only change the pull when it read digital mode).