-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcore.py
97 lines (78 loc) · 2.66 KB
/
core.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# a 3x7 button matrix
# each button plays a sound, up to 6 can play simultaneously
import RPi.GPIO as GPIO
import pygame.mixer
from time import sleep
from sys import exit
import os
import sounds
print("sound matrix is running...")
GPIO.setmode(GPIO.BCM)
ButtonIDs = [ [1,2,3,4,5,6,7],
[8,9,10,11,12,13,14],
[15,16,17,18,19,20,21] ]
# gpio inputs for rows
RowPins = [16,20,21]
# gpio outputs for columns
ColumnPins = [26,19,13,6,5,22,27]
# gpio input for shutdown button pin
ShutdownPin = 12
# set maximum number of channels to 5
pygame.mixer.set_num_channels(6)
# define four inputs with pull up resistor
for i in range(len(RowPins)):
GPIO.setup(RowPins[i], GPIO.IN, pull_up_down = GPIO.PUD_UP)
# define four outputs and set to high
for j in range(len(ColumnPins)):
GPIO.setup(ColumnPins[j], GPIO.OUT)
GPIO.output(ColumnPins[j], 1)
# define input for shutdown button pin
GPIO.setup(ShutdownPin, GPIO.IN, pull_up_down = GPIO.PUD_UP)
def ActivateButton(rowPin, colPin):
sndIndex = ButtonIDs[rowPin][colPin] - 1
print("button " + str(sndIndex + 1) + " pressed")
PlaySound(sounds.SoundsList[sndIndex])
# prevent button presses too close together
sleep(.3)
def PlaySound(sound):
# get next available sound channel
# True arg override returns longest running
# sound channel when none are available
nextAvailableChannel = pygame.mixer.find_channel(True)
# channel should techincally never be null because above arg is True
# null checking just in case
if nextAvailableChannel != None and nextAvailableChannel.get_busy() == False:
nextAvailableChannel.play(sound)
def EndScript():
GPIO.cleanup()
print("sound matrix is exiting...")
exit()
def ShutdownSystem():
# slowly stop pygame operations and shutdown the RPi
sleep(1)
pygame.mixer.stop()
sleep(.5)
pygame.mixer.quit()
sleep(1)
os.system('shutdown now -h')
# comment this out to run script
#EndScript()
try:
while(True):
for j in range(len(ColumnPins)):
# set each output pin to low
GPIO.output(ColumnPins[j],0)
for i in range(len(RowPins)):
if GPIO.input(RowPins[i]) == 0:
ActivateButton(i,j)
# do nothing while button is being held down
while(GPIO.input(RowPins[i]) == 0):
pass
# return each output pin to high
GPIO.output(ColumnPins[j],1)
# listen for shutdown button
shutdownInputValue = GPIO.input(ShutdownPin)
if (shutdownInputValue == False):
ShutdownSystem()
except KeyboardInterrupt:
EndScript()