def doTestSlicedRval(self, mem): # A chain slice can be read leds = WS2812(spi_bus=1, led_count=9, mem=mem) self.assertTrue(all(isinstance(v, Pixel) for v in leds[:3])) self.assertTrue(all(isinstance(v, Pixel) for v in leds[2:5])) self.assertTrue(all(isinstance(v, Pixel) for v in leds[7:11])) for i in range(len(leds)): leds[i] = (i, 2 * i, 3 * i) for k, led in enumerate(leds[3:6]): i = k + 3 self.assertEqual(tuple(led), (i, 2 * i, 3 * i)) self.assertEqual(list(tuple(led) for led in leds[-2:]), \ [(i, 2*i, 3*i) for i in (7,8)]) self.assertEqual(list(tuple(led) for led in leds[:]), \ [(i, 2*i, 3*i) for i in range(len(leds))]) # Negative index can be used i = len(leds) - 1 self.assertEqual(tuple(leds[-1]), (i, 2 * i, 3 * i)) i = len(leds) - 5 self.assertEqual(tuple(leds[-5]), (i, 2 * i, 3 * i)) i = 0 self.assertEqual(tuple(leds[-len(leds)]), (i, 2 * i, 3 * i)) # Negative index doesn't blow up unallocated leds = WS2812(spi_bus=1, led_count=66, mem=mem) sum_neg = sum( sum([leds[i].r, leds[i].g, leds[i].b]) for i in range(-1, -len(leds), -1)) sum_pos = sum( sum([leds[i].r, leds[i].g, leds[i].b]) for i in range(len(leds))) self.assertEqual(sum_neg, 0) self.assertEqual(sum_pos, 0)
def doTestRotateInset(self, mem): # A WSlice that starts past the beginning of the underlying WS2812 can be rotated ws = WS2812(spi_bus=1, led_count=7, mem=mem) for i in range(len(ws)): ws[i] = b'foo' self.assertTrue(all(bytes(ws[i]) == b'foo' for i in range(len(ws)))) leds = WSlice(ws, 2, 5) n = len(leds) for i, t in zip(range(n), tg(n,0)): leds[i] = t self.assertEqual([tuple(led) for led in leds], [(0,1,2), (3,4,5), (6,7,8)]) self.assertTrue(all(bytes(ws[i]) == b'foo' for i in range(len(ws)) if i not in range(2,5))) leds.cw() self.assertEqual([tuple(led) for led in leds], [(3,4,5), (6,7,8), (0,1,2)]) self.assertTrue(all(bytes(ws[i]) == b'foo' for i in range(len(ws)) if i not in range(2,5))) leds.cw() self.assertEqual([tuple(led) for led in leds], [(6,7,8), (0,1,2), (3,4,5)]) self.assertTrue(all(bytes(ws[i]) == b'foo' for i in range(len(ws)) if i not in range(2,5))) leds.cw() self.assertEqual([tuple(led) for led in leds], [(0,1,2), (3,4,5), (6,7,8)]) self.assertTrue(all(bytes(ws[i]) == b'foo' for i in range(len(ws)) if i not in range(2,5))) leds.ccw() self.assertEqual([tuple(led) for led in leds], [(6,7,8), (0,1,2), (3,4,5)]) self.assertTrue(all(bytes(ws[i]) == b'foo' for i in range(len(ws)) if i not in range(2,5))) leds.ccw() self.assertEqual([tuple(led) for led in leds], [(3,4,5), (6,7,8), (0,1,2)]) self.assertTrue(all(bytes(ws[i]) == b'foo' for i in range(len(ws)) if i not in range(2,5))) leds.ccw() self.assertEqual([tuple(led) for led in leds], [(0,1,2), (3,4,5), (6,7,8)]) self.assertTrue(all(bytes(ws[i]) == b'foo' for i in range(len(ws)) if i not in range(2,5))) ws = WS2812(spi_bus=1, led_count=90, mem=mem) for n in (1, 2, 7, 16, 33, 70):#, 190, 244, 400): leds = ref = None gc.collect() leds_start = n//7 leds_end = n + n//7 leds = WSlice(ws, leds_start, leds_end) self.assertEqual(len(leds), n) for i, t in enumerate(tg(n,0)): leds[i] = t ref = list(tuple(leds[i]) for i in range(n)) #print("\n\nstart %d, end %d" % (leds_start, leds_end)) #print("ref", ref) #print("leds", leds[:]) for k in range(n): #print("\nk", k, "leds", leds[:]) #print("leds before cw:", [tuple(leds[i]) for i in range(n)]) leds.cw() #print(" leds after cw:", [tuple(leds[i]) for i in range(n)]) #print("ref:", [ref[(k+1+i)%n] for i in range(n)]) self.assertTrue(all(tuple(leds[i]) == ref[(k+1+i)%n] for i in range(n))) for k in range(n): leds.ccw() self.assertTrue(all(tuple(leds[i]) == ref[(-(k+1)+i)%n] for i in range(n)))
def doTestRotatePart(self, mem): # Some part of a WSlice that starts past the beginning of the # underlying WS2812 can be rotated ws = WS2812(spi_bus=1, led_count=8, mem=mem) for i in range(len(ws)): ws[i] = b'foo' self.assertTrue(all(bytes(ws[i]) == b'foo' for i in range(len(ws)))) leds = WSlice(ws, 2, 6) n = len(leds) for i, t in zip(range(n), tg(n,0)): leds[i] = t self.assertTrue(all(bytes(ws[i]) == b'foo' for i in range(len(ws)) if i not in range(2,6))) self.assertEqual([tuple(led) for led in leds], [(0,1,2), (3,4,5), (6,7,8), (9,10,11)]) leds.cw(1) self.assertTrue(all(bytes(ws[i]) == b'foo' for i in range(len(ws)) if i not in range(2,6))) self.assertEqual([tuple(led) for led in leds], [(0,1,2), (6,7,8), (9,10,11), (3,4,5)]) leds.cw(1) self.assertTrue(all(bytes(ws[i]) == b'foo' for i in range(len(ws)) if i not in range(2,6))) self.assertEqual([tuple(led) for led in leds], [(0,1,2), (9,10,11), (3,4,5), (6,7,8)]) leds.cw(1) self.assertTrue(all(bytes(ws[i]) == b'foo' for i in range(len(ws)) if i not in range(2,6))) self.assertEqual([tuple(led) for led in leds], [(0,1,2), (3,4,5), (6,7,8), (9,10,11)]) leds.ccw(1) self.assertTrue(all(bytes(ws[i]) == b'foo' for i in range(len(ws)) if i not in range(2,6))) self.assertEqual([tuple(led) for led in leds], [(0,1,2), (9,10,11), (3,4,5), (6,7,8)]) leds.ccw(1) self.assertTrue(all(bytes(ws[i]) == b'foo' for i in range(len(ws)) if i not in range(2,6))) self.assertEqual([tuple(led) for led in leds], [(0,1,2), (6,7,8), (9,10,11), (3,4,5)]) leds.ccw(1) self.assertTrue(all(bytes(ws[i]) == b'foo' for i in range(len(ws)) if i not in range(2,6))) self.assertEqual([tuple(led) for led in leds], [(0,1,2), (3,4,5), (6,7,8), (9,10,11)])
def testSizes(self): gc.collect() m0 = gc.mem_free() leds = WS2812(spi_bus=1, led_count=256, mem=PREALLOCATE) gc.collect() m1 = gc.mem_free() print((m1 - m0) / 256)
def test_Slice_sliced_rval(self): ws = WS2812(1, 8) lights = Lights(ws) # Fill the lattice with a recognizable pattern for i, p in enumerate(tg(len(lights), 0)): lat = lights.lattice[i] for j, c in enumerate(p): lat[j] = c sls = lights[:2] # A sliced Lights has the correct length self.assertEqual(len(sls), 2) # A sliced Lights refers to the same lattice, not a copy self.assertIs(sls.lattice, lights.lattice) # A sliced Lights iterates over the correct lattice points self.assertEqual(list(sls), [bytearray(v) for v in tg(2,0)]) # A Lights can be sliced non-trivially and iterate over the # correct lattice points sls = lights[-2:-6:-2] self.assertEqual(len(sls), 2) self.assertEqual(list(sls), [bytearray(v) for v in [(18,19,20), (12,13,14)]])
def test_Slice_sliced_lval(self): ws = WS2812(1, 8) lights = Lights(ws) # Fill the lattice with a recognizable pattern for i, p in enumerate(tg(len(lights), 0)): lat = lights.lattice[i] for j, c in enumerate(p): lat[j] = c ref = lights.lattice[:] sls = lights[-2:-6:-2] # A Lights is indexable by an int for writing sls[1] = (10, 9, 8) self.assertEqual(list(sls), [bytearray(v) for v in [(18,19,20), (10,9,8)]]) # A Lights is indexable by a slice for writing sls[:] = [b'foo', b'bar'] self.assertEqual(list(sls), [bytearray(v) for v in [b'foo', b'bar']]) # The writes happen in the right place self.assertEqual([lights[6], lights[4]], [bytearray(v) for v in [b'foo', b'bar']]) # Other places don't get written self.assertEqual(list((i, lights[i]) for i in range(len(lights)) if i not in (6,4)), list((i, t) for i, t in enumerate(tg(len(lights), 0)) if i not in (6,4)))
def setUp(self): #logging.basicConfig(level=logging.INFO) #random.seed("WSlice") self.ws = ws = WS2812(1, 64) for i in range(len(ws)): ws[i] = (i, 2 * i, 3 * i) self.p = Percolator(ws)
def doTestGrindSinglePixel(self, mem): # get / set work as expected leds = WS2812(spi_bus=1, led_count=1, intensity=1, mem=mem) for i in range(1000): r = leds[0].r = random.getrandbits(8) g = leds[0].g = random.getrandbits(8) b = leds[0].b = random.getrandbits(8) self.assertEqual(list(leds[0]), [r, g, b])
def testMemoryUsed8(self): leds = WS2812(spi_bus=1, led_count=64, mem=PREALLOCATE) prev_mem_free = gc.mem_free() for i in range(8): r, g, b = leds[i].r, leds[i].g, leds[i].b # no leak delta_mem = gc.mem_free() - prev_mem_free if platform == 'pyboard': self.assertEqual(delta_mem, 0)
def testMemoryUsed2(self): leds = WS2812(spi_bus=1, led_count=64, mem=PREALLOCATE) prev_mem_free = gc.mem_free() for i in range(8): leds[0] = b'\x08\x00\x00' # no leak delta_mem = gc.mem_free() - prev_mem_free if platform == 'pyboard': self.assertEqual(delta_mem, 0)
def doTestPixelAccess(self, mem): # A WSlice gives a view into the underlying WS2912 ws = WS2812(spi_bus=1, led_count=99, mem=mem) ws.fill_buf(tg(len(ws), 0)) # A simple slice has the correct length leds = WSlice(ws, 3, 6) #print("ws:", ws[:8], "...") #print("leds:", leds[:]) self.assertEqual(len(leds), 3) # A simple slice has the correct values self.assertEqual([tuple(led) for led in leds], [tuple(v) for v in tg(len(leds), 3*3)]) # The end can be negative, with correct slice behavior leds = WSlice(ws, len(ws)-5, -2) self.assertEqual(len(leds), 3) self.assertEqual([tuple(led) for led in leds], [tuple(v) for v in tg(len(leds), 3*(len(ws)-5))]) # The start and end can be negative, with correct slice behavior leds = WSlice(ws, 3 - len(ws), 6 - len(ws)) self.assertEqual(len(leds), 3) self.assertEqual([tuple(led) for led in leds], [tuple(v) for v in tg(len(leds), 3*3)]) # The start can be negative, with correct slice behavior leds = WSlice(ws, 3 - len(ws), 6) self.assertEqual(len(leds), 3) self.assertEqual([tuple(led) for led in leds], [tuple(v) for v in tg(len(leds), 3*3)]) # These are true for multiple test values for n in (1, 2, 7, 16, 33, 70): leds = WSlice(ws, n, n+5) self.assertEqual(len(leds), 5) self.assertTrue(all(v==r) for v, r in \ zip(tuple(led) for led in leds, tuple(v) for v in tg(len(leds), 3*n))) leds = WSlice(ws, len(ws)-(n+5), -n) self.assertEqual(len(leds), 5) self.assertTrue(all(v==r) for v, r in \ zip(tuple(led) for led in leds, tuple(v) for v in tg(len(leds), 3*(len(ws)-(n+5))))) leds = WSlice(ws, -(n+5), -n) self.assertEqual(len(leds), 5) self.assertTrue(all(v==r) for v, r in \ zip(tuple(led) for led in leds, tuple(v) for v in tg(len(leds), 3*(len(ws)-(n+5))))) leds = WSlice(ws, -(n+5), len(ws)-n) self.assertEqual(len(leds), 5) self.assertTrue(all(v==r) for v, r in \ zip(tuple(led) for led in leds, tuple(v) for v in tg(len(leds), 3*(len(ws)-(n+5)))))
def testMemoryUsed11(self): leds = WS2812(spi_bus=1, led_count=64, mem=PREALLOCATE) foolist = list(range(3)) prev_mem_free = gc.mem_free() for i in range(8): leds[i] = foolist # no leak delta_mem = gc.mem_free() - prev_mem_free if platform == 'pyboard': self.assertEqual(delta_mem, 0)
def testMemoryUsed4(self): leds = WS2812(spi_bus=1, led_count=64, mem=PREALLOCATE) bar = bytearray(range(3)) prev_mem_free = gc.mem_free() for i in range(8): leds[0] = bar # no leak delta_mem = gc.mem_free() - prev_mem_free if platform == 'pyboard': self.assertEqual(delta_mem, 0)
def testMemoryUsed10(self): leds = WS2812(spi_bus=1, led_count=64, mem=PREALLOCATE) prev_mem_free = gc.mem_free() for i in range(8): for k in range(len(leds[i])): # no leak leds[i][k] = leds[i - 1][k] delta_mem = gc.mem_free() - prev_mem_free if platform == 'pyboard': self.assertEqual(delta_mem, 0)
def doTestMultiPixelFedIterator(self, mem): # A chain can be fed from an iterator for n in range(1, 200, 19): leds = None gc.collect() leds = WS2812(spi_bus=1, led_count=n, mem=mem) leds.fill_buf(tg(n, 1)) for pix, pg in zip(leds, tg(n, 1)): self.assertEqual(list(pix), list(pg))
def setUp(self): #logging.basicConfig(level=logging.INFO) self.ws = WS2812(1, 8) self.lights = lights = Lights(self.ws) # Fill the lattice with a recognizable pattern for i, c in enumerate(tg(len(lights), 0)): p = lights.lattice[i] for j, v in enumerate(c): p[j] = v
def setUp(self): #logging.basicConfig(level=logging.INFO) self.ws = WS2812(1, 8) self.lights = lights = Lights(self.ws) # Fill the lattice with a recognizable pattern for i, p in enumerate(tg(len(lights), 0)): lat = lights.lattice[i] for j, c in enumerate(p): lat[j] = c self.sls = lights[-2:-6:-2]
def doTestSinglePixel(self, mem): # buf is the correct length leds = WS2812(spi_bus=1, led_count=1, mem=mem) self.assertEqual(len(leds.buf), 13) # As-created the pixels are all off # leds can be accessed via iterator self.assertEqual(list(list(v) for v in leds), [[0] * 3]) # Individual leds can be accessed by indexing pix = leds[0] # pixels have r,g,b pix.r = 1 pix.g = 2 pix.b = 3 self.assertEqual([pix.r, pix.g, pix.b], [1, 2, 3]) self.assertEqual(list(pix), [1, 2, 3]) # Can get a named tuple of values p = leds.get_led_values(0) self.assertEqual([p.r, p.g, p.b], [1, 2, 3]) # pixels can also be indexed into for colors self.assertEqual(list(pix), [1, 2, 3]) for i in range(len(pix)): pix[i] = 12 * (i + 1) self.assertEqual(list(pix), [12, 24, 36]) # A pixel position in a chain can be mutated by setting it with a bytearray leds[0] = bytearray((7, 11, 92)) self.assertEqual(list(leds[0]), [7, 11, 92]) self.assertEqual([pix.r, pix.g, pix.b], [7, 11, 92]) # A pixel position in a chain can be mutated by setting it with bytes leds[0] = b'foo' self.assertEqual([pix.r, pix.g, pix.b], [102, 111, 111]) # A pixel position in a chain can be mutated by setting it with a list leds[0] = [11, 22, 33] self.assertEqual([pix.r, pix.g, pix.b], [11, 22, 33]) # A pixel position in a chain can be mutated by setting it with an iterator leds[0] = (7 * i + 3 for i in range(3)) self.assertEqual([pix.r, pix.g, pix.b], [3, 10, 17]) # The pixel.off() method works leds[0] = bytearray((7, 11, 92)) leds[0].off() self.assertEqual(list(leds[0]), [0] * 3)
def doTestSlicedLval(self, mem): # A chain slice can be written leds = WS2812(spi_bus=1, led_count=9, mem=mem) for i in range(len(leds)): leds[i] = (i, 2 * i, 3 * i) leds[0:3] = leds[3:6] for k in range(3): i = k + 3 self.assertEqual(tuple(leds[k]), (i, 2 * i, 3 * i)) for i in range(len(leds)): leds[i] = (i, 2 * i, 3 * i) leds[-3:] = leds[:3] for i in range(3): k = i + 6 self.assertEqual(tuple(leds[k]), (i, 2 * i, 3 * i))
def setUp(self): #logging.basicConfig(level=logging.INFO) ws = self.ws = WS2812(1, 8) lights = self.lights = Lights(ws) # Fill the lattice with a recognizable pattern for i, p in enumerate(tg(len(lights), 0)): lat = lights.lattice[i] for j, c in enumerate(p): lat[j] = c # The leds are all clear self.assertEqual(sum(sum(c) for c in ws), 0) self.gear = Gear(lights=lights)
def testRotatePlaces(self): # A chain can be rotated for n in range(1, 400, 19): leds = None gc.collect() leds = WS2812(spi_bus=1, led_count=n) leds.fill_buf(tg(n, 1)) for j in range(0, n, (n//7)+1): gc.collect() for pix, pg in zip(leds, tg(n, 1)): self.assertEqual(list(pix), list(pg)) #pixlist = list(list(pix) for pix in leds) #rpixlist = pixlist[-j:] + pixlist[:-j] leds.rotate_places(j) gc.collect() #self.assertEqual(list(list(pix) for pix in leds), rpixlist) leds.rotate_places(-j)
def doTestPixelAssignPixel(self, mem): # A pixel can be assigned to another pixel leds = WS2812(spi_bus=1, led_count=3, mem=mem) for i in range(len(leds)): leds[i] = (i, 2 * i, 3 * i) self.assertEqual(list(leds[0]), [0, 0, 0]) self.assertEqual(list(leds[1]), [1, 2, 3]) self.assertEqual(list(leds[2]), [2, 4, 6]) leds[0] = leds[2] leds[2] = leds[1] leds[1] = [19, 23, 29] self.assertEqual(list(leds[0]), [2, 4, 6]) self.assertEqual(list(leds[1]), [19, 23, 29]) self.assertEqual(list(leds[2]), [1, 2, 3]) self.assertIsNot(leds[0], leds[1]) self.assertIsNot(leds[0], leds[2]) self.assertIsNot(leds[1], leds[2])
def doTestRotate(self, mem): # A whole WSlice can be rotated ws = WS2812(spi_bus=1, led_count=75, mem=mem) for n in (1, 2, 7, 16, 33, 70):#, 190, 244, 400): leds = None t = None gc.collect() leds = WSlice(ws, 0, n) ws.fill_buf(tg(n, 0)) ref = list(tuple(t) for t in tg(n, 0)) self.assertTrue(all(tuple(leds[i]) == ref[i] for i in range(len(leds)))) for k in range(n): leds.cw() self.assertTrue(all(tuple(leds[i]) == ref[(k+1+i)%n] for i in range(n))) for k in range(n): leds.ccw() self.assertTrue(all(tuple(leds[i]) == ref[(-(k+1)+i)%n] for i in range(n)))
def __init__(self): self.ultrasonic_sensor = ADC(Pin("X4")) self.hall_sensor = Pin("X6", Pin.IN) self.pot = ADC(Pin("X8")) self.ring = WS2812(spi_bus=2, led_count=15) self.red = Colour(1, 0, 0) self.green = Colour(0.4, 1, 0) self.purple = Colour(0.4, 0, 1) self.off = Colour(0, 0, 0) self.magnet_detected = self.green self.ultrasound_detected = self.purple self.colour = self.red self.HISTORY_DEPTH = 15 self.history = [0 for i in range(self.HISTORY_DEPTH)] self.i = 0
def doTestPixelBufferBits(self, mem): leds = WS2812(spi_bus=1, led_count=1, mem=mem) if platform == 'pyboard': plati = 0 else: plati = 1 # As-created the pixels are all off # Off is represented correctly in the buffer self.assertEqual('|'.join('%x' % v for v in leds.buf), ('11|11|11|11|11|11|11|11|11|11|11|11|0', '0|0|0|0|0|0|0|0|0|0|0|0|0')[plati]) # All-ones is represented correctly in the buffer leds[0] = b'\xff\xff\xff' self.assertEqual(list(leds[0]), [255, 255, 255]) self.assertEqual('|'.join('%x' % v for v in leds.buf), ('33|33|33|33|33|33|33|33|33|33|33|33|0', 'ff|ff|ff|0|0|0|0|0|0|0|0|0|0')[plati]) pix = leds[0] # The colors are in the right place, affecting the correct bits in the buffer pix[0] = 2 pix[1] = 1 pix[2] = 4 self.assertEqual('|'.join('%x' % v for v in leds.buf), ('11|11|11|13|11|11|11|31|11|11|13|11|0', '1|2|4|0|0|0|0|0|0|0|0|0|0')[plati]) # variation pix[0] = 12 pix[1] = 34 pix[2] = 56 self.assertEqual(list(leds[0]), [12, 34, 56]) self.assertEqual('|'.join('%x' % v for v in leds.buf), ('11|31|11|31|11|11|33|11|11|33|31|11|0', '22|c|38|0|0|0|0|0|0|0|0|0|0')[plati]) # variation pix[0] = -1 pix[1] = 345 pix[2] = 777777777 self.assertEqual(list(leds[0]), [255, 89, 113]) self.assertEqual('|'.join('%x' % v for v in leds.buf), ('13|13|31|13|33|33|33|33|13|33|11|13|0', '59|ff|71|0|0|0|0|0|0|0|0|0|0')[plati])
def doTestMultiPixel(self, mem): # buf is the correct length # WS2812 can be iterated over to yield pixels # pixel values can be set and read back for n in range(1, 400, 19): leds = None gc.collect() leds = WS2812(spi_bus=1, led_count=n, mem=mem) self.assertEqual(len(leds), n) self.assertEqual(len(leds.buf), 12 * n + 1) random.seed(n) for pix in leds: self.assertEqual(list(pix), [0] * 3) for j in range(len(pix)): pix[j] = random.getrandbits(8) pb = [0] * 3 random.seed(n) for pix in leds: for j in range(len(pix)): pb[j] = random.getrandbits(8) self.assertEqual(list(pix), pb)
# -*- coding: utf-8 -*- from ws2812 import WS2812 ring = WS2812(spi_bus=1, led_count=16) data = [ (24, 0, 0), (0, 24, 0), (0, 0, 24), (12, 12, 0), (0, 12, 12), (12, 0, 12), (24, 0, 0), (21, 3, 0), (18, 6, 0), (15, 9, 0), (12, 12, 0), (9, 15, 0), (6, 18, 0), (3, 21, 0), (0, 24, 0), (8, 8, 8), ] ring.show(data)
from ws2812 import WS2812 from utime import sleep_ms chain = WS2812(ledNumber=16, brightness=100, dataPin='P22') # dataPin is for LoPy board only data = [(255, 102, 0), (127, 21, 0), (63, 10, 0), (31, 5, 0), (15, 2, 0), (7, 1, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0)] chain.show(data) while True: data = data[1:] + data[0:1] chain.show(data) sleep_ms(150)
def thread_echo(args): global DEBUG global clock global ws2812_chain # Set up our singleton for polling the sockets for data ready ws2812_chain = WS2812(ledNumber=ledNumber, brightness=100) p = poller() # NOTE: As of 2015-08-17, the Echo appears to have a hard-coded limit of # 16 switches it can control. Only the first 16 elements of the 'devices' # list will be used. devices = [ { "description": "white led", "port": 12340, "handler": rest_api_handler((255, 255, 255), 50) }, { "description": "red led", "port": 12341, "handler": rest_api_handler((255, 0, 0), 50) }, { "description": "blue led", "port": 12342, "handler": rest_api_handler((30, 144, 255), 90) }, # {"description": "green led", # "port": 12343, # "handler": rest_api_handler((0,255,0), 90)}, # {"description": "orange led", # "port": 12345, # "handler": rest_api_handler((255,165,0), 90)}, ] # Set up our singleton listener for UPnP broadcasts u = upnp_broadcast_responder() u.init_socket() # Add the UPnP broadcast listener to the poller so we can respond # when a broadcast is received. p.add(u) # Create our FauxMo virtual switch devices # Initialize FauxMo devices for device in devices: #if `port` doesnt exist, populate it #if it isnt an int, flip out with a descriptive exception if not device.get("port"): device["port"] = 0 elif type(device["port"]) is not int: raise InvalidPortException( "Invalid port of type: {}, with a value of: {}".format( type(device["port"]), device["port"])) fauxmo(device["description"], u, p, None, device["port"], action_handler=device["handler"]) # setting the clock using ntp if uname().machine == 'WiPy with ESP32': # Wipy 2.0 clock_tmp = RTC() clock_tmp.ntp_sync('time1.google.com') clock = time #gmtime function needed elif uname().machine == 'ESP32 module with ESP32': # Wemos ESP-WROOM-32 clock = RTC() #gmtime function needed clock.ntp_sync('time1.google.com') dbg("Entering main loop\n") while True: try: # Allow time for a ctrl-c to stop the process p.poll(10) time.sleep(0.1) gc.collect() except Exception as e: dbg(e) break
def doTestAttrs(self, mem): # A WSlice attributes have expected values ws = WS2812(spi_bus=1, led_count=7, mem=mem) ws.fill_buf(tg(len(ws), 0)) leds = WSlice(ws, 2, 4) self.assertEqual(leds.buf, ws.buf[12*2:12*4])