Subversion Repositories svn.mios32

Rev

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