karrika wrote:Looks like an UFO. Nice.
I have a close encounters setup working now ;D
https://www.youtube.com/watch?v=2JNOUuct06w
Here is the working Beta Code for this project.
It's not optimized. It's not written by a professional programmer.
It works and that is all that I need.
6 different defined functions.
instant switching between each.
constant data rate set at 1,700fps for best viewing at ~60mph.
grxxl.py
Code: Select all
import time
import pigpio
from dotstar import Adafruit_DotStar
import Image
import random
import readrx
numpixels = 119 #number of pixels in the strip
strip = Adafruit_DotStar(numpixels, 24000000, order='bgr')
filename1 = "pattern03.jpg" #file name of image to be converted
filename2 = "triangles.jpg"
filename3 = "pattern04.jpg"
filename4 = "closeencounters.jpg"
rOffset = 3 #fix RGB color order
gOffset = 2
bOffset = 1
strip.begin()
strip.setBrightness(127)
class pov1:# converting an image to a byte array for sending to led strip
print "Loading {}...".format(filename1)
img = Image.open(filename1).convert("RGB")
pixels = img.load()
width = img.size[0]
height = img.size[1]
# print "%dx%d pixels" % img.size
gamma = bytearray(256)
for i in range(256):
gamma[i] = int(pow(float(i) / 255.0, 2.7) * 255.0 + 0.5)
# print "Allocating..."
column = [0 for x in range(width)]
for x in range(width):
column[x] = bytearray(height * 4)
# print "Converting..."
for x in range(width): # For each column of image...
for y in range(height): # For each pixel in column...
value = pixels[x, y] # Read pixel in image
y4 = y * 4 # Position in raw buffer
column[x][y4] = 0xFF # Pixel start marker
column[x][y4 + rOffset] = gamma[value[0]] # Gamma-corrected R
column[x][y4 + gOffset] = gamma[value[1]] # Gamma-corrected G
column[x][y4 + bOffset] = gamma[value[2]] # Gamma-corrected B
print "Done Loading {}...".format(filename1)
class pov2:# converting an image to a byte array for sending to led strip
print "Loading {}...".format(filename2)
img = Image.open(filename2).convert("RGB")
pixels = img.load()
width = img.size[0]
height = img.size[1]
# print "%dx%d pixels" % img.size
gamma = bytearray(256)
for i in range(256):
gamma[i] = int(pow(float(i) / 255.0, 2.7) * 255.0 + 0.5)
# print "Allocating..."
column = [0 for x in range(width)]
for x in range(width):
column[x] = bytearray(height * 4)
# print "Converting..."
for x in range(width): # For each column of image...
for y in range(height): # For each pixel in column...
value = pixels[x, y] # Read pixel in image
y4 = y * 4 # Position in raw buffer
column[x][y4] = 0xFF # Pixel start marker
column[x][y4 + rOffset] = gamma[value[0]] # Gamma-corrected R
column[x][y4 + gOffset] = gamma[value[1]] # Gamma-corrected G
column[x][y4 + bOffset] = gamma[value[2]] # Gamma-corrected B
print "Done Loading {}...".format(filename2)
class pov3:# converting an image to a byte array for sending to led strip
print "Loading {}...".format(filename3)
img = Image.open(filename3).convert("RGB")
pixels = img.load()
width = img.size[0]
height = img.size[1]
# print "%dx%d pixels" % img.size
gamma = bytearray(256)
for i in range(256):
gamma[i] = int(pow(float(i) / 255.0, 2.7) * 255.0 + 0.5)
# print "Allocating..."
column = [0 for x in range(width)]
for x in range(width):
column[x] = bytearray(height * 4)
# print "Converting..."
for x in range(width): # For each column of image...
for y in range(height): # For each pixel in column...
value = pixels[x, y] # Read pixel in image
y4 = y * 4 # Position in raw buffer
column[x][y4] = 0xFF # Pixel start marker
column[x][y4 + rOffset] = gamma[value[0]] # Gamma-corrected R
column[x][y4 + gOffset] = gamma[value[1]] # Gamma-corrected G
column[x][y4 + bOffset] = gamma[value[2]] # Gamma-corrected B
print "Done Loading {}...".format(filename3)
class pov4:# converting an image to a byte array for sending to led strip
print "Loading {}...".format(filename4)
img = Image.open(filename4).convert("RGB")
pixels = img.load()
width = img.size[0]
height = img.size[1]
# print "%dx%d pixels" % img.size
gamma = bytearray(256)
for i in range(256):
gamma[i] = int(pow(float(i) / 255.0, 2.7) * 255.0 + 0.5)
# print "Allocating..."
column = [0 for x in range(width)]
for x in range(width):
column[x] = bytearray(height * 4)
# print "Converting..."
for x in range(width): # For each column of image...
for y in range(height): # For each pixel in column...
value = pixels[x, y] # Read pixel in image
y4 = y * 4 # Position in raw buffer
column[x][y4] = 0xFF # Pixel start marker
column[x][y4 + rOffset] = gamma[value[0]] # Gamma-corrected R
column[x][y4 + gOffset] = gamma[value[1]] # Gamma-corrected G
column[x][y4 + bOffset] = gamma[value[2]] # Gamma-corrected B
print "Done Loading {}...".format(filename4)
def hsv2rgb(HSV):#convert HSV 255 values to RGB 255
H, S, V = HSV
if S == 0:
R = V
G = V
B = V
return (R, G, B)
region = H // 43;
remainder = (H - (region * 43)) * 6;
P = (V * (255 - S)) >> 8;
Q = (V * (255 - ((S * remainder) >> 8))) >> 8;
T = (V * (255 - ((S * (255 - remainder)) >> 8))) >> 8;
if region == 0:
R = V
G = T
B = P
elif region == 1:
R = Q;
G = V;
B = P;
elif region == 2:
R = P;
G = V;
B = T;
elif region == 3:
R = P;
G = Q;
B = V;
elif region == 4:
R = T;
G = P;
B = V;
else:
R = V;
G = P;
B = Q;
return R, G, B
"""
six toggle states.
toggle readrx.pw1()(aux1) has three positions
toggle readrx.pw2()(gear) has two positions
"""
def toggle31():
if readrx.pw1() == 1900 and readrx.pw2() == 1100:
print("aux 3 pw ={}, gear 1 pw ={}".format(int(readrx.pw1()), int(readrx.pw2())))
pi.set_PWM_dutycycle(23, 0)
running = True
while running:
for x in range(pov4.width):
strip.show(pov4.column[x])
time.sleep(.02)
if readrx.pw1() != 1900:
running = False
break
if readrx.pw2() != 1100:
running = False
break
break
"""
cylon
"""
"""
running = True
while running:
i = 0
while (i < numpixels):
if readrx.pw1() != 1900:
running = False
break
if readrx.pw2() != 1100:
running = False
break
strip.setPixelColor(i, 0x0000FF)
strip.show()
strip.setPixelColor(i, 0x000000)
time.sleep(0.001)
i += 1
i = numpixels - 1
while (i < numpixels):
if readrx.pw1() != 1900:
running = False
break
if readrx.pw2() != 1100:
running = False
break
strip.setPixelColor(i, 0xFF0000)
strip.show()
strip.setPixelColor(i, 0x000000)
time.sleep(0.001)
i -= 1
if i <= 0:
break
"""
def toggle21():
if readrx.pw1() == 1500 and readrx.pw2() == 1100:
print("aux 2 pw ={}, gear 1 pw ={}".format(int(readrx.pw1()), int(readrx.pw2())))
pi.set_PWM_dutycycle(23, 0)
"""
confetti
"""
running = True
while running:
if readrx.pw1() != 1500:
running = False
break
if readrx.pw2() != 1100:
running = False
break
randompixel = random.randint(0, 119)
randomcolor = random.randint(0, 255), 255, 255
randomhsv = hsv2rgb(randomcolor)
strip.setPixelColor((randompixel), randomhsv[0], randomhsv[1], randomhsv[2])
strip.show()
time.sleep(.01)
strip.setPixelColor((randompixel), 0, 0, 0)
strip.show()
def toggle11():
if readrx.pw1() == 1100 and readrx.pw2() == 1100:
print("aux 1 pw ={}, gear 1 pw ={}".format(int(readrx.pw1()), int(readrx.pw2())))
strip.show()
running = True
while running:
i = 0
while (i < 200):
if readrx.pw1() != 1100:
running = False
break
if readrx.pw2() != 1100:
running = False
break
pi.set_PWM_dutycycle(23, i)
time.sleep(0.001)
i += 1
while (i == 200):
if readrx.pw1() != 1100:
running = False
break
if readrx.pw2() != 1100:
running = False
break
time.sleep(.1)
i -= 1
while (i < 200):
if readrx.pw1() != 1100:
running = False
break
if readrx.pw2() != 1100:
running = False
break
pi.set_PWM_dutycycle(23, i)
time.sleep(0.001)
i -= 1
if i <= 0:
break
def toggle32():
if readrx.pw1() == 1900 and readrx.pw2() == 1900:
print("aux 3 pw ={}, gear 2 pw ={}".format(int(readrx.pw1()), int(readrx.pw2())))
pi.set_PWM_dutycycle(23, 255)
# print "image-pov 3"
while True:
for x in range(pov3.width):
strip.show(pov3.column[x])
time.sleep(.0005)
if readrx.pw1() != 1900:
running = False
break
if readrx.pw2() != 1900:
running = False
break
break
def toggle22():
if readrx.pw1() == 1500 and readrx.pw2() == 1900:
print("aux 2 pw ={}, gear 2 pw ={}".format(int(readrx.pw1()), int(readrx.pw2())))
pi.set_PWM_dutycycle(23, 255)
# print "image-pov 2"
while True:
for x in range(pov2.width):
strip.show(pov2.column[x])
time.sleep(.0005)
if readrx.pw1() != 1500:
running = False
break
if readrx.pw2() != 1900:
running = False
break
break
def toggle12():
running = True
if readrx.pw1() == 1100 and readrx.pw2() == 1900:
print("aux 1 pw ={}, gear 2 pw ={}".format(int(readrx.pw1()), int(readrx.pw2())))
pi.set_PWM_dutycycle(23, 255)
# print "image-pov 1"
running = True
while running:
for x in range(pov1.width):
strip.show(pov1.column[x])
time.sleep(.0005)
if readrx.pw1() != 1100:
running = False
break
if readrx.pw2() != 1900:
running = False
break
break
if __name__ == "__main__":
SAMPLE_TIME = 0.01
pi = pigpio.pi()
pov1()#convert the image file
pov2()
pov3()
print "Starting Main Loop"
while True:
# print dir(readrx)
# print readrx.pw1()
time.sleep(SAMPLE_TIME)
toggle31()
toggle21()
toggle11()
toggle32()
toggle22()
toggle12()
readrx.py
Code: Select all
import time
import pigpio
PWM_GPIO_1 = 12 #PWM input gpio pins
PWM_GPIO_2 = 16
class reader:# Reads the Pulse Width signal of a gpio input and returns it's value
def __init__(self, pi, gpio, weighting=0.0):
self.pi = pi
self.gpio = gpio
if weighting < 0.0:
weighting = 0.0
elif weighting > 0.99:
weighting = 0.99
self._new = 1.0 - weighting # Weighting for new reading.
self._old = weighting # Weighting for old reading.
self._high_tick = None
self._period = None
self._high = None
pi.set_mode(gpio, pigpio.INPUT)
self._cb = pi.callback(gpio, pigpio.EITHER_EDGE, self._cbf)
def _cbf(self, gpio, level, tick):
if level == 1:
if self._high_tick is not None:
t = pigpio.tickDiff(self._high_tick, tick)
if self._period is not None:
self._period = (self._old * self._period) + (self._new * t)
else:
self._period = t
self._high_tick = tick
elif level == 0:
if self._high_tick is not None:
t = pigpio.tickDiff(self._high_tick, tick)
if self._high is not None:
self._high = (self._old * self._high) + (self._new * t)
else:
self._high = t
def pulse_width(self): #Returns the PWM pulse width in microseconds.
if self._high is not None:
return ((round(self._high/100))*100) #round pwm to nerest hundred
else:
return 2.0
def cancel(self):# Cancels the reader and releases resources.
self._cb.cancel()
pi = pigpio.pi()
p1 = reader(pi, PWM_GPIO_1)
p2 = reader(pi, PWM_GPIO_2)
def pw1():
pw1 = p1.pulse_width()
return pw1
def pw2():
pw2 = p2.pulse_width()
return pw2