Subversion Repositories svn.mios32

Rev

Rev 741 | Rev 743 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
134 tk 1
// $Id: seq_ui.c 742 2009-10-10 18:57:18Z tk $
2
/*
3
 * User Interface Routines
4
 *
5
 * ==========================================================================
6
 *
7
 *  Copyright (C) 2008 Thorsten Klose (tk@midibox.org)
8
 *  Licensed for personal non-commercial use only.
9
 *  All other rights reserved.
10
 *
11
 * ==========================================================================
12
 */
13
 
14
/////////////////////////////////////////////////////////////////////////////
15
// Include files
16
/////////////////////////////////////////////////////////////////////////////
17
 
272 tk 18
// with this switch, seq_ui.h/seq_ui_pages.inc will create local variables
19
#define SEQ_UI_PAGES_INC_LOCAL_VARS 1
20
 
134 tk 21
#include <mios32.h>
299 tk 22
#include <string.h>
690 tk 23
#include <blm.h>
464 tk 24
#include <blm_x.h>
190 tk 25
#include <seq_midi_out.h>
625 tk 26
#include <seq_midi_sysex.h>
190 tk 27
#include <seq_bpm.h>
134 tk 28
 
290 tk 29
#include "tasks.h"
134 tk 30
#include "seq_ui.h"
728 tk 31
#include "seq_lcd.h"
32
#include "seq_lcd_logo.h"
492 tk 33
#include "seq_hwcfg.h"
134 tk 34
#include "seq_lcd.h"
35
#include "seq_led.h"
186 tk 36
#include "seq_midply.h"
134 tk 37
#include "seq_core.h"
399 tk 38
#include "seq_song.h"
333 tk 39
#include "seq_par.h"
178 tk 40
#include "seq_layer.h"
184 tk 41
#include "seq_cc.h"
299 tk 42
#include "seq_file.h"
690 tk 43
#include "seq_file_hw.h"
134 tk 44
 
45
 
46
/////////////////////////////////////////////////////////////////////////////
47
// Global variables
48
/////////////////////////////////////////////////////////////////////////////
49
 
159 tk 50
u8 seq_ui_display_update_req;
51
u8 seq_ui_display_init_req;
134 tk 52
 
167 tk 53
seq_ui_button_state_t seq_ui_button_state;
54
 
134 tk 55
u8 ui_selected_group;
484 tk 56
u16 ui_selected_tracks;
134 tk 57
u8 ui_selected_par_layer;
58
u8 ui_selected_trg_layer;
328 tk 59
u8 ui_selected_instrument;
134 tk 60
u8 ui_selected_step_view;
61
u8 ui_selected_step;
168 tk 62
u8 ui_selected_item;
134 tk 63
 
168 tk 64
u8 ui_selected_item;
134 tk 65
 
240 tk 66
u16 ui_hold_msg_ctr;
67
 
206 tk 68
seq_ui_page_t ui_page;
272 tk 69
seq_ui_page_t ui_selected_page;
600 tk 70
seq_ui_page_t ui_stepview_prev_page;
71
seq_ui_page_t ui_trglayer_prev_page;
72
seq_ui_page_t ui_parlayer_prev_page;
206 tk 73
 
173 tk 74
volatile u8 ui_cursor_flash;
740 tk 75
volatile u8 ui_cursor_flash_overrun_ctr;
173 tk 76
u16 ui_cursor_flash_ctr;
134 tk 77
 
316 tk 78
u8 ui_edit_name_cursor;
79
u8 ui_edit_preset_num_category;
80
u8 ui_edit_preset_num_label;
729 tk 81
u8 ui_edit_preset_num_drum;
316 tk 82
 
193 tk 83
u8 ui_seq_pause;
84
 
625 tk 85
seq_ui_remote_mode_t seq_ui_remote_mode;
626 tk 86
seq_ui_remote_mode_t seq_ui_remote_active_mode;
625 tk 87
mios32_midi_port_t seq_ui_remote_port;
626 tk 88
mios32_midi_port_t seq_ui_remote_active_port;
625 tk 89
u8 seq_ui_remote_id;
626 tk 90
u16 seq_ui_remote_client_timeout_ctr;
91
u8 seq_ui_remote_force_lcd_update;
92
u8 seq_ui_remote_force_led_update;
193 tk 93
 
634 tk 94
u8 seq_ui_backup_req;
740 tk 95
u8 seq_ui_format_req;
625 tk 96
 
634 tk 97
 
134 tk 98
/////////////////////////////////////////////////////////////////////////////
99
// Local variables
100
/////////////////////////////////////////////////////////////////////////////
101
 
168 tk 102
static s32 (*ui_button_callback)(seq_ui_button_t button, s32 depressed);
103
static s32 (*ui_encoder_callback)(seq_ui_encoder_t encoder, s32 incrementer);
173 tk 104
static s32 (*ui_led_callback)(u16 *gp_leds);
167 tk 105
static s32 (*ui_lcd_callback)(u8 high_prio);
352 tk 106
static s32 (*ui_exit_callback)(void);
740 tk 107
static s32 (*ui_delayed_action_callback)(void);
167 tk 108
 
168 tk 109
static u16 ui_gp_leds;
167 tk 110
 
524 tk 111
#define NUM_BLM_LED_SRS (2*4)
112
static u8 ui_blm_leds[NUM_BLM_LED_SRS];
113
 
596 tk 114
#define UI_MSG_MAX_CHAR 21
115
static char ui_msg[2][UI_MSG_MAX_CHAR];
116
static u16 ui_msg_ctr;
117
static seq_ui_msg_type_t ui_msg_type;
299 tk 118
 
740 tk 119
static u16 ui_delayed_action_ctr;
299 tk 120
 
134 tk 121
/////////////////////////////////////////////////////////////////////////////
122
// Initialisation
123
/////////////////////////////////////////////////////////////////////////////
124
s32 SEQ_UI_Init(u32 mode)
125
{
626 tk 126
  int i;
127
  // clear all LEDs
128
  for(i=0; i<SEQ_LED_NUM_SR; ++i)
129
    SEQ_LED_SRSet(i, 0x00);
130
 
134 tk 131
  // init selection variables
132
  ui_selected_group = 0;
133
  ui_selected_tracks = (1 << 0);
134
  ui_selected_par_layer = 0;
135
  ui_selected_trg_layer = 0;
328 tk 136
  ui_selected_instrument = 0;
134 tk 137
  ui_selected_step_view = 0;
138
  ui_selected_step = 0;
168 tk 139
  ui_selected_item = 0;
134 tk 140
 
240 tk 141
  ui_hold_msg_ctr = 0;
596 tk 142
  ui_msg_ctr = 0;
740 tk 143
  ui_delayed_action_ctr = 0;
240 tk 144
 
740 tk 145
  ui_cursor_flash_ctr = ui_cursor_flash_overrun_ctr = 0;
173 tk 146
  ui_cursor_flash = 0;
168 tk 147
 
167 tk 148
  seq_ui_button_state.ALL = 0;
149
 
193 tk 150
  ui_seq_pause = 0;
151
 
134 tk 152
  // visible GP pattern
153
  ui_gp_leds = 0x0000;
154
 
625 tk 155
  // default remote mode
156
  seq_ui_remote_mode = SEQ_UI_REMOTE_MODE_AUTO;
626 tk 157
  seq_ui_remote_active_mode = SEQ_UI_REMOTE_MODE_AUTO;
625 tk 158
  seq_ui_remote_port = DEFAULT;
626 tk 159
  seq_ui_remote_active_port = DEFAULT;
625 tk 160
  seq_ui_remote_id = 0x00;
626 tk 161
  seq_ui_remote_client_timeout_ctr = 0;
162
  seq_ui_remote_force_lcd_update = 0;
163
  seq_ui_remote_force_led_update = 0;
625 tk 164
 
634 tk 165
  // misc
166
  seq_ui_backup_req = 0;
740 tk 167
  seq_ui_format_req = 0;
634 tk 168
 
167 tk 169
  // change to edit page
170
  ui_page = SEQ_UI_PAGE_NONE;
171
  SEQ_UI_PageSet(SEQ_UI_PAGE_EDIT);
134 tk 172
 
596 tk 173
 
134 tk 174
  return 0; // no error
175
}
176
 
177
 
178
/////////////////////////////////////////////////////////////////////////////
178 tk 179
// Inits the speed mode of all encoders
180
// Auto mode should be used whenever:
181
//    - the edit screen is entered
182
//    - the group is changed
183
//    - a track is changed
184
//    - a layer is changed
185
/////////////////////////////////////////////////////////////////////////////
186
s32 SEQ_UI_InitEncSpeed(u32 auto_config)
187
{
188
  mios32_enc_config_t enc_config;
189
 
190
  if( auto_config ) {
513 tk 191
 
192
    if( !seq_hwcfg_enc.auto_fast )
193
      return 0; // auto mode not enabled - ignore auto reconfiguration request
194
 
333 tk 195
    switch( SEQ_PAR_AssignmentGet(SEQ_UI_VisibleTrackGet(), ui_selected_par_layer) ) {
196
      case SEQ_PAR_Type_Velocity:
197
      case SEQ_PAR_Type_Length:
198
      case SEQ_PAR_Type_CC:
199
      case SEQ_PAR_Type_PitchBend:
200
      case SEQ_PAR_Type_Probability:
201
      case SEQ_PAR_Type_Delay:
178 tk 202
    seq_ui_button_state.FAST_ENCODERS = 1;
203
    break;
204
 
205
      default:
206
    seq_ui_button_state.FAST_ENCODERS = 0;
207
    }
208
  }
209
 
210
  // change for datawheel and GP encoders
211
  int enc;
212
  for(enc=0; enc<17; ++enc) {
213
    enc_config = MIOS32_ENC_ConfigGet(enc);
214
    enc_config.cfg.speed = seq_ui_button_state.FAST_ENCODERS ? FAST : NORMAL;
513 tk 215
    enc_config.cfg.speed_par = (enc == 0) ? seq_hwcfg_enc.datawheel_fast_speed : seq_hwcfg_enc.gp_fast_speed;
178 tk 216
    MIOS32_ENC_ConfigSet(enc, enc_config);
217
  }
218
 
219
  return 0; // no error
220
}
221
 
222
 
223
/////////////////////////////////////////////////////////////////////////////
167 tk 224
// Various installation routines for menu page LCD handlers
225
/////////////////////////////////////////////////////////////////////////////
168 tk 226
s32 SEQ_UI_InstallButtonCallback(void *callback)
167 tk 227
{
168 tk 228
  ui_button_callback = callback;
167 tk 229
  return 0; // no error
230
}
231
 
168 tk 232
s32 SEQ_UI_InstallEncoderCallback(void *callback)
167 tk 233
{
168 tk 234
  ui_encoder_callback = callback;
167 tk 235
  return 0; // no error
236
}
237
 
168 tk 238
s32 SEQ_UI_InstallLEDCallback(void *callback)
167 tk 239
{
168 tk 240
  ui_led_callback = callback;
167 tk 241
  return 0; // no error
242
}
243
 
168 tk 244
s32 SEQ_UI_InstallLCDCallback(void *callback)
167 tk 245
{
246
  ui_lcd_callback = callback;
247
  return 0; // no error
248
}
249
 
352 tk 250
s32 SEQ_UI_InstallExitCallback(void *callback)
251
{
252
  ui_exit_callback = callback;
253
  return 0; // no error
254
}
167 tk 255
 
352 tk 256
 
740 tk 257
s32 SEQ_UI_InstallDelayedActionCallback(void *callback, u16 ctr, char *msg)
258
{
259
  ui_delayed_action_callback = callback;
260
  ui_delayed_action_ctr = ctr;
261
 
262
  SEQ_UI_Msg(SEQ_UI_MSG_DELAYED_ACTION, ctr+1, "", msg);
263
 
264
  return 0; // no error
265
}
266
 
267
s32 SEQ_UI_UnInstallDelayedActionCallback(void *callback)
268
{
269
  if( ui_delayed_action_callback == callback )
270
    ui_delayed_action_callback = 0;
271
  return 0; // no error
272
}
273
 
274
 
167 tk 275
/////////////////////////////////////////////////////////////////////////////
276
// Change the menu page
277
/////////////////////////////////////////////////////////////////////////////
278
s32 SEQ_UI_PageSet(seq_ui_page_t page)
279
{
280
  if( page != ui_page ) {
352 tk 281
 
282
    // call page exit callback
283
    if( ui_exit_callback != NULL )
284
      ui_exit_callback();
285
 
167 tk 286
    // disable hooks of previous page and request re-initialisation
318 tk 287
    portENTER_CRITICAL();
167 tk 288
    ui_page = page;
168 tk 289
    ui_button_callback = NULL;
290
    ui_encoder_callback = NULL;
291
    ui_led_callback = NULL;
167 tk 292
    ui_lcd_callback = NULL;
352 tk 293
    ui_exit_callback = NULL;
740 tk 294
    ui_delayed_action_callback = NULL;
318 tk 295
    portEXIT_CRITICAL();
167 tk 296
 
513 tk 297
    if( seq_hwcfg_button_beh.menu )
298
      seq_ui_button_state.MENU_PRESSED = 0; // MENU page selection finished
167 tk 299
 
300
    // request display initialisation
301
    seq_ui_display_init_req = 1;
302
  }
173 tk 303
 
306 tk 304
  // for MENU button:
305
  // first page has been selected - display new screen
306
  seq_ui_button_state.MENU_FIRST_PAGE_SELECTED = 1;
307
 
272 tk 308
  // for SEQ_UI_MENU which is accessible with EXIT button
309
  // remember the current selectable page
310
  if( ui_page >= SEQ_UI_FIRST_MENU_SELECTION_PAGE )
311
    ui_selected_page = ui_page;
312
 
184 tk 313
  return 0; // no error
167 tk 314
}
315
 
316
 
317
/////////////////////////////////////////////////////////////////////////////
272 tk 318
// Returns name of menu page (18 characters)
319
/////////////////////////////////////////////////////////////////////////////
320
char *SEQ_UI_PageNameGet(seq_ui_page_t page)
321
{
690 tk 322
  return (char *)ui_menu_pages[page].name;
272 tk 323
}
324
 
325
 
326
/////////////////////////////////////////////////////////////////////////////
134 tk 327
// Dedicated button functions
328
// Mapped to physical buttons in SEQ_UI_Button_Handler()
329
// Will also be mapped to MIDI keys later (for MIDI remote function)
330
/////////////////////////////////////////////////////////////////////////////
331
static s32 SEQ_UI_Button_GP(s32 depressed, u32 gp)
332
{
306 tk 333
  // in MENU page: overrule GP buttons so long MENU button is pressed/active
334
  if( seq_ui_button_state.MENU_PRESSED ) {
335
    if( depressed ) return -1;
336
    SEQ_UI_PageSet(ui_shortcut_menu_pages[gp]);
337
  } else {
338
    // forward to menu page
740 tk 339
    if( ui_button_callback != NULL ) {
306 tk 340
      ui_button_callback(gp, depressed);
740 tk 341
      ui_cursor_flash_ctr = ui_cursor_flash_overrun_ctr = 0;
342
    }
306 tk 343
  }
134 tk 344
 
345
  return 0; // no error
346
}
347
 
348
static s32 SEQ_UI_Button_Left(s32 depressed)
349
{
168 tk 350
  // forward to menu page
740 tk 351
  if( !seq_ui_button_state.MENU_PRESSED && ui_button_callback != NULL ) {
168 tk 352
    ui_button_callback(SEQ_UI_BUTTON_Left, depressed);
740 tk 353
    ui_cursor_flash_ctr = ui_cursor_flash_overrun_ctr = 0;
354
  }
134 tk 355
 
356
  return 0; // no error
357
}
358
 
359
static s32 SEQ_UI_Button_Right(s32 depressed)
360
{
168 tk 361
  // forward to menu page
740 tk 362
  if( !seq_ui_button_state.MENU_PRESSED && ui_button_callback != NULL ) {
168 tk 363
    ui_button_callback(SEQ_UI_BUTTON_Right, depressed);
740 tk 364
    ui_cursor_flash_ctr = ui_cursor_flash_overrun_ctr = 0;
365
  }
134 tk 366
 
367
  return 0; // no error
368
}
369
 
168 tk 370
static s32 SEQ_UI_Button_Down(s32 depressed)
371
{
240 tk 372
  seq_ui_button_state.DOWN = depressed ? 0 : 1;
373
 
168 tk 374
  // forward to menu page
740 tk 375
  if( !seq_ui_button_state.MENU_PRESSED && ui_button_callback != NULL ) {
168 tk 376
    ui_button_callback(SEQ_UI_BUTTON_Down, depressed);
740 tk 377
    ui_cursor_flash_ctr = ui_cursor_flash_overrun_ctr = 0;
378
  }
168 tk 379
 
380
  return 0; // no error
381
}
382
 
383
static s32 SEQ_UI_Button_Up(s32 depressed)
384
{
240 tk 385
  seq_ui_button_state.UP = depressed ? 0 : 1;
386
 
168 tk 387
  // forward to menu page
740 tk 388
  if( !seq_ui_button_state.MENU_PRESSED && ui_button_callback != NULL ) {
168 tk 389
    ui_button_callback(SEQ_UI_BUTTON_Up, depressed);
740 tk 390
    ui_cursor_flash_ctr = ui_cursor_flash_overrun_ctr = 0;
391
  }
168 tk 392
 
393
  return 0; // no error
394
}
395
 
134 tk 396
static s32 SEQ_UI_Button_Stop(s32 depressed)
397
{
398
  if( depressed ) return -1; // ignore when button depressed
399
 
159 tk 400
  // if sequencer running: stop it
401
  // if sequencer already stopped: reset song position
186 tk 402
#if MID_PLAYER_TEST
193 tk 403
  if( SEQ_BPM_IsRunning() )
404
    SEQ_BPM_Stop();
186 tk 405
  else
406
    SEQ_MIDPLY_Reset();
407
#else
193 tk 408
  if( SEQ_BPM_IsRunning() )
409
    SEQ_BPM_Stop();
159 tk 410
  else
411
    SEQ_CORE_Reset();
186 tk 412
#endif
159 tk 413
 
134 tk 414
  return 0; // no error
415
}
416
 
417
static s32 SEQ_UI_Button_Pause(s32 depressed)
418
{
419
  if( depressed ) return -1; // ignore when button depressed
420
 
193 tk 421
  // if in auto mode and BPM generator is clocked in slave mode:
422
  // change to master mode
423
  SEQ_BPM_CheckAutoMaster();
159 tk 424
 
193 tk 425
  // toggle pause mode
426
  ui_seq_pause ^= 1;
427
 
428
  // execute stop/continue depending on new mode
429
  if( ui_seq_pause )
430
    SEQ_BPM_Stop();
431
  else
432
    SEQ_BPM_Cont();
433
 
134 tk 434
  return 0; // no error
435
}
436
 
437
static s32 SEQ_UI_Button_Play(s32 depressed)
438
{
439
  if( depressed ) return -1; // ignore when button depressed
440
 
186 tk 441
  // if in auto mode and BPM generator is clocked in slave mode:
442
  // change to master mode
443
  SEQ_BPM_CheckAutoMaster();
444
 
193 tk 445
#if 0
446
  // if sequencer running: restart it
447
  // if sequencer stopped: continue at last song position
448
  if( SEQ_BPM_IsRunning() )
449
    SEQ_BPM_Start();
450
  else
451
    SEQ_BPM_Cont();
186 tk 452
#else
193 tk 453
  // always restart sequencer
454
  SEQ_BPM_Start();
186 tk 455
#endif
159 tk 456
 
134 tk 457
  return 0; // no error
458
}
459
 
460
static s32 SEQ_UI_Button_Rew(s32 depressed)
461
{
240 tk 462
  seq_ui_button_state.REW = depressed ? 0 : 1;
463
 
134 tk 464
  if( depressed ) return -1; // ignore when button depressed
465
 
607 tk 466
  if( SEQ_SONG_ActiveGet() ) {
467
    portENTER_CRITICAL();
468
    SEQ_SONG_Rew();
469
    portEXIT_CRITICAL();
470
  } else
471
    SEQ_UI_Msg(SEQ_UI_MSG_USER, 1000, "We are not", "in Song Mode!");
472
 
134 tk 473
  return 0; // no error
474
}
475
 
476
static s32 SEQ_UI_Button_Fwd(s32 depressed)
477
{
240 tk 478
  seq_ui_button_state.FWD = depressed ? 0 : 1;
479
 
134 tk 480
  if( depressed ) return -1; // ignore when button depressed
481
 
607 tk 482
  if( SEQ_SONG_ActiveGet() ) {
483
    portENTER_CRITICAL();
484
    SEQ_SONG_Fwd();
485
    portEXIT_CRITICAL();
486
  } else
487
    SEQ_UI_Msg(SEQ_UI_MSG_USER, 1000, "We are not", "in Song Mode!");
488
 
134 tk 489
  return 0; // no error
490
}
491
 
596 tk 492
static s32 SEQ_UI_Button_Loop(s32 depressed)
493
{
494
  if( seq_hwcfg_button_beh.loop ) {
495
    // toggle mode
496
    if( depressed ) return -1; // ignore when button depressed
497
    // should be atomic
498
    portENTER_CRITICAL();
499
    seq_core_state.LOOP ^= 1;
500
  } else {
501
    // should be atomic
502
    portENTER_CRITICAL();
503
    // set mode
504
    seq_core_state.LOOP = depressed ? 0 : 1;
505
  }
506
  portEXIT_CRITICAL();
507
 
508
  SEQ_UI_Msg(SEQ_UI_MSG_USER, 1000, "Loop Mode", seq_core_state.LOOP ? "    on" : "   off");
509
 
510
  return 0; // no error
511
}
512
 
513
static s32 SEQ_UI_Button_Scrub(s32 depressed)
514
{
515
  // double function: -> Loop if menu button pressed
516
  if( seq_ui_button_state.MENU_PRESSED )
517
    return SEQ_UI_Button_Loop(depressed);
518
 
519
  if( seq_hwcfg_button_beh.scrub ) {
520
    // toggle mode
521
    if( depressed ) return -1; // ignore when button depressed
522
    seq_ui_button_state.SCRUB ^= 1;
523
  } else {
524
    // set mode
525
    seq_ui_button_state.SCRUB = depressed ? 0 : 1;
526
  }
527
 
528
  SEQ_UI_Msg(SEQ_UI_MSG_USER, 1000, "Scrub Mode", seq_ui_button_state.SCRUB ? "    on" : "   off");
529
 
530
  return 0; // no error
531
}
532
 
513 tk 533
static s32 SEQ_UI_Button_TempoPreset(s32 depressed)
134 tk 534
{
513 tk 535
  static seq_ui_page_t prev_page = SEQ_UI_PAGE_NONE;
240 tk 536
 
513 tk 537
  if( seq_hwcfg_button_beh.tempo_preset ) {
538
    if( depressed ) return -1; // ignore when button depressed
678 tk 539
    if( !seq_ui_button_state.TEMPO_PRESET ) // due to page change: button going to be set, clear other toggle buttons
540
      seq_ui_button_state.PAGE_CHANGE_BUTTON_FLAGS = 0;
513 tk 541
    seq_ui_button_state.TEMPO_PRESET ^= 1; // toggle TEMPO_PRESET pressed (will also be released once GP button has been pressed)
542
  } else {
543
    // set mode
544
    seq_ui_button_state.TEMPO_PRESET = depressed ? 0 : 1;
545
  }
134 tk 546
 
513 tk 547
  if( seq_ui_button_state.TEMPO_PRESET ) {
548
    prev_page = ui_page;
549
    SEQ_UI_PageSet(SEQ_UI_PAGE_BPM_PRESETS);
550
  } else {
524 tk 551
    if( ui_page == SEQ_UI_PAGE_BPM_PRESETS )
552
      SEQ_UI_PageSet(prev_page);
513 tk 553
  }
240 tk 554
 
134 tk 555
  return 0; // no error
556
}
557
 
513 tk 558
static s32 SEQ_UI_Button_TapTempo(s32 depressed)
134 tk 559
{
513 tk 560
  seq_ui_button_state.TAP_TEMPO = depressed ? 0 : 1;
240 tk 561
 
134 tk 562
  if( depressed ) return -1; // ignore when button depressed
563
 
564
  return 0; // no error
565
}
566
 
524 tk 567
static s32 SEQ_UI_Button_ExtRestart(s32 depressed)
134 tk 568
{
569
  if( depressed ) return -1; // ignore when button depressed
570
 
596 tk 571
  // should be atomic
524 tk 572
  portENTER_CRITICAL();
573
  seq_core_state.EXT_RESTART_REQ = 1;
574
  portEXIT_CRITICAL();
575
 
596 tk 576
  SEQ_UI_Msg(SEQ_UI_MSG_USER, 1000, "External Restart", "requested");
577
 
134 tk 578
  return 0; // no error
579
}
580
 
596 tk 581
static s32 SEQ_UI_Button_Metronome(s32 depressed)
582
{
583
  // double function: -> ExtRestart if menu button pressed
584
  if( seq_ui_button_state.MENU_PRESSED )
585
    return SEQ_UI_Button_ExtRestart(depressed);
586
 
587
  if( seq_hwcfg_button_beh.metronome ) {
588
    // toggle mode
589
    if( depressed ) return -1; // ignore when button depressed
590
    // should be atomic
591
    portENTER_CRITICAL();
592
    seq_core_state.METRONOME ^= 1;
593
  } else {
594
    // should be atomic
595
    portENTER_CRITICAL();
596
    // set mode
597
    seq_core_state.METRONOME = depressed ? 0 : 1;
598
  }
599
  portEXIT_CRITICAL();
600
 
601
  SEQ_UI_Msg(SEQ_UI_MSG_USER, 1000, "Metronome", seq_core_state.METRONOME ? "    on" : "   off");
602
 
599 tk 603
#if 0
604
  // metronome button can be used to trigger pattern file fixes
605
  if( !depressed )
606
    SEQ_PATTERN_FixAll();
607
#endif
608
 
596 tk 609
  return 0; // no error
610
}
611
 
630 tk 612
static s32 SEQ_UI_Button_Record(s32 depressed)
613
{
614
  if( depressed ) return -1; // ignore when button depressed
615
 
616
  // change to utility page
617
  SEQ_UI_PageSet(SEQ_UI_PAGE_TRKREC);
618
 
619
  return 0; // no error
620
}
621
 
134 tk 622
static s32 SEQ_UI_Button_Utility(s32 depressed)
623
{
624
  if( depressed ) return -1; // ignore when button depressed
625
 
240 tk 626
  // change to utility page
627
  SEQ_UI_PageSet(SEQ_UI_PAGE_UTIL);
628
 
134 tk 629
  return 0; // no error
630
}
631
 
632
static s32 SEQ_UI_Button_Copy(s32 depressed)
633
{
240 tk 634
  static seq_ui_page_t prev_page = SEQ_UI_PAGE_NONE;
134 tk 635
 
240 tk 636
  seq_ui_button_state.COPY = depressed ? 0 : 1;
637
 
280 tk 638
  if( ui_page == SEQ_UI_PAGE_MIXER ) {
639
    if( depressed ) return -1;
640
    SEQ_UI_MIXER_Copy();
729 tk 641
    SEQ_UI_Msg(SEQ_UI_MSG_USER, 1000, "Mixer Map", "copied");
280 tk 642
    return 1;
729 tk 643
  } else if( ui_page == SEQ_UI_PAGE_SONG ) {
644
    if( depressed ) return -1;
645
    SEQ_UI_SONG_Copy();
646
    SEQ_UI_Msg(SEQ_UI_MSG_USER, 1000, "Song Position", "copied");
647
    return 1;
280 tk 648
  } else {
649
    if( !depressed ) {
650
      prev_page = ui_page;
651
      SEQ_UI_PageSet(SEQ_UI_PAGE_UTIL);
652
    }
240 tk 653
 
280 tk 654
    s32 status = SEQ_UI_UTIL_CopyButton(depressed);
240 tk 655
 
596 tk 656
    if( depressed ) {
657
      if( prev_page != SEQ_UI_PAGE_UTIL )
658
    SEQ_UI_PageSet(prev_page);
240 tk 659
 
596 tk 660
      SEQ_UI_Msg(SEQ_UI_MSG_USER, 1000, "Track", "copied");
661
    }
662
 
280 tk 663
    return status;
664
  }
134 tk 665
}
666
 
667
static s32 SEQ_UI_Button_Paste(s32 depressed)
668
{
240 tk 669
  static seq_ui_page_t prev_page = SEQ_UI_PAGE_NONE;
134 tk 670
 
240 tk 671
  seq_ui_button_state.PASTE = depressed ? 0 : 1;
672
 
280 tk 673
  if( ui_page == SEQ_UI_PAGE_MIXER ) {
674
    if( depressed ) return -1;
675
    SEQ_UI_MIXER_Paste();
729 tk 676
    SEQ_UI_Msg(SEQ_UI_MSG_USER, 1000, "Mixer Map", "pasted");
280 tk 677
    return 1;
729 tk 678
  } else if( ui_page == SEQ_UI_PAGE_SONG ) {
679
    if( depressed ) return -1;
680
    SEQ_UI_SONG_Paste();
681
    SEQ_UI_Msg(SEQ_UI_MSG_USER, 1000, "Song Position", "pasted");
682
    return 1;
280 tk 683
  } else {
684
    if( !depressed ) {
685
      prev_page = ui_page;
686
      SEQ_UI_PageSet(SEQ_UI_PAGE_UTIL);
687
    }
240 tk 688
 
280 tk 689
    s32 status = SEQ_UI_UTIL_PasteButton(depressed);
240 tk 690
 
596 tk 691
    if( depressed ) {
692
      if( prev_page != SEQ_UI_PAGE_UTIL )
693
    SEQ_UI_PageSet(prev_page);
240 tk 694
 
596 tk 695
      SEQ_UI_Msg(SEQ_UI_MSG_USER, 1000, "Track", "pasted");
696
    }
697
 
280 tk 698
    return status;
699
  }
134 tk 700
}
701
 
740 tk 702
 
703
// callback function for delayed Clear Mixer function
704
static void SEQ_UI_Button_Clear_Mixer(void)
705
{
706
  SEQ_UI_MIXER_Clear();
707
  SEQ_UI_Msg(SEQ_UI_MSG_USER, 1000, "Mixer Map", "cleared");
708
}
709
 
710
// callback function for delayed Clear SongPos function
711
static void SEQ_UI_Button_Clear_SongPos(void)
712
{
713
  SEQ_UI_SONG_Clear();
714
  SEQ_UI_Msg(SEQ_UI_MSG_USER, 1000, "Song Position", "cleared");
715
}
716
 
717
// callback function for clear track
718
static void SEQ_UI_Button_Clear_Track(void)
719
{
720
  SEQ_UI_UTIL_ClearButton(0); // button pressed
721
  SEQ_UI_UTIL_ClearButton(1); // button depressed
722
  SEQ_UI_Msg(SEQ_UI_MSG_USER, 1000, "Track", "cleared");
723
}
724
 
134 tk 725
static s32 SEQ_UI_Button_Clear(s32 depressed)
726
{
240 tk 727
  seq_ui_button_state.CLEAR = depressed ? 0 : 1;
280 tk 728
 
729
  if( ui_page == SEQ_UI_PAGE_MIXER ) {
740 tk 730
    if( depressed )
731
      SEQ_UI_UnInstallDelayedActionCallback(SEQ_UI_Button_Clear_Mixer);
732
    else
733
      SEQ_UI_InstallDelayedActionCallback(SEQ_UI_Button_Clear_Mixer, 2000, "to clear Mixer Map");
729 tk 734
    return 1;
735
  } else if( ui_page == SEQ_UI_PAGE_SONG ) {
740 tk 736
    if( depressed )
737
      SEQ_UI_UnInstallDelayedActionCallback(SEQ_UI_Button_Clear_SongPos);
738
    else
739
      SEQ_UI_InstallDelayedActionCallback(SEQ_UI_Button_Clear_SongPos, 2000, "to clear SongPos");
729 tk 740
    return 1;
741
  } else {
740 tk 742
    if( depressed )
743
      SEQ_UI_UnInstallDelayedActionCallback(SEQ_UI_Button_Clear_Track);
744
    else
745
      SEQ_UI_InstallDelayedActionCallback(SEQ_UI_Button_Clear_Track, 2000, "to clear Track");
280 tk 746
    return 1;
747
  }
134 tk 748
}
749
 
750
static s32 SEQ_UI_Button_Menu(s32 depressed)
751
{
513 tk 752
  if( seq_hwcfg_button_beh.menu ) {
753
    // toggle mode
754
    if( depressed ) return -1; // ignore when button depressed
755
    seq_ui_button_state.MENU_FIRST_PAGE_SELECTED = 0;
678 tk 756
    if( !seq_ui_button_state.MENU_PRESSED ) // due to page change: button going to be set, clear other toggle buttons
757
      seq_ui_button_state.PAGE_CHANGE_BUTTON_FLAGS = 0;
513 tk 758
    seq_ui_button_state.MENU_PRESSED ^= 1; // toggle MENU pressed (will also be released once GP button has been pressed)
759
  } else {
760
    // set mode
761
    seq_ui_button_state.MENU_FIRST_PAGE_SELECTED = 0;
762
    seq_ui_button_state.MENU_PRESSED = depressed ? 0 : 1;
763
  }
134 tk 764
 
765
  return 0; // no error
766
}
767
 
768
static s32 SEQ_UI_Button_Select(s32 depressed)
769
{
168 tk 770
  // forward to menu page
591 tk 771
  if( !seq_ui_button_state.MENU_PRESSED ) {
772
    seq_ui_button_state.SELECT_PRESSED = depressed ? 0 : 1;
740 tk 773
    if( ui_button_callback != NULL ) {
591 tk 774
      ui_button_callback(SEQ_UI_BUTTON_Select, depressed);
740 tk 775
      ui_cursor_flash_ctr = ui_cursor_flash_overrun_ctr = 0;
776
    }
591 tk 777
  }
134 tk 778
 
779
  return 0; // no error
780
}
781
 
782
static s32 SEQ_UI_Button_Exit(s32 depressed)
783
{
272 tk 784
  if( depressed ) return -1; // ignore when button depressed
785
 
786
  u8 prev_ui_page = ui_page;
787
 
591 tk 788
  seq_ui_button_state.EXIT_PRESSED = depressed ? 0 : 1;
789
 
168 tk 790
  // forward to menu page
740 tk 791
  if( !seq_ui_button_state.MENU_PRESSED && ui_button_callback != NULL ) {
168 tk 792
    ui_button_callback(SEQ_UI_BUTTON_Exit, depressed);
740 tk 793
    ui_cursor_flash_ctr = ui_cursor_flash_overrun_ctr = 0;
794
  }
134 tk 795
 
167 tk 796
  // release all button states
797
  seq_ui_button_state.ALL = 0;
798
 
272 tk 799
  // enter menu page if we were not there before
800
  if( prev_ui_page != SEQ_UI_PAGE_MENU )
801
    SEQ_UI_PageSet(SEQ_UI_PAGE_MENU);
802
 
134 tk 803
  return 0; // no error
804
}
805
 
806
static s32 SEQ_UI_Button_Edit(s32 depressed)
807
{
741 tk 808
  seq_ui_button_state.EDIT_PRESSED = depressed ? 0 : 1;
591 tk 809
 
134 tk 810
  if( depressed ) return -1; // ignore when button depressed
811
 
167 tk 812
  // change to edit page
813
  SEQ_UI_PageSet(SEQ_UI_PAGE_EDIT);
814
 
178 tk 815
  // set/clear encoder fast function if required
816
  SEQ_UI_InitEncSpeed(1); // auto config
817
 
134 tk 818
  return 0; // no error
819
}
820
 
821
static s32 SEQ_UI_Button_Mute(s32 depressed)
822
{
591 tk 823
  seq_ui_button_state.MUTE_PRESSED = depressed ? 0 : 1;
824
 
134 tk 825
  if( depressed ) return -1; // ignore when button depressed
826
 
184 tk 827
  SEQ_UI_PageSet(SEQ_UI_PAGE_MUTE);
828
 
134 tk 829
  return 0; // no error
830
}
831
 
832
static s32 SEQ_UI_Button_Pattern(s32 depressed)
833
{
591 tk 834
  seq_ui_button_state.PATTERN_PRESSED = depressed ? 0 : 1;
835
 
134 tk 836
  if( depressed ) return -1; // ignore when button depressed
837
 
184 tk 838
  SEQ_UI_PageSet(SEQ_UI_PAGE_PATTERN);
839
 
134 tk 840
  return 0; // no error
841
}
842
 
843
static s32 SEQ_UI_Button_Song(s32 depressed)
844
{
591 tk 845
  seq_ui_button_state.SONG_PRESSED = depressed ? 0 : 1;
846
 
134 tk 847
  if( depressed ) return -1; // ignore when button depressed
848
 
399 tk 849
  SEQ_UI_PageSet(SEQ_UI_PAGE_SONG);
850
 
134 tk 851
  return 0; // no error
852
}
853
 
854
static s32 SEQ_UI_Button_Solo(s32 depressed)
855
{
513 tk 856
  if( seq_hwcfg_button_beh.solo ) {
857
    // toggle mode
858
    if( depressed ) return -1; // ignore when button depressed
859
    seq_ui_button_state.SOLO ^= 1;
860
  } else {
861
    // set mode
862
    seq_ui_button_state.SOLO = depressed ? 0 : 1;
863
  }
134 tk 864
 
865
  return 0; // no error
866
}
867
 
868
static s32 SEQ_UI_Button_Fast(s32 depressed)
869
{
513 tk 870
  if( seq_hwcfg_button_beh.fast ) {
871
    // toggle mode
872
    if( depressed ) return -1; // ignore when button depressed
873
    seq_ui_button_state.FAST_ENCODERS ^= 1;
874
  } else {
875
    // set mode
876
    seq_ui_button_state.FAST_ENCODERS = depressed ? 0 : 1;
877
  }
134 tk 878
 
178 tk 879
  SEQ_UI_InitEncSpeed(0); // no auto config
880
 
134 tk 881
  return 0; // no error
882
}
883
 
884
static s32 SEQ_UI_Button_All(s32 depressed)
885
{
178 tk 886
  seq_ui_button_state.CHANGE_ALL_STEPS_SAME_VALUE = depressed ? 0 : 1;
134 tk 887
 
513 tk 888
  if( seq_hwcfg_button_beh.all ) {
889
    // toggle mode
890
    if( depressed ) return -1;
891
    seq_ui_button_state.CHANGE_ALL_STEPS ^= 1;
892
  } else {
893
    // set mode
894
    seq_ui_button_state.CHANGE_ALL_STEPS = depressed ? 0 : 1;
895
  }
178 tk 896
 
134 tk 897
  return 0; // no error
898
}
899
 
900
static s32 SEQ_UI_Button_StepView(s32 depressed)
901
{
600 tk 902
  //  static seq_ui_page_t prev_page = SEQ_UI_PAGE_NONE;
903
  // also used by seq_ui_stepsel
904
 
513 tk 905
  if( seq_hwcfg_button_beh.step_view ) {
906
    if( depressed ) return -1; // ignore when button depressed
678 tk 907
    if( !seq_ui_button_state.STEP_VIEW ) // due to page change: button going to be set, clear other toggle buttons
908
      seq_ui_button_state.PAGE_CHANGE_BUTTON_FLAGS = 0;
513 tk 909
    seq_ui_button_state.STEP_VIEW ^= 1; // toggle STEP_VIEW pressed (will also be released once GP button has been pressed)
910
  } else {
911
    // set mode
912
    seq_ui_button_state.STEP_VIEW = depressed ? 0 : 1;
913
  }
134 tk 914
 
513 tk 915
  if( seq_ui_button_state.STEP_VIEW ) {
600 tk 916
    ui_stepview_prev_page = ui_page;
303 tk 917
    SEQ_UI_PageSet(SEQ_UI_PAGE_STEPSEL);
918
  } else {
524 tk 919
    if( ui_page == SEQ_UI_PAGE_STEPSEL )
600 tk 920
      SEQ_UI_PageSet(ui_stepview_prev_page);
303 tk 921
  }
134 tk 922
 
923
  return 0; // no error
924
}
925
 
513 tk 926
static s32 SEQ_UI_Button_TrackSel(s32 depressed)
134 tk 927
{
513 tk 928
  static seq_ui_page_t prev_page = SEQ_UI_PAGE_NONE;
134 tk 929
 
513 tk 930
  if( seq_hwcfg_button_beh.track_sel ) {
931
    if( depressed ) return -1; // ignore when button depressed
678 tk 932
    if( !seq_ui_button_state.TRACK_SEL ) // due to page change: button going to be set, clear other toggle buttons
933
      seq_ui_button_state.PAGE_CHANGE_BUTTON_FLAGS = 0;
513 tk 934
    seq_ui_button_state.TRACK_SEL ^= 1; // toggle TRACKSEL status (will also be released once GP button has been pressed)
935
  } else {
936
    seq_ui_button_state.TRACK_SEL = depressed ? 0 : 1;
937
  }
938
 
939
  if( seq_ui_button_state.TRACK_SEL ) {
940
    prev_page = ui_page;
941
    SEQ_UI_PageSet(SEQ_UI_PAGE_TRACKSEL);
942
  } else {
524 tk 943
    if( ui_page == SEQ_UI_PAGE_TRACKSEL )
944
      SEQ_UI_PageSet(prev_page);
513 tk 945
  }
946
 
134 tk 947
  return 0; // no error
948
}
949
 
484 tk 950
static s32 SEQ_UI_Button_Track(s32 depressed, u32 track_button)
134 tk 951
{
484 tk 952
  static u8 button_state = 0x0f; // all 4 buttons depressed
134 tk 953
 
484 tk 954
  if( track_button >= 4 ) return -2; // max. 4 track buttons
134 tk 955
 
484 tk 956
  if( depressed ) {
957
    button_state |= (1 << track_button);
958
    return 0; // no error
959
  }
134 tk 960
 
484 tk 961
  button_state &= ~(1 << track_button);
962
 
963
  if( button_state == (~(1 << track_button) & 0xf) ) {
964
    // if only one select button pressed: radio-button function (1 of 4)
965
    ui_selected_tracks = 1 << (track_button + 4*ui_selected_group);
966
  } else {
967
    // if more than one select button pressed: toggle function (4 of 4)
968
    ui_selected_tracks ^= 1 << (track_button + 4*ui_selected_group);
969
  }
970
 
178 tk 971
  // set/clear encoder fast function if required
972
  SEQ_UI_InitEncSpeed(1); // auto config
973
 
134 tk 974
  return 0; // no error
975
}
976
 
977
static s32 SEQ_UI_Button_Group(s32 depressed, u32 group)
978
{
979
  if( depressed ) return -1; // ignore when button depressed
980
 
981
  if( group >= 4 ) return -2; // max. 4 group buttons
982
 
484 tk 983
  // if group has changed:
984
  if( group != ui_selected_group ) {
985
    // get current track selection
986
    u16 old_tracks = ui_selected_tracks >> (4*ui_selected_group);
134 tk 987
 
484 tk 988
    // select new group
989
    ui_selected_group = group;
990
 
991
    // take over old track selection
992
    ui_selected_tracks = old_tracks << (4*ui_selected_group);
993
  }
994
 
178 tk 995
  // set/clear encoder fast function if required
996
  SEQ_UI_InitEncSpeed(1); // auto config
997
 
134 tk 998
  return 0; // no error
999
}
1000
 
513 tk 1001
static s32 SEQ_UI_Button_ParLayerSel(s32 depressed)
1002
{
600 tk 1003
  // static seq_ui_page_t prev_page = SEQ_UI_PAGE_NONE;
1004
  // also used by seq_ui_parsel.c
513 tk 1005
 
1006
  if( seq_hwcfg_button_beh.par_layer ) {
1007
    if( depressed ) return -1; // ignore when button depressed
678 tk 1008
    if( !seq_ui_button_state.PAR_LAYER_SEL ) // due to page change: button going to be set, clear other toggle buttons
1009
      seq_ui_button_state.PAGE_CHANGE_BUTTON_FLAGS = 0;
513 tk 1010
    seq_ui_button_state.PAR_LAYER_SEL ^= 1; // toggle PARSEL status (will also be released once GP button has been pressed)
1011
  } else {
1012
    seq_ui_button_state.PAR_LAYER_SEL = depressed ? 0 : 1;
1013
  }
1014
 
1015
  if( seq_ui_button_state.PAR_LAYER_SEL ) {
600 tk 1016
    ui_parlayer_prev_page = ui_page;
513 tk 1017
    SEQ_UI_PageSet(SEQ_UI_PAGE_PARSEL);
1018
  } else {
524 tk 1019
    if( ui_page == SEQ_UI_PAGE_PARSEL )
600 tk 1020
      SEQ_UI_PageSet(ui_parlayer_prev_page);
513 tk 1021
  }
1022
 
1023
  // set/clear encoder fast function if required
1024
  SEQ_UI_InitEncSpeed(1); // auto config
1025
 
1026
  return 0; // no error
1027
}
1028
 
134 tk 1029
static s32 SEQ_UI_Button_ParLayer(s32 depressed, u32 par_layer)
1030
{
1031
  if( par_layer >= 3 ) return -2; // max. 3 parlayer buttons
1032
 
333 tk 1033
  u8 visible_track = SEQ_UI_VisibleTrackGet();
1034
  u8 num_p_layers = SEQ_PAR_NumLayersGet(visible_track);
134 tk 1035
 
333 tk 1036
  if( num_p_layers <= 3 ) {
1037
    // 3 layers: direct selection with LayerA/B/C button
1038
    if( depressed ) return -1; // ignore when button depressed
596 tk 1039
    if( par_layer >= num_p_layers ) {
1040
      char str1[21];
1041
      sprintf(str1, "Parameter Layer %c", 'A'+par_layer);
1042
      SEQ_UI_Msg(SEQ_UI_MSG_USER, 1000, str1, "not available!");
1043
    } else {
1044
      seq_ui_button_state.PAR_LAYER_SEL = 0;
1045
      ui_selected_par_layer = par_layer;
1046
    }
333 tk 1047
  } else if( num_p_layers <= 4 ) {
1048
    // 4 layers: LayerC Button toggles between C and D
1049
    if( depressed ) return -1; // ignore when button depressed
1050
    seq_ui_button_state.PAR_LAYER_SEL = 0;
336 tk 1051
    if( par_layer == 2 )
1052
      ui_selected_par_layer = (ui_selected_par_layer == 2) ? 3 : 2;
333 tk 1053
    else
1054
      ui_selected_par_layer = par_layer;
1055
  } else {
1056
    // >4 layers: LayerA/B button selects directly, Layer C button enters layer selection page
1057
    if( par_layer <= 1 ) {
1058
      if( depressed ) return -1; // ignore when button depressed
1059
      seq_ui_button_state.PAR_LAYER_SEL = 0;
1060
      ui_selected_par_layer = par_layer;
1061
    } else {
513 tk 1062
      return SEQ_UI_Button_ParLayerSel(depressed);
333 tk 1063
    }
1064
  }
1065
 
178 tk 1066
  // set/clear encoder fast function if required
1067
  SEQ_UI_InitEncSpeed(1); // auto config
1068
 
134 tk 1069
  return 0; // no error
1070
}
1071
 
513 tk 1072
static s32 SEQ_UI_Button_TrgLayerSel(s32 depressed)
1073
{
600 tk 1074
  // static seq_ui_page_t prev_page = SEQ_UI_PAGE_NONE;
1075
  // also used by seq_ui_trgsel.c
513 tk 1076
 
1077
  if( seq_hwcfg_button_beh.trg_layer ) {
1078
    if( depressed ) return -1; // ignore when button depressed
678 tk 1079
    if( !seq_ui_button_state.TRG_LAYER_SEL ) // due to page change: button going to be set, clear other toggle buttons
1080
      seq_ui_button_state.PAGE_CHANGE_BUTTON_FLAGS = 0;
513 tk 1081
    seq_ui_button_state.TRG_LAYER_SEL ^= 1; // toggle TRGSEL status (will also be released once GP button has been pressed)
1082
  } else {
1083
    seq_ui_button_state.TRG_LAYER_SEL = depressed ? 0 : 1;
1084
  }
1085
 
1086
  if( seq_ui_button_state.TRG_LAYER_SEL ) {
600 tk 1087
    ui_trglayer_prev_page = ui_page;
513 tk 1088
    SEQ_UI_PageSet(SEQ_UI_PAGE_TRGSEL);
1089
  } else {
524 tk 1090
    if( ui_page == SEQ_UI_PAGE_TRGSEL )
600 tk 1091
      SEQ_UI_PageSet(ui_trglayer_prev_page);
513 tk 1092
  }
1093
 
1094
  return 0; // no error
1095
}
1096
 
134 tk 1097
static s32 SEQ_UI_Button_TrgLayer(s32 depressed, u32 trg_layer)
1098
{
1099
  if( trg_layer >= 3 ) return -2; // max. 3 trglayer buttons
1100
 
326 tk 1101
  u8 visible_track = SEQ_UI_VisibleTrackGet();
1102
  u8 event_mode = SEQ_CC_Get(visible_track, SEQ_CC_MIDI_EVENT_MODE);
1103
  u8 num_t_layers = SEQ_TRG_NumLayersGet(visible_track);
134 tk 1104
 
328 tk 1105
  if( event_mode != SEQ_EVENT_MODE_Drum && num_t_layers <= 3 ) {
326 tk 1106
    // 3 layers: direct selection with LayerA/B/C button
1107
    if( depressed ) return -1; // ignore when button depressed
596 tk 1108
    if( trg_layer >= num_t_layers ) {
1109
      char str1[21];
1110
      sprintf(str1, "Trigger Layer %c", 'A'+trg_layer);
1111
      SEQ_UI_Msg(SEQ_UI_MSG_USER, 1000, str1, "not available!");
1112
    } else {
1113
      seq_ui_button_state.TRG_LAYER_SEL = 0;
1114
      ui_selected_trg_layer = trg_layer;
1115
    }
328 tk 1116
  } else if( event_mode != SEQ_EVENT_MODE_Drum && num_t_layers <= 4 ) {
326 tk 1117
    // 4 layers: LayerC Button toggles between C and D
1118
    if( depressed ) return -1; // ignore when button depressed
1119
    seq_ui_button_state.TRG_LAYER_SEL = 0;
336 tk 1120
    if( trg_layer == 2 )
1121
      ui_selected_trg_layer = (ui_selected_trg_layer == 2) ? 3 : 2;
326 tk 1122
    else
1123
      ui_selected_trg_layer = trg_layer;
1124
  } else {
333 tk 1125
    // >4 layers or drum mode: LayerA/B button selects directly, Layer C button enters trigger selection page
326 tk 1126
    // also used for drum tracks
1127
    if( trg_layer <= 1 ) {
1128
      if( depressed ) return -1; // ignore when button depressed
328 tk 1129
      seq_ui_button_state.TRG_LAYER_SEL = 0;
1130
      ui_selected_trg_layer = trg_layer;
326 tk 1131
    } else {
513 tk 1132
      return SEQ_UI_Button_TrgLayerSel(depressed);
326 tk 1133
    }
1134
  }
1135
 
134 tk 1136
  return 0; // no error
1137
}
1138
 
1139
 
544 tk 1140
static s32 SEQ_UI_Button_Morph(s32 depressed)
1141
{
1142
  if( depressed ) return -1; // ignore when button depressed
134 tk 1143
 
544 tk 1144
  SEQ_UI_PageSet(SEQ_UI_PAGE_TRKMORPH);
1145
 
1146
  return 0; // no error
1147
}
1148
 
1149
static s32 SEQ_UI_Button_Mixer(s32 depressed)
1150
{
1151
  if( depressed ) return -1; // ignore when button depressed
1152
 
1153
  SEQ_UI_PageSet(SEQ_UI_PAGE_MIXER);
1154
 
1155
  return 0; // no error
1156
}
1157
 
1158
static s32 SEQ_UI_Button_Transpose(s32 depressed)
1159
{
1160
  if( depressed ) return -1; // ignore when button depressed
1161
 
1162
  SEQ_UI_PageSet(SEQ_UI_PAGE_TRKTRAN);
1163
 
1164
  return 0; // no error
1165
}
1166
 
1167
 
742 tk 1168
// only used by keyboard remote function
1169
static s32 SEQ_UI_Button_ToggleGate(s32 depressed)
1170
{
1171
  if( depressed ) return -1; // ignore when button depressed
544 tk 1172
 
742 tk 1173
  u8 visible_track = SEQ_UI_VisibleTrackGet();
1174
  u8 gate = SEQ_TRG_GateGet(visible_track, ui_selected_step, ui_selected_instrument) ? 0 : 1;
1175
  SEQ_TRG_GateSet(visible_track, ui_selected_step, ui_selected_instrument, gate);
544 tk 1176
 
742 tk 1177
  return 0; // no error
1178
}
1179
 
1180
 
1181
 
134 tk 1182
/////////////////////////////////////////////////////////////////////////////
1183
// Button handler
1184
/////////////////////////////////////////////////////////////////////////////
1185
s32 SEQ_UI_Button_Handler(u32 pin, u32 pin_value)
1186
{
492 tk 1187
  int i;
1188
 
625 tk 1189
  // send MIDI event in remote mode and exit
626 tk 1190
  if( seq_ui_remote_active_mode == SEQ_UI_REMOTE_MODE_CLIENT )
625 tk 1191
    return SEQ_MIDI_SYSEX_REMOTE_Client_SendButton(pin, pin_value);
1192
 
496 tk 1193
  // ignore so long hardware config hasn't been read
1194
  if( !SEQ_FILE_HW_ConfigLocked() )
1195
    return -1;
1196
 
740 tk 1197
  // ignore during a backup or format is created
1198
  if( seq_ui_backup_req || seq_ui_format_req )
634 tk 1199
    return -1;
1200
 
328 tk 1201
  // ensure that selections are matching with track constraints
1202
  SEQ_UI_CheckSelections();
1203
 
492 tk 1204
  // request display update
1205
  seq_ui_display_update_req = 1;
134 tk 1206
 
596 tk 1207
  // stop current message if (new) button has been pressed
1208
  if( pin_value == 0 )
1209
    SEQ_UI_MsgStop();
134 tk 1210
 
596 tk 1211
 
492 tk 1212
  // MEMO: we could also use a jump table with references to the functions
1213
  // here, but this "spagetthi code" simplifies the configuration and
1214
  // the resulting ASM doesn't look that bad!
168 tk 1215
 
492 tk 1216
  for(i=0; i<SEQ_HWCFG_NUM_GP; ++i)
1217
    if( pin == seq_hwcfg_button.gp[i] )
1218
      return SEQ_UI_Button_GP(pin_value, i);
134 tk 1219
 
492 tk 1220
  for(i=0; i<SEQ_HWCFG_NUM_TRACK; ++i)
1221
    if( pin == seq_hwcfg_button.track[i] )
1222
      return SEQ_UI_Button_Track(pin_value, i);
513 tk 1223
  if( pin == seq_hwcfg_button.track_sel )
1224
    return SEQ_UI_Button_TrackSel(pin_value);
134 tk 1225
 
492 tk 1226
  for(i=0; i<SEQ_HWCFG_NUM_GROUP; ++i)
1227
    if( pin == seq_hwcfg_button.group[i] )
1228
      return SEQ_UI_Button_Group(pin_value, i);
134 tk 1229
 
492 tk 1230
  for(i=0; i<SEQ_HWCFG_NUM_PAR_LAYER; ++i)
1231
    if( pin == seq_hwcfg_button.par_layer[i] )
1232
      return SEQ_UI_Button_ParLayer(pin_value, i);
513 tk 1233
  if( pin == seq_hwcfg_button.par_layer_sel )
1234
    return SEQ_UI_Button_ParLayerSel(pin_value);
134 tk 1235
 
492 tk 1236
  for(i=0; i<SEQ_HWCFG_NUM_TRG_LAYER; ++i)
1237
    if( pin == seq_hwcfg_button.trg_layer[i] )
1238
      return SEQ_UI_Button_TrgLayer(pin_value, i);
513 tk 1239
  if( pin == seq_hwcfg_button.trg_layer_sel )
1240
    return SEQ_UI_Button_TrgLayerSel(pin_value);
134 tk 1241
 
492 tk 1242
  if( pin == seq_hwcfg_button.left )
1243
    return SEQ_UI_Button_Left(pin_value);
1244
  if( pin == seq_hwcfg_button.right )
1245
    return SEQ_UI_Button_Right(pin_value);
1246
  if( pin == seq_hwcfg_button.down )
1247
    return SEQ_UI_Button_Down(pin_value);
1248
  if( pin == seq_hwcfg_button.up )
1249
    return SEQ_UI_Button_Up(pin_value);
134 tk 1250
 
492 tk 1251
  if( pin == seq_hwcfg_button.scrub )
1252
    return SEQ_UI_Button_Scrub(pin_value);
1253
  if( pin == seq_hwcfg_button.metronome )
1254
    return SEQ_UI_Button_Metronome(pin_value);
134 tk 1255
 
630 tk 1256
  if( pin == seq_hwcfg_button.record )
1257
    return SEQ_UI_Button_Record(pin_value);
1258
 
492 tk 1259
  if( pin == seq_hwcfg_button.stop )
1260
    return SEQ_UI_Button_Stop(pin_value);
1261
  if( pin == seq_hwcfg_button.pause )
1262
    return SEQ_UI_Button_Pause(pin_value);
1263
  if( pin == seq_hwcfg_button.play )
1264
    return SEQ_UI_Button_Play(pin_value);
1265
  if( pin == seq_hwcfg_button.rew )
1266
    return SEQ_UI_Button_Rew(pin_value);
1267
  if( pin == seq_hwcfg_button.fwd )
1268
    return SEQ_UI_Button_Fwd(pin_value);
596 tk 1269
  if( pin == seq_hwcfg_button.loop )
1270
    return SEQ_UI_Button_Loop(pin_value);
134 tk 1271
 
492 tk 1272
  if( pin == seq_hwcfg_button.utility )
1273
    return SEQ_UI_Button_Utility(pin_value);
1274
  if( pin == seq_hwcfg_button.copy )
1275
    return SEQ_UI_Button_Copy(pin_value);
1276
  if( pin == seq_hwcfg_button.paste )
1277
    return SEQ_UI_Button_Paste(pin_value);
1278
  if( pin == seq_hwcfg_button.clear )
1279
    return SEQ_UI_Button_Clear(pin_value);
134 tk 1280
 
492 tk 1281
  if( pin == seq_hwcfg_button.menu )
1282
    return SEQ_UI_Button_Menu(pin_value);
1283
  if( pin == seq_hwcfg_button.select )
1284
    return SEQ_UI_Button_Select(pin_value);
1285
  if( pin == seq_hwcfg_button.exit )
1286
    return SEQ_UI_Button_Exit(pin_value);
134 tk 1287
 
513 tk 1288
  if( pin == seq_hwcfg_button.tap_tempo )
1289
    return SEQ_UI_Button_TapTempo(pin_value);
1290
  if( pin == seq_hwcfg_button.tempo_preset )
1291
    return SEQ_UI_Button_TempoPreset(pin_value);
524 tk 1292
  if( pin == seq_hwcfg_button.ext_restart )
1293
    return SEQ_UI_Button_ExtRestart(pin_value);
134 tk 1294
 
492 tk 1295
  if( pin == seq_hwcfg_button.edit )
1296
    return SEQ_UI_Button_Edit(pin_value);
1297
  if( pin == seq_hwcfg_button.mute )
1298
    return SEQ_UI_Button_Mute(pin_value);
1299
  if( pin == seq_hwcfg_button.pattern )
1300
    return SEQ_UI_Button_Pattern(pin_value);
1301
  if( pin == seq_hwcfg_button.song )
1302
    return SEQ_UI_Button_Song(pin_value);
134 tk 1303
 
492 tk 1304
  if( pin == seq_hwcfg_button.solo )
1305
    return SEQ_UI_Button_Solo(pin_value);
1306
  if( pin == seq_hwcfg_button.fast )
1307
    return SEQ_UI_Button_Fast(pin_value);
1308
  if( pin == seq_hwcfg_button.all )
1309
    return SEQ_UI_Button_All(pin_value);
134 tk 1310
 
492 tk 1311
  if( pin == seq_hwcfg_button.step_view )
1312
    return SEQ_UI_Button_StepView(pin_value);
1313
  if( pin == seq_hwcfg_button.tap_tempo )
1314
    return SEQ_UI_Button_TapTempo(pin_value);
134 tk 1315
 
544 tk 1316
  if( pin == seq_hwcfg_button.morph )
1317
    return SEQ_UI_Button_Morph(pin_value);
1318
  if( pin == seq_hwcfg_button.mixer )
1319
    return SEQ_UI_Button_Mixer(pin_value);
1320
  if( pin == seq_hwcfg_button.transpose )
1321
    return SEQ_UI_Button_Transpose(pin_value);
1322
 
625 tk 1323
  // always print debugging message
1324
#if 1
1325
  MIOS32_MIDI_SendDebugMessage("[SEQ_UI_Button_Handler] Button SR:%d, Pin:%d not mapped, it has been %s.\n",
1326
                   (pin >> 3) + 1,
1327
                   pin & 7,
1328
                   pin_value ? "depressed" : "pressed");
1329
#endif
1330
 
492 tk 1331
  return -1; // button not mapped
134 tk 1332
}
1333
 
1334
 
1335
/////////////////////////////////////////////////////////////////////////////
524 tk 1336
// BLM Button handler
1337
/////////////////////////////////////////////////////////////////////////////
1338
s32 SEQ_UI_BLM_Button_Handler(u32 row, u32 pin, u32 pin_value)
1339
{
625 tk 1340
  // send MIDI event in remote mode and exit
626 tk 1341
  if( seq_ui_remote_active_mode == SEQ_UI_REMOTE_MODE_CLIENT )
625 tk 1342
    return SEQ_MIDI_SYSEX_REMOTE_Client_Send_BLM_Button(row, pin, pin_value);
1343
 
524 tk 1344
  // ignore so long hardware config hasn't been read
1345
  if( !SEQ_FILE_HW_ConfigLocked() )
1346
    return -1;
1347
 
740 tk 1348
  // ignore during a backup or format is created
1349
  if( seq_ui_backup_req || seq_ui_format_req )
634 tk 1350
    return -1;
1351
 
524 tk 1352
  if( row >= SEQ_CORE_NUM_TRACKS_PER_GROUP )
1353
    return -1; // more than 4 tracks not supported (yet) - could be done in this function w/o much effort
1354
 
1355
  if( pin >= 16 )
1356
    return -1; // more than 16 step buttons not supported (yet) - could be done by selecting the step view
1357
 
1358
  // select track depending on row
1359
  ui_selected_tracks = 1 << (row + 4*ui_selected_group);
1360
 
1361
  // ensure that selections are matching with track constraints
1362
  SEQ_UI_CheckSelections();
1363
 
1364
  // request display update
1365
  seq_ui_display_update_req = 1;
1366
 
1367
  // emulate general purpose button
1368
  return SEQ_UI_Button_GP(pin_value, pin);
1369
}
1370
 
1371
 
1372
/////////////////////////////////////////////////////////////////////////////
134 tk 1373
// Encoder handler
1374
/////////////////////////////////////////////////////////////////////////////
1375
s32 SEQ_UI_Encoder_Handler(u32 encoder, s32 incrementer)
1376
{
625 tk 1377
  // send MIDI event in remote mode and exit
626 tk 1378
  if( seq_ui_remote_active_mode == SEQ_UI_REMOTE_MODE_CLIENT )
625 tk 1379
    return SEQ_MIDI_SYSEX_REMOTE_Client_SendEncoder(encoder, incrementer);
1380
 
496 tk 1381
  // ignore so long hardware config hasn't been read
1382
  if( !SEQ_FILE_HW_ConfigLocked() )
1383
    return -1;
1384
 
740 tk 1385
  // ignore during a backup or format is created
1386
  if( seq_ui_backup_req || seq_ui_format_req )
634 tk 1387
    return -1;
1388
 
134 tk 1389
  if( encoder > 16 )
1390
    return -1; // encoder doesn't exist
1391
 
328 tk 1392
  // ensure that selections are matching with track constraints
1393
  SEQ_UI_CheckSelections();
1394
 
596 tk 1395
  // stop current message
1396
  SEQ_UI_MsgStop();
1397
 
134 tk 1398
  // limit incrementer
1399
  if( incrementer > 3 )
1400
    incrementer = 3;
1401
  else if( incrementer < -3 )
1402
    incrementer = -3;
1403
 
607 tk 1404
  if( seq_ui_button_state.SCRUB && encoder == 0 ) {
1405
    // if sequencer isn't already running, continue it (don't restart)
1406
    if( !SEQ_BPM_IsRunning() )
1407
      SEQ_BPM_Cont();
1408
    ui_seq_pause = 0; // clear pause mode
1409
    // Scrub sequence back or forth
1410
    portENTER_CRITICAL(); // should be atomic
1411
    SEQ_CORE_Scrub(incrementer);
1412
    portEXIT_CRITICAL();
1413
  } else if( !seq_ui_button_state.MENU_PRESSED && ui_encoder_callback != NULL ) {
173 tk 1414
    ui_encoder_callback((encoder == 0) ? SEQ_UI_ENCODER_Datawheel : (encoder-1), incrementer);
740 tk 1415
    ui_cursor_flash_ctr = ui_cursor_flash_overrun_ctr = 0; // ensure that value is visible when it has been changed
134 tk 1416
  }
1417
 
1418
  // request display update
159 tk 1419
  seq_ui_display_update_req = 1;
134 tk 1420
 
1421
  return 0; // no error
1422
}
1423
 
1424
 
353 tk 1425
 
134 tk 1426
/////////////////////////////////////////////////////////////////////////////
626 tk 1427
// Receives a MIDI package from APP_NotifyReceivedEvent (-> app.c)
1428
/////////////////////////////////////////////////////////////////////////////
1429
s32 SEQ_UI_REMOTE_MIDI_Receive(mios32_midi_port_t port, mios32_midi_package_t midi_package)
1430
{
1431
#if 1
1432
  // check for active remote mode
1433
  if( seq_ui_remote_active_mode != SEQ_UI_REMOTE_MODE_SERVER )
1434
    return 0; // no error
1435
#endif
1436
 
1437
  if( (seq_ui_remote_port == DEFAULT && seq_ui_remote_active_port != port) ||
1438
      (seq_ui_remote_port != DEFAULT && port != seq_ui_remote_port) )
1439
    return 0; // wrong port
1440
 
1441
  // for easier parsing: convert Note Off -> Note On with velocity 0
1442
  if( midi_package.event == NoteOff ) {
1443
    midi_package.event = NoteOn;
1444
    midi_package.velocity = 0;
1445
  }
1446
 
1447
  switch( midi_package.event ) {
1448
    case NoteOn: {
1449
      switch( midi_package.chn ) {
1450
        case Chn1:
1451
      SEQ_UI_Button_Handler(midi_package.note + 0x00, midi_package.velocity ? 0 : 1);
1452
      break;
1453
        case Chn2:
1454
      SEQ_UI_Button_Handler(midi_package.note + 0x80, midi_package.velocity ? 0 : 1);
1455
      break;
1456
        case Chn3:
1457
      SEQ_UI_BLM_Button_Handler(midi_package.note >> 5, midi_package.note & 0x1f, midi_package.velocity ? 0 : 1);
1458
      break;
1459
      }
1460
    } break;
1461
 
1462
    case CC: {
1463
      if( midi_package.cc_number >= 15 && midi_package.cc_number <= 31 )
1464
    SEQ_UI_Encoder_Handler(midi_package.cc_number-15, (int)midi_package.value - 0x40);
1465
    } break;
1466
  }
1467
 
632 tk 1468
  return 1; // MIDI event has been taken for remote function -> don't forward to router/MIDI event parser
626 tk 1469
}
1470
 
1471
 
1472
/////////////////////////////////////////////////////////////////////////////
741 tk 1473
// MIDI Remote Keyboard Function (called from SEQ_MIDI_IN)
1474
/////////////////////////////////////////////////////////////////////////////
742 tk 1475
s32 SEQ_UI_REMOTE_MIDI_Keyboard(u8 key, u8 depressed)
741 tk 1476
{
742 tk 1477
#if 0
1478
  MIOS32_MIDI_SendDebugMessage("SEQ_UI_REMOTE_MIDI_Keyboard(%d, %d)\n", key, depressed);
741 tk 1479
#endif
742 tk 1480
 
1481
  switch( key ) {
1482
    case 0x24: // C-2
1483
      return SEQ_UI_Button_GP(depressed, 0);
1484
    case 0x25: // C#2
1485
      return SEQ_UI_Button_Track(depressed, 0);
1486
    case 0x26: // D-2
1487
      return SEQ_UI_Button_GP(depressed, 1);
1488
    case 0x27: // D#2
1489
      return SEQ_UI_Button_Track(depressed, 1);
1490
    case 0x28: // E-2
1491
      return SEQ_UI_Button_GP(depressed, 2);
1492
    case 0x29: // F-2
1493
      return SEQ_UI_Button_GP(depressed, 3);
1494
    case 0x2a: // F#2
1495
      return SEQ_UI_Button_Track(depressed, 2);
1496
    case 0x2b: // G-2
1497
      return SEQ_UI_Button_GP(depressed, 4);
1498
    case 0x2c: // G#2
1499
      return SEQ_UI_Button_Track(depressed, 3);
1500
    case 0x2d: // A-2
1501
      return SEQ_UI_Button_GP(depressed, 5);
1502
    case 0x2e: // A#2
1503
      return SEQ_UI_Button_ParLayer(depressed, 0);
1504
    case 0x2f: // B-2
1505
      return SEQ_UI_Button_GP(depressed, 6);
1506
 
1507
    case 0x30: // C-3
1508
      return SEQ_UI_Button_GP(depressed, 7);
1509
    case 0x31: // C#3
1510
      return SEQ_UI_Button_ParLayer(depressed, 1);
1511
    case 0x32: // D-3
1512
      return SEQ_UI_Button_GP(depressed, 8);
1513
    case 0x33: // D#3
1514
      return SEQ_UI_Button_ParLayer(depressed, 2);
1515
    case 0x34: // E-3
1516
      return SEQ_UI_Button_GP(depressed, 9);
1517
    case 0x35: // F-3
1518
      return SEQ_UI_Button_GP(depressed, 10);
1519
    case 0x36: // F#3
1520
      return SEQ_UI_Button_TrgLayer(depressed, 0);
1521
    case 0x37: // G-3
1522
      return SEQ_UI_Button_GP(depressed, 11);
1523
    case 0x38: // G#3
1524
      return SEQ_UI_Button_TrgLayer(depressed, 1);
1525
    case 0x39: // A-3
1526
      return SEQ_UI_Button_GP(depressed, 12);
1527
    case 0x3a: // A#3
1528
      return SEQ_UI_Button_TrgLayer(depressed, 2);
1529
    case 0x3b: // B-3
1530
      return SEQ_UI_Button_GP(depressed, 13);
1531
 
1532
    case 0x3c: // C-4
1533
      return SEQ_UI_Button_GP(depressed, 14);
1534
    case 0x3d: // C#4
1535
      return SEQ_UI_Button_Group(depressed, 0);
1536
    case 0x3e: // D-4
1537
      return SEQ_UI_Button_GP(depressed, 15);
1538
    case 0x3f: // D#4
1539
      return SEQ_UI_Button_Group(depressed, 1);
1540
    case 0x40: // E-4
1541
      return 0; // ignore
1542
    case 0x41: // F-4
1543
      return SEQ_UI_Button_StepView(depressed);
1544
    case 0x42: // F#4
1545
      return SEQ_UI_Button_Group(depressed, 1);
1546
    case 0x43: // G-4
1547
      return 0; // ignore
1548
    case 0x44: // G#4
1549
      return SEQ_UI_Button_Group(depressed, 2);
1550
    case 0x45: // A-4
1551
      return SEQ_UI_Button_Left(depressed);
1552
    case 0x46: // A#4
1553
      return SEQ_UI_Button_ToggleGate(depressed);
1554
    case 0x47: // B-4
1555
      return SEQ_UI_Button_Right(depressed);
1556
 
1557
    case 0x48: // C-5
1558
      return SEQ_UI_Button_Edit(depressed);
1559
    case 0x49: // C#5
1560
      return SEQ_UI_Button_Solo(depressed);
1561
    case 0x4a: // D-5
1562
      return SEQ_UI_Button_Mute(depressed);
1563
    case 0x4b: // D#5
1564
      return SEQ_UI_Button_All(depressed);
1565
    case 0x4c: // E-5
1566
      return SEQ_UI_Button_Pattern(depressed);
1567
    case 0x4d: // F-5
1568
      return SEQ_UI_Button_Song(depressed);
1569
    case 0x4e: // F#5
1570
      return SEQ_UI_Button_Fast(depressed);
1571
    case 0x4f: // G-5
1572
      return SEQ_UI_Button_Metronome(depressed);
1573
    case 0x50: // G#5
1574
      return SEQ_UI_Button_ExtRestart(depressed);
1575
    case 0x51: // A-5
1576
      return SEQ_UI_Button_ParLayerSel(depressed);
1577
    case 0x52: // A#5
1578
      return SEQ_UI_Button_TrackSel(depressed);
1579
    case 0x53: // B-5
1580
      return SEQ_UI_Button_Stop(depressed);
1581
 
1582
    case 0x54: // C-6
1583
      return SEQ_UI_Button_Play(depressed);
1584
    case 0x55: // C#6
1585
      return SEQ_UI_Button_Pause(depressed);
1586
    case 0x56: // D-6
1587
      return SEQ_UI_Button_Rew(depressed);
1588
    case 0x57: // D#6
1589
      return SEQ_UI_Button_Fwd(depressed);
1590
    case 0x58: // E-6
1591
      return SEQ_UI_Button_Utility(depressed);
1592
    case 0x59: // F-6
1593
      return SEQ_UI_Button_TempoPreset(depressed);
1594
    case 0x5a: // F#6
1595
      return 0; // ignore
1596
    case 0x5b: // G-6
1597
      return SEQ_UI_Button_Menu(depressed);
1598
    case 0x5c: // G#6
1599
      return SEQ_UI_Button_Select(depressed);
1600
    case 0x5d: // A-6
1601
      return SEQ_UI_Button_Exit(depressed);
1602
    case 0x5e: // A#6
1603
      return SEQ_UI_Button_Down(depressed);
1604
    case 0x5f: // B-6
1605
      return SEQ_UI_Button_Up(depressed);
1606
  }
1607
 
741 tk 1608
  return 0; // no error
1609
}
1610
 
1611
 
1612
/////////////////////////////////////////////////////////////////////////////
134 tk 1613
// Update LCD messages
1614
// Usually called from background task
1615
/////////////////////////////////////////////////////////////////////////////
1616
s32 SEQ_UI_LCD_Handler(void)
1617
{
728 tk 1618
  static u8 boot_animation_wait_ctr = 0;
1619
  static u8 boot_animation_lcd_pos = 0;
1620
 
625 tk 1621
  // special handling in remote client mode
626 tk 1622
  if( seq_ui_remote_active_mode == SEQ_UI_REMOTE_MODE_CLIENT )
625 tk 1623
    return SEQ_UI_LCD_Update();
1624
 
159 tk 1625
  if( seq_ui_display_init_req ) {
1626
    seq_ui_display_init_req = 0; // clear request
134 tk 1627
 
278 tk 1628
    // clear force update of LCD
1629
    SEQ_LCD_Clear();
1630
    SEQ_LCD_Update(1);
134 tk 1631
 
168 tk 1632
    // select first menu item
1633
    ui_selected_item = 0;
1634
 
167 tk 1635
    // call init function of current page
272 tk 1636
    if( ui_menu_pages[ui_page].init_callback != NULL )
1637
      ui_menu_pages[ui_page].init_callback(0); // mode
134 tk 1638
 
1639
    // request display update
159 tk 1640
    seq_ui_display_update_req = 1;
134 tk 1641
  }
1642
 
496 tk 1643
  // print boot screen so long hardware config hasn't been read
1644
  if( !SEQ_FILE_HW_ConfigLocked() ) {
728 tk 1645
    if( boot_animation_lcd_pos < (40-3) ) {
1646
      if( ++boot_animation_wait_ctr >= 75 ) {
1647
    boot_animation_wait_ctr = 0;
637 tk 1648
 
728 tk 1649
    if( boot_animation_lcd_pos == 0 ) {
1650
      SEQ_LCD_Clear();
1651
      SEQ_LCD_CursorSet(0, 0);
1652
      SEQ_LCD_PrintString(MIOS32_LCD_BOOT_MSG_LINE1 " " MIOS32_LCD_BOOT_MSG_LINE2);
1653
      SEQ_LCD_CursorSet(0, 1);
1654
      SEQ_LCD_PrintString("Searching for SD Card...");
1655
    }
1656
 
1657
    // logo is print on second LCD
1658
    SEQ_LCD_LOGO_Print(boot_animation_lcd_pos++);
1659
      }
1660
    }
740 tk 1661
  } else if( seq_ui_backup_req || seq_ui_format_req ) {
634 tk 1662
    SEQ_LCD_Clear();
1663
    SEQ_LCD_CursorSet(0, 0);
740 tk 1664
    //                     <-------------------------------------->
1665
    //                     0123456789012345678901234567890123456789
1666
    if( seq_ui_backup_req )
1667
      SEQ_LCD_PrintString("Creating File Backup - be patient!!!");
1668
    else if( seq_ui_format_req )
1669
      SEQ_LCD_PrintString("Formatting Files - be patient!!!");
1670
    else
1671
      SEQ_LCD_PrintString("Don't know what I'm doing! :-/");
637 tk 1672
 
1673
    if( seq_file_backup_notification != NULL ) {
1674
      int i;
1675
 
1676
      SEQ_LCD_CursorSet(0, 1);
634 tk 1677
      SEQ_LCD_PrintFormattedString("Creating '%s'", seq_file_backup_notification);
637 tk 1678
 
1679
      SEQ_LCD_CursorSet(40+3, 0);
1680
      SEQ_LCD_PrintString("Total: [");
1681
      for(i=0; i<20; ++i)
1682
    SEQ_LCD_PrintChar((i>(seq_file_backup_percentage/5)) ? ' ' : '#');
1683
      SEQ_LCD_PrintFormattedString("] %3d%%", seq_file_backup_percentage);
1684
 
1685
      SEQ_LCD_CursorSet(40+3, 1);
1686
      SEQ_LCD_PrintString("File:  [");
1687
      for(i=0; i<20; ++i)
1688
    SEQ_LCD_PrintChar((i>(seq_file_copy_percentage/5)) ? ' ' : '#');
1689
      SEQ_LCD_PrintFormattedString("] %3d%%", seq_file_copy_percentage);
1690
    }
1691
 
496 tk 1692
  } else if( seq_ui_button_state.MENU_PRESSED && !seq_ui_button_state.MENU_FIRST_PAGE_SELECTED ) {
1693
    SEQ_LCD_CursorSet(0, 0);
1694
    //                   <-------------------------------------->
1695
    //                   0123456789012345678901234567890123456789
306 tk 1696
    SEQ_LCD_PrintString("Menu Shortcuts:");
1697
    SEQ_LCD_PrintSpaces(25 + 40);
1698
    SEQ_LCD_CursorSet(0, 1);
1699
    SEQ_LCD_PrintString(UI_SHORTCUT_STR); // defined in seq_ui_pages.inc
1700
  } else {
1701
    // perform high priority LCD update request
1702
    if( ui_lcd_callback != NULL )
1703
      ui_lcd_callback(1); // high_prio
167 tk 1704
 
306 tk 1705
    // perform low priority LCD update request if requested
1706
    if( seq_ui_display_update_req ) {
1707
      seq_ui_display_update_req = 0; // clear request
328 tk 1708
 
1709
      // ensure that selections are matching with track constraints
1710
      SEQ_UI_CheckSelections();
1711
 
306 tk 1712
      if( ui_lcd_callback != NULL )
1713
    ui_lcd_callback(0); // no high_prio
1714
    }
134 tk 1715
  }
1716
 
353 tk 1717
  // transfer all changed characters to LCD
1718
  SEQ_UI_LCD_Update();
1719
 
1720
  return 0; // no error
1721
}
1722
 
1723
 
1724
/////////////////////////////////////////////////////////////////////////////
1725
// Called from SEQ_UI_LCD_Handler(), but optionally also from other tasks
1726
// to update the LCD screen immediately
1727
/////////////////////////////////////////////////////////////////////////////
1728
s32 SEQ_UI_LCD_Update(void)
1729
{
625 tk 1730
  // special handling in remote client mode
626 tk 1731
  if( seq_ui_remote_active_mode == SEQ_UI_REMOTE_MODE_CLIENT ) {
1732
    MIOS32_IRQ_Disable();
1733
    u8 force = seq_ui_remote_force_lcd_update;
1734
    seq_ui_remote_force_lcd_update = 0;
1735
    MIOS32_IRQ_Enable();
1736
    return SEQ_LCD_Update(force);
625 tk 1737
  }
1738
 
596 tk 1739
  // if UI message active: copy over the text
1740
  if( ui_msg_ctr ) {
1741
    const char *animation_l_ptr;
1742
    const char *animation_r_ptr;
1743
    u8 msg_x = 0;
1744
    u8 right_aligned = 0;
740 tk 1745
    u8 disable_message = 0;
299 tk 1746
 
596 tk 1747
    switch( ui_msg_type ) {
1748
      case SEQ_UI_MSG_SDCARD: {
1749
    //                             00112233
1750
    const char animation_l[2*4] = "   >>>> ";
1751
    //                             00112233
1752
    const char animation_r[2*4] = "  < << <";
1753
    animation_l_ptr = animation_l;
1754
    animation_r_ptr = animation_r;
1755
    msg_x = 0; // MEMO: print such important information at first LCD for the case the user hasn't connected the second LCD yet
1756
    right_aligned = 0;
1757
      } break;
1758
 
740 tk 1759
      case SEQ_UI_MSG_DELAYED_ACTION: {
1760
    //                             00112233
1761
    const char animation_l[2*4] = "   )))) ";
1762
    //                             00112233
1763
    const char animation_r[2*4] = "  ( (( (";
1764
    animation_l_ptr = animation_l;
1765
    animation_r_ptr = animation_r;
1766
    msg_x = 39;
1767
    right_aligned = 1;
1768
 
1769
    if( ui_delayed_action_callback == NULL ) {
1770
      disable_message = 1; // button has been depressed before delay
1771
    } else {
1772
      int seconds = (ui_delayed_action_ctr / 1000) + 1;
1773
      if( seconds == 1 )
1774
        sprintf(ui_msg[0], "Hold for 1 second ");
1775
      else
1776
        sprintf(ui_msg[0], "Hold for %d seconds", seconds);
1777
    }
1778
      } break;
1779
 
741 tk 1780
      case SEQ_UI_MSG_USER_R: {
1781
    //                             00112233
1782
    const char animation_l[2*4] = "   **** ";
1783
    //                             00112233
1784
    const char animation_r[2*4] = "  * ** *";
1785
    animation_l_ptr = animation_l;
1786
    animation_r_ptr = animation_r;
1787
    msg_x = 40; // right display
1788
    right_aligned = 0;
1789
      } break;
1790
 
596 tk 1791
      default: {
1792
    //                             00112233
1793
    const char animation_l[2*4] = "   **** ";
1794
    //                             00112233
1795
    const char animation_r[2*4] = "  * ** *";
1796
    animation_l_ptr = animation_l;
1797
    animation_r_ptr = animation_r;
1798
    msg_x = 39;
1799
    right_aligned = 1;
1800
      } break;
1801
 
1802
    }
1803
 
740 tk 1804
    if( !disable_message ) {
1805
      int anum = (ui_msg_ctr % 1000) / 250;
596 tk 1806
 
740 tk 1807
      int len[2];
1808
      len[0] = strlen((char *)ui_msg[0]);
1809
      len[1] = strlen((char *)ui_msg[1]);
1810
      int len_max = len[0];
1811
      if( len[1] > len_max )
1812
    len_max = len[1];
596 tk 1813
 
740 tk 1814
      if( right_aligned )
1815
    msg_x -= (9 + len_max);
596 tk 1816
 
740 tk 1817
      int line;
1818
      for(line=0; line<2; ++line) {
1819
    SEQ_LCD_CursorSet(msg_x, line);
596 tk 1820
 
740 tk 1821
    // ensure that both lines are padded with same number of spaces
1822
    int end_pos = len[line];
1823
    while( end_pos < len_max )
1824
      ui_msg[line][end_pos++] = ' ';
1825
    ui_msg[line][end_pos] = 0;
1826
 
1827
    SEQ_LCD_PrintFormattedString(" %c%c| %s |%c%c ",
1828
                     *(animation_l_ptr + 2*anum + 0), *(animation_l_ptr + 2*anum + 1),
1829
                     (char *)ui_msg[line],
1830
                     *(animation_r_ptr + 2*anum + 0), *(animation_r_ptr + 2*anum + 1));
1831
      }
299 tk 1832
    }
1833
  }
1834
 
637 tk 1835
  // MSD USB notification at right corner if not in Disk page
1836
  // to warn user that USB MIDI is disabled and seq performance is bad now!
638 tk 1837
  if( (TASK_MSD_EnableGet() > 0) && ui_page != SEQ_UI_PAGE_DISK ) {
637 tk 1838
    SEQ_LCD_CursorSet(80-11, 0);
1839
    if( ui_cursor_flash ) SEQ_LCD_PrintSpaces(13); else SEQ_LCD_PrintString(" [MSD USB] ");
1840
    SEQ_LCD_CursorSet(80-11, 1);
1841
    char str[5];
1842
    TASK_MSD_FlagStrGet(str);
1843
    if( ui_cursor_flash ) SEQ_LCD_PrintSpaces(13); else SEQ_LCD_PrintFormattedString(" [ %s  ] ", str);
1844
  }
596 tk 1845
 
278 tk 1846
  // transfer all changed characters to LCD
353 tk 1847
  // SEQ_LCD_Update provides a MUTEX handling to allow updates from different tasks
1848
  return SEQ_LCD_Update(0);
134 tk 1849
}
1850
 
1851
 
1852
/////////////////////////////////////////////////////////////////////////////
1853
// Update all LEDs
1854
// Usually called from background task
1855
/////////////////////////////////////////////////////////////////////////////
1856
s32 SEQ_UI_LED_Handler(void)
1857
{
626 tk 1858
  static u8 remote_led_sr[SEQ_LED_NUM_SR];
1859
 
524 tk 1860
  int i;
1861
 
625 tk 1862
  // ignore in remote client mode
626 tk 1863
  if( seq_ui_remote_active_mode == SEQ_UI_REMOTE_MODE_CLIENT )
625 tk 1864
    return 0; // no error
1865
 
496 tk 1866
  // ignore so long hardware config hasn't been read
1867
  if( !SEQ_FILE_HW_ConfigLocked() )
1868
    return -1;
1869
 
134 tk 1870
  // track LEDs
484 tk 1871
  u8 selected_tracks = ui_selected_tracks >> (4*ui_selected_group);
492 tk 1872
  SEQ_LED_PinSet(seq_hwcfg_led.track[0], (selected_tracks & (1 << 0)));
1873
  SEQ_LED_PinSet(seq_hwcfg_led.track[1], (selected_tracks & (1 << 1)));
1874
  SEQ_LED_PinSet(seq_hwcfg_led.track[2], (selected_tracks & (1 << 2)));
1875
  SEQ_LED_PinSet(seq_hwcfg_led.track[3], (selected_tracks & (1 << 3)));
678 tk 1876
  SEQ_LED_PinSet(seq_hwcfg_led.track_sel, ui_page == SEQ_UI_PAGE_TRACKSEL);
134 tk 1877
 
1878
  // parameter layer LEDs
492 tk 1879
  SEQ_LED_PinSet(seq_hwcfg_led.par_layer[0], (ui_selected_par_layer == 0));
1880
  SEQ_LED_PinSet(seq_hwcfg_led.par_layer[1], (ui_selected_par_layer == 1));
1881
  SEQ_LED_PinSet(seq_hwcfg_led.par_layer[2], (ui_selected_par_layer >= 2) || seq_ui_button_state.PAR_LAYER_SEL);
678 tk 1882
  SEQ_LED_PinSet(seq_hwcfg_led.par_layer_sel, ui_page == SEQ_UI_PAGE_PARSEL);
134 tk 1883
 
1884
  // group LEDs
492 tk 1885
  SEQ_LED_PinSet(seq_hwcfg_led.group[0], (ui_selected_group == 0));
1886
  SEQ_LED_PinSet(seq_hwcfg_led.group[1], (ui_selected_group == 1));
1887
  SEQ_LED_PinSet(seq_hwcfg_led.group[2], (ui_selected_group == 2));
1888
  SEQ_LED_PinSet(seq_hwcfg_led.group[3], (ui_selected_group == 3));
134 tk 1889
 
1890
  // trigger layer LEDs
492 tk 1891
  SEQ_LED_PinSet(seq_hwcfg_led.trg_layer[0], (ui_selected_trg_layer == 0));
1892
  SEQ_LED_PinSet(seq_hwcfg_led.trg_layer[1], (ui_selected_trg_layer == 1));
1893
  SEQ_LED_PinSet(seq_hwcfg_led.trg_layer[2], (ui_selected_trg_layer >= 2) || seq_ui_button_state.TRG_LAYER_SEL);
678 tk 1894
  SEQ_LED_PinSet(seq_hwcfg_led.trg_layer_sel, ui_page == SEQ_UI_PAGE_TRGSEL);
134 tk 1895
 
1896
  // remaining LEDs
492 tk 1897
  SEQ_LED_PinSet(seq_hwcfg_led.edit, ui_page == SEQ_UI_PAGE_EDIT);
1898
  SEQ_LED_PinSet(seq_hwcfg_led.mute, ui_page == SEQ_UI_PAGE_MUTE);
1899
  SEQ_LED_PinSet(seq_hwcfg_led.pattern, ui_page == SEQ_UI_PAGE_PATTERN);
399 tk 1900
  if( SEQ_SONG_ActiveGet() )
492 tk 1901
    SEQ_LED_PinSet(seq_hwcfg_led.song, 1);
399 tk 1902
  else
492 tk 1903
    SEQ_LED_PinSet(seq_hwcfg_led.song, ui_cursor_flash ? 0 : (ui_page == SEQ_UI_PAGE_SONG));
134 tk 1904
 
492 tk 1905
  SEQ_LED_PinSet(seq_hwcfg_led.solo, seq_ui_button_state.SOLO);
1906
  SEQ_LED_PinSet(seq_hwcfg_led.fast, seq_ui_button_state.FAST_ENCODERS);
1907
  SEQ_LED_PinSet(seq_hwcfg_led.all, seq_ui_button_state.CHANGE_ALL_STEPS);
134 tk 1908
 
492 tk 1909
  SEQ_LED_PinSet(seq_hwcfg_led.play, SEQ_BPM_IsRunning());
1910
  SEQ_LED_PinSet(seq_hwcfg_led.stop, !SEQ_BPM_IsRunning() && !ui_seq_pause);
1911
  SEQ_LED_PinSet(seq_hwcfg_led.pause, ui_seq_pause);
240 tk 1912
 
492 tk 1913
  SEQ_LED_PinSet(seq_hwcfg_led.rew, seq_ui_button_state.REW);
1914
  SEQ_LED_PinSet(seq_hwcfg_led.fwd, seq_ui_button_state.FWD);
596 tk 1915
 
1916
  SEQ_LED_PinSet(seq_hwcfg_led.loop, seq_core_state.LOOP);
134 tk 1917
 
678 tk 1918
  SEQ_LED_PinSet(seq_hwcfg_led.step_view, ui_page == SEQ_UI_PAGE_STEPSEL);
134 tk 1919
 
591 tk 1920
  SEQ_LED_PinSet(seq_hwcfg_led.exit, seq_ui_button_state.EXIT_PRESSED);
1921
  SEQ_LED_PinSet(seq_hwcfg_led.select, seq_ui_button_state.SELECT_PRESSED);
492 tk 1922
  SEQ_LED_PinSet(seq_hwcfg_led.menu, seq_ui_button_state.MENU_PRESSED);
134 tk 1923
 
596 tk 1924
  // handle double functions
1925
  if( seq_ui_button_state.MENU_PRESSED ) {
1926
    SEQ_LED_PinSet(seq_hwcfg_led.scrub, seq_core_state.LOOP);
1927
    SEQ_LED_PinSet(seq_hwcfg_led.metronome, seq_core_state.EXT_RESTART_REQ);
1928
  } else {
1929
    SEQ_LED_PinSet(seq_hwcfg_led.scrub, seq_ui_button_state.SCRUB);
1930
    SEQ_LED_PinSet(seq_hwcfg_led.metronome, seq_core_state.METRONOME);
1931
  }
1932
 
630 tk 1933
  SEQ_LED_PinSet(seq_hwcfg_led.record, ui_page == SEQ_UI_PAGE_TRKREC);
1934
 
492 tk 1935
  SEQ_LED_PinSet(seq_hwcfg_led.utility, ui_page == SEQ_UI_PAGE_UTIL);
1936
  SEQ_LED_PinSet(seq_hwcfg_led.copy, seq_ui_button_state.COPY);
1937
  SEQ_LED_PinSet(seq_hwcfg_led.paste, seq_ui_button_state.PASTE);
1938
  SEQ_LED_PinSet(seq_hwcfg_led.clear, seq_ui_button_state.CLEAR);
240 tk 1939
 
513 tk 1940
  SEQ_LED_PinSet(seq_hwcfg_led.tap_tempo, seq_ui_button_state.TAP_TEMPO);
678 tk 1941
  SEQ_LED_PinSet(seq_hwcfg_led.tempo_preset, ui_page == SEQ_UI_PAGE_BPM_PRESETS);
524 tk 1942
  SEQ_LED_PinSet(seq_hwcfg_led.ext_restart, seq_core_state.EXT_RESTART_REQ);
240 tk 1943
 
492 tk 1944
  SEQ_LED_PinSet(seq_hwcfg_led.down, seq_ui_button_state.DOWN);
1945
  SEQ_LED_PinSet(seq_hwcfg_led.up, seq_ui_button_state.UP);
240 tk 1946
 
167 tk 1947
 
306 tk 1948
  // in MENU page: overrule GP LEDs so long MENU button is pressed/active
1949
  if( seq_ui_button_state.MENU_PRESSED ) {
1950
    if( ui_cursor_flash ) // if flashing flag active: no LED flag set
1951
      ui_gp_leds = 0x0000;
1952
    else {
1953
      int i;
1954
      u16 new_ui_gp_leds = 0x0000;
1955
      for(i=0; i<16; ++i)
1956
    if( ui_page == ui_shortcut_menu_pages[i] )
1957
      new_ui_gp_leds |= (1 << i);
1958
      ui_gp_leds = new_ui_gp_leds;
1959
    }
1960
  } else {
1961
    // note: the background function is permanently interrupted - therefore we write the GP pattern
1962
    // into a temporary variable, and take it over once completed
1963
    u16 new_ui_gp_leds = 0x0000;
1964
    // request GP LED values from current menu page
1965
    // will be transfered to DOUT registers in SEQ_UI_LED_Handler_Periodic
1966
    new_ui_gp_leds = 0x0000;
206 tk 1967
 
306 tk 1968
    if( ui_led_callback != NULL )
1969
      ui_led_callback(&new_ui_gp_leds);
167 tk 1970
 
306 tk 1971
    ui_gp_leds = new_ui_gp_leds;
1972
  }
1973
 
524 tk 1974
 
1975
  // BLM LEDs
1976
  u8 visible_track0 = 4*ui_selected_group;
1977
  u8 visible_sr0  = 2*ui_selected_step_view;
1978
 
1979
  for(i=0; i<NUM_BLM_LED_SRS; ++i)
1980
    ui_blm_leds[i] = SEQ_TRG_Get8(visible_track0+(i>>1), visible_sr0+(i&1), ui_selected_trg_layer, ui_selected_instrument);
1981
 
626 tk 1982
 
1983
  // send LED changes in remote server mode
1984
  if( seq_ui_remote_mode == SEQ_UI_REMOTE_MODE_SERVER || seq_ui_remote_active_mode == SEQ_UI_REMOTE_MODE_SERVER ) {
1985
    int first_sr = -1;
1986
    int last_sr = -1;
1987
    int sr;
1988
    for(sr=0; sr<SEQ_LED_NUM_SR; ++sr) {
1989
      u8 value = SEQ_LED_SRGet(sr);
1990
      if( value != remote_led_sr[sr] ) {
1991
    if( first_sr == -1 )
1992
      first_sr = sr;
1993
    last_sr = sr;
1994
    remote_led_sr[sr] = value;
1995
      }
1996
    }
1997
 
1998
    MIOS32_IRQ_Disable();
1999
    if( seq_ui_remote_force_led_update ) {
2000
      first_sr = 0;
2001
      last_sr = SEQ_LED_NUM_SR-1;
2002
    }
2003
    seq_ui_remote_force_led_update = 0;
2004
    MIOS32_IRQ_Enable();
2005
 
2006
    if( first_sr >= 0 )
2007
      SEQ_MIDI_SYSEX_REMOTE_Server_SendLED(first_sr, (u8 *)&remote_led_sr[first_sr], last_sr-first_sr+1);
2008
  }
2009
 
2010
 
134 tk 2011
  return 0; // no error
2012
}
2013
 
2014
 
2015
/////////////////////////////////////////////////////////////////////////////
2016
// updates high-prio LED functions (GP LEDs and Beat LED)
168 tk 2017
// called each mS
134 tk 2018
/////////////////////////////////////////////////////////////////////////////
2019
s32 SEQ_UI_LED_Handler_Periodic()
2020
{
625 tk 2021
  // ignore in remote client mode
626 tk 2022
  if( seq_ui_remote_active_mode == SEQ_UI_REMOTE_MODE_CLIENT )
625 tk 2023
    return 0; // no error
2024
 
496 tk 2025
  // ignore so long hardware config hasn't been read
2026
  if( !SEQ_FILE_HW_ConfigLocked() )
2027
    return -1;
2028
 
134 tk 2029
  // GP LEDs are only updated when ui_gp_leds or pos_marker_mask has changed
2030
  static u16 prev_ui_gp_leds = 0x0000;
2031
  static u16 prev_pos_marker_mask = 0x0000;
2032
 
2033
  // beat LED: tmp. for demo w/o real sequencer
193 tk 2034
  u8 sequencer_running = SEQ_BPM_IsRunning();
492 tk 2035
  SEQ_LED_PinSet(seq_hwcfg_led.beat, sequencer_running && ((seq_core_state.ref_step & 3) == 0));
134 tk 2036
 
2037
  // for song position marker (supports 16 LEDs, check for selected step view)
2038
  u16 pos_marker_mask = 0x0000;
325 tk 2039
  u8 visible_track = SEQ_UI_VisibleTrackGet();
2040
  u8 played_step = seq_core_trk[visible_track].step;
513 tk 2041
  if( seq_ui_button_state.STEP_VIEW ) {
2042
    // if STEP_VIEW button pressed: pos marker correlated to zoom ratio
325 tk 2043
    if( sequencer_running )
2044
      pos_marker_mask = 1 << (played_step / (SEQ_TRG_NumStepsGet(visible_track)/16));
2045
  } else {
2046
    if( sequencer_running && (played_step >> 4) == ui_selected_step_view )
2047
      pos_marker_mask = 1 << (played_step & 0xf);
2048
  }
134 tk 2049
 
2050
  // exit of pattern hasn't changed
173 tk 2051
  if( prev_ui_gp_leds == ui_gp_leds && prev_pos_marker_mask == pos_marker_mask )
134 tk 2052
    return 0;
173 tk 2053
  prev_ui_gp_leds = ui_gp_leds;
134 tk 2054
  prev_pos_marker_mask = pos_marker_mask;
2055
 
2056
  // transfer to GP LEDs
514 tk 2057
  if( seq_hwcfg_led.gp_dout_l_sr ) {
2058
    if( seq_hwcfg_led.gp_dout_l2_sr )
2059
      SEQ_LED_SRSet(seq_hwcfg_led.gp_dout_l_sr-1, (ui_gp_leds >> 0) & 0xff);
496 tk 2060
    else
514 tk 2061
      SEQ_LED_SRSet(seq_hwcfg_led.gp_dout_l_sr-1, ((ui_gp_leds ^ pos_marker_mask) >> 0) & 0xff);
496 tk 2062
  }
134 tk 2063
 
514 tk 2064
  if( seq_hwcfg_led.gp_dout_r_sr ) {
2065
    if( seq_hwcfg_led.gp_dout_r2_sr )
2066
      SEQ_LED_SRSet(seq_hwcfg_led.gp_dout_r_sr-1, (ui_gp_leds >> 8) & 0xff);
496 tk 2067
    else
514 tk 2068
      SEQ_LED_SRSet(seq_hwcfg_led.gp_dout_r_sr-1, ((ui_gp_leds ^ pos_marker_mask) >> 8) & 0xff);
496 tk 2069
  }
134 tk 2070
 
514 tk 2071
  if( seq_hwcfg_led.gp_dout_l2_sr )
2072
    SEQ_LED_SRSet(seq_hwcfg_led.gp_dout_l2_sr-1, (pos_marker_mask >> 0) & 0xff);
2073
  if( seq_hwcfg_led.gp_dout_r2_sr )
2074
    SEQ_LED_SRSet(seq_hwcfg_led.gp_dout_r2_sr-1, (pos_marker_mask >> 8) & 0xff);
496 tk 2075
 
524 tk 2076
 
2077
  if( seq_hwcfg_blm.enabled ) {
2078
    // Red LEDs (position marker)
2079
    int track_ix;
2080
    for(track_ix=0; track_ix<(NUM_BLM_LED_SRS/2); ++track_ix) {
2081
      u16 pos_marker_mask = 0x0000;
2082
      if( sequencer_running ) {
2083
    u8 track = ui_selected_group + track_ix;
2084
    u8 played_step = seq_core_trk[track].step;
2085
    if( (played_step >> 4) == ui_selected_step_view )
2086
      pos_marker_mask = 1 << (played_step & 0xf);
2087
      }
2088
 
2089
      // Prepare Green LEDs (triggers)
2090
      u8 green_l = ui_blm_leds[2*track_ix+0];
2091
      u8 green_r = ui_blm_leds[2*track_ix+1];
2092
 
2093
      // Red LEDs (position marker)
2094
      if( seq_hwcfg_blm.dout_duocolour ) {
2095
    BLM_DOUT_SRSet(1, 2*track_ix+0, pos_marker_mask & 0xff);
2096
    BLM_DOUT_SRSet(1, 2*track_ix+1, (pos_marker_mask >> 8) & 0xff);
2097
 
2098
    if( seq_hwcfg_blm.dout_duocolour == 2 ) {
2099
      // Colour Mode 2: clear green LED, so that only one LED is lit
2100
      if( pos_marker_mask & 0x00ff )
2101
        green_l &= ~pos_marker_mask;
2102
      else if( pos_marker_mask & 0xff00 )
2103
        green_r &= ~(pos_marker_mask >> 8);
2104
    }
2105
      } else {
2106
    // If Duo-LEDs not enabled: invert Green LEDs
2107
    if( pos_marker_mask & 0x00ff )
2108
      green_l ^= pos_marker_mask;
2109
    else if( pos_marker_mask & 0xff00 )
2110
      green_r ^= (pos_marker_mask >> 8);
2111
      }
2112
 
2113
      // Set Green LEDs
2114
      BLM_DOUT_SRSet(0, 2*track_ix+0, green_l);
2115
      BLM_DOUT_SRSet(0, 2*track_ix+1, green_r);
2116
    }
2117
  }
2118
 
2119
 
514 tk 2120
  if( seq_hwcfg_blm8x8.enabled && seq_hwcfg_blm8x8.dout_gp_mapping ) {
513 tk 2121
    // for wilba's frontpanel
134 tk 2122
 
513 tk 2123
    // BLM_X DOUT -> GP LED mapping
2124
    // 0 = 15,16    1 = 13,14   2 = 11,12   3 = 9,10
2125
    // 4 = 1,2  5 = 3,4     6 = 5,6     7 = 7,8
134 tk 2126
 
513 tk 2127
    // bit 7: first green (i.e. GP1-G)
2128
    // bit 6: first red (i.e. GP1-R)
2129
    // bit 5: second green (i.e. GP2-G)
2130
    // bit 4: second red (i.e. GP2-R)
134 tk 2131
 
513 tk 2132
    // this mapping routine takes ca. 5 uS
2133
    // since it's only executed when ui_gp_leds or gp_mask has changed, it doesn't really hurt
134 tk 2134
 
513 tk 2135
    u16 modified_gp_leds = ui_gp_leds;
134 tk 2136
#if 1
513 tk 2137
    // extra: red LED is lit exclusively for higher contrast
2138
    modified_gp_leds &= ~pos_marker_mask;
134 tk 2139
#endif
2140
 
513 tk 2141
    int sr;
2142
    const u8 blm_x_sr_map[8] = {4, 5, 6, 7, 3, 2, 1, 0};
2143
    u16 gp_mask = 1 << 0;
2144
    for(sr=0; sr<8; ++sr) {
2145
      u8 pattern = 0;
134 tk 2146
 
513 tk 2147
      if( modified_gp_leds & gp_mask )
2148
    pattern |= 0x80;
2149
      if( pos_marker_mask & gp_mask )
2150
    pattern |= 0x40;
2151
      gp_mask <<= 1;
2152
      if( modified_gp_leds & gp_mask )
2153
    pattern |= 0x20;
2154
      if( pos_marker_mask & gp_mask )
2155
    pattern |= 0x10;
2156
      gp_mask <<= 1;
134 tk 2157
 
513 tk 2158
      u8 mapped_sr = blm_x_sr_map[sr];
2159
      BLM_X_LED_rows[mapped_sr][0] = (BLM_X_LED_rows[mapped_sr][0] & 0x0f) | pattern;
2160
    }
134 tk 2161
  }
2162
 
184 tk 2163
  return 0; // no error
134 tk 2164
}
2165
 
2166
 
2167
/////////////////////////////////////////////////////////////////////////////
168 tk 2168
// for menu handling (e.g. flashing cursor, doubleclick counter, etc...)
2169
// called each mS
2170
/////////////////////////////////////////////////////////////////////////////
2171
s32 SEQ_UI_MENU_Handler_Periodic()
2172
{
625 tk 2173
  // ignore in remote client mode
626 tk 2174
  if( seq_ui_remote_active_mode == SEQ_UI_REMOTE_MODE_CLIENT )
625 tk 2175
    return 0; // no error
2176
 
740 tk 2177
  if( ++ui_cursor_flash_ctr >= SEQ_UI_CURSOR_FLASH_CTR_MAX ) {
168 tk 2178
    ui_cursor_flash_ctr = 0;
740 tk 2179
    ++ui_cursor_flash_overrun_ctr;
2180
    seq_ui_display_update_req = 1;
2181
  }
2182
 
173 tk 2183
  // important: flash flag has to be recalculated on each invocation of this
2184
  // handler, since counter could also be reseted outside this function
652 tk 2185
  u8 old_ui_cursor_flash = ui_cursor_flash;
173 tk 2186
  ui_cursor_flash = ui_cursor_flash_ctr >= SEQ_UI_CURSOR_FLASH_CTR_LED_OFF;
652 tk 2187
  if( old_ui_cursor_flash != ui_cursor_flash )
2188
    seq_ui_display_update_req = 1;
168 tk 2189
 
240 tk 2190
  // used in some pages for temporary messages
416 tk 2191
  if( ui_hold_msg_ctr ) {
240 tk 2192
    --ui_hold_msg_ctr;
2193
 
416 tk 2194
    if( !ui_hold_msg_ctr )
2195
      seq_ui_display_update_req = 1;
2196
  }
2197
 
596 tk 2198
  // used for temporary messages
2199
  if( ui_msg_ctr )
2200
    --ui_msg_ctr;
299 tk 2201
 
184 tk 2202
  // VU meters (used in MUTE menu, could also be available as LED matrix...)
2203
  static u8 vu_meter_prediv = 0; // predivider for VU meters
2204
 
2205
  if( ++vu_meter_prediv >= 4 ) {
2206
    vu_meter_prediv = 0;
2207
 
333 tk 2208
 
2209
    portENTER_CRITICAL();
2210
 
184 tk 2211
    u8 track;
2212
    seq_core_trk_t *t = &seq_core_trk[0];
2213
    for(track=0; track<SEQ_CORE_NUM_TRACKS; ++t, ++track)
2214
      if( t->vu_meter )
2215
    --t->vu_meter;
333 tk 2216
 
2217
    int i;
2218
    u8 *vu_meter = (u8 *)&seq_layer_vu_meter[0];
2219
    for(i=0; i<sizeof(seq_layer_vu_meter); ++i, ++vu_meter) {
2220
      if( *vu_meter && !(*vu_meter & 0x80) ) // if bit 7 set: static value
2221
    *vu_meter -= 1;
2222
    }
2223
 
318 tk 2224
    portEXIT_CRITICAL();
184 tk 2225
  }
2226
 
740 tk 2227
  // delayed action will be triggered once counter reached 0
2228
  if( !ui_delayed_action_callback )
2229
    ui_delayed_action_ctr = 0;
2230
  else if( ui_delayed_action_ctr ) {
2231
    if( --ui_delayed_action_ctr == 0 ) {
2232
      ui_delayed_action_callback();
2233
      ui_delayed_action_callback = NULL;
2234
    }
2235
  }
2236
 
168 tk 2237
  return 0;
2238
}
2239
 
2240
 
2241
/////////////////////////////////////////////////////////////////////////////
328 tk 2242
// Should be regulary called to check if the layer/instrument/step selection
2243
// is valid for the current track
2244
// At least executed before button/encoder and LCD function calls
2245
/////////////////////////////////////////////////////////////////////////////
2246
s32 SEQ_UI_CheckSelections(void)
2247
{
492 tk 2248
  if( (ui_selected_tracks >> (4*ui_selected_group)) == 0 )
2249
    ui_selected_tracks = 1 << (4*ui_selected_group);
2250
 
328 tk 2251
  u8 visible_track = SEQ_UI_VisibleTrackGet();
2252
 
2253
  if( ui_selected_instrument >= SEQ_PAR_NumInstrumentsGet(visible_track) )
2254
    ui_selected_instrument = 0;
2255
 
2256
  if( ui_selected_par_layer >= SEQ_PAR_NumLayersGet(visible_track) )
2257
    ui_selected_par_layer = 0;
2258
 
2259
  if( ui_selected_trg_layer >= SEQ_TRG_NumLayersGet(visible_track) )
2260
    ui_selected_trg_layer = 0;
2261
 
2262
  if( ui_selected_step >= SEQ_TRG_NumStepsGet(visible_track) )
2263
    ui_selected_step = 0;
2264
 
2265
  if( ui_selected_step_view >= (SEQ_TRG_NumStepsGet(visible_track)/16) ) {
2266
    ui_selected_step_view = 0;
2267
    ui_selected_step %= 16;
2268
  }
2269
 
336 tk 2270
  if( ui_selected_step < (16*ui_selected_step_view) ||
2271
      ui_selected_step >= (16*(ui_selected_step_view+1)) )
328 tk 2272
    ui_selected_step_view = ui_selected_step / 16;
2273
 
2274
  return 0; // no error
2275
}
2276
 
2277
 
2278
/////////////////////////////////////////////////////////////////////////////
134 tk 2279
// Returns the currently visible track
2280
/////////////////////////////////////////////////////////////////////////////
2281
u8 SEQ_UI_VisibleTrackGet(void)
2282
{
2283
  u8 offset = 0;
2284
 
484 tk 2285
  u8 selected_tracks = ui_selected_tracks >> (4*ui_selected_group);
2286
  if( selected_tracks & (1 << 3) )
134 tk 2287
    offset = 3;
484 tk 2288
  if( selected_tracks & (1 << 2) )
134 tk 2289
    offset = 2;
484 tk 2290
  if( selected_tracks & (1 << 1) )
134 tk 2291
    offset = 1;
484 tk 2292
  if( selected_tracks & (1 << 0) )
134 tk 2293
    offset = 0;
2294
 
2295
  return 4*ui_selected_group + offset;
2296
}
2297
 
168 tk 2298
 
2299
/////////////////////////////////////////////////////////////////////////////
178 tk 2300
// Returns 1 if 'track' is selected
2301
/////////////////////////////////////////////////////////////////////////////
2302
s32 SEQ_UI_IsSelectedTrack(u8 track)
2303
{
484 tk 2304
  return (ui_selected_tracks & (1 << track)) ? 1 : 0;
178 tk 2305
}
2306
 
2307
 
2308
/////////////////////////////////////////////////////////////////////////////
240 tk 2309
// Sets a new selected step and updates the step view
2310
/////////////////////////////////////////////////////////////////////////////
2311
s32 SEQ_UI_SelectedStepSet(u8 step)
2312
{
2313
  ui_selected_step = step;
328 tk 2314
  SEQ_UI_CheckSelections();
2315
 
240 tk 2316
  return 0; // no error
2317
}
2318
 
2319
 
2320
/////////////////////////////////////////////////////////////////////////////
168 tk 2321
// Increments the selected tracks/groups
2322
// OUT: 1 if value has been changed, otherwise 0
2323
/////////////////////////////////////////////////////////////////////////////
2324
s32 SEQ_UI_GxTyInc(s32 incrementer)
2325
{
2326
  int gxty = SEQ_UI_VisibleTrackGet();
2327
  int prev_gxty = gxty;
2328
 
2329
  if( incrementer >= 0 ) {
2330
    if( (gxty += incrementer) >= SEQ_CORE_NUM_TRACKS )
2331
      gxty = SEQ_CORE_NUM_TRACKS-1;
2332
  } else {
2333
    if( (gxty += incrementer) < 0 )
2334
      gxty = 0;
2335
  }
2336
 
2337
  if( gxty == prev_gxty )
2338
    return 0; // no change
2339
 
484 tk 2340
  ui_selected_tracks = 1 << gxty;
168 tk 2341
  ui_selected_group = gxty / 4;
2342
 
2343
  return 1; // value changed
2344
}
2345
 
2346
 
2347
/////////////////////////////////////////////////////////////////////////////
236 tk 2348
// Increments a 16bit variable within given min/max range
2349
// OUT: 1 if value has been changed, otherwise 0
2350
/////////////////////////////////////////////////////////////////////////////
240 tk 2351
s32 SEQ_UI_Var16_Inc(u16 *value, u16 min, u16 max, s32 incrementer)
236 tk 2352
{
2353
  int new_value = *value;
2354
  int prev_value = new_value;
2355
 
2356
  if( incrementer >= 0 ) {
2357
    if( (new_value += incrementer) >= max )
2358
      new_value = max;
2359
  } else {
2360
    if( (new_value += incrementer) < min )
2361
      new_value = min;
2362
  }
2363
 
2364
  if( new_value == prev_value )
2365
    return 0; // no change
2366
 
2367
  *value = new_value;
2368
 
2369
  return 1; // value changed
2370
}
2371
 
240 tk 2372
/////////////////////////////////////////////////////////////////////////////
2373
// Increments an 8bit variable within given min/max range
2374
// OUT: 1 if value has been changed, otherwise 0
2375
/////////////////////////////////////////////////////////////////////////////
2376
s32 SEQ_UI_Var8_Inc(u8 *value, u16 min, u16 max, s32 incrementer)
2377
{
2378
  u16 tmp = *value;
2379
  if( SEQ_UI_Var16_Inc(&tmp, min, max, incrementer) ) {
2380
    *value = tmp;
2381
    return 1; // value changed
2382
  }
236 tk 2383
 
240 tk 2384
  return 0; // value hasn't been changed
2385
}
2386
 
2387
 
236 tk 2388
/////////////////////////////////////////////////////////////////////////////
168 tk 2389
// Increments a CC within given min/max range
2390
// OUT: 1 if value has been changed, otherwise 0
2391
/////////////////////////////////////////////////////////////////////////////
248 tk 2392
s32 SEQ_UI_CC_Inc(u8 cc, u8 min, u8 max, s32 incrementer)
168 tk 2393
{
2394
  u8 visible_track = SEQ_UI_VisibleTrackGet();
173 tk 2395
  int new_value = SEQ_CC_Get(visible_track, cc);
2396
  int prev_value = new_value;
168 tk 2397
 
2398
  if( incrementer >= 0 ) {
173 tk 2399
    if( (new_value += incrementer) >= max )
2400
      new_value = max;
168 tk 2401
  } else {
173 tk 2402
    if( (new_value += incrementer) < min )
2403
      new_value = min;
168 tk 2404
  }
2405
 
173 tk 2406
  if( new_value == prev_value )
168 tk 2407
    return 0; // no change
2408
 
173 tk 2409
  SEQ_CC_Set(visible_track, cc, new_value);
168 tk 2410
 
179 tk 2411
  // set same value for all selected tracks
2412
  u8 track;
2413
  for(track=0; track<SEQ_CORE_NUM_TRACKS; ++track)
2414
    if( track != visible_track && SEQ_UI_IsSelectedTrack(track) )
2415
      SEQ_CC_Set(track, cc, new_value);
2416
 
168 tk 2417
  return 1; // value changed
2418
}
2419
 
173 tk 2420
 
2421
/////////////////////////////////////////////////////////////////////////////
179 tk 2422
// Sets a CC value on all selected tracks
2423
// OUT: 1 if value has been changed, otherwise 0
2424
/////////////////////////////////////////////////////////////////////////////
248 tk 2425
s32 SEQ_UI_CC_Set(u8 cc, u8 value)
179 tk 2426
{
2427
  u8 visible_track = SEQ_UI_VisibleTrackGet();
2428
  int prev_value = SEQ_CC_Get(visible_track, cc);
2429
 
2430
  if( value == prev_value )
2431
    return 0; // no change
2432
 
2433
  SEQ_CC_Set(visible_track, cc, value);
2434
 
2435
  // set same value for all selected tracks
2436
  u8 track;
2437
  for(track=0; track<SEQ_CORE_NUM_TRACKS; ++track)
2438
    if( track != visible_track && SEQ_UI_IsSelectedTrack(track) )
2439
      SEQ_CC_Set(track, cc, value);
2440
 
2441
  return 1; // value changed
2442
}
2443
 
2444
/////////////////////////////////////////////////////////////////////////////
173 tk 2445
// Modifies a bitfield in a CC value to a given value
2446
// OUT: 1 if value has been changed, otherwise 0
2447
/////////////////////////////////////////////////////////////////////////////
248 tk 2448
s32 SEQ_UI_CC_SetFlags(u8 cc, u8 flag_mask, u8 value)
173 tk 2449
{
2450
  u8 visible_track = SEQ_UI_VisibleTrackGet();
2451
  int new_value = SEQ_CC_Get(visible_track, cc);
2452
  int prev_value = new_value;
2453
 
2454
  new_value = (new_value & ~flag_mask) | value;
2455
 
2456
  if( new_value == prev_value )
2457
    return 0; // no change
2458
 
2459
  SEQ_CC_Set(visible_track, cc, new_value);
2460
 
179 tk 2461
  // do same modification for all selected tracks
2462
  u8 track;
2463
  for(track=0; track<SEQ_CORE_NUM_TRACKS; ++track)
2464
    if( track != visible_track && SEQ_UI_IsSelectedTrack(track) ) {
2465
      int new_value = SEQ_CC_Get(track, cc);
2466
      new_value = (new_value & ~flag_mask) | value;
2467
      SEQ_CC_Set(track, cc, new_value);
2468
    }
2469
 
173 tk 2470
  return 1; // value changed
2471
}
2472
 
2473
 
299 tk 2474
/////////////////////////////////////////////////////////////////////////////
596 tk 2475
// Print temporary user messages (e.g. warnings, errors)
299 tk 2476
// expects mS delay and two lines, each up to 20 characters
2477
/////////////////////////////////////////////////////////////////////////////
596 tk 2478
s32 SEQ_UI_Msg(seq_ui_msg_type_t msg_type, u16 delay, char *line1, char *line2)
299 tk 2479
{
596 tk 2480
  ui_msg_type = msg_type;
2481
  ui_msg_ctr = delay;
2482
  strncpy((char *)ui_msg[0], line1, UI_MSG_MAX_CHAR);
2483
  strncpy((char *)ui_msg[1], line2, UI_MSG_MAX_CHAR);
299 tk 2484
 
2485
  return 0; // no error
2486
}
2487
 
2488
/////////////////////////////////////////////////////////////////////////////
596 tk 2489
// Stops temporary message if no SD card warning
2490
/////////////////////////////////////////////////////////////////////////////
2491
s32 SEQ_UI_MsgStop(void)
2492
{
2493
  if( ui_msg_type != SEQ_UI_MSG_SDCARD )
2494
    ui_msg_ctr = 0;
2495
 
2496
  return 0; // no error
2497
}
2498
 
2499
/////////////////////////////////////////////////////////////////////////////
299 tk 2500
// Prints a temporary error messages after file operation
2501
// Expects error status number (as defined in seq_file.h)
2502
/////////////////////////////////////////////////////////////////////////////
2503
s32 SEQ_UI_SDCardErrMsg(u16 delay, s32 status)
2504
{
2505
  // TODO: add more verbose error messages, they are clearly defined in seq_file.h)
2506
  char str[21];
2507
  sprintf(str, "E%3d (DOSFS: D%3d)", -status, seq_file_dfs_errno < 1000 ? seq_file_dfs_errno : 999);
596 tk 2508
  return SEQ_UI_Msg(SEQ_UI_MSG_SDCARD, delay, "!! SD Card Error !!!", str);
299 tk 2509
}