Subversion Repositories svn.mios32

Rev

Rev 742 | Rev 744 | 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 743 2009-10-11 20:12:08Z 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
 
168 tk 788
  // forward to menu page
740 tk 789
  if( !seq_ui_button_state.MENU_PRESSED && ui_button_callback != NULL ) {
168 tk 790
    ui_button_callback(SEQ_UI_BUTTON_Exit, depressed);
740 tk 791
    ui_cursor_flash_ctr = ui_cursor_flash_overrun_ctr = 0;
792
  }
134 tk 793
 
167 tk 794
  // release all button states
795
  seq_ui_button_state.ALL = 0;
796
 
272 tk 797
  // enter menu page if we were not there before
798
  if( prev_ui_page != SEQ_UI_PAGE_MENU )
799
    SEQ_UI_PageSet(SEQ_UI_PAGE_MENU);
800
 
134 tk 801
  return 0; // no error
802
}
803
 
804
static s32 SEQ_UI_Button_Edit(s32 depressed)
805
{
741 tk 806
  seq_ui_button_state.EDIT_PRESSED = depressed ? 0 : 1;
591 tk 807
 
134 tk 808
  if( depressed ) return -1; // ignore when button depressed
809
 
167 tk 810
  // change to edit page
811
  SEQ_UI_PageSet(SEQ_UI_PAGE_EDIT);
812
 
178 tk 813
  // set/clear encoder fast function if required
814
  SEQ_UI_InitEncSpeed(1); // auto config
815
 
134 tk 816
  return 0; // no error
817
}
818
 
819
static s32 SEQ_UI_Button_Mute(s32 depressed)
820
{
591 tk 821
  seq_ui_button_state.MUTE_PRESSED = depressed ? 0 : 1;
822
 
134 tk 823
  if( depressed ) return -1; // ignore when button depressed
824
 
184 tk 825
  SEQ_UI_PageSet(SEQ_UI_PAGE_MUTE);
826
 
134 tk 827
  return 0; // no error
828
}
829
 
830
static s32 SEQ_UI_Button_Pattern(s32 depressed)
831
{
591 tk 832
  seq_ui_button_state.PATTERN_PRESSED = depressed ? 0 : 1;
833
 
134 tk 834
  if( depressed ) return -1; // ignore when button depressed
835
 
184 tk 836
  SEQ_UI_PageSet(SEQ_UI_PAGE_PATTERN);
837
 
134 tk 838
  return 0; // no error
839
}
840
 
841
static s32 SEQ_UI_Button_Song(s32 depressed)
842
{
591 tk 843
  seq_ui_button_state.SONG_PRESSED = depressed ? 0 : 1;
844
 
134 tk 845
  if( depressed ) return -1; // ignore when button depressed
846
 
399 tk 847
  SEQ_UI_PageSet(SEQ_UI_PAGE_SONG);
848
 
134 tk 849
  return 0; // no error
850
}
851
 
852
static s32 SEQ_UI_Button_Solo(s32 depressed)
853
{
513 tk 854
  if( seq_hwcfg_button_beh.solo ) {
855
    // toggle mode
856
    if( depressed ) return -1; // ignore when button depressed
857
    seq_ui_button_state.SOLO ^= 1;
858
  } else {
859
    // set mode
860
    seq_ui_button_state.SOLO = depressed ? 0 : 1;
861
  }
134 tk 862
 
863
  return 0; // no error
864
}
865
 
866
static s32 SEQ_UI_Button_Fast(s32 depressed)
867
{
513 tk 868
  if( seq_hwcfg_button_beh.fast ) {
869
    // toggle mode
870
    if( depressed ) return -1; // ignore when button depressed
871
    seq_ui_button_state.FAST_ENCODERS ^= 1;
872
  } else {
873
    // set mode
874
    seq_ui_button_state.FAST_ENCODERS = depressed ? 0 : 1;
875
  }
134 tk 876
 
178 tk 877
  SEQ_UI_InitEncSpeed(0); // no auto config
878
 
134 tk 879
  return 0; // no error
880
}
881
 
882
static s32 SEQ_UI_Button_All(s32 depressed)
883
{
178 tk 884
  seq_ui_button_state.CHANGE_ALL_STEPS_SAME_VALUE = depressed ? 0 : 1;
134 tk 885
 
513 tk 886
  if( seq_hwcfg_button_beh.all ) {
887
    // toggle mode
888
    if( depressed ) return -1;
889
    seq_ui_button_state.CHANGE_ALL_STEPS ^= 1;
890
  } else {
891
    // set mode
892
    seq_ui_button_state.CHANGE_ALL_STEPS = depressed ? 0 : 1;
893
  }
178 tk 894
 
134 tk 895
  return 0; // no error
896
}
897
 
898
static s32 SEQ_UI_Button_StepView(s32 depressed)
899
{
600 tk 900
  //  static seq_ui_page_t prev_page = SEQ_UI_PAGE_NONE;
901
  // also used by seq_ui_stepsel
902
 
513 tk 903
  if( seq_hwcfg_button_beh.step_view ) {
904
    if( depressed ) return -1; // ignore when button depressed
678 tk 905
    if( !seq_ui_button_state.STEP_VIEW ) // due to page change: button going to be set, clear other toggle buttons
906
      seq_ui_button_state.PAGE_CHANGE_BUTTON_FLAGS = 0;
513 tk 907
    seq_ui_button_state.STEP_VIEW ^= 1; // toggle STEP_VIEW pressed (will also be released once GP button has been pressed)
908
  } else {
909
    // set mode
910
    seq_ui_button_state.STEP_VIEW = depressed ? 0 : 1;
911
  }
134 tk 912
 
513 tk 913
  if( seq_ui_button_state.STEP_VIEW ) {
600 tk 914
    ui_stepview_prev_page = ui_page;
303 tk 915
    SEQ_UI_PageSet(SEQ_UI_PAGE_STEPSEL);
916
  } else {
524 tk 917
    if( ui_page == SEQ_UI_PAGE_STEPSEL )
600 tk 918
      SEQ_UI_PageSet(ui_stepview_prev_page);
303 tk 919
  }
134 tk 920
 
921
  return 0; // no error
922
}
923
 
513 tk 924
static s32 SEQ_UI_Button_TrackSel(s32 depressed)
134 tk 925
{
513 tk 926
  static seq_ui_page_t prev_page = SEQ_UI_PAGE_NONE;
134 tk 927
 
513 tk 928
  if( seq_hwcfg_button_beh.track_sel ) {
929
    if( depressed ) return -1; // ignore when button depressed
678 tk 930
    if( !seq_ui_button_state.TRACK_SEL ) // due to page change: button going to be set, clear other toggle buttons
931
      seq_ui_button_state.PAGE_CHANGE_BUTTON_FLAGS = 0;
513 tk 932
    seq_ui_button_state.TRACK_SEL ^= 1; // toggle TRACKSEL status (will also be released once GP button has been pressed)
933
  } else {
934
    seq_ui_button_state.TRACK_SEL = depressed ? 0 : 1;
935
  }
936
 
937
  if( seq_ui_button_state.TRACK_SEL ) {
938
    prev_page = ui_page;
939
    SEQ_UI_PageSet(SEQ_UI_PAGE_TRACKSEL);
940
  } else {
524 tk 941
    if( ui_page == SEQ_UI_PAGE_TRACKSEL )
942
      SEQ_UI_PageSet(prev_page);
513 tk 943
  }
944
 
134 tk 945
  return 0; // no error
946
}
947
 
484 tk 948
static s32 SEQ_UI_Button_Track(s32 depressed, u32 track_button)
134 tk 949
{
484 tk 950
  static u8 button_state = 0x0f; // all 4 buttons depressed
134 tk 951
 
484 tk 952
  if( track_button >= 4 ) return -2; // max. 4 track buttons
134 tk 953
 
484 tk 954
  if( depressed ) {
955
    button_state |= (1 << track_button);
956
    return 0; // no error
957
  }
134 tk 958
 
484 tk 959
  button_state &= ~(1 << track_button);
960
 
961
  if( button_state == (~(1 << track_button) & 0xf) ) {
962
    // if only one select button pressed: radio-button function (1 of 4)
963
    ui_selected_tracks = 1 << (track_button + 4*ui_selected_group);
964
  } else {
965
    // if more than one select button pressed: toggle function (4 of 4)
966
    ui_selected_tracks ^= 1 << (track_button + 4*ui_selected_group);
967
  }
968
 
178 tk 969
  // set/clear encoder fast function if required
970
  SEQ_UI_InitEncSpeed(1); // auto config
971
 
134 tk 972
  return 0; // no error
973
}
974
 
975
static s32 SEQ_UI_Button_Group(s32 depressed, u32 group)
976
{
977
  if( depressed ) return -1; // ignore when button depressed
978
 
979
  if( group >= 4 ) return -2; // max. 4 group buttons
980
 
484 tk 981
  // if group has changed:
982
  if( group != ui_selected_group ) {
983
    // get current track selection
984
    u16 old_tracks = ui_selected_tracks >> (4*ui_selected_group);
134 tk 985
 
484 tk 986
    // select new group
987
    ui_selected_group = group;
988
 
989
    // take over old track selection
990
    ui_selected_tracks = old_tracks << (4*ui_selected_group);
991
  }
992
 
178 tk 993
  // set/clear encoder fast function if required
994
  SEQ_UI_InitEncSpeed(1); // auto config
995
 
134 tk 996
  return 0; // no error
997
}
998
 
513 tk 999
static s32 SEQ_UI_Button_ParLayerSel(s32 depressed)
1000
{
600 tk 1001
  // static seq_ui_page_t prev_page = SEQ_UI_PAGE_NONE;
1002
  // also used by seq_ui_parsel.c
513 tk 1003
 
1004
  if( seq_hwcfg_button_beh.par_layer ) {
1005
    if( depressed ) return -1; // ignore when button depressed
678 tk 1006
    if( !seq_ui_button_state.PAR_LAYER_SEL ) // due to page change: button going to be set, clear other toggle buttons
1007
      seq_ui_button_state.PAGE_CHANGE_BUTTON_FLAGS = 0;
513 tk 1008
    seq_ui_button_state.PAR_LAYER_SEL ^= 1; // toggle PARSEL status (will also be released once GP button has been pressed)
1009
  } else {
1010
    seq_ui_button_state.PAR_LAYER_SEL = depressed ? 0 : 1;
1011
  }
1012
 
1013
  if( seq_ui_button_state.PAR_LAYER_SEL ) {
600 tk 1014
    ui_parlayer_prev_page = ui_page;
513 tk 1015
    SEQ_UI_PageSet(SEQ_UI_PAGE_PARSEL);
1016
  } else {
524 tk 1017
    if( ui_page == SEQ_UI_PAGE_PARSEL )
600 tk 1018
      SEQ_UI_PageSet(ui_parlayer_prev_page);
513 tk 1019
  }
1020
 
1021
  // set/clear encoder fast function if required
1022
  SEQ_UI_InitEncSpeed(1); // auto config
1023
 
1024
  return 0; // no error
1025
}
1026
 
134 tk 1027
static s32 SEQ_UI_Button_ParLayer(s32 depressed, u32 par_layer)
1028
{
1029
  if( par_layer >= 3 ) return -2; // max. 3 parlayer buttons
1030
 
333 tk 1031
  u8 visible_track = SEQ_UI_VisibleTrackGet();
1032
  u8 num_p_layers = SEQ_PAR_NumLayersGet(visible_track);
134 tk 1033
 
333 tk 1034
  if( num_p_layers <= 3 ) {
1035
    // 3 layers: direct selection with LayerA/B/C button
1036
    if( depressed ) return -1; // ignore when button depressed
596 tk 1037
    if( par_layer >= num_p_layers ) {
1038
      char str1[21];
1039
      sprintf(str1, "Parameter Layer %c", 'A'+par_layer);
1040
      SEQ_UI_Msg(SEQ_UI_MSG_USER, 1000, str1, "not available!");
1041
    } else {
1042
      seq_ui_button_state.PAR_LAYER_SEL = 0;
1043
      ui_selected_par_layer = par_layer;
1044
    }
333 tk 1045
  } else if( num_p_layers <= 4 ) {
1046
    // 4 layers: LayerC Button toggles between C and D
1047
    if( depressed ) return -1; // ignore when button depressed
1048
    seq_ui_button_state.PAR_LAYER_SEL = 0;
336 tk 1049
    if( par_layer == 2 )
1050
      ui_selected_par_layer = (ui_selected_par_layer == 2) ? 3 : 2;
333 tk 1051
    else
1052
      ui_selected_par_layer = par_layer;
1053
  } else {
1054
    // >4 layers: LayerA/B button selects directly, Layer C button enters layer selection page
1055
    if( par_layer <= 1 ) {
1056
      if( depressed ) return -1; // ignore when button depressed
1057
      seq_ui_button_state.PAR_LAYER_SEL = 0;
1058
      ui_selected_par_layer = par_layer;
1059
    } else {
513 tk 1060
      return SEQ_UI_Button_ParLayerSel(depressed);
333 tk 1061
    }
1062
  }
1063
 
178 tk 1064
  // set/clear encoder fast function if required
1065
  SEQ_UI_InitEncSpeed(1); // auto config
1066
 
134 tk 1067
  return 0; // no error
1068
}
1069
 
513 tk 1070
static s32 SEQ_UI_Button_TrgLayerSel(s32 depressed)
1071
{
600 tk 1072
  // static seq_ui_page_t prev_page = SEQ_UI_PAGE_NONE;
1073
  // also used by seq_ui_trgsel.c
513 tk 1074
 
1075
  if( seq_hwcfg_button_beh.trg_layer ) {
1076
    if( depressed ) return -1; // ignore when button depressed
678 tk 1077
    if( !seq_ui_button_state.TRG_LAYER_SEL ) // due to page change: button going to be set, clear other toggle buttons
1078
      seq_ui_button_state.PAGE_CHANGE_BUTTON_FLAGS = 0;
513 tk 1079
    seq_ui_button_state.TRG_LAYER_SEL ^= 1; // toggle TRGSEL status (will also be released once GP button has been pressed)
1080
  } else {
1081
    seq_ui_button_state.TRG_LAYER_SEL = depressed ? 0 : 1;
1082
  }
1083
 
1084
  if( seq_ui_button_state.TRG_LAYER_SEL ) {
600 tk 1085
    ui_trglayer_prev_page = ui_page;
513 tk 1086
    SEQ_UI_PageSet(SEQ_UI_PAGE_TRGSEL);
1087
  } else {
524 tk 1088
    if( ui_page == SEQ_UI_PAGE_TRGSEL )
600 tk 1089
      SEQ_UI_PageSet(ui_trglayer_prev_page);
513 tk 1090
  }
1091
 
1092
  return 0; // no error
1093
}
1094
 
134 tk 1095
static s32 SEQ_UI_Button_TrgLayer(s32 depressed, u32 trg_layer)
1096
{
1097
  if( trg_layer >= 3 ) return -2; // max. 3 trglayer buttons
1098
 
326 tk 1099
  u8 visible_track = SEQ_UI_VisibleTrackGet();
1100
  u8 event_mode = SEQ_CC_Get(visible_track, SEQ_CC_MIDI_EVENT_MODE);
1101
  u8 num_t_layers = SEQ_TRG_NumLayersGet(visible_track);
134 tk 1102
 
328 tk 1103
  if( event_mode != SEQ_EVENT_MODE_Drum && num_t_layers <= 3 ) {
326 tk 1104
    // 3 layers: direct selection with LayerA/B/C button
1105
    if( depressed ) return -1; // ignore when button depressed
596 tk 1106
    if( trg_layer >= num_t_layers ) {
1107
      char str1[21];
1108
      sprintf(str1, "Trigger Layer %c", 'A'+trg_layer);
1109
      SEQ_UI_Msg(SEQ_UI_MSG_USER, 1000, str1, "not available!");
1110
    } else {
1111
      seq_ui_button_state.TRG_LAYER_SEL = 0;
1112
      ui_selected_trg_layer = trg_layer;
1113
    }
328 tk 1114
  } else if( event_mode != SEQ_EVENT_MODE_Drum && num_t_layers <= 4 ) {
326 tk 1115
    // 4 layers: LayerC Button toggles between C and D
1116
    if( depressed ) return -1; // ignore when button depressed
1117
    seq_ui_button_state.TRG_LAYER_SEL = 0;
336 tk 1118
    if( trg_layer == 2 )
1119
      ui_selected_trg_layer = (ui_selected_trg_layer == 2) ? 3 : 2;
326 tk 1120
    else
1121
      ui_selected_trg_layer = trg_layer;
1122
  } else {
333 tk 1123
    // >4 layers or drum mode: LayerA/B button selects directly, Layer C button enters trigger selection page
326 tk 1124
    // also used for drum tracks
1125
    if( trg_layer <= 1 ) {
1126
      if( depressed ) return -1; // ignore when button depressed
328 tk 1127
      seq_ui_button_state.TRG_LAYER_SEL = 0;
1128
      ui_selected_trg_layer = trg_layer;
326 tk 1129
    } else {
513 tk 1130
      return SEQ_UI_Button_TrgLayerSel(depressed);
326 tk 1131
    }
1132
  }
1133
 
134 tk 1134
  return 0; // no error
1135
}
1136
 
1137
 
544 tk 1138
static s32 SEQ_UI_Button_Morph(s32 depressed)
1139
{
1140
  if( depressed ) return -1; // ignore when button depressed
134 tk 1141
 
544 tk 1142
  SEQ_UI_PageSet(SEQ_UI_PAGE_TRKMORPH);
1143
 
1144
  return 0; // no error
1145
}
1146
 
1147
static s32 SEQ_UI_Button_Mixer(s32 depressed)
1148
{
1149
  if( depressed ) return -1; // ignore when button depressed
1150
 
1151
  SEQ_UI_PageSet(SEQ_UI_PAGE_MIXER);
1152
 
1153
  return 0; // no error
1154
}
1155
 
1156
static s32 SEQ_UI_Button_Transpose(s32 depressed)
1157
{
1158
  if( depressed ) return -1; // ignore when button depressed
1159
 
1160
  SEQ_UI_PageSet(SEQ_UI_PAGE_TRKTRAN);
1161
 
1162
  return 0; // no error
1163
}
1164
 
1165
 
742 tk 1166
// only used by keyboard remote function
1167
static s32 SEQ_UI_Button_ToggleGate(s32 depressed)
1168
{
1169
  if( depressed ) return -1; // ignore when button depressed
544 tk 1170
 
742 tk 1171
  u8 visible_track = SEQ_UI_VisibleTrackGet();
1172
  u8 gate = SEQ_TRG_GateGet(visible_track, ui_selected_step, ui_selected_instrument) ? 0 : 1;
1173
  SEQ_TRG_GateSet(visible_track, ui_selected_step, ui_selected_instrument, gate);
544 tk 1174
 
742 tk 1175
  return 0; // no error
1176
}
1177
 
1178
 
1179
 
134 tk 1180
/////////////////////////////////////////////////////////////////////////////
1181
// Button handler
1182
/////////////////////////////////////////////////////////////////////////////
1183
s32 SEQ_UI_Button_Handler(u32 pin, u32 pin_value)
1184
{
492 tk 1185
  int i;
1186
 
625 tk 1187
  // send MIDI event in remote mode and exit
626 tk 1188
  if( seq_ui_remote_active_mode == SEQ_UI_REMOTE_MODE_CLIENT )
625 tk 1189
    return SEQ_MIDI_SYSEX_REMOTE_Client_SendButton(pin, pin_value);
1190
 
496 tk 1191
  // ignore so long hardware config hasn't been read
1192
  if( !SEQ_FILE_HW_ConfigLocked() )
1193
    return -1;
1194
 
740 tk 1195
  // ignore during a backup or format is created
1196
  if( seq_ui_backup_req || seq_ui_format_req )
634 tk 1197
    return -1;
1198
 
328 tk 1199
  // ensure that selections are matching with track constraints
1200
  SEQ_UI_CheckSelections();
1201
 
492 tk 1202
  // request display update
1203
  seq_ui_display_update_req = 1;
134 tk 1204
 
596 tk 1205
  // stop current message if (new) button has been pressed
1206
  if( pin_value == 0 )
1207
    SEQ_UI_MsgStop();
134 tk 1208
 
596 tk 1209
 
492 tk 1210
  // MEMO: we could also use a jump table with references to the functions
1211
  // here, but this "spagetthi code" simplifies the configuration and
1212
  // the resulting ASM doesn't look that bad!
168 tk 1213
 
492 tk 1214
  for(i=0; i<SEQ_HWCFG_NUM_GP; ++i)
1215
    if( pin == seq_hwcfg_button.gp[i] )
1216
      return SEQ_UI_Button_GP(pin_value, i);
134 tk 1217
 
492 tk 1218
  for(i=0; i<SEQ_HWCFG_NUM_TRACK; ++i)
1219
    if( pin == seq_hwcfg_button.track[i] )
1220
      return SEQ_UI_Button_Track(pin_value, i);
513 tk 1221
  if( pin == seq_hwcfg_button.track_sel )
1222
    return SEQ_UI_Button_TrackSel(pin_value);
134 tk 1223
 
492 tk 1224
  for(i=0; i<SEQ_HWCFG_NUM_GROUP; ++i)
1225
    if( pin == seq_hwcfg_button.group[i] )
1226
      return SEQ_UI_Button_Group(pin_value, i);
134 tk 1227
 
492 tk 1228
  for(i=0; i<SEQ_HWCFG_NUM_PAR_LAYER; ++i)
1229
    if( pin == seq_hwcfg_button.par_layer[i] )
1230
      return SEQ_UI_Button_ParLayer(pin_value, i);
513 tk 1231
  if( pin == seq_hwcfg_button.par_layer_sel )
1232
    return SEQ_UI_Button_ParLayerSel(pin_value);
134 tk 1233
 
492 tk 1234
  for(i=0; i<SEQ_HWCFG_NUM_TRG_LAYER; ++i)
1235
    if( pin == seq_hwcfg_button.trg_layer[i] )
1236
      return SEQ_UI_Button_TrgLayer(pin_value, i);
513 tk 1237
  if( pin == seq_hwcfg_button.trg_layer_sel )
1238
    return SEQ_UI_Button_TrgLayerSel(pin_value);
134 tk 1239
 
492 tk 1240
  if( pin == seq_hwcfg_button.left )
1241
    return SEQ_UI_Button_Left(pin_value);
1242
  if( pin == seq_hwcfg_button.right )
1243
    return SEQ_UI_Button_Right(pin_value);
1244
  if( pin == seq_hwcfg_button.down )
1245
    return SEQ_UI_Button_Down(pin_value);
1246
  if( pin == seq_hwcfg_button.up )
1247
    return SEQ_UI_Button_Up(pin_value);
134 tk 1248
 
492 tk 1249
  if( pin == seq_hwcfg_button.scrub )
1250
    return SEQ_UI_Button_Scrub(pin_value);
1251
  if( pin == seq_hwcfg_button.metronome )
1252
    return SEQ_UI_Button_Metronome(pin_value);
134 tk 1253
 
630 tk 1254
  if( pin == seq_hwcfg_button.record )
1255
    return SEQ_UI_Button_Record(pin_value);
1256
 
492 tk 1257
  if( pin == seq_hwcfg_button.stop )
1258
    return SEQ_UI_Button_Stop(pin_value);
1259
  if( pin == seq_hwcfg_button.pause )
1260
    return SEQ_UI_Button_Pause(pin_value);
1261
  if( pin == seq_hwcfg_button.play )
1262
    return SEQ_UI_Button_Play(pin_value);
1263
  if( pin == seq_hwcfg_button.rew )
1264
    return SEQ_UI_Button_Rew(pin_value);
1265
  if( pin == seq_hwcfg_button.fwd )
1266
    return SEQ_UI_Button_Fwd(pin_value);
596 tk 1267
  if( pin == seq_hwcfg_button.loop )
1268
    return SEQ_UI_Button_Loop(pin_value);
134 tk 1269
 
492 tk 1270
  if( pin == seq_hwcfg_button.utility )
1271
    return SEQ_UI_Button_Utility(pin_value);
1272
  if( pin == seq_hwcfg_button.copy )
1273
    return SEQ_UI_Button_Copy(pin_value);
1274
  if( pin == seq_hwcfg_button.paste )
1275
    return SEQ_UI_Button_Paste(pin_value);
1276
  if( pin == seq_hwcfg_button.clear )
1277
    return SEQ_UI_Button_Clear(pin_value);
134 tk 1278
 
492 tk 1279
  if( pin == seq_hwcfg_button.menu )
1280
    return SEQ_UI_Button_Menu(pin_value);
1281
  if( pin == seq_hwcfg_button.select )
1282
    return SEQ_UI_Button_Select(pin_value);
1283
  if( pin == seq_hwcfg_button.exit )
1284
    return SEQ_UI_Button_Exit(pin_value);
134 tk 1285
 
513 tk 1286
  if( pin == seq_hwcfg_button.tap_tempo )
1287
    return SEQ_UI_Button_TapTempo(pin_value);
1288
  if( pin == seq_hwcfg_button.tempo_preset )
1289
    return SEQ_UI_Button_TempoPreset(pin_value);
524 tk 1290
  if( pin == seq_hwcfg_button.ext_restart )
1291
    return SEQ_UI_Button_ExtRestart(pin_value);
134 tk 1292
 
492 tk 1293
  if( pin == seq_hwcfg_button.edit )
1294
    return SEQ_UI_Button_Edit(pin_value);
1295
  if( pin == seq_hwcfg_button.mute )
1296
    return SEQ_UI_Button_Mute(pin_value);
1297
  if( pin == seq_hwcfg_button.pattern )
1298
    return SEQ_UI_Button_Pattern(pin_value);
1299
  if( pin == seq_hwcfg_button.song )
1300
    return SEQ_UI_Button_Song(pin_value);
134 tk 1301
 
492 tk 1302
  if( pin == seq_hwcfg_button.solo )
1303
    return SEQ_UI_Button_Solo(pin_value);
1304
  if( pin == seq_hwcfg_button.fast )
1305
    return SEQ_UI_Button_Fast(pin_value);
1306
  if( pin == seq_hwcfg_button.all )
1307
    return SEQ_UI_Button_All(pin_value);
134 tk 1308
 
492 tk 1309
  if( pin == seq_hwcfg_button.step_view )
1310
    return SEQ_UI_Button_StepView(pin_value);
1311
  if( pin == seq_hwcfg_button.tap_tempo )
1312
    return SEQ_UI_Button_TapTempo(pin_value);
134 tk 1313
 
544 tk 1314
  if( pin == seq_hwcfg_button.morph )
1315
    return SEQ_UI_Button_Morph(pin_value);
1316
  if( pin == seq_hwcfg_button.mixer )
1317
    return SEQ_UI_Button_Mixer(pin_value);
1318
  if( pin == seq_hwcfg_button.transpose )
1319
    return SEQ_UI_Button_Transpose(pin_value);
1320
 
625 tk 1321
  // always print debugging message
1322
#if 1
1323
  MIOS32_MIDI_SendDebugMessage("[SEQ_UI_Button_Handler] Button SR:%d, Pin:%d not mapped, it has been %s.\n",
1324
                   (pin >> 3) + 1,
1325
                   pin & 7,
1326
                   pin_value ? "depressed" : "pressed");
1327
#endif
1328
 
492 tk 1329
  return -1; // button not mapped
134 tk 1330
}
1331
 
1332
 
1333
/////////////////////////////////////////////////////////////////////////////
524 tk 1334
// BLM Button handler
1335
/////////////////////////////////////////////////////////////////////////////
1336
s32 SEQ_UI_BLM_Button_Handler(u32 row, u32 pin, u32 pin_value)
1337
{
625 tk 1338
  // send MIDI event in remote mode and exit
626 tk 1339
  if( seq_ui_remote_active_mode == SEQ_UI_REMOTE_MODE_CLIENT )
625 tk 1340
    return SEQ_MIDI_SYSEX_REMOTE_Client_Send_BLM_Button(row, pin, pin_value);
1341
 
524 tk 1342
  // ignore so long hardware config hasn't been read
1343
  if( !SEQ_FILE_HW_ConfigLocked() )
1344
    return -1;
1345
 
740 tk 1346
  // ignore during a backup or format is created
1347
  if( seq_ui_backup_req || seq_ui_format_req )
634 tk 1348
    return -1;
1349
 
524 tk 1350
  if( row >= SEQ_CORE_NUM_TRACKS_PER_GROUP )
1351
    return -1; // more than 4 tracks not supported (yet) - could be done in this function w/o much effort
1352
 
1353
  if( pin >= 16 )
1354
    return -1; // more than 16 step buttons not supported (yet) - could be done by selecting the step view
1355
 
1356
  // select track depending on row
1357
  ui_selected_tracks = 1 << (row + 4*ui_selected_group);
1358
 
1359
  // ensure that selections are matching with track constraints
1360
  SEQ_UI_CheckSelections();
1361
 
1362
  // request display update
1363
  seq_ui_display_update_req = 1;
1364
 
1365
  // emulate general purpose button
1366
  return SEQ_UI_Button_GP(pin_value, pin);
1367
}
1368
 
1369
 
1370
/////////////////////////////////////////////////////////////////////////////
134 tk 1371
// Encoder handler
1372
/////////////////////////////////////////////////////////////////////////////
1373
s32 SEQ_UI_Encoder_Handler(u32 encoder, s32 incrementer)
1374
{
625 tk 1375
  // send MIDI event in remote mode and exit
626 tk 1376
  if( seq_ui_remote_active_mode == SEQ_UI_REMOTE_MODE_CLIENT )
625 tk 1377
    return SEQ_MIDI_SYSEX_REMOTE_Client_SendEncoder(encoder, incrementer);
1378
 
496 tk 1379
  // ignore so long hardware config hasn't been read
1380
  if( !SEQ_FILE_HW_ConfigLocked() )
1381
    return -1;
1382
 
740 tk 1383
  // ignore during a backup or format is created
1384
  if( seq_ui_backup_req || seq_ui_format_req )
634 tk 1385
    return -1;
1386
 
134 tk 1387
  if( encoder > 16 )
1388
    return -1; // encoder doesn't exist
1389
 
328 tk 1390
  // ensure that selections are matching with track constraints
1391
  SEQ_UI_CheckSelections();
1392
 
596 tk 1393
  // stop current message
1394
  SEQ_UI_MsgStop();
1395
 
134 tk 1396
  // limit incrementer
1397
  if( incrementer > 3 )
1398
    incrementer = 3;
1399
  else if( incrementer < -3 )
1400
    incrementer = -3;
1401
 
607 tk 1402
  if( seq_ui_button_state.SCRUB && encoder == 0 ) {
1403
    // if sequencer isn't already running, continue it (don't restart)
1404
    if( !SEQ_BPM_IsRunning() )
1405
      SEQ_BPM_Cont();
1406
    ui_seq_pause = 0; // clear pause mode
1407
    // Scrub sequence back or forth
1408
    portENTER_CRITICAL(); // should be atomic
1409
    SEQ_CORE_Scrub(incrementer);
1410
    portEXIT_CRITICAL();
1411
  } else if( !seq_ui_button_state.MENU_PRESSED && ui_encoder_callback != NULL ) {
173 tk 1412
    ui_encoder_callback((encoder == 0) ? SEQ_UI_ENCODER_Datawheel : (encoder-1), incrementer);
740 tk 1413
    ui_cursor_flash_ctr = ui_cursor_flash_overrun_ctr = 0; // ensure that value is visible when it has been changed
134 tk 1414
  }
1415
 
1416
  // request display update
159 tk 1417
  seq_ui_display_update_req = 1;
134 tk 1418
 
1419
  return 0; // no error
1420
}
1421
 
1422
 
353 tk 1423
 
134 tk 1424
/////////////////////////////////////////////////////////////////////////////
626 tk 1425
// Receives a MIDI package from APP_NotifyReceivedEvent (-> app.c)
1426
/////////////////////////////////////////////////////////////////////////////
1427
s32 SEQ_UI_REMOTE_MIDI_Receive(mios32_midi_port_t port, mios32_midi_package_t midi_package)
1428
{
1429
#if 1
1430
  // check for active remote mode
1431
  if( seq_ui_remote_active_mode != SEQ_UI_REMOTE_MODE_SERVER )
1432
    return 0; // no error
1433
#endif
1434
 
1435
  if( (seq_ui_remote_port == DEFAULT && seq_ui_remote_active_port != port) ||
1436
      (seq_ui_remote_port != DEFAULT && port != seq_ui_remote_port) )
1437
    return 0; // wrong port
1438
 
1439
  // for easier parsing: convert Note Off -> Note On with velocity 0
1440
  if( midi_package.event == NoteOff ) {
1441
    midi_package.event = NoteOn;
1442
    midi_package.velocity = 0;
1443
  }
1444
 
1445
  switch( midi_package.event ) {
1446
    case NoteOn: {
1447
      switch( midi_package.chn ) {
1448
        case Chn1:
1449
      SEQ_UI_Button_Handler(midi_package.note + 0x00, midi_package.velocity ? 0 : 1);
1450
      break;
1451
        case Chn2:
1452
      SEQ_UI_Button_Handler(midi_package.note + 0x80, midi_package.velocity ? 0 : 1);
1453
      break;
1454
        case Chn3:
1455
      SEQ_UI_BLM_Button_Handler(midi_package.note >> 5, midi_package.note & 0x1f, midi_package.velocity ? 0 : 1);
1456
      break;
1457
      }
1458
    } break;
1459
 
1460
    case CC: {
1461
      if( midi_package.cc_number >= 15 && midi_package.cc_number <= 31 )
1462
    SEQ_UI_Encoder_Handler(midi_package.cc_number-15, (int)midi_package.value - 0x40);
1463
    } break;
1464
  }
1465
 
632 tk 1466
  return 1; // MIDI event has been taken for remote function -> don't forward to router/MIDI event parser
626 tk 1467
}
1468
 
1469
 
1470
/////////////////////////////////////////////////////////////////////////////
741 tk 1471
// MIDI Remote Keyboard Function (called from SEQ_MIDI_IN)
1472
/////////////////////////////////////////////////////////////////////////////
742 tk 1473
s32 SEQ_UI_REMOTE_MIDI_Keyboard(u8 key, u8 depressed)
741 tk 1474
{
742 tk 1475
#if 0
1476
  MIOS32_MIDI_SendDebugMessage("SEQ_UI_REMOTE_MIDI_Keyboard(%d, %d)\n", key, depressed);
741 tk 1477
#endif
742 tk 1478
 
1479
  switch( key ) {
1480
    case 0x24: // C-2
1481
      return SEQ_UI_Button_GP(depressed, 0);
1482
    case 0x25: // C#2
1483
      return SEQ_UI_Button_Track(depressed, 0);
1484
    case 0x26: // D-2
1485
      return SEQ_UI_Button_GP(depressed, 1);
1486
    case 0x27: // D#2
1487
      return SEQ_UI_Button_Track(depressed, 1);
1488
    case 0x28: // E-2
1489
      return SEQ_UI_Button_GP(depressed, 2);
1490
    case 0x29: // F-2
1491
      return SEQ_UI_Button_GP(depressed, 3);
1492
    case 0x2a: // F#2
1493
      return SEQ_UI_Button_Track(depressed, 2);
1494
    case 0x2b: // G-2
1495
      return SEQ_UI_Button_GP(depressed, 4);
1496
    case 0x2c: // G#2
1497
      return SEQ_UI_Button_Track(depressed, 3);
1498
    case 0x2d: // A-2
1499
      return SEQ_UI_Button_GP(depressed, 5);
1500
    case 0x2e: // A#2
1501
      return SEQ_UI_Button_ParLayer(depressed, 0);
1502
    case 0x2f: // B-2
1503
      return SEQ_UI_Button_GP(depressed, 6);
1504
 
1505
    case 0x30: // C-3
1506
      return SEQ_UI_Button_GP(depressed, 7);
1507
    case 0x31: // C#3
1508
      return SEQ_UI_Button_ParLayer(depressed, 1);
1509
    case 0x32: // D-3
1510
      return SEQ_UI_Button_GP(depressed, 8);
1511
    case 0x33: // D#3
1512
      return SEQ_UI_Button_ParLayer(depressed, 2);
1513
    case 0x34: // E-3
1514
      return SEQ_UI_Button_GP(depressed, 9);
1515
    case 0x35: // F-3
1516
      return SEQ_UI_Button_GP(depressed, 10);
1517
    case 0x36: // F#3
1518
      return SEQ_UI_Button_TrgLayer(depressed, 0);
1519
    case 0x37: // G-3
1520
      return SEQ_UI_Button_GP(depressed, 11);
1521
    case 0x38: // G#3
1522
      return SEQ_UI_Button_TrgLayer(depressed, 1);
1523
    case 0x39: // A-3
1524
      return SEQ_UI_Button_GP(depressed, 12);
1525
    case 0x3a: // A#3
1526
      return SEQ_UI_Button_TrgLayer(depressed, 2);
1527
    case 0x3b: // B-3
1528
      return SEQ_UI_Button_GP(depressed, 13);
1529
 
1530
    case 0x3c: // C-4
1531
      return SEQ_UI_Button_GP(depressed, 14);
1532
    case 0x3d: // C#4
1533
      return SEQ_UI_Button_Group(depressed, 0);
1534
    case 0x3e: // D-4
1535
      return SEQ_UI_Button_GP(depressed, 15);
1536
    case 0x3f: // D#4
1537
      return SEQ_UI_Button_Group(depressed, 1);
1538
    case 0x40: // E-4
1539
      return 0; // ignore
1540
    case 0x41: // F-4
1541
      return SEQ_UI_Button_StepView(depressed);
1542
    case 0x42: // F#4
1543
      return SEQ_UI_Button_Group(depressed, 1);
1544
    case 0x43: // G-4
1545
      return 0; // ignore
1546
    case 0x44: // G#4
1547
      return SEQ_UI_Button_Group(depressed, 2);
1548
    case 0x45: // A-4
1549
      return SEQ_UI_Button_Left(depressed);
1550
    case 0x46: // A#4
1551
      return SEQ_UI_Button_ToggleGate(depressed);
1552
    case 0x47: // B-4
1553
      return SEQ_UI_Button_Right(depressed);
1554
 
1555
    case 0x48: // C-5
1556
      return SEQ_UI_Button_Edit(depressed);
1557
    case 0x49: // C#5
1558
      return SEQ_UI_Button_Solo(depressed);
1559
    case 0x4a: // D-5
1560
      return SEQ_UI_Button_Mute(depressed);
1561
    case 0x4b: // D#5
1562
      return SEQ_UI_Button_All(depressed);
1563
    case 0x4c: // E-5
1564
      return SEQ_UI_Button_Pattern(depressed);
1565
    case 0x4d: // F-5
1566
      return SEQ_UI_Button_Song(depressed);
1567
    case 0x4e: // F#5
1568
      return SEQ_UI_Button_Fast(depressed);
1569
    case 0x4f: // G-5
1570
      return SEQ_UI_Button_Metronome(depressed);
1571
    case 0x50: // G#5
1572
      return SEQ_UI_Button_ExtRestart(depressed);
1573
    case 0x51: // A-5
1574
      return SEQ_UI_Button_ParLayerSel(depressed);
1575
    case 0x52: // A#5
1576
      return SEQ_UI_Button_TrackSel(depressed);
1577
    case 0x53: // B-5
1578
      return SEQ_UI_Button_Stop(depressed);
1579
 
1580
    case 0x54: // C-6
1581
      return SEQ_UI_Button_Play(depressed);
1582
    case 0x55: // C#6
1583
      return SEQ_UI_Button_Pause(depressed);
1584
    case 0x56: // D-6
1585
      return SEQ_UI_Button_Rew(depressed);
1586
    case 0x57: // D#6
1587
      return SEQ_UI_Button_Fwd(depressed);
1588
    case 0x58: // E-6
1589
      return SEQ_UI_Button_Utility(depressed);
1590
    case 0x59: // F-6
1591
      return SEQ_UI_Button_TempoPreset(depressed);
1592
    case 0x5a: // F#6
1593
      return 0; // ignore
1594
    case 0x5b: // G-6
1595
      return SEQ_UI_Button_Menu(depressed);
1596
    case 0x5c: // G#6
1597
      return SEQ_UI_Button_Select(depressed);
1598
    case 0x5d: // A-6
1599
      return SEQ_UI_Button_Exit(depressed);
1600
    case 0x5e: // A#6
1601
      return SEQ_UI_Button_Down(depressed);
1602
    case 0x5f: // B-6
1603
      return SEQ_UI_Button_Up(depressed);
1604
  }
1605
 
741 tk 1606
  return 0; // no error
1607
}
1608
 
1609
 
1610
/////////////////////////////////////////////////////////////////////////////
134 tk 1611
// Update LCD messages
1612
// Usually called from background task
1613
/////////////////////////////////////////////////////////////////////////////
1614
s32 SEQ_UI_LCD_Handler(void)
1615
{
728 tk 1616
  static u8 boot_animation_wait_ctr = 0;
1617
  static u8 boot_animation_lcd_pos = 0;
1618
 
625 tk 1619
  // special handling in remote client mode
626 tk 1620
  if( seq_ui_remote_active_mode == SEQ_UI_REMOTE_MODE_CLIENT )
625 tk 1621
    return SEQ_UI_LCD_Update();
1622
 
159 tk 1623
  if( seq_ui_display_init_req ) {
1624
    seq_ui_display_init_req = 0; // clear request
134 tk 1625
 
278 tk 1626
    // clear force update of LCD
1627
    SEQ_LCD_Clear();
1628
    SEQ_LCD_Update(1);
134 tk 1629
 
168 tk 1630
    // select first menu item
1631
    ui_selected_item = 0;
1632
 
167 tk 1633
    // call init function of current page
272 tk 1634
    if( ui_menu_pages[ui_page].init_callback != NULL )
1635
      ui_menu_pages[ui_page].init_callback(0); // mode
134 tk 1636
 
1637
    // request display update
159 tk 1638
    seq_ui_display_update_req = 1;
134 tk 1639
  }
1640
 
496 tk 1641
  // print boot screen so long hardware config hasn't been read
1642
  if( !SEQ_FILE_HW_ConfigLocked() ) {
728 tk 1643
    if( boot_animation_lcd_pos < (40-3) ) {
1644
      if( ++boot_animation_wait_ctr >= 75 ) {
1645
    boot_animation_wait_ctr = 0;
637 tk 1646
 
728 tk 1647
    if( boot_animation_lcd_pos == 0 ) {
1648
      SEQ_LCD_Clear();
1649
      SEQ_LCD_CursorSet(0, 0);
1650
      SEQ_LCD_PrintString(MIOS32_LCD_BOOT_MSG_LINE1 " " MIOS32_LCD_BOOT_MSG_LINE2);
1651
      SEQ_LCD_CursorSet(0, 1);
1652
      SEQ_LCD_PrintString("Searching for SD Card...");
1653
    }
1654
 
1655
    // logo is print on second LCD
1656
    SEQ_LCD_LOGO_Print(boot_animation_lcd_pos++);
1657
      }
1658
    }
740 tk 1659
  } else if( seq_ui_backup_req || seq_ui_format_req ) {
634 tk 1660
    SEQ_LCD_Clear();
1661
    SEQ_LCD_CursorSet(0, 0);
740 tk 1662
    //                     <-------------------------------------->
1663
    //                     0123456789012345678901234567890123456789
1664
    if( seq_ui_backup_req )
1665
      SEQ_LCD_PrintString("Creating File Backup - be patient!!!");
1666
    else if( seq_ui_format_req )
1667
      SEQ_LCD_PrintString("Formatting Files - be patient!!!");
1668
    else
1669
      SEQ_LCD_PrintString("Don't know what I'm doing! :-/");
637 tk 1670
 
1671
    if( seq_file_backup_notification != NULL ) {
1672
      int i;
1673
 
1674
      SEQ_LCD_CursorSet(0, 1);
634 tk 1675
      SEQ_LCD_PrintFormattedString("Creating '%s'", seq_file_backup_notification);
637 tk 1676
 
1677
      SEQ_LCD_CursorSet(40+3, 0);
1678
      SEQ_LCD_PrintString("Total: [");
1679
      for(i=0; i<20; ++i)
1680
    SEQ_LCD_PrintChar((i>(seq_file_backup_percentage/5)) ? ' ' : '#');
1681
      SEQ_LCD_PrintFormattedString("] %3d%%", seq_file_backup_percentage);
1682
 
1683
      SEQ_LCD_CursorSet(40+3, 1);
1684
      SEQ_LCD_PrintString("File:  [");
1685
      for(i=0; i<20; ++i)
1686
    SEQ_LCD_PrintChar((i>(seq_file_copy_percentage/5)) ? ' ' : '#');
1687
      SEQ_LCD_PrintFormattedString("] %3d%%", seq_file_copy_percentage);
1688
    }
1689
 
496 tk 1690
  } else if( seq_ui_button_state.MENU_PRESSED && !seq_ui_button_state.MENU_FIRST_PAGE_SELECTED ) {
1691
    SEQ_LCD_CursorSet(0, 0);
1692
    //                   <-------------------------------------->
1693
    //                   0123456789012345678901234567890123456789
306 tk 1694
    SEQ_LCD_PrintString("Menu Shortcuts:");
1695
    SEQ_LCD_PrintSpaces(25 + 40);
1696
    SEQ_LCD_CursorSet(0, 1);
1697
    SEQ_LCD_PrintString(UI_SHORTCUT_STR); // defined in seq_ui_pages.inc
1698
  } else {
1699
    // perform high priority LCD update request
1700
    if( ui_lcd_callback != NULL )
1701
      ui_lcd_callback(1); // high_prio
167 tk 1702
 
306 tk 1703
    // perform low priority LCD update request if requested
1704
    if( seq_ui_display_update_req ) {
1705
      seq_ui_display_update_req = 0; // clear request
328 tk 1706
 
1707
      // ensure that selections are matching with track constraints
1708
      SEQ_UI_CheckSelections();
1709
 
306 tk 1710
      if( ui_lcd_callback != NULL )
1711
    ui_lcd_callback(0); // no high_prio
1712
    }
134 tk 1713
  }
1714
 
353 tk 1715
  // transfer all changed characters to LCD
1716
  SEQ_UI_LCD_Update();
1717
 
1718
  return 0; // no error
1719
}
1720
 
1721
 
1722
/////////////////////////////////////////////////////////////////////////////
1723
// Called from SEQ_UI_LCD_Handler(), but optionally also from other tasks
1724
// to update the LCD screen immediately
1725
/////////////////////////////////////////////////////////////////////////////
1726
s32 SEQ_UI_LCD_Update(void)
1727
{
625 tk 1728
  // special handling in remote client mode
626 tk 1729
  if( seq_ui_remote_active_mode == SEQ_UI_REMOTE_MODE_CLIENT ) {
1730
    MIOS32_IRQ_Disable();
1731
    u8 force = seq_ui_remote_force_lcd_update;
1732
    seq_ui_remote_force_lcd_update = 0;
1733
    MIOS32_IRQ_Enable();
1734
    return SEQ_LCD_Update(force);
625 tk 1735
  }
1736
 
596 tk 1737
  // if UI message active: copy over the text
1738
  if( ui_msg_ctr ) {
1739
    const char *animation_l_ptr;
1740
    const char *animation_r_ptr;
1741
    u8 msg_x = 0;
1742
    u8 right_aligned = 0;
740 tk 1743
    u8 disable_message = 0;
299 tk 1744
 
596 tk 1745
    switch( ui_msg_type ) {
1746
      case SEQ_UI_MSG_SDCARD: {
1747
    //                             00112233
1748
    const char animation_l[2*4] = "   >>>> ";
1749
    //                             00112233
1750
    const char animation_r[2*4] = "  < << <";
1751
    animation_l_ptr = animation_l;
1752
    animation_r_ptr = animation_r;
1753
    msg_x = 0; // MEMO: print such important information at first LCD for the case the user hasn't connected the second LCD yet
1754
    right_aligned = 0;
1755
      } break;
1756
 
740 tk 1757
      case SEQ_UI_MSG_DELAYED_ACTION: {
1758
    //                             00112233
1759
    const char animation_l[2*4] = "   )))) ";
1760
    //                             00112233
1761
    const char animation_r[2*4] = "  ( (( (";
1762
    animation_l_ptr = animation_l;
1763
    animation_r_ptr = animation_r;
1764
    msg_x = 39;
1765
    right_aligned = 1;
1766
 
1767
    if( ui_delayed_action_callback == NULL ) {
1768
      disable_message = 1; // button has been depressed before delay
1769
    } else {
1770
      int seconds = (ui_delayed_action_ctr / 1000) + 1;
1771
      if( seconds == 1 )
1772
        sprintf(ui_msg[0], "Hold for 1 second ");
1773
      else
1774
        sprintf(ui_msg[0], "Hold for %d seconds", seconds);
1775
    }
1776
      } break;
1777
 
741 tk 1778
      case SEQ_UI_MSG_USER_R: {
1779
    //                             00112233
1780
    const char animation_l[2*4] = "   **** ";
1781
    //                             00112233
1782
    const char animation_r[2*4] = "  * ** *";
1783
    animation_l_ptr = animation_l;
1784
    animation_r_ptr = animation_r;
1785
    msg_x = 40; // right display
1786
    right_aligned = 0;
1787
      } break;
1788
 
596 tk 1789
      default: {
1790
    //                             00112233
1791
    const char animation_l[2*4] = "   **** ";
1792
    //                             00112233
1793
    const char animation_r[2*4] = "  * ** *";
1794
    animation_l_ptr = animation_l;
1795
    animation_r_ptr = animation_r;
1796
    msg_x = 39;
1797
    right_aligned = 1;
1798
      } break;
1799
 
1800
    }
1801
 
740 tk 1802
    if( !disable_message ) {
1803
      int anum = (ui_msg_ctr % 1000) / 250;
596 tk 1804
 
740 tk 1805
      int len[2];
1806
      len[0] = strlen((char *)ui_msg[0]);
1807
      len[1] = strlen((char *)ui_msg[1]);
1808
      int len_max = len[0];
1809
      if( len[1] > len_max )
1810
    len_max = len[1];
596 tk 1811
 
740 tk 1812
      if( right_aligned )
1813
    msg_x -= (9 + len_max);
596 tk 1814
 
740 tk 1815
      int line;
1816
      for(line=0; line<2; ++line) {
1817
    SEQ_LCD_CursorSet(msg_x, line);
596 tk 1818
 
740 tk 1819
    // ensure that both lines are padded with same number of spaces
1820
    int end_pos = len[line];
1821
    while( end_pos < len_max )
1822
      ui_msg[line][end_pos++] = ' ';
1823
    ui_msg[line][end_pos] = 0;
1824
 
1825
    SEQ_LCD_PrintFormattedString(" %c%c| %s |%c%c ",
1826
                     *(animation_l_ptr + 2*anum + 0), *(animation_l_ptr + 2*anum + 1),
1827
                     (char *)ui_msg[line],
1828
                     *(animation_r_ptr + 2*anum + 0), *(animation_r_ptr + 2*anum + 1));
1829
      }
299 tk 1830
    }
1831
  }
1832
 
637 tk 1833
  // MSD USB notification at right corner if not in Disk page
1834
  // to warn user that USB MIDI is disabled and seq performance is bad now!
638 tk 1835
  if( (TASK_MSD_EnableGet() > 0) && ui_page != SEQ_UI_PAGE_DISK ) {
637 tk 1836
    SEQ_LCD_CursorSet(80-11, 0);
1837
    if( ui_cursor_flash ) SEQ_LCD_PrintSpaces(13); else SEQ_LCD_PrintString(" [MSD USB] ");
1838
    SEQ_LCD_CursorSet(80-11, 1);
1839
    char str[5];
1840
    TASK_MSD_FlagStrGet(str);
1841
    if( ui_cursor_flash ) SEQ_LCD_PrintSpaces(13); else SEQ_LCD_PrintFormattedString(" [ %s  ] ", str);
1842
  }
596 tk 1843
 
278 tk 1844
  // transfer all changed characters to LCD
353 tk 1845
  // SEQ_LCD_Update provides a MUTEX handling to allow updates from different tasks
1846
  return SEQ_LCD_Update(0);
134 tk 1847
}
1848
 
1849
 
1850
/////////////////////////////////////////////////////////////////////////////
1851
// Update all LEDs
1852
// Usually called from background task
1853
/////////////////////////////////////////////////////////////////////////////
1854
s32 SEQ_UI_LED_Handler(void)
1855
{
626 tk 1856
  static u8 remote_led_sr[SEQ_LED_NUM_SR];
1857
 
524 tk 1858
  int i;
1859
 
625 tk 1860
  // ignore in remote client mode
626 tk 1861
  if( seq_ui_remote_active_mode == SEQ_UI_REMOTE_MODE_CLIENT )
625 tk 1862
    return 0; // no error
1863
 
496 tk 1864
  // ignore so long hardware config hasn't been read
1865
  if( !SEQ_FILE_HW_ConfigLocked() )
1866
    return -1;
1867
 
134 tk 1868
  // track LEDs
484 tk 1869
  u8 selected_tracks = ui_selected_tracks >> (4*ui_selected_group);
492 tk 1870
  SEQ_LED_PinSet(seq_hwcfg_led.track[0], (selected_tracks & (1 << 0)));
1871
  SEQ_LED_PinSet(seq_hwcfg_led.track[1], (selected_tracks & (1 << 1)));
1872
  SEQ_LED_PinSet(seq_hwcfg_led.track[2], (selected_tracks & (1 << 2)));
1873
  SEQ_LED_PinSet(seq_hwcfg_led.track[3], (selected_tracks & (1 << 3)));
678 tk 1874
  SEQ_LED_PinSet(seq_hwcfg_led.track_sel, ui_page == SEQ_UI_PAGE_TRACKSEL);
134 tk 1875
 
1876
  // parameter layer LEDs
492 tk 1877
  SEQ_LED_PinSet(seq_hwcfg_led.par_layer[0], (ui_selected_par_layer == 0));
1878
  SEQ_LED_PinSet(seq_hwcfg_led.par_layer[1], (ui_selected_par_layer == 1));
1879
  SEQ_LED_PinSet(seq_hwcfg_led.par_layer[2], (ui_selected_par_layer >= 2) || seq_ui_button_state.PAR_LAYER_SEL);
678 tk 1880
  SEQ_LED_PinSet(seq_hwcfg_led.par_layer_sel, ui_page == SEQ_UI_PAGE_PARSEL);
134 tk 1881
 
1882
  // group LEDs
492 tk 1883
  SEQ_LED_PinSet(seq_hwcfg_led.group[0], (ui_selected_group == 0));
1884
  SEQ_LED_PinSet(seq_hwcfg_led.group[1], (ui_selected_group == 1));
1885
  SEQ_LED_PinSet(seq_hwcfg_led.group[2], (ui_selected_group == 2));
1886
  SEQ_LED_PinSet(seq_hwcfg_led.group[3], (ui_selected_group == 3));
134 tk 1887
 
1888
  // trigger layer LEDs
492 tk 1889
  SEQ_LED_PinSet(seq_hwcfg_led.trg_layer[0], (ui_selected_trg_layer == 0));
1890
  SEQ_LED_PinSet(seq_hwcfg_led.trg_layer[1], (ui_selected_trg_layer == 1));
1891
  SEQ_LED_PinSet(seq_hwcfg_led.trg_layer[2], (ui_selected_trg_layer >= 2) || seq_ui_button_state.TRG_LAYER_SEL);
678 tk 1892
  SEQ_LED_PinSet(seq_hwcfg_led.trg_layer_sel, ui_page == SEQ_UI_PAGE_TRGSEL);
134 tk 1893
 
1894
  // remaining LEDs
492 tk 1895
  SEQ_LED_PinSet(seq_hwcfg_led.edit, ui_page == SEQ_UI_PAGE_EDIT);
1896
  SEQ_LED_PinSet(seq_hwcfg_led.mute, ui_page == SEQ_UI_PAGE_MUTE);
1897
  SEQ_LED_PinSet(seq_hwcfg_led.pattern, ui_page == SEQ_UI_PAGE_PATTERN);
399 tk 1898
  if( SEQ_SONG_ActiveGet() )
492 tk 1899
    SEQ_LED_PinSet(seq_hwcfg_led.song, 1);
399 tk 1900
  else
492 tk 1901
    SEQ_LED_PinSet(seq_hwcfg_led.song, ui_cursor_flash ? 0 : (ui_page == SEQ_UI_PAGE_SONG));
743 tk 1902
  SEQ_LED_PinSet(seq_hwcfg_led.morph, ui_page == SEQ_UI_PAGE_TRKMORPH);
1903
  SEQ_LED_PinSet(seq_hwcfg_led.mixer, ui_page == SEQ_UI_PAGE_MIXER);
1904
  SEQ_LED_PinSet(seq_hwcfg_led.transpose, ui_page == SEQ_UI_PAGE_TRKTRAN);
134 tk 1905
 
492 tk 1906
  SEQ_LED_PinSet(seq_hwcfg_led.solo, seq_ui_button_state.SOLO);
1907
  SEQ_LED_PinSet(seq_hwcfg_led.fast, seq_ui_button_state.FAST_ENCODERS);
1908
  SEQ_LED_PinSet(seq_hwcfg_led.all, seq_ui_button_state.CHANGE_ALL_STEPS);
134 tk 1909
 
492 tk 1910
  SEQ_LED_PinSet(seq_hwcfg_led.play, SEQ_BPM_IsRunning());
1911
  SEQ_LED_PinSet(seq_hwcfg_led.stop, !SEQ_BPM_IsRunning() && !ui_seq_pause);
1912
  SEQ_LED_PinSet(seq_hwcfg_led.pause, ui_seq_pause);
240 tk 1913
 
492 tk 1914
  SEQ_LED_PinSet(seq_hwcfg_led.rew, seq_ui_button_state.REW);
1915
  SEQ_LED_PinSet(seq_hwcfg_led.fwd, seq_ui_button_state.FWD);
596 tk 1916
 
1917
  SEQ_LED_PinSet(seq_hwcfg_led.loop, seq_core_state.LOOP);
134 tk 1918
 
678 tk 1919
  SEQ_LED_PinSet(seq_hwcfg_led.step_view, ui_page == SEQ_UI_PAGE_STEPSEL);
134 tk 1920
 
743 tk 1921
  SEQ_LED_PinSet(seq_hwcfg_led.exit, ui_page == SEQ_UI_PAGE_MENU);
591 tk 1922
  SEQ_LED_PinSet(seq_hwcfg_led.select, seq_ui_button_state.SELECT_PRESSED);
492 tk 1923
  SEQ_LED_PinSet(seq_hwcfg_led.menu, seq_ui_button_state.MENU_PRESSED);
134 tk 1924
 
596 tk 1925
  // handle double functions
1926
  if( seq_ui_button_state.MENU_PRESSED ) {
1927
    SEQ_LED_PinSet(seq_hwcfg_led.scrub, seq_core_state.LOOP);
1928
    SEQ_LED_PinSet(seq_hwcfg_led.metronome, seq_core_state.EXT_RESTART_REQ);
1929
  } else {
1930
    SEQ_LED_PinSet(seq_hwcfg_led.scrub, seq_ui_button_state.SCRUB);
1931
    SEQ_LED_PinSet(seq_hwcfg_led.metronome, seq_core_state.METRONOME);
1932
  }
1933
 
630 tk 1934
  SEQ_LED_PinSet(seq_hwcfg_led.record, ui_page == SEQ_UI_PAGE_TRKREC);
1935
 
492 tk 1936
  SEQ_LED_PinSet(seq_hwcfg_led.utility, ui_page == SEQ_UI_PAGE_UTIL);
1937
  SEQ_LED_PinSet(seq_hwcfg_led.copy, seq_ui_button_state.COPY);
1938
  SEQ_LED_PinSet(seq_hwcfg_led.paste, seq_ui_button_state.PASTE);
1939
  SEQ_LED_PinSet(seq_hwcfg_led.clear, seq_ui_button_state.CLEAR);
240 tk 1940
 
513 tk 1941
  SEQ_LED_PinSet(seq_hwcfg_led.tap_tempo, seq_ui_button_state.TAP_TEMPO);
678 tk 1942
  SEQ_LED_PinSet(seq_hwcfg_led.tempo_preset, ui_page == SEQ_UI_PAGE_BPM_PRESETS);
524 tk 1943
  SEQ_LED_PinSet(seq_hwcfg_led.ext_restart, seq_core_state.EXT_RESTART_REQ);
240 tk 1944
 
492 tk 1945
  SEQ_LED_PinSet(seq_hwcfg_led.down, seq_ui_button_state.DOWN);
1946
  SEQ_LED_PinSet(seq_hwcfg_led.up, seq_ui_button_state.UP);
240 tk 1947
 
167 tk 1948
 
306 tk 1949
  // in MENU page: overrule GP LEDs so long MENU button is pressed/active
1950
  if( seq_ui_button_state.MENU_PRESSED ) {
1951
    if( ui_cursor_flash ) // if flashing flag active: no LED flag set
1952
      ui_gp_leds = 0x0000;
1953
    else {
1954
      int i;
1955
      u16 new_ui_gp_leds = 0x0000;
1956
      for(i=0; i<16; ++i)
1957
    if( ui_page == ui_shortcut_menu_pages[i] )
1958
      new_ui_gp_leds |= (1 << i);
1959
      ui_gp_leds = new_ui_gp_leds;
1960
    }
1961
  } else {
1962
    // note: the background function is permanently interrupted - therefore we write the GP pattern
1963
    // into a temporary variable, and take it over once completed
1964
    u16 new_ui_gp_leds = 0x0000;
1965
    // request GP LED values from current menu page
1966
    // will be transfered to DOUT registers in SEQ_UI_LED_Handler_Periodic
1967
    new_ui_gp_leds = 0x0000;
206 tk 1968
 
306 tk 1969
    if( ui_led_callback != NULL )
1970
      ui_led_callback(&new_ui_gp_leds);
167 tk 1971
 
306 tk 1972
    ui_gp_leds = new_ui_gp_leds;
1973
  }
1974
 
524 tk 1975
 
1976
  // BLM LEDs
1977
  u8 visible_track0 = 4*ui_selected_group;
1978
  u8 visible_sr0  = 2*ui_selected_step_view;
1979
 
1980
  for(i=0; i<NUM_BLM_LED_SRS; ++i)
1981
    ui_blm_leds[i] = SEQ_TRG_Get8(visible_track0+(i>>1), visible_sr0+(i&1), ui_selected_trg_layer, ui_selected_instrument);
1982
 
626 tk 1983
 
1984
  // send LED changes in remote server mode
1985
  if( seq_ui_remote_mode == SEQ_UI_REMOTE_MODE_SERVER || seq_ui_remote_active_mode == SEQ_UI_REMOTE_MODE_SERVER ) {
1986
    int first_sr = -1;
1987
    int last_sr = -1;
1988
    int sr;
1989
    for(sr=0; sr<SEQ_LED_NUM_SR; ++sr) {
1990
      u8 value = SEQ_LED_SRGet(sr);
1991
      if( value != remote_led_sr[sr] ) {
1992
    if( first_sr == -1 )
1993
      first_sr = sr;
1994
    last_sr = sr;
1995
    remote_led_sr[sr] = value;
1996
      }
1997
    }
1998
 
1999
    MIOS32_IRQ_Disable();
2000
    if( seq_ui_remote_force_led_update ) {
2001
      first_sr = 0;
2002
      last_sr = SEQ_LED_NUM_SR-1;
2003
    }
2004
    seq_ui_remote_force_led_update = 0;
2005
    MIOS32_IRQ_Enable();
2006
 
2007
    if( first_sr >= 0 )
2008
      SEQ_MIDI_SYSEX_REMOTE_Server_SendLED(first_sr, (u8 *)&remote_led_sr[first_sr], last_sr-first_sr+1);
2009
  }
2010
 
2011
 
134 tk 2012
  return 0; // no error
2013
}
2014
 
2015
 
2016
/////////////////////////////////////////////////////////////////////////////
2017
// updates high-prio LED functions (GP LEDs and Beat LED)
168 tk 2018
// called each mS
134 tk 2019
/////////////////////////////////////////////////////////////////////////////
2020
s32 SEQ_UI_LED_Handler_Periodic()
2021
{
625 tk 2022
  // ignore in remote client mode
626 tk 2023
  if( seq_ui_remote_active_mode == SEQ_UI_REMOTE_MODE_CLIENT )
625 tk 2024
    return 0; // no error
2025
 
496 tk 2026
  // ignore so long hardware config hasn't been read
2027
  if( !SEQ_FILE_HW_ConfigLocked() )
2028
    return -1;
2029
 
134 tk 2030
  // GP LEDs are only updated when ui_gp_leds or pos_marker_mask has changed
2031
  static u16 prev_ui_gp_leds = 0x0000;
2032
  static u16 prev_pos_marker_mask = 0x0000;
2033
 
2034
  // beat LED: tmp. for demo w/o real sequencer
193 tk 2035
  u8 sequencer_running = SEQ_BPM_IsRunning();
492 tk 2036
  SEQ_LED_PinSet(seq_hwcfg_led.beat, sequencer_running && ((seq_core_state.ref_step & 3) == 0));
134 tk 2037
 
2038
  // for song position marker (supports 16 LEDs, check for selected step view)
2039
  u16 pos_marker_mask = 0x0000;
325 tk 2040
  u8 visible_track = SEQ_UI_VisibleTrackGet();
2041
  u8 played_step = seq_core_trk[visible_track].step;
513 tk 2042
  if( seq_ui_button_state.STEP_VIEW ) {
2043
    // if STEP_VIEW button pressed: pos marker correlated to zoom ratio
325 tk 2044
    if( sequencer_running )
2045
      pos_marker_mask = 1 << (played_step / (SEQ_TRG_NumStepsGet(visible_track)/16));
2046
  } else {
2047
    if( sequencer_running && (played_step >> 4) == ui_selected_step_view )
2048
      pos_marker_mask = 1 << (played_step & 0xf);
2049
  }
134 tk 2050
 
2051
  // exit of pattern hasn't changed
173 tk 2052
  if( prev_ui_gp_leds == ui_gp_leds && prev_pos_marker_mask == pos_marker_mask )
134 tk 2053
    return 0;
173 tk 2054
  prev_ui_gp_leds = ui_gp_leds;
134 tk 2055
  prev_pos_marker_mask = pos_marker_mask;
2056
 
2057
  // transfer to GP LEDs
514 tk 2058
  if( seq_hwcfg_led.gp_dout_l_sr ) {
2059
    if( seq_hwcfg_led.gp_dout_l2_sr )
2060
      SEQ_LED_SRSet(seq_hwcfg_led.gp_dout_l_sr-1, (ui_gp_leds >> 0) & 0xff);
496 tk 2061
    else
514 tk 2062
      SEQ_LED_SRSet(seq_hwcfg_led.gp_dout_l_sr-1, ((ui_gp_leds ^ pos_marker_mask) >> 0) & 0xff);
496 tk 2063
  }
134 tk 2064
 
514 tk 2065
  if( seq_hwcfg_led.gp_dout_r_sr ) {
2066
    if( seq_hwcfg_led.gp_dout_r2_sr )
2067
      SEQ_LED_SRSet(seq_hwcfg_led.gp_dout_r_sr-1, (ui_gp_leds >> 8) & 0xff);
496 tk 2068
    else
514 tk 2069
      SEQ_LED_SRSet(seq_hwcfg_led.gp_dout_r_sr-1, ((ui_gp_leds ^ pos_marker_mask) >> 8) & 0xff);
496 tk 2070
  }
134 tk 2071
 
514 tk 2072
  if( seq_hwcfg_led.gp_dout_l2_sr )
2073
    SEQ_LED_SRSet(seq_hwcfg_led.gp_dout_l2_sr-1, (pos_marker_mask >> 0) & 0xff);
2074
  if( seq_hwcfg_led.gp_dout_r2_sr )
2075
    SEQ_LED_SRSet(seq_hwcfg_led.gp_dout_r2_sr-1, (pos_marker_mask >> 8) & 0xff);
496 tk 2076
 
524 tk 2077
 
2078
  if( seq_hwcfg_blm.enabled ) {
2079
    // Red LEDs (position marker)
2080
    int track_ix;
2081
    for(track_ix=0; track_ix<(NUM_BLM_LED_SRS/2); ++track_ix) {
2082
      u16 pos_marker_mask = 0x0000;
2083
      if( sequencer_running ) {
2084
    u8 track = ui_selected_group + track_ix;
2085
    u8 played_step = seq_core_trk[track].step;
2086
    if( (played_step >> 4) == ui_selected_step_view )
2087
      pos_marker_mask = 1 << (played_step & 0xf);
2088
      }
2089
 
2090
      // Prepare Green LEDs (triggers)
2091
      u8 green_l = ui_blm_leds[2*track_ix+0];
2092
      u8 green_r = ui_blm_leds[2*track_ix+1];
2093
 
2094
      // Red LEDs (position marker)
2095
      if( seq_hwcfg_blm.dout_duocolour ) {
2096
    BLM_DOUT_SRSet(1, 2*track_ix+0, pos_marker_mask & 0xff);
2097
    BLM_DOUT_SRSet(1, 2*track_ix+1, (pos_marker_mask >> 8) & 0xff);
2098
 
2099
    if( seq_hwcfg_blm.dout_duocolour == 2 ) {
2100
      // Colour Mode 2: clear green LED, so that only one LED is lit
2101
      if( pos_marker_mask & 0x00ff )
2102
        green_l &= ~pos_marker_mask;
2103
      else if( pos_marker_mask & 0xff00 )
2104
        green_r &= ~(pos_marker_mask >> 8);
2105
    }
2106
      } else {
2107
    // If Duo-LEDs not enabled: invert Green LEDs
2108
    if( pos_marker_mask & 0x00ff )
2109
      green_l ^= pos_marker_mask;
2110
    else if( pos_marker_mask & 0xff00 )
2111
      green_r ^= (pos_marker_mask >> 8);
2112
      }
2113
 
2114
      // Set Green LEDs
2115
      BLM_DOUT_SRSet(0, 2*track_ix+0, green_l);
2116
      BLM_DOUT_SRSet(0, 2*track_ix+1, green_r);
2117
    }
2118
  }
2119
 
2120
 
514 tk 2121
  if( seq_hwcfg_blm8x8.enabled && seq_hwcfg_blm8x8.dout_gp_mapping ) {
513 tk 2122
    // for wilba's frontpanel
134 tk 2123
 
513 tk 2124
    // BLM_X DOUT -> GP LED mapping
2125
    // 0 = 15,16    1 = 13,14   2 = 11,12   3 = 9,10
2126
    // 4 = 1,2  5 = 3,4     6 = 5,6     7 = 7,8
134 tk 2127
 
513 tk 2128
    // bit 7: first green (i.e. GP1-G)
2129
    // bit 6: first red (i.e. GP1-R)
2130
    // bit 5: second green (i.e. GP2-G)
2131
    // bit 4: second red (i.e. GP2-R)
134 tk 2132
 
513 tk 2133
    // this mapping routine takes ca. 5 uS
2134
    // since it's only executed when ui_gp_leds or gp_mask has changed, it doesn't really hurt
134 tk 2135
 
513 tk 2136
    u16 modified_gp_leds = ui_gp_leds;
134 tk 2137
#if 1
513 tk 2138
    // extra: red LED is lit exclusively for higher contrast
2139
    modified_gp_leds &= ~pos_marker_mask;
134 tk 2140
#endif
2141
 
513 tk 2142
    int sr;
2143
    const u8 blm_x_sr_map[8] = {4, 5, 6, 7, 3, 2, 1, 0};
2144
    u16 gp_mask = 1 << 0;
2145
    for(sr=0; sr<8; ++sr) {
2146
      u8 pattern = 0;
134 tk 2147
 
513 tk 2148
      if( modified_gp_leds & gp_mask )
2149
    pattern |= 0x80;
2150
      if( pos_marker_mask & gp_mask )
2151
    pattern |= 0x40;
2152
      gp_mask <<= 1;
2153
      if( modified_gp_leds & gp_mask )
2154
    pattern |= 0x20;
2155
      if( pos_marker_mask & gp_mask )
2156
    pattern |= 0x10;
2157
      gp_mask <<= 1;
134 tk 2158
 
513 tk 2159
      u8 mapped_sr = blm_x_sr_map[sr];
2160
      BLM_X_LED_rows[mapped_sr][0] = (BLM_X_LED_rows[mapped_sr][0] & 0x0f) | pattern;
2161
    }
134 tk 2162
  }
2163
 
184 tk 2164
  return 0; // no error
134 tk 2165
}
2166
 
2167
 
2168
/////////////////////////////////////////////////////////////////////////////
168 tk 2169
// for menu handling (e.g. flashing cursor, doubleclick counter, etc...)
2170
// called each mS
2171
/////////////////////////////////////////////////////////////////////////////
2172
s32 SEQ_UI_MENU_Handler_Periodic()
2173
{
625 tk 2174
  // ignore in remote client mode
626 tk 2175
  if( seq_ui_remote_active_mode == SEQ_UI_REMOTE_MODE_CLIENT )
625 tk 2176
    return 0; // no error
2177
 
740 tk 2178
  if( ++ui_cursor_flash_ctr >= SEQ_UI_CURSOR_FLASH_CTR_MAX ) {
168 tk 2179
    ui_cursor_flash_ctr = 0;
740 tk 2180
    ++ui_cursor_flash_overrun_ctr;
2181
    seq_ui_display_update_req = 1;
2182
  }
2183
 
173 tk 2184
  // important: flash flag has to be recalculated on each invocation of this
2185
  // handler, since counter could also be reseted outside this function
652 tk 2186
  u8 old_ui_cursor_flash = ui_cursor_flash;
173 tk 2187
  ui_cursor_flash = ui_cursor_flash_ctr >= SEQ_UI_CURSOR_FLASH_CTR_LED_OFF;
652 tk 2188
  if( old_ui_cursor_flash != ui_cursor_flash )
2189
    seq_ui_display_update_req = 1;
168 tk 2190
 
240 tk 2191
  // used in some pages for temporary messages
416 tk 2192
  if( ui_hold_msg_ctr ) {
240 tk 2193
    --ui_hold_msg_ctr;
2194
 
416 tk 2195
    if( !ui_hold_msg_ctr )
2196
      seq_ui_display_update_req = 1;
2197
  }
2198
 
596 tk 2199
  // used for temporary messages
2200
  if( ui_msg_ctr )
2201
    --ui_msg_ctr;
299 tk 2202
 
184 tk 2203
  // VU meters (used in MUTE menu, could also be available as LED matrix...)
2204
  static u8 vu_meter_prediv = 0; // predivider for VU meters
2205
 
2206
  if( ++vu_meter_prediv >= 4 ) {
2207
    vu_meter_prediv = 0;
2208
 
333 tk 2209
 
2210
    portENTER_CRITICAL();
2211
 
184 tk 2212
    u8 track;
2213
    seq_core_trk_t *t = &seq_core_trk[0];
2214
    for(track=0; track<SEQ_CORE_NUM_TRACKS; ++t, ++track)
2215
      if( t->vu_meter )
2216
    --t->vu_meter;
333 tk 2217
 
2218
    int i;
2219
    u8 *vu_meter = (u8 *)&seq_layer_vu_meter[0];
2220
    for(i=0; i<sizeof(seq_layer_vu_meter); ++i, ++vu_meter) {
2221
      if( *vu_meter && !(*vu_meter & 0x80) ) // if bit 7 set: static value
2222
    *vu_meter -= 1;
2223
    }
2224
 
318 tk 2225
    portEXIT_CRITICAL();
184 tk 2226
  }
2227
 
740 tk 2228
  // delayed action will be triggered once counter reached 0
2229
  if( !ui_delayed_action_callback )
2230
    ui_delayed_action_ctr = 0;
2231
  else if( ui_delayed_action_ctr ) {
2232
    if( --ui_delayed_action_ctr == 0 ) {
2233
      ui_delayed_action_callback();
2234
      ui_delayed_action_callback = NULL;
2235
    }
2236
  }
2237
 
168 tk 2238
  return 0;
2239
}
2240
 
2241
 
2242
/////////////////////////////////////////////////////////////////////////////
328 tk 2243
// Should be regulary called to check if the layer/instrument/step selection
2244
// is valid for the current track
2245
// At least executed before button/encoder and LCD function calls
2246
/////////////////////////////////////////////////////////////////////////////
2247
s32 SEQ_UI_CheckSelections(void)
2248
{
492 tk 2249
  if( (ui_selected_tracks >> (4*ui_selected_group)) == 0 )
2250
    ui_selected_tracks = 1 << (4*ui_selected_group);
2251
 
328 tk 2252
  u8 visible_track = SEQ_UI_VisibleTrackGet();
2253
 
2254
  if( ui_selected_instrument >= SEQ_PAR_NumInstrumentsGet(visible_track) )
2255
    ui_selected_instrument = 0;
2256
 
2257
  if( ui_selected_par_layer >= SEQ_PAR_NumLayersGet(visible_track) )
2258
    ui_selected_par_layer = 0;
2259
 
2260
  if( ui_selected_trg_layer >= SEQ_TRG_NumLayersGet(visible_track) )
2261
    ui_selected_trg_layer = 0;
2262
 
2263
  if( ui_selected_step >= SEQ_TRG_NumStepsGet(visible_track) )
2264
    ui_selected_step = 0;
2265
 
2266
  if( ui_selected_step_view >= (SEQ_TRG_NumStepsGet(visible_track)/16) ) {
2267
    ui_selected_step_view = 0;
2268
    ui_selected_step %= 16;
2269
  }
2270
 
336 tk 2271
  if( ui_selected_step < (16*ui_selected_step_view) ||
2272
      ui_selected_step >= (16*(ui_selected_step_view+1)) )
328 tk 2273
    ui_selected_step_view = ui_selected_step / 16;
2274
 
2275
  return 0; // no error
2276
}
2277
 
2278
 
2279
/////////////////////////////////////////////////////////////////////////////
134 tk 2280
// Returns the currently visible track
2281
/////////////////////////////////////////////////////////////////////////////
2282
u8 SEQ_UI_VisibleTrackGet(void)
2283
{
2284
  u8 offset = 0;
2285
 
484 tk 2286
  u8 selected_tracks = ui_selected_tracks >> (4*ui_selected_group);
2287
  if( selected_tracks & (1 << 3) )
134 tk 2288
    offset = 3;
484 tk 2289
  if( selected_tracks & (1 << 2) )
134 tk 2290
    offset = 2;
484 tk 2291
  if( selected_tracks & (1 << 1) )
134 tk 2292
    offset = 1;
484 tk 2293
  if( selected_tracks & (1 << 0) )
134 tk 2294
    offset = 0;
2295
 
2296
  return 4*ui_selected_group + offset;
2297
}
2298
 
168 tk 2299
 
2300
/////////////////////////////////////////////////////////////////////////////
178 tk 2301
// Returns 1 if 'track' is selected
2302
/////////////////////////////////////////////////////////////////////////////
2303
s32 SEQ_UI_IsSelectedTrack(u8 track)
2304
{
484 tk 2305
  return (ui_selected_tracks & (1 << track)) ? 1 : 0;
178 tk 2306
}
2307
 
2308
 
2309
/////////////////////////////////////////////////////////////////////////////
240 tk 2310
// Sets a new selected step and updates the step view
2311
/////////////////////////////////////////////////////////////////////////////
2312
s32 SEQ_UI_SelectedStepSet(u8 step)
2313
{
2314
  ui_selected_step = step;
328 tk 2315
  SEQ_UI_CheckSelections();
2316
 
240 tk 2317
  return 0; // no error
2318
}
2319
 
2320
 
2321
/////////////////////////////////////////////////////////////////////////////
168 tk 2322
// Increments the selected tracks/groups
2323
// OUT: 1 if value has been changed, otherwise 0
2324
/////////////////////////////////////////////////////////////////////////////
2325
s32 SEQ_UI_GxTyInc(s32 incrementer)
2326
{
2327
  int gxty = SEQ_UI_VisibleTrackGet();
2328
  int prev_gxty = gxty;
2329
 
2330
  if( incrementer >= 0 ) {
2331
    if( (gxty += incrementer) >= SEQ_CORE_NUM_TRACKS )
2332
      gxty = SEQ_CORE_NUM_TRACKS-1;
2333
  } else {
2334
    if( (gxty += incrementer) < 0 )
2335
      gxty = 0;
2336
  }
2337
 
2338
  if( gxty == prev_gxty )
2339
    return 0; // no change
2340
 
484 tk 2341
  ui_selected_tracks = 1 << gxty;
168 tk 2342
  ui_selected_group = gxty / 4;
2343
 
2344
  return 1; // value changed
2345
}
2346
 
2347
 
2348
/////////////////////////////////////////////////////////////////////////////
236 tk 2349
// Increments a 16bit variable within given min/max range
2350
// OUT: 1 if value has been changed, otherwise 0
2351
/////////////////////////////////////////////////////////////////////////////
240 tk 2352
s32 SEQ_UI_Var16_Inc(u16 *value, u16 min, u16 max, s32 incrementer)
236 tk 2353
{
2354
  int new_value = *value;
2355
  int prev_value = new_value;
2356
 
2357
  if( incrementer >= 0 ) {
2358
    if( (new_value += incrementer) >= max )
2359
      new_value = max;
2360
  } else {
2361
    if( (new_value += incrementer) < min )
2362
      new_value = min;
2363
  }
2364
 
2365
  if( new_value == prev_value )
2366
    return 0; // no change
2367
 
2368
  *value = new_value;
2369
 
2370
  return 1; // value changed
2371
}
2372
 
240 tk 2373
/////////////////////////////////////////////////////////////////////////////
2374
// Increments an 8bit variable within given min/max range
2375
// OUT: 1 if value has been changed, otherwise 0
2376
/////////////////////////////////////////////////////////////////////////////
2377
s32 SEQ_UI_Var8_Inc(u8 *value, u16 min, u16 max, s32 incrementer)
2378
{
2379
  u16 tmp = *value;
2380
  if( SEQ_UI_Var16_Inc(&tmp, min, max, incrementer) ) {
2381
    *value = tmp;
2382
    return 1; // value changed
2383
  }
236 tk 2384
 
240 tk 2385
  return 0; // value hasn't been changed
2386
}
2387
 
2388
 
236 tk 2389
/////////////////////////////////////////////////////////////////////////////
168 tk 2390
// Increments a CC within given min/max range
2391
// OUT: 1 if value has been changed, otherwise 0
2392
/////////////////////////////////////////////////////////////////////////////
248 tk 2393
s32 SEQ_UI_CC_Inc(u8 cc, u8 min, u8 max, s32 incrementer)
168 tk 2394
{
2395
  u8 visible_track = SEQ_UI_VisibleTrackGet();
173 tk 2396
  int new_value = SEQ_CC_Get(visible_track, cc);
2397
  int prev_value = new_value;
168 tk 2398
 
2399
  if( incrementer >= 0 ) {
173 tk 2400
    if( (new_value += incrementer) >= max )
2401
      new_value = max;
168 tk 2402
  } else {
173 tk 2403
    if( (new_value += incrementer) < min )
2404
      new_value = min;
168 tk 2405
  }
2406
 
173 tk 2407
  if( new_value == prev_value )
168 tk 2408
    return 0; // no change
2409
 
173 tk 2410
  SEQ_CC_Set(visible_track, cc, new_value);
168 tk 2411
 
179 tk 2412
  // set same value for all selected tracks
2413
  u8 track;
2414
  for(track=0; track<SEQ_CORE_NUM_TRACKS; ++track)
2415
    if( track != visible_track && SEQ_UI_IsSelectedTrack(track) )
2416
      SEQ_CC_Set(track, cc, new_value);
2417
 
168 tk 2418
  return 1; // value changed
2419
}
2420
 
173 tk 2421
 
2422
/////////////////////////////////////////////////////////////////////////////
179 tk 2423
// Sets a CC value on all selected tracks
2424
// OUT: 1 if value has been changed, otherwise 0
2425
/////////////////////////////////////////////////////////////////////////////
248 tk 2426
s32 SEQ_UI_CC_Set(u8 cc, u8 value)
179 tk 2427
{
2428
  u8 visible_track = SEQ_UI_VisibleTrackGet();
2429
  int prev_value = SEQ_CC_Get(visible_track, cc);
2430
 
2431
  if( value == prev_value )
2432
    return 0; // no change
2433
 
2434
  SEQ_CC_Set(visible_track, cc, value);
2435
 
2436
  // set same value for all selected tracks
2437
  u8 track;
2438
  for(track=0; track<SEQ_CORE_NUM_TRACKS; ++track)
2439
    if( track != visible_track && SEQ_UI_IsSelectedTrack(track) )
2440
      SEQ_CC_Set(track, cc, value);
2441
 
2442
  return 1; // value changed
2443
}
2444
 
2445
/////////////////////////////////////////////////////////////////////////////
173 tk 2446
// Modifies a bitfield in a CC value to a given value
2447
// OUT: 1 if value has been changed, otherwise 0
2448
/////////////////////////////////////////////////////////////////////////////
248 tk 2449
s32 SEQ_UI_CC_SetFlags(u8 cc, u8 flag_mask, u8 value)
173 tk 2450
{
2451
  u8 visible_track = SEQ_UI_VisibleTrackGet();
2452
  int new_value = SEQ_CC_Get(visible_track, cc);
2453
  int prev_value = new_value;
2454
 
2455
  new_value = (new_value & ~flag_mask) | value;
2456
 
2457
  if( new_value == prev_value )
2458
    return 0; // no change
2459
 
2460
  SEQ_CC_Set(visible_track, cc, new_value);
2461
 
179 tk 2462
  // do same modification for all selected tracks
2463
  u8 track;
2464
  for(track=0; track<SEQ_CORE_NUM_TRACKS; ++track)
2465
    if( track != visible_track && SEQ_UI_IsSelectedTrack(track) ) {
2466
      int new_value = SEQ_CC_Get(track, cc);
2467
      new_value = (new_value & ~flag_mask) | value;
2468
      SEQ_CC_Set(track, cc, new_value);
2469
    }
2470
 
173 tk 2471
  return 1; // value changed
2472
}
2473
 
2474
 
299 tk 2475
/////////////////////////////////////////////////////////////////////////////
596 tk 2476
// Print temporary user messages (e.g. warnings, errors)
299 tk 2477
// expects mS delay and two lines, each up to 20 characters
2478
/////////////////////////////////////////////////////////////////////////////
596 tk 2479
s32 SEQ_UI_Msg(seq_ui_msg_type_t msg_type, u16 delay, char *line1, char *line2)
299 tk 2480
{
596 tk 2481
  ui_msg_type = msg_type;
2482
  ui_msg_ctr = delay;
2483
  strncpy((char *)ui_msg[0], line1, UI_MSG_MAX_CHAR);
2484
  strncpy((char *)ui_msg[1], line2, UI_MSG_MAX_CHAR);
299 tk 2485
 
2486
  return 0; // no error
2487
}
2488
 
2489
/////////////////////////////////////////////////////////////////////////////
596 tk 2490
// Stops temporary message if no SD card warning
2491
/////////////////////////////////////////////////////////////////////////////
2492
s32 SEQ_UI_MsgStop(void)
2493
{
2494
  if( ui_msg_type != SEQ_UI_MSG_SDCARD )
2495
    ui_msg_ctr = 0;
2496
 
2497
  return 0; // no error
2498
}
2499
 
2500
/////////////////////////////////////////////////////////////////////////////
299 tk 2501
// Prints a temporary error messages after file operation
2502
// Expects error status number (as defined in seq_file.h)
2503
/////////////////////////////////////////////////////////////////////////////
2504
s32 SEQ_UI_SDCardErrMsg(u16 delay, s32 status)
2505
{
2506
  // TODO: add more verbose error messages, they are clearly defined in seq_file.h)
2507
  char str[21];
2508
  sprintf(str, "E%3d (DOSFS: D%3d)", -status, seq_file_dfs_errno < 1000 ? seq_file_dfs_errno : 999);
596 tk 2509
  return SEQ_UI_Msg(SEQ_UI_MSG_SDCARD, delay, "!! SD Card Error !!!", str);
299 tk 2510
}