Subversion Repositories svn.mios32

Rev

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