Subversion Repositories svn.mios32

Rev

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