Subversion Repositories svn.mios32

Rev

Rev 740 | Rev 742 | 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 741 2009-10-10 16:00:10Z 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
{
741 tk 808
  seq_ui_button_state.EDIT_PRESSED = depressed ? 0 : 1;
591 tk 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
/////////////////////////////////////////////////////////////////////////////
741 tk 1461
// MIDI Remote Keyboard Function (called from SEQ_MIDI_IN)
1462
/////////////////////////////////////////////////////////////////////////////
1463
s32 SEQ_UI_REMOTE_MIDI_Keyboard(u8 note, u8 depressed)
1464
{
1465
#if 1
1466
  MIOS32_MIDI_SendDebugMessage("SEQ_UI_REMOTE_MIDI_Keyboard(%d, %d)\n", note, depressed);
1467
#endif
1468
  return 0; // no error
1469
}
1470
 
1471
 
1472
/////////////////////////////////////////////////////////////////////////////
134 tk 1473
// Update LCD messages
1474
// Usually called from background task
1475
/////////////////////////////////////////////////////////////////////////////
1476
s32 SEQ_UI_LCD_Handler(void)
1477
{
728 tk 1478
  static u8 boot_animation_wait_ctr = 0;
1479
  static u8 boot_animation_lcd_pos = 0;
1480
 
625 tk 1481
  // special handling in remote client mode
626 tk 1482
  if( seq_ui_remote_active_mode == SEQ_UI_REMOTE_MODE_CLIENT )
625 tk 1483
    return SEQ_UI_LCD_Update();
1484
 
159 tk 1485
  if( seq_ui_display_init_req ) {
1486
    seq_ui_display_init_req = 0; // clear request
134 tk 1487
 
278 tk 1488
    // clear force update of LCD
1489
    SEQ_LCD_Clear();
1490
    SEQ_LCD_Update(1);
134 tk 1491
 
168 tk 1492
    // select first menu item
1493
    ui_selected_item = 0;
1494
 
167 tk 1495
    // call init function of current page
272 tk 1496
    if( ui_menu_pages[ui_page].init_callback != NULL )
1497
      ui_menu_pages[ui_page].init_callback(0); // mode
134 tk 1498
 
1499
    // request display update
159 tk 1500
    seq_ui_display_update_req = 1;
134 tk 1501
  }
1502
 
496 tk 1503
  // print boot screen so long hardware config hasn't been read
1504
  if( !SEQ_FILE_HW_ConfigLocked() ) {
728 tk 1505
    if( boot_animation_lcd_pos < (40-3) ) {
1506
      if( ++boot_animation_wait_ctr >= 75 ) {
1507
    boot_animation_wait_ctr = 0;
637 tk 1508
 
728 tk 1509
    if( boot_animation_lcd_pos == 0 ) {
1510
      SEQ_LCD_Clear();
1511
      SEQ_LCD_CursorSet(0, 0);
1512
      SEQ_LCD_PrintString(MIOS32_LCD_BOOT_MSG_LINE1 " " MIOS32_LCD_BOOT_MSG_LINE2);
1513
      SEQ_LCD_CursorSet(0, 1);
1514
      SEQ_LCD_PrintString("Searching for SD Card...");
1515
    }
1516
 
1517
    // logo is print on second LCD
1518
    SEQ_LCD_LOGO_Print(boot_animation_lcd_pos++);
1519
      }
1520
    }
740 tk 1521
  } else if( seq_ui_backup_req || seq_ui_format_req ) {
634 tk 1522
    SEQ_LCD_Clear();
1523
    SEQ_LCD_CursorSet(0, 0);
740 tk 1524
    //                     <-------------------------------------->
1525
    //                     0123456789012345678901234567890123456789
1526
    if( seq_ui_backup_req )
1527
      SEQ_LCD_PrintString("Creating File Backup - be patient!!!");
1528
    else if( seq_ui_format_req )
1529
      SEQ_LCD_PrintString("Formatting Files - be patient!!!");
1530
    else
1531
      SEQ_LCD_PrintString("Don't know what I'm doing! :-/");
637 tk 1532
 
1533
    if( seq_file_backup_notification != NULL ) {
1534
      int i;
1535
 
1536
      SEQ_LCD_CursorSet(0, 1);
634 tk 1537
      SEQ_LCD_PrintFormattedString("Creating '%s'", seq_file_backup_notification);
637 tk 1538
 
1539
      SEQ_LCD_CursorSet(40+3, 0);
1540
      SEQ_LCD_PrintString("Total: [");
1541
      for(i=0; i<20; ++i)
1542
    SEQ_LCD_PrintChar((i>(seq_file_backup_percentage/5)) ? ' ' : '#');
1543
      SEQ_LCD_PrintFormattedString("] %3d%%", seq_file_backup_percentage);
1544
 
1545
      SEQ_LCD_CursorSet(40+3, 1);
1546
      SEQ_LCD_PrintString("File:  [");
1547
      for(i=0; i<20; ++i)
1548
    SEQ_LCD_PrintChar((i>(seq_file_copy_percentage/5)) ? ' ' : '#');
1549
      SEQ_LCD_PrintFormattedString("] %3d%%", seq_file_copy_percentage);
1550
    }
1551
 
496 tk 1552
  } else if( seq_ui_button_state.MENU_PRESSED && !seq_ui_button_state.MENU_FIRST_PAGE_SELECTED ) {
1553
    SEQ_LCD_CursorSet(0, 0);
1554
    //                   <-------------------------------------->
1555
    //                   0123456789012345678901234567890123456789
306 tk 1556
    SEQ_LCD_PrintString("Menu Shortcuts:");
1557
    SEQ_LCD_PrintSpaces(25 + 40);
1558
    SEQ_LCD_CursorSet(0, 1);
1559
    SEQ_LCD_PrintString(UI_SHORTCUT_STR); // defined in seq_ui_pages.inc
1560
  } else {
1561
    // perform high priority LCD update request
1562
    if( ui_lcd_callback != NULL )
1563
      ui_lcd_callback(1); // high_prio
167 tk 1564
 
306 tk 1565
    // perform low priority LCD update request if requested
1566
    if( seq_ui_display_update_req ) {
1567
      seq_ui_display_update_req = 0; // clear request
328 tk 1568
 
1569
      // ensure that selections are matching with track constraints
1570
      SEQ_UI_CheckSelections();
1571
 
306 tk 1572
      if( ui_lcd_callback != NULL )
1573
    ui_lcd_callback(0); // no high_prio
1574
    }
134 tk 1575
  }
1576
 
353 tk 1577
  // transfer all changed characters to LCD
1578
  SEQ_UI_LCD_Update();
1579
 
1580
  return 0; // no error
1581
}
1582
 
1583
 
1584
/////////////////////////////////////////////////////////////////////////////
1585
// Called from SEQ_UI_LCD_Handler(), but optionally also from other tasks
1586
// to update the LCD screen immediately
1587
/////////////////////////////////////////////////////////////////////////////
1588
s32 SEQ_UI_LCD_Update(void)
1589
{
625 tk 1590
  // special handling in remote client mode
626 tk 1591
  if( seq_ui_remote_active_mode == SEQ_UI_REMOTE_MODE_CLIENT ) {
1592
    MIOS32_IRQ_Disable();
1593
    u8 force = seq_ui_remote_force_lcd_update;
1594
    seq_ui_remote_force_lcd_update = 0;
1595
    MIOS32_IRQ_Enable();
1596
    return SEQ_LCD_Update(force);
625 tk 1597
  }
1598
 
596 tk 1599
  // if UI message active: copy over the text
1600
  if( ui_msg_ctr ) {
1601
    const char *animation_l_ptr;
1602
    const char *animation_r_ptr;
1603
    u8 msg_x = 0;
1604
    u8 right_aligned = 0;
740 tk 1605
    u8 disable_message = 0;
299 tk 1606
 
596 tk 1607
    switch( ui_msg_type ) {
1608
      case SEQ_UI_MSG_SDCARD: {
1609
    //                             00112233
1610
    const char animation_l[2*4] = "   >>>> ";
1611
    //                             00112233
1612
    const char animation_r[2*4] = "  < << <";
1613
    animation_l_ptr = animation_l;
1614
    animation_r_ptr = animation_r;
1615
    msg_x = 0; // MEMO: print such important information at first LCD for the case the user hasn't connected the second LCD yet
1616
    right_aligned = 0;
1617
      } break;
1618
 
740 tk 1619
      case SEQ_UI_MSG_DELAYED_ACTION: {
1620
    //                             00112233
1621
    const char animation_l[2*4] = "   )))) ";
1622
    //                             00112233
1623
    const char animation_r[2*4] = "  ( (( (";
1624
    animation_l_ptr = animation_l;
1625
    animation_r_ptr = animation_r;
1626
    msg_x = 39;
1627
    right_aligned = 1;
1628
 
1629
    if( ui_delayed_action_callback == NULL ) {
1630
      disable_message = 1; // button has been depressed before delay
1631
    } else {
1632
      int seconds = (ui_delayed_action_ctr / 1000) + 1;
1633
      if( seconds == 1 )
1634
        sprintf(ui_msg[0], "Hold for 1 second ");
1635
      else
1636
        sprintf(ui_msg[0], "Hold for %d seconds", seconds);
1637
    }
1638
      } break;
1639
 
741 tk 1640
      case SEQ_UI_MSG_USER_R: {
1641
    //                             00112233
1642
    const char animation_l[2*4] = "   **** ";
1643
    //                             00112233
1644
    const char animation_r[2*4] = "  * ** *";
1645
    animation_l_ptr = animation_l;
1646
    animation_r_ptr = animation_r;
1647
    msg_x = 40; // right display
1648
    right_aligned = 0;
1649
      } break;
1650
 
596 tk 1651
      default: {
1652
    //                             00112233
1653
    const char animation_l[2*4] = "   **** ";
1654
    //                             00112233
1655
    const char animation_r[2*4] = "  * ** *";
1656
    animation_l_ptr = animation_l;
1657
    animation_r_ptr = animation_r;
1658
    msg_x = 39;
1659
    right_aligned = 1;
1660
      } break;
1661
 
1662
    }
1663
 
740 tk 1664
    if( !disable_message ) {
1665
      int anum = (ui_msg_ctr % 1000) / 250;
596 tk 1666
 
740 tk 1667
      int len[2];
1668
      len[0] = strlen((char *)ui_msg[0]);
1669
      len[1] = strlen((char *)ui_msg[1]);
1670
      int len_max = len[0];
1671
      if( len[1] > len_max )
1672
    len_max = len[1];
596 tk 1673
 
740 tk 1674
      if( right_aligned )
1675
    msg_x -= (9 + len_max);
596 tk 1676
 
740 tk 1677
      int line;
1678
      for(line=0; line<2; ++line) {
1679
    SEQ_LCD_CursorSet(msg_x, line);
596 tk 1680
 
740 tk 1681
    // ensure that both lines are padded with same number of spaces
1682
    int end_pos = len[line];
1683
    while( end_pos < len_max )
1684
      ui_msg[line][end_pos++] = ' ';
1685
    ui_msg[line][end_pos] = 0;
1686
 
1687
    SEQ_LCD_PrintFormattedString(" %c%c| %s |%c%c ",
1688
                     *(animation_l_ptr + 2*anum + 0), *(animation_l_ptr + 2*anum + 1),
1689
                     (char *)ui_msg[line],
1690
                     *(animation_r_ptr + 2*anum + 0), *(animation_r_ptr + 2*anum + 1));
1691
      }
299 tk 1692
    }
1693
  }
1694
 
637 tk 1695
  // MSD USB notification at right corner if not in Disk page
1696
  // to warn user that USB MIDI is disabled and seq performance is bad now!
638 tk 1697
  if( (TASK_MSD_EnableGet() > 0) && ui_page != SEQ_UI_PAGE_DISK ) {
637 tk 1698
    SEQ_LCD_CursorSet(80-11, 0);
1699
    if( ui_cursor_flash ) SEQ_LCD_PrintSpaces(13); else SEQ_LCD_PrintString(" [MSD USB] ");
1700
    SEQ_LCD_CursorSet(80-11, 1);
1701
    char str[5];
1702
    TASK_MSD_FlagStrGet(str);
1703
    if( ui_cursor_flash ) SEQ_LCD_PrintSpaces(13); else SEQ_LCD_PrintFormattedString(" [ %s  ] ", str);
1704
  }
596 tk 1705
 
278 tk 1706
  // transfer all changed characters to LCD
353 tk 1707
  // SEQ_LCD_Update provides a MUTEX handling to allow updates from different tasks
1708
  return SEQ_LCD_Update(0);
134 tk 1709
}
1710
 
1711
 
1712
/////////////////////////////////////////////////////////////////////////////
1713
// Update all LEDs
1714
// Usually called from background task
1715
/////////////////////////////////////////////////////////////////////////////
1716
s32 SEQ_UI_LED_Handler(void)
1717
{
626 tk 1718
  static u8 remote_led_sr[SEQ_LED_NUM_SR];
1719
 
524 tk 1720
  int i;
1721
 
625 tk 1722
  // ignore in remote client mode
626 tk 1723
  if( seq_ui_remote_active_mode == SEQ_UI_REMOTE_MODE_CLIENT )
625 tk 1724
    return 0; // no error
1725
 
496 tk 1726
  // ignore so long hardware config hasn't been read
1727
  if( !SEQ_FILE_HW_ConfigLocked() )
1728
    return -1;
1729
 
134 tk 1730
  // track LEDs
484 tk 1731
  u8 selected_tracks = ui_selected_tracks >> (4*ui_selected_group);
492 tk 1732
  SEQ_LED_PinSet(seq_hwcfg_led.track[0], (selected_tracks & (1 << 0)));
1733
  SEQ_LED_PinSet(seq_hwcfg_led.track[1], (selected_tracks & (1 << 1)));
1734
  SEQ_LED_PinSet(seq_hwcfg_led.track[2], (selected_tracks & (1 << 2)));
1735
  SEQ_LED_PinSet(seq_hwcfg_led.track[3], (selected_tracks & (1 << 3)));
678 tk 1736
  SEQ_LED_PinSet(seq_hwcfg_led.track_sel, ui_page == SEQ_UI_PAGE_TRACKSEL);
134 tk 1737
 
1738
  // parameter layer LEDs
492 tk 1739
  SEQ_LED_PinSet(seq_hwcfg_led.par_layer[0], (ui_selected_par_layer == 0));
1740
  SEQ_LED_PinSet(seq_hwcfg_led.par_layer[1], (ui_selected_par_layer == 1));
1741
  SEQ_LED_PinSet(seq_hwcfg_led.par_layer[2], (ui_selected_par_layer >= 2) || seq_ui_button_state.PAR_LAYER_SEL);
678 tk 1742
  SEQ_LED_PinSet(seq_hwcfg_led.par_layer_sel, ui_page == SEQ_UI_PAGE_PARSEL);
134 tk 1743
 
1744
  // group LEDs
492 tk 1745
  SEQ_LED_PinSet(seq_hwcfg_led.group[0], (ui_selected_group == 0));
1746
  SEQ_LED_PinSet(seq_hwcfg_led.group[1], (ui_selected_group == 1));
1747
  SEQ_LED_PinSet(seq_hwcfg_led.group[2], (ui_selected_group == 2));
1748
  SEQ_LED_PinSet(seq_hwcfg_led.group[3], (ui_selected_group == 3));
134 tk 1749
 
1750
  // trigger layer LEDs
492 tk 1751
  SEQ_LED_PinSet(seq_hwcfg_led.trg_layer[0], (ui_selected_trg_layer == 0));
1752
  SEQ_LED_PinSet(seq_hwcfg_led.trg_layer[1], (ui_selected_trg_layer == 1));
1753
  SEQ_LED_PinSet(seq_hwcfg_led.trg_layer[2], (ui_selected_trg_layer >= 2) || seq_ui_button_state.TRG_LAYER_SEL);
678 tk 1754
  SEQ_LED_PinSet(seq_hwcfg_led.trg_layer_sel, ui_page == SEQ_UI_PAGE_TRGSEL);
134 tk 1755
 
1756
  // remaining LEDs
492 tk 1757
  SEQ_LED_PinSet(seq_hwcfg_led.edit, ui_page == SEQ_UI_PAGE_EDIT);
1758
  SEQ_LED_PinSet(seq_hwcfg_led.mute, ui_page == SEQ_UI_PAGE_MUTE);
1759
  SEQ_LED_PinSet(seq_hwcfg_led.pattern, ui_page == SEQ_UI_PAGE_PATTERN);
399 tk 1760
  if( SEQ_SONG_ActiveGet() )
492 tk 1761
    SEQ_LED_PinSet(seq_hwcfg_led.song, 1);
399 tk 1762
  else
492 tk 1763
    SEQ_LED_PinSet(seq_hwcfg_led.song, ui_cursor_flash ? 0 : (ui_page == SEQ_UI_PAGE_SONG));
134 tk 1764
 
492 tk 1765
  SEQ_LED_PinSet(seq_hwcfg_led.solo, seq_ui_button_state.SOLO);
1766
  SEQ_LED_PinSet(seq_hwcfg_led.fast, seq_ui_button_state.FAST_ENCODERS);
1767
  SEQ_LED_PinSet(seq_hwcfg_led.all, seq_ui_button_state.CHANGE_ALL_STEPS);
134 tk 1768
 
492 tk 1769
  SEQ_LED_PinSet(seq_hwcfg_led.play, SEQ_BPM_IsRunning());
1770
  SEQ_LED_PinSet(seq_hwcfg_led.stop, !SEQ_BPM_IsRunning() && !ui_seq_pause);
1771
  SEQ_LED_PinSet(seq_hwcfg_led.pause, ui_seq_pause);
240 tk 1772
 
492 tk 1773
  SEQ_LED_PinSet(seq_hwcfg_led.rew, seq_ui_button_state.REW);
1774
  SEQ_LED_PinSet(seq_hwcfg_led.fwd, seq_ui_button_state.FWD);
596 tk 1775
 
1776
  SEQ_LED_PinSet(seq_hwcfg_led.loop, seq_core_state.LOOP);
134 tk 1777
 
678 tk 1778
  SEQ_LED_PinSet(seq_hwcfg_led.step_view, ui_page == SEQ_UI_PAGE_STEPSEL);
134 tk 1779
 
591 tk 1780
  SEQ_LED_PinSet(seq_hwcfg_led.exit, seq_ui_button_state.EXIT_PRESSED);
1781
  SEQ_LED_PinSet(seq_hwcfg_led.select, seq_ui_button_state.SELECT_PRESSED);
492 tk 1782
  SEQ_LED_PinSet(seq_hwcfg_led.menu, seq_ui_button_state.MENU_PRESSED);
134 tk 1783
 
596 tk 1784
  // handle double functions
1785
  if( seq_ui_button_state.MENU_PRESSED ) {
1786
    SEQ_LED_PinSet(seq_hwcfg_led.scrub, seq_core_state.LOOP);
1787
    SEQ_LED_PinSet(seq_hwcfg_led.metronome, seq_core_state.EXT_RESTART_REQ);
1788
  } else {
1789
    SEQ_LED_PinSet(seq_hwcfg_led.scrub, seq_ui_button_state.SCRUB);
1790
    SEQ_LED_PinSet(seq_hwcfg_led.metronome, seq_core_state.METRONOME);
1791
  }
1792
 
630 tk 1793
  SEQ_LED_PinSet(seq_hwcfg_led.record, ui_page == SEQ_UI_PAGE_TRKREC);
1794
 
492 tk 1795
  SEQ_LED_PinSet(seq_hwcfg_led.utility, ui_page == SEQ_UI_PAGE_UTIL);
1796
  SEQ_LED_PinSet(seq_hwcfg_led.copy, seq_ui_button_state.COPY);
1797
  SEQ_LED_PinSet(seq_hwcfg_led.paste, seq_ui_button_state.PASTE);
1798
  SEQ_LED_PinSet(seq_hwcfg_led.clear, seq_ui_button_state.CLEAR);
240 tk 1799
 
513 tk 1800
  SEQ_LED_PinSet(seq_hwcfg_led.tap_tempo, seq_ui_button_state.TAP_TEMPO);
678 tk 1801
  SEQ_LED_PinSet(seq_hwcfg_led.tempo_preset, ui_page == SEQ_UI_PAGE_BPM_PRESETS);
524 tk 1802
  SEQ_LED_PinSet(seq_hwcfg_led.ext_restart, seq_core_state.EXT_RESTART_REQ);
240 tk 1803
 
492 tk 1804
  SEQ_LED_PinSet(seq_hwcfg_led.down, seq_ui_button_state.DOWN);
1805
  SEQ_LED_PinSet(seq_hwcfg_led.up, seq_ui_button_state.UP);
240 tk 1806
 
167 tk 1807
 
306 tk 1808
  // in MENU page: overrule GP LEDs so long MENU button is pressed/active
1809
  if( seq_ui_button_state.MENU_PRESSED ) {
1810
    if( ui_cursor_flash ) // if flashing flag active: no LED flag set
1811
      ui_gp_leds = 0x0000;
1812
    else {
1813
      int i;
1814
      u16 new_ui_gp_leds = 0x0000;
1815
      for(i=0; i<16; ++i)
1816
    if( ui_page == ui_shortcut_menu_pages[i] )
1817
      new_ui_gp_leds |= (1 << i);
1818
      ui_gp_leds = new_ui_gp_leds;
1819
    }
1820
  } else {
1821
    // note: the background function is permanently interrupted - therefore we write the GP pattern
1822
    // into a temporary variable, and take it over once completed
1823
    u16 new_ui_gp_leds = 0x0000;
1824
    // request GP LED values from current menu page
1825
    // will be transfered to DOUT registers in SEQ_UI_LED_Handler_Periodic
1826
    new_ui_gp_leds = 0x0000;
206 tk 1827
 
306 tk 1828
    if( ui_led_callback != NULL )
1829
      ui_led_callback(&new_ui_gp_leds);
167 tk 1830
 
306 tk 1831
    ui_gp_leds = new_ui_gp_leds;
1832
  }
1833
 
524 tk 1834
 
1835
  // BLM LEDs
1836
  u8 visible_track0 = 4*ui_selected_group;
1837
  u8 visible_sr0  = 2*ui_selected_step_view;
1838
 
1839
  for(i=0; i<NUM_BLM_LED_SRS; ++i)
1840
    ui_blm_leds[i] = SEQ_TRG_Get8(visible_track0+(i>>1), visible_sr0+(i&1), ui_selected_trg_layer, ui_selected_instrument);
1841
 
626 tk 1842
 
1843
  // send LED changes in remote server mode
1844
  if( seq_ui_remote_mode == SEQ_UI_REMOTE_MODE_SERVER || seq_ui_remote_active_mode == SEQ_UI_REMOTE_MODE_SERVER ) {
1845
    int first_sr = -1;
1846
    int last_sr = -1;
1847
    int sr;
1848
    for(sr=0; sr<SEQ_LED_NUM_SR; ++sr) {
1849
      u8 value = SEQ_LED_SRGet(sr);
1850
      if( value != remote_led_sr[sr] ) {
1851
    if( first_sr == -1 )
1852
      first_sr = sr;
1853
    last_sr = sr;
1854
    remote_led_sr[sr] = value;
1855
      }
1856
    }
1857
 
1858
    MIOS32_IRQ_Disable();
1859
    if( seq_ui_remote_force_led_update ) {
1860
      first_sr = 0;
1861
      last_sr = SEQ_LED_NUM_SR-1;
1862
    }
1863
    seq_ui_remote_force_led_update = 0;
1864
    MIOS32_IRQ_Enable();
1865
 
1866
    if( first_sr >= 0 )
1867
      SEQ_MIDI_SYSEX_REMOTE_Server_SendLED(first_sr, (u8 *)&remote_led_sr[first_sr], last_sr-first_sr+1);
1868
  }
1869
 
1870
 
134 tk 1871
  return 0; // no error
1872
}
1873
 
1874
 
1875
/////////////////////////////////////////////////////////////////////////////
1876
// updates high-prio LED functions (GP LEDs and Beat LED)
168 tk 1877
// called each mS
134 tk 1878
/////////////////////////////////////////////////////////////////////////////
1879
s32 SEQ_UI_LED_Handler_Periodic()
1880
{
625 tk 1881
  // ignore in remote client mode
626 tk 1882
  if( seq_ui_remote_active_mode == SEQ_UI_REMOTE_MODE_CLIENT )
625 tk 1883
    return 0; // no error
1884
 
496 tk 1885
  // ignore so long hardware config hasn't been read
1886
  if( !SEQ_FILE_HW_ConfigLocked() )
1887
    return -1;
1888
 
134 tk 1889
  // GP LEDs are only updated when ui_gp_leds or pos_marker_mask has changed
1890
  static u16 prev_ui_gp_leds = 0x0000;
1891
  static u16 prev_pos_marker_mask = 0x0000;
1892
 
1893
  // beat LED: tmp. for demo w/o real sequencer
193 tk 1894
  u8 sequencer_running = SEQ_BPM_IsRunning();
492 tk 1895
  SEQ_LED_PinSet(seq_hwcfg_led.beat, sequencer_running && ((seq_core_state.ref_step & 3) == 0));
134 tk 1896
 
1897
  // for song position marker (supports 16 LEDs, check for selected step view)
1898
  u16 pos_marker_mask = 0x0000;
325 tk 1899
  u8 visible_track = SEQ_UI_VisibleTrackGet();
1900
  u8 played_step = seq_core_trk[visible_track].step;
513 tk 1901
  if( seq_ui_button_state.STEP_VIEW ) {
1902
    // if STEP_VIEW button pressed: pos marker correlated to zoom ratio
325 tk 1903
    if( sequencer_running )
1904
      pos_marker_mask = 1 << (played_step / (SEQ_TRG_NumStepsGet(visible_track)/16));
1905
  } else {
1906
    if( sequencer_running && (played_step >> 4) == ui_selected_step_view )
1907
      pos_marker_mask = 1 << (played_step & 0xf);
1908
  }
134 tk 1909
 
1910
  // exit of pattern hasn't changed
173 tk 1911
  if( prev_ui_gp_leds == ui_gp_leds && prev_pos_marker_mask == pos_marker_mask )
134 tk 1912
    return 0;
173 tk 1913
  prev_ui_gp_leds = ui_gp_leds;
134 tk 1914
  prev_pos_marker_mask = pos_marker_mask;
1915
 
1916
  // transfer to GP LEDs
514 tk 1917
  if( seq_hwcfg_led.gp_dout_l_sr ) {
1918
    if( seq_hwcfg_led.gp_dout_l2_sr )
1919
      SEQ_LED_SRSet(seq_hwcfg_led.gp_dout_l_sr-1, (ui_gp_leds >> 0) & 0xff);
496 tk 1920
    else
514 tk 1921
      SEQ_LED_SRSet(seq_hwcfg_led.gp_dout_l_sr-1, ((ui_gp_leds ^ pos_marker_mask) >> 0) & 0xff);
496 tk 1922
  }
134 tk 1923
 
514 tk 1924
  if( seq_hwcfg_led.gp_dout_r_sr ) {
1925
    if( seq_hwcfg_led.gp_dout_r2_sr )
1926
      SEQ_LED_SRSet(seq_hwcfg_led.gp_dout_r_sr-1, (ui_gp_leds >> 8) & 0xff);
496 tk 1927
    else
514 tk 1928
      SEQ_LED_SRSet(seq_hwcfg_led.gp_dout_r_sr-1, ((ui_gp_leds ^ pos_marker_mask) >> 8) & 0xff);
496 tk 1929
  }
134 tk 1930
 
514 tk 1931
  if( seq_hwcfg_led.gp_dout_l2_sr )
1932
    SEQ_LED_SRSet(seq_hwcfg_led.gp_dout_l2_sr-1, (pos_marker_mask >> 0) & 0xff);
1933
  if( seq_hwcfg_led.gp_dout_r2_sr )
1934
    SEQ_LED_SRSet(seq_hwcfg_led.gp_dout_r2_sr-1, (pos_marker_mask >> 8) & 0xff);
496 tk 1935
 
524 tk 1936
 
1937
  if( seq_hwcfg_blm.enabled ) {
1938
    // Red LEDs (position marker)
1939
    int track_ix;
1940
    for(track_ix=0; track_ix<(NUM_BLM_LED_SRS/2); ++track_ix) {
1941
      u16 pos_marker_mask = 0x0000;
1942
      if( sequencer_running ) {
1943
    u8 track = ui_selected_group + track_ix;
1944
    u8 played_step = seq_core_trk[track].step;
1945
    if( (played_step >> 4) == ui_selected_step_view )
1946
      pos_marker_mask = 1 << (played_step & 0xf);
1947
      }
1948
 
1949
      // Prepare Green LEDs (triggers)
1950
      u8 green_l = ui_blm_leds[2*track_ix+0];
1951
      u8 green_r = ui_blm_leds[2*track_ix+1];
1952
 
1953
      // Red LEDs (position marker)
1954
      if( seq_hwcfg_blm.dout_duocolour ) {
1955
    BLM_DOUT_SRSet(1, 2*track_ix+0, pos_marker_mask & 0xff);
1956
    BLM_DOUT_SRSet(1, 2*track_ix+1, (pos_marker_mask >> 8) & 0xff);
1957
 
1958
    if( seq_hwcfg_blm.dout_duocolour == 2 ) {
1959
      // Colour Mode 2: clear green LED, so that only one LED is lit
1960
      if( pos_marker_mask & 0x00ff )
1961
        green_l &= ~pos_marker_mask;
1962
      else if( pos_marker_mask & 0xff00 )
1963
        green_r &= ~(pos_marker_mask >> 8);
1964
    }
1965
      } else {
1966
    // If Duo-LEDs not enabled: invert Green LEDs
1967
    if( pos_marker_mask & 0x00ff )
1968
      green_l ^= pos_marker_mask;
1969
    else if( pos_marker_mask & 0xff00 )
1970
      green_r ^= (pos_marker_mask >> 8);
1971
      }
1972
 
1973
      // Set Green LEDs
1974
      BLM_DOUT_SRSet(0, 2*track_ix+0, green_l);
1975
      BLM_DOUT_SRSet(0, 2*track_ix+1, green_r);
1976
    }
1977
  }
1978
 
1979
 
514 tk 1980
  if( seq_hwcfg_blm8x8.enabled && seq_hwcfg_blm8x8.dout_gp_mapping ) {
513 tk 1981
    // for wilba's frontpanel
134 tk 1982
 
513 tk 1983
    // BLM_X DOUT -> GP LED mapping
1984
    // 0 = 15,16    1 = 13,14   2 = 11,12   3 = 9,10
1985
    // 4 = 1,2  5 = 3,4     6 = 5,6     7 = 7,8
134 tk 1986
 
513 tk 1987
    // bit 7: first green (i.e. GP1-G)
1988
    // bit 6: first red (i.e. GP1-R)
1989
    // bit 5: second green (i.e. GP2-G)
1990
    // bit 4: second red (i.e. GP2-R)
134 tk 1991
 
513 tk 1992
    // this mapping routine takes ca. 5 uS
1993
    // since it's only executed when ui_gp_leds or gp_mask has changed, it doesn't really hurt
134 tk 1994
 
513 tk 1995
    u16 modified_gp_leds = ui_gp_leds;
134 tk 1996
#if 1
513 tk 1997
    // extra: red LED is lit exclusively for higher contrast
1998
    modified_gp_leds &= ~pos_marker_mask;
134 tk 1999
#endif
2000
 
513 tk 2001
    int sr;
2002
    const u8 blm_x_sr_map[8] = {4, 5, 6, 7, 3, 2, 1, 0};
2003
    u16 gp_mask = 1 << 0;
2004
    for(sr=0; sr<8; ++sr) {
2005
      u8 pattern = 0;
134 tk 2006
 
513 tk 2007
      if( modified_gp_leds & gp_mask )
2008
    pattern |= 0x80;
2009
      if( pos_marker_mask & gp_mask )
2010
    pattern |= 0x40;
2011
      gp_mask <<= 1;
2012
      if( modified_gp_leds & gp_mask )
2013
    pattern |= 0x20;
2014
      if( pos_marker_mask & gp_mask )
2015
    pattern |= 0x10;
2016
      gp_mask <<= 1;
134 tk 2017
 
513 tk 2018
      u8 mapped_sr = blm_x_sr_map[sr];
2019
      BLM_X_LED_rows[mapped_sr][0] = (BLM_X_LED_rows[mapped_sr][0] & 0x0f) | pattern;
2020
    }
134 tk 2021
  }
2022
 
184 tk 2023
  return 0; // no error
134 tk 2024
}
2025
 
2026
 
2027
/////////////////////////////////////////////////////////////////////////////
168 tk 2028
// for menu handling (e.g. flashing cursor, doubleclick counter, etc...)
2029
// called each mS
2030
/////////////////////////////////////////////////////////////////////////////
2031
s32 SEQ_UI_MENU_Handler_Periodic()
2032
{
625 tk 2033
  // ignore in remote client mode
626 tk 2034
  if( seq_ui_remote_active_mode == SEQ_UI_REMOTE_MODE_CLIENT )
625 tk 2035
    return 0; // no error
2036
 
740 tk 2037
  if( ++ui_cursor_flash_ctr >= SEQ_UI_CURSOR_FLASH_CTR_MAX ) {
168 tk 2038
    ui_cursor_flash_ctr = 0;
740 tk 2039
    ++ui_cursor_flash_overrun_ctr;
2040
    seq_ui_display_update_req = 1;
2041
  }
2042
 
173 tk 2043
  // important: flash flag has to be recalculated on each invocation of this
2044
  // handler, since counter could also be reseted outside this function
652 tk 2045
  u8 old_ui_cursor_flash = ui_cursor_flash;
173 tk 2046
  ui_cursor_flash = ui_cursor_flash_ctr >= SEQ_UI_CURSOR_FLASH_CTR_LED_OFF;
652 tk 2047
  if( old_ui_cursor_flash != ui_cursor_flash )
2048
    seq_ui_display_update_req = 1;
168 tk 2049
 
240 tk 2050
  // used in some pages for temporary messages
416 tk 2051
  if( ui_hold_msg_ctr ) {
240 tk 2052
    --ui_hold_msg_ctr;
2053
 
416 tk 2054
    if( !ui_hold_msg_ctr )
2055
      seq_ui_display_update_req = 1;
2056
  }
2057
 
596 tk 2058
  // used for temporary messages
2059
  if( ui_msg_ctr )
2060
    --ui_msg_ctr;
299 tk 2061
 
184 tk 2062
  // VU meters (used in MUTE menu, could also be available as LED matrix...)
2063
  static u8 vu_meter_prediv = 0; // predivider for VU meters
2064
 
2065
  if( ++vu_meter_prediv >= 4 ) {
2066
    vu_meter_prediv = 0;
2067
 
333 tk 2068
 
2069
    portENTER_CRITICAL();
2070
 
184 tk 2071
    u8 track;
2072
    seq_core_trk_t *t = &seq_core_trk[0];
2073
    for(track=0; track<SEQ_CORE_NUM_TRACKS; ++t, ++track)
2074
      if( t->vu_meter )
2075
    --t->vu_meter;
333 tk 2076
 
2077
    int i;
2078
    u8 *vu_meter = (u8 *)&seq_layer_vu_meter[0];
2079
    for(i=0; i<sizeof(seq_layer_vu_meter); ++i, ++vu_meter) {
2080
      if( *vu_meter && !(*vu_meter & 0x80) ) // if bit 7 set: static value
2081
    *vu_meter -= 1;
2082
    }
2083
 
318 tk 2084
    portEXIT_CRITICAL();
184 tk 2085
  }
2086
 
740 tk 2087
  // delayed action will be triggered once counter reached 0
2088
  if( !ui_delayed_action_callback )
2089
    ui_delayed_action_ctr = 0;
2090
  else if( ui_delayed_action_ctr ) {
2091
    if( --ui_delayed_action_ctr == 0 ) {
2092
      ui_delayed_action_callback();
2093
      ui_delayed_action_callback = NULL;
2094
    }
2095
  }
2096
 
168 tk 2097
  return 0;
2098
}
2099
 
2100
 
2101
/////////////////////////////////////////////////////////////////////////////
328 tk 2102
// Should be regulary called to check if the layer/instrument/step selection
2103
// is valid for the current track
2104
// At least executed before button/encoder and LCD function calls
2105
/////////////////////////////////////////////////////////////////////////////
2106
s32 SEQ_UI_CheckSelections(void)
2107
{
492 tk 2108
  if( (ui_selected_tracks >> (4*ui_selected_group)) == 0 )
2109
    ui_selected_tracks = 1 << (4*ui_selected_group);
2110
 
328 tk 2111
  u8 visible_track = SEQ_UI_VisibleTrackGet();
2112
 
2113
  if( ui_selected_instrument >= SEQ_PAR_NumInstrumentsGet(visible_track) )
2114
    ui_selected_instrument = 0;
2115
 
2116
  if( ui_selected_par_layer >= SEQ_PAR_NumLayersGet(visible_track) )
2117
    ui_selected_par_layer = 0;
2118
 
2119
  if( ui_selected_trg_layer >= SEQ_TRG_NumLayersGet(visible_track) )
2120
    ui_selected_trg_layer = 0;
2121
 
2122
  if( ui_selected_step >= SEQ_TRG_NumStepsGet(visible_track) )
2123
    ui_selected_step = 0;
2124
 
2125
  if( ui_selected_step_view >= (SEQ_TRG_NumStepsGet(visible_track)/16) ) {
2126
    ui_selected_step_view = 0;
2127
    ui_selected_step %= 16;
2128
  }
2129
 
336 tk 2130
  if( ui_selected_step < (16*ui_selected_step_view) ||
2131
      ui_selected_step >= (16*(ui_selected_step_view+1)) )
328 tk 2132
    ui_selected_step_view = ui_selected_step / 16;
2133
 
2134
  return 0; // no error
2135
}
2136
 
2137
 
2138
/////////////////////////////////////////////////////////////////////////////
134 tk 2139
// Returns the currently visible track
2140
/////////////////////////////////////////////////////////////////////////////
2141
u8 SEQ_UI_VisibleTrackGet(void)
2142
{
2143
  u8 offset = 0;
2144
 
484 tk 2145
  u8 selected_tracks = ui_selected_tracks >> (4*ui_selected_group);
2146
  if( selected_tracks & (1 << 3) )
134 tk 2147
    offset = 3;
484 tk 2148
  if( selected_tracks & (1 << 2) )
134 tk 2149
    offset = 2;
484 tk 2150
  if( selected_tracks & (1 << 1) )
134 tk 2151
    offset = 1;
484 tk 2152
  if( selected_tracks & (1 << 0) )
134 tk 2153
    offset = 0;
2154
 
2155
  return 4*ui_selected_group + offset;
2156
}
2157
 
168 tk 2158
 
2159
/////////////////////////////////////////////////////////////////////////////
178 tk 2160
// Returns 1 if 'track' is selected
2161
/////////////////////////////////////////////////////////////////////////////
2162
s32 SEQ_UI_IsSelectedTrack(u8 track)
2163
{
484 tk 2164
  return (ui_selected_tracks & (1 << track)) ? 1 : 0;
178 tk 2165
}
2166
 
2167
 
2168
/////////////////////////////////////////////////////////////////////////////
240 tk 2169
// Sets a new selected step and updates the step view
2170
/////////////////////////////////////////////////////////////////////////////
2171
s32 SEQ_UI_SelectedStepSet(u8 step)
2172
{
2173
  ui_selected_step = step;
328 tk 2174
  SEQ_UI_CheckSelections();
2175
 
240 tk 2176
  return 0; // no error
2177
}
2178
 
2179
 
2180
/////////////////////////////////////////////////////////////////////////////
168 tk 2181
// Increments the selected tracks/groups
2182
// OUT: 1 if value has been changed, otherwise 0
2183
/////////////////////////////////////////////////////////////////////////////
2184
s32 SEQ_UI_GxTyInc(s32 incrementer)
2185
{
2186
  int gxty = SEQ_UI_VisibleTrackGet();
2187
  int prev_gxty = gxty;
2188
 
2189
  if( incrementer >= 0 ) {
2190
    if( (gxty += incrementer) >= SEQ_CORE_NUM_TRACKS )
2191
      gxty = SEQ_CORE_NUM_TRACKS-1;
2192
  } else {
2193
    if( (gxty += incrementer) < 0 )
2194
      gxty = 0;
2195
  }
2196
 
2197
  if( gxty == prev_gxty )
2198
    return 0; // no change
2199
 
484 tk 2200
  ui_selected_tracks = 1 << gxty;
168 tk 2201
  ui_selected_group = gxty / 4;
2202
 
2203
  return 1; // value changed
2204
}
2205
 
2206
 
2207
/////////////////////////////////////////////////////////////////////////////
236 tk 2208
// Increments a 16bit variable within given min/max range
2209
// OUT: 1 if value has been changed, otherwise 0
2210
/////////////////////////////////////////////////////////////////////////////
240 tk 2211
s32 SEQ_UI_Var16_Inc(u16 *value, u16 min, u16 max, s32 incrementer)
236 tk 2212
{
2213
  int new_value = *value;
2214
  int prev_value = new_value;
2215
 
2216
  if( incrementer >= 0 ) {
2217
    if( (new_value += incrementer) >= max )
2218
      new_value = max;
2219
  } else {
2220
    if( (new_value += incrementer) < min )
2221
      new_value = min;
2222
  }
2223
 
2224
  if( new_value == prev_value )
2225
    return 0; // no change
2226
 
2227
  *value = new_value;
2228
 
2229
  return 1; // value changed
2230
}
2231
 
240 tk 2232
/////////////////////////////////////////////////////////////////////////////
2233
// Increments an 8bit variable within given min/max range
2234
// OUT: 1 if value has been changed, otherwise 0
2235
/////////////////////////////////////////////////////////////////////////////
2236
s32 SEQ_UI_Var8_Inc(u8 *value, u16 min, u16 max, s32 incrementer)
2237
{
2238
  u16 tmp = *value;
2239
  if( SEQ_UI_Var16_Inc(&tmp, min, max, incrementer) ) {
2240
    *value = tmp;
2241
    return 1; // value changed
2242
  }
236 tk 2243
 
240 tk 2244
  return 0; // value hasn't been changed
2245
}
2246
 
2247
 
236 tk 2248
/////////////////////////////////////////////////////////////////////////////
168 tk 2249
// Increments a CC within given min/max range
2250
// OUT: 1 if value has been changed, otherwise 0
2251
/////////////////////////////////////////////////////////////////////////////
248 tk 2252
s32 SEQ_UI_CC_Inc(u8 cc, u8 min, u8 max, s32 incrementer)
168 tk 2253
{
2254
  u8 visible_track = SEQ_UI_VisibleTrackGet();
173 tk 2255
  int new_value = SEQ_CC_Get(visible_track, cc);
2256
  int prev_value = new_value;
168 tk 2257
 
2258
  if( incrementer >= 0 ) {
173 tk 2259
    if( (new_value += incrementer) >= max )
2260
      new_value = max;
168 tk 2261
  } else {
173 tk 2262
    if( (new_value += incrementer) < min )
2263
      new_value = min;
168 tk 2264
  }
2265
 
173 tk 2266
  if( new_value == prev_value )
168 tk 2267
    return 0; // no change
2268
 
173 tk 2269
  SEQ_CC_Set(visible_track, cc, new_value);
168 tk 2270
 
179 tk 2271
  // set same value for all selected tracks
2272
  u8 track;
2273
  for(track=0; track<SEQ_CORE_NUM_TRACKS; ++track)
2274
    if( track != visible_track && SEQ_UI_IsSelectedTrack(track) )
2275
      SEQ_CC_Set(track, cc, new_value);
2276
 
168 tk 2277
  return 1; // value changed
2278
}
2279
 
173 tk 2280
 
2281
/////////////////////////////////////////////////////////////////////////////
179 tk 2282
// Sets a CC value on all selected tracks
2283
// OUT: 1 if value has been changed, otherwise 0
2284
/////////////////////////////////////////////////////////////////////////////
248 tk 2285
s32 SEQ_UI_CC_Set(u8 cc, u8 value)
179 tk 2286
{
2287
  u8 visible_track = SEQ_UI_VisibleTrackGet();
2288
  int prev_value = SEQ_CC_Get(visible_track, cc);
2289
 
2290
  if( value == prev_value )
2291
    return 0; // no change
2292
 
2293
  SEQ_CC_Set(visible_track, cc, value);
2294
 
2295
  // set same value for all selected tracks
2296
  u8 track;
2297
  for(track=0; track<SEQ_CORE_NUM_TRACKS; ++track)
2298
    if( track != visible_track && SEQ_UI_IsSelectedTrack(track) )
2299
      SEQ_CC_Set(track, cc, value);
2300
 
2301
  return 1; // value changed
2302
}
2303
 
2304
/////////////////////////////////////////////////////////////////////////////
173 tk 2305
// Modifies a bitfield in a CC value to a given value
2306
// OUT: 1 if value has been changed, otherwise 0
2307
/////////////////////////////////////////////////////////////////////////////
248 tk 2308
s32 SEQ_UI_CC_SetFlags(u8 cc, u8 flag_mask, u8 value)
173 tk 2309
{
2310
  u8 visible_track = SEQ_UI_VisibleTrackGet();
2311
  int new_value = SEQ_CC_Get(visible_track, cc);
2312
  int prev_value = new_value;
2313
 
2314
  new_value = (new_value & ~flag_mask) | value;
2315
 
2316
  if( new_value == prev_value )
2317
    return 0; // no change
2318
 
2319
  SEQ_CC_Set(visible_track, cc, new_value);
2320
 
179 tk 2321
  // do same modification for all selected tracks
2322
  u8 track;
2323
  for(track=0; track<SEQ_CORE_NUM_TRACKS; ++track)
2324
    if( track != visible_track && SEQ_UI_IsSelectedTrack(track) ) {
2325
      int new_value = SEQ_CC_Get(track, cc);
2326
      new_value = (new_value & ~flag_mask) | value;
2327
      SEQ_CC_Set(track, cc, new_value);
2328
    }
2329
 
173 tk 2330
  return 1; // value changed
2331
}
2332
 
2333
 
299 tk 2334
/////////////////////////////////////////////////////////////////////////////
596 tk 2335
// Print temporary user messages (e.g. warnings, errors)
299 tk 2336
// expects mS delay and two lines, each up to 20 characters
2337
/////////////////////////////////////////////////////////////////////////////
596 tk 2338
s32 SEQ_UI_Msg(seq_ui_msg_type_t msg_type, u16 delay, char *line1, char *line2)
299 tk 2339
{
596 tk 2340
  ui_msg_type = msg_type;
2341
  ui_msg_ctr = delay;
2342
  strncpy((char *)ui_msg[0], line1, UI_MSG_MAX_CHAR);
2343
  strncpy((char *)ui_msg[1], line2, UI_MSG_MAX_CHAR);
299 tk 2344
 
2345
  return 0; // no error
2346
}
2347
 
2348
/////////////////////////////////////////////////////////////////////////////
596 tk 2349
// Stops temporary message if no SD card warning
2350
/////////////////////////////////////////////////////////////////////////////
2351
s32 SEQ_UI_MsgStop(void)
2352
{
2353
  if( ui_msg_type != SEQ_UI_MSG_SDCARD )
2354
    ui_msg_ctr = 0;
2355
 
2356
  return 0; // no error
2357
}
2358
 
2359
/////////////////////////////////////////////////////////////////////////////
299 tk 2360
// Prints a temporary error messages after file operation
2361
// Expects error status number (as defined in seq_file.h)
2362
/////////////////////////////////////////////////////////////////////////////
2363
s32 SEQ_UI_SDCardErrMsg(u16 delay, s32 status)
2364
{
2365
  // TODO: add more verbose error messages, they are clearly defined in seq_file.h)
2366
  char str[21];
2367
  sprintf(str, "E%3d (DOSFS: D%3d)", -status, seq_file_dfs_errno < 1000 ? seq_file_dfs_errno : 999);
596 tk 2368
  return SEQ_UI_Msg(SEQ_UI_MSG_SDCARD, delay, "!! SD Card Error !!!", str);
299 tk 2369
}