Subversion Repositories svn.mios32

Rev

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