import neopixel
from color_hsv import colorHSV
from time import sleep
from machine import Pin, Timer
import machine
import array
import time
from machine import RTC
import ntptime
import network
## Setup
NUM_LEDS = 12
LED_PIN = 12
B1_PIN = 14
print("Connecting to WiFi", end="")
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.connect('Wokwi-GUEST', '')
while not sta_if.isconnected():
print(".", end="")
time.sleep(0.1)
print(" Connected!")
def set_time(rtc, tz):
ntptime.settime()
date = list(rtc.datetime())
date[4] = date[4] + tz
rtc.datetime(date)
#def get_button():
# return not button.value()
def button_press_function(pin):
global b1
b1 = b1 + 1
global interrupt_pin
interrupt_pin = pin
#print("pressed")
print(b1)
def button_released_function():
print("released")
print(b1)
rtc = RTC()
set_time(rtc, 1)
#rtc.datetime((2022, 11, 20, 1, 12, 48, 0, 0)) # set a specific date and time
#rtc.datetime() # get date and time
button = Pin(B1_PIN, Pin.IN, Pin.PULL_UP)
b1 = 0
button.irq(trigger=Pin.IRQ_RISING, handler=button_press_function)
px = neopixel.NeoPixel(machine.Pin(LED_PIN), NUM_LEDS)
## Constants
red = colorHSV(0, 255, 255)
green = colorHSV(21845, 255, 255)
blue = colorHSV(43691, 255, 255)
def hms_2bits():
global px
global b1
global NUM_LEDS
## led_s = array.array('i', (0 for _ in range(6)))
led_h = []
led_m = []
led_s = []
tm = time.localtime(time.time())
h = tm[3]
m = tm[4]
s = tm[5]
print("s: ", s)
##set_color(0, 0, 0)
led_h = [0 for i in range(6)]
led_m = [0 for i in range(6)]
led_s = [0 for i in range(6)]
for bit in range(0, 6, 1):
if (b1 % 2) != 0:
if h & 1 == 1:
led_h[bit] = 1
h = h >> 1
if m & 1 == 1:
led_m[bit] = 1
m = m >> 1
if s & 1 == 1:
led_s[bit] = 1
s = s >> 1
print("led_s: ", led_s)
for bit in range(0, 6, 1):
if led_h[bit] == 1:
px[NUM_LEDS -1 -bit] = blue
else:
px[NUM_LEDS -1 -bit] = (0, 0, 0)
if led_m[bit] == 1:
px[bit] = red
else:
px[bit] = (0, 0, 0)
if (b1 % 2) == 0:
pix = NUM_LEDS -1 -bit
if led_s[bit] == 1:
if led_h[bit] == 1:
px[pix] = (200, 0, 255);
else:
px[pix] = (200, 0, 200);
else:
if led_h[bit] == 0:
px[pix] = (0, 0, 0)
px.write()
#if get_button() == 1:
# button_press_function()
#else:
# button_released_function()
print("time: ", tm)
def set_color(r, g, b):
global px
global NUM_LEDS
for p in range(0, NUM_LEDS, 1):
px[p] = (r, g, b)
## px[p] = red
## print("p: ", p)
px.write()
def handler_0(timer0):
hms_2bits()
timer0 = Timer(0)
timer0.init(period=4000, mode=Timer.PERIODIC, callback=handler_0)
## Loop
while not True:
## for hue in range(65534 , 43691, -500):
for hue in range(65534 , 63691, -500):
color = colorHSV(hue, 255, 255)
#px[11] = color
#px.write()
#sleep(0.05)
hms_2bits()
sleep(100)
print("loop")