Subversion Repositories svn.mios32

Rev

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