Subversion Repositories svn.mios32

Rev

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