Skip to content

Commit

Permalink
Merge pull request #50 from humrochagf/master
Browse files Browse the repository at this point in the history
Sprint at PythonBrasil 10
  • Loading branch information
ramalho committed Nov 8, 2014
2 parents bcd3745 + f6f68af commit 0ec7b40
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,8 @@ Lucas Vido (Vido)
Paulo R. O. Castro (brk00)
Thiago M. Sanches
Valder Gallo (valdergallo)
Humberto Rocha (humrochagf)
Diego Guimaraes (diegoguimaraes)
Héctor Velarde (hvelarde)
Theo A. Monteiro (theoamonteiro)
Davi Garcia (davivcgarcia)
28 changes: 28 additions & 0 deletions pingo/examples/parts/7seg_demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
""" Seven segment display demo using the Garoa Dojo Shield """

from time import sleep
from pingo import OUT, detect, parts

INTERVAL = 0.3

ard = detect.MyBoard()

display_pins = [ard.pins[i] for i in range(8, 14)] + [7]

for pin in display_pins:
pin.mode = OUT

seg_display = parts.led.SevenSegments(*display_pins)

for num in range(16):
seg_display.digit = num
print('Current digit: {:x}'.format(seg_display.digit))
sleep(INTERVAL)

for i in range(3):
seg_display.off()
sleep(INTERVAL)
seg_display.on()
sleep(INTERVAL)

seg_display.off()
68 changes: 65 additions & 3 deletions pingo/parts/led.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
import time
import threading


class Led(object):
"""A single LED"""

def __init__(self, pin, lit_state=pingo.HIGH):
"""Set lit_state to pingo.LOW to turn on led by bringing cathode to low state.
"""Set lit_state to pingo.LOW to turn on led by bringing
cathode to low state.
:param lit_state: use pingo.HI for anode control, pingo.LOW for cathode control
:param lit_state: use pingo.HI for anode control, pingo.LOW
for cathode control
"""

pin.mode = pingo.OUT
Expand Down Expand Up @@ -89,4 +92,63 @@ def run(self):
time.sleep(self.off_delay)
else:
self.led.off()
self.active = False
self.active = False


DIGIT_MAP = {
0: '1111110',
1: '0110000',
2: '1101101',
3: '1111001',
4: '0110011',
5: '1011011',
6: '1011111',
7: '1110000',
8: '1111111',
9: '1111011',
10: '1110111',
11: '0011111',
12: '1001110',
13: '0111101',
14: '1001111',
15: '1000111'
}


class SevenSegments(object):

def __init__(self, pin_a, pin_b, pin_c, pin_d,
pin_e, pin_f, pin_g, pin_dp=None,
lit_state=pingo.HIGH):
self._leds = [Led(pin_a, lit_state), Led(pin_b, lit_state),
Led(pin_c, lit_state), Led(pin_d, lit_state),
Led(pin_e, lit_state), Led(pin_f, lit_state),
Led(pin_g, lit_state)]

if pin_dp:
self._leds.append(Led(pin_dp, lit_state))

self._digit = 0

def _configure(self, pattern):
for segment, state in zip(self._leds, pattern):
if state == '1':
segment.on()
else:
segment.off()

@property
def digit(self):
return self._digit

@digit.setter
def digit(self, digit):
self._digit = digit
pattern = DIGIT_MAP[digit]
self._configure(pattern)

def on(self):
self.digit = self._digit

def off(self):
self._configure('0'*7)

0 comments on commit 0ec7b40

Please sign in to comment.