#!/usr/bin/env python import time from sys import exit import drumhat def hit_handler(event): print("Hit on pad: {}".format(event.pad)) def rel_handler(event): print("Release on pad: {}".format(event.pad)) drumhat.on_hit(drumhat.PADS, hit_handler) drumhat.on_release(drumhat.PADS, rel_handler) try: drumhat.auto_leds = False pads = drumhat.PADS while True: drumhat.led_on(pads[-1]) drumhat.led_off(pads[0]) pads.insert(0, pads.pop()) time.sleep(0.1) except KeyboardInterrupt: exit()
#try/except语句用来检测try语句块中的错误, #从而让except语句捕获异常信息并处理。 #小车循环前进1s,后退1s,左转2s,右转2s,原地左转3s #原地右转3s,停止1s。 userin = 0 #userin = input("Press a command to use the car. Type q to leave.") if userin != quit: motor_init() while userin != "quit": try: drumhat.on_hit(drumhat.PADS, handle_hit) drumhat.on_release(drumhat.PADS, handle_release) #hile True: # run(1) #fo #back(1) #left(2) #right(2) #spin_left(3) #spin_right(3) #brake(1) except KeyboardInterrupt: pass #userin = input("Press a command to use the car. Type q to leave.") pwm_ENA.stop() pwm_ENB.stop()
def __init__(self, sound_index): super(Drums, self).__init__(sound_index) drumhat.on_hit(drumhat.PADS, self.handle_hit) drumhat.on_release(drumhat.PADS, self.handle_release)
6 = Hat, 8 = Clap, 3 = Cowbell 5 = Snare, 4 = Base Press CTRL+C to exit! """) BANK = os.path.join(os.path.dirname(__file__), DRUM_FOLDER) pygame.mixer.init(44100, -16, 1, 512) pygame.mixer.set_num_channels(16) files = glob.glob(os.path.join(BANK, "*.wav")) files.sort() samples = [pygame.mixer.Sound(f) for f in files] def handle_hit(event): # event.channel is a zero based channel index for each pad # event.pad is the pad number from 1 to 8 samples[event.channel].play(loops=0) print("You hit pad {}, playing: {}".format(event.pad,files[event.channel])) def handle_release(): pass drumhat.on_hit(drumhat.PADS, handle_hit) drumhat.on_release(drumhat.PADS, handle_release) while True: time.sleep(1)
import drumhat import time def hit_handler(event): print("Hit on pad: {}".format(event.pad)) def rel_handler(event): print("Release on pad: {}".format(event.pad)) drumhat.on_hit(drumhat.PADS, hit_handler) drumhat.on_release(drumhat.PADS, rel_handler) try: drumhat.auto_leds = False pads = drumhat.PADS while True: drumhat.led_on(pads[-1]) drumhat.led_off(pads[0]) pads.insert(0,pads.pop()) time.sleep(0.1) except KeyboardInterrupt: exit()