Skip to content

Commit 7da0dc3

Browse files
authored
Merge pull request #11 from chibiegg/feat/spi_gpio
Supports SPI with Raspberry Pi GPIO
2 parents 88bbca0 + 2a9f640 commit 7da0dc3

File tree

2 files changed

+63
-4
lines changed

2 files changed

+63
-4
lines changed

README.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
This library contains two functions.
88
One is to connect to Sakura Communication Modules (for Hardware).
9-
Onother one is to connect to Platform API in https://api.sakura.io/ (for Service).
9+
Another one is to connect to Platform API in https://api.sakura.io/ (for Service).
1010

1111

1212
## Documentation
@@ -29,12 +29,10 @@ It currently supports I2C (SMBus) ONLY, and tested with Raspberry Pi.
2929

3030
* Python >= 3.4
3131
* python3-smbus (for I2C)
32-
32+
* python3-rpi.gpio (for GPIO on Raspberry Pi)
3333

3434
### Install
3535

36-
37-
3836
```bash
3937
# From PyPi
4038
sudo pip3 install sakuraio
@@ -44,13 +42,25 @@ sudo pip3 install -e git+https://github.com/sakuraio/python-sakuraio.git#egg=sak
4442

4543
### Example
4644

45+
#### I2C (SMBus)
46+
4747
```python
4848
from sakuraio.hardware.rpi import SakuraIOSMBus
4949

5050
sakuraio = SakuraIOSMBus()
5151
print(sakuraio.get_unique_id())
5252
```
5353

54+
#### SPI (GPIO)
55+
56+
```python
57+
from sakuraio.hardware.rpi import SakuraIOGPIO
58+
59+
sakuraio = SakuraIOGPIO()
60+
print(sakuraio.get_unique_id())
61+
```
62+
63+
5464
#### output
5565

5666
```

sakuraio/hardware/rpi/__init__.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,55 @@ def recv_byte(self):
2626
return value
2727

2828

29+
class SakuraIOGPIO(SakuraIOBase):
30+
31+
def __init__(self, miso=9, mosi=10, clk=11, cs=8):
32+
from RPi import GPIO
33+
self.GPIO = GPIO
34+
self.miso = miso
35+
self.mosi = mosi
36+
self.clk = clk
37+
self.cs = cs
38+
GPIO.setmode(GPIO.BCM)
39+
GPIO.setup(self.miso, GPIO.IN)
40+
GPIO.setup(self.mosi, GPIO.OUT)
41+
GPIO.setup(self.clk, GPIO.OUT)
42+
GPIO.setup(self.cs, GPIO.OUT)
43+
44+
self.GPIO.output(self.cs, self.GPIO.HIGH)
45+
self.GPIO.output(self.clk, self.GPIO.LOW)
46+
47+
def start(self, write=True):
48+
self.GPIO.output(self.cs, self.GPIO.LOW)
49+
50+
def end(self):
51+
self.GPIO.output(self.cs, self.GPIO.HIGH)
52+
53+
def send_byte(self, value):
54+
ret = 0x00
55+
for bit in range(8):
56+
57+
if value & 0x80:
58+
self.GPIO.output(self.mosi, self.GPIO.HIGH)
59+
else:
60+
self.GPIO.output(self.mosi, self.GPIO.LOW)
61+
62+
self.GPIO.output(self.clk, self.GPIO.HIGH)
63+
64+
ret <<= 1
65+
if self.GPIO.input(self.miso):
66+
ret |= 0x01
67+
68+
self.GPIO.output(self.clk, self.GPIO.LOW)
69+
70+
value <<= 1
71+
72+
return ret
73+
74+
def recv_byte(self):
75+
return self.send_byte(0x00)
76+
77+
2978
class SakuraIOSPI(SakuraIOBase):
3079
def __init__(self):
3180
raise NotImplementedError()

0 commit comments

Comments
 (0)