Subversion Repositories svn.mios32

Rev

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