add a neopixel driver

doublebuf
Ondřej Hruška 7 years ago
parent 5d2810cacf
commit 5cf2022bbc
Signed by: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 4
      gex/PayloadBuilder.py
  2. 5
      gex/PayloadParser.py
  3. 2
      gex/__init__.py
  4. 33
      gex/units/Neopixel.py
  5. 17
      gex/units/Pin.py
  6. 19
      main.py

@ -21,6 +21,10 @@ class PayloadBuilder:
""" Add a uint16_t """
self.buf.extend((num&0xFFFF).to_bytes(length=2, byteorder=self.endian, signed=False))
def u24(self, num:int):
""" Add a uint24_t (for use with RGB colors) """
self.buf.extend((num&0xFFFFFF).to_bytes(length=3, byteorder=self.endian, signed=False))
def u32(self, num:int):
""" Add a uint32_t """
self.buf.extend((num&0xFFFFFFFF).to_bytes(length=4, byteorder=self.endian, signed=False))

@ -45,6 +45,11 @@ class PayloadParser:
slice = self._slice(2)
return int.from_bytes(slice, byteorder=self.endian, signed=False)
def u24(self) -> int:
""" Read a uint24_t """
slice = self._slice(3)
return int.from_bytes(slice, byteorder=self.endian, signed=False)
def u32(self) -> int:
""" Read a uint32_t """
slice = self._slice(4)

@ -7,9 +7,9 @@ from gex.Unit import Unit
from gex.Client import Client
# import all the units
from gex.units.Pin import Pin
from gex.units.DOut import DOut
from gex.units.DIn import DIn
from gex.units.Neopixel import Neopixel
# General, low level

@ -0,0 +1,33 @@
import gex
class Neopixel(gex.Unit):
"""
Raw access to a neopixel strip.
"""
def _type(self):
return 'NEOPIXEL'
def get_len(self):
""" Get the neopixel strip length """
resp = self.query(0x04)
pp = gex.PayloadParser(resp)
return pp.u16()
def load(self, colors, reverse=True):
"""
Load colors to the strip.
The numbers are normally 0xRRGGBB
If 'reverse' is false, they're treated as little-endian: 0xBBGGRR.
"""
pb = gex.PayloadBuilder(endian='big' if reverse else 'little')
for c in colors:
pb.u24(c)
self.send(0x01, pb.close())
def clear(self):
"""
Reset the strip (set all to black)
"""
self.send(0x00)

@ -1,17 +0,0 @@
import gex
class Pin(gex.Unit):
def _type(self):
return 'PIN'
def on_event(self, event:int, payload):
pass
def off(self):
self.send(0x00)
def on(self):
self.send(0x01)
def toggle(self):
self.send(0x02)

@ -14,13 +14,6 @@ if False:
client.bulk_write(gex.MSG_INI_WRITE, buf)
if False:
led = gex.Pin(client, 'LED')
for i in range(0,10):
led.toggle()
time.sleep(.1)
if False:
leds = gex.DOut(client, 'strip')
@ -48,7 +41,7 @@ if False:
leds.toggle(0xFF)
time.sleep(.1)
if True:
if False:
btn = gex.DIn(client, 'btn')
strip = gex.DOut(client, 'strip')
@ -56,3 +49,13 @@ if True:
b = btn.read()
strip.write((b << 2) | ((~b) & 1))
time.sleep(.02)
if True:
neo = gex.Neopixel(client, 'npx')
print('We have %d neopixels.\n' % neo.get_len())
for i in range(0,512):
j = i if i < 256 else 255-(i-256)
neo.load([0x660000+j, 0x3300FF-j, 0xFFFF00-(j<<8), 0x0000FF+(j<<8)-j])
time.sleep(.005)

Loading…
Cancel
Save