Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added example to instantiate _GPIO in order to setup non-random input bit #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
19 changes: 19 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down