code.py
 1  # SPDX-FileCopyrightText: 2018 Dave Astels for Adafruit Industries
 2  #
 3  # SPDX-License-Identifier: MIT
 4  
 5  """
 6  Main signal generator code.
 7  
 8  Adafruit invests time and resources providing this open source code.
 9  Please support Adafruit and open source hardware by purchasing
10  products from Adafruit!
11  
12  Written by Dave Astels for Adafruit Industries
13  Copyright (c) 2018 Adafruit Industries
14  Licensed under the MIT license.
15  
16  All text above must be included in any redistribution.
17  """
18  
19  import rotaryio
20  import board
21  import digitalio
22  from display import Display
23  from adafruit_debouncer import Debouncer
24  from generator import Generator
25  import shapes
26  
27  
28  
29  
30  def change_frequency(frequency, delta):
31      return min(20000, max(150, frequency + delta))
32  
33  
34  def change_shape(shape):
35      return (shape + 1) % shapes.NUMBER_OF_SHAPES
36  
37  
38  def get_encoder_change(encoder, pos):
39      new_position = encoder.position
40      if pos is None:
41          return (new_position, 0)
42      else:
43          return (new_position, new_position - pos)
44  
45  def make_debouncable(pin):
46      switch_io = digitalio.DigitalInOut(pin)
47      switch_io.direction = digitalio.Direction.INPUT
48      switch_io.pull = digitalio.Pull.UP
49      return switch_io
50  
51  def run():
52      display = Display()
53      generator = Generator()
54      button_a = Debouncer(make_debouncable(board.D9))
55      button_b = Debouncer(make_debouncable(board.D6))
56      button_c = Debouncer(make_debouncable(board.D5))
57      encoder_button = Debouncer(make_debouncable(board.D12))
58      encoder = rotaryio.IncrementalEncoder(board.D10, board.D11)
59  
60      current_position = None               # current encoder position
61      change = 0                            # the change in encoder position
62      delta = 0                             # how much to change the frequency by
63      shape = shapes.SINE                          # the active waveform
64      frequency = 440                       # the current frequency
65  
66      display.update_shape(shape)           # initialize the display contents
67      display.update_frequency(frequency)
68  
69      while True:
70          encoder_button.update()
71          button_a.update()
72          button_b.update()
73          button_c.update()
74          current_position, change = get_encoder_change(encoder, current_position)
75  
76          if change != 0:
77              if not button_a.value:
78                  delta = change * 1000
79              elif not button_b.value:
80                  delta = change * 100
81              elif not button_c.value:
82                  delta = change * 10
83              else:
84                  delta = change
85              frequency = change_frequency(frequency, delta)
86  
87          if encoder_button.fell:
88              shape = change_shape(shape)
89  
90          display.update_shape(shape)
91          display.update_frequency(frequency)
92          generator.update(shape, frequency)
93  
94  
95  run()