-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurses_grid.py
206 lines (178 loc) · 7.48 KB
/
curses_grid.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import curses
import random
import os
import json
from typing import Any
# Constants
BANKS_FILE_PATH = os.path.join(
os.path.expanduser("~"),
".local", "share", "FD5",
"banks.json")
def save_banks(bank_location: str):
"""
Save the current bank as JSON in the BANKS_FILE_PATH
folder.
"""
with open(BANKS_FILE_PATH, "w") as f:
json.dump(banks, f)
def load_banks(bank_location: str):
"""
Load the bank from the BANKS_FILE_PATH file, typically
found in (./local/share/DE5). If no file is found, it
will return a default bank.
"""
if os.path.exists(BANKS_FILE_PATH):
with open(BANKS_FILE_PATH, "r") as f:
try:
return json.load(f)
except json.JSONDecodeError as e:
print(f"Error loading banks from {BANKS_FILE_PATH}: {e}")
else:
os.makedirs(os.path.dirname(BANKS_FILE_PATH), exist_ok=True)
return {
"default": {
"params": [f"P{index:02}" for index in range(64)],
"values": {param: 0 for param in [f"P{index:02}" for index in range(64)]},
"channels": {param: 0 for param in [f"P{index:02}" for index in range(64)]},
"control_numbers": {param: 0 for param in [f"P{index:02}" for index in range(64)]}
}
}
def get_param_value(stdscr, y, x, default_value):
"""
Get a parameter value and update the screen accordingly.
"""
stdscr.move(y, x)
curses.echo()
value = stdscr.getstr(y, x, 3).decode('utf-8')
curses.noecho()
return value or default_value
def clamp(value: Any, min_value: Any, max_value: Any):
"""
Clamp a value between a minimum and a maximum
"""
return max(min(value, max_value), min_value)
def on_parameter_change(param_name, channel, control_number, value):
print(f"Parameter {param_name} (Channel: {channel}, Control Number: {control_number}) changed to {value}")
def main(stdscr):
"""
Main event loop for the Curses application. Draws the screen, update information.
The mechanism will also be in charge of auto-saving application state every now
and then.
"""
global banks, current_bank
application_loop: int = 0
# Load banks from file
current_bank = "default"
banks = load_banks(bank_location=BANKS_FILE_PATH)
# Set up the screen
curses.curs_set(2)
stdscr.nodelay(0)
stdscr.timeout(-1)
curses.start_color()
curses.init_pair(1, curses.COLOR_YELLOW, curses.COLOR_BLUE)
# Initialize the cursor position
cursor_y, cursor_x = 0, 0
while True:
stdscr.clear()
# Show current bank
stdscr.attron(curses.color_pair(1))
stdscr.attron(curses.A_BOLD)
stdscr.addstr(0, 0, f"BANK: {current_bank}")
stdscr.attroff(curses.A_BOLD)
stdscr.attroff(curses.color_pair(1))
# Draw the 8x8 grid
params = banks[current_bank]["params"]
param_values = banks[current_bank]["values"]
for y in range(8):
for x in range(8):
index = y * 8 + x
param = params[index]
stdscr.addstr(y + 1, x * 4, param)
# Draw the cursor
cursor_index = cursor_y * 8 + cursor_x
cursor_param = params[cursor_index]
cursor_value = f"{param_values[cursor_param]: >3}"
stdscr.addstr(cursor_y + 1, cursor_x * 4, cursor_value, curses.A_REVERSE)
# Show channel and control number at the bottom
cursor_channel = banks[current_bank]["channels"][cursor_param]
cursor_control_number = banks[current_bank]["control_numbers"][cursor_param]
stdscr.addstr(10, 0, f"Channel: {cursor_channel} Control Number: {cursor_control_number}")
# Capture user input
key = stdscr.getch()
# Update cursor position based on arrow keys
if key == curses.KEY_UP and cursor_y > 0:
cursor_y -= 1
elif key == curses.KEY_DOWN and cursor_y < 7:
cursor_y += 1
elif key == curses.KEY_LEFT and cursor_x > 0:
cursor_x -= 1
elif key == curses.KEY_RIGHT and cursor_x < 7:
cursor_x += 1
elif key == ord('v'): # Change value
curses.curs_set(0)
stdscr.nodelay(0)
new_value = get_param_value(stdscr, cursor_y + 1, cursor_x * 4, param_values[cursor_param])[:3]
new_value = clamp(int(new_value), 0, 127)
param_values[cursor_param] = new_value
on_parameter_change(cursor_param, cursor_channel, cursor_control_number, new_value)
stdscr.nodelay(1)
curses.curs_set(2)
elif key == ord('r'): # Rename parameter
curses.curs_set(0)
stdscr.nodelay(0)
new_name = get_param_value(stdscr, cursor_y + 1, cursor_x * 4, cursor_param)[:3].upper()
if len(new_name) == 3 and new_name.isalpha():
old_index = params.index(cursor_param)
params[old_index] = new_name
param_values[new_name] = param_values.pop(cursor_param)
banks[current_bank]["channels"][new_name] = banks[current_bank]["channels"].pop(cursor_param)
banks[current_bank]["control_numbers"][new_name] = banks[current_bank]["control_numbers"].pop(cursor_param)
cursor_param = new_name
stdscr.nodelay(1)
curses.curs_set(2)
elif key == ord('n'): # Enter control number
curses.curs_set(0)
stdscr.nodelay(0)
new_control_number = get_param_value(stdscr, 10, 16, str(cursor_control_number))
new_control_number = clamp(int(new_control_number), 0, 127)
banks[current_bank]["control_numbers"][cursor_param] = new_control_number
stdscr.nodelay(1)
curses.curs_set(2)
elif key == ord('c'): # Enter channel
curses.curs_set(0)
stdscr.nodelay(0)
new_channel = get_param_value(stdscr, 10, 9, str(cursor_channel))
new_channel = clamp(int(new_channel), 0, 15)
banks[current_bank]["channels"][cursor_param] = new_channel
stdscr.nodelay(1)
curses.curs_set(2)
elif key == ord('q'): # Quit
stdscr.addstr("\nDo you want to quit? (y/n): ")
if stdscr.getch() == ord('y'):
exit()
elif key == ord('b'): # Switch bank
# Get the new bank name from the user
curses.curs_set(0)
stdscr.nodelay(0)
stdscr.addstr(0, 5, " " * 10)
new_bank_name = get_param_value(stdscr, 0, 6, "").upper()
if new_bank_name.isalpha():
if new_bank_name not in banks:
# Initialize a new bank if it doesn't exist
banks[new_bank_name] = {
"params": [f"P{index:02}" for index in range(64)],
"values": {param: 0 for param in [f"P{index:02}" for index in range(64)]},
"channels": {param: 0 for param in [f"P{index:02}" for index in range(64)]},
"control_numbers": {param: 0 for param in [f"P{index:02}" for index in range(64)]}
}
current_bank = new_bank_name
stdscr.nodelay(1)
curses.curs_set(2)
# Save every few cycles around
if application_loop % 5 == 0:
save_banks(BANKS_FILE_PATH)
# Refresh and increment the rate
stdscr.refresh()
application_loop += 1
if __name__ == "__main__":
curses.wrapper(main)