Using a Raspberry Pi with WiFi and a webserver you access a web page with your phone/tablet and upload/change the image or pattern to be displayed. There is also a page to create a custom text message with font and color selection. A python script runs in the background and handles sending the image data to a Teensy 3.0 connected to a 2M, 192 LED WS2811 strip. More details to come.
Finished the Lightbar enough to take it out for a test run the other night. I met up with some friends and I set up the first image and stood on a ledge and walked while my friend Jason took a long exposure with his camera. What did we see? Well, an 8bit rendering of Michael Jackson! Success! We proceeded to head to another part of town and had some fun putting this thing through it's paces.
/* 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>constint ledsPerStrip = 192;
DMAMEM int displayMemory[ledsPerStrip*6];
//int drawingMemory[ledsPerStrip*6];byte databuffer[ledsPerStrip*3];
constint config = WS2811_GRB | WS2811_800kHz;
OctoWS2811 leds(ledsPerStrip, displayMemory, NULL, config);
voidsetup(){
Serial.setTimeout(100);
leds.begin();
leds.show();
}
voidloop(){
//// wait for a Start-Of-Message character://// '*' = begin columnint 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 syncfor (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();
//}
} elseif(startChar >= 0){
// discard unknown characters
}
}
2
Step 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-pyimport 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 = [0for 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 defimageload(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 = [0for 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"defpushpixels(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")
returnif (loop == "1"):
print"looping"
pushpixels(0.001, 0, 0); #keep looping while the button is pressed.else:
returnelse: #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.defgodisplay(channel):#button handling functionif 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)
whileTrue:
#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
Jake,
Am I correct to assume you're using serial comm from pi to teensy? Did you write your own code for that? I've done similar and keep wondering if there's a better way.
Yes I'm communicating to the Teensy over usb serial communication. I modified example code for the Octows2811 library (https://www.pjrc.com/teensy/td_libs_OctoWS2811.html) on the teensy and pieced together the code for the python script. I'm going to post my Arduino code and Python script within the next couple hours. Let me know if you have any more questions!
Is it possible to get more information about your project?
I would like to know how you connected the pi with the teensy and the teensy to the ledstrip.
HI Maximus. I've been meaning to sit down and put more info up on here but haven't had the time. The teensy is connected to the Pi through USB. I'm using the Octows2811 library (https://www.pjrc.com/teensy/td_libs_OctoWS2811.html) on the Teensy to talk to the LED strip. A Python script runs on the Pi and when activated converts pixel data from a png image into the correct format and sends it to the Teensy which then blinks the lights. Hope this helps. If you have any more questions don't hesitate to ask!
Thank you for your fast reaction!
Has been a busy week and haven't had time to react earlier.
I am starting to understand your project and I'm gonna give it a go and try to make it myself.
I'm gonna start with an 'easier' version though and try to load the PNG files from an USB-stick instead of using a webserver.
Is there anything I should pay extra attention to or do you have any tips?
Am I correct to assume you're using serial comm from pi to teensy? Did you write your own code for that? I've done similar and keep wondering if there's a better way.
Thanks,
razputin