# MAKE ART PROJECT

import board
import neopixel
import time

import digitalio
import adafruit_lis3dh
import busio

import random

from audiopwmio import PWMAudioOut as AudioOut
from audiocore import WaveFile

import pwmio
from adafruit_motor import servo

brightness = 1

RED = (255, 0, 0)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
BLACK = (0, 0, 0)
JADE = (0, 255, 40)
MAGENTA = (255, 0, 20)
ORANGE = (255, 40, 0)
PURPLE = (180, 0, 255)
WHITE = (255, 255, 255)
YELLOW = (255, 150, 0)
INDIGO = (63, 0, 255)
VIOLET = (127, 0, 255)

colors = [RED, MAGENTA, ORANGE, YELLOW, GREEN, JADE, BLUE, INDIGO, VIOLET, PURPLE, BLACK]

UNIT = 0.1

pixels_pin = board.NEOPIXEL
pixels_num_of_lights = 10

pixels = neopixel.NeoPixel(pixels_pin, pixels_num_of_lights, brightness=brightness)

strip_pin = board.A1
strip_num_of_lights = 30

strip = neopixel.NeoPixel(strip_pin, strip_num_of_lights, brightness=brightness)

strip_lights_per_neopixel = int(strip_num_of_lights / pixels_num_of_lights)

i2c = busio.I2C(board.ACCELEROMETER_SCL, board.ACCELEROMETER_SDA)
int1 = digitalio.DigitalInOut(board.ACCELEROMETER_INTERRUPT)
accelerometer = adafruit_lis3dh.LIS3DH_I2C(i2c, address=0x19, int1=int1)
accelerometer.range = adafruit_lis3dh.RANGE_8_G

pwm = pwmio.PWMOut(board.A2, frequency=50)
servo_1 = servo.Servo(pwm, min_pulse = 500, max_pulse = 2500)
servo_1.angle = 180

speaker = digitalio.DigitalInOut(board.SPEAKER_ENABLE)
speaker.direction = digitalio.Direction.OUTPUT
speaker.value = True
audio = AudioOut(board.SPEAKER)

path = "sounds/"

def play_sound(filename):
    with open(path + filename, "rb") as wave_file:
        wave = WaveFile(wave_file)
        audio.play(wave)
        while audio.playing:
            pass

def PaintTheRainbow():
    for i in range(len(pixels)):
        pixels[i] = colors[i]
        for light_in_group in range(strip_lights_per_neopixel):
            strip_light = i * (strip_lights_per_neopixel) + light_in_group
            strip[strip_light] = colors[i]
        time.sleep(3 * UNIT)

while True:
    random_number = -1
    if accelerometer.shake(shake_threshold=12):
        PaintTheRainbow()
        random_number = random.randint(0,3)
        time.sleep(2 * UNIT)
        if random_number == 0:
            pixels.fill(RED)
            strip.fill(RED)
            play_sound("NewtonCampus.wav")
            servo_1.angle = 180 - (45 * random_number + 22)
            time.sleep(10 * UNIT)
        if random_number == 1:
            pixels.fill(YELLOW)
            strip.fill(YELLOW)
            play_sound("Chevy.wav")
            servo_1.angle = 180 - (45 * random_number + 22)
            time.sleep(10 * UNIT)
        if random_number == 2:
            pixels.fill(GREEN)
            strip.fill(GREEN)
            play_sound("CXLF.wav")
            servo_1.angle = 180 - (45 * random_number + 22)
            time.sleep(10 * UNIT)
        if random_number == 3:
            pixels.fill(BLUE)
            strip.fill(BLUE)
            play_sound("Kostka.wav")
            servo_1.angle = 180 - (45 * random_number + 22)
            time.sleep(10 * UNIT)
    pixels.fill(BLACK)
    strip.fill(BLACK)
