aboutsummaryrefslogtreecommitdiff
path: root/src/compileAndRun/pythonSupportFiles
diff options
context:
space:
mode:
authorJakob Stendahl <jakob.stendahl@outlook.com>2019-09-22 13:12:44 +0200
committerJakob Stendahl <jakob.stendahl@outlook.com>2019-09-22 13:12:44 +0200
commite6880cd8ccf82d993f222cb14b4860581654acb8 (patch)
tree45f318f4ece2f0d6ca73fc3f775b9ba277ccdb77 /src/compileAndRun/pythonSupportFiles
parente911f8029ca612d3b17ced300cdf5b6f13e20972 (diff)
parent2e60b25ae368b8c19ce5e982aa2672a6c56edf90 (diff)
downloadLuxcena-Neo-e6880cd8ccf82d993f222cb14b4860581654acb8.tar.gz
Luxcena-Neo-e6880cd8ccf82d993f222cb14b4860581654acb8.zip
Merge branch 'master' of https://github.com/JakobST1n/Luxcena-Neo
Diffstat (limited to 'src/compileAndRun/pythonSupportFiles')
-rw-r--r--src/compileAndRun/pythonSupportFiles/LuxcenaNeo/Matrix.py73
-rw-r--r--src/compileAndRun/pythonSupportFiles/LuxcenaNeo/Strip.py123
-rw-r--r--src/compileAndRun/pythonSupportFiles/entry.py6
3 files changed, 194 insertions, 8 deletions
diff --git a/src/compileAndRun/pythonSupportFiles/LuxcenaNeo/Matrix.py b/src/compileAndRun/pythonSupportFiles/LuxcenaNeo/Matrix.py
new file mode 100644
index 0000000..320da02
--- /dev/null
+++ b/src/compileAndRun/pythonSupportFiles/LuxcenaNeo/Matrix.py
@@ -0,0 +1,73 @@
+def getSegmentRange(segments, n):
+ """ Return a list of all the actual led-numbers in a segment """
+ # Sum all the segments prior to the one we are looking for
+ i = 0
+ start = 0
+ while True:
+ if i >= n: break
+ start += segments[i] # Add number of leds in this segment to the start-index
+ i += 1
+
+ # Add all numbers in the segment we are looking for to a list
+ i = start
+ breakPoint = i + segments[n]
+ range = []
+ while True:
+ range.append(i)
+ i += 1
+ if i >= breakPoint: break
+ return range
+
+
+class Matrix:
+
+ def __init__(self, segments, matrix):
+ self.matrix = [] # Holds the matrix
+ self.xLen = 0 # The width of the matrix
+ self.yLen = len(matrix) # The heigth of the matrix
+
+ for yPos in range(len(matrix)):
+ yVal = matrix[yPos]
+
+ thisY = []
+ for xPos in range(len(yVal)):
+ # This gets the range of segment n
+ segmentRange = getSegmentRange(segments, matrix[yPos][xPos][0])
+
+ # This adds the range to the current row's list
+ # if in the config [<segment_num>, <reversed>]
+ # reversed == true, revese the list before adding it
+ thisY += reversed(segmentRange) if matrix[yPos][xPos][1] else segmentRange
+
+ # This just finds the longest row in the matrix
+ if (len(thisY) > self.xLen):
+ self.xLen = len(thisY)
+
+ self.matrix.append(thisY)
+
+ def get(self, x, y):
+ """ Return the value of a place in the matrix given x and y coordinates """
+ return self.matrix[y][x]
+
+ def dump(self):
+ nSpacers = (self.xLen*6) // 2 - 6
+ print( ("=" * nSpacers) + "Matrix dump" + ("=" * nSpacers) )
+
+ for y in self.matrix:
+ thisYLine = ""
+ for x in y:
+ thisYLine += ( ' ' * (5 - len(str(x))) ) + str(x) + ' '
+ print(thisYLine)
+
+ print("=" * (self.xLen*6))
+
+
+if __name__ == "__main__":
+ testMatrix = Matrix(
+ [2, 2, 2, 2, 2, 2, 2, 2, 2],
+ [
+ [[0, False], [1, True], [2, False]],
+ [[3, True], [4, False], [5, True]],
+ [[6, False], [7, True], [8, False]]
+ ]
+ )
diff --git a/src/compileAndRun/pythonSupportFiles/LuxcenaNeo/Strip.py b/src/compileAndRun/pythonSupportFiles/LuxcenaNeo/Strip.py
index c4ac8c9..c3a913f 100644
--- a/src/compileAndRun/pythonSupportFiles/LuxcenaNeo/Strip.py
+++ b/src/compileAndRun/pythonSupportFiles/LuxcenaNeo/Strip.py
@@ -1,6 +1,121 @@
+from neopixel import *
+from Matrix import Matrix, getSegmentRange
+
class Strip:
- def __init__(self, segments, segmentConfiguration):
- self.segments = segments
- self.segmentConfiguration = segmentConfiguration
- return
+ def __init__(self, stripConf):
+ self.SEGMENTS = stripConf["segments"]
+ self.SEGMENT_CONFIG = stripConf["segment_config"]
+
+ self.LED_FREQ_HZ = stripConf["led_freq_hz"] # LED signal frequency in hertz (usually 800khz)
+ self.LED_CHANNEL = stripConf["led_channel"] # Set to '1' for GPIOs 13, 19, 41, 45, 53
+ self.LED_INVERT = stripConf["led_invert"] # True to invert the signal, (when using NPN transistor level shift)
+ self.LED_PIN = stripConf["led_pin"] # 18 uses PWM, 10 uses SPI /dev/spidev0.0
+ self.LED_DMA = stripConf["led_dma"] # DMA channel for generating the signal, on the newer ones, try 10
+ self.LED_COUNT = sum(self.SEGMENTS) # Number of LEDs in strip
+
+
+ self.LED_BRIGHTNESS = 255
+
+ self.strip = Adafruit_NeoPixel(
+ self.LED_COUNT,
+ self.LED_PIN,
+ self.LED_FREQ_HZ,
+ self.LED_DMA,
+ self.LED_INVERT,
+ self.LED_BRIGHTNESS,
+ self.LED_CHANNEL
+ )
+
+ self.strip.begin()
+
+ # Blank out all the LEDs
+ i = 0
+ while True:
+ self.strip.setPixelColor(i, 0)
+ i += 1
+ if (i > self.LED_COUNT): break
+ self.strip.show()
+
+ # Setup matrix
+ print(" * Generating matrix")
+ #try:
+ self.pixelMatrix = Matrix(self.SEGMENTS, stripConf["matrix"])
+ self.pixelMatrix.dump()
+ #except:
+ # print("Something went wrong while setting up your self-defined matrix.")
+
+ def show(self):
+ """Update the display with the data from the LED buffer."""
+ self.strip.show()
+
+ def setPixelColor(self, n, color):
+ """Set LED at position n to the provided 24-bit color value (in RGB order).
+ """
+ self.strip.setPixelColor(n, color)
+
+ def setPixelColorXY(self, x, y, color):
+ """Set LED at position n to the provided 24-bit color value (in RGB order).
+ """
+ self.strip.setPixelColor(self.pixelMatrix.get(x, y), color)
+
+ def setPixelColorRGB(self, n, red, green, blue, white = 0):
+ """Set LED at position n to the provided red, green, and blue color.
+ Each color component should be a value from 0 to 255 (where 0 is the
+ lowest intensity and 255 is the highest intensity).
+ """
+ self.strip.setPixelColor(n, Color(red, green, blue, white))
+
+ def setPixelColorXYRGB(self, x, y, red, green, blue, white = 0):
+ """Set LED at position n to the provided red, green, and blue color.
+ Each color component should be a value from 0 to 255 (where 0 is the
+ lowest intensity and 255 is the highest intensity).
+ """
+ self.strip.setPixelColor(self.pixelMatrix.get(x, y), Color(red, green, blue, white))
+
+ def setSegmentColorRGB(self, segment, red, green, blue, white = 0):
+ """Set a whole segment to the provided red, green and blue color.
+ Each color component should be a value from 0 to 255 (where 0 is the
+ lowest intensity and 255 is the highest intensity)."""
+ for n in getSegmentRange(self.SEGMENTS, segment):
+ self.strip.setPixelColor(n, Color(red, green, blue, white))
+
+ def setBrightness(self, brightness):
+ """Scale each LED in the buffer by the provided brightness. A brightness
+ of 0 is the darkest and 255 is the brightest.
+ """
+ self.strip.setBrightness(brightness)
+
+ def getBrightness(self):
+ """Get the brightness value for each LED in the buffer. A brightness
+ of 0 is the darkest and 255 is the brightest.
+ """
+ return self.strip.getBrightness()
+
+ def getPixels(self):
+ """Return an object which allows access to the LED display data as if
+ it were a sequence of 24-bit RGB values.
+ """
+ return self.strip.getPixels()
+
+ def numPixels(self):
+ """Return the number of pixels in the display."""
+ return self.LED_COUNT
+
+ def getPixelColor(self, n):
+ """Get the 24-bit RGB color value for the LED at position n."""
+ return self.strip.getPixelColor(n)
+
+
+def Color(red, green, blue, white = 0):
+ """Convert the provided red, green, blue color to a 24-bit color value.
+ Each color component should be a value 0-255 where 0 is the lowest intensity
+ and 255 is the highest intensity.
+ """
+ return (white << 24) | (red << 16)| (green << 8) | blue
+
+def hexColor(value):
+ value = value.lstrip('#')
+ lv = len(value)
+ rgb = tuple(int(value[i:i+lv/3], 16) for i in range(0, lv, lv/3))
+ return (0 << 24) | (rgb[1] << 16) | (rgb[0] << 8) | rgb[2]
diff --git a/src/compileAndRun/pythonSupportFiles/entry.py b/src/compileAndRun/pythonSupportFiles/entry.py
index 4ba1031..45de822 100644
--- a/src/compileAndRun/pythonSupportFiles/entry.py
+++ b/src/compileAndRun/pythonSupportFiles/entry.py
@@ -49,16 +49,14 @@ def main():
print ("> Loading pixel-configuration...")
with open(config_dir + "strip.json", "r") as rawStripConf:
stripConf = json.load(rawStripConf)
- segments = stripConf["segments"]
- segmentConfiguration = stripConf["segmentConfiguration"]
print ("> Initializing script...")
moduleSc = importlib.import_module("script")
if ("LuxcenaNeo" in dir(moduleSc)):
- moduleSc.LuxcenaNeo.strip = moduleSc.LuxcenaNeo.Strip(segments, segmentConfiguration)
+ moduleSc.LuxcenaNeo.strip = moduleSc.LuxcenaNeo.Strip(stripConf)
elif ("neo" in dir(moduleSc)):
- moduleSc.neo.strip = moduleSc.neo.Strip(segments, segmentConfiguration)
+ moduleSc.neo.strip = moduleSc.neo.Strip(stripConf)
else:
raise Exception("Neither LuxcenaNeo nor neo found in script, check docs!")