# mPy - neopixel demonstration on ESP32 pin 25
#       intensity as 0..100%
# Provided by Anthony Vogelaar, Perth Scotland
# rev 0.10  2025-05-20  initial release

from	machine		import	Pin
import	time
from	array		import	array
import	neopixel	as		npx

# ====================== Hardware setup ===========================

leds = npx.NeoPixel(Pin(25), 6)		# Setup for 6 leds on Pin 25

# ================== Neopixel constants ===========================

# convert percentage x (0..100) into byte 0..255 
def perc(x):
    return int(x * 2.55)


GREEN   = array('i', [perc(100), perc(  0), perc(  0)])		# 'i' is 32 bit int
RED     = array('i', [perc(  0), perc(100), perc(  0)])
BLUE    = array('i', [perc(  0), perc(  0), perc(100)])
YELLOW  = array('i', [perc(100), perc(100), perc(  0)])
MAGENTA = array('i', [perc(  0), perc(100), perc(100)])
CYAN    = array('i', [perc(100), perc(  0), perc(100)])
WHITE   = array('i', [perc(100), perc(100), perc(100)])
OFF     = array('i', [perc(  0), perc(  0), perc(  0)])

# ========================== Local ================================

col = RED

def pos_0():
    leds[0]=col; leds[1]=OFF; leds[2]=OFF; leds[3]=OFF; leds[4]=OFF; leds[5]=OFF
    leds.write()
    
def pos_1():
    leds[0]=OFF; leds[1]=col; leds[2]=OFF; leds[3]=OFF; leds[4]=OFF; leds[5]=OFF
    leds.write()


def pos_2():
    leds[0]=OFF; leds[1]=OFF; leds[2]=col; leds[3]=OFF; leds[4]=OFF; leds[5]=OFF
    leds.write()


def pos_3():
    leds[0]=OFF; leds[1]=OFF; leds[2]=OFF; leds[3]=col; leds[4]=OFF; leds[5]=OFF
    leds.write()


def pos_4():
    leds[0]=OFF; leds[1]=OFF; leds[2]=OFF; leds[3]=OFF; leds[4]=col; leds[5]=OFF
    leds.write()


def pos_5():
    leds[0]=OFF; leds[1]=OFF; leds[2]=OFF; leds[3]=OFF; leds[4]=OFF; leds[5]=col
    leds.write()

# =========================== Main ================================

def main():
    dly = 1000
    while True:
        pos_0(); time.sleep_ms(dly)
        pos_1(); time.sleep_ms(dly)
        pos_2(); time.sleep_ms(dly)
        pos_3(); time.sleep_ms(dly)
        pos_4(); time.sleep_ms(dly)
        pos_5(); time.sleep_ms(dly)
    
    
main()

# ============================ End =================================
