Subversion Repositories svn.mios32

Rev

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