get_mode for micro:bit pins with analog read and write

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
kjw
Posts: 11
Joined: Wed Jun 17, 2020 11:34 am

get_mode for micro:bit pins with analog read and write

Post by kjw » Sat Aug 22, 2020 4:32 pm

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'

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

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

Post by jimmo » Thu Sep 03, 2020 5:44 am

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).

Post Reply