import touchio import board import time import neopixel import adafruit_debouncer import digitalio import busio import adafruit_lis3dh import rainbowio from adafruit_ble import BLERadio from adafruit_ble.advertising.standard import ProvideServicesAdvertisement from adafruit_ble.services.nordic import UARTService import math uart = UARTService() advertisement = ProvideServicesAdvertisement(uart) ble = BLERadio() advertisement.complete_name = "RIGHTPAW" ble.name = advertisement.complete_name A_pin = digitalio.DigitalInOut(board.BUTTON_A) A_pin.pull = digitalio.Pull.DOWN A_button = adafruit_debouncer.Debouncer(A_pin) B_pin = digitalio.DigitalInOut(board.BUTTON_B) B_pin.pull = digitalio.Pull.DOWN B_button = adafruit_debouncer.Debouncer(B_pin) i2c = busio.I2C(board.ACCELEROMETER_SCL, board.ACCELEROMETER_SDA) int1 = digitalio.DigitalInOut(board.ACCELEROMETER_INTERRUPT) accelerometer = adafruit_lis3dh.LIS3DH_I2C(i2c, address=0x19, int1=int1) accelerometer.set_tap(2, 40, time_window= 65)#15 default accelerometer.range = adafruit_lis3dh.RANGE_16_G pix = neopixel.NeoPixel(board.NEOPIXEL,10,brightness=5/255) # def placeholding_lambda(): # return False pins = [board.A1,board.A2,board.A3,board.TX] # pinky = touchio.TouchIn(board.TX) # ring = touchio.TouchIn(board.A1) # index = touchio.TouchIn(board.A2) # thumb = touchio.TouchIn(board.A1) pawpad = digitalio.DigitalInOut(board.RX) pawpad.direction = digitalio.Direction.OUTPUT pawpad.value = False # pin1 = touchio.TouchIn(board.A2) # pin2 = touchio.TouchIn(board.A3) # pads = [pinky,ring,index,pawpad] pin_dig_input = [] dig = [] for i in pins: pin = digitalio.DigitalInOut(i) pin.direction = digitalio.Direction.INPUT pin.pull = digitalio.Pull.UP dig.append(pin) pin_dig_input.append(adafruit_debouncer.Debouncer(pin)) interaction_mode = False GREEN = [0,150,0] ORANGE = [255,110,2] RED = [150,0,0] pawpad_debounce = adafruit_debouncer.Debouncer(pawpad) air_guitar = False previous_synth_delay_time = 0 last_time_step = time.monotonic() vx, vy, vz, v = 0,0,0,0 note_on = False accelerations = [] angle_mode = False angle_samples = 20 angles = [] while True: # while True: # A_button.update() # if A_button.fell: # print(A_button.last_duration) # #print(A_button.last_duration,end='\r') # #time.sleep(0.016) if interaction_mode: pix[9] = [150,0,150] else: pix[9] = 0 ble.start_advertising(advertisement) while not ble.connected: pix[5] = RED pass if ble.connected: pix[5] = GREEN else: pix[5] = RED while ble.connected: pix[8] = rainbowio.colorwheel(time.monotonic_ns()//8e6) A_button.update() B_button.update() for pin in pin_dig_input: pin.update() if B_button.fell: uart.write(b'ZVOL') #print(A_button.fell) #print(pin1.value,pin2.value,pin1.raw_value,pin2.raw_value) #print([i.value for i in pin_touch], end = '\r') if A_button.fell: interaction_mode = not interaction_mode print(f'interaction mode : {interaction_mode}') if interaction_mode: pawpad.deinit() pawpad = touchio.TouchIn(board.RX) pawpad_debounce = adafruit_debouncer.Debouncer(pawpad) pix[9] = [150,0,150] else: for i in dig: print(i.pull) pawpad.deinit() pawpad = digitalio.DigitalInOut(board.RX) pawpad.direction = digitalio.Direction.OUTPUT pawpad.value = False pix[9] = 0 for i in range(len(pin_dig_input)): if pin_dig_input[i].fell: msg = f'{i}R@@'#our msg, example: 1R@ 1 is pad, R is rose, @ is always there uart.write(msg) pix[i] = ORANGE print(f'pin:{i}') #uart.write(str(i)) if pin_dig_input[i].rose: msg = f'{i}F@@'#our msg, example: 1R@ 1 is pad, F is fell, @ is always there uart.write(msg) pix[i] = 0 if interaction_mode: pawpad_debounce.update() if pawpad_debounce.rose: pix[4] = ORANGE uart.write(b'5R@@') elif pawpad_debounce.fell: uart.write(b'5F@@') pix[4] = 0 if uart.in_waiting: received = uart.read(4) if received == b'AIRT': air_guitar = True if received == b'AIRF': air_guitar = False #if received == b'ANGL': #angle_mode = not angle_mode #print(received) if angle_mode: Ax, Ay, Az, = accelerometer.acceleration try: theta_angle = math.degrees(math.atan(Ax/(math.sqrt((Ay**2)+(Az**2))))) psi_angle = math.degrees(math.atan(Ay/(math.sqrt((Ax**2)+(Az**2))))) phi_angle = math.degrees(math.atan((math.sqrt((Ax**2)+(Ay**2)))/Az)) except ZeroDivisionError: pass angle = theta_angle angles.append(angle) if len(angles) > angle_samples: angles.pop(0) avg_angle = sum(angles)//angle_samples shift_angle = int(avg_angle + 90) print(f'{shift_angle:03d}') #uart.write(f'{angle:2.1f}') # if B_button.rose: # air_guitar = not air_guitar # if air_guitar: # uart.write(b'AIRT') # elif not air_guitar: # uart.write(b'AIRF') # print(f'air guitar:{air_guitar}') if air_guitar: Ax, Ay, Az, = accelerometer.acceleration A = ((Ax**2)+(Ay**2)+(Az**2))**.5 now = time.monotonic() time_between = now - last_time_step if time_between > 0.001: # v = v_init + ((Ay)*time_between) # v_init = v if A>11: vx += Ax * time_between vy += Ay * time_between vz += Az * time_between v = ((vx**2)+(vy**2)+(vz**2))**.5 #slope = (Ay - prev_Ay)/time_between #prev_Ay = Ay else: vx, vy, vz, v = 0,0,0,0 last_time_step = now if v > .75:#prev 1 accelerations.append(A) max_A = max(accelerations) synth_delay_time = int((max_A/45)*100) #print(accelerations) if previous_synth_delay_time != synth_delay_time: print('HELLO?',previous_synth_delay_time, synth_delay_time) uart.write(f'N{synth_delay_time:03d}') previous_synth_delay_time = synth_delay_time #print(max_A/40) # amp_env_slow = synthio.Envelope(attack_time=0.0, release_time=max_A/release_quotient, sustain_level=1.0) # synth.envelope = amp_env_slow # spkEN.value = True # synth.press(note) last_time_touched = time.monotonic() note_on = True else: accelerations = [] if note_on: uart.write(b'G000') note_on = False # synth.release(note)