Skip to content

Commit

Permalink
Merge branch 'some-update'
Browse files Browse the repository at this point in the history
* some-update:
  accept seesaw pins or microcontroller.Pin, add 4 digit example
  • Loading branch information
Neradoc committed May 10, 2023
2 parents 4913855 + cd8a72f commit 835b28f
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 10 deletions.
32 changes: 32 additions & 0 deletions examples/tm1637_display_4digits.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# SPDX-FileCopyrightText: Copyright (c) 2023 Neradoc
#
# SPDX-License-Identifier: Unlicense

"""
4-digits 7-segments display with default order.
"""

import time
import board
from tm1637_display import TM1637Display

display = TM1637Display(board.GP13, board.GP12, length=4, digit_order=(0, 1, 2, 3))

message = " 3141592653589793 "

while True:
for i in range(5):
display.print("0000")
time.sleep(0.1)
display.print(" ")
time.sleep(0.1)

for pos in range(len(message) - 4):
display.print(message[pos : pos + 4])
time.sleep(0.2)

for i in range(0, 10000, 7):
display.print(f"{i:4}")
display.print(9999)

time.sleep(0.2)
29 changes: 29 additions & 0 deletions examples/tm1637_display_6digits.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# SPDX-FileCopyrightText: Copyright (c) 2023 Neradoc
#
# SPDX-License-Identifier: Unlicense

import time
import board
from tm1637_display import TM1637Display

display = TM1637Display(board.SCL, board.SDA, length=6, digit_order=(2, 1, 0, 5, 4, 3))
DELAY = 0.5

message = " 3141592653589793 "

while True:
for i in range(5):
display.print("000000")
time.sleep(0.1)
display.print(" ")
time.sleep(0.1)

for pos in range(len(message) - 6):
display.print(message[pos : pos + 6])
time.sleep(0.2)

for i in range(0, 1_000_000, 7):
display.print(f"{i:4}")
display.print(9999)

time.sleep(0.2)
6 changes: 2 additions & 4 deletions examples/tm1637_display_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
import board
from tm1637_display import TM1637Display

display = TM1637Display(board.SCL, board.SDA, length=6)
display = TM1637Display(board.SCL, board.SDA)
DELAY = 0.5

display.print("HELLO .")
time.sleep(DELAY)
display.print("PQ-SUJ")
display.print("HELLO")
time.sleep(DELAY)

for i in range(7):
Expand Down
Binary file modified images/photo.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 15 additions & 6 deletions tm1637_display.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@

import digitalio
import microcontroller
from microcontroller import Pin
from micropython import const

try:
from typing import Union
from microcontroller import Pin
except ImportError:
pass

Expand Down Expand Up @@ -120,7 +120,7 @@ def __init__(
brightness: int = 7,
):
if not digit_order:
self.digit_order = (2, 1, 0, 5, 4, 3)
self.digit_order = list(range(length))
else:
if len(digit_order) != length:
raise ValueError("digit_order is a list of all digit positions")
Expand All @@ -133,10 +133,17 @@ def __init__(
self.digits = bytearray(length)

# Set the pin direction and default value.
self.clk = digitalio.DigitalInOut(clock)
self.dio = digitalio.DigitalInOut(data)
self.clk.switch_to_output(True)
self.dio.switch_to_output(True)
if isinstance(clock, Pin):
self.clk = digitalio.DigitalInOut(clock)
self.clk.switch_to_output(True)
else:
self.clk = clock

if isinstance(data, Pin):
self.dio = digitalio.DigitalInOut(data)
self.dio.switch_to_output(True)
else:
self.dio = data

def _set_brightness(self, brightness: int, enabled: bool = True):
"""Set the brightness from 0 to 7, and enable light or not"""
Expand Down Expand Up @@ -302,6 +309,8 @@ def _text(self, text: str) -> None:
if letter == ".":
dot = True
continue
if letter.lower() not in letter_to_segment:
raise ValueError(f"Letter {letter} has not matching representation")
self.digits[k] = letter_to_segment[letter.lower()]
if dot:
self.digits[k] |= _DOT_SEGMENT
Expand Down

0 comments on commit 835b28f

Please sign in to comment.