Subversion Repositories svn.mios32

Rev

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