#! /usr/bin/python

import atexit, pickle, socket, time, subprocess
import os, sys, digitalio, busio, pwmio, adafruit_ssd1306, board ## PG added
from board import SCL, SDA
import datetime as dt ## PG added
from PIL                   import Image, ImageDraw, ImageFont ## PG added
from adafruit_debouncer    import Debouncer ## PG added
from pygame                import mixer

## Load the sounds:
mixer.init()
sound_applause = mixer.Sound('/home/pi/pianobar/wav_files/applause-1.wav')
sound_play     = mixer.Sound('/home/pi/pianobar/wav_files/play.wav')
sound_potty    = mixer.Sound('/home/pi/pianobar/wav_files/potty.wav')
sound_water    = mixer.Sound('/home/pi/pianobar/wav_files/water.wav')
sound_walk     = mixer.Sound('/home/pi/pianobar/wav_files/walk.wav')
sound_later    = mixer.Sound('/home/pi/pianobar/wav_files/later.wav')
sound_anabelle = mixer.Sound('/home/pi/pianobar/wav_files/anabelle.wav')
sound_outside  = mixer.Sound('/home/pi/pianobar/wav_files/outside.wav')
sound_hmmm     = mixer.Sound('/home/pi/pianobar/wav_files/hmmm.wav')

HOLD_TIME    = 1.0 # Time (seconds) to hold select button for shut down

def make_pin_reader(pin):
    io = digitalio.DigitalInOut(pin)
    io.direction = digitalio.Direction.INPUT
    io.pull = digitalio.Pull.UP
    return lambda: io.value

## define debounced buttons
D4_TBD1      = Debouncer(make_pin_reader(board.D4),  interval=0.02)
D7_shutdown  = Debouncer(make_pin_reader(board.D7),  interval=0.02) #CS1
D8_TBD2      = Debouncer(make_pin_reader(board.D8),  interval=0.02) #CS0
D9_TBD3      = Debouncer(make_pin_reader(board.D9),  interval=0.02) #MISO
D10_applause = Debouncer(make_pin_reader(board.D10), interval=0.02) #MOSI
D11_play     = Debouncer(make_pin_reader(board.D11), interval=0.02) #SCLK
D17_potty    = Debouncer(make_pin_reader(board.D17), interval=0.02)
D18_water    = Debouncer(make_pin_reader(board.D18), interval=0.02)
D22_walk     = Debouncer(make_pin_reader(board.D22), interval=0.02)
D23_later    = Debouncer(make_pin_reader(board.D23), interval=0.02)
D24_anabelle = Debouncer(make_pin_reader(board.D24), interval=0.02)
D25_outside  = Debouncer(make_pin_reader(board.D25), interval=0.02)
D27_hmmm     = Debouncer(make_pin_reader(board.D27), interval=0.02)

t2 = 0
continue_looping = True
while continue_looping:
    D4_TBD1.update()
    D7_shutdown.update()
    D8_TBD2.update()
    D9_TBD3.update()
    D10_applause.update()
    D11_play.update()
    D17_potty.update()
    D18_water.update()
    D22_walk.update()
    D23_later.update()
    D24_anabelle.update()
    D25_outside.update()
    D27_hmmm.update()

    if D4_TBD1.fell:      t2 = time.time()
#    if D7_shutdown.fell: # Handled later
    if D8_TBD2.fell:      t2 = time.time()
    if D9_TBD3.fell:      t2 = time.time()
    if D10_applause.fell: sound_applause.play()
    if D11_play.fell:     sound_play.play()
    if D17_potty.fell:    sound_potty.play()
    if D18_water.fell:    sound_water.play()
    if D22_walk.fell:     sound_walk.play()
    if D23_later.fell:    sound_later.play()
    if D24_anabelle.fell: sound_anabelle.play()
    if D25_outside.fell:  sound_outside.play()
    if D27_hmmm.fell:     sound_hmmm.play()

    if D7_shutdown.fell:
        t = time.time()
        while not D7_shutdown.value:
            D7_shutdown.update()
            if (time.time() - t) >= HOLD_TIME:
                t2 = time.time()
                continue_looping = False
                break # needed in case button isn't released

    if not continue_looping:
        break


time.sleep(1)
os.system('sudo shutdown now -h')
exit(0)