ソースを参照

fix some ui bugs

Thomas Buck 3週間前
コミット
ff21b7393f
7個のファイルの変更123行の追加87行の削除
  1. 4
    0
      include/config.h
  2. 3
    3
      include/sequence.h
  3. 2
    1
      src/buttons.c
  4. 3
    2
      src/led.c
  5. 3
    2
      src/pulse.c
  6. 36
    29
      src/sequence.c
  7. 72
    50
      src/ui.c

+ 4
- 0
include/config.h ファイルの表示

@@ -31,6 +31,10 @@
31 31
 //#define DISABLE_CDC_DTR_CHECK
32 32
 #define DEBOUNCE_DELAY_MS 5
33 33
 
34
+#define BAT_FETCH_MS 66
35
+
36
+#define MAX_BPM 360
37
+
34 38
 // ----------------------------------------------------------------------------
35 39
 
36 40
 #define STR_HELPER(x) #x

+ 3
- 3
include/sequence.h ファイルの表示

@@ -44,15 +44,15 @@ void sequence_init(void);
44 44
 void sequence_set_bpm(uint32_t new_bpm);
45 45
 uint32_t sequence_get_bpm(void);
46 46
 
47
-void sequence_set_ms(uint32_t new_ms);
48
-uint32_t sequence_get_ms(void);
49
-
50 47
 void sequence_set_beats(uint32_t new_beats);
51 48
 uint32_t sequence_get_beats(void);
52 49
 
53 50
 void sequence_set_bank(uint32_t new_bank);
54 51
 uint32_t sequence_get_bank(void);
55 52
 
53
+uint32_t sequence_get_max_banks(void);
54
+uint64_t sequence_get_us(void);
55
+
56 56
 void sequence_handle_button_loopstation(enum buttons btn, bool rec);
57 57
 void sequence_handle_button_drummachine(enum buttons btn);
58 58
 

+ 2
- 1
src/buttons.c ファイルの表示

@@ -20,6 +20,7 @@
20 20
 #include "pico/stdlib.h"
21 21
 
22 22
 #include "config.h"
23
+#include "log.h"
23 24
 #include "main.h"
24 25
 #include "buttons.h"
25 26
 
@@ -128,7 +129,7 @@ static void button_run_single(bool state, uint i) {
128 129
 
129 130
     if ((now - buttons[i].last_time) > DEBOUNCE_DELAY_MS) {
130 131
         if (state != buttons[i].current_state) {
131
-            printf("btn %d now %s\n", i, state ? "pressed" : "released");
132
+            //debug("btn %d now %s", i, state ? "pressed" : "released");
132 133
 
133 134
             buttons[i].current_state = state;
134 135
             if (callback) {

+ 3
- 2
src/led.c ファイルの表示

@@ -21,6 +21,7 @@
21 21
 #include "pico/stdlib.h"
22 22
 
23 23
 #include "sequence.h"
24
+#include "log.h"
24 25
 #include "led.h"
25 26
 
26 27
 #if 0
@@ -51,11 +52,11 @@ void led_init(void) {
51 52
 void led_set(uint32_t i, bool v) {
52 53
     i %= LED_COUNT;
53 54
     gpio_put(led_gpio_num[i], v);
54
-    //printf("led %"PRIu32" now %d\n", i, v);
55
+    //debug("led %"PRIu32" now %d", i, v);
55 56
 }
56 57
 
57 58
 void ch_set(uint32_t i, bool v) {
58 59
     i %= NUM_CHANNELS;
59 60
     gpio_put(ch_gpio_num[i], v);
60
-    //printf("ch %"PRIu32" now %d\n", i, v);
61
+    //debug("ch %"PRIu32" now %d", i, v);
61 62
 }

+ 3
- 2
src/pulse.c ファイルの表示

@@ -21,6 +21,7 @@
21 21
 #include "pico/stdlib.h"
22 22
 
23 23
 #include "led.h"
24
+#include "log.h"
24 25
 #include "sequence.h"
25 26
 #include "pulse.h"
26 27
 
@@ -35,14 +36,14 @@ static void pulse_trigger(uint32_t i, uint32_t t_ms, bool out) {
35 36
         if (out_time[i % NUM_CHANNELS] == 0) {
36 37
             out_time[i % NUM_CHANNELS] = off_t;
37 38
         } else {
38
-            printf("%s: skip retrigger out %"PRIu32"\n", __func__, i);
39
+            debug("skip retrigger out %"PRIu32, i);
39 40
         }
40 41
     } else {
41 42
         led_set(i, true);
42 43
         if (led_time[i % LED_COUNT] == 0) {
43 44
             led_time[i % LED_COUNT] = off_t;
44 45
         } else {
45
-            printf("%s: skip retrigger led %"PRIu32"\n", __func__, i);
46
+            debug("skip retrigger led %"PRIu32, i);
46 47
         }
47 48
     }
48 49
 }

+ 36
- 29
src/sequence.c ファイルの表示

@@ -21,54 +21,58 @@
21 21
 #include "pico/stdlib.h"
22 22
 
23 23
 #include "led.h"
24
+#include "log.h"
24 25
 #include "pulse.h"
25 26
 #include "ui.h"
26 27
 #include "sequence.h"
27 28
 
28 29
 static const uint32_t channel_times[NUM_CHANNELS] = CH_GPIO_TIMINGS;
29 30
 
30
-static uint32_t ms_per_beat = 0;
31
+static uint64_t us_per_beat = 0;
32
+static uint64_t last_t = 0;
33
+static uint64_t beattimer_start = 0;
34
+
31 35
 static uint32_t beats = MAX_BEATS;
32
-static uint32_t last_t = 0;
33 36
 static uint32_t last_i = 0;
34 37
 static uint32_t bank = 0;
35
-static uint32_t beattimer_start = 0;
38
+static uint32_t max_banks_currently = 0;
36 39
 
37 40
 static enum channels sequence[MAX_BEATS] = {0};
38 41
 
39 42
 void sequence_init(void) {
40
-    ms_per_beat = 0;
43
+    us_per_beat = 0;
41 44
     beats = MAX_BEATS;
42 45
     last_t = to_ms_since_boot(get_absolute_time());
43 46
     last_i = 0;
47
+    max_banks_currently = (beats + (NUM_BTNS - 1)) / NUM_BTNS;
44 48
 
45 49
     for (uint i = 0; i < MAX_BEATS; i++) {
46 50
         sequence[i] = 0;
47 51
     }
48 52
 }
49 53
 
50
-void sequence_set_bpm(uint32_t new_bpm) {
51
-    ms_per_beat = 60000 / new_bpm / beats;
52
-    printf("bpm now %"PRIu32" = %"PRIu32"ms\n", new_bpm, ms_per_beat);
53
-}
54
-
55
-uint32_t sequence_get_bpm(void) {
56
-    return 60000 / (ms_per_beat * beats);
54
+uint32_t sequence_get_max_banks(void) {
55
+    return max_banks_currently;
57 56
 }
58 57
 
59
-void sequence_set_ms(uint32_t new_ms) {
60
-    ms_per_beat = new_ms;
58
+void sequence_set_bpm(uint32_t new_bpm) {
59
+    us_per_beat = (60UL * 1000UL * 1000UL) / new_bpm;
60
+    debug("bpm @ %"PRIu64"us = %"PRIu64"us", us_per_beat, us_per_beat / beats);
61 61
 }
62 62
 
63
-uint32_t sequence_get_ms(void) {
64
-    return ms_per_beat;
63
+uint32_t sequence_get_bpm(void) {
64
+    if (us_per_beat == 0) {
65
+        return 0;
66
+    }
67
+    return (60UL * 1000UL * 1000UL) / us_per_beat;
65 68
 }
66 69
 
67 70
 void sequence_set_beats(uint32_t new_beats) {
68 71
     beats = (new_beats <= MAX_BEATS) ? new_beats : MAX_BEATS;
72
+    debug("beats @ %"PRIu64"us = %"PRIu64"us", us_per_beat, us_per_beat / beats);
69 73
 
70
-    uint32_t max_banks_currently = (beats + (NUM_BTNS - 1)) / NUM_BTNS;
71
-    bank = (bank < max_banks_currently) ? bank : max_banks_currently;
74
+    max_banks_currently = (beats + (NUM_BTNS - 1)) / NUM_BTNS;
75
+    bank = (bank < max_banks_currently) ? bank : 0;
72 76
 }
73 77
 
74 78
 uint32_t sequence_get_beats(void) {
@@ -77,15 +81,17 @@ uint32_t sequence_get_beats(void) {
77 81
 
78 82
 void sequence_set_bank(uint32_t new_bank) {
79 83
     uint32_t b = (new_bank < MAX_BANKS) ? new_bank : MAX_BANKS;
80
-
81
-    uint32_t max_banks_currently = (beats + (NUM_BTNS - 1)) / NUM_BTNS;
82
-    bank = (b < max_banks_currently) ? b : max_banks_currently;
84
+    bank = (b < max_banks_currently) ? b : 0;
83 85
 }
84 86
 
85 87
 uint32_t sequence_get_bank(void) {
86 88
     return bank;
87 89
 }
88 90
 
91
+uint64_t sequence_get_us(void) {
92
+    return us_per_beat;
93
+}
94
+
89 95
 static void sequence_set(uint32_t beat, enum channels ch, bool value) {
90 96
     if (beat < MAX_BEATS) {
91 97
         if (value) {
@@ -194,22 +200,23 @@ void sequence_handle_button_drummachine(enum buttons btn) {
194 200
 
195 201
 void sequence_looptime(bool fin) {
196 202
     if (!fin) {
197
-        beattimer_start = to_ms_since_boot(get_absolute_time());
203
+        beattimer_start = to_us_since_boot(get_absolute_time());
198 204
     } else {
199
-        if (ms_per_beat == 0) {
200
-            uint32_t now = to_ms_since_boot(get_absolute_time());
201
-            uint32_t diff = now - beattimer_start;
202
-            ms_per_beat = diff / beats;
203
-            printf("looptime: diff=%"PRIu32"ms per_beat=%"PRIu32"ms\n", diff, ms_per_beat);
205
+        if (us_per_beat == 0) {
206
+            uint64_t now = to_us_since_boot(get_absolute_time());
207
+            uint64_t diff = now - beattimer_start;
208
+            us_per_beat = diff;
209
+            debug("looptime @ %"PRIu64"us = %"PRIu64"us", us_per_beat, us_per_beat / beats);
204 210
         }
205 211
     }
206 212
 }
207 213
 
208 214
 void sequence_run(void) {
209
-    uint32_t now = to_ms_since_boot(get_absolute_time());
215
+    uint64_t now = to_us_since_boot(get_absolute_time());
216
+    uint64_t us = us_per_beat / beats;
210 217
 
211
-    if ((ms_per_beat > 0) && (now >= (last_t + ms_per_beat))) {
212
-        //printf("trigger\n");
218
+    if ((us > 0) && (now >= (last_t + us))) {
219
+        //debug("trigger");
213 220
 
214 221
         uint32_t i = last_i + 1;
215 222
         if (i >= beats) i = 0;

+ 72
- 50
src/ui.c ファイルの表示

@@ -21,17 +21,26 @@
21 21
 #include <math.h>
22 22
 #include "pico/stdlib.h"
23 23
 
24
+#include "config.h"
24 25
 #include "adc.h"
25 26
 #include "buttons.h"
26 27
 #include "lcd.h"
27 28
 #include "led.h"
29
+#include "log.h"
28 30
 #include "pulse.h"
29 31
 #include "sequence.h"
30 32
 #include "usb.h"
31 33
 #include "usb_midi.h"
32 34
 #include "ui.h"
33 35
 
34
-#define BAT_FETCH_MS 66
36
+#define KEEP_IN_RANGE(val, min, len) { \
37
+    while (val > (len - min)) {        \
38
+        val -= len;                    \
39
+    }                                  \
40
+    while (val < min) {                \
41
+        val += len;                    \
42
+    }                                  \
43
+}
35 44
 
36 45
 enum ui_settings {
37 46
     SETTING_MODE = 0,
@@ -41,8 +50,9 @@ enum ui_settings {
41 50
 
42 51
     // drum machine
43 52
     SETTING_BPM,
44
-    SETTING_BANK,
45 53
     SETTING_LENGTH,
54
+    SETTING_BANK,
55
+    SETTING_CHANNEL,
46 56
 
47 57
     // midi
48 58
     SETTING_CH_RX,
@@ -54,22 +64,25 @@ enum ui_settings {
54 64
 static bool allowed_settings[MACHINE_NUM_MODES][SETTING_NUM_MODES] = {
55 65
     // MODE_LOOPSTATION
56 66
     {
57
-        true, true,
58
-        false, false, false,
67
+        true, // SETTING_MODE
68
+        true,
69
+        false, false, false, false,
59 70
         false, false,
60 71
     },
61 72
 
62 73
     // MODE_DRUMMACHINE
63 74
     {
64
-        true, false,
65
-        true, true, true,
75
+        true, // SETTING_MODE
76
+        false,
77
+        true, true, true, true,
66 78
         false, false,
67 79
     },
68 80
 
69 81
     // MODE_MIDI
70 82
     {
71
-        true, false,
72
-        false, false, false,
83
+        true, // SETTING_MODE
84
+        false,
85
+        false, false, false, false,
73 86
         true, true,
74 87
     },
75 88
 };
@@ -94,15 +107,9 @@ static void ui_redraw(void) {
94 107
     char bat[64] = {0};
95 108
 
96 109
     switch (ui_setting) {
97
-        case SETTING_BPM: {
98
-            snprintf(mode, sizeof(mode) - 1, "BPM:");
99
-            snprintf(val, sizeof(val) - 1, "%"PRIu32, sequence_get_bpm());
100
-            break;
101
-        }
102
-
103 110
         case SETTING_MODE: {
104
-            if ((machine_mode == MODE_LOOPSTATION) && (sequence_get_ms() != 0)) {
105
-                snprintf(mode, sizeof(mode) - 1, "Mode: %"PRIu32"ms", sequence_get_ms());
111
+            if ((machine_mode == MODE_LOOPSTATION) && (sequence_get_us() != 0)) {
112
+                snprintf(mode, sizeof(mode) - 1, "Mode: %"PRIu64"ms", sequence_get_us() / 1000);
106 113
             } else {
107 114
                 snprintf(mode, sizeof(mode) - 1, "Mode:");
108 115
             }
@@ -134,6 +141,17 @@ static void ui_redraw(void) {
134 141
             break;
135 142
         }
136 143
 
144
+        case SETTING_SPEED:
145
+        case SETTING_BPM: {
146
+            if ((machine_mode == MODE_LOOPSTATION) && (sequence_get_us() != 0)) {
147
+                snprintf(mode, sizeof(mode) - 1, "BPM: %"PRIu64"ms", sequence_get_us() / 1000);
148
+            } else {
149
+                snprintf(mode, sizeof(mode) - 1, "BPM:");
150
+            }
151
+            snprintf(val, sizeof(val) - 1, "%"PRIu32, sequence_get_bpm());
152
+            break;
153
+        }
154
+
137 155
         case SETTING_LENGTH: {
138 156
             snprintf(mode, sizeof(mode) - 1, "Length:");
139 157
             snprintf(val, sizeof(val) - 1, "%"PRIu32, sequence_get_beats());
@@ -142,13 +160,7 @@ static void ui_redraw(void) {
142 160
 
143 161
         case SETTING_BANK: {
144 162
             snprintf(mode, sizeof(mode) - 1, "Bank:");
145
-            snprintf(val, sizeof(val) - 1, "%"PRIu32, sequence_get_bank());
146
-            break;
147
-        }
148
-
149
-        case SETTING_SPEED: {
150
-            snprintf(mode, sizeof(mode) - 1, "Speed:");
151
-            snprintf(val, sizeof(val) - 1, "%"PRIu32, sequence_get_ms());
163
+            snprintf(val, sizeof(val) - 1, "%"PRIu32, sequence_get_bank() + 1);
152 164
             break;
153 165
         }
154 166
 
@@ -164,8 +176,14 @@ static void ui_redraw(void) {
164 176
             break;
165 177
         }
166 178
 
179
+        case SETTING_CHANNEL: {
180
+            snprintf(mode, sizeof(mode) - 1, "Channel:");
181
+            snprintf(val, sizeof(val) - 1, "todo");
182
+            break;
183
+        }
184
+
167 185
         default: {
168
-            printf("%s: invalid setting: %d\n", __func__, ui_setting);
186
+            debug("invalid setting: %d", ui_setting);
169 187
             ui_setting = 0;
170 188
             ui_redraw();
171 189
             return;
@@ -208,7 +226,7 @@ static void ui_buttons_loopstation(enum buttons btn, bool val) {
208 226
         }
209 227
 
210 228
         default: {
211
-            printf("%s: invalid btn: %d\n", __func__, btn);
229
+            debug("invalid btn: %d", btn);
212 230
             break;
213 231
         }
214 232
     }
@@ -232,7 +250,7 @@ static void ui_buttons_drummachine(enum buttons btn, bool val) {
232 250
         }
233 251
 
234 252
         default: {
235
-            printf("%s: invalid btn: %d\n", __func__, btn);
253
+            debug("invalid btn: %d", btn);
236 254
             break;
237 255
         }
238 256
     }
@@ -291,7 +309,7 @@ static void ui_buttons(enum buttons btn, bool val) {
291 309
                 }
292 310
 
293 311
                 default: {
294
-                    printf("%s: invalid mode: %d\n", __func__, machine_mode);
312
+                    debug("invalid mode: %d", machine_mode);
295 313
                     machine_mode = 0;
296 314
                     ui_buttons(btn, val);
297 315
                     break;
@@ -308,20 +326,9 @@ void ui_encoder(int32_t val) {
308 326
     }
309 327
 
310 328
     switch (ui_setting) {
311
-        case SETTING_BPM: {
312
-            sequence_set_bpm(sequence_get_bpm() + val);
313
-            break;
314
-        }
315
-
316 329
         case SETTING_MODE: {
317 330
             int32_t tmp = machine_mode + val;
318
-
319
-            while (tmp < 0) {
320
-                tmp += MACHINE_NUM_MODES;
321
-            }
322
-            while (tmp >= MACHINE_NUM_MODES) {
323
-                tmp -= MACHINE_NUM_MODES;
324
-            }
331
+            KEEP_IN_RANGE(tmp, 0, MACHINE_NUM_MODES);
325 332
 
326 333
             // midi only when connected to pc
327 334
             if ((tmp == MODE_MIDI) && !usb_is_connected()) {
@@ -331,8 +338,6 @@ void ui_encoder(int32_t val) {
331 338
             enum machine_modes prev_mode = machine_mode;
332 339
             machine_mode = tmp;
333 340
 
334
-            printf("mode add %"PRIi32" now %d\n", val, machine_mode);
335
-
336 341
             if (prev_mode != machine_mode) {
337 342
                 // reset sequence
338 343
                 sequence_init();
@@ -350,38 +355,55 @@ void ui_encoder(int32_t val) {
350 355
                     led_set(NUM_CHANNELS + 4, true);
351 356
                 } else if (machine_mode == MODE_DRUMMACHINE) {
352 357
                     sequence_set_beats(LED_COUNT);
358
+                    sequence_set_bpm(120);
353 359
                 }
354 360
             }
355 361
             break;
356 362
         }
357 363
 
358
-        case SETTING_LENGTH: {
359
-            sequence_set_beats(sequence_get_beats() + val);
364
+        case SETTING_SPEED:
365
+        case SETTING_BPM: {
366
+            int32_t tmp = sequence_get_bpm() + val;
367
+            KEEP_IN_RANGE(tmp, 1, MAX_BPM);
368
+            sequence_set_bpm(tmp);
360 369
             break;
361 370
         }
362 371
 
363
-        case SETTING_BANK: {
364
-            sequence_set_bank(sequence_get_bank() + val);
372
+        case SETTING_LENGTH: {
373
+            int32_t tmp = sequence_get_beats() + val;
374
+            KEEP_IN_RANGE(tmp, 1, MAX_BEATS);
375
+            sequence_set_beats(tmp);
365 376
             break;
366 377
         }
367 378
 
368
-        case SETTING_SPEED: {
369
-            sequence_set_ms(sequence_get_ms() + val);
379
+        case SETTING_BANK: {
380
+            int32_t tmp = sequence_get_bank() + val;
381
+            KEEP_IN_RANGE(tmp, 0, (int32_t)sequence_get_max_banks());
382
+            sequence_set_bank(tmp);
370 383
             break;
371 384
         }
372 385
 
373 386
         case SETTING_CH_RX: {
374
-            midi_rx = (uint8_t)(midi_rx + val) % MIDI_MAX_CH;
387
+            int32_t tmp = midi_rx + val;
388
+            KEEP_IN_RANGE(tmp, 0, MIDI_MAX_CH);
389
+            midi_rx = tmp;
375 390
             break;
376 391
         }
377 392
 
378 393
         case SETTING_CH_TX: {
379
-            midi_tx = (uint8_t)(midi_tx + val) % MIDI_MAX_CH;
394
+            int32_t tmp = midi_tx + val;
395
+            KEEP_IN_RANGE(tmp, 0, MIDI_MAX_CH);
396
+            midi_tx = tmp;
397
+            break;
398
+        }
399
+
400
+        case SETTING_CHANNEL: {
401
+            // TODO
380 402
             break;
381 403
         }
382 404
 
383 405
         default: {
384
-            printf("%s: invalid setting: %d\n", __func__, ui_setting);
406
+            debug("invalid setting: %d", ui_setting);
385 407
             ui_setting = 0;
386 408
             ui_encoder(val);
387 409
             return;

読み込み中…
キャンセル
保存