#Cerebral Frequencies by Gabriel Rodriguez
#This Art project will allow you to create an interactive brain model.
#Our model will light up and make sounds for specific brain regions.
#We'll use concepts from LED animation, audio, capacitive touch and storage expansion.
#This build is using a Rasberry Pi Pico W.
#Instructions for wiring and building the apparatus will be in the instructable!


#These libraries will allow us to program our external components.
import board, neopixel, touchio, time, digitalio, busio, sdcardio, storage, adafruit_mpr121, audiomixer
from audiopwmio import PWMAudioOut as AudioOut
from audiocore import WaveFile


#We'll use preset colors to light up the brain.
#Import Colors
from adafruit_led_animation.color import RED, YELLOW, ORANGE, GREEN, TEAL, CYAN, BLUE, PURPLE, MAGENTA, PINK, AQUA, WHITE, BLACK


#Let's Create a neopixel object for our neopixel strand
#Red = Power, White = Ground, Green = Signal
#Make sure to change the board location to where the green wire is plugged into the breadboard
strand = neopixel.NeoPixel(board.GP27, 20, brightness = 1, auto_write=True)


#Let's set up the SDCard so that we can have space for all the audio files we will need.
#The comments after each board is specific to my wiring.
#But the connections from a SD card location to breadboard location is consistent and in the instructables.
sck = board.GP10 #GRAY
si = board.GP11 #PURPLE
so = board.GP12 #Blue Short
cs = board.GP13 #Blue Loop
spi = busio.SPI(sck, si, so)
sdcard = sdcardio.SDCard(spi, cs)
vfs = storage.VfsFat(sdcard)
storage.mount(vfs, "/sd") #make sure your PicoW has a folder "sd" in the CIRCUITPY volume or this code won't work.




#Now let's set up audio.
#Again, make sure you specifiy the board location of your speaker to where it is connected on the breadboard.
audio = AudioOut(board.GP16) # Assuming you've got tip of speaker plug wired to GP16
#We can set the path where sound files can be found in the SD card, mine is "brain_sounds"
#Chage path variable if yours is different
path = "sd/brain_sounds/"
# play_sound function - pass in the FULL NAME of file to play
def play_sound(filename):
   with open(path + filename, "rb") as wave_file:
       wave = WaveFile(wave_file)
       audio.play(wave)
       while audio.playing:
           pass


#While we are setting up audio, we'll create a list of all the files we want to play later.
#All formated sounds will be downloadable in the instructable.
#If there are other sounds you would like to use, instructions for how to format them will also be in the instructable.
region_sounds = ["24_formatted.wav", "9.25_formatted.wav", "10.25_formatted.wav", "8_formatted.wav", "13_formatted.wav",\
               "7.25_formatted.wav", "12_formatted.wav", "200_formatted.wav"]


#These sounds are listed to correspond to the frequencies observed in:
#(0)Frontal Lobe, (1) Parietal Lobe, (2) Occipital Lobe, (3) Cingulate Cortex
#(4) Thalamus, (5) Temporal Lobe, (6) Cerebellum Granule Cell Layer, (7) Cerebellum Purkinge Cell Layer


#Now we can set up our capacitive touch object to interact with our brain.
i2c = board.STEMMA_I2C()
touch_pad = adafruit_mpr121.MPR121(i2c)

while True:
   if touch_pad[2].value:
       print("Occipital Lobe")
       strand[5:8] = WHITE * 3
       play_sound("10.25_formatted.wav")
       strand[5:8] = BLACK * 3
       time.sleep(.5)

   elif touch_pad[0].value:
       print("Frontal Lobe")
       strand[0:3] = WHITE * 3
       play_sound("24_formatted.wav")
       strand[0:3] = BLACK * 3
       time.sleep(.5)

   elif touch_pad[1].value:
       print("Parietal Lobe")
       strand[3:5] = WHITE * 2
       play_sound("9.25_formatted.wav")
       strand[3:5] = BLACK * 2

   elif touch_pad[3].value:
       print("Cingulate Cortex")
       strand[8:11] = WHITE * 3
       play_sound("8_formatted.wav")
       strand[8:11] = BLACK * 3
       time.sleep(.5)

   elif touch_pad[4].value:
       print("Thalamus")
       strand[11:14] = WHITE * 3
       play_sound("13_formatted.wav")
       strand[11:14] = BLACK * 3
       time.sleep(.5)

   elif touch_pad[5].value:
       print("Temporal Lobe")
       strand[14:16] = WHITE * 2
       play_sound("7.25_formatted.wav")
       strand[14:16] = BLACK * 2
       time.sleep(.5)

   elif touch_pad[6].value:
       print("Cerebellum: Granule Layer")
       strand[14:18] = WHITE * 4
       play_sound("12_formatted.wav")
       strand[14:18] = BLACK * 4
       time.sleep(.5)

   elif touch_pad[7].value:
       print("Cerebellum: Purkinje Layer")
       strand[14:18] = WHITE * 4
       play_sound("200_formatted.wav")
       strand[14:18] = BLACK * 4
       time.sleep(.5)