Subversion Repositories svn.mios32

Rev

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