diff --git a/example.py b/example.py index 030d924..211ff3e 100755 --- a/example.py +++ b/example.py @@ -8,9 +8,11 @@ import fake_rpi sys.modules['RPi'] = fake_rpi.RPi +sys.modules['RPi.GPIO'] = fake_rpi.RPi.GPIO sys.modules['smbus'] = fake_rpi.smbus # Then keep the transparent import everywhere in the application and dependencies +import RPi import RPi.GPIO as GPIO import smbus @@ -43,3 +45,16 @@ def read_byte_data(self, a, b): i2c = MyBus() i2c.read_byte_data(1, 2) i2c.read_i2c_block_data(1, 2, 3) + + +class MyGPIO(RPi._GPIO): + def setup(self, channel, state, initial=0, pull_up_down=None): + self._inputs[channel] = state + + +GPIO = MyGPIO() +GPIO.setmode(GPIO.BCM) +GPIO.setup(21, 1) # fake GPIO input is set to 1 +GPIO.setup(22, 0) # fake GPIO input is set to 0 +b = GPIO.input(21) +b = GPIO.input(22) \ No newline at end of file diff --git a/readme.md b/readme.md index d50726d..43629d8 100644 --- a/readme.md +++ b/readme.md @@ -109,6 +109,25 @@ sm = MyBus() b = sm.read_byte_data(0x21, 0x32) # read in a byte ``` +What if I need take control of `GPIO` input so that it returns +non-random bit. Ok, then create a child of my `_GPIO` like +below and modify the `setup` method: + +```python +import RPi + +class MyGPIO(RPi._GPIO): + def setup(self, channel, state, initial=0, pull_up_down=None): + self._inputs[channel] = state + +GPIO = MyGPIO() +GPIO.setmode(GPIO.BCM) +GPIO.setup(21, 1) # fake GPIO input is set to 1 +GPIO.setup(22, 1) # fake GPIO input is set to 0 +b = GPIO.input(21) +b = GPIO.input(22) +``` + ### Printing On or Off Here is the output from `example.py` in the `git` repo when the printing