Début de refonte

This commit is contained in:
arofarn
2020-04-21 17:56:30 +02:00
parent 5d34806292
commit 1564fb44ac
8 changed files with 247 additions and 123 deletions

View File

View File

@ -1,7 +1,6 @@
# This file is executed on every boot (including wake-boot from deepsleep)
#import esp
#esp.osdebug(None)
import gc
import time
import network
@ -9,22 +8,29 @@ import webrepl
from wifi_config import known_wifi_ap
# Connect to one of the known wifi AP
interface = network.WLAN(network.STA_IF)
interface.active(True)
wifi_ap_list = interface.scan()
WLAN = network.WLAN(network.STA_IF)
WLAN.active(True)
wifi_ap_list = WLAN.scan()
for ap in wifi_ap_list:
if interface.isconnected():
if WLAN.isconnected():
break
ap_ssid = ap[0].decode("utf-8")
if ap_ssid in known_wifi_ap.keys():
print("Known wifi network found : {}".format(ap_ssid))
print("Try to connect...")
interface.connect(ap_ssid, known_wifi_ap[ap_ssid])
WLAN.connect(ap_ssid, known_wifi_ap[ap_ssid])
# Wait for wifi
time.sleep(5)
DELAY = 0
while not WLAN.isconnected():
print("Waiting for wifi to connect...")
time.sleep(1)
DELAY += 1
if DELAY > 10:
print("Wifi time-out")
break
webrepl.start()

View File

@ -29,14 +29,19 @@ def update_neopixel(mode, np_strp, col=(255, 255, 255), bri=50):
:param col : color [R, G, B] (default: [255, 255, 255])
:param bri : brightness 0-100 (default: 50)
"""
if bri > 100:
bri = 100
elif bri < 0:
bri = 0
bri_coef = bri/100
# 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.buf = bytearray([int(x * bri_coef) for x in np_strp.buf])
# Finally display color on LEDs
np_strp.write()
def fill_usr(np_strp, col):
"""Update NeoPixel strip with one color
:param np_strp : NeoPixel object
@ -54,14 +59,19 @@ def fill_white(np_strp, col=(255, 255, 255)):
def sparkles(np_strp, col):
"""Make Neopixel sparkle with user defined color!!!
"""
"""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))
print(pix)
np_strp[pix] = col
def christmas(np_strp, col):
"""Shine like christmas tree °<:oD
TODO !!!
"""
pass
MODES_LST = (fill_usr, fill_white, sparkles)

View File

@ -16,76 +16,129 @@ Eclairage à LED Neopixel
avec contrôle (encodeur rotatif)
"""
__author__ = "arofarn"
__version__ = 0.2
__version__ = 0.3
# Imports
import time
import sys
import machine
import neopixel
from uos import uname
from encoder import Encoder
import light_modes
#Paramètres
# Paramètres
NB_PIX = 8 # Nombre de pixels
MAX_BRIGHT = 100 # Luminosité max (100 max.)
USR_COLOR = [255, 120, 20] # Couleur de base (à luminosité max)
NB_PIX = 67 # Nombre de pixels de la 1e bande de LED
NB_PIX2 = 67 # Nombre de pixels de la 2e bande de LED
MAX_BRIGHT = 100 # Luminosité max (100 max.)
USR_COLOR = [255, 130, 20] # Couleur de base (à luminosité max)
#Déclaration des objets et initialisation des variables
# Déclaration des objets et initialisation des variables
BRD_TYPE = uname()[0]
print("Système :", BRD_TYPE)
NEOPIX_PIN = 2
NEOPIX2_PIN = 16
# Les deux broches suivantes doivent être capable d'interruption !!!
# sur ESP8266 => : 4, 5, 12, 13 et 14
ENC_PIN_A = 14 # N° de la 1ere broche de l'encodeur
ENC_PIN_B = 12 # N° de la 2e broche de l'encodeur
ENC_PIN_C = 13 # broche du clic central de l'encodeur
if BRD_TYPE == 'esp8266':
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
ENC_PIN_C = 14 # broche du clic central de l'encodeur
WIFI_LED_PIN = 0
# With ESP8266 (no timing parameter)
NPXL_STRIP = neopixel.NeoPixel(machine.Pin(NEOPIX_PIN), NB_PIX)
# With ESP8266 (no timing parameter)
NPXL_STRIP = neopixel.NeoPixel(machine.Pin(NEOPIX_PIN), NB_PIX)
NPXL_STRIP2 = neopixel.NeoPixel(machine.Pin(NEOPIX2_PIN), NB_PIX)
elif BRD_TYPE == 'esp32':
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
ENC_PIN_C = 27 # broche du clic central de l'encodeur
# # Only with ESP32 : add timing param.
# # timing param =0 for "old" 400kHz neopixel (default)
# # =1 for "new" 800kHz neopixel
NPXL_STRIP = neopixel.NeoPixel(machine.Pin(NEOPIX_PIN), NB_PIX, timing=1)
else:
# ce code n'est pas prévu pour d'autre carte pour l'instant
print("Carte non-supportée :", BRD_TYPE)
quit()
#Eteint tout à l'initialisation
# Eteint tout à l'initialisation
NPXL_STRIP.fill([0, 0, 0])
NPXL_STRIP.write()
# Encodeur rotatif
ENCODER = Encoder(ENC_PIN_B, ENC_PIN_A,
ENCODER = Encoder(ENC_PIN_A, ENC_PIN_B,
min_val=0, max_val=MAX_BRIGHT,
clicks=1)
#Bouton
# Bouton
ENC_BUT = machine.Pin(ENC_PIN_C, machine.Pin.IN)
#Variables d'état
# LED status WIFI_LED_PIN
WIFI_LED = machine.Pin(WIFI_LED_PIN, machine.Pin.OUT)
# initialisation des variables d'état
BRIGHTN = 50 # Luminosité (0 - 100)
PWR = True # Est-ce que l'éclairage est allumé ?
BUTTN_STATE = 1
curr_mode = 0
pwroff_dl = 0
CURRENT_MODE = 0
PWROFF_DELAY = 0
#############
# FUNCTIONS #
#############
def power_cycle():
"""ON/OFF function"""
global PWR
global BRIGHTN
PWR = not PWR
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)
def update_button_c(button, button_state, pwr, mode):
"""Surveille le bouton C (clic central)"""
global PWROFF_DELAY
if button.value() == 0:
# print("appui")
if button_state == 1:
# Deadline avant de changer l'état d'allumage
PWROFF_DELAY = time.ticks_add(time.ticks_ms(), 2000)
# print(PWROFF_DELAY)
else:
# Est-ce que la deadline est atteinte ? si oui on change l'état
# d'allumage
# print(PWROFF_DELAY, "/", time.ticks_ms())
if time.ticks_diff(time.ticks_ms(), PWROFF_DELAY) >= 0:
PWROFF_DELAY = 0
power_cycle()
else:
if pwr and button_state == 0:
# Si on a juste un clic rapide sur le bouton, on change de mode au
# relachement du bouton
mode = mode + 1
print("Mode : {}/{}".format(CURRENT_MODE,
len(light_modes.MODES_LST)))
if mode >= len(light_modes.MODES_LST):
mode = 0
return mode
def update_wifi_led():
"""Refresh Wifi status LED"""
global WIFI_LED
global WLAN
WIFI_LED = WLAN.isconnected()
#####################
# BOUCLE PRINCIPALE #
@ -93,59 +146,20 @@ pwroff_dl = 0
while True:
# Si on est dans l'état allumé:
# Si on a un changement de valeur, on met à jour la luminosité
if PWR:
# Si on a un changement de valeur, on met à jour la luminosité
if PWR and BRIGHTN != ENCODER.value:
if 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)
light_modes.update_neopixel(CURRENT_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:
# 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)
CURRENT_MODE = update_button_c(ENC_BUT,
BUTTN_STATE,
PWR, CURRENT_MODE)
BUTTN_STATE = ENC_BUT.value()
time.sleep_ms(5)