Skip to content

Instantly share code, notes, and snippets.

@tuftii
Created January 18, 2019 16:40
Show Gist options
  • Save tuftii/879c9c62482b4b27b67f658da760c3ca to your computer and use it in GitHub Desktop.
Save tuftii/879c9c62482b4b27b67f658da760c3ca to your computer and use it in GitHub Desktop.
Jam-Hat demo
from jamhat import JamHat
from time import sleep
hat = JamHat()
tune = ['G5', 'G5', 'A5', 'G5', 'C6', 'B5', 0,
'G5', 'G5', 'A5', 'G5', 'D6', 'C6', 0,
'G5', 'G5', 'GS6', 'E6', 'C6', 'B5', 'A5', 0,
'F6', 'F6', 'E6', 'C6', 'D6', 'C6']
tempo = [0.5, 0.5, 1, 1, 1, 1, 1,
0.5, 0.5, 1, 1, 1, 1, 1,
0.5, 0.5, 1, 1, 1, 1, 1, 1,
0.5, 0.5, 1, 1, 1, 1]
row = [hat.lights_1, hat.lights_2]
count = 0
for note in tune:
if(note == 0):
row[count % 2].off()
sleep(tempo[count])
else:
row[count % 2].on()
hat.buzzer.play(note)
sleep(tempo[count]/2)
hat.off()
sleep(0.1)
count += 1
hat.off()
from gpiozero import CompositeOutputDevice, LEDBoard, Button, PWMOutputDevice
class PWMBuzzer(PWMOutputDevice):
def __init__(self, pin=None, active_high=True, initial_value=0, frequency=100,
pin_factory=None):
super(PWMBuzzer, self).__init__(
pin=pin, active_high=active_high, initial_value=initial_value, frequency=frequency,
pin_factory=pin_factory
)
self.notes = {
'G5': 783.991,
'A5': 880.000,
'B5': 987.767,
'C6': 1046.50,
'D6': 1174.66,
'E6': 1318.51,
'F6': 1396.91,
'G6': 1567.98,
'GS6': 1661.22,
}
def play(self, note):
self.frequency = self.notes[note]
self.value = 0.5 # self.magic_num #for pwm
class JamHat(CompositeOutputDevice):
# Set up a star using GPIO Zero to build a class.
# To use:
def __init__(self, pwm=False, initial_value=False, pin_factory=None):
super(JamHat, self).__init__(
lights_1=LEDBoard(red=5, yellow=12, green=16,
pwm=pwm, initial_value=initial_value,
_order=('red', 'yellow', 'green'),
pin_factory=pin_factory),
lights_2=LEDBoard(red=6, yellow=13, green=17,
pwm=pwm, initial_value=initial_value,
_order=('red', 'yellow', 'green'),
pin_factory=pin_factory),
button_1=Button(19, pull_up=False, pin_factory=pin_factory),
button_2=Button(18, pull_up=False, pin_factory=pin_factory),
buzzer=PWMBuzzer(20, pin_factory=pin_factory),
pin_factory=pin_factory
)
@lurch
Copy link

lurch commented Jan 21, 2019

Given that the else part does hat.off() I suspect that the row[count % 2].off() line is redundant?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment