11
11
import uasyncio
12
12
import urequests
13
13
import time
14
+ import random
14
15
from machine import Timer , Pin
15
16
from stellar import StellarUnicorn
16
- from picographics import PicoGraphics , DISPLAY_STELLAR_UNICORN as DISPLAY
17
-
18
- URL = 'http://api.thingspeak.com/channels/1417/field/2/last.json'
19
-
20
- UPDATE_INTERVAL = 113 # refresh interval in secs. Be nice to free APIs!
21
- # this esoteric number is used so that a column of LEDs equates (approximately) to an hour
17
+ from picographics import PicoGraphics , DISPLAY_STELLAR_UNICORN as DISPLAY , PEN_P8 as PEN
18
+
19
+ URL = 'http://api.thingspeak.com/channels/1417/field/1/last.txt'
20
+
21
+ UPDATE_INTERVAL = 60 * 60 / 16 # refresh interval in secs. Be nice to free APIs!
22
+ # Calculated as 60 minutes * 60 seconds divided by number of pixels per row
23
+ # so that a row of LEDs equates (approximately) to an hour
24
+
25
+ CHEERLIGHTS_COLOR_VALUES = [
26
+ (0x00 , 0x00 , 0x00 ), # Black/Unlit
27
+ (0xFF , 0x00 , 0x00 ),
28
+ (0x00 , 0x80 , 0x00 ),
29
+ (0x00 , 0x00 , 0xFF ),
30
+ (0x00 , 0xFF , 0xFF ),
31
+ (0xFF , 0xFF , 0xFF ),
32
+ (0xFD , 0xF5 , 0xE6 ),
33
+ (0x80 , 0x00 , 0x80 ),
34
+ (0xFF , 0x00 , 0xFF ),
35
+ (0xFF , 0xFF , 0x00 ),
36
+ (0xFF , 0xA5 , 0x00 ),
37
+ (0xFF , 0xC0 , 0xCB ),
38
+ ]
39
+
40
+ CHEERLIGHTS_COLOR_NAMES = [
41
+ "black" , # Black/Unlit, not part of cheerlights colours
42
+ "red" ,
43
+ "green" ,
44
+ "blue" ,
45
+ "cyan" ,
46
+ "white" ,
47
+ "oldlace" ,
48
+ "purple" ,
49
+ "magenta" ,
50
+ "yellow" ,
51
+ "orange" ,
52
+ "pink"
53
+ ]
22
54
23
55
24
56
def status_handler (mode , status , ip ):
@@ -32,71 +64,62 @@ def status_handler(mode, status, ip):
32
64
print ('Wifi connection failed!' )
33
65
34
66
35
- def hex_to_rgb (hex ):
36
- # converts a hex colour code into RGB
37
- h = hex .lstrip ('#' )
38
- r , g , b = (int (h [i :i + 2 ], 16 ) for i in (0 , 2 , 4 ))
39
- return r , g , b
40
-
41
-
42
67
def get_data ():
68
+ global index
43
69
# open the json file
44
- print (f'Requesting URL: { URL } ' )
45
- r = urequests .get (URL )
46
- # open the json data
47
- j = r .json ()
48
- print ('Data obtained!' )
49
- r .close ()
70
+ if UPDATE_INTERVAL >= 60 :
71
+ print (f'Requesting URL: { URL } ' )
72
+ r = urequests .get (URL )
73
+ name = r .content .decode ("utf-8" ).strip ()
74
+ r .close ()
75
+ print ('Data obtained!' )
76
+
77
+ else :
78
+ print ("Random test colour!" )
79
+ # For sped-up testing we don't want to hit the API at all
80
+ name = random .choice (CHEERLIGHTS_COLOR_NAMES [1 :])
50
81
51
82
# flash the onboard LED after getting data
52
83
pico_led .value (True )
53
84
time .sleep (0.2 )
54
85
pico_led .value (False )
55
86
56
- # extract hex colour from the json data
57
- hex = j ['field2' ]
58
-
59
87
# add the new hex colour to the end of the array
60
- colour_array .append (hex )
61
- print (f'Colour added to array: { hex } ' )
62
- # remove the oldest colour in the array
63
- colour_array .pop (0 )
64
- update_leds ()
65
-
66
-
67
- def update_leds ():
68
- # light up the LEDs
69
- # this step takes a second, it's doing a lot of hex_to_rgb calculations!
70
- print ("Updating LEDs..." )
71
- i = 0
72
- for x in range (width ):
73
- for y in range (height ):
74
- r , g , b = hex_to_rgb (colour_array [i ])
75
-
76
- current_colour = graphics .create_pen (r , g , b )
77
- graphics .set_pen (current_colour )
78
- graphics .pixel (x , y )
79
- i = i + 1
88
+ if index == (width * height ):
89
+ index = 0
90
+ graphics .clear ()
91
+
92
+ colour_array [index ] = CHEERLIGHTS_COLOR_NAMES .index (name )
93
+ index += 1
94
+ print (f'Colour added to array: { name } ' )
95
+
80
96
su .update (graphics )
81
97
print ("LEDs updated!" )
82
98
83
99
84
100
su = StellarUnicorn ()
85
- graphics = PicoGraphics (DISPLAY )
86
-
87
101
width = StellarUnicorn .WIDTH
88
102
height = StellarUnicorn .HEIGHT
89
103
104
+ # set up a buffer to store the colours
105
+ colour_array = bytearray (width * height )
106
+
107
+ # We'll use palette mode, so just make the colour list the display buffer
108
+ graphics = PicoGraphics (DISPLAY , pen_type = PEN , buffer = colour_array )
109
+
110
+ # Set up the palette with cheerlights colour values
111
+ graphics .set_palette (CHEERLIGHTS_COLOR_VALUES )
112
+ graphics .set_pen (0 )
113
+ graphics .clear ()
114
+
115
+ # Keep track of the pixel we're lighting
116
+ index = 0
117
+
90
118
su .set_brightness (0.5 )
91
119
92
120
# set up the Pico W's onboard LED
93
121
pico_led = Pin ('LED' , Pin .OUT )
94
122
95
- current_colour = graphics .create_pen (0 , 0 , 0 )
96
-
97
- # set up an list to store the colours
98
- colour_array = ["#000000" ] * 1024
99
-
100
123
# set up wifi
101
124
try :
102
125
network_manager = NetworkManager (WIFI_CONFIG .COUNTRY , status_handler = status_handler )
@@ -109,19 +132,19 @@ def update_leds():
109
132
110
133
# start timer (the timer will call the function to update our data every UPDATE_INTERVAL)
111
134
timer = Timer (- 1 )
112
- timer .init (period = UPDATE_INTERVAL * 1000 , mode = Timer .PERIODIC , callback = lambda t : get_data ())
135
+ timer .init (period = int ( UPDATE_INTERVAL * 1000 ) , mode = Timer .PERIODIC , callback = lambda t : get_data ())
113
136
114
137
while True :
115
138
# adjust brightness with LUX + and -
116
139
# LEDs take a couple of secs to update, so adjust in big (10%) steps
117
140
if su .is_pressed (StellarUnicorn .SWITCH_BRIGHTNESS_UP ):
118
141
su .adjust_brightness (+ 0.1 )
119
- update_leds ( )
142
+ su . update ( graphics )
120
143
print (f"Brightness set to { su .get_brightness ()} " )
121
144
122
145
if su .is_pressed (StellarUnicorn .SWITCH_BRIGHTNESS_DOWN ):
123
146
su .adjust_brightness (- 0.1 )
124
- update_leds ( )
147
+ su . update ( graphics )
125
148
print (f"Brightness set to { su .get_brightness ()} " )
126
149
127
150
# pause for a moment (important or the USB serial device will fail)
0 commit comments