Subversion Repositories svn.mios32

Rev

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