# imports
import machine
import math
import time

#######################################
# Pin and constant definitions
#######################################
SEVEN_SEGMENT_START_PIN = 0
DISPLAY_COUNT = 4
DECIMAL_PRECISION = 3

# HEX values for 7 segment display values
digit_list_hex = [
    0b11000000,  # 0
    0b11111001,  # 1
    0x24,  # 2
    0x30,  # 3
    0x19,  # 4
    0x12,  # 5
    0x02,  # 6
    0x78,  # 7
    0x00,  # 8
    0x10,  # 9
]

#######################################
# Global variables
#######################################
display_value = 5749
segment_pins = []
display_select_pins = []
current_display_index = DISPLAY_COUNT -1  # to keep track of which digit is currently being displayed

def scan_display():
    global current_display_index, display_value

    digit = display_value

    # Display the digit,
    # enable the decimal point if the current digit index equals to the set decimal precision
    display_digit(digit, current_display_index, current_display_index == DECIMAL_PRECISION and 0 != DECIMAL_PRECISION)

    # Move to the next display
    current_display_index = (current_display_index - 1)
    if current_display_index < 0:
        current_display_index = DISPLAY_COUNT -1

    # for i in range(4):
    #     display_digit(int(digit % 10), i, i == DECIMAL_PRECISION and 0 != DECIMAL_PRECISION)
    #     time.sleep(0.03)
    #     digit /= 10

def display_digit(digit_value, digit_index, dp_enable=False):
    # Ensure the value is valid
    if digit_value < 0 or digit_value > len(digit_list_hex):
        #error
        return

    # Deselect all display select pins
    for pin in display_select_pins:
        pin.value(0)

    # Set the segments according to the digit value
    mask = digit_list_hex[digit_value]
    #mask = 0x0b11000000;
    for i in range(7):  # 7 segments from A to G
        segment_pins[i].value((mask >> i) & 1)

    # Set the DP if it's enabled
    segment_pins[7].value(1 if dp_enable == False else 0)

    # Otherwise, ensure the index is valid and activate the relevant display select pin
    if 0 <= digit_index < DISPLAY_COUNT:
        display_select_pins[digit_index].value(1)
    else:
        return

def main():

    # Set up display select pins
    for i in range(SEVEN_SEGMENT_START_PIN + 8, SEVEN_SEGMENT_START_PIN + 8 + DISPLAY_COUNT):
        pin = machine.Pin(i, machine.Pin.OUT)
        pin.value(0)
        display_select_pins.append(pin)
    
    # Set up seven segment pins
    for i in range(SEVEN_SEGMENT_START_PIN, SEVEN_SEGMENT_START_PIN + 8):
        pin = machine.Pin(i, machine.Pin.OUT)
        pin.value(1)
        segment_pins.append(pin)
    
    while True:
        scan_display()


if __name__ == '__main__':
    main()
$abcdeabcde151015202530354045505560fghijfghij
$abcdeabcde151015202530fghijfghij
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT