I can't give you a direct answer to your question. I suspect that mode() doesn't work that way (as you can see from the mode value available in the Pin constructor, PWM isn't one of the possible values - it would just be an Output in terms of mode).
http://docs.micropython.org/en/v1.9.3/e ... e.Pin.html
The way you use PWM and Pin are surprising and I am not sure they behave as you expect. Using them differently would help you.
First of all, I think you are expected to create a Pin when you want to use it, and keep the reference as long as the Pin is in use. It isn't just a wrapper for an id, which is then intended to be thrown away after every line. As you can see from the Pin#__init__() method there can be side effects from every call to the Pin constructor (including all of the default values which get set if they are not explicitly specified in the Pin() call).
Similarly, a PWM isn't intended to be created just to issue a single PWM-related request and then be thrown away, it is an object representing a specific function which has been allocated to a pin, and should be kept around as long as that function remains allocated to the pin, and especially if you plan to reinitialise or manipulate that function.
If you want to keep a map of inputs, outputs and pwms, then I suggest to create a dict of tuples, indexed by pin id and keep the references in there, with some metadata which helps you recall what function was allocated.
The first item of the tuple could be the reference to the Pin object, which is created ONCE when a pin is allocated a specific function. Following items should be data structures detailing previous configuration which has been made of the pin. You could put the PWM in there, or perhaps a mode value, or whatever else suits you to be able to recover relevant prior information about configuration. Obviously this structure can be managed lots of ways, but
- there is only one place to look for a pin's function - not under multiple headings - "led", "input", "output",
- a Pin is only created once, is stored, and has its functions manipulated later
- similarly any PWM is only created once, is stored, and has its functions manipulated later
Since all configuration would have been done by your own code, you don't need to rely on some other built-in record to retrieve this information from the micropython API, it will be in your managed dict.
That way, when you seek to switch a pin from one function to another, you can look up in your dict to see what function it was previously allocated, and use the Pin and or PWM reference directly to change its configuration (e.g. using pin.init()) or to undo prior operations, (for example calling deinit() on a previously created PWM).
If you were to artificially populate the dict structure (rather than populating by pin-function allocations one-by-one as in the real class, it might look like this pseudocode (sorry for any errors I haven't got a module to run this on right now)...
Code: Select all
from machine import Pin, PWM
MYIN = 0
MYOUT = 1
MYPWM = 2
pinRef4 = Pin(4, mode=Pin.OUT)
pinRef5 = Pin(5, mode=Pin.IN)
pinRef6 = Pin(6, mode=Pin.Out)
pwmRef6 = PWM(pinRef6)
WittyBoard.lookup = dict(
4=(pinRef4, MYOUT),
5=(pinRef5, MYIN),
6=(pinRef6, MYPWM, pwmRef6)
)
You can see how this data structure can allow you to save and retrieve the Pin and (sometimes) PWM references, and query previously allocated functions.