Subversion Repositories svn.mios32

Rev

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