Skip to content

Port changes to the number() routine in the micropython library to _number() #53

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

Merged
merged 9 commits into from
Jan 27, 2020
73 changes: 59 additions & 14 deletions adafruit_ht16k33/segments.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"""

from time import sleep
from adafruit_ht16k33.ht16k33 import HT16K33
#from adafruit_ht16k33.ht16k33 import HT16K33
Copy link
Collaborator

Choose a reason for hiding this comment

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

Just a minor nitpick, can we remove this commented out line please?

Copy link
Author

Choose a reason for hiding this comment

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

Done! I would have nitpicked that too.

from adafruit_ht16k33.matrix import HT16K33

__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_HT16K33.git"
Expand Down Expand Up @@ -151,12 +152,15 @@

class Seg14x4(HT16K33):
"""Alpha-numeric, 14-segment display."""
def print(self, value):
# If True, debugging code will be executed
debug = False

def print(self, value, decimal=0):
"""Print the value to the display."""
if isinstance(value, (str)):
self._text(value)
elif isinstance(value, (int, float)):
self._number(value)
self._number(value, decimal)
else:
raise ValueError('Unsupported display value type: {}'.format(type(value)))
if self._auto_write:
Expand Down Expand Up @@ -211,21 +215,62 @@ def _text(self, text):
for character in text:
self._push(character)

def _number(self, number):
"""Display the specified decimal number."""
'''
Display a floating point or integer number on the Adafruit HT16K33 based displays

Param: number - The floating point or integer number to be displayed, which must be
in the range 0 (zero) to 9999 for integers and floating point or integer numbers
and between 0.0 and 999.0 or 99.00 or 9.000 for floating point numbers.
Param: decimal - The number of decimal places for a floating point number if decimal
is greater than zero, or the input number is an integer if decimal is zero.

Returns: The output text string to be displayed.'''
def _number(self, number, decimal=0):
auto_write = self._auto_write
self._auto_write = False
string = "{}".format(number)
if len(string) > 4:
if string.find('.') > 4:
raise ValueError("Overflow")
self.fill(False)
places = 4
if '.' in string:
places += 1
self._text(string[:places])
stnum = str(number)
dot = stnum.find('.')

if ((len(stnum) > 5) or ((len(stnum) > 4) and (dot < 0))):
raise ValueError("Input overflow - {0} is too large for the display!".format(number))

if dot < 0:
# No decimal point (Integer)
places = len(stnum)
else:
places = len(stnum[:dot])

if self.debug:
print("(1) number = {0}, places = {1}, decimal = {2}, dot = {3}, stnum = '{4}'".format(number, places, decimal, dot, stnum))

if ((places <= 0) and (decimal > 0)):
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is failing pylint because of the parenthesis and variable > 0 can be shortened to variable. I'd suggest changing this line to:
if places <= 0 and decimal:

Copy link
Author

Choose a reason for hiding this comment

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

Same comment on zero checks.

self.fill(False)
places = 4

if '.' in stnum:
places += 1

if self.debug:
print("(2) places = {0}, dot = {1}, decimal = {2}, stnum = '{3}'".format(places, dot, decimal, stnum))

# Set decimal places, if number of decimal places is specified (decimal > 0)
if ((places > 0) and (decimal > 0) and (dot > 0) and (len(stnum[places:]) > decimal)):
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is failing pylint because of all the parenthesis and variable > 0 can be shortened to variable. I'd suggest changing this line to:
if places and decimal and dot and len(stnum[places:]) > decimal:

Copy link
Author

Choose a reason for hiding this comment

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

[code]if places and decimal and dot will be true as long as one of them is not zero. This is true for Python 3.7.3. I should be able to remove the parentheses without changing the meaning.

txt = stnum[:dot + decimal + 1]
elif places > 0:
txt = stnum[:places]

if self.debug:
print("(3) places = {0}, stnum = '{1}', decimal = {2}, txt = '{3}'".format(places, stnum, decimal, txt))
print()

if len(txt) > 5:
raise ValueError("Output string ('{0}') is too long!".format(txt))

self._text(txt)
self._auto_write = auto_write

return txt

def set_digit_raw(self, index, bitmask):
"""Set digit at position to raw bitmask value. Position should be a value
of 0 to 3 with 0 being the left most character on the display.
Expand Down