From ea90d8701916c7d12307872f56f3da4470cd204e Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Wed, 30 Mar 2022 20:15:41 +0700 Subject: [PATCH] add other effects --- .gitignore | 2 ++ ambilight_mulitprocessing.py | 55 +++++++++++++++++++++--------------- beat_detection.py | 47 ++++++++++++++++++++++-------- bubblesort.py | 51 +++++++++++++++++++++++++++++++++ rainbow.py | 28 ++++++++++++++++++ random_light.py | 34 ++++++++++++++++++++++ requirements.txt | 7 +++++ single_speed_particles.py | 47 ++++++++++++++++++++++++++++++ test.py | 16 +++++++---- 9 files changed, 248 insertions(+), 39 deletions(-) create mode 100644 .gitignore create mode 100644 bubblesort.py create mode 100644 rainbow.py create mode 100644 random_light.py create mode 100644 requirements.txt create mode 100644 single_speed_particles.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1b05740 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ + +__pycache__/ diff --git a/ambilight_mulitprocessing.py b/ambilight_mulitprocessing.py index c3e8d60..9a35ffd 100644 --- a/ambilight_mulitprocessing.py +++ b/ambilight_mulitprocessing.py @@ -1,7 +1,6 @@ from tkinter import TOP import pyautogui import scipy.cluster as cluster -import scipy import sys import numpy as np 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 multiprocessing import Pool import PIL.ImageGrab - +from PIL import Image +import colorsys +import win32gui, win32ui, win32con class size: width = None height = None +def get_screenshot(): + return PIL.ImageGrab.grab() + def sigint_handler(signal=None, frame=None): print ('KeyboardInterrupt is caught') board.neopixel_clear() sleep(0.75) board.shutdown() - sys.exit(0) + exit() -FAST_MODE = False +FAST_MODE = True +SATURATION_BOOST_FACTOR = 2.5 def find_dorminant_color(im): if FAST_MODE: color = numpy.reshape(numpy.asarray(im),(-1,3)) 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: - NUM_CLUSTERS = 1 + NUM_CLUSTERS = 2 im = im.resize((100, 100)) ar = np.asarray(im) @@ -45,9 +53,15 @@ def find_dorminant_color(im): index_max = np.argmax(counts) 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__': + size = size() + size.width=pyautogui.size().width + size.height=pyautogui.size().height signal.signal(signal.SIGINT, sigint_handler) atexit.register(sigint_handler) 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 RIGHT_LEDS = 11 - BUTTOM_LEDS = 17 - LEFT_LEDS = 10 + BUTTOM_LEDS = 19 + LEFT_LEDS = 11 + MIN_TIME = 0.05 + NUM_LEDS = TOP_LEDS+RIGHT_LEDS+BUTTOM_LEDS+LEFT_LEDS board.set_pin_mode_neopixel(pin_number=2,num_pixels=NUM_LEDS) board.neopixel_clear(auto_show=True) - size = size() - - size.width=pyautogui.size().width - size.height=pyautogui.size().height - board.neopixel_show() screenshot = pyautogui.screenshot() - pool = Pool(16) - + pool = Pool(8) + while True: try: - screenshot = PIL.ImageGrab.grab() + screenshot = get_screenshot() lastTime = timestamp() chunk = [] 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)): segment = left.crop(box=[0,i*size.height/LEFT_LEDS,BOARDER_SIZE,(i+1)*size.height/LEFT_LEDS]) chunk.append(segment) - - colors = pool.map(find_dorminant_color,chunk) 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.neopixel_show() + while timestamp()-lastTime < MIN_TIME: + sleep(0.001) + print("loop time : "+str(timestamp()-lastTime)) except Exception as e: print(e) print("Retrying") - #print("loop time : "+str(timestamp()-lastTime)) diff --git a/beat_detection.py b/beat_detection.py index 8caaebf..2edd016 100644 --- a/beat_detection.py +++ b/beat_detection.py @@ -2,23 +2,48 @@ import scipy.signal as signal import pyaudio from matplotlib import pyplot as plt 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() -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: data = audioStream.read(SAMPLE_SIZE) - sample = np.frombuffer(data, dtype=np.int16) - - # plot data - plt.plot(sample) - plt.show() - freqdom_signal = signal.stft(sample) - print(freqdom_signal) -# close stream + sample = cupy.frombuffer(data, dtype=np.int16) + power = cupy.sum(cupy.abs(sample))/SAMPLE_SIZE + if power > 1000: + freq_dom = cupy.fft.fft(sample,10000) + freqs = cupy.fft.fftfreq(len(freq_dom)) + power_bass = cupy.sum(cupy.abs(freq_dom[0:30]))/cupy.sum(cupy.abs(freq_dom))*power + power_bass = max(0,power_bass-275) + 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.close() audio.terminate() \ No newline at end of file diff --git a/bubblesort.py b/bubblesort.py new file mode 100644 index 0000000..5124d34 --- /dev/null +++ b/bubblesort.py @@ -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() diff --git a/rainbow.py b/rainbow.py new file mode 100644 index 0000000..964e3cd --- /dev/null +++ b/rainbow.py @@ -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() diff --git a/random_light.py b/random_light.py new file mode 100644 index 0000000..0c9ab2c --- /dev/null +++ b/random_light.py @@ -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() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..6511497 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,7 @@ +pyautogui +pillow +telemetrix_rpi_pico +numpy +cupy +scipy +pywin32 diff --git a/single_speed_particles.py b/single_speed_particles.py new file mode 100644 index 0000000..0315c25 --- /dev/null +++ b/single_speed_particles.py @@ -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 + + + + diff --git a/test.py b/test.py index 5414ecf..e35637f 100644 --- a/test.py +++ b/test.py @@ -1,7 +1,11 @@ import cupy -a = cupy.asarray([[[1, 2, 3],[4,5,6],[7,8,9]],[[10, 11, 12],[13,14,15],[16,17,18]]]) -print(a) -a= cupy.reshape(a,(-1,3)) -print(a) -a = cupy.median(a,axis=0) -print(a) \ No newline at end of file +import colorsys + + +peak = [124.3,231.5, 64.4] +h,s,v = colorsys.rgb_to_hsv(r=peak[0]/255,g=peak[1]/255,b=peak[2]/255) +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]) \ No newline at end of file