Page 1 of 1

Any clever ways to set up pins

Posted: Wed Sep 15, 2021 8:56 am
by mbell
Hi all,

Does anyone know of a clever way to set up pins from a config file ?

Id like to create a generic firmware that is able to load a config file to set up pins with pyb classes.

THanks in advance

Re: Any clever ways to set up pins

Posted: Wed Sep 15, 2021 9:48 am
by davef
I would hesitate to call it clever but maybe in a file called config.py

Code: Select all

ow1 = onewire.OneWire(Pin(15)) #  create a OneWire bus on GPIO15
ds1 = ds18x20.DS18X20(ow1) #  room temp
ow2 = onewire.OneWire(Pin(14)) #  create a OneWire bus on GPIO14
ds2 = ds18x20.DS18X20(ow2) #  hot water tank temp
adc1_ch4 = ADC(Pin(32)) #  create ADC object on ADC pin 32
adc1_ch4.atten(ADC.ATTN_11DB) #  set 11dB input attenuation (voltage range roughly 0.0v - 3.6v)
adc1_ch4.width(ADC.WIDTH_12BIT) #  set 12 bit return values (returned range 0-4095)
pin27 = Pin(27, Pin.OUT) #  LED drive pin
pin26 = Pin(26, Pin.IN, Pin.PULL_UP) #  intrusion alarm input pin
pin25 = Pin(25, Pin.IN, Pin.PULL_UP) #  mains failure alarm input pin
pin33 = Pin(33, Pin.OUT) #  siren drive pin
then in your main program:

Code: Select all

import config

#  heartbeat, this also slows the loop down
    pin27.on()
    utime.sleep_ms(250)
    pin27.off()
    utime.sleep_ms(250)
    
#  1 wire conversions
    ds1.convert_temp()
    ds2.convert_temp()
    etc
    

Re: Any clever ways to set up pins

Posted: Wed Sep 15, 2021 1:54 pm
by mbell
Ah so you would define all in a separate .py file.

I was hoping to try a human readable something like the following:

GP5=ToggleSwitch,"TABLE SWITCH", 5, Pin.OUT, Pin.PULL_UP

create variable called GP5 that is an instance of ToggleSwitch with the following values as its constructor.

maybes im just complicating things

Re: Any clever ways to set up pins

Posted: Wed Sep 15, 2021 7:49 pm
by balance
Hi together,

take a look at my answer to mbells question in Pi Pico thread:
viewtopic.php?f=21&t=11127

@Moderator: Maybe you can merge both threads to one. Thank you.

Re: Any clever ways to set up pins

Posted: Sat Sep 25, 2021 11:40 pm
by CarlFK
I have done 'this' but I am hesitant to say it is what you want.
https://github.com/evezor/Edge_Boards has the code that sets up pins:

Code: Select all

    {
      "name": "button_0",
      "type": "button",
      "pin": "D11",
      "old": null
    },
https://github.com/evezor/Edge_Boards/b ... son#L7-L12

Code: Select all

    if parameter['type'] == "button":
                self.pins[parameter['name']] = \
                    Pin(parameter['pin'], Pin.IN, Pin.PULL_UP)

            elif parameter['type'] == "adc":
                self.pins[parameter['name']] = \
                    pyb.ADC(parameter['pin'])

            elif parameter['type'] == "led":
                pin = Pin(parameter['pin'], Pin.OUT)
                self.pins[parameter['name']] = pin

            elif parameter['type'] == "pwm":
                pin = Pin(parameter['pin'], Pin.OUT)
                tim = Timer(3, freq=1000)
                self.pins[parameter['name']] = \
                        tim.channel(2, Timer.PWM, pin=pin )

            elif parameter['type'] == "neo":
                pin = Pin(parameter['pin'], Pin.OUT)
                self.pins[parameter['name']] = pin
https://github.com/evezor/Edge_Boards/b ... py#L33-L53

I think that is about what you are looking for.

I'm not super happy about the code I wrote. The lines are fine, but the overall abstractions... It works, but this project stalled and if it ever gets traction I expect to have to fill in some holes, like the way timers are handled.

We kinda got lost when it came to timers.

I would say the problem was: I was writing code that knew what was connected to the pin, like "button" and "led" - I should have put that code somewhere else and focused on just what an mcp pin does.

also the json file has similar issues: it is for managing what is connected to the pin, like should the led be set to on, and has its state changed (that is what "old" is for.)

If you want to know more about the system, start with
http://ide.evezor.com drag and drop and connect some pins, then hit Export JSON and see the config file that drives the code.