Page 1 of 1
dynamically set up pin classes
Posted: Tue Sep 14, 2021 3:27 pm
by mbell
Hi Gang,
I would like my PICOs pins to be somewhat dynamically initialised when the PICO loads using some sort of file.
To now my idea is to have a config.txt file with something like the following.
in the example below the CSV variables represent a class and a setup:
Code: Select all
GP5=ToggleSwitch,"TABLE SWITCH", 5, Pin.OUT, Pin.PULL_UP
GP6=ToggleSwitch,"DESK SWITCH", 6, Pin.OUT, Pin.PULL_UP
using GP5 line as an example :
GP5 is the pin I am trying to set up.
ToggleSwitch is a class
"TABLE SWITCH" is a ToggleSwitch constructor
5 is a ToggleSwitch constructor
Pin.OUT is a ToggleSwitch constructor
Pin.PULL_UP is a ToggleSwitch constructor
is there a way i can do this ?
Re: dynamically set up pin classes
Posted: Wed Sep 15, 2021 7:05 pm
by balance
Hi mbell,
this answer will only be a part of your solution, but may help you.
I have dynamical declared my output pins with a class.
Code: Select all
from machine import Pin, Timer
import time
my_OUT = [] # this will contain all dynamic declared output objects
my_output_pins = [0, 1, 6, 7, 8, 9] # these are my Pi Pico pins i connected a led to
class my_output(): #here starts the class for all output pins
def __init__(self, id, pin): # this function will create a output object
self.id = id #not mandatory needed but can be helpful later in more complex context
self.out_pin = Pin(pin, Pin.OUT) # configure the output pin as output ;-)
self.out_pin.off() # the pin starts LOW
def state(self, state): # function to manipulate the pinstate
if state == 0:
self.out_pin.off()
if state == 1:
self.out_pin.on()
if state == 2:
self.out_pin.toggle()
for idx, pin in enumerate(my_output_pins): # for loop to create the output objects and put them in the list my_OUT to make them accessable and iterable
my_OUT.append(my_output(idx, pin))
while True:
my_OUT[2].state(2) # this accesses the third object (0, 1, 2, ... ) (which means GP06 in this case) in the objects list and goes to the state function in the class. The parameter 2 makes the output toggling every 250 ms
time.sleep_ms(250)
With this solution, you can have "unlimited" objects declared as a ouput and acces every objects state very easy.
I hope this helps you a bit.
Re: dynamically set up pin classes
Posted: Thu Sep 16, 2021 9:10 am
by mbell
Thanks for that,
say i wanted to parse a file to create classes that are defined in the file
GP5=ToggleSwitch,"TABLE SWITCH", 5, 0, 1
GP4=LED,"MAIN LIGHT",4,1,1
where ToggleSwitch and LED are objects i want to create.
again maybe I am tryharding.
Re: dynamically set up pin classes
Posted: Thu Sep 16, 2021 5:10 pm
by balance
Show us what you have so far. For example the code you already have.
What idea did you develop until now? I know your starting point (.csv) and your aim (dynamic pin declaration) but not the inbetween.
Than helping you is much more effective and you will learn much more.
Re: dynamically set up pin classes
Posted: Fri Sep 17, 2021 9:32 am
by hippy
mbell wrote: ↑Thu Sep 16, 2021 9:10 am
say i wanted to parse a file to create classes that are defined in the file
GP5=ToggleSwitch,"TABLE SWITCH", 5, 0, 1
GP4=LED,"MAIN LIGHT",4,1,1
It seems to me you could use something like below, though it's not entirely robust -
Code: Select all
with open('file.txt', 'r') as f:
for line in f:
a = line.strip().split('=') # a = ['GP5', 'ToggleSwitch, "TABLE SWITCH", 5, 0, 1']
b = a[1].split(',') # b = ['ToggleSwitch','"TABLE SWITCH"', '5', '0', '1']
if b[0] == 'ToggleSwitch' : ToggleSwitch(a[0], b[1:])
elif b[0] == 'LED' : LED(a[0], b[1:])