This repository was archived by the owner on May 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLedStrip.py
More file actions
61 lines (52 loc) · 2.09 KB
/
LedStrip.py
File metadata and controls
61 lines (52 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# Simple Python Library for accessing WS2801 LED stripes
# Copyright (C) 2013 Philipp Tiefenbacher <wizards23@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# For more information about this project please visit:
# http://www.hackerspaceshop.com/ledstrips/raspberrypi-ws2801.html
class LedStrip(object):
def __init__(self, nLeds, nBuffers=1):
self.nLeds = nLeds
self.nBuffers = nBuffers
self.buffers = []
for i in range(0, nBuffers):
ba = bytearray()
for l in range(0, nLeds):
ba.extend([0,0,0])
self.buffers.append(ba)
def close(self):
""
def update(self, bufferNr=0):
""
def setAll(self, color, bufferNr=0):
for i in range(0, self.nLeds):
self.setPixel(i, color, bufferNr)
def setPixel(self, index, color, bufferNr=0):
self.buffers[bufferNr][index*3:index*3+3] = (color[0], color[2], color[1])
def getPixel(self, index, bufferNr=0):
return [self.buffers[bufferNr][index*3],
self.buffers[bufferNr][index*3+2],
self.buffers[bufferNr][index*3+1]]
class LedStrip_Dummy(LedStrip):
def __init__(self, nLeds, nBuffers=1):
super(LedStrip_Dummy, self).__init__(nLeds, nBuffers)
class LedStrip_WS2801(LedStrip):
def __init__(self, spiDevice, nLeds, nBuffers=1):
self.f = open(spiDevice, "w")
super(LedStrip_WS2801, self).__init__(nLeds, nBuffers)
def close(self):
if (self.f != None):
self.f.close()
self.f = None
def update(self, bufferNr=0):
self.f.write(self.buffers[bufferNr])
self.f.flush()