code.py
  1  # SPDX-FileCopyrightText: 2017 John Edgar Park for Adafruit Industries
  2  #
  3  # SPDX-License-Identifier: MIT
  4  
  5  # FruitBox Sequencer
  6  # for Adafruit Circuit Playground express
  7  # with CircuitPython
  8  
  9  import time
 10  
 11  from adafruit_circuitplayground.express import cpx
 12  
 13  # Change this number to adjust touch sensitivity threshold, 0 is default
 14  cpx.adjust_touch_threshold(600)
 15  
 16  bpm = 60  # quarter note beats per minute, change this to suit your tempo
 17  
 18  beat = 15 / bpm  # 16th note expressed as seconds
 19  
 20  WHITE = (30, 30, 30)
 21  RED = (90, 0, 0)
 22  YELLOW = (45, 45, 0)
 23  GREEN = (0, 90, 0)
 24  AQUA = (0, 45, 45)
 25  BLUE = (0, 0, 90)
 26  PURPLE = (45, 0, 45)
 27  BLACK = (0, 0, 0)
 28  
 29  cpx.pixels.brightness = 0.1  # set brightness value
 30  
 31  # The seven files assigned to the touchpads
 32  audio_files = ["fB_bd_tek.wav", "fB_elec_hi_snare.wav", "fB_elec_cymbal.wav",
 33                 "fB_elec_blip2.wav", "fB_bd_zome.wav", "fB_bass_hit_c.wav",
 34                 "fB_drum_cowbell.wav"]
 35  
 36  step_advance = 0  # to count steps
 37  step = 0
 38  
 39  # sixteen steps in a sequence
 40  step_note = [1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1]
 41  
 42  # step pixels
 43  step_pixel = [9, 8, 7, 6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2]
 44  
 45  # step colors
 46  step_col = [WHITE, RED, YELLOW, GREEN, AQUA, BLUE, PURPLE, BLACK]
 47  
 48  
 49  def prog_mode(index):
 50      cpx.play_file(audio_files[index])
 51      step_note[step] = index
 52      cpx.pixels[step_pixel[step]] = step_col[step_note[step]]
 53      print("playing file " + audio_files[index])
 54  
 55  
 56  while True:
 57      # playback mode
 58      if cpx.switch:  # switch is slid to the left, play mode
 59  
 60          cpx.red_led = False
 61  
 62          if cpx.button_a:
 63              cpx.pixels.fill(GREEN)
 64              time.sleep(.2)
 65              cpx.pixels.fill(BLACK)
 66  
 67          if cpx.button_b:
 68              for i in range(16):
 69                  step = i
 70                  # light a pixel
 71                  cpx.pixels[step_pixel[i]] = step_col[step_note[i]]
 72                  cpx.pixels[step_pixel[i - 1]] = BLACK
 73  
 74                  # play a file
 75                  cpx.play_file(audio_files[step_note[i]])
 76  
 77                  # sleep a beat
 78                  time.sleep(beat)
 79              cpx.pixels.fill(BLACK)
 80  
 81      # beat programming mode
 82      else:  # switch is slid to the right, record mode
 83          cpx.red_led = True
 84          if cpx.button_a:  # clear pixels, reset step to first step
 85              cpx.pixels.fill(RED)
 86              time.sleep(.2)
 87              cpx.pixels.fill(BLACK)
 88              cpx.pixels[9] = WHITE
 89              step = 0
 90              step_advance = 0
 91  
 92          # press B button to advance neo pixel steps
 93          if cpx.button_b:  # button has been pressed
 94              step_advance += 1
 95              step = step_advance % 16
 96              cpx.play_file(audio_files[step_note[step]])
 97              cpx.pixels[step_pixel[step]] = step_col[step_note[step]]
 98              cpx.pixels[step_pixel[step - 1]] = BLACK
 99  
100          if cpx.touch_A1:
101              prog_mode(0)
102          if cpx.touch_A2:
103              prog_mode(1)
104          if cpx.touch_A3:
105              prog_mode(2)
106          if cpx.touch_A4:
107              prog_mode(3)
108          if cpx.touch_A5:
109              prog_mode(4)
110          if cpx.touch_A6:
111              prog_mode(5)
112          if cpx.touch_A7:
113              prog_mode(6)