proper CLI

This commit is contained in:
Thomas Lindner 2024-12-23 15:17:26 +01:00
parent b952810e7a
commit b172dacd6f
4 changed files with 103 additions and 68 deletions

10
.editorconfig Normal file
View file

@ -0,0 +1,10 @@
root = true
[*]
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
charset = utf-8
indent_style = space
indent_size = 4
max_line_length = 88

2
.flake8 Normal file
View file

@ -0,0 +1,2 @@
[flake8]
max_line_length = 88

53
freude.py Normal file → Executable file
View file

@ -1,25 +1,48 @@
#!/usr/bin/env python
import argparse
import csv
import sys
parser = argparse.ArgumentParser()
parser.add_argument("input", nargs="+", help="input CSV (pitch, duration, pause)")
parser.add_argument("-o", "--output", default="/dev/stdout", help="output file (gcode)")
parser.add_argument(
"--steps-per-mm",
default=5,
type=float,
help="motor steps per mm (actually an arbitrary scaling factor)",
)
parser.add_argument("--bpm", default=60, type=float, help="beats per minute")
args = parser.parse_args()
# actually an arbitrary scaling factor
steps_per_mm = 5
# starting from C3 = 130.81 Hz
frequencies = [2 ** (i / 12) * 130.81 for i in range(24)]
bpm = 60
# duration of 1/8th
base_duration = 60 / bpm / 8
# duration of 1/16th beat
base_duration = 60 / args.bpm / 16
with open("FREUDE.gcode", "w") as f:
with open(args.output, "w") as f:
f.write("G21 ; set units to millimeters\n")
f.write("G90 ; use absolute coordinates\n")
f.write("G0 Z10 F300\n")
f.write("G0 X0 Y0 F3000\n")
f.write("G4 S2\n")
f.write("G4 S2 ; wait 2 seconds\n")
position = 0
with open("freude.txt", "r") as notes:
for note, duration, pause in csv.reader(notes):
feedrate = int(frequencies[int(note) + 12] * 60 / steps_per_mm)
length = int(duration) * base_duration / 60 * feedrate
position += -length if position > 100 else length
f.write(f"G0 X{position:.6f} Y{position:.6f} F{feedrate}\n")
f.write(f"G4 P{int(int(pause) * base_duration)}\n")
f.write("G4 S2\n")
for filename in args.input:
with open(filename, "r") as notes:
for pitch, duration, pause in csv.reader(notes):
pitch = int(pitch)
if pitch < -12 or pitch > 11:
sys.exit("pitch goes from -12 == C3 to 11 == B4")
duration = int(duration)
pause = int(pause)
if duration < 1 or pause < 0:
sys.exit("duration/pause are integer multiples of 1/16th beat")
feedrate = int(frequencies[pitch + 12] * 60 / args.steps_per_mm)
length = duration * base_duration / 60 * feedrate
position += -length if position > 100 else length
if position < 0 or position > 200:
sys.exit("does not fit in 200x200")
pause_ms = int(pause * base_duration)
f.write(f"G0 X{position:.6f} Y{position:.6f} F{feedrate}\n")
f.write(f"G4 P{pause_ms}\n")
f.write("G4 S2 ; wait 2 seconds\n")

View file

@ -1,62 +1,62 @@
4,2,0
4,2,0
5,2,0
7,2,0
7,2,0
5,2,0
4,2,0
4,4,0
4,4,0
5,4,0
7,4,0
7,4,0
5,4,0
4,4,0
2,4,0
0,4,0
0,4,0
2,4,0
4,4,0
4,6,0
2,2,0
2,8,0
4,4,0
4,4,0
5,4,0
7,4,0
7,4,0
5,4,0
4,4,0
2,4,0
0,4,0
0,4,0
2,4,0
4,4,0
2,6,0
0,2,0
0,2,0
2,2,0
4,2,0
4,3,0
2,1,0
0,8,0
2,4,0
2,4,0
4,4,0
0,4,0
2,4,0
4,2,0
4,2,0
5,2,0
7,2,0
7,2,0
5,2,0
4,2,0
2,2,0
0,2,0
0,2,0
2,2,0
4,2,0
2,3,0
0,1,0
4,4,0
0,4,0
2,2,0
2,2,0
4,2,0
0,2,0
2,2,0
4,1,0
5,1,0
4,2,0
0,2,0
2,2,0
4,1,0
5,1,0
4,2,0
2,2,0
0,2,0
2,2,0
-5,4,0
4,2,0
2,4,0
4,2,0
5,2,0
7,2,0
7,2,0
5,2,0
4,2,0
2,2,0
0,2,0
0,2,0
2,2,0
4,2,0
2,3,0
0,1,0
4,4,0
2,4,0
0,4,0
2,4,0
-5,8,0
4,4,0
4,4,0
5,4,0
7,4,0
7,4,0
5,4,0
4,4,0
2,4,0
0,4,0
0,4,0
2,4,0
4,4,0
2,6,0
0,2,0
0,8,0