7 segmenti – Orario

Con 3 coppie di 7-segmenti si visualizza lo scorrere dell’orario

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 6 copie per l'orario
POS_X = [-4.75*D, -3.25*D, -1.25*D, +0.25*D, +2.25*D, +3.75*D]
for i in range(6):
    sette_segmenti = []
    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)
#------------------------------------------------------------------------------- Funzione
def aggiorna_stato(posizione, valore):
    for segmento in range(7):
        CIFRE[posizione][segmento].visible = VALORE_SEGMENTI[valore][segmento]
#------------------------------------------------------------------------------- MAIN
cifre_orario = 6*[0]
while(True):
    rate(5)
    adesso  = datetime.now()
    ore     = adesso.hour
    minuti  = adesso.minute
    secondi = adesso.second

    cifre_orario[0] = ore     // 10
    cifre_orario[1] = ore      % 10
    cifre_orario[2] = minuti  // 10
    cifre_orario[3] = minuti   % 10
    cifre_orario[4] = secondi // 10
    cifre_orario[5] = secondi  % 10

    for i in range(6):    
        aggiorna_stato(i, cifre_orario[i])

Lascia un commento