Browse Source

init

master
heck 3 years ago
commit
739e828c0b
  1. 7
      Makefile
  2. 13
      blinky.py
  3. 74
      code.py

7
Makefile

@ -0,0 +1,7 @@
.PHONY: all
all: flash
flash:
cp code.py /Volumes/CIRCUITPY/

13
blinky.py

@ -0,0 +1,13 @@
# import board
# import digitalio
# import time
#
# led = digitalio.DigitalInOut(board.LED)
# led.direction = digitalio.Direction.OUTPUT
#
# while True:
# print("Hello, CircuitPython!")
# led.value = True
# time.sleep(0.1)
# led.value = False
# time.sleep(0.1)

74
code.py

@ -0,0 +1,74 @@
# MIDI Cable
# Black - Data
# White - VCC 3.3v
# Brown - GND
import time
import board
import busio
import adafruit_midi
from adafruit_midi.control_change import ControlChange
from adafruit_midi.note_off import NoteOff
from adafruit_midi.note_on import NoteOn
def init():
print("init")
global midi
uart = busio.UART(board.TX, board.RX, baudrate=31250, timeout=0.001) # init UART
midi_in_channel = 2
midi_out_channel = 1
midi = adafruit_midi.MIDI(
midi_in=uart,
midi_out=uart,
in_channel=(midi_in_channel - 1),
out_channel=(midi_out_channel - 1),
debug=False,
)
print("Default output channel:", midi.out_channel + 1)
def trig(note_nr):
# print("trig: " + str(note_nr))
midi.send(NoteOn(note_nr, 127))
midi.send(NoteOff(note_nr, 0))
def subdiv(clk_bar, clk_bar_old, div, note_nr):
div_ns = div_to_ns(div)
clk_sub1_old = clk_bar_old % div_ns
clk_sub1 = clk_bar % div_ns
clk_sub1_diff = clk_sub1 - clk_sub1_old
if clk_sub1_diff < 0:
trig(note_nr)
def div_to_ns(div) -> int:
clk_pulse_per_second = 1000000000
beat_per_bar = 4
bar_ns = beat_per_bar / bpm * 60 * clk_pulse_per_second
return bar_ns / div
def main():
print("main")
bar_ns = div_to_ns(1)
clk_bar = time.monotonic_ns() % bar_ns
while True:
clk_bar_old = clk_bar
clk_bar = time.monotonic_ns() % bar_ns
# for i in range(1,4):
# subdiv(clk_bar, clk_bar_old, i, 36+i)
subdiv(clk_bar, clk_bar_old, 1, 36)
subdiv(clk_bar, clk_bar_old, 2, 37)
subdiv(clk_bar, clk_bar_old, 4, 38)
subdiv(clk_bar, clk_bar_old, 8, 39)
subdiv(clk_bar, clk_bar_old, 16, 40)
bpm = 80
init()
main()
Loading…
Cancel
Save