2018-09-10 20:30:17 +02:00
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
"""
|
|
|
|
Eclairage à LED Neopixel
|
2018-11-18 10:28:07 +01:00
|
|
|
avec contrôle (encodeur rotatif)
|
2018-09-10 20:30:17 +02:00
|
|
|
"""
|
|
|
|
__author__ = "arofarn"
|
2018-11-18 10:28:07 +01:00
|
|
|
__version__ = 0.2
|
|
|
|
|
|
|
|
# Imports
|
2018-09-10 20:30:17 +02:00
|
|
|
|
|
|
|
import time
|
|
|
|
import machine
|
|
|
|
import neopixel
|
2018-11-18 23:15:25 +01:00
|
|
|
|
2018-11-18 10:28:07 +01:00
|
|
|
from uos import uname
|
|
|
|
from encoder import Encoder
|
|
|
|
|
2018-11-19 11:51:04 +01:00
|
|
|
import light_modes
|
|
|
|
|
2018-11-18 10:28:07 +01:00
|
|
|
#Paramètres
|
|
|
|
|
|
|
|
NB_PIX = 8 # Nombre de pixels
|
2018-11-18 16:34:00 +01:00
|
|
|
MAX_BRIGHT = 100 # Luminosité max (100 max.)
|
2018-11-19 11:51:04 +01:00
|
|
|
USR_COLOR = [255, 120, 20] # Couleur de base (à luminosité max)
|
2018-11-18 10:28:07 +01:00
|
|
|
|
|
|
|
#Déclaration des objets et initialisation des variables
|
|
|
|
|
2018-11-18 16:34:00 +01:00
|
|
|
BRD_TYPE = uname()[0]
|
|
|
|
print("Système :", BRD_TYPE)
|
2018-11-18 10:28:07 +01:00
|
|
|
|
2018-11-18 16:34:00 +01:00
|
|
|
if BRD_TYPE == 'esp8266':
|
2018-11-18 10:28:07 +01:00
|
|
|
NEOPIX_PIN = 2
|
|
|
|
# Les deux broches suivantes doivent être capable d'interruption !!!
|
|
|
|
# sur ESP8266 => : 4, 5, 12, 13 et 14
|
|
|
|
ENC_PIN_A = 13 # N° de la 1ere broche de l'encodeur
|
|
|
|
ENC_PIN_B = 12 # N° de la 2e broche de l'encodeur
|
2018-11-18 16:34:00 +01:00
|
|
|
ENC_PIN_C = 14 # broche du clic central de l'encodeur
|
2018-11-18 10:28:07 +01:00
|
|
|
|
|
|
|
# With ESP8266 (no timing parameter)
|
2018-11-18 16:34:00 +01:00
|
|
|
NPXL_STRIP = neopixel.NeoPixel(machine.Pin(NEOPIX_PIN), NB_PIX)
|
2018-11-18 10:28:07 +01:00
|
|
|
|
2018-11-18 16:34:00 +01:00
|
|
|
elif BRD_TYPE == 'esp32':
|
2018-11-18 10:28:07 +01:00
|
|
|
NEOPIX_PIN = 14
|
|
|
|
# Pins
|
|
|
|
ENC_PIN_A = 15 # N° de la 1ere broche de l'encodeur
|
|
|
|
ENC_PIN_B = 33 # N° de la 2e broche de l'encodeur
|
2018-11-18 16:34:00 +01:00
|
|
|
ENC_PIN_C = 27 # broche du clic central de l'encodeur
|
2018-11-18 10:28:07 +01:00
|
|
|
|
|
|
|
# # Only with ESP32 : add timing param.
|
|
|
|
# # timing param =0 for "old" 400kHz neopixel (default)
|
|
|
|
# # =1 for "new" 800kHz neopixel
|
2018-11-18 16:34:00 +01:00
|
|
|
NPXL_STRIP = neopixel.NeoPixel(machine.Pin(NEOPIX_PIN), NB_PIX, timing=1)
|
2018-09-10 20:30:17 +02:00
|
|
|
|
2018-11-18 10:28:07 +01:00
|
|
|
else:
|
|
|
|
# ce code n'est pas prévu pour d'autre carte pour l'instant
|
2018-11-18 16:34:00 +01:00
|
|
|
print("Carte non-supportée :", BRD_TYPE)
|
2018-11-18 10:28:07 +01:00
|
|
|
quit()
|
2018-09-10 20:30:17 +02:00
|
|
|
|
2018-11-01 18:37:53 +01:00
|
|
|
#Eteint tout à l'initialisation
|
2018-11-18 16:34:00 +01:00
|
|
|
NPXL_STRIP.fill([0, 0, 0])
|
|
|
|
NPXL_STRIP.write()
|
2018-09-10 20:30:17 +02:00
|
|
|
|
2018-11-18 10:28:07 +01:00
|
|
|
# Encodeur rotatif
|
2018-11-18 16:34:00 +01:00
|
|
|
ENCODER = Encoder(ENC_PIN_B, ENC_PIN_A,
|
|
|
|
min_val=0, max_val=MAX_BRIGHT,
|
|
|
|
clicks=1)
|
2018-11-18 10:28:07 +01:00
|
|
|
|
|
|
|
#Bouton
|
2018-11-18 16:34:00 +01:00
|
|
|
ENC_BUT = machine.Pin(ENC_PIN_C, machine.Pin.IN)
|
2018-11-18 10:28:07 +01:00
|
|
|
|
2018-11-18 16:34:00 +01:00
|
|
|
#Variables d'état
|
2018-11-18 23:15:25 +01:00
|
|
|
BRIGHTN = 50 # Luminosité (0 - 100)
|
2018-11-19 11:51:04 +01:00
|
|
|
PWR = True # Est-ce que l'éclairage est allumé ?
|
2018-11-18 16:34:00 +01:00
|
|
|
BUTTN_STATE = 1
|
2018-11-19 11:51:04 +01:00
|
|
|
curr_mode = 0
|
|
|
|
pwroff_dl = 0
|
2018-11-18 16:34:00 +01:00
|
|
|
|
|
|
|
#####################
|
|
|
|
# BOUCLE PRINCIPALE #
|
|
|
|
#####################
|
2018-11-18 10:28:07 +01:00
|
|
|
|
2018-09-10 20:30:17 +02:00
|
|
|
while True:
|
2018-11-18 10:28:07 +01:00
|
|
|
|
2018-11-19 11:51:04 +01:00
|
|
|
# Si on est dans l'état allumé:
|
|
|
|
if PWR:
|
|
|
|
# Si on a un changement de valeur, on met à jour la luminosité
|
|
|
|
if PWR and BRIGHTN != ENCODER.value:
|
|
|
|
print(ENCODER.value)
|
|
|
|
BRIGHTN = ENCODER.value
|
|
|
|
|
|
|
|
# Si on a juste un clic rapide sur le bouton, on change de mode au
|
|
|
|
# relachement du bouton
|
|
|
|
if ENC_BUT.value() == 1 and BUTTN_STATE == 0:
|
|
|
|
curr_mode = curr_mode + 1
|
|
|
|
print("Mode : {}/{}".format(curr_mode, len(light_modes.MODES_LST)))
|
|
|
|
if curr_mode >= len(light_modes.MODES_LST):
|
|
|
|
curr_mode = 0
|
|
|
|
|
|
|
|
# Mise à jour des LED
|
|
|
|
light_modes.update_neopixel(curr_mode, NPXL_STRIP, USR_COLOR, BRIGHTN)
|
|
|
|
|
|
|
|
|
|
|
|
# Quand le bouton central est appuyé puis relâché rapidement, on change de
|
|
|
|
# mode
|
|
|
|
# Si on laisse appuyer 2 secondes: extinction
|
|
|
|
if ENC_BUT.value() == 0:
|
|
|
|
print("appui")
|
|
|
|
if BUTTN_STATE == 1:
|
|
|
|
# Deadline avant de changer l'état d'allumage
|
|
|
|
pwroff_dl = time.ticks_add(time.ticks_ms(), 2000)
|
|
|
|
print(pwroff_dl)
|
2018-11-18 10:28:07 +01:00
|
|
|
else:
|
2018-11-19 11:51:04 +01:00
|
|
|
# Est-ce que la deadline est atteinte ? si oui on change l'état
|
|
|
|
# d'allumage
|
|
|
|
print(pwroff_dl, "/", time.ticks_ms())
|
|
|
|
if time.ticks_diff(time.ticks_ms(), pwroff_dl) >= 0:
|
|
|
|
PWR = not PWR
|
|
|
|
pwroff_dl = 0
|
|
|
|
print("Power :", PWR)
|
|
|
|
if not PWR:
|
|
|
|
# Extinction des LED
|
|
|
|
NPXL_STRIP.fill((0, 0, 0))
|
|
|
|
NPXL_STRIP.write()
|
|
|
|
else:
|
|
|
|
# Luminosité basse si on rallume avec une luminisité de 0
|
|
|
|
if BRIGHTN == 0:
|
|
|
|
BRIGHTN = 10
|
|
|
|
|
|
|
|
# On remet l'encodeur à la dernière luminosité connue
|
|
|
|
# pour ignorer les mouvements pendant l'extinction
|
|
|
|
ENCODER.reset(BRIGHTN)
|
|
|
|
|
|
|
|
# on attend que le bouton soit relâché
|
|
|
|
while ENC_BUT.value() == 0:
|
|
|
|
time.sleep_ms(10)
|
2018-11-18 10:28:07 +01:00
|
|
|
|
2018-11-18 16:34:00 +01:00
|
|
|
BUTTN_STATE = ENC_BUT.value()
|
2018-11-18 10:28:07 +01:00
|
|
|
|
2018-11-19 11:51:04 +01:00
|
|
|
time.sleep_ms(5)
|