User avatar
sn4k3
Posts: 39
Joined: Tue Sep 30, 2014 3:50 pm
Contact: Website

[Library] FakeRPi

Tue Sep 30, 2014 4:01 pm

Hello,

I have made a small library to help users develop for Raspberry Pi on Windows or other systems that don't have RPi.GPIO libraries. It implements RPi.GPIO functions and return None values.
When we are developing on some IDE's, they will verify the code syntax and consequently report many errors as the library is not present on include paths. That can be annoying for the user, as they see a lot of errors everywhere and some red lines.
With FakeRPi you can fix that and also benefits from code completion and some utilities.
Also people who like to mantain code in windows and deploy/sync with Pi benefits from this.
Note: This is not an emulator of Rasberry Pi!

Importing

Importing library Python >= 2.7 & Python >= 3.1

Code: Select all

import importlib.util
try:
    importlib.util.find_spec('RPi.GPIO')
    import RPi.GPIO as GPIO
except ImportError:
    """
    import FakeRPi.GPIO as GPIO
    OR
    import FakeRPi.RPiO as RPiO
    """

    import FakeRPi.GPIO as GPIO

# Do your code here
Importing library Python < 2.7 & Python < 3.4 OR Python >= 2.7 & Python >= 3.1 (Deprecated)

Code: Select all

import imp
try:
    imp.find_module('RPi.GPIO')
    import RPi.GPIO as GPIO
except ImportError:
    """
    import FakeRPi.GPIO as GPIO
    OR
    import FakeRPi.RPiO as RPiO
    """

    import FakeRPi.GPIO as GPIO

# Do your code here
Examples:

Code: Select all

import importlib.util
try:
    importlib.util.find_spec('RPi.GPIO')
    import RPi.GPIO as GPIO
except ImportError:
    """
    import FakeRPi.GPIO as GPIO
    OR
    import FakeRPi.RPiO as RPiO
    """

    import FakeRPi.GPIO as GPIO


GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
GPIO.setup(22, GPIO.IN)

GPIO.output(18, GPIO.HIGH)

if GPIO.input(22):
    print("Pin 2 is HIGH")
else:
    print("Pin 2 is LOW")

GPIO.cleanup()
Utilities:

Code: Select all

import importlib.util
try:
    importlib.util.find_spec('RPi.GPIO')
    import RPi.GPIO as GPIO
except ImportError:
    """
    import FakeRPi.GPIO as GPIO
    OR
    import FakeRPi.RPiO as RPiO
    """

    import FakeRPi.GPIO as GPIO


import FakeRPi.Utilities as Utilities

Utilities.set_default_pintype(Utilities.PIN_TYPE_BOARD)

pin1 = Utilities.get_pin(Utilities.PIN_GPIO_GEN_1)
pin2 = Utilities.get_pin(Utilities.PIN_GPIO_GEN_2)
pin3 = Utilities.get_pin(Utilities.PIN_GPIO_GEN_3)
pin4 = Utilities.get_pin(Utilities.PIN_GPIO_GEN_4)
pin5 = Utilities.get_pin(Utilities.PIN_GPIO_GEN_5)
pin6 = Utilities.get_pin(Utilities.PIN_GPIO_GEN_6)
pin7 = Utilities.get_pin(Utilities.PIN_GPIO_GEN_7)
sda1 = Utilities.get_pin(Utilities.PIN_GPIO_02_SDA1_I2C)

GPIO.setmode(RPiO.BOARD)
GPIO.setup(pin1, GPIO.OUTPUT)
GPIO.setup(pin1, GPIO.OUTPUT)
GPIO.setup(pin2, GPIO.INPUT)
GPIO.setup(pin3, GPIO.INPUT)
GPIO.setup(pin4, GPIO.OUTPUT)
GPIO.setup(pin5, GPIO.OUTPUT)
GPIO.setup(pin6, GPIO.INPUT)
GPIO.setup(pin7, GPIO.OUTPUT)

print(GPIO.input(pin6))
print(Utilities.PIN_GPIO_GEN_6 == Utilities.PIN_GPIO_25_GEN_6 == Utilities.PIN_GPIO_25)  # Must be true
Project URL: https://github.com/sn4k3/FakeRPi
Last edited by sn4k3 on Mon Oct 06, 2014 12:56 am, edited 4 times in total.

User avatar
paddyg
Posts: 2541
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: [Library] FakeRPI

Tue Sep 30, 2014 9:06 pm

As people might be developing on linux or mac would it be better to put the import RPi.GPIO in a try/except rather than testing for platform? I understand that using try for things you expect to cause errors is 'pythonic' (I'm not really happy with it myself but probably old fashioned) alternatively look for something that narrows the platform down to the pi i.e. ctypes.util.find_library(..so used by gpio etc..)
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

User avatar
sn4k3
Posts: 39
Joined: Tue Sep 30, 2014 3:50 pm
Contact: Website

Re: [Library] FakeRPI

Tue Sep 30, 2014 9:52 pm

paddyg wrote:As people might be developing on linux or mac would it be better to put the import RPi.GPIO in a try/except rather than testing for platform? I understand that using try for things you expect to cause errors is 'pythonic' (I'm not really happy with it myself but probably old fashioned) alternatively look for something that narrows the platform down to the pi i.e. ctypes.util.find_library(..so used by gpio etc..)
In that case they can use the system 'mac' or 'linux' or controlling it with a custom varaible, its more a personal choose.
Personal i don't like the try/cath alternative because it will throw a excepetion and can generate some errors as you said, but is not that bad since script is only to run on Raspberry Pi, so it always have the RPI libs.

This FakeRPI is for people like me who use Windows and a good IDE with code completion, that speed up build process and prevent errors. So i need a alternative for RPI to not trigger errors on IDE. Also i don't like to work on two different environments, so i keep code on my main computer and i sync with Raspberry. Easy and fast :)

User avatar
paddyg
Posts: 2541
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: [Library] FakeRPI

Wed Oct 01, 2014 9:14 am

@sn4k3, it's a good idea and I know it's been recommended on this forum before so sensible to have some standard code that 'people' can just use. That's why you posted it here after all!

I too do much of my pi coding on this laptop for similar reasons to you but I find it's preferable to do it in linux however sys.platform won't differentiate the laptop from the pi so I would have to do something more complicated so the try method may be preferable.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

User avatar
sn4k3
Posts: 39
Joined: Tue Sep 30, 2014 3:50 pm
Contact: Website

Re: [Library] FakeRPI

Wed Oct 01, 2014 1:31 pm

paddyg wrote:@sn4k3, it's a good idea and I know it's been recommended on this forum before so sensible to have some standard code that 'people' can just use. That's why you posted it here after all!

I too do much of my pi coding on this laptop for similar reasons to you but I find it's preferable to do it in linux however sys.platform won't differentiate the laptop from the pi so I would have to do something more complicated so the try method may be preferable.
Yes i understand, thats why i said its a personal choose. Also i have give the freedom of people control the imports so you can use whatever fit you best :)

You can use try/cath, it will not hurt your code because you only run your script on your laptop for interpreter check the errors, like i do or maybe some debuging with commands sent via input.

Thanks for your support, i will add that alternative in code documentation :)

User avatar
sn4k3
Posts: 39
Joined: Tue Sep 30, 2014 3:50 pm
Contact: Website

Re: [Library] FakeRPI

Wed Oct 01, 2014 2:04 pm

I have updated the readme to use importlib.util for check the RPI.GPIO module, otherwise FakeRPI.GPIO will be imported

User avatar
sn4k3
Posts: 39
Joined: Tue Sep 30, 2014 3:50 pm
Contact: Website

Re: [Library] FakeRPi

Mon Oct 06, 2014 12:57 am

New update:

- Fixed RPI typo to RPi

Return to “Python”