add other effects

This commit is contained in:
Siwat Sirichai 2022-03-30 20:15:41 +07:00
parent f285eca6d6
commit ea90d87019
9 changed files with 248 additions and 39 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
__pycache__/

View File

@ -1,7 +1,6 @@
from tkinter import TOP from tkinter import TOP
import pyautogui import pyautogui
import scipy.cluster as cluster import scipy.cluster as cluster
import scipy
import sys import sys
import numpy as np import numpy as np
from time import sleep, time from time import sleep, time
@ -12,27 +11,36 @@ from time import perf_counter as timestamp
from telemetrix_rpi_pico import telemetrix_rpi_pico from telemetrix_rpi_pico import telemetrix_rpi_pico
from multiprocessing import Pool from multiprocessing import Pool
import PIL.ImageGrab import PIL.ImageGrab
from PIL import Image
import colorsys
import win32gui, win32ui, win32con
class size: class size:
width = None width = None
height = None height = None
def get_screenshot():
return PIL.ImageGrab.grab()
def sigint_handler(signal=None, frame=None): def sigint_handler(signal=None, frame=None):
print ('KeyboardInterrupt is caught') print ('KeyboardInterrupt is caught')
board.neopixel_clear() board.neopixel_clear()
sleep(0.75) sleep(0.75)
board.shutdown() board.shutdown()
sys.exit(0) exit()
FAST_MODE = False FAST_MODE = True
SATURATION_BOOST_FACTOR = 2.5
def find_dorminant_color(im): def find_dorminant_color(im):
if FAST_MODE: if FAST_MODE:
color = numpy.reshape(numpy.asarray(im),(-1,3)) color = numpy.reshape(numpy.asarray(im),(-1,3))
color = numpy.median(color,axis=0) color = numpy.median(color,axis=0)
return color h,s,v = colorsys.rgb_to_hsv(r=color[0]/255,g=color[1]/255,b=color[2]/255)
s = min(1,s*SATURATION_BOOST_FACTOR)
r,g,b = colorsys.hsv_to_rgb(h=h,s=s,v=v)
return [r*255,g*255,b*255]
else: else:
NUM_CLUSTERS = 1 NUM_CLUSTERS = 2
im = im.resize((100, 100)) im = im.resize((100, 100))
ar = np.asarray(im) ar = np.asarray(im)
@ -45,9 +53,15 @@ def find_dorminant_color(im):
index_max = np.argmax(counts) index_max = np.argmax(counts)
peak = codes[index_max] peak = codes[index_max]
return peak h,s,v = colorsys.rgb_to_hsv(r=peak[0]/255,g=peak[1]/255,b=peak[2]/255)
s = min(1,s*SATURATION_BOOST_FACTOR)
r,g,b = colorsys.hsv_to_rgb(h=h,s=s,v=v)
return [r*255,g*255,b*255]
if __name__ == '__main__': if __name__ == '__main__':
size = size()
size.width=pyautogui.size().width
size.height=pyautogui.size().height
signal.signal(signal.SIGINT, sigint_handler) signal.signal(signal.SIGINT, sigint_handler)
atexit.register(sigint_handler) atexit.register(sigint_handler)
global NUM_LEDS,BOARDER_SIZE,TOP_LEDS,RIGHT_LEDS,BUTTOM_LEDS,LEFT_LEDS,screenshot,board global NUM_LEDS,BOARDER_SIZE,TOP_LEDS,RIGHT_LEDS,BUTTOM_LEDS,LEFT_LEDS,screenshot,board
@ -56,28 +70,25 @@ if __name__ == '__main__':
BOARDER_SIZE = 300 BOARDER_SIZE = 50
TOP_LEDS = 19 TOP_LEDS = 19
RIGHT_LEDS = 11 RIGHT_LEDS = 11
BUTTOM_LEDS = 17 BUTTOM_LEDS = 19
LEFT_LEDS = 10 LEFT_LEDS = 11
MIN_TIME = 0.05
NUM_LEDS = TOP_LEDS+RIGHT_LEDS+BUTTOM_LEDS+LEFT_LEDS NUM_LEDS = TOP_LEDS+RIGHT_LEDS+BUTTOM_LEDS+LEFT_LEDS
board.set_pin_mode_neopixel(pin_number=2,num_pixels=NUM_LEDS) board.set_pin_mode_neopixel(pin_number=2,num_pixels=NUM_LEDS)
board.neopixel_clear(auto_show=True) board.neopixel_clear(auto_show=True)
size = size()
size.width=pyautogui.size().width
size.height=pyautogui.size().height
board.neopixel_show() board.neopixel_show()
screenshot = pyautogui.screenshot() screenshot = pyautogui.screenshot()
pool = Pool(16) pool = Pool(8)
while True: while True:
try: try:
screenshot = PIL.ImageGrab.grab() screenshot = get_screenshot()
lastTime = timestamp() lastTime = timestamp()
chunk = [] chunk = []
top = screenshot.crop(box=[0,0,size.width,BOARDER_SIZE]) top = screenshot.crop(box=[0,0,size.width,BOARDER_SIZE])
@ -99,13 +110,13 @@ if __name__ == '__main__':
for i in reversed(range(0,LEFT_LEDS)): for i in reversed(range(0,LEFT_LEDS)):
segment = left.crop(box=[0,i*size.height/LEFT_LEDS,BOARDER_SIZE,(i+1)*size.height/LEFT_LEDS]) segment = left.crop(box=[0,i*size.height/LEFT_LEDS,BOARDER_SIZE,(i+1)*size.height/LEFT_LEDS])
chunk.append(segment) chunk.append(segment)
colors = pool.map(find_dorminant_color,chunk) colors = pool.map(find_dorminant_color,chunk)
for i in range(0,len(colors)): for i in range(0,len(colors)):
board.neo_pixel_set_value(i,r=int(colors[i][0]),g=int(colors[i][1]),b=int(colors[i][2])) board.neo_pixel_set_value(i,r=int(colors[i][0]),g=int(colors[i][1]),b=int(colors[i][2]))
board.neopixel_show() board.neopixel_show()
while timestamp()-lastTime < MIN_TIME:
sleep(0.001)
print("loop time : "+str(timestamp()-lastTime))
except Exception as e: except Exception as e:
print(e) print(e)
print("Retrying") print("Retrying")
#print("loop time : "+str(timestamp()-lastTime))

View File

@ -2,23 +2,48 @@ import scipy.signal as signal
import pyaudio import pyaudio
from matplotlib import pyplot as plt from matplotlib import pyplot as plt
import numpy as np import numpy as np
from time import sleep from time import sleep, time
import cupy
import atexit
import signal
from telemetrix_rpi_pico import telemetrix_rpi_pico
SAMPLE_SIZE = 1024 def sigint_handler(signal=None, frame=None):
print ('KeyboardInterrupt is caught')
board.neopixel_clear()
sleep(0.75)
board.shutdown()
exit()
signal.signal(signal.SIGINT, sigint_handler)
atexit.register(sigint_handler)
NUM_LEDS = 60
board = telemetrix_rpi_pico.TelemetrixRpiPico()
board.set_pin_mode_neopixel(pin_number=2,num_pixels=NUM_LEDS)
board.neopixel_clear(auto_show=True)
SAMPLE_SIZE = 4096
SAMPLE_RATE = 48000
audio = pyaudio.PyAudio() audio = pyaudio.PyAudio()
audioStream = audio.open(format=pyaudio.paInt16, channels=1, rate=1000, input=True, frames_per_buffer=SAMPLE_SIZE)
audioStream = audio.open(format=pyaudio.paInt16, channels=1, rate=SAMPLE_RATE, input=True, frames_per_buffer=SAMPLE_SIZE)
while True: while True:
data = audioStream.read(SAMPLE_SIZE) data = audioStream.read(SAMPLE_SIZE)
sample = np.frombuffer(data, dtype=np.int16) sample = cupy.frombuffer(data, dtype=np.int16)
power = cupy.sum(cupy.abs(sample))/SAMPLE_SIZE
# plot data if power > 1000:
plt.plot(sample) freq_dom = cupy.fft.fft(sample,10000)
plt.show() freqs = cupy.fft.fftfreq(len(freq_dom))
freqdom_signal = signal.stft(sample) power_bass = cupy.sum(cupy.abs(freq_dom[0:30]))/cupy.sum(cupy.abs(freq_dom))*power
print(freqdom_signal) power_bass = max(0,power_bass-275)
# close stream print(power_bass)
idmax = cupy.argmax(cupy.abs(freq_dom))
freqmax = abs(freqs[idmax]*SAMPLE_RATE)
board.neopixel_fill(r=int(min(255,power_bass)),g=0,b=0,auto_show=True)
else:
board.neopixel_clear(auto_show=True)
audioStream.stop_stream() audioStream.stop_stream()
audioStream.close() audioStream.close()
audio.terminate() audio.terminate()

51
bubblesort.py Normal file
View File

@ -0,0 +1,51 @@
from telemetrix_rpi_pico import telemetrix_rpi_pico
from time import sleep as delay
import signal
import sys
import colorsys
import numpy
from time import perf_counter as millis
def sigint_handler(signal, frame):
print ('KeyboardInterrupt is caught')
board.shutdown()
sys.exit(0)
signal.signal(signal.SIGINT, sigint_handler)
board = telemetrix_rpi_pico.TelemetrixRpiPico()
NUM_LEDS = 60
board.set_pin_mode_neopixel(pin_number=2,num_pixels=NUM_LEDS)
board.neopixel_clear(auto_show=True)
color = [[255,0,0],[255,127,0],[255,255,0],
[127,255,0],[0,255,0],[0,255,127],
[0,255,255],[0,127,255],[0,0,255],
[127,0,255],[255,0,255],[255,0,127]]
timecounter = 0
array = numpy.random.randint(low=0,high=9,size=60)
def updateLED():
for i in range(0,NUM_LEDS):
board.neo_pixel_set_value(i,r=color[array[i]][0],g=color[array[i]][1],b=color[array[i]][2])
board.neopixel_show()
while True:
array = numpy.random.randint(low=0,high=9,size=60)
isSorted = False
for i in range(len(array)):
# loop to compare array elements
for j in range(0, len(array) - i - 1):
# compare two adjacent elements
# change > to < to sort in descending order
if array[j] > array[j + 1]:
# swapping elements if elements
# are not in the intended order
temp = array[j]
array[j] = array[j+1]
array[j+1] = temp
updateLED()
delay(0.1)
board.shutdown()

28
rainbow.py Normal file
View File

@ -0,0 +1,28 @@
from telemetrix_rpi_pico import telemetrix_rpi_pico
from time import sleep as delay
import signal
import sys
import colorsys
from time import perf_counter as millis
def sigint_handler(signal, frame):
print ('KeyboardInterrupt is caught')
board.shutdown()
sys.exit(0)
signal.signal(signal.SIGINT, sigint_handler)
board = telemetrix_rpi_pico.TelemetrixRpiPico()
board.set_pin_mode_neopixel(pin_number=2,num_pixels=60)
board.neopixel_clear(auto_show=True)
SEGMENT_SIZE = 1
VELOCITY = 10
NUM_LEDS = 60
color = []
k = 0
timecounter = 0
while(True):
for j in range(0,NUM_LEDS):
r, g, b = colorsys.hsv_to_rgb(((-timecounter*VELOCITY+j*4)%360)/360,1,1)
board.neo_pixel_set_value(j,r=int(r*255),g=int(g*255),b=int(b*255))
timecounter+=1
delay(0.1)
board.neopixel_show()

34
random_light.py Normal file
View File

@ -0,0 +1,34 @@
from telemetrix_rpi_pico import telemetrix_rpi_pico
from time import sleep as delay
import signal
import sys
import numpy
from time import perf_counter as millis
def sigint_handler(signal, frame):
print ('KeyboardInterrupt is caught')
board.shutdown()
sys.exit(0)
signal.signal(signal.SIGINT, sigint_handler)
board = telemetrix_rpi_pico.TelemetrixRpiPico()
NUM_LEDS = 60
board.set_pin_mode_neopixel(pin_number=2,num_pixels=NUM_LEDS)
board.neopixel_clear(auto_show=True)
color = [[255,0,0],[255,127,0],[255,255,0],
[127,255,0],[0,255,0],[0,255,127],
[0,255,255],[0,127,255],[0,0,255],
[127,0,255],[255,0,255],[255,0,127]]
array = numpy.random.randint(low=0,high=9,size=60)
def updateLED():
for i in range(0,NUM_LEDS):
board.neo_pixel_set_value(i,r=color[array[i]][0],g=color[array[i]][1],b=color[array[i]][2])
board.neopixel_show()
while True:
array = numpy.random.randint(low=0,high=9,size=60)
updateLED()
delay(0.1)
board.shutdown()

7
requirements.txt Normal file
View File

@ -0,0 +1,7 @@
pyautogui
pillow
telemetrix_rpi_pico
numpy
cupy
scipy
pywin32

47
single_speed_particles.py Normal file
View File

@ -0,0 +1,47 @@
from telemetrix_rpi_pico import telemetrix_rpi_pico
from time import sleep as delay
import signal
import sys
import colorsys
import numpy
from scipy.ndimage.interpolation import shift
from time import perf_counter as millis
import threading
def sigint_handler(signal, frame):
print ('KeyboardInterrupt is caught')
board.shutdown()
sys.exit(0)
signal.signal(signal.SIGINT, sigint_handler)
board = telemetrix_rpi_pico.TelemetrixRpiPico()
NUM_LEDS = 60
TIME_UNIT = 0.1
global spaces
spaces = numpy.zeros(NUM_LEDS)
board.set_pin_mode_neopixel(pin_number=2,num_pixels=NUM_LEDS)
board.neopixel_clear(auto_show=True)
def updateLED():
for i in range(0,NUM_LEDS):
board.neo_pixel_set_value(i,r=int(spaces[i]*255),g=int(spaces[i]*255),b=int(spaces[i]*255))
board.neopixel_show()
def advanceTimeUnit():
global spaces
while True:
spaces = shift(spaces,1,cval=0)
updateLED()
delay(TIME_UNIT)
atu = threading.Thread(target=advanceTimeUnit)
atu.start()
while True:
input("Press Enter to Particle")
spaces[0] = 1

16
test.py
View File

@ -1,7 +1,11 @@
import cupy import cupy
a = cupy.asarray([[[1, 2, 3],[4,5,6],[7,8,9]],[[10, 11, 12],[13,14,15],[16,17,18]]]) import colorsys
print(a)
a= cupy.reshape(a,(-1,3))
print(a) peak = [124.3,231.5, 64.4]
a = cupy.median(a,axis=0) h,s,v = colorsys.rgb_to_hsv(r=peak[0]/255,g=peak[1]/255,b=peak[2]/255)
print(a) SATURATION_BOOST_FACTOR = 1.5
s = min(1,s*SATURATION_BOOST_FACTOR)
print([h,s,v])
r,g,b = colorsys.hsv_to_rgb(h=h,s=s,v=v)
print([r,g,b])