Package: fake-rpi
Fake Raspberry PiWhy??I do a lot of development on my Powerbook and I got tired of constantly
creating a fake interface for dev on my laptop and testing on Travis.ci or github workflows.2017 Apr 2:Beta Quality2017 Apr 8:Initialpython3 supportSo, does this simulate everything on a Raspberry Pi?No!Right now
it simulates what I use and need. Over time, more will be added. You are
also welcome to submit pull requests for things I haven't added yet.AdafruitLSM303(accelerometer)nxp_imuadafruit accelerometerGPIOgpio pinspicameracameraRPiPWMsmbusi2cserialnot done yetInstallThe preferred way to install this is:pip install fake_rpiDevelopmentTo submit pull requests for new sensors or fixes, just do:git clone https://github.com/MomsFriendlyRobotCompany/fake_rpi.git
cd fake_rpi
poetry installThen do a pull request.UsageTo fake RPi.GPIO or smbus, this following
code must be executed before your application:# Replace libraries by fake onesimportsysimportfake_rpisys.modules['RPi']=fake_rpi.RPi# Fake RPisys.modules['RPi.GPIO']=fake_rpi.RPi.GPIO# Fake GPIOsys.modules['smbus']=fake_rpi.smbus# Fake smbus (I2C)Then you can keep your usual imports in your application:importRPi.GPIOasGPIOimportsmbusGPIO.setmode(io.BCM)# now use the fake GPIOb=GPIO.input(21)sm=smbus.SMBus(1)# now use the fake smbusb=sm.read_byte_data(0x21,0x32)# read in a byteTurning on/off fake calls logging:fromfake_rpiimporttoggle_print# by default it prints everything to std.errortoggle_print(False)# turn on/off printingBut I needsmbusto return a specific byte for unit testing! Ok, then
create a child of mysmbuslike below and modifyonlythe methods
you need changed:fromfake_rpiimportsmbusfromfake_rpiimportprintfclassMyBus(smbus.SMBus):@printfdefread_byte_data(self,i2c_addr,register):ret=0xffifi2c_addr==0x21:ret=0x55elifi2c_addr==0x25:ret=0x11returnretsm=MyBus()b=sm.read_byte_data(0x21,0x32)# read in a bytePrinting On or OffHere is the output fromexample.pyin thegitrepo when the printing
is toggled on or off:kevin@Logan fake_rpi $ ./example.py
<<< WARNING: using fake raspberry pi interfaces >>>

kevin@Logan fake_rpi $ ./example.py
<<< WARNING: using fake raspberry pi interfaces >>>
fake_rpi.RPi.PWM.__init__()
fake_rpi.RPi.PWM.start(5,)
fake_rpi.smbus.SMBus.__init__(1,)
fake_rpi.smbus.SMBus.write_byte_data(1, 2, 3)
fake_rpi.smbus.SMBus.read_byte_data(1, 2): 21
fake_rpi.smbus.SMBus.close()
__main__.MyBus.__init__()
__main__.MyBus.read_byte_data(1, 2): 72
__main__.MyBus.read_i2c_block_data(1, 2, 3): [90, 90, 90]Change LogDateVer.Notes2020-04-030.7.0additions to gpio and camera2020-02-030.6.3moved to toml and github workflows2019-10-190.6.2fixes from scivision and Rotzbua2019-03-290.6.1bug fix with randint range2017-11-300.6.0bug fix with printing2017-10-230.5.3bug fix with randint2017-09-050.5.1flushing out interfaces2017-07-070.3.0fixed bugs, print statement, and reduced dups2017-04-080.1.0initial python3 setup and support2017-04-020.0.2pushed to pypi with landscape.io fixes2017-04-010.0.1createdMIT LicenseCopyright (c) 2017 Kevin J. WalchkoPermission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.