Subversion Repositories svn.mios32

Rev

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