Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Gadgetoid committed Dec 5, 2016
0 parents commit 2ee4b0a
Show file tree
Hide file tree
Showing 16 changed files with 919 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
build/
_build/
*.o
*.so
*.a
*.py[cod]
*.egg-info
dist/
__pycache__
.DS_Store
*.deb
*.dsc
*.build
*.changes
*.orig.*
packaging/*tar.xz
library/debian/
Empty file added library/CHANGELOG.txt
Empty file.
21 changes: 21 additions & 0 deletions library/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2016 Pimoroni Ltd

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
5 changes: 5 additions & 0 deletions library/MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
include CHANGELOG.txt
include LICENSE.txt
include README.txt
include setup.py
include rainbowhat/*
Empty file added library/README.txt
Empty file.
99 changes: 99 additions & 0 deletions library/rainbowhat/HT16K33.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# Copyright (c) 2014 Adafruit Industries
# Author: Tony DiCola
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
from __future__ import division


# Constants
DEFAULT_ADDRESS = 0x70
HT16K33_BLINK_CMD = 0x80
HT16K33_BLINK_DISPLAYON = 0x01
HT16K33_BLINK_OFF = 0x00
HT16K33_BLINK_2HZ = 0x02
HT16K33_BLINK_1HZ = 0x04
HT16K33_BLINK_HALFHZ = 0x06
HT16K33_SYSTEM_SETUP = 0x20
HT16K33_OSCILLATOR = 0x01
HT16K33_CMD_BRIGHTNESS = 0xE0


class HT16K33(object):
"""Driver for interfacing with a Holtek HT16K33 16x8 LED driver."""

def __init__(self, i2c, address=DEFAULT_ADDRESS):
"""Create an HT16K33 driver for devie on the specified I2C address
(defaults to 0x70) and I2C bus (defaults to platform specific bus).
"""
self._i2c_addr = address
self._device = i2c
self.buffer = bytearray([0]*16)

def begin(self):
"""Initialize driver with LEDs enabled and all turned off."""
# Turn on the oscillator.
self._device.write_i2c_block_data(self._i2c_addr, HT16K33_SYSTEM_SETUP | HT16K33_OSCILLATOR, [])
# Turn display on with no blinking.
self.set_blink(HT16K33_BLINK_OFF)
# Set display to full brightness.
self.set_brightness(15)

def set_blink(self, frequency):
"""Blink display at specified frequency. Note that frequency must be a
value allowed by the HT16K33, specifically one of: HT16K33_BLINK_OFF,
HT16K33_BLINK_2HZ, HT16K33_BLINK_1HZ, or HT16K33_BLINK_HALFHZ.
"""
if frequency not in [HT16K33_BLINK_OFF, HT16K33_BLINK_2HZ,
HT16K33_BLINK_1HZ, HT16K33_BLINK_HALFHZ]:
raise ValueError('Frequency must be one of HT16K33_BLINK_OFF, HT16K33_BLINK_2HZ, HT16K33_BLINK_1HZ, or HT16K33_BLINK_HALFHZ.')
self._device.write_i2c_block_data(self._i2c_addr, HT16K33_BLINK_CMD | HT16K33_BLINK_DISPLAYON | frequency, [])

def set_brightness(self, brightness):
"""Set brightness of entire display to specified value (16 levels, from
0 to 15).
"""
if brightness < 0 or brightness > 15:
raise ValueError('Brightness must be a value of 0 to 15.')
self._device.write_i2c_block_data(self._i2c_addr, HT16K33_CMD_BRIGHTNESS | brightness, [])

def set_led(self, led, value):
"""Sets specified LED (value of 0 to 127) to the specified value, 0/False
for off and 1 (or any True/non-zero value) for on.
"""
if led < 0 or led > 127:
raise ValueError('LED must be value of 0 to 127.')
# Calculate position in byte buffer and bit offset of desired LED.
pos = led // 8
offset = led % 8
if not value:
# Turn off the specified LED (set bit to zero).
self.buffer[pos] &= ~(1 << offset)
else:
# Turn on the speciried LED (set bit to one).
self.buffer[pos] |= (1 << offset)

def write_display(self):
"""Write display buffer to display hardware."""
for i, value in enumerate(self.buffer):
self._device.write_byte_data(self._i2c_addr, i, value)

def clear(self):
"""Clear contents of display buffer."""
for i, value in enumerate(self.buffer):
self.buffer[i] = 0
13 changes: 13 additions & 0 deletions library/rainbowhat/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from .i2c_bus import bus
from . import apa102
from .bmp280 import bmp280
from .alphanum4 import AlphaNum4
from . import touch
from . import lights

display = AlphaNum4(i2c=bus)
display.begin()

weather = bmp280(bus)
rainbow = apa102
buzzer = None
217 changes: 217 additions & 0 deletions library/rainbowhat/alphanum4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
# Copyright (c) 2014 Adafruit Industries
# Author: Tony DiCola
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
from . import HT16K33


# Digit value to bitmask mapping:
DIGIT_VALUES = {
' ': 0b0000000000000000,
'!': 0b0000000000000110,
'"': 0b0000001000100000,
'#': 0b0001001011001110,
'$': 0b0001001011101101,
'%': 0b0000110000100100,
'&': 0b0010001101011101,
'\'': 0b0000010000000000,
'(': 0b0010010000000000,
')': 0b0000100100000000,
'*': 0b0011111111000000,
'+': 0b0001001011000000,
',': 0b0000100000000000,
'-': 0b0000000011000000,
'.': 0b0000000000000000,
'/': 0b0000110000000000,
'0': 0b0000110000111111,
'1': 0b0000000000000110,
'2': 0b0000000011011011,
'3': 0b0000000010001111,
'4': 0b0000000011100110,
'5': 0b0010000001101001,
'6': 0b0000000011111101,
'7': 0b0000000000000111,
'8': 0b0000000011111111,
'9': 0b0000000011101111,
':': 0b0001001000000000,
';': 0b0000101000000000,
'<': 0b0010010000000000,
'=': 0b0000000011001000,
'>': 0b0000100100000000,
'?': 0b0001000010000011,
'@': 0b0000001010111011,
'A': 0b0000000011110111,
'B': 0b0001001010001111,
'C': 0b0000000000111001,
'D': 0b0001001000001111,
'E': 0b0000000011111001,
'F': 0b0000000001110001,
'G': 0b0000000010111101,
'H': 0b0000000011110110,
'I': 0b0001001000000000,
'J': 0b0000000000011110,
'K': 0b0010010001110000,
'L': 0b0000000000111000,
'M': 0b0000010100110110,
'N': 0b0010000100110110,
'O': 0b0000000000111111,
'P': 0b0000000011110011,
'Q': 0b0010000000111111,
'R': 0b0010000011110011,
'S': 0b0000000011101101,
'T': 0b0001001000000001,
'U': 0b0000000000111110,
'V': 0b0000110000110000,
'W': 0b0010100000110110,
'X': 0b0010110100000000,
'Y': 0b0001010100000000,
'Z': 0b0000110000001001,
'[': 0b0000000000111001,
'\\': 0b0010000100000000,
']': 0b0000000000001111,
'^': 0b0000110000000011,
'_': 0b0000000000001000,
'`': 0b0000000100000000,
'a': 0b0001000001011000,
'b': 0b0010000001111000,
'c': 0b0000000011011000,
'd': 0b0000100010001110,
'e': 0b0000100001011000,
'f': 0b0000000001110001,
'g': 0b0000010010001110,
'h': 0b0001000001110000,
'i': 0b0001000000000000,
'j': 0b0000000000001110,
'k': 0b0011011000000000,
'l': 0b0000000000110000,
'm': 0b0001000011010100,
'n': 0b0001000001010000,
'o': 0b0000000011011100,
'p': 0b0000000101110000,
'q': 0b0000010010000110,
'r': 0b0000000001010000,
's': 0b0010000010001000,
't': 0b0000000001111000,
'u': 0b0000000000011100,
'v': 0b0010000000000100,
'w': 0b0010100000010100,
'x': 0b0010100011000000,
'y': 0b0010000000001100,
'z': 0b0000100001001000,
'{': 0b0000100101001001,
'|': 0b0001001000000000,
'}': 0b0010010010001001,
'~': 0b0000010100100000
}


class AlphaNum4(HT16K33.HT16K33):
"""Alphanumeric 14 segment LED backpack display."""

def __init__(self, **kwargs):
"""Initialize display. All arguments will be passed to the HT16K33 class
initializer, including optional I2C address and bus number parameters.
"""
super(AlphaNum4, self).__init__(**kwargs)

def set_digit_raw(self, pos, bitmask):
"""Set digit at position to raw bitmask value. Position should be a value
of 0 to 3 with 0 being the left most digit on the display."""
if pos < 0 or pos > 3:
# Ignore out of bounds digits.
return
# Set the digit bitmask value at the appropriate position.
# Also set bit 7 (decimal point) if decimal is True.
self.buffer[pos*2] = bitmask & 0xFF
self.buffer[pos*2+1] = (bitmask >> 8) & 0xFF

def set_decimal(self, pos, decimal):
"""Turn decimal point on or off at provided position. Position should be
a value 0 to 3 with 0 being the left most digit on the display. Decimal
should be True to turn on the decimal point and False to turn it off.
"""
if pos < 0 or pos > 3:
# Ignore out of bounds digits.
return
# Set bit 14 (decimal point) based on provided value.
if decimal:
self.buffer[pos*2+1] |= (1 << 6)
else:
self.buffer[pos*2+1] &= ~(1 << 6)

def set_digit(self, pos, digit, decimal=False):
"""Set digit at position to provided value. Position should be a value
of 0 to 3 with 0 being the left most digit on the display. Digit should
be any ASCII value 32-127 (printable ASCII).
"""
self.set_digit_raw(pos, DIGIT_VALUES.get(str(digit), 0x00))
if decimal:
self.set_decimal(pos, True)

def print_str(self, value, justify_right=True):
"""Print a 4 character long string of values to the display. Characters
in the string should be any ASCII value 32 to 127 (printable ASCII).
"""
# Calculcate starting position of digits based on justification.
pos = (4-len(value)) if justify_right else 0
# Go through each character and print it on the display.
for i, ch in enumerate(value):
self.set_digit(i+pos, ch)

def print_number_str(self, value, justify_right=True):
"""Print a 4 character long string of numeric values to the display. This
function is similar to print_str but will interpret periods not as
characters but as decimal points associated with the previous character.
"""
# Calculate length of value without decimals.
length = len(value.translate(None, '.'))
# Error if value without decimals is longer than 4 characters.
if length > 4:
self.print_str('----')
return
# Calculcate starting position of digits based on justification.
pos = (4-length) if justify_right else 0
# Go through each character and print it on the display.
for i, ch in enumerate(value):
if ch == '.':
# Print decimal points on the previous digit.
self.set_decimal(pos-1, True)
else:
self.set_digit(pos, ch)
pos += 1

def print_float(self, value, decimal_digits=2, justify_right=True):
"""Print a numeric value to the display. If value is negative
it will be printed with a leading minus sign. Decimal digits is the
desired number of digits after the decimal point.
"""
format_string = '{{0:0.{0}F}}'.format(decimal_digits)
self.print_number_str(format_string.format(value), justify_right)

def print_hex(self, value, justify_right=True):
"""Print a numeric value in hexadecimal. Value should be from 0 to FFFF.
"""
if value < 0 or value > 0xFFFF:
# Ignore out of range values.
return
self.print_str('{0:X}'.format(value), justify_right)

def show(self):
self.write_display()

Loading

0 comments on commit 2ee4b0a

Please sign in to comment.