Subversion Repositories svn.mios32

Rev

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