Subversion Repositories svn.mios32

Rev

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