ESP32-S3-Zero/CircuitPython read single key using keypad.Keys and control onboard RGB LED (NeoPixel)

ESP32-S3-Zero/CircuitPython exercises to read single key using keypad.Keys and control onboard RGB LED (NeoPixel).


neopixel is a CircuitPython NeoPixel strip driver.
keypad module provides native support to scan sets of keys or buttons, connected independently to individual pins, connected to a shift register, or connected in a row-and-column matrix.

(misspelled filename, it's S3)

cpy_C3-Zero_RGB.py
"""
ESP32-S3-Zero/circuitpython 9.0.0-beta.0 exercise
to control on-board RGB (NeoPixel)
"""
import os, sys
import time
import neopixel
import board

def cycleNeopixel(wait):
    for r in range(255):
        pixel[0] = (r, 0, 0)
        time.sleep(wait)
    for r in range(255, 0, -1):
        pixel[0] = (r, 0, 0)
        time.sleep(wait)
        
    for g in range(255):
        pixel[0] = (0, g, 0)
        time.sleep(wait)
    for g in range(255, 0, -1):
        pixel[0] = (0, g, 0)
        time.sleep(wait)
        
    for b in range(255):
        pixel[0] = (0, 0, b)
        time.sleep(wait)
    for b in range(255, 0, -1):
        pixel[0] = (0, 0, b)
        time.sleep(wait)
        
print("=================================================")
info = sys.implementation[0] + ' ' + os.uname()[3] + '\n' + \
       'run on ' + os.uname()[4]
print(info)
print("- Control onboard RGB (NeoPixel) -")
print("=================================================")
print("board.NEOPIXEL:", board.NEOPIXEL)
print()

# Create the NeoPixel object
pixel = neopixel.NeoPixel(board.NEOPIXEL,
                          1,
                          pixel_order=neopixel.RGB)
pixel[0] = (0, 0, 0)
time.sleep(2.0)

cycleNeopixel(0.005)

pixel[0] = (0, 0, 0)
time.sleep(2.0)

print("- bye -\n")


cpy_C3-Zero_key_RGB.py
"""
CircuitPython exercise of using keypad.Keys for single key detection.
ref:
https://docs.circuitpython.org/en/latest/shared-bindings/keypad/index.html#keypad.Keys
"""
import os, sys
import board
import keypad
import neopixel
import time

print("=================================================")
info = sys.implementation[0] + ' ' + os.uname()[3] + '\n' + \
       'run on ' + os.uname()[4]
print(info)
print("- Exercise of using keypad.Keys for single key (board.IO7) detection:")
print("  once the key pressed, change onboard RGB (NeoPixel) sequently.")
print("=================================================")

# Create the NeoPixel object
pixel = neopixel.NeoPixel(board.NEOPIXEL,
                          1,
                          pixel_order=neopixel.RGB)
pixel[0] = (0, 0, 0)
time.sleep(0.5)

rgb_tuple = (((50, 50, 50), "ALL ON"),
             ((50, 0, 0), "RED"),
             ((0, 50, 0), "GREEN"),
             ((0, 0, 50), "BLUE"),
             ((0, 0, 0), "ALL OFF"))
for i in range(5):
    pixel[0] = rgb_tuple[i][0]
    print(rgb_tuple[i][1])
    time.sleep(0.5)
    
keys = keypad.Keys((board.IO7,), value_when_pressed=False, pull=True)

rgb_index = len(rgb_tuple)-1
while True:
    event = keys.events.get()
    # event will be None if nothing has happened.
    if event:
        if event.pressed and event.key_number==0:
            rgb_index = rgb_index+1
            if rgb_index >= len(rgb_tuple):
                rgb_index=0
            pixel[0] = rgb_tuple[rgb_index][0]
            print(rgb_tuple[rgb_index][1])

            



Comments

Popular posts from this blog

MicroPython/ESP32-C3 + 1.8" 128x160 TFT ST7735 SPI, using boochow/MicroPython-ST7735 library.

CameraWebServe: ESP32-S3 (arduino-esp32) + OV5640 camera module