def update(self, buttons): #A3 and A4 are left and right if buttons.pressed["A3"]: pixel_index = sequence_led_indices[self.selected_step] cp.pixels[pixel_index] = BLACK self.selected_step = (self.selected_step - 1) % 8 trigger_degree(self.sequence[self.selected_step]) self.selected_blinker.start() # TODO schedule note off instead of sleeping time.sleep(0.1) cp.stop_tone() if buttons.pressed["A4"]: pixel_index = sequence_led_indices[self.selected_step] cp.pixels[pixel_index] = BLACK self.selected_step = (self.selected_step + 1) % 8 trigger_degree(self.sequence[self.selected_step]) self.selected_blinker.start() # TODO schedule note off instead of sleeping time.sleep(0.1) cp.stop_tone() self.bottom_blinker.update() self.selected_blinker.update()
def change_tone_if_needed(self): """Find the frequency for the current arpeggio and tilt, and restart the tone if changed""" arpeggio_index = 0 if cp.switch else 1 freq = self.freq_maker.freq(TiltingArpeggios.tilt(), arpeggio_index) if freq != self.last_freq: self.last_freq = freq cp.stop_tone() cp.start_tone(freq)
def keyboard(): arp() buttons = Buttons() octave_offset = 1 # track which keys are currently pressed, and in what order they were # pressed (most recent is last in the list). This allows us to revert to # keys that are still being held when # a key is released. note_stack = [] while True: buttons.update() for k in ALL_BUTTONS: if buttons.pressed[k]: note_stack.append(k) cp.stop_tone() trigger_note(k, octave_offset) for k in ALL_BUTTONS: if buttons.released[k]: # if we release the most recently pressed key, ie, the note currently playing, # we stop the tone and start the next most recently pressed note, if any if note_stack[-1] == k: # switch off the LED (pixel_ix, _) = key_pixel_color_map[k] cp.pixels[pixel_ix] = BLACK note_stack.pop() cp.stop_tone() if note_stack: curr_key = note_stack[-1] trigger_note(curr_key, octave_offset) else: note_stack.remove(k)
def play_game(): cp.pixels.brightness = 50 cp.pixels.fill(0) spot = -1 direction = 1 delay = delay_start win_lose = 0 while win_lose == 0: spot = spot + direction show_spot(spot) play_note(spot) if spot >= 5: direction = -1 if cp.button_b == False: win_lose = -1 elif spot <= 0: direction = 1 delay = delay - delay_incr if cp.button_a == False: win_lose = -1 elif spot > 1 and spot < 5: if cp.button_a or cp.button_b: win_lose = -1 time.sleep(delay) if delay <= delay_end: win_lose = 1 cp.pixels.fill(0) cp.stop_tone() if win_lose > 0: cp.play_file(winner) else: cp.play_file(loser)
else: cp.pixels[index] = (0, 0, 0) # off sound_duration.start() while (1): # typical "loop" for "keep doing stuff" in circuit/micro python # Repeating if blink_interval(): cp.red_led = not cp.red_led # blink if long_short_blink(): # on is longer than off blink_neo(0) if fancy_blink_interval(): # a pattern of 4 intervals, then repeat blink_neo(1) # Timer/One-shots # in this example, these happen only once per run if sound_duration(): # the even/odd trick: if sound_duration.i % 2: # not playing: cp.start_tone(262) else: # playing, so: cp.stop_tone()
def stop_note(self): cp.stop_tone() self.note_on = False
def play_note(note): cp.stop_tone() if cp.switch == False: cp.start_tone(tones[note])
from adafruit_circuitplayground import cp import time import random import board import audiocore import array """ #fft https://github.com/Tschucker/CircuitPython_FFT/blob/master/examples/fft_simpletest.py """ """ #play from array #PLAY FILE FROM ARRAY FREQUENCY = 440 # 440 Hz middle 'A' SAMPLERATE = 8000 # 8000 samples/second, recommended! # Generate one period of sine wav. length = SAMPLERATE // FREQUENCY sine_wave = array.array("H", [0] * length) for i in range(length): sine_wave[i] = int(math.sin(math.pi * 2 * i / 18) * (2 ** 15) + 2 ** 15) print((sine_wave[i],)) cp.stop_tone() cp._speaker_enable.value = True audio = cp._audio_out(board.SPEAKER) sine_wave_sample = audiocore.RawSample(sine_wave) audio.play(sine_wave_sample,loop=1) """ """ #play file from object cp.stop_tone()
def Update(self): #update the state of this button. Needed to incriment timer in button. slide = Button.CURRENT_ACTIVATED is not None and Button.LAST_ACTIVATED is self #If there was a button activated last frame that wasn't us. if self.touchCondition() and not Button.CURRENT_ACTIVATED: # this is touched and no button is the current touched button self.holdTime += 0.01 Button.CURRENT_ACTIVATED = self if(self.state == ButtonStates.UP and not self.Pressed()): #contact made but not long enough to count as a press time.sleep(0.1) # Wait a frame and see if we're still being touched if(self.touchCondition()): #A little bit of debounce on sound (but not light) in case we get some noise self.state = ButtonStates.CONTACT Button.LAST_CONTACTED = self cp.stop_tone() cp.start_tone(100 + (50 * (MODE * 9 +self.order))) elif (self.state == ButtonStates.CONTACT and self.Pressed()): self.state = ButtonStates.JUST_PRESSED cp.stop_tone() time.sleep(0.01) elif self.state == ButtonStates.JUST_PRESSED: self.state = ButtonStates.PRESSED cp.start_tone(100 + (50 * (MODE * 9 +self.order))) elif self.state == ButtonStates.PRESSED and self.Held(): cp.stop_tone() time.sleep(0.01) cp.start_tone(100 + (50 * (MODE * 9 +self.order))) self.state = ButtonStates.DOWN else : self.holdTime = 0.0 if self.state == ButtonStates.DOWN: self.state = ButtonStates.JUST_RELEASED_HOLD if not slide: cp.stop_tone() elif self.state== ButtonStates.JUST_PRESSED or self.state == ButtonStates.PRESSED: self.state = ButtonStates.JUST_RELEASED_PRESS if not slide: cp.stop_tone() elif self.state == ButtonStates.JUST_RELEASED_HOLD or self.state == ButtonStates.JUST_RELEASED_PRESS: self.state = ButtonStates.RELEASED elif self.state == ButtonStates.CONTACT: if not slide: cp.stop_tone() self.state = ButtonStates.UP else: self.state = ButtonStates.UP