Rotary encoder
This commit is contained in:
parent
e730db0822
commit
48dbd0c593
@ -2,6 +2,6 @@
|
|||||||
#import esp
|
#import esp
|
||||||
#esp.osdebug(None)
|
#esp.osdebug(None)
|
||||||
import gc
|
import gc
|
||||||
#import webrepl
|
import webrepl
|
||||||
#webrepl.start()
|
webrepl.start()
|
||||||
gc.collect()
|
gc.collect()
|
||||||
|
@ -92,8 +92,8 @@ class Encoder(object):
|
|||||||
def value(self):
|
def value(self):
|
||||||
return self._value // self.clicks
|
return self._value // self.clicks
|
||||||
|
|
||||||
def reset(self):
|
def reset(self, val=0):
|
||||||
self._value = 0
|
self._value = val
|
||||||
|
|
||||||
|
|
||||||
def test(enc=None, **kwargs):
|
def test(enc=None, **kwargs):
|
||||||
|
131
code/main.py
131
code/main.py
@ -13,51 +13,120 @@
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
Eclairage à LED Neopixel
|
Eclairage à LED Neopixel
|
||||||
|
avec contrôle (encodeur rotatif)
|
||||||
"""
|
"""
|
||||||
__author__ = "arofarn"
|
__author__ = "arofarn"
|
||||||
__version__ = 0.1
|
__version__ = 0.2
|
||||||
|
|
||||||
|
# Imports
|
||||||
|
|
||||||
import time
|
import time
|
||||||
import machine
|
import machine
|
||||||
import neopixel
|
import neopixel
|
||||||
|
from uos import uname
|
||||||
|
from encoder import Encoder
|
||||||
|
|
||||||
NB_PIX = 8 # Nombre de pixel
|
#Paramètres
|
||||||
LUMINOSITE = 1.0 # Luminosité (0.0-1.0)
|
|
||||||
NEOPIX_PIN = 2 # N° de la broche de contrôle des néopixels
|
|
||||||
|
|
||||||
pixel_strip = neopixel.NeoPixel(machine.Pin(NEOPIX_PIN), NB_PIX)
|
NB_PIX = 8 # Nombre de pixels
|
||||||
|
MAX_LUM = 100 # Luminosité max (100 max.)
|
||||||
|
COULEUR = [255, 255, 255] # Couleur de base (à luminosité max)
|
||||||
|
|
||||||
|
#Déclaration des objets et initialisation des variables
|
||||||
|
|
||||||
|
board_type = uname()[0]
|
||||||
|
print("Système :", board_type)
|
||||||
|
|
||||||
|
# Neopixels
|
||||||
|
|
||||||
|
if board_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_BOUTON = 14 # broche du clic central de l'encodeur
|
||||||
|
|
||||||
|
# With ESP8266 (no timing parameter)
|
||||||
|
pixel_strip = neopixel.NeoPixel(machine.Pin(NEOPIX_PIN), NB_PIX)
|
||||||
|
|
||||||
|
elif board_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_BOUTON = 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
|
||||||
|
pixel_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")
|
||||||
|
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
|
#Eteint tout à l'initialisation
|
||||||
pixel_strip.fill((0, 0, 0))
|
pixel_strip.fill([0, 0, 0])
|
||||||
pixel_strip.write()
|
pixel_strip.write()
|
||||||
|
|
||||||
|
# Encodeur rotatif
|
||||||
|
enc = Encoder(ENC_PIN_B, ENC_PIN_A,
|
||||||
|
min_val=0, max_val=MAX_LUM,
|
||||||
|
clicks=1)
|
||||||
|
|
||||||
|
#Bouton
|
||||||
|
bout = machine.Pin(ENC_BOUTON, machine.Pin.IN)
|
||||||
|
|
||||||
|
#Variable d'état
|
||||||
|
LUM = 0 # Luminosité (0 - 100)
|
||||||
|
PWR = False # Est-ce que l'éclairage est allumé ?
|
||||||
|
BOUT_PRESS_STATE = 1
|
||||||
|
|
||||||
|
def update_neopixel(np_strp, coul, lum=50):
|
||||||
|
"""Update NeoPixel strip with one color
|
||||||
|
:param np_strp : NeoPixel object
|
||||||
|
:param coul : color [R, G, B]
|
||||||
|
:param lum : brightness 0-100 (default: 50)
|
||||||
|
"""
|
||||||
|
col_tmp = [0, 0, 0]
|
||||||
|
|
||||||
|
for i in range(3):
|
||||||
|
col_tmp[i] = int(coul[i] * lum / 100)
|
||||||
|
|
||||||
|
pixel_strip.fill(col_tmp)
|
||||||
|
pixel_strip.write()
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
for i in range(NB_PIX):
|
|
||||||
print("blanc")
|
|
||||||
pixel_strip[i] = (255, 255, 255)
|
|
||||||
pixel_strip.write()
|
|
||||||
time.sleep(0.5)
|
|
||||||
|
|
||||||
for i in range(NB_PIX):
|
# Si on a un changement de valeur, on met à jour la luminisité et l'éclairage
|
||||||
print("éteint")
|
# des NeoPixel
|
||||||
pixel_strip[i] = (0, 0, 0)
|
if PWR and LUM != enc.value:
|
||||||
pixel_strip.write()
|
print(enc.value)
|
||||||
time.sleep(0.5)
|
LUM = enc.value
|
||||||
|
update_neopixel(pixel_strip, COULEUR, LUM)
|
||||||
|
|
||||||
for i in range(NB_PIX):
|
# Quand le bouton central est appuyé puis relâché, on allume ou on éteint
|
||||||
print("rouge")
|
if bout.value() == 0 and BOUT_PRESS_STATE == 1:
|
||||||
pixel_strip[i] = (255, 0, 0)
|
PWR = not PWR
|
||||||
pixel_strip.write()
|
print("Power :", PWR)
|
||||||
time.sleep(0.5)
|
if not PWR:
|
||||||
|
# Extinction des LED
|
||||||
|
pixel_strip.fill((0, 0, 0))
|
||||||
|
pixel_strip.write()
|
||||||
|
else:
|
||||||
|
# On remet l'encodeur à la dernière luminosité connue
|
||||||
|
# pour ignorer les mouvements pendant l'extinction
|
||||||
|
enc.reset(LUM)
|
||||||
|
update_neopixel(pixel_strip, COULEUR, LUM)
|
||||||
|
|
||||||
for i in range(NB_PIX):
|
BOUT_PRESS_STATE = bout.value()
|
||||||
print("vert")
|
|
||||||
pixel_strip[i] = (0, 255, 0)
|
|
||||||
pixel_strip.write()
|
|
||||||
time.sleep(0.5)
|
|
||||||
|
|
||||||
for i in range(NB_PIX):
|
time.sleep_ms(20)
|
||||||
print("bleu")
|
|
||||||
pixel_strip[i] = (0, 0, 255)
|
|
||||||
pixel_strip.write()
|
|
||||||
time.sleep(0.5)
|
|
||||||
|
Loading…
Reference in New Issue
Block a user