"""
Assignment: ITSC305 Lab 2 - Part 1
Author: Mykola Grymalyuk
Student ID: 000898089
----------
1. Lookup and download the specification sheet for the 7 segment display
Note: This must be for specific model included in your kit
2. Inside the spec sheet, look up the following information. Screen capture, highlight and include these parts in your report:
o Is this a common cathode or common anode display?
o What is the maximum forward current for each LED?
o Lead/pin assignment (what does each lead/pin do?)
3. Connect the 7 segment display to appropriate number of GPIO outputs (using appropriate resistors) of your Raspberry Pi and make it count from 0 to 9
Important: Do NOT use classes specifically designed for 7-segment displays, such as LEDBoard. Create your own functions using bit-mapping techniques.
4. Show this to your instructor, include code and video (YouTube link) in report
----------
Q1. Specification Sheet
- Model: 5161AS
- Data sheet: http://www.xlitx.com/datasheet/5161AS.pdf
- Data sheet (alt): https://www.electronicoscaldas.com/datasheet/TOS-5161AS-B_Oasis.pdf
Q2. Model Information
A. Common Cathode
B. Max current: 20mA
C. Pin Assignment:
- A: 7
- B: 6
- C: 4
- D: 2
- E: 1
- F: 9
- G: 10
- DP: 5
----------
Resistor calculation:
- Voltage: 3.3V (Pico)
- Current: 20mA (Max)
R = V / I
R = 3.3 / 0.02
R = 165 Ohms
In practice, lowest available in kit was 220 Ohms.
"""
PIN_A: int = 17
PIN_B: int = 16
PIN_C: int = 14
PIN_D: int = 13
PIN_E: int = 12
PIN_F: int = 18
PIN_G: int = 19
import time
import machine
class SevenSegmentDisplayHandler:
"""
Seven Segment Display Handler
Public Methods:
display(number: int) -> None
clear() -> None
display_all() -> None
"""
def __init__(self) -> None:
self._pins = [
machine.Pin(PIN_A, machine.Pin.OUT),
machine.Pin(PIN_B, machine.Pin.OUT),
machine.Pin(PIN_C, machine.Pin.OUT),
machine.Pin(PIN_D, machine.Pin.OUT),
machine.Pin(PIN_E, machine.Pin.OUT),
machine.Pin(PIN_F, machine.Pin.OUT),
machine.Pin(PIN_G, machine.Pin.OUT),
]
# Ref: https://electrocredible.com/7-segment-display-with-raspberry-pi-pico/
self.value_matrix = [
[True, True, True, True, True, True, False], # 0
[False, True, True, False, False, False, False], # 1
[True, True, False, True, True, False, True ], # 2
[True, True, True, True, False, False, True ], # 3
[False, True, True, False, False, True, True ], # 4
[True, False, True, True, False, True, True ], # 5
[True, False, True, True, True, True, True ], # 6
[True, True, True, False, False, False, False], # 7
[True, True, True, True, True, True, True ], # 8
[True, True, True, True, False, True, True ], # 9
]
self.clear_matrix = [False, False, False, False, False, False, False]
self.pin_matrix = [PIN_A, PIN_B, PIN_C, PIN_D, PIN_E, PIN_F, PIN_G]
def _pin_to_index(self, pin: int) -> int:
# This is such a weird hack...
for name in globals():
if not name.startswith('PIN_'):
continue
if globals()[name] != pin:
continue
letter = name.replace('PIN_', '')
return ord(letter) - ord('A')
def _set_pins(self, pins: list[int], value: list[bool]) -> None:
for pin in pins:
self._pins[self._pin_to_index(pin)].value(value[self._pin_to_index(pin)])
def _set_digit(self, number: int) -> None:
self._set_pins(self.pin_matrix, self.value_matrix[number])
def display(self, number: int) -> None:
self._set_digit(number)
def clear(self) -> None:
self._set_pins(self.pin_matrix, self.clear_matrix)
def display_all(self) -> None:
for i in range(10):
self.display(i)
time.sleep(1)
if __name__ == '__main__':
display = SevenSegmentDisplayHandler()
display.display_all()
display.clear()