Add simple support for different modes
This commit is contained in:
parent
32ebdd4d30
commit
66ec8247da
65
code/light_modes.py
Normal file
65
code/light_modes.py
Normal file
@ -0,0 +1,65 @@
|
||||
# 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/>.
|
||||
|
||||
"""Modes for neopixels strip
|
||||
Each function update the NeoPixel strip buffer (except update_neopixel() that
|
||||
call them)
|
||||
"""
|
||||
|
||||
import time
|
||||
import urandom
|
||||
|
||||
|
||||
def update_neopixel(mode, np_strp, col=(255, 255, 255), bri=50):
|
||||
"""Update NeoPixel strip with one color
|
||||
:param np_strp : NeoPixel object
|
||||
:param mode : mode ;)
|
||||
:param col : color [R, G, B] (default: [255, 255, 255])
|
||||
:param bri : brightness 0-100 (default: 50)
|
||||
"""
|
||||
# Apply mode
|
||||
MODES_LST[mode](np_strp, col)
|
||||
|
||||
# Apply brightness to the whole LEDs
|
||||
np_strp.buf = bytearray([int(x * bri / 100) for x in np_strp.buf])
|
||||
|
||||
np_strp.write()
|
||||
|
||||
def fill_usr(np_strp, col):
|
||||
"""Update NeoPixel strip with one color
|
||||
:param np_strp : NeoPixel object
|
||||
:param col : color [R, G, B]
|
||||
"""
|
||||
np_strp.fill(col)
|
||||
|
||||
|
||||
def fill_white(np_strp, col=(255, 255, 255)):
|
||||
"""Update NeoPixel strip with only white
|
||||
:param np_strp : NeoPixel object
|
||||
:param col : color [R, G, B] (ignored, default=(255, 255, 255))
|
||||
"""
|
||||
np_strp.fill((255, 255, 255))
|
||||
|
||||
|
||||
def sparkles(np_strp, col):
|
||||
"""Make Neopixel sparkle with user defined color!!!
|
||||
"""
|
||||
np_strp.fill((0, 0, 0))
|
||||
|
||||
for _ in range(int(np_strp.n / 4)):
|
||||
pix = int(urandom.getrandbits(8) / 256 * (np_strp.n - 1))
|
||||
print(pix)
|
||||
np_strp[pix] = col
|
||||
|
||||
|
||||
MODES_LST = (fill_usr, fill_white, sparkles)
|
101
code/main.py
101
code/main.py
@ -27,11 +27,13 @@ import neopixel
|
||||
from uos import uname
|
||||
from encoder import Encoder
|
||||
|
||||
import light_modes
|
||||
|
||||
#Paramètres
|
||||
|
||||
NB_PIX = 8 # Nombre de pixels
|
||||
MAX_BRIGHT = 100 # Luminosité max (100 max.)
|
||||
COLOR = [255, 255, 255] # Couleur de base (à luminosité max)
|
||||
USR_COLOR = [255, 120, 20] # Couleur de base (à luminosité max)
|
||||
|
||||
#Déclaration des objets et initialisation des variables
|
||||
|
||||
@ -66,12 +68,6 @@ else:
|
||||
print("Carte non-supportée :", BRD_TYPE)
|
||||
quit()
|
||||
|
||||
# # Only with ESP32 : add timing param.
|
||||
# # timing param =0 for "old" 400kHz neopixel (default)
|
||||
# # =1 for "new" 800kHz neopixel
|
||||
# pixel_strip = neopixel.NeoPixel(machine.Pin(NEOPIX_PIN), NB_PIX, timing=1)
|
||||
# coul_tmp = [0, 0, 0]
|
||||
|
||||
#Eteint tout à l'initialisation
|
||||
NPXL_STRIP.fill([0, 0, 0])
|
||||
NPXL_STRIP.write()
|
||||
@ -86,22 +82,10 @@ ENC_BUT = machine.Pin(ENC_PIN_C, machine.Pin.IN)
|
||||
|
||||
#Variables d'état
|
||||
BRIGHTN = 50 # Luminosité (0 - 100)
|
||||
PWR = False # Est-ce que l'éclairage est allumé ?
|
||||
PWR = True # Est-ce que l'éclairage est allumé ?
|
||||
BUTTN_STATE = 1
|
||||
|
||||
def update_neopixel(np_strp, col, bri=50):
|
||||
"""Update NeoPixel strip with one color
|
||||
:param np_strp : NeoPixel object
|
||||
:param col : color [R, G, B]
|
||||
:param bri : brightness 0-100 (default: 50)
|
||||
"""
|
||||
col_tmp = [0, 0, 0]
|
||||
|
||||
for i in range(3):
|
||||
col_tmp[i] = int(col[i] * bri / 100)
|
||||
|
||||
np_strp.fill(col_tmp)
|
||||
np_strp.write()
|
||||
curr_mode = 0
|
||||
pwroff_dl = 0
|
||||
|
||||
#####################
|
||||
# BOUCLE PRINCIPALE #
|
||||
@ -109,32 +93,59 @@ def update_neopixel(np_strp, col, bri=50):
|
||||
|
||||
while True:
|
||||
|
||||
# Si on a un changement de valeur, on met à jour la luminisité et l'éclairage
|
||||
# des NeoPixel
|
||||
if PWR and BRIGHTN != ENCODER.value:
|
||||
print(ENCODER.value)
|
||||
BRIGHTN = ENCODER.value
|
||||
update_neopixel(NPXL_STRIP, COLOR, BRIGHTN)
|
||||
# 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
|
||||
|
||||
# Quand le bouton central est appuyé puis relâché, on allume ou on éteint
|
||||
if ENC_BUT.value() == 0 and BUTTN_STATE == 1:
|
||||
PWR = not PWR
|
||||
print("Power :", PWR)
|
||||
if not PWR:
|
||||
# Extinction des LED
|
||||
NPXL_STRIP.fill((0, 0, 0))
|
||||
NPXL_STRIP.write()
|
||||
# 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)
|
||||
else:
|
||||
# Luminité basse si on allume avec une luminisité de 0
|
||||
if BRIGHTN == 0:
|
||||
BRIGHTN = 10
|
||||
# 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)
|
||||
# Rallume les LED:
|
||||
update_neopixel(NPXL_STRIP, COLOR, BRIGHTN)
|
||||
# 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)
|
||||
|
||||
BUTTN_STATE = ENC_BUT.value()
|
||||
|
||||
time.sleep_ms(20)
|
||||
time.sleep_ms(5)
|
||||
|
Loading…
Reference in New Issue
Block a user