7 segmenti – Secondi

Con una coppia di 7-segmenti si visualizza lo scorrere dei secondi

from vpython  import *
from datetime import datetime
#------------------------------------------------------------------------------- Parametri
C = color.yellow   # Colore dei cilindri
D = 1              # Dimensione, lunghezza, dei cilindri
R = D/5            # Raggio dei cilindri
#------------------------------------------------------------------------------- Tutto acceso!
SETTE_SEGMENTI = [
    cylinder(pos=vector(0,2*D,0), axis=vector(1,0,0), radius=R, color=C), # 0
    cylinder(pos=vector(0,  D,0), axis=vector(0,1,0), radius=R, color=C), # 1
    cylinder(pos=vector(D,  D,0), axis=vector(0,1,0), radius=R, color=C), # 2
    cylinder(pos=vector(0,  D,0), axis=vector(1,0,0), radius=R, color=C), # 3
    cylinder(pos=vector(0,  0,0), axis=vector(0,1,0), radius=R, color=C), # 4
    cylinder(pos=vector(D,  0,0), axis=vector(0,1,0), radius=R, color=C), # 5
    cylinder(pos=vector(0,  0,0), axis=vector(1,0,0), radius=R, color=C)  # 6
]
#------------------------------------------------------------------------------- Mappa
VALORE_SEGMENTI = [
    [True , True , True , False, True , True , True ], # 0
    [False, False, True , False, False, True , False], # 1
    [True , False, True , True , True , False, True ], # 2
    [True , False, True , True , False, True , True ], # 3
    [False, True , True , True , False, True , False], # 4
    [True , True , False, True , False, True , True ], # 5
    [False, True , False, True , True , True , True ], # 6
    [True , False, True , False, False, True , False], # 7
    [True , True , True , True , True , True , True ], # 8
    [True , True , True , True , False, True , False]  # 9
]
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
for i in range(7):                      # Tutto spento...
    SETTE_SEGMENTI[i].visible = False

CIFRE = []                              # Crea una coppia di 7-segmenti
POS_X = [-1.5*D, +0.5*D]                # Posizioni in orizzontale
for i in range(2):
    sette_segmenti = []                 # Nuovo...
    for j in range(7):
        segmento        = SETTE_SEGMENTI[j].clone()
        segmento.pos.x += POS_X[i]
        segmento.pos.y += -D  
        sette_segmenti.append(segmento)
    CIFRE.append(sette_segmenti)        # Aggiunge il nuovo 7-segmenti
#------------------------------------------------------------------------------- Funzione
def aggiorna_stato(posizione, valore):
    for segmento in range(7):
        CIFRE[posizione][segmento].visible = VALORE_SEGMENTI[valore][segmento]
#------------------------------------------------------------------------------- MAIN
while(True):
    rate(5)
    
    secondi = datetime.now().second
    decine  = secondi // 10
    unità   = secondi  % 10
    
    aggiorna_stato(0, decine)
    aggiorna_stato(1, unità)

Lascia un commento