python-telemetrix-ambilight/bubblesort.py

52 lines
1.5 KiB
Python
Raw Permalink Normal View History

2022-03-30 13:15:41 +00:00
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()