Last active
December 31, 2024 06:10
-
-
Save lurch/c6965457172a1b89a8bc to your computer and use it in GitHub Desktop.
Sample code to display decimal numbers on a 4-digit 7-segment display
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# code modified, tweaked and tailored from code at | |
# http://raspi.tv/2015/how-to-drive-a-7-segment-display-directly-on-raspberry-pi-in-python | |
import RPi.GPIO as GPIO | |
import random | |
import time | |
NUMBER_OF_DIGITS = 4 | |
def format_num(num): | |
digits_left = NUMBER_OF_DIGITS | |
format_str = '' | |
if num < 0: | |
digits_left -= 1 | |
format_str += '-' | |
num *= -1 | |
if isinstance(num, int): | |
len_int = len(str(num)) | |
else: | |
len_int = len(str(int(round(num)))) | |
if len_int > digits_left: | |
return '' | |
else: | |
if isinstance(num, int): | |
format_str += '%d' | |
else: | |
len_frac = digits_left - len_int | |
format_str += '%%.%df' % len_frac | |
return format_str % num | |
GPIO.setmode(GPIO.BCM) | |
# GPIO ports for the 7seg pins | |
segments = (11,4,23,8,7,10,18,25) | |
# 7seg_segment_pins (11,7,4,2,1,10,5,3) + 100R inline | |
for segment in segments: | |
GPIO.setup(segment, GPIO.OUT) | |
GPIO.output(segment, 0) | |
# GPIO ports for the digit 0-3 pins | |
digits = (22,27,17,24) | |
# 7seg_digit_pins (12,9,8,6) digits 0-3 respectively | |
for digit in digits: | |
GPIO.setup(digit, GPIO.OUT) | |
GPIO.output(digit, 1) | |
num = { | |
' ':(0,0,0,0,0,0,0), | |
'-':(0,0,0,0,0,0,1), | |
'0':(1,1,1,1,1,1,0), | |
'1':(0,1,1,0,0,0,0), | |
'2':(1,1,0,1,1,0,1), | |
'3':(1,1,1,1,0,0,1), | |
'4':(0,1,1,0,0,1,1), | |
'5':(1,0,1,1,0,1,1), | |
'6':(1,0,1,1,1,1,1), | |
'7':(1,1,1,0,0,0,0), | |
'8':(1,1,1,1,1,1,1), | |
'9':(1,1,1,1,0,1,1) | |
} | |
RAND_MAX = (10 ** NUMBER_OF_DIGITS) - 1 | |
RAND_MIN = (-10 ** (NUMBER_OF_DIGITS - 1)) + 1 | |
r = random.uniform(-1, 1) | |
max_scale = NUMBER_OF_DIGITS | |
if r < 0: | |
max_scale -= 1 | |
n = r * (10 ** random.randint(0, max_scale)) | |
print(n) | |
next_mode = 'int' | |
last_change = time.time() | |
try: | |
while True: | |
s = format_num(n).rjust(NUMBER_OF_DIGITS) | |
digit = 0 | |
for char in s: | |
if char == '.': | |
continue | |
for loop in range(0,7): | |
GPIO.output(segments[loop], num[char][loop]) | |
if digit < (NUMBER_OF_DIGITS-1) and s[digit+1] == '.': | |
GPIO.output(25, 1) | |
else: | |
GPIO.output(25, 0) | |
GPIO.output(digits[digit], 0) | |
time.sleep(0.001) | |
GPIO.output(digits[digit], 1) | |
digit += 1 | |
if (time.time() - last_change) > 2: | |
r = random.uniform(-1, 1) | |
max_scale = NUMBER_OF_DIGITS | |
if r < 0: | |
max_scale -= 1 | |
n = r * (10 ** random.randint(0, max_scale)) | |
if next_mode == 'int': | |
n = int(n) | |
next_mode = 'float' | |
elif next_mode == 'float': | |
next_mode = 'int' | |
print(n) | |
last_change = time.time() | |
finally: | |
GPIO.cleanup() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment