-
1Step 1
Here's the Arduino Code I am using on the Teensy. It's uses the Octows2811 library (https://www.pjrc.com/teensy/td_libs_OctoWS2811.html)
/* LIGHTBAR By IAMTHESOUNDMAN - Based on: OctoWS2811 VideoDisplay.ino - Video on LEDs, from a PC, Mac, Raspberry Pi http://www.pjrc.com/teensy/td_libs_OctoWS2811.html Copyright (c) 2013 Paul Stoffregen, PJRC.COM, LLC Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Required Connections -------------------- pin 2: LED Strip #1 pin 15 & 16 - Connect together, but do not use pin 4: Do not use pin 3: Do not use as PWM. Normal use is okay */ #include <OctoWS2811.h> const int ledsPerStrip = 192; DMAMEM int displayMemory[ledsPerStrip*6]; //int drawingMemory[ledsPerStrip*6]; byte databuffer[ledsPerStrip*3]; const int config = WS2811_GRB | WS2811_800kHz; OctoWS2811 leds(ledsPerStrip, displayMemory, NULL, config); void setup() { Serial.setTimeout(100); leds.begin(); leds.show(); } void loop() { // // wait for a Start-Of-Message character: // // '*' = begin column int startChar = Serial.read(); if (startChar == '*') { int count = Serial.readBytes((char *)databuffer, sizeof(databuffer)); //if (count == sizeof(databuffer)) { // WS2811 update begins immediately after falling edge of frame sync for (int x = 0; x < 192; x++){ int x3 = x *3; leds.setPixel(x, (databuffer[x3] << 16) | (databuffer[x3 + 1] << 8) | (databuffer[x3 +2])); } leds.show(); //} } else if (startChar >= 0) { // discard unknown characters } }
-
2Step 2
Python Code.
This reads a text file modified by the web server and then updates the image buffer. Handles button presses and sending the data to the Teensy/light strip.
My python knowledge isn't the best so I'm sure there was a better way to do a lot of things that I did. There's a bit of debug related code in here as well but this functions as is.
#!/usr/bin/python #Light Painting control script #interfaces with web server to send image data to the WS2811 strip #based on code from Ladyada - https://gist.github.com/ladyada/3309494#file-adafruitlightpaint4pi-py import RPi.GPIO as GPIO, Image, time, serial, binascii, json GPIO.setmode(GPIO.BCM) # Configurable values #filename = "/var/www/images/current/current.png" #dev = "/dev/spidev0.0" ser = serial.Serial('/dev/ttyACM0', 115200) gammanum = float(1.7) f = open('/var/www/update.txt', 'r') #open text file modified from web server. This contains a random number that is updated when a change has been made as well as data for whether the loop mode is set. firstnum = f.read() f.close() pixels = int() width = int() height = int() column = bytearray() blank = bytearray() prev_input = 0 GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Calculate gamma correction table. This includes gamma = bytearray(256) for i in range(256): gamma[i] = int(pow(float(i) / 255.0, gammanum) * 255.0 + 0.5) blank = [0 for x in range(1)] for x in range(1): blank[x] = bytearray(192 * 3) for x in range(1): for y in range(192): value = 0 y3 = y * 3 blank[x][y3] = gamma[value] blank[x][y3 + 1] = gamma[value] blank[x][y3 + 2] = gamma[value] # Load the image def imageload(filename): #handle loading the image file into memory and converting it into the format needed to sending to the Teensy. global pixels, width, height, column, loop print "Loading..." img = Image.open(filename).convert("RGB") pixels = img.load() width = img.size[0] height = img.size[1] print "%dx%d pixels" % img.size print "width: " + str(float(width)*float(0.03125)) + "ft" # Create list of bytearrays, one for each column of image. # R, G, B byte per pixel. print "Allocating..." column = [0 for x in range(width)] for x in range(width): column[x] = bytearray(height * 3) # Convert 8-bit RGB image into column-wise RGB bytearray list. print "Converting..." for x in range(width): for y in range(height): value = pixels[x, y] y3 = y * 3 column[x][y3] = gamma[value[0]] column[x][y3 + 1] = gamma[value[1]] column[x][y3 + 2] = gamma[value[2]] print "Ready" def pushpixels(freq, delay, isblank): #pushes the image data to the Teensy over serial. global pixels, width, height, column, blank,loop print "Displaying..." if isblank == 0: for x in range(width): ser.write('*') #command to tell the Teensy it is about to send data. ser.flush() ser.write(column[x]) ser.flush() time.sleep(freq) if (GPIO.input(17) and loop == "1"): #if loop mode is enabled stop writing image data when the button is let go. print("Loop Mode Ended") return if (loop == "1"): print "looping" pushpixels(0.001, 0, 0); #keep looping while the button is pressed. else: return else: #if isblank is 1 send blank data to clear out the image buffer on the Teensy. for x in range(1): ser.write('*') ser.flush() ser.write(blank[x]) ser.flush() print "wait..." time.sleep(delay) #pushpixels(0.010, 1); # Then it's a trivial matter of writing each column to the SPI port. def godisplay(channel): #button handling function if GPIO.input(17): # if port 25 == 1 print "Rising edge detected on 25" else: # if port 25 != 1 print("Button pressed") print GPIO.input(17) pushpixels(0.001, 0,0); pushpixels(0.001, 0, 1); #imageload("/var/www/images/current/current.png"); pushpixels(0.001, 0, 1); #clear the display GPIO.add_event_detect(17, GPIO.BOTH, callback=godisplay, bouncetime=20) while True: #GPIO.add_event_detect(17, GPIO.RISING, callback=pushpixels(0.010, 1,0)); #take a reading #input = GPIO.input(17) #if the last reading was low and this one high, print #if ((not prev_input) and input): #if (GPIO.input(17, GPIO.FALLING)): #GPIO.wait_for_edge(17, GPIO.FALLING) # print("Button pressed") # pushpixels(0.010, 0,0) #update previous input #prev_input = input #slight pause to debounce s = open('/var/www/update.txt', 'r') snum = s.readline() global loop loop = s.readline() s.close() if firstnum != snum: print "updated image" if (loop == "1"): print "loop mode!" imageload("/var/www/images/current/current.png"); #the web server copies the currently selected image to this file. firstnum = snum #time.sleep(0.05) #spidev.write(column[x]) #spidev.flush() #print (binascii.hexlify(column[x])) #for y in range(height): # print "row" + str(y) #y3 = y * 3 # print hex(column[x][y3]), # print hex(column[x][y3 + 1]), # print hex(column[x][y3 + 2]), # #ser.close() #raise SystemExit
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.