"""
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Raspberry Pi Pico 7-Segment Display Counter (MicroPython)┃
┃                                                          ┃
┃ A program to demonstrate the use of a 7-segment display  ┃
┃ by implementing an ascending/descending hexadecimal      ┃
┃ counter based on the state of an input switch.           ┃
┃                                                          ┃
┃ Copyright (c) 2023 Anderson Costa                        ┃
┃ GitHub: github.com/arcostasi                             ┃
┃ License: MIT                                             ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
"""
from machine import Pin
from utime import sleep
# 7-segment display layout
#       A
#      ---
#  F |  G  | B
#      ---
#  E |     | C
#      ---
#       D
pins = [
    Pin(2, Pin.OUT),  # A
    Pin(3, Pin.OUT),  # B
    Pin(4, Pin.OUT),  # C
    Pin(5, Pin.OUT),  # D
    Pin(6, Pin.OUT),  # E
    Pin(8, Pin.OUT),  # F
    Pin(7, Pin.OUT),  # G
    Pin(0, Pin.OUT),   # DP (not connected)
]
pins2 = [
    Pin(28, Pin.OUT), #A2
    Pin(27, Pin.OUT), #B2
    Pin(19, Pin.OUT), #C2
    Pin(20, Pin.OUT), #D2
    Pin(21, Pin.OUT), #E2
    Pin(26, Pin.OUT), #F2
    Pin(22, Pin.OUT), #G2
    Pin(18, Pin.OUT), #DP2
]
# Common anode 7-segment display digit patterns
digits = [
    [0, 0, 0, 0, 0, 0, 1, 1], # 0
    [1, 0, 0, 1, 1, 1, 1, 1], # 1
    [0, 0, 1, 0, 0, 1, 0, 1], # 2 
    [0, 0, 0, 0, 1, 1, 0, 1], # 3
    [1, 0, 0, 1, 1, 0, 0, 1], # 4
    [0, 1, 0, 0, 1, 0, 0, 1], # 5
    [0, 1, 0, 0, 0, 0, 0, 1], # 6
    [0, 0, 0, 1, 1, 1, 1, 1], # 7
    [0, 0, 0, 0, 0, 0, 0, 1], # 8
    [0, 0, 0, 1, 1, 0, 0, 1], # 9
]
def reset():
    """Turns off all segments on the 7-segment display."""
    for pin in pins:
        pin.value(1)
    for pin in pins2:
        pin.value(1)
reset()
while True:
# Ascending counter
    for i in range(len(digits)):
        for j in range(len(pins) - 1):
            pins[j].value(digits[i][j])
        for k in range(len(digits)):
            for l in range(len(pins) - 1):
                pins2[l].value(digits[k][l])
            sleep(0.5)