Skip to content

SAA1064 and Seven Segment display APIs #41

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

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
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
13 changes: 13 additions & 0 deletions examples/saa1064-two-digits.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import quick2wire.i2c as i2c
from quick2wire.parts.saa1064 import SAA1064, STATIC_MODE
from time import sleep

saa1064 = SAA1064(i2c.I2CMaster(), digits=2)
saa1064.brightness=0b01100000

for y in range(10):
saa1064.digit(0).value(y)
for x in range(10):
saa1064.digit(1).value(x)
saa1064.write()
sleep(0.1)
17 changes: 17 additions & 0 deletions examples/seven_segment_display_with_two_digits.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
__author__ = 'stuartervine'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use a consistent naming convention for programs in the examples directory? Hyphens, not underscores, to separate words and no .py suffix (makes working at the command-line more convenient). Also they should be executable and start with the line "#!/usr/bin/env python3"


from quick2wire import i2c
from quick2wire.parts.saa1064 import SAA1064
from quick2wire.parts.seven_segment_display import SevenSegmentDisplay
from time import sleep

saa1064 = SAA1064(i2c.I2CMaster(), digits=4)
sevenSegmentDisplay=SevenSegmentDisplay(saa1064)

sevenSegmentDisplay.display('1.5')
sleep(1)
sevenSegmentDisplay.display('100R')
sleep(1)

for i in range(9999):
sevenSegmentDisplay.display(i)
44 changes: 1 addition & 43 deletions quick2wire/gpio.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import subprocess
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about moving PinAPI and PinBankAPI out of the gpio module. To fully implement the Pin and PinBank APIs you'll also need constants defined in gpio module (directions for example).

Because in Python you can't define interfaces except by documentation (which I haven't done, I admit), the PinAPI and PinBankAPI are only partial implementations of the interface and things that want to be Pin-like or PinBank-like must implement things that are not defined on PinAPI & PinBankAPI. In fact there's no reason to subclass PinAPI or PinBankAPI if it's easier not to. So, I'd say move them back to gpio. They are modelling digital GPIO pins, and it seems reasonable for them to be in gpio.

from contextlib import contextmanager
from quick2wire.board_revision import revision
from quick2wire.pin import PinAPI, PinBankAPI
from quick2wire.selector import EDGE


Expand All @@ -27,46 +28,6 @@ def gpio_admin(subcommand, pin, pull=None):
PullUp = "pullup"



class PinAPI(object):
def __init__(self, bank, index):
self._bank = bank
self._index = index

@property
def index(self):
return self._index

@property
def bank(self):
return self._bank

def __enter__(self):
self.open()
return self

def __exit__(self, exc_type, exc_value, traceback):
self.close()

value = property(lambda p: p.get(),
lambda p,v: p.set(v),
doc="""The value of the pin: 1 if the pin is high, 0 if the pin is low.""")


class PinBankAPI(object):
def __getitem__(self, n):
if 0 < n < len(self):
raise ValueError("no pin index {n} out of range", n=n)
return self.pin(n)

def write(self):
pass

def read(self):
pass



class Pin(PinAPI):
"""Controls a GPIO pin."""

Expand Down Expand Up @@ -204,9 +165,6 @@ def __str__(self):
index=self.index)





class PinBank(PinBankAPI):
def __init__(self, index_to_soc_fn, count=None):
super(PinBank,self).__init__()
Expand Down
56 changes: 56 additions & 0 deletions quick2wire/parts/fake_i2c.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
__author__ = 'stuart'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've not been bothering with author statements, as everyone has contributed to almost every file.


class FakeI2CMaster:
def __init__(self):
self._requests = []
self._responses = []
self._next_response = 0
self.message_precondition = lambda m: True

def all_messages_must(self, p):
self.message_precondition

def clear(self):
self.__init__()

def transaction(self, *messages):
for m in messages:
self.message_precondition(m)

self._requests.append(messages)
return []

def add_response(self, *messages):
self._responses.append(messages)


@property
def request_count(self):
return len(self._requests)

def request(self, n):
return self._requests[n]

def request_at(self, n):
return I2CRequestWrapper(self._requests[n])

def message(self, message_index):
request = self.request(0)
return I2CMessageWrapper(request[message_index])

class I2CRequestWrapper:
def __init__(self, i2c_request):
self._i2c_request = i2c_request

def message(self, index):
return I2CMessageWrapper(self._i2c_request[index])


class I2CMessageWrapper:
def __init__(self, i2_message):
self._i2c_message = i2_message
self.len = i2_message.len

def byte(self, index):
return self._i2c_message.buf[index][0]

Loading