Subversion Repositories svn.mios32

Rev

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