Subversion Repositories svn.mios32

Rev

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