-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnrpn.h
415 lines (374 loc) · 14.3 KB
/
nrpn.h
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/* -*- mode: c++; tab-width: 8 -*- */
/*
* hasak (ham and swiss army knife) keyer for Teensy 4.X, 3.X
* Copyright (c) 2021 by Roger Critchlow, Charlestown, MA, USA
* ad5dz, rec@elf.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice, development funding notice, and this permission
* notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
//#ifdef __cplusplus
//extern "C" {
//#endif
#include "EEPROM.h"
#define KYR_NRPN_SIZE (KYR_N_NRPN*sizeof(int16_t))
#define KYR_MSG_SIZE (EEPROM_BYTES-KYR_NRPN_SIZE-6*sizeof(int16_t))
#define KYR_SAMPLE_RATE (((uint16_t)AUDIO_SAMPLE_RATE)/100)
#define KYR_EEPROM_LENGTH (EEPROM.length())
#define KYR_ID_CPU (TEENSY_CPU_ID)
typedef struct {
/* header for eeprom */
int16_t header[2];
/* saved messages */
int8_t msgs[KYR_MSG_SIZE];
/* index for reading and writing message bytes */
uint16_t index;
} hasak_t;
typedef struct {
int16_t header1[2];
int16_t nrpns[KYR_N_NRPN];
int16_t header2[2];
int8_t msgs[KYR_MSG_SIZE];
int16_t header3[2];
} hasak_eeprom_image_t;
static hasak_t hasak = {
{ (int16_t)KYR_MAGIC, KYR_NRPN_SIZE },
{ 0 },
0
};
/*
** mirror settings to NRPN_TONE into NRPN_XTONE
*/
static void nrpn_mirror_xtone(int nrpn, int _) {
xnrpn_set(NRPN_XTONE, signed_value(nrpn_get(NRPN_TONE))*1000); // Hz to Hz/1000
}
/*
** Update the keyer timing, specified by speed, weight, ratio, comp, and farnsworth,
** and produce the samples per dit, dah, ies, ils, and iws.
*/
static void nrpn_update_keyer_timing(int numb, int _) {
const int wordDits = 50;
const int sampleRate = AUDIO_SAMPLE_RATE;
const float wpm = nrpn_get(NRPN_SPEED)+nrpn_get(NRPN_SPEED_FRAC)*7.8125e-3f;
const int weight = nrpn_get(NRPN_WEIGHT);
const int ratio = nrpn_get(NRPN_RATIO);
const int compensation = signed_value(nrpn_get(NRPN_COMP));
const int farnsworth = nrpn_get(NRPN_FARNS);;
// const float ms_per_dit = (1000 * 60) / (wpm * wordDits);
const float r = (ratio-50)*0.01;
const float w = (weight-50)*0.01;
const int c = compensation;
/* samples_per_dit = (samples_per_second * second_per_minute) / (words_per_minute * dits_per_word); */
const uint32_t ticksPerBaseDit = ((sampleRate * 60.0f) / (wpm * wordDits));
const int32_t ticksPerDit = (1+r+w) * ticksPerBaseDit+c+0.5;
const int32_t ticksPerDah = (3-r+w) * ticksPerBaseDit+c+0.5;
const int32_t ticksPerIes = (1 -w) * ticksPerBaseDit-c+0.5;
int32_t ticksPerIls = (3 -w) * ticksPerBaseDit-c+0.5;
int32_t ticksPerIws = (7 -w) * ticksPerBaseDit-c+0.5;
// Serial.printf("recompute_morse settings\n");
//
// Farnsworth timing: stretch inter-character and inter-word pauses
// to reduce wpm to the specified farnsworth speed.
// formula from https://morsecode.world/international/timing.html
//
if (farnsworth > 5 && farnsworth < wpm) {
float f = 50.0/19.0 * wpm / farnsworth - 31.0/19.0;
ticksPerIls *= f; // stretched inter-character pause
ticksPerIws *= f; // stretched inter-word pause
}
// store the values
/* Serial.printf("morse_keyer base dit %ld, dit %ld, dah %ld, ies %ld, ils %ld, iws %ld\n",
ticksPerBaseDit, ticksPerDit, ticksPerDah, ticksPerIes, ticksPerIls, ticksPerIws); */
// AudioNoInterrupts();
xnrpn_set(NRPN_XPER_DIT, ticksPerDit);
xnrpn_set(NRPN_XPER_DAH, ticksPerDah);
xnrpn_set(NRPN_XPER_IES, ticksPerIes);
xnrpn_set(NRPN_XPER_ILS, ticksPerIls);
xnrpn_set(NRPN_XPER_IWS, ticksPerIws);
// AudioInterrupts();
/* Serial.printf("morse_keyer dit %ld, dah %ld, ies %ld, ils %ld, iws %ld\n",
get_vox_xnrpn(vox, NRPN_XPER_DIT), get_vox_xnrpn(vox, NRPN_XPER_DAH),
get_vox_xnrpn(vox, NRPN_XPER_IES), get_vox_xnrpn(vox, NRPN_XPER_ILS),
get_vox_xnrpn(vox, NRPN_XPER_IWS)); */
}
/*
** trigger a morse timing update when any of the required values changes.
*/
static void nrpn_recompute_morse(int nrpn, int _) {
// Serial.printf("recompute_morse setting handler\n");
after_idle(nrpn_update_keyer_timing);
}
/*
** EEPROM functions. Disabled.
*/
static void nrpn_write_eeprom(int nrpn, int _) {
#if 0
hasak_eeprom_image_t data;
int error = 0;
for (int i = 0; i < 2; i += 1)
data.header1[i] = data.header2[i] = data.header3[i] = hasak.header[i];
for (int i = 0; i < NRPN_LAST; i += 1)
data.nrpns[i] = nrpn_get(i);
for (unsigned i = 0; i < sizeof(data.msgs); i += 1)
data.msgs[i] = hasak.msgs[i];
EEPROM.put(0, data);
/* verify */
for (int i = 0; i < NRPN_LAST; i += 1) {
int16_t value;
EEPROM.get(sizeof(data.header1)+i*sizeof(int16_t), value);
if (value != data.nrpn[i]) {
Serial.printf("nrpn_write_eeprom hasak.nrpn[%d] == %d != %d\n", nrpn, value, hasak.nrpn[nrpn]);
++error;
}
}
for (unsigned i = 0; i < sizeof(hasak.msgs); i += 1) {
int8_t value;
EEPROM.get(sizeof(data.header1)+sizeof(data.nrpn)+sizeof(data.header2)+i, value);
if (value != data.msgs[i]) {
Serial.printf("nrpn_write_eeprom hasak.msgs[%d] == %d != %d\n", i, value, hasak.msgs[i]);
}
}
nrpn_send(nrpn, error ? 0 : 1);
#endif
}
static void nrpn_read_eeprom(int nrpn, int _) {
#if 0
hasak_eeprom_image_t data;
EEPROM.get(0, data);
for (int i = 0; i < 2; i += 1;)
if (data.header1[i] != data.header2[i] ||
data.header2[i] != data.header3[i] ||
data.header3[i] != hasak.header[i]) {
/* signal failure */
nrpn_send(nrpn, 0);
return;
}
/* apply nrpns */
for (int n = 0; n < NRPN_LAST; n += 1)
nrpn_set(n, data.nrpns[n]);
/* copy msgs */
for (int i = 0; i < sizeof(data.msgs); i += 1)
hasak.msgs[i] = data.msgs[i];
/* signal success */
nrpn_send(nrpn, 1);
#endif
}
/*
** echo all nrpn values to usb, slowly
*/
static int nrpn_echo_index;
static void nrpn_echo_all_step(int _, int __) {
if (nrpn_echo_index < NRPN_LAST_SAVED) {
if (nrpn_get(nrpn_echo_index) != VAL_NOT_SET)
nrpn_send(nrpn_echo_index, nrpn_get(nrpn_echo_index));
nrpn_echo_index += 1;
after_idle(nrpn_echo_all_step);
} else {
nrpn_send(NRPN_ECHO_ALL, 0);
nrpn_echo_index = -1;
}
}
static void nrpn_echo_all(int nrpn, int _) {
nrpn_echo_index = NRPN_SAVED;
after_idle(nrpn_echo_all_step);
}
/*
** feed the text keyers
*/
static void nrpn_send_text(int nrpn, int _) {
const int value = nrpn_get(nrpn);
if (nrpn == NRPN_SEND_TEXT) {
note_set(NOTE_TXT_TEXT, value&127);
// keyer_text.send_text(value&127);
} else if (nrpn == NRPN_SEND_TEXT2) {
note_set(NOTE_TXT_TEXT2, value&127);
// keyer_text2.send_text(value&127);
} else {
Serial.printf("unexpected nrpn %d in nrpn_send_text\n", nrpn);
}
nrpn_send(nrpn, value&127);
}
/*
** maintain the message buffers
*/
static void nrpn_msg_handler(int nrpn, int _) {
const int value = nrpn_get(nrpn);
if (nrpn == NRPN_MSG_INDEX) {
hasak.index = value%sizeof(hasak.msgs);
nrpn_send(nrpn, hasak.index);
} else if (nrpn == NRPN_MSG_WRITE) {
nrpn_send(nrpn, value&127);
hasak.msgs[hasak.index++] = value&127;
hasak.index %= sizeof(hasak.msgs);
} else if (nrpn == NRPN_MSG_READ) {
nrpn_send(nrpn, hasak.msgs[hasak.index++]);
hasak.index %= sizeof(hasak.msgs);
} else {
Serial.printf("unexpected nrpn %d in nrpn_msg_handler\n", nrpn);
}
}
static void nrpn_nrpn_query(int nrpn, int _) {
const int query = nrpn_get(nrpn);
if (nrpn_is_valid(query))
nrpn_send(query, nrpn_get(query));
}
/*
** this where we initialize the keyer nrpns
*/
static void nrpn_set_default(void) {
nrpn_set(NRPN_INPUT_ENABLE, 0); // disable input
nrpn_set(NRPN_OUTPUT_ENABLE, 0); // disable output
nrpn_set(NRPN_CHANNEL, KYR_CHANNEL);
// nrpn_set(NRPN_INPUT_ENABLE, 1); moved to end
// nrpn_set(NRPN_OUTPUT_ENABLE, 1); moved to end
nrpn_set(NRPN_ECHO_ENABLE, 1);
nrpn_set(NRPN_LISTENER_ENABLE, 1);
nrpn_set(NRPN_STRING_THROTTLE, 3750);
nrpn_set(NRPN_PIN_ENABLE, 1);
nrpn_set(NRPN_POUT_ENABLE, 1);
nrpn_set(NRPN_PADC_ENABLE, 0);
nrpn_set(NRPN_ST_ENABLE, KYR_ENABLE_ST);
nrpn_set(NRPN_TX_ENABLE, KYR_ENABLE_TX);
nrpn_set(NRPN_IQ_ENABLE, 0);
nrpn_set(NRPN_PTT_REQUIRE, 0);
nrpn_set(NRPN_RKEY_ENABLE, 1);
nrpn_set(NRPN_CW_AUTOPTT, 1);
nrpn_set(NRPN_RX_MUTE, 0);
nrpn_set(NRPN_MIC_HWPTT, 0);
nrpn_set(NRPN_CW_HWPTT, 1);
nrpn_set(NRPN_HDW_IN_ENABLE, 0);
nrpn_set(NRPN_HDW_OUT_ENABLE, 1);
nrpn_set(NRPN_VOLUME, 0);
nrpn_set(NRPN_LEVEL, 0); /* dB/10, centiBel */
nrpn_set(NRPN_USB_LEVEL, 0);
nrpn_set(NRPN_I2S_LEVEL, 0);
nrpn_set(NRPN_HDW_LEVEL, 0);
nrpn_set(NRPN_ST_BALANCE, 0);
nrpn_set(NRPN_RX_PTT_LEVEL, 0);
/* keyer defaults */
nrpn_set(NRPN_TONE, 600); /* Hz */
nrpn_set(NRPN_SPEED, 18); /* wpm */
nrpn_set(NRPN_SPEED_FRAC, 0); /* wpm 1/128ths */
nrpn_set(NRPN_WEIGHT, 50); /* percent */
nrpn_set(NRPN_RATIO, 50); /* percent */
nrpn_set(NRPN_COMP,ms_to_samples(0)); /* 0 samples */
nrpn_set(NRPN_FARNS, 0); /* wpm */
nrpn_set(NRPN_HEAD_TIME, ms_to_samples(0)); // 0 ms
nrpn_set(NRPN_TAIL_TIME, ms_to_samples(0)); // 0 ms
nrpn_set(NRPN_HANG_TIME, 0); /* 0 dits */
nrpn_set(NRPN_RISE_TIME, ms_to_samples(5)); // 5.0 ms
nrpn_set(NRPN_FALL_TIME, ms_to_samples(5)); // 5.0 ms
nrpn_set(NRPN_RISE_RAMP, VAL_RAMP_HANN);
nrpn_set(NRPN_FALL_RAMP, VAL_RAMP_HANN);
nrpn_set(NRPN_PAD_MODE, VAL_MODE_A);
nrpn_set(NRPN_PAD_SWAP, 0);
nrpn_set(NRPN_PAD_ADAPT, VAL_ADAPT_NORMAL);
nrpn_set(NRPN_AUTO_ILS, 1);
nrpn_set(NRPN_AUTO_IWS, 0);
nrpn_set(NRPN_PAD_KEYER, VAL_KEYER_VK6PH);
// nrpn_set(NRPN_PAD_KEYER, VAL_KEYER_AD5DZ);
// nrpn_set(NRPN_PAD_KEYER, VAL_KEYER_ND7PA);
// nrpn_set(NRPN_PAD_KEYER, VAL_KEYER_K1EL);
nrpn_set(NRPN_MIXER_SLEW_RAMP, VAL_RAMP_HANN);
nrpn_set(NRPN_MIXER_SLEW_TIME, 128);
nrpn_set(NRPN_FREQ_SLEW_RAMP, VAL_RAMP_HANN);
nrpn_set(NRPN_FREQ_SLEW_TIME, 128);
nrpn_set(NRPN_PIN_DEBOUNCE, 1000);
nrpn_set(NRPN_POUT_LOGIC, 1);
nrpn_set(NRPN_PADC_RATE, 32);
nrpn_set(NRPN_PIN0_PIN, KYR_L_PAD_PIN); // left paddle pin
nrpn_set(NRPN_PIN1_PIN, KYR_R_PAD_PIN); // right paddle pin
nrpn_set(NRPN_PIN2_PIN, KYR_S_KEY_PIN); // straight key pin
nrpn_set(NRPN_PIN3_PIN, KYR_EXT_PTT_PIN); // external ptt switch pin
nrpn_set(NRPN_PIN4_PIN, 127);
nrpn_set(NRPN_PIN5_PIN, 127);
nrpn_set(NRPN_PIN6_PIN, 127);
nrpn_set(NRPN_PIN7_PIN, 127);
nrpn_set(NRPN_POUT0_PIN, KYR_PTT_OUT_PIN); // ptt out
nrpn_set(NRPN_POUT1_PIN, KYR_KEY_OUT_PIN); // key out
nrpn_set(NRPN_POUT2_PIN, 127);
nrpn_set(NRPN_POUT3_PIN, 127);
nrpn_set(NRPN_POUT4_PIN, 127);
nrpn_set(NRPN_POUT5_PIN, 127);
nrpn_set(NRPN_POUT6_PIN, 127);
nrpn_set(NRPN_POUT7_PIN, 127);
nrpn_set(NRPN_PADC0_PIN, KYR_VOLUME_POT);
nrpn_set(NRPN_PADC0_NRPN, VAL_PADC_VOLUME);
nrpn_set(NRPN_PADC1_PIN, KYR_ST_VOL_POT);
nrpn_set(NRPN_PADC1_NRPN, VAL_PADC_LEVEL);
nrpn_set(NRPN_PADC2_PIN, KYR_ST_FREQ_POT);
nrpn_set(NRPN_PADC2_NRPN, VAL_PADC_TONE);
nrpn_set(NRPN_PADC3_PIN, KYR_SPEED_POT);
nrpn_set(NRPN_PADC3_NRPN, VAL_PADC_SPEED);
nrpn_set(NRPN_PADC4_PIN, 127);
nrpn_set(NRPN_PADC4_NRPN, -1);
nrpn_set(NRPN_PADC5_PIN, 1);
nrpn_set(NRPN_PADC5_NRPN, -1);
nrpn_set(NRPN_PADC6_PIN, 127);
nrpn_set(NRPN_PADC6_NRPN, -1);
nrpn_set(NRPN_PADC7_PIN, 127);
nrpn_set(NRPN_PADC7_NRPN, -1);
nrpn_set(NRPN_ACTIVE_ST, NOTE_ST_NONE);
/* output mixer level */
for (int i = NRPN_MIXER; i < NRPN_MIXER+24; i += 1) nrpn_set(i, 0); /* 0 dB */
/* output mixer enable */
for (int i = NRPN_MIXER2; i < NRPN_MIXER2+24; i += 1) nrpn_set(i, 0); /* muted */
nrpn_set(NRPN_MIX_ENABLE, 0b001011001100); // enables left and right mixers according to 12 bits: IQ to usb, usb+sidetone to i2s and hdw
//nrpn_set(NRPN_MIX_ENABLE_L, 0b001011001100); // enables left mixers according to 12 bits.
//nrpn_set(NRPN_MIX_ENABLE_R, 0b001011001100); // enables right mixers according to 12 bits.
/* codec */
nrpn_set(NRPN_VOLUME, 0);
nrpn_set(NRPN_INPUT_SELECT, 0);
nrpn_set(NRPN_INPUT_LEVEL, 0);
nrpn_set(NRPN_OUTPUT_ENABLE, 1); // enable output
nrpn_set(NRPN_INPUT_ENABLE, 1); // enable input
// Serial.printf("nrpn_set_default ID_DEVICE %d ID_VERSION %d\n", nrpn_get(NRPN_ID_DEVICE), nrpn_get(NRPN_ID_VERSION));
}
static void nrpn_set_default_shim(int nrpn, int _) {
nrpn_set_default();
}
static void nrpn_setup(void) {
/* sidetone, mirror TONE to XTONE */
nrpn_listen(NRPN_TONE, nrpn_mirror_xtone);
/* morse timing update, listen for changes */
nrpn_listen(NRPN_SPEED, nrpn_recompute_morse);
nrpn_listen(NRPN_SPEED_FRAC, nrpn_recompute_morse);
nrpn_listen(NRPN_WEIGHT, nrpn_recompute_morse);
nrpn_listen(NRPN_RATIO, nrpn_recompute_morse);
nrpn_listen(NRPN_COMP, nrpn_recompute_morse);
nrpn_listen(NRPN_FARNS, nrpn_recompute_morse);
/* commands */
nrpn_listen(NRPN_NRPN_QUERY, nrpn_nrpn_query);
nrpn_listen(NRPN_WRITE_EEPROM, nrpn_write_eeprom);
nrpn_listen(NRPN_READ_EEPROM, nrpn_read_eeprom);
nrpn_listen(NRPN_SET_DEFAULT, nrpn_set_default_shim);
nrpn_listen(NRPN_ECHO_ALL, nrpn_echo_all);
nrpn_listen(NRPN_SEND_TEXT, nrpn_send_text);
nrpn_listen(NRPN_SEND_TEXT2, nrpn_send_text);
nrpn_listen(NRPN_MSG_INDEX, nrpn_msg_handler);
nrpn_listen(NRPN_MSG_WRITE, nrpn_msg_handler);
nrpn_listen(NRPN_MSG_READ, nrpn_msg_handler);
/* information set in the nrpn array so it will be echoed */
nrpn_set(NRPN_ID_DEVICE, KYR_IDENT);
nrpn_set(NRPN_ID_VERSION, KYR_VERSION);
nrpn_set(NRPN_NRPN_SIZE, KYR_NRPN_SIZE); // obsolete, in the JSON
nrpn_set(NRPN_MSG_SIZE, KYR_MSG_SIZE); // obsolete, in the JSON
nrpn_set(NRPN_SAMPLE_RATE, KYR_SAMPLE_RATE); // obsolete, in the JSON
nrpn_set(NRPN_EEPROM_LENGTH, KYR_EEPROM_LENGTH);; // obsolete, in the JSON
nrpn_set(NRPN_ID_CPU, KYR_ID_CPU);
}