1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
#a lancer avec cd piLedClock/rpi_ws281x/python
#et sudo python3 wordclock.py
from datetime import datetime
from time import sleep
from neopixel import *
import argparse
import RPi.GPIO as GPIO #bibliothèque RPi.GPIO
import time #bibliothèque time
# LED strip configuration:
LED_COUNT = 114 # Number of LED pixels.
LED_PIN = 18 # GPIO pin connected to the pixels (18 uses PWM!).
#LED_PIN = 10 # GPIO pin connected to the pixels (10 uses SPI /dev/spidev0.0).
LED_FREQ_HZ = 800000 # LED signal frequency in hertz (usually 800khz)
LED_DMA = 10 # DMA channel to use for generating signal (try 10)
LED_BRIGHTNESS = 255 # Set to 0 for darkest and 255 for brightest
LED_INVERT = False # True to invert the signal (when using NPN transistor level shift)
LED_CHANNEL = 0 # set to '1' for GPIOs 13, 19, 41, 45 or 53
#-------------------------------------------------------------------------------
pin = 17, 22, 24 #broche utilisé en entrée
#temps = 1 #valeur attente en msec
#temps = 10
temps = 100
#temps = 100
#temps = 1000
GPIO.setwarnings(False) #désactive le mode warning
GPIO.setmode(GPIO.BCM) #utilisation des numéros de ports du
#processeur
GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
#mise en entrée du port GPIO 22
#et activation résistance soutirage
#au ground
panel = \
"\
ILNESTODEUX\
QUATRETROIS\
NEUFUNESEPT\
HUITSIXCINQ\
MIDIXMINUIT\
ONZERHEURES\
MOINSOLEDIX\
ETRQUARTPMD\
TRENTEOURXT\
VINGT-CINQU\
....\
"
# stripLettre est la liste des LEDs en fonction de l'ordre des lettres dans panel
# tient compte du tortillement du ruban
stripLettre = list(range(110,0,-1))
for i in range(0,110,22): stripLettre[i:i+11] = stripLettre[i:i+11][::-1]
stripDot = [111, 114, 113, 112]
stripLettre += stripDot
def convert(heure,minute):
"""convertit l'heure en texte, recherche chaque terme dans le panel,
et utilise l'index des mots pour extraire dans stripLettre les numéros des leds à allumer"""
heure = (heure+(minute>=35))%24
liaison = heure%12>1 and'HEURES'or heure%12 and'HEURE'or''
heure = (heure//12 and'MIDI'or'MINUIT','UNE','DEUX','TROIS','QUATRE','CINQ','SIX','SEPT','HUIT','NEUF','DIX','ONZE')[heure%12]
dot = ' . . . .'[:minute%5*2]
minute = ('','CINQ','DIX','ET QUART','VINGT','VINGT-CINQ','TRENTE','TRENTE CINQ','MOINS VINGT','MOINS LE QUART','MOINS DIX','MOINS CINQ')[minute//5]
litt = f'IL EST {heure}'
if liaison: litt = litt + ' ' + liaison
if minute : litt = litt + ' ' + minute
litt += dot
print(litt)
output = []
index = 0
for mot in litt.split():
i = panel.find(mot,index)
output.extend(stripLettre[i:i+len(mot)])
index = i + len(mot)
return output
def getHour():
maintenant = datetime.now()
heure = maintenant.hour
minute = maintenant.minute
return heure,minute
# Define functions which animate LEDs in various ways.
def colorWipe(strip, color, wait_ms=50):
"""Wipe color across display a pixel at a time."""
for i in range(strip.numPixels()):
strip.setPixelColor(i, color)
strip.show()
sleep(wait_ms/1000.0)
#Show array
def showArray(strip, color, array):
array = set(array)
for i in range(strip.numPixels()):
strip.setPixelColor(i, color if (i+1) in array else Color(0,0,0))
strip.show()
def get_color():
R,G,B = list_color[index_color]
return Color(int(R*current_bright),int(G*current_bright),int(B*current_bright))
list_color = [(253, 51, 100), (138, 36, 177), (240, 120, 228), (129, 77, 86), (111, 147, 168), (221, 254, 42), (152, 216, 39), (166, 165, 158), (144, 44, 102), (236, 25, 252)]
index_color = 0
current_bright = 1
delay_low = 0
pin17state = True
if __name__ == '__main__':
# Process arguments
parser = argparse.ArgumentParser()
parser.add_argument('-c', '--clear', action='store_true', help='clear the display on exit')
args = parser.parse_args()
# Create NeoPixel object with appropriate configuration.
strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS, LED_CHANNEL)
# Intialize the library (must be called once before other functions).
strip.begin()
print ('Press Ctrl-C to quit.')
if not args.clear:
print('Use "-c" argument to clear LEDs on exit')
# attendre si on est moins d'une seconde de la minute suivante
while datetime.now().second == 59: continue
showArray(strip, get_color(), convert(*getHour()))
try:
while True:
if datetime.now().second == 0:
output = convert(*getHour())
print(output)
showArray(strip, get_color(), convert(*getHour()))
entreepin17 = GPIO.input(17)
if (entreepin17 == True) and not pin17state:
pin17state = True
delay_low = 0
current_bright = round((current_bright+.1)%1.1,1) or .1
# met à jour au cas de dépassement de minute
showArray(strip, get_color(), convert(*getHour()))
if (entreepin17 == False):
pin17state = False
print ('PIN17')
delay_low += 1
if delay_low == 20: # valeur à ajuster
colorWipe(strip, Color(0,0,0), 10)
current_bright = round((current_bright-.1),1)
while not GPIO.input(17): pass
if (entreepin17 == False):
pin17state = False
print ('PIN17')
delay_low += 1
if delay_low == 20: # valeur à ajuster
colorWipe(strip, Color(0,0,0), 10)
current_bright = round((current_bright-.1),1)
pin17state = True
while not GPIO.input(17): pass
entreepin22 = GPIO.input(22)
if (entreepin22 == False):
print ('PIN22')
index_color = (index_color+1)%len(list_color)
# met à jour au cas de dépassement de minute
showArray(strip, get_color(), convert(*getHour()))
entreepin24 = GPIO.input(24)
if (entreepin24 == False):
print ('PIN24')
# met à jour au cas de dépassement de minute
showArray(strip, Color(50,50,50), convert(*getHour()))
time.sleep(temps / 1000)
except KeyboardInterrupt:
if args.clear:
colorWipe(strip, Color(0,0,0), 10) |
Partager