|
22 | 22 |
|
23 | 23 | import time |
24 | 24 |
|
25 | | -import audioio |
26 | 25 | import audiocore |
| 26 | +import audioio |
27 | 27 | import board |
28 | 28 | import digitalio |
29 | 29 | import winterbloom_voltageio |
30 | 30 |
|
| 31 | +try: |
| 32 | + import _bhb |
| 33 | +except ImportError: |
| 34 | + raise EnvironmentError("This BHB library requires CircuitPython >= 6.0.0") |
| 35 | + |
| 36 | + |
| 37 | +def _detect_board_revision(): |
| 38 | + v5pin = digitalio.DigitalInOut(board.V5) |
| 39 | + v5pin.switch_to_input(pull=digitalio.Pull.UP) |
| 40 | + |
| 41 | + # Pulled low on v5+, pull-up will make it true |
| 42 | + # on <=v4. |
| 43 | + if not v5pin.value: |
| 44 | + return 5 |
| 45 | + else: |
| 46 | + return 4 |
| 47 | + |
| 48 | + |
| 49 | +class _AnalogIn: |
| 50 | + def __init__(self): |
| 51 | + _bhb.init_adc() |
| 52 | + |
| 53 | + @property |
| 54 | + def value(self): |
| 55 | + return _bhb.read_adc() |
| 56 | + |
31 | 57 |
|
32 | 58 | class BigHonkingButton: |
33 | 59 | def __init__(self): |
| 60 | + self.board_revision = _detect_board_revision() |
34 | 61 | self.pitch_settle_time = 0.001 |
35 | 62 | self._button = digitalio.DigitalInOut(board.BUTTON) |
36 | 63 | self._button.switch_to_input(pull=digitalio.Pull.UP) |
37 | 64 | self._gate_in = digitalio.DigitalInOut(board.GATE_IN) |
38 | 65 | self._gate_in.switch_to_input() |
39 | 66 | self._gate_out = digitalio.DigitalInOut(board.GATE_OUT) |
40 | 67 | self._gate_out.switch_to_output() |
41 | | - self._pitch_in = winterbloom_voltageio.VoltageIn.from_pin(board.PITCH_IN) |
42 | | - self._pitch_in.direct_calibration( |
43 | | - {64736: -2.0, 48384: -1.0, 32048: 0, 15552: 1.0, 128: 2.0} |
44 | | - ) |
| 68 | + self._pitch_in = winterbloom_voltageio.VoltageIn(_AnalogIn()) |
| 69 | + |
| 70 | + if self.board_revision >= 5: |
| 71 | + self._pitch_in.direct_calibration( |
| 72 | + {4068: -5.0, 3049: -2.5, 2025: 0, 1001: 2.5, 8: 5.0} |
| 73 | + ) |
| 74 | + else: |
| 75 | + self._pitch_in.direct_calibration( |
| 76 | + {4068: -2.0, 3049: -1.0, 2025: 0, 1001: 1.0, 8: 2.0} |
| 77 | + ) |
| 78 | + |
45 | 79 | self.audio_out = audioio.AudioOut(board.HONK_OUT) |
46 | 80 |
|
47 | 81 | self._last_gate_value = False |
|
0 commit comments