hippy
Posts: 14344
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

'RPi.GPIO', 'gpiozero' library shims for the Pico

Fri Jul 16, 2021 12:39 pm

Here is a triplet of lightweight library shims which may be useful for those using GPIO who want to have their Raspberry Pi Python code run 'as is' on a Pico using MicroPython, or want to run their MicroPython code for a Pico on a Raspberry Pi.

Note that pin numbers used will have to be altered for any code written for one when moved to the other.

This is why it is always recommended to create named constants for pins used. Define those at the top of the program and then only use those names in the rest of the code. Moving code to a different platform will only require those defintiions to be changed, no need to go through the entire code to adjust pin numbers.

These shims only offer basic functionality, digital pin support, but that should be good enough for most people who would be using these shims, those who only want to use button and LED's.

The shims have no error checking to make them lightweight and easier to understand but, if pin numbers are valid, and the code runs correctly, it should also work when moved to the other platform.

I am not planning to turn these shims into '100% compatibility layers' but I will update the shims here if I add support for other things or anyone in the community provides that.

Please note that I am not particularly recommending the use of these shims; they are purely a means to an end. In fact I'm mostly putting them here so code which has been scattered throughout the forum becomes curated, so we have a thread to point people to if they need such things.

I believe al the code is correct but some issues may have crept in while corralling what I have, so E&OE. I will test it all when I have hardware to hand and correct any errors I find, as well as making parameter names match what the originals use.

History

2021-07-16 - Initial release
Last edited by hippy on Fri Jul 16, 2021 12:44 pm, edited 2 times in total.

hippy
Posts: 14344
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: 'RPI.GPIO', 'gpiozero' library shims for the Pico

Fri Jul 16, 2021 12:40 pm

Running 'RPi.GPIO' code on a Pico

This is the library shim for running 'RPi.GPIO' code for a Raspberry Pi on a Pico using MicroPython.

GPIO.py saved in the /RPi directory of the SD Card

Code: Select all

from machine import Pin

BCM, BOARD                 = 0, 1
OUT, IN                    = 0, 1
LOW, HIGH                  = 0, 1
PUD_DOWN, PUD_UP, PUD_NONE = 0, 1, -1

gpio = [ None ] * 30

def setwarnings(giveWarnings) : pass
def cleanup()                 : pass
def setmode(mode)             : pass

def setup(pin, mode, pullup=PUD_NONE):
    if   mode   == OUT      : gpio[pin] = Pin(pin, Pin.OUT)
    elif pullup == PUD_UP   : gpio[pin] = Pin(pin, Pin.IN, Pin.PULL_UP)
    elif pullup == PUD_DOWN : gpio[pin] = Pin(pin, Pin.IN, Pin.PULL_DOWN)
    else                    : gpio[pin] = Pin(pin, Pin.IN)
  
def output(pin, level):
    if level : gpio[pin].high()
    else     : gpio[pin].low()

def input(pin):
    return gpio[pin].value()
Test code to run on the Pico under MicroPython -

Code: Select all

import RPi.GPIO as GPIO

BTN_PIN = ? # Pin number of button
LED_PIN = ? # Pin number of LED

GPIO.setwarnings(False)
GPIO.cleanup()
GPIO.setmode(GPIO.BCM)

GPIO.setup(BTN_PIN, GPIO.IN)
GPIO.setup(LED_PIN, GPIO.OUT)

while True:
    if GPIO.input(BTN_PIN) == 0 : GPIO.output(LED_PIN, 0)
    else                        : GPIO.output(LED_PIN, 1)
History

2021-07-16 - Initial release

hippy
Posts: 14344
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: 'RPI.GPIO', 'gpiozero' library shims for the Pico

Fri Jul 16, 2021 12:40 pm

Running 'gpiozero' code on a Pico

This is the library shim for running 'gpiozero' code for a Raspberry Pi on a Pico using MicroPython.

gpiozero.py saved in the root directory of the SD Card

Code: Select all

from machine import Pin

class LED():
    def __init__(self, pin):
        self.pin = pin
        self.led = Pin(pin, Pin.OUT)
        self.off()
    def on(self):
        self.led.on()
    def off(self):
        self.led.off()       

class Button():
    btnList = {}
    def __init__(self, pin):
        self.pin = pin
        self.btn = Pin(pin, Pin.IN, Pin.PULL_UP)
        self.state = self.btn.value()
        self.when_pressed  = None
        self.when_released = None
        btnList[pin] = self
    def value(self):
        return self.btn.value()
    def pressed(self):
        if self.when_pressed != None:
            self.when_pressed()
    def released(self):
        if self.when_released != None:
            self.when_released()
    def check(self):
        if self.btn.value() != self.state:
            self.state = self.state ^ 1
            if self.state == 0:
                self.released()
            else:
                self.pressed()
    def pause(listButtons=False):
        if listButtons:
            print(Button.btnList)
        while len(Button.btnList) > 0:
            for btn in Button.btnList:
                Button.btnList[btn].check()

pause = Button.pause()
Test code to run on the Pico under MicroPython -

Code: Select all

import gpiozero

BTN_PIN = ? # Pin number of button
LED_PIN = ? # Pin number of LED

button = Button(BTN_PIN)
led    = LED(LED_PIN)

button.when_pressed  = led.on
button.when_released = led.off

pause()
History

2021-07-16 - Initial release

hippy
Posts: 14344
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: 'RPI.GPIO', 'gpiozero' library shims for the Pico

Fri Jul 16, 2021 12:41 pm

Running MicroPython code on a Pi

This is the library shim for running MicroPython code for a Pico on a Raspberry Pi using Python 2 or 3.

machine.py on the host system

Code: Select all

import RPi.GPIO as GPIO

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)

class Pin():
    OUT, IN                       = 0, 1
    PULL_DOWN, PULL_UP, PULL_NONE = 0, 1, -1
    def __init__(self, pin, mode, pullup=PULL_NONE):
        self.pin = pin
        if   mode   == self.OUT       : GPIO.setup(pin, GPIO.OUT)
        elif pullup == self.PULL_UP   : GPIO.setup(pin, GPIO.IN, GPIO.PUD_UP)
        elif pullup == self.PULL_DOWN : GPIO.setup(pin, GPIO.IN, GPIO.PUD_DOWN)
        else                          : GPIO.setup(pin, GPIO.IN, GPIO.PUD_NONE)
    def high(self)  : GPIO.output(self.pin, GPIO.HIGH)
    def low(self)   : GPIO.output(self.pin, GPIO.LOW)
    def on(self)    : GPIO.output(self.pin, GPIO.HIGH)
    def off(self)   : GPIO.output(self.pin, GPIO.LOW)
    def value(self) : return GPIO.input(self.pin)
Test code to run on the Raspberry Pi under Python -

Code: Select all

from machine import Pin

BTN_PIN = ? # Pin number of button
LED_PIN = ? # Pin number of LED

btn = Pin(BTN_PIN, Pin.IN, Pin.PULL_UP)
led = Pin(LED_PIN, Pin.OUT)

while True:
    if btn.value() == 0 : led.low()
    else                : led.high()
History

2021-07-16 - Initial release

Return to “MicroPython”