Subversion Repositories svn.mios32

Rev

Rev 762 | Rev 774 | 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 763 2009-10-29 17:43:04Z tk $
2
/*
3
 * User Interface Routines
4
 *
5
 * ==========================================================================
6
 *
7
 *  Copyright (C) 2008 Thorsten Klose (tk@midibox.org)
8
 *  Licensed for personal non-commercial use only.
9
 *  All other rights reserved.
10
 *
11
 * ==========================================================================
12
 */
13
 
14
/////////////////////////////////////////////////////////////////////////////
15
// Include files
16
/////////////////////////////////////////////////////////////////////////////
17
 
272 tk 18
// with this switch, seq_ui.h/seq_ui_pages.inc will create local variables
19
#define SEQ_UI_PAGES_INC_LOCAL_VARS 1
20
 
134 tk 21
#include <mios32.h>
299 tk 22
#include <string.h>
690 tk 23
#include <blm.h>
464 tk 24
#include <blm_x.h>
190 tk 25
#include <seq_midi_out.h>
625 tk 26
#include <seq_midi_sysex.h>
190 tk 27
#include <seq_bpm.h>
134 tk 28
 
290 tk 29
#include "tasks.h"
134 tk 30
#include "seq_ui.h"
728 tk 31
#include "seq_lcd.h"
32
#include "seq_lcd_logo.h"
492 tk 33
#include "seq_hwcfg.h"
134 tk 34
#include "seq_lcd.h"
35
#include "seq_led.h"
186 tk 36
#include "seq_midply.h"
134 tk 37
#include "seq_core.h"
399 tk 38
#include "seq_song.h"
333 tk 39
#include "seq_par.h"
178 tk 40
#include "seq_layer.h"
184 tk 41
#include "seq_cc.h"
299 tk 42
#include "seq_file.h"
690 tk 43
#include "seq_file_hw.h"
134 tk 44
 
45
 
46
/////////////////////////////////////////////////////////////////////////////
47
// Global variables
48
/////////////////////////////////////////////////////////////////////////////
49
 
159 tk 50
u8 seq_ui_display_update_req;
51
u8 seq_ui_display_init_req;
134 tk 52
 
167 tk 53
seq_ui_button_state_t seq_ui_button_state;
54
 
134 tk 55
u8 ui_selected_group;
484 tk 56
u16 ui_selected_tracks;
134 tk 57
u8 ui_selected_par_layer;
58
u8 ui_selected_trg_layer;
328 tk 59
u8 ui_selected_instrument;
134 tk 60
u8 ui_selected_step_view;
61
u8 ui_selected_step;
168 tk 62
u8 ui_selected_item;
134 tk 63
 
168 tk 64
u8 ui_selected_item;
134 tk 65
 
240 tk 66
u16 ui_hold_msg_ctr;
67
 
206 tk 68
seq_ui_page_t ui_page;
272 tk 69
seq_ui_page_t ui_selected_page;
600 tk 70
seq_ui_page_t ui_stepview_prev_page;
71
seq_ui_page_t ui_trglayer_prev_page;
72
seq_ui_page_t ui_parlayer_prev_page;
206 tk 73
 
173 tk 74
volatile u8 ui_cursor_flash;
740 tk 75
volatile u8 ui_cursor_flash_overrun_ctr;
173 tk 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;
729 tk 81
u8 ui_edit_preset_num_drum;
316 tk 82
 
193 tk 83
u8 ui_seq_pause;
84
 
625 tk 85
seq_ui_remote_mode_t seq_ui_remote_mode;
626 tk 86
seq_ui_remote_mode_t seq_ui_remote_active_mode;
625 tk 87
mios32_midi_port_t seq_ui_remote_port;
626 tk 88
mios32_midi_port_t seq_ui_remote_active_port;
625 tk 89
u8 seq_ui_remote_id;
626 tk 90
u16 seq_ui_remote_client_timeout_ctr;
91
u8 seq_ui_remote_force_lcd_update;
92
u8 seq_ui_remote_force_led_update;
193 tk 93
 
634 tk 94
u8 seq_ui_backup_req;
740 tk 95
u8 seq_ui_format_req;
625 tk 96
 
744 tk 97
// to display directories via SEQ_UI_SelectListItem() and SEQ_LCD_PrintList() -- see seq_ui_sysex.c as example
746 tk 98
char ui_global_dir_list[80];
634 tk 99
 
744 tk 100
 
134 tk 101
/////////////////////////////////////////////////////////////////////////////
102
// Local variables
103
/////////////////////////////////////////////////////////////////////////////
104
 
168 tk 105
static s32 (*ui_button_callback)(seq_ui_button_t button, s32 depressed);
106
static s32 (*ui_encoder_callback)(seq_ui_encoder_t encoder, s32 incrementer);
173 tk 107
static s32 (*ui_led_callback)(u16 *gp_leds);
167 tk 108
static s32 (*ui_lcd_callback)(u8 high_prio);
352 tk 109
static s32 (*ui_exit_callback)(void);
752 tk 110
static s32 (*ui_delayed_action_callback)(u32 parameter);
111
static u32 ui_delayed_action_parameter;
167 tk 112
 
168 tk 113
static u16 ui_gp_leds;
167 tk 114
 
524 tk 115
#define NUM_BLM_LED_SRS (2*4)
116
static u8 ui_blm_leds[NUM_BLM_LED_SRS];
117
 
596 tk 118
#define UI_MSG_MAX_CHAR 21
119
static char ui_msg[2][UI_MSG_MAX_CHAR];
120
static u16 ui_msg_ctr;
121
static seq_ui_msg_type_t ui_msg_type;
299 tk 122
 
740 tk 123
static u16 ui_delayed_action_ctr;
299 tk 124
 
752 tk 125
 
126
 
134 tk 127
/////////////////////////////////////////////////////////////////////////////
128
// Initialisation
129
/////////////////////////////////////////////////////////////////////////////
130
s32 SEQ_UI_Init(u32 mode)
131
{
626 tk 132
  int i;
133
  // clear all LEDs
134
  for(i=0; i<SEQ_LED_NUM_SR; ++i)
135
    SEQ_LED_SRSet(i, 0x00);
136
 
134 tk 137
  // init selection variables
138
  ui_selected_group = 0;
139
  ui_selected_tracks = (1 << 0);
140
  ui_selected_par_layer = 0;
141
  ui_selected_trg_layer = 0;
328 tk 142
  ui_selected_instrument = 0;
134 tk 143
  ui_selected_step_view = 0;
144
  ui_selected_step = 0;
168 tk 145
  ui_selected_item = 0;
134 tk 146
 
240 tk 147
  ui_hold_msg_ctr = 0;
596 tk 148
  ui_msg_ctr = 0;
740 tk 149
  ui_delayed_action_ctr = 0;
240 tk 150
 
740 tk 151
  ui_cursor_flash_ctr = ui_cursor_flash_overrun_ctr = 0;
173 tk 152
  ui_cursor_flash = 0;
168 tk 153
 
167 tk 154
  seq_ui_button_state.ALL = 0;
155
 
193 tk 156
  ui_seq_pause = 0;
157
 
134 tk 158
  // visible GP pattern
159
  ui_gp_leds = 0x0000;
160
 
625 tk 161
  // default remote mode
162
  seq_ui_remote_mode = SEQ_UI_REMOTE_MODE_AUTO;
626 tk 163
  seq_ui_remote_active_mode = SEQ_UI_REMOTE_MODE_AUTO;
625 tk 164
  seq_ui_remote_port = DEFAULT;
626 tk 165
  seq_ui_remote_active_port = DEFAULT;
625 tk 166
  seq_ui_remote_id = 0x00;
626 tk 167
  seq_ui_remote_client_timeout_ctr = 0;
168
  seq_ui_remote_force_lcd_update = 0;
169
  seq_ui_remote_force_led_update = 0;
625 tk 170
 
634 tk 171
  // misc
172
  seq_ui_backup_req = 0;
740 tk 173
  seq_ui_format_req = 0;
634 tk 174
 
167 tk 175
  // change to edit page
176
  ui_page = SEQ_UI_PAGE_NONE;
177
  SEQ_UI_PageSet(SEQ_UI_PAGE_EDIT);
134 tk 178
 
596 tk 179
 
134 tk 180
  return 0; // no error
181
}
182
 
183
 
184
/////////////////////////////////////////////////////////////////////////////
178 tk 185
// Inits the speed mode of all encoders
186
// Auto mode should be used whenever:
187
//    - the edit screen is entered
188
//    - the group is changed
189
//    - a track is changed
190
//    - a layer is changed
191
/////////////////////////////////////////////////////////////////////////////
192
s32 SEQ_UI_InitEncSpeed(u32 auto_config)
193
{
194
  mios32_enc_config_t enc_config;
195
 
196
  if( auto_config ) {
513 tk 197
 
198
    if( !seq_hwcfg_enc.auto_fast )
199
      return 0; // auto mode not enabled - ignore auto reconfiguration request
200
 
333 tk 201
    switch( SEQ_PAR_AssignmentGet(SEQ_UI_VisibleTrackGet(), ui_selected_par_layer) ) {
202
      case SEQ_PAR_Type_Velocity:
203
      case SEQ_PAR_Type_Length:
204
      case SEQ_PAR_Type_CC:
205
      case SEQ_PAR_Type_PitchBend:
206
      case SEQ_PAR_Type_Probability:
207
      case SEQ_PAR_Type_Delay:
178 tk 208
    seq_ui_button_state.FAST_ENCODERS = 1;
209
    break;
210
 
211
      default:
212
    seq_ui_button_state.FAST_ENCODERS = 0;
213
    }
214
  }
215
 
216
  // change for datawheel and GP encoders
217
  int enc;
218
  for(enc=0; enc<17; ++enc) {
219
    enc_config = MIOS32_ENC_ConfigGet(enc);
220
    enc_config.cfg.speed = seq_ui_button_state.FAST_ENCODERS ? FAST : NORMAL;
513 tk 221
    enc_config.cfg.speed_par = (enc == 0) ? seq_hwcfg_enc.datawheel_fast_speed : seq_hwcfg_enc.gp_fast_speed;
178 tk 222
    MIOS32_ENC_ConfigSet(enc, enc_config);
223
  }
224
 
225
  return 0; // no error
226
}
227
 
228
 
229
/////////////////////////////////////////////////////////////////////////////
167 tk 230
// Various installation routines for menu page LCD handlers
231
/////////////////////////////////////////////////////////////////////////////
168 tk 232
s32 SEQ_UI_InstallButtonCallback(void *callback)
167 tk 233
{
168 tk 234
  ui_button_callback = callback;
167 tk 235
  return 0; // no error
236
}
237
 
168 tk 238
s32 SEQ_UI_InstallEncoderCallback(void *callback)
167 tk 239
{
168 tk 240
  ui_encoder_callback = callback;
167 tk 241
  return 0; // no error
242
}
243
 
168 tk 244
s32 SEQ_UI_InstallLEDCallback(void *callback)
167 tk 245
{
168 tk 246
  ui_led_callback = callback;
167 tk 247
  return 0; // no error
248
}
249
 
168 tk 250
s32 SEQ_UI_InstallLCDCallback(void *callback)
167 tk 251
{
252
  ui_lcd_callback = callback;
253
  return 0; // no error
254
}
255
 
352 tk 256
s32 SEQ_UI_InstallExitCallback(void *callback)
257
{
258
  ui_exit_callback = callback;
259
  return 0; // no error
260
}
167 tk 261
 
352 tk 262
 
752 tk 263
s32 SEQ_UI_InstallDelayedActionCallback(void *callback, u16 delay_mS, u32 parameter)
740 tk 264
{
752 tk 265
  // must be atomic
266
  MIOS32_IRQ_Disable();
267
  ui_delayed_action_parameter = parameter;
740 tk 268
  ui_delayed_action_callback = callback;
752 tk 269
  ui_delayed_action_ctr = delay_mS;
270
  MIOS32_IRQ_Enable();
740 tk 271
 
272
  return 0; // no error
273
}
274
 
275
s32 SEQ_UI_UnInstallDelayedActionCallback(void *callback)
276
{
752 tk 277
  // must be atomic
278
  MIOS32_IRQ_Disable();
740 tk 279
  if( ui_delayed_action_callback == callback )
280
    ui_delayed_action_callback = 0;
752 tk 281
  MIOS32_IRQ_Enable();
282
 
740 tk 283
  return 0; // no error
284
}
285
 
286
 
167 tk 287
/////////////////////////////////////////////////////////////////////////////
288
// Change the menu page
289
/////////////////////////////////////////////////////////////////////////////
290
s32 SEQ_UI_PageSet(seq_ui_page_t page)
291
{
292
  if( page != ui_page ) {
352 tk 293
 
294
    // call page exit callback
295
    if( ui_exit_callback != NULL )
296
      ui_exit_callback();
297
 
167 tk 298
    // disable hooks of previous page and request re-initialisation
318 tk 299
    portENTER_CRITICAL();
167 tk 300
    ui_page = page;
168 tk 301
    ui_button_callback = NULL;
302
    ui_encoder_callback = NULL;
303
    ui_led_callback = NULL;
167 tk 304
    ui_lcd_callback = NULL;
352 tk 305
    ui_exit_callback = NULL;
740 tk 306
    ui_delayed_action_callback = NULL;
318 tk 307
    portEXIT_CRITICAL();
167 tk 308
 
513 tk 309
    if( seq_hwcfg_button_beh.menu )
310
      seq_ui_button_state.MENU_PRESSED = 0; // MENU page selection finished
167 tk 311
 
312
    // request display initialisation
313
    seq_ui_display_init_req = 1;
314
  }
173 tk 315
 
306 tk 316
  // for MENU button:
317
  // first page has been selected - display new screen
318
  seq_ui_button_state.MENU_FIRST_PAGE_SELECTED = 1;
319
 
272 tk 320
  // for SEQ_UI_MENU which is accessible with EXIT button
321
  // remember the current selectable page
322
  if( ui_page >= SEQ_UI_FIRST_MENU_SELECTION_PAGE )
323
    ui_selected_page = ui_page;
324
 
184 tk 325
  return 0; // no error
167 tk 326
}
327
 
328
 
329
/////////////////////////////////////////////////////////////////////////////
272 tk 330
// Returns name of menu page (18 characters)
331
/////////////////////////////////////////////////////////////////////////////
332
char *SEQ_UI_PageNameGet(seq_ui_page_t page)
333
{
690 tk 334
  return (char *)ui_menu_pages[page].name;
272 tk 335
}
336
 
337
 
338
/////////////////////////////////////////////////////////////////////////////
134 tk 339
// Dedicated button functions
340
// Mapped to physical buttons in SEQ_UI_Button_Handler()
341
// Will also be mapped to MIDI keys later (for MIDI remote function)
342
/////////////////////////////////////////////////////////////////////////////
343
static s32 SEQ_UI_Button_GP(s32 depressed, u32 gp)
344
{
306 tk 345
  // in MENU page: overrule GP buttons so long MENU button is pressed/active
346
  if( seq_ui_button_state.MENU_PRESSED ) {
347
    if( depressed ) return -1;
348
    SEQ_UI_PageSet(ui_shortcut_menu_pages[gp]);
349
  } else {
350
    // forward to menu page
740 tk 351
    if( ui_button_callback != NULL ) {
306 tk 352
      ui_button_callback(gp, depressed);
740 tk 353
      ui_cursor_flash_ctr = ui_cursor_flash_overrun_ctr = 0;
354
    }
306 tk 355
  }
134 tk 356
 
357
  return 0; // no error
358
}
359
 
360
static s32 SEQ_UI_Button_Left(s32 depressed)
361
{
168 tk 362
  // forward to menu page
740 tk 363
  if( !seq_ui_button_state.MENU_PRESSED && ui_button_callback != NULL ) {
168 tk 364
    ui_button_callback(SEQ_UI_BUTTON_Left, depressed);
740 tk 365
    ui_cursor_flash_ctr = ui_cursor_flash_overrun_ctr = 0;
366
  }
134 tk 367
 
368
  return 0; // no error
369
}
370
 
371
static s32 SEQ_UI_Button_Right(s32 depressed)
372
{
168 tk 373
  // forward to menu page
740 tk 374
  if( !seq_ui_button_state.MENU_PRESSED && ui_button_callback != NULL ) {
168 tk 375
    ui_button_callback(SEQ_UI_BUTTON_Right, depressed);
740 tk 376
    ui_cursor_flash_ctr = ui_cursor_flash_overrun_ctr = 0;
377
  }
134 tk 378
 
379
  return 0; // no error
380
}
381
 
168 tk 382
static s32 SEQ_UI_Button_Down(s32 depressed)
383
{
240 tk 384
  seq_ui_button_state.DOWN = depressed ? 0 : 1;
385
 
168 tk 386
  // forward to menu page
740 tk 387
  if( !seq_ui_button_state.MENU_PRESSED && ui_button_callback != NULL ) {
168 tk 388
    ui_button_callback(SEQ_UI_BUTTON_Down, depressed);
740 tk 389
    ui_cursor_flash_ctr = ui_cursor_flash_overrun_ctr = 0;
390
  }
168 tk 391
 
392
  return 0; // no error
393
}
394
 
395
static s32 SEQ_UI_Button_Up(s32 depressed)
396
{
240 tk 397
  seq_ui_button_state.UP = depressed ? 0 : 1;
398
 
168 tk 399
  // forward to menu page
740 tk 400
  if( !seq_ui_button_state.MENU_PRESSED && ui_button_callback != NULL ) {
168 tk 401
    ui_button_callback(SEQ_UI_BUTTON_Up, depressed);
740 tk 402
    ui_cursor_flash_ctr = ui_cursor_flash_overrun_ctr = 0;
403
  }
168 tk 404
 
405
  return 0; // no error
406
}
407
 
134 tk 408
static s32 SEQ_UI_Button_Stop(s32 depressed)
409
{
410
  if( depressed ) return -1; // ignore when button depressed
411
 
159 tk 412
  // if sequencer running: stop it
413
  // if sequencer already stopped: reset song position
193 tk 414
  if( SEQ_BPM_IsRunning() )
415
    SEQ_BPM_Stop();
759 tk 416
  else {
417
    SEQ_SONG_Reset();
159 tk 418
    SEQ_CORE_Reset();
759 tk 419
    SEQ_MIDPLY_Reset();
420
  }
159 tk 421
 
134 tk 422
  return 0; // no error
423
}
424
 
425
static s32 SEQ_UI_Button_Pause(s32 depressed)
426
{
427
  if( depressed ) return -1; // ignore when button depressed
428
 
193 tk 429
  // if in auto mode and BPM generator is clocked in slave mode:
430
  // change to master mode
431
  SEQ_BPM_CheckAutoMaster();
159 tk 432
 
193 tk 433
  // toggle pause mode
434
  ui_seq_pause ^= 1;
435
 
436
  // execute stop/continue depending on new mode
437
  if( ui_seq_pause )
438
    SEQ_BPM_Stop();
439
  else
440
    SEQ_BPM_Cont();
441
 
134 tk 442
  return 0; // no error
443
}
444
 
445
static s32 SEQ_UI_Button_Play(s32 depressed)
446
{
447
  if( depressed ) return -1; // ignore when button depressed
448
 
186 tk 449
  // if in auto mode and BPM generator is clocked in slave mode:
450
  // change to master mode
451
  SEQ_BPM_CheckAutoMaster();
452
 
193 tk 453
#if 0
454
  // if sequencer running: restart it
455
  // if sequencer stopped: continue at last song position
456
  if( SEQ_BPM_IsRunning() )
457
    SEQ_BPM_Start();
458
  else
459
    SEQ_BPM_Cont();
186 tk 460
#else
193 tk 461
  // always restart sequencer
462
  SEQ_BPM_Start();
186 tk 463
#endif
159 tk 464
 
134 tk 465
  return 0; // no error
466
}
467
 
468
static s32 SEQ_UI_Button_Rew(s32 depressed)
469
{
240 tk 470
  seq_ui_button_state.REW = depressed ? 0 : 1;
471
 
134 tk 472
  if( depressed ) return -1; // ignore when button depressed
473
 
607 tk 474
  if( SEQ_SONG_ActiveGet() ) {
475
    portENTER_CRITICAL();
476
    SEQ_SONG_Rew();
477
    portEXIT_CRITICAL();
478
  } else
479
    SEQ_UI_Msg(SEQ_UI_MSG_USER, 1000, "We are not", "in Song Mode!");
480
 
134 tk 481
  return 0; // no error
482
}
483
 
484
static s32 SEQ_UI_Button_Fwd(s32 depressed)
485
{
240 tk 486
  seq_ui_button_state.FWD = depressed ? 0 : 1;
487
 
134 tk 488
  if( depressed ) return -1; // ignore when button depressed
489
 
607 tk 490
  if( SEQ_SONG_ActiveGet() ) {
491
    portENTER_CRITICAL();
492
    SEQ_SONG_Fwd();
493
    portEXIT_CRITICAL();
494
  } else
495
    SEQ_UI_Msg(SEQ_UI_MSG_USER, 1000, "We are not", "in Song Mode!");
496
 
134 tk 497
  return 0; // no error
498
}
499
 
596 tk 500
static s32 SEQ_UI_Button_Loop(s32 depressed)
501
{
502
  if( seq_hwcfg_button_beh.loop ) {
503
    // toggle mode
504
    if( depressed ) return -1; // ignore when button depressed
505
    // should be atomic
506
    portENTER_CRITICAL();
507
    seq_core_state.LOOP ^= 1;
508
  } else {
509
    // should be atomic
510
    portENTER_CRITICAL();
511
    // set mode
512
    seq_core_state.LOOP = depressed ? 0 : 1;
513
  }
514
  portEXIT_CRITICAL();
515
 
516
  SEQ_UI_Msg(SEQ_UI_MSG_USER, 1000, "Loop Mode", seq_core_state.LOOP ? "    on" : "   off");
517
 
518
  return 0; // no error
519
}
520
 
521
static s32 SEQ_UI_Button_Scrub(s32 depressed)
522
{
523
  // double function: -> Loop if menu button pressed
524
  if( seq_ui_button_state.MENU_PRESSED )
525
    return SEQ_UI_Button_Loop(depressed);
526
 
527
  if( seq_hwcfg_button_beh.scrub ) {
528
    // toggle mode
529
    if( depressed ) return -1; // ignore when button depressed
530
    seq_ui_button_state.SCRUB ^= 1;
531
  } else {
532
    // set mode
533
    seq_ui_button_state.SCRUB = depressed ? 0 : 1;
534
  }
535
 
536
  SEQ_UI_Msg(SEQ_UI_MSG_USER, 1000, "Scrub Mode", seq_ui_button_state.SCRUB ? "    on" : "   off");
537
 
538
  return 0; // no error
539
}
540
 
513 tk 541
static s32 SEQ_UI_Button_TempoPreset(s32 depressed)
134 tk 542
{
513 tk 543
  static seq_ui_page_t prev_page = SEQ_UI_PAGE_NONE;
240 tk 544
 
513 tk 545
  if( seq_hwcfg_button_beh.tempo_preset ) {
546
    if( depressed ) return -1; // ignore when button depressed
678 tk 547
    if( !seq_ui_button_state.TEMPO_PRESET ) // due to page change: button going to be set, clear other toggle buttons
548
      seq_ui_button_state.PAGE_CHANGE_BUTTON_FLAGS = 0;
513 tk 549
    seq_ui_button_state.TEMPO_PRESET ^= 1; // toggle TEMPO_PRESET pressed (will also be released once GP button has been pressed)
550
  } else {
551
    // set mode
552
    seq_ui_button_state.TEMPO_PRESET = depressed ? 0 : 1;
553
  }
134 tk 554
 
513 tk 555
  if( seq_ui_button_state.TEMPO_PRESET ) {
556
    prev_page = ui_page;
557
    SEQ_UI_PageSet(SEQ_UI_PAGE_BPM_PRESETS);
558
  } else {
524 tk 559
    if( ui_page == SEQ_UI_PAGE_BPM_PRESETS )
560
      SEQ_UI_PageSet(prev_page);
513 tk 561
  }
240 tk 562
 
134 tk 563
  return 0; // no error
564
}
565
 
513 tk 566
static s32 SEQ_UI_Button_TapTempo(s32 depressed)
134 tk 567
{
513 tk 568
  seq_ui_button_state.TAP_TEMPO = depressed ? 0 : 1;
240 tk 569
 
134 tk 570
  if( depressed ) return -1; // ignore when button depressed
571
 
572
  return 0; // no error
573
}
574
 
524 tk 575
static s32 SEQ_UI_Button_ExtRestart(s32 depressed)
134 tk 576
{
577
  if( depressed ) return -1; // ignore when button depressed
578
 
596 tk 579
  // should be atomic
524 tk 580
  portENTER_CRITICAL();
581
  seq_core_state.EXT_RESTART_REQ = 1;
582
  portEXIT_CRITICAL();
583
 
596 tk 584
  SEQ_UI_Msg(SEQ_UI_MSG_USER, 1000, "External Restart", "requested");
585
 
134 tk 586
  return 0; // no error
587
}
588
 
596 tk 589
static s32 SEQ_UI_Button_Metronome(s32 depressed)
590
{
591
  // double function: -> ExtRestart if menu button pressed
592
  if( seq_ui_button_state.MENU_PRESSED )
593
    return SEQ_UI_Button_ExtRestart(depressed);
594
 
595
  if( seq_hwcfg_button_beh.metronome ) {
596
    // toggle mode
597
    if( depressed ) return -1; // ignore when button depressed
598
    // should be atomic
599
    portENTER_CRITICAL();
600
    seq_core_state.METRONOME ^= 1;
601
  } else {
602
    // should be atomic
603
    portENTER_CRITICAL();
604
    // set mode
605
    seq_core_state.METRONOME = depressed ? 0 : 1;
606
  }
607
  portEXIT_CRITICAL();
608
 
609
  SEQ_UI_Msg(SEQ_UI_MSG_USER, 1000, "Metronome", seq_core_state.METRONOME ? "    on" : "   off");
610
 
599 tk 611
#if 0
612
  // metronome button can be used to trigger pattern file fixes
613
  if( !depressed )
614
    SEQ_PATTERN_FixAll();
615
#endif
616
 
596 tk 617
  return 0; // no error
618
}
619
 
630 tk 620
static s32 SEQ_UI_Button_Record(s32 depressed)
621
{
622
  if( depressed ) return -1; // ignore when button depressed
623
 
624
  // change to utility page
625
  SEQ_UI_PageSet(SEQ_UI_PAGE_TRKREC);
626
 
627
  return 0; // no error
628
}
629
 
134 tk 630
static s32 SEQ_UI_Button_Utility(s32 depressed)
631
{
632
  if( depressed ) return -1; // ignore when button depressed
633
 
240 tk 634
  // change to utility page
635
  SEQ_UI_PageSet(SEQ_UI_PAGE_UTIL);
636
 
134 tk 637
  return 0; // no error
638
}
639
 
640
static s32 SEQ_UI_Button_Copy(s32 depressed)
641
{
240 tk 642
  static seq_ui_page_t prev_page = SEQ_UI_PAGE_NONE;
134 tk 643
 
240 tk 644
  seq_ui_button_state.COPY = depressed ? 0 : 1;
645
 
280 tk 646
  if( ui_page == SEQ_UI_PAGE_MIXER ) {
647
    if( depressed ) return -1;
648
    SEQ_UI_MIXER_Copy();
729 tk 649
    SEQ_UI_Msg(SEQ_UI_MSG_USER, 1000, "Mixer Map", "copied");
280 tk 650
    return 1;
729 tk 651
  } else if( ui_page == SEQ_UI_PAGE_SONG ) {
652
    if( depressed ) return -1;
653
    SEQ_UI_SONG_Copy();
654
    SEQ_UI_Msg(SEQ_UI_MSG_USER, 1000, "Song Position", "copied");
655
    return 1;
280 tk 656
  } else {
657
    if( !depressed ) {
658
      prev_page = ui_page;
659
      SEQ_UI_PageSet(SEQ_UI_PAGE_UTIL);
660
    }
240 tk 661
 
280 tk 662
    s32 status = SEQ_UI_UTIL_CopyButton(depressed);
240 tk 663
 
596 tk 664
    if( depressed ) {
665
      if( prev_page != SEQ_UI_PAGE_UTIL )
666
    SEQ_UI_PageSet(prev_page);
240 tk 667
 
596 tk 668
      SEQ_UI_Msg(SEQ_UI_MSG_USER, 1000, "Track", "copied");
669
    }
670
 
280 tk 671
    return status;
672
  }
134 tk 673
}
674
 
675
static s32 SEQ_UI_Button_Paste(s32 depressed)
676
{
240 tk 677
  static seq_ui_page_t prev_page = SEQ_UI_PAGE_NONE;
134 tk 678
 
240 tk 679
  seq_ui_button_state.PASTE = depressed ? 0 : 1;
680
 
280 tk 681
  if( ui_page == SEQ_UI_PAGE_MIXER ) {
682
    if( depressed ) return -1;
683
    SEQ_UI_MIXER_Paste();
729 tk 684
    SEQ_UI_Msg(SEQ_UI_MSG_USER, 1000, "Mixer Map", "pasted");
280 tk 685
    return 1;
729 tk 686
  } else if( ui_page == SEQ_UI_PAGE_SONG ) {
687
    if( depressed ) return -1;
688
    SEQ_UI_SONG_Paste();
689
    SEQ_UI_Msg(SEQ_UI_MSG_USER, 1000, "Song Position", "pasted");
690
    return 1;
280 tk 691
  } else {
692
    if( !depressed ) {
693
      prev_page = ui_page;
694
      SEQ_UI_PageSet(SEQ_UI_PAGE_UTIL);
695
    }
240 tk 696
 
280 tk 697
    s32 status = SEQ_UI_UTIL_PasteButton(depressed);
240 tk 698
 
596 tk 699
    if( depressed ) {
700
      if( prev_page != SEQ_UI_PAGE_UTIL )
701
    SEQ_UI_PageSet(prev_page);
240 tk 702
 
596 tk 703
      SEQ_UI_Msg(SEQ_UI_MSG_USER, 1000, "Track", "pasted");
704
    }
705
 
280 tk 706
    return status;
707
  }
134 tk 708
}
709
 
740 tk 710
 
711
// callback function for delayed Clear Mixer function
752 tk 712
static void SEQ_UI_Button_Clear_Mixer(u32 dummy)
740 tk 713
{
714
  SEQ_UI_MIXER_Clear();
715
  SEQ_UI_Msg(SEQ_UI_MSG_USER, 1000, "Mixer Map", "cleared");
716
}
717
 
718
// callback function for delayed Clear SongPos function
752 tk 719
static void SEQ_UI_Button_Clear_SongPos(u32 dummy)
740 tk 720
{
721
  SEQ_UI_SONG_Clear();
722
  SEQ_UI_Msg(SEQ_UI_MSG_USER, 1000, "Song Position", "cleared");
723
}
724
 
725
// callback function for clear track
752 tk 726
static void SEQ_UI_Button_Clear_Track(u32 dummy)
740 tk 727
{
728
  SEQ_UI_UTIL_ClearButton(0); // button pressed
729
  SEQ_UI_UTIL_ClearButton(1); // button depressed
730
  SEQ_UI_Msg(SEQ_UI_MSG_USER, 1000, "Track", "cleared");
731
}
732
 
134 tk 733
static s32 SEQ_UI_Button_Clear(s32 depressed)
734
{
240 tk 735
  seq_ui_button_state.CLEAR = depressed ? 0 : 1;
280 tk 736
 
737
  if( ui_page == SEQ_UI_PAGE_MIXER ) {
740 tk 738
    if( depressed )
739
      SEQ_UI_UnInstallDelayedActionCallback(SEQ_UI_Button_Clear_Mixer);
752 tk 740
    else {
741
      SEQ_UI_InstallDelayedActionCallback(SEQ_UI_Button_Clear_Mixer, 2000, 0);
742
      SEQ_UI_Msg(SEQ_UI_MSG_DELAYED_ACTION, 2001, "", "to clear Mixer Map");
743
    }
729 tk 744
    return 1;
745
  } else if( ui_page == SEQ_UI_PAGE_SONG ) {
740 tk 746
    if( depressed )
747
      SEQ_UI_UnInstallDelayedActionCallback(SEQ_UI_Button_Clear_SongPos);
752 tk 748
    else {
749
      SEQ_UI_InstallDelayedActionCallback(SEQ_UI_Button_Clear_SongPos, 2000, 0);
750
      SEQ_UI_Msg(SEQ_UI_MSG_DELAYED_ACTION, 2001, "", "to clear SongPos");
751
    }
729 tk 752
    return 1;
753
  } else {
740 tk 754
    if( depressed )
755
      SEQ_UI_UnInstallDelayedActionCallback(SEQ_UI_Button_Clear_Track);
752 tk 756
    else {
757
      SEQ_UI_InstallDelayedActionCallback(SEQ_UI_Button_Clear_Track, 2000, 0);
758
      SEQ_UI_Msg(SEQ_UI_MSG_DELAYED_ACTION, 2001, "", "to clear Track");
759
    }
280 tk 760
    return 1;
761
  }
134 tk 762
}
763
 
764
static s32 SEQ_UI_Button_Menu(s32 depressed)
765
{
513 tk 766
  if( seq_hwcfg_button_beh.menu ) {
767
    // toggle mode
768
    if( depressed ) return -1; // ignore when button depressed
769
    seq_ui_button_state.MENU_FIRST_PAGE_SELECTED = 0;
678 tk 770
    if( !seq_ui_button_state.MENU_PRESSED ) // due to page change: button going to be set, clear other toggle buttons
771
      seq_ui_button_state.PAGE_CHANGE_BUTTON_FLAGS = 0;
513 tk 772
    seq_ui_button_state.MENU_PRESSED ^= 1; // toggle MENU pressed (will also be released once GP button has been pressed)
773
  } else {
774
    // set mode
775
    seq_ui_button_state.MENU_FIRST_PAGE_SELECTED = 0;
776
    seq_ui_button_state.MENU_PRESSED = depressed ? 0 : 1;
777
  }
134 tk 778
 
779
  return 0; // no error
780
}
781
 
782
static s32 SEQ_UI_Button_Select(s32 depressed)
783
{
168 tk 784
  // forward to menu page
591 tk 785
  if( !seq_ui_button_state.MENU_PRESSED ) {
786
    seq_ui_button_state.SELECT_PRESSED = depressed ? 0 : 1;
740 tk 787
    if( ui_button_callback != NULL ) {
591 tk 788
      ui_button_callback(SEQ_UI_BUTTON_Select, depressed);
740 tk 789
      ui_cursor_flash_ctr = ui_cursor_flash_overrun_ctr = 0;
790
    }
591 tk 791
  }
134 tk 792
 
793
  return 0; // no error
794
}
795
 
796
static s32 SEQ_UI_Button_Exit(s32 depressed)
797
{
272 tk 798
  if( depressed ) return -1; // ignore when button depressed
799
 
800
  u8 prev_ui_page = ui_page;
801
 
168 tk 802
  // forward to menu page
740 tk 803
  if( !seq_ui_button_state.MENU_PRESSED && ui_button_callback != NULL ) {
168 tk 804
    ui_button_callback(SEQ_UI_BUTTON_Exit, depressed);
740 tk 805
    ui_cursor_flash_ctr = ui_cursor_flash_overrun_ctr = 0;
806
  }
134 tk 807
 
167 tk 808
  // release all button states
809
  seq_ui_button_state.ALL = 0;
810
 
272 tk 811
  // enter menu page if we were not there before
812
  if( prev_ui_page != SEQ_UI_PAGE_MENU )
813
    SEQ_UI_PageSet(SEQ_UI_PAGE_MENU);
814
 
134 tk 815
  return 0; // no error
816
}
817
 
818
static s32 SEQ_UI_Button_Edit(s32 depressed)
819
{
741 tk 820
  seq_ui_button_state.EDIT_PRESSED = depressed ? 0 : 1;
591 tk 821
 
134 tk 822
  if( depressed ) return -1; // ignore when button depressed
823
 
167 tk 824
  // change to edit page
825
  SEQ_UI_PageSet(SEQ_UI_PAGE_EDIT);
826
 
178 tk 827
  // set/clear encoder fast function if required
828
  SEQ_UI_InitEncSpeed(1); // auto config
829
 
134 tk 830
  return 0; // no error
831
}
832
 
833
static s32 SEQ_UI_Button_Mute(s32 depressed)
834
{
591 tk 835
  seq_ui_button_state.MUTE_PRESSED = depressed ? 0 : 1;
836
 
134 tk 837
  if( depressed ) return -1; // ignore when button depressed
838
 
184 tk 839
  SEQ_UI_PageSet(SEQ_UI_PAGE_MUTE);
840
 
134 tk 841
  return 0; // no error
842
}
843
 
844
static s32 SEQ_UI_Button_Pattern(s32 depressed)
845
{
591 tk 846
  seq_ui_button_state.PATTERN_PRESSED = depressed ? 0 : 1;
847
 
134 tk 848
  if( depressed ) return -1; // ignore when button depressed
849
 
184 tk 850
  SEQ_UI_PageSet(SEQ_UI_PAGE_PATTERN);
851
 
134 tk 852
  return 0; // no error
853
}
854
 
855
static s32 SEQ_UI_Button_Song(s32 depressed)
856
{
591 tk 857
  seq_ui_button_state.SONG_PRESSED = depressed ? 0 : 1;
858
 
134 tk 859
  if( depressed ) return -1; // ignore when button depressed
860
 
399 tk 861
  SEQ_UI_PageSet(SEQ_UI_PAGE_SONG);
862
 
134 tk 863
  return 0; // no error
864
}
865
 
866
static s32 SEQ_UI_Button_Solo(s32 depressed)
867
{
513 tk 868
  if( seq_hwcfg_button_beh.solo ) {
869
    // toggle mode
870
    if( depressed ) return -1; // ignore when button depressed
871
    seq_ui_button_state.SOLO ^= 1;
872
  } else {
873
    // set mode
874
    seq_ui_button_state.SOLO = depressed ? 0 : 1;
875
  }
134 tk 876
 
877
  return 0; // no error
878
}
879
 
880
static s32 SEQ_UI_Button_Fast(s32 depressed)
881
{
513 tk 882
  if( seq_hwcfg_button_beh.fast ) {
883
    // toggle mode
884
    if( depressed ) return -1; // ignore when button depressed
885
    seq_ui_button_state.FAST_ENCODERS ^= 1;
886
  } else {
887
    // set mode
888
    seq_ui_button_state.FAST_ENCODERS = depressed ? 0 : 1;
889
  }
134 tk 890
 
178 tk 891
  SEQ_UI_InitEncSpeed(0); // no auto config
892
 
134 tk 893
  return 0; // no error
894
}
895
 
896
static s32 SEQ_UI_Button_All(s32 depressed)
897
{
178 tk 898
  seq_ui_button_state.CHANGE_ALL_STEPS_SAME_VALUE = depressed ? 0 : 1;
134 tk 899
 
513 tk 900
  if( seq_hwcfg_button_beh.all ) {
901
    // toggle mode
902
    if( depressed ) return -1;
903
    seq_ui_button_state.CHANGE_ALL_STEPS ^= 1;
904
  } else {
905
    // set mode
906
    seq_ui_button_state.CHANGE_ALL_STEPS = depressed ? 0 : 1;
907
  }
178 tk 908
 
134 tk 909
  return 0; // no error
910
}
911
 
912
static s32 SEQ_UI_Button_StepView(s32 depressed)
913
{
600 tk 914
  //  static seq_ui_page_t prev_page = SEQ_UI_PAGE_NONE;
915
  // also used by seq_ui_stepsel
916
 
513 tk 917
  if( seq_hwcfg_button_beh.step_view ) {
918
    if( depressed ) return -1; // ignore when button depressed
678 tk 919
    if( !seq_ui_button_state.STEP_VIEW ) // due to page change: button going to be set, clear other toggle buttons
920
      seq_ui_button_state.PAGE_CHANGE_BUTTON_FLAGS = 0;
513 tk 921
    seq_ui_button_state.STEP_VIEW ^= 1; // toggle STEP_VIEW pressed (will also be released once GP button has been pressed)
922
  } else {
923
    // set mode
924
    seq_ui_button_state.STEP_VIEW = depressed ? 0 : 1;
925
  }
134 tk 926
 
513 tk 927
  if( seq_ui_button_state.STEP_VIEW ) {
600 tk 928
    ui_stepview_prev_page = ui_page;
303 tk 929
    SEQ_UI_PageSet(SEQ_UI_PAGE_STEPSEL);
930
  } else {
524 tk 931
    if( ui_page == SEQ_UI_PAGE_STEPSEL )
600 tk 932
      SEQ_UI_PageSet(ui_stepview_prev_page);
303 tk 933
  }
134 tk 934
 
935
  return 0; // no error
936
}
937
 
513 tk 938
static s32 SEQ_UI_Button_TrackSel(s32 depressed)
134 tk 939
{
513 tk 940
  static seq_ui_page_t prev_page = SEQ_UI_PAGE_NONE;
134 tk 941
 
513 tk 942
  if( seq_hwcfg_button_beh.track_sel ) {
943
    if( depressed ) return -1; // ignore when button depressed
678 tk 944
    if( !seq_ui_button_state.TRACK_SEL ) // due to page change: button going to be set, clear other toggle buttons
945
      seq_ui_button_state.PAGE_CHANGE_BUTTON_FLAGS = 0;
513 tk 946
    seq_ui_button_state.TRACK_SEL ^= 1; // toggle TRACKSEL status (will also be released once GP button has been pressed)
947
  } else {
948
    seq_ui_button_state.TRACK_SEL = depressed ? 0 : 1;
949
  }
950
 
951
  if( seq_ui_button_state.TRACK_SEL ) {
952
    prev_page = ui_page;
953
    SEQ_UI_PageSet(SEQ_UI_PAGE_TRACKSEL);
954
  } else {
524 tk 955
    if( ui_page == SEQ_UI_PAGE_TRACKSEL )
956
      SEQ_UI_PageSet(prev_page);
513 tk 957
  }
958
 
134 tk 959
  return 0; // no error
960
}
961
 
484 tk 962
static s32 SEQ_UI_Button_Track(s32 depressed, u32 track_button)
134 tk 963
{
484 tk 964
  static u8 button_state = 0x0f; // all 4 buttons depressed
134 tk 965
 
484 tk 966
  if( track_button >= 4 ) return -2; // max. 4 track buttons
134 tk 967
 
484 tk 968
  if( depressed ) {
969
    button_state |= (1 << track_button);
970
    return 0; // no error
971
  }
134 tk 972
 
484 tk 973
  button_state &= ~(1 << track_button);
974
 
975
  if( button_state == (~(1 << track_button) & 0xf) ) {
976
    // if only one select button pressed: radio-button function (1 of 4)
977
    ui_selected_tracks = 1 << (track_button + 4*ui_selected_group);
978
  } else {
979
    // if more than one select button pressed: toggle function (4 of 4)
980
    ui_selected_tracks ^= 1 << (track_button + 4*ui_selected_group);
981
  }
982
 
178 tk 983
  // set/clear encoder fast function if required
984
  SEQ_UI_InitEncSpeed(1); // auto config
985
 
134 tk 986
  return 0; // no error
987
}
988
 
989
static s32 SEQ_UI_Button_Group(s32 depressed, u32 group)
990
{
991
  if( depressed ) return -1; // ignore when button depressed
992
 
993
  if( group >= 4 ) return -2; // max. 4 group buttons
994
 
484 tk 995
  // if group has changed:
996
  if( group != ui_selected_group ) {
997
    // get current track selection
998
    u16 old_tracks = ui_selected_tracks >> (4*ui_selected_group);
134 tk 999
 
484 tk 1000
    // select new group
1001
    ui_selected_group = group;
1002
 
1003
    // take over old track selection
1004
    ui_selected_tracks = old_tracks << (4*ui_selected_group);
1005
  }
1006
 
178 tk 1007
  // set/clear encoder fast function if required
1008
  SEQ_UI_InitEncSpeed(1); // auto config
1009
 
134 tk 1010
  return 0; // no error
1011
}
1012
 
513 tk 1013
static s32 SEQ_UI_Button_ParLayerSel(s32 depressed)
1014
{
600 tk 1015
  // static seq_ui_page_t prev_page = SEQ_UI_PAGE_NONE;
1016
  // also used by seq_ui_parsel.c
513 tk 1017
 
1018
  if( seq_hwcfg_button_beh.par_layer ) {
1019
    if( depressed ) return -1; // ignore when button depressed
678 tk 1020
    if( !seq_ui_button_state.PAR_LAYER_SEL ) // due to page change: button going to be set, clear other toggle buttons
1021
      seq_ui_button_state.PAGE_CHANGE_BUTTON_FLAGS = 0;
513 tk 1022
    seq_ui_button_state.PAR_LAYER_SEL ^= 1; // toggle PARSEL status (will also be released once GP button has been pressed)
1023
  } else {
1024
    seq_ui_button_state.PAR_LAYER_SEL = depressed ? 0 : 1;
1025
  }
1026
 
1027
  if( seq_ui_button_state.PAR_LAYER_SEL ) {
600 tk 1028
    ui_parlayer_prev_page = ui_page;
513 tk 1029
    SEQ_UI_PageSet(SEQ_UI_PAGE_PARSEL);
1030
  } else {
524 tk 1031
    if( ui_page == SEQ_UI_PAGE_PARSEL )
600 tk 1032
      SEQ_UI_PageSet(ui_parlayer_prev_page);
513 tk 1033
  }
1034
 
1035
  // set/clear encoder fast function if required
1036
  SEQ_UI_InitEncSpeed(1); // auto config
1037
 
1038
  return 0; // no error
1039
}
1040
 
134 tk 1041
static s32 SEQ_UI_Button_ParLayer(s32 depressed, u32 par_layer)
1042
{
1043
  if( par_layer >= 3 ) return -2; // max. 3 parlayer buttons
1044
 
333 tk 1045
  u8 visible_track = SEQ_UI_VisibleTrackGet();
1046
  u8 num_p_layers = SEQ_PAR_NumLayersGet(visible_track);
134 tk 1047
 
333 tk 1048
  if( num_p_layers <= 3 ) {
1049
    // 3 layers: direct selection with LayerA/B/C button
1050
    if( depressed ) return -1; // ignore when button depressed
596 tk 1051
    if( par_layer >= num_p_layers ) {
1052
      char str1[21];
1053
      sprintf(str1, "Parameter Layer %c", 'A'+par_layer);
1054
      SEQ_UI_Msg(SEQ_UI_MSG_USER, 1000, str1, "not available!");
1055
    } else {
1056
      seq_ui_button_state.PAR_LAYER_SEL = 0;
1057
      ui_selected_par_layer = par_layer;
1058
    }
333 tk 1059
  } else if( num_p_layers <= 4 ) {
1060
    // 4 layers: LayerC Button toggles between C and D
1061
    if( depressed ) return -1; // ignore when button depressed
1062
    seq_ui_button_state.PAR_LAYER_SEL = 0;
336 tk 1063
    if( par_layer == 2 )
1064
      ui_selected_par_layer = (ui_selected_par_layer == 2) ? 3 : 2;
333 tk 1065
    else
1066
      ui_selected_par_layer = par_layer;
1067
  } else {
1068
    // >4 layers: LayerA/B button selects directly, Layer C button enters layer selection page
1069
    if( par_layer <= 1 ) {
1070
      if( depressed ) return -1; // ignore when button depressed
1071
      seq_ui_button_state.PAR_LAYER_SEL = 0;
1072
      ui_selected_par_layer = par_layer;
1073
    } else {
513 tk 1074
      return SEQ_UI_Button_ParLayerSel(depressed);
333 tk 1075
    }
1076
  }
1077
 
178 tk 1078
  // set/clear encoder fast function if required
1079
  SEQ_UI_InitEncSpeed(1); // auto config
1080
 
134 tk 1081
  return 0; // no error
1082
}
1083
 
513 tk 1084
static s32 SEQ_UI_Button_TrgLayerSel(s32 depressed)
1085
{
600 tk 1086
  // static seq_ui_page_t prev_page = SEQ_UI_PAGE_NONE;
1087
  // also used by seq_ui_trgsel.c
513 tk 1088
 
1089
  if( seq_hwcfg_button_beh.trg_layer ) {
1090
    if( depressed ) return -1; // ignore when button depressed
678 tk 1091
    if( !seq_ui_button_state.TRG_LAYER_SEL ) // due to page change: button going to be set, clear other toggle buttons
1092
      seq_ui_button_state.PAGE_CHANGE_BUTTON_FLAGS = 0;
513 tk 1093
    seq_ui_button_state.TRG_LAYER_SEL ^= 1; // toggle TRGSEL status (will also be released once GP button has been pressed)
1094
  } else {
1095
    seq_ui_button_state.TRG_LAYER_SEL = depressed ? 0 : 1;
1096
  }
1097
 
1098
  if( seq_ui_button_state.TRG_LAYER_SEL ) {
600 tk 1099
    ui_trglayer_prev_page = ui_page;
513 tk 1100
    SEQ_UI_PageSet(SEQ_UI_PAGE_TRGSEL);
1101
  } else {
524 tk 1102
    if( ui_page == SEQ_UI_PAGE_TRGSEL )
600 tk 1103
      SEQ_UI_PageSet(ui_trglayer_prev_page);
513 tk 1104
  }
1105
 
1106
  return 0; // no error
1107
}
1108
 
134 tk 1109
static s32 SEQ_UI_Button_TrgLayer(s32 depressed, u32 trg_layer)
1110
{
1111
  if( trg_layer >= 3 ) return -2; // max. 3 trglayer buttons
1112
 
326 tk 1113
  u8 visible_track = SEQ_UI_VisibleTrackGet();
1114
  u8 event_mode = SEQ_CC_Get(visible_track, SEQ_CC_MIDI_EVENT_MODE);
1115
  u8 num_t_layers = SEQ_TRG_NumLayersGet(visible_track);
134 tk 1116
 
328 tk 1117
  if( event_mode != SEQ_EVENT_MODE_Drum && num_t_layers <= 3 ) {
326 tk 1118
    // 3 layers: direct selection with LayerA/B/C button
1119
    if( depressed ) return -1; // ignore when button depressed
596 tk 1120
    if( trg_layer >= num_t_layers ) {
1121
      char str1[21];
1122
      sprintf(str1, "Trigger Layer %c", 'A'+trg_layer);
1123
      SEQ_UI_Msg(SEQ_UI_MSG_USER, 1000, str1, "not available!");
1124
    } else {
1125
      seq_ui_button_state.TRG_LAYER_SEL = 0;
1126
      ui_selected_trg_layer = trg_layer;
1127
    }
328 tk 1128
  } else if( event_mode != SEQ_EVENT_MODE_Drum && num_t_layers <= 4 ) {
326 tk 1129
    // 4 layers: LayerC Button toggles between C and D
1130
    if( depressed ) return -1; // ignore when button depressed
1131
    seq_ui_button_state.TRG_LAYER_SEL = 0;
336 tk 1132
    if( trg_layer == 2 )
1133
      ui_selected_trg_layer = (ui_selected_trg_layer == 2) ? 3 : 2;
326 tk 1134
    else
1135
      ui_selected_trg_layer = trg_layer;
1136
  } else {
333 tk 1137
    // >4 layers or drum mode: LayerA/B button selects directly, Layer C button enters trigger selection page
326 tk 1138
    // also used for drum tracks
1139
    if( trg_layer <= 1 ) {
1140
      if( depressed ) return -1; // ignore when button depressed
328 tk 1141
      seq_ui_button_state.TRG_LAYER_SEL = 0;
1142
      ui_selected_trg_layer = trg_layer;
326 tk 1143
    } else {
513 tk 1144
      return SEQ_UI_Button_TrgLayerSel(depressed);
326 tk 1145
    }
1146
  }
1147
 
134 tk 1148
  return 0; // no error
1149
}
1150
 
1151
 
544 tk 1152
static s32 SEQ_UI_Button_Morph(s32 depressed)
1153
{
1154
  if( depressed ) return -1; // ignore when button depressed
134 tk 1155
 
544 tk 1156
  SEQ_UI_PageSet(SEQ_UI_PAGE_TRKMORPH);
1157
 
1158
  return 0; // no error
1159
}
1160
 
1161
static s32 SEQ_UI_Button_Mixer(s32 depressed)
1162
{
1163
  if( depressed ) return -1; // ignore when button depressed
1164
 
1165
  SEQ_UI_PageSet(SEQ_UI_PAGE_MIXER);
1166
 
1167
  return 0; // no error
1168
}
1169
 
1170
static s32 SEQ_UI_Button_Transpose(s32 depressed)
1171
{
1172
  if( depressed ) return -1; // ignore when button depressed
1173
 
1174
  SEQ_UI_PageSet(SEQ_UI_PAGE_TRKTRAN);
1175
 
1176
  return 0; // no error
1177
}
1178
 
1179
 
742 tk 1180
// only used by keyboard remote function
1181
static s32 SEQ_UI_Button_ToggleGate(s32 depressed)
1182
{
1183
  if( depressed ) return -1; // ignore when button depressed
544 tk 1184
 
742 tk 1185
  u8 visible_track = SEQ_UI_VisibleTrackGet();
1186
  u8 gate = SEQ_TRG_GateGet(visible_track, ui_selected_step, ui_selected_instrument) ? 0 : 1;
1187
  SEQ_TRG_GateSet(visible_track, ui_selected_step, ui_selected_instrument, gate);
544 tk 1188
 
742 tk 1189
  return 0; // no error
1190
}
1191
 
1192
 
1193
 
134 tk 1194
/////////////////////////////////////////////////////////////////////////////
1195
// Button handler
1196
/////////////////////////////////////////////////////////////////////////////
1197
s32 SEQ_UI_Button_Handler(u32 pin, u32 pin_value)
1198
{
492 tk 1199
  int i;
1200
 
625 tk 1201
  // send MIDI event in remote mode and exit
626 tk 1202
  if( seq_ui_remote_active_mode == SEQ_UI_REMOTE_MODE_CLIENT )
625 tk 1203
    return SEQ_MIDI_SYSEX_REMOTE_Client_SendButton(pin, pin_value);
1204
 
496 tk 1205
  // ignore so long hardware config hasn't been read
1206
  if( !SEQ_FILE_HW_ConfigLocked() )
1207
    return -1;
1208
 
740 tk 1209
  // ignore during a backup or format is created
1210
  if( seq_ui_backup_req || seq_ui_format_req )
634 tk 1211
    return -1;
1212
 
328 tk 1213
  // ensure that selections are matching with track constraints
1214
  SEQ_UI_CheckSelections();
1215
 
492 tk 1216
  // request display update
1217
  seq_ui_display_update_req = 1;
134 tk 1218
 
596 tk 1219
  // stop current message if (new) button has been pressed
1220
  if( pin_value == 0 )
1221
    SEQ_UI_MsgStop();
134 tk 1222
 
596 tk 1223
 
492 tk 1224
  // MEMO: we could also use a jump table with references to the functions
1225
  // here, but this "spagetthi code" simplifies the configuration and
1226
  // the resulting ASM doesn't look that bad!
168 tk 1227
 
492 tk 1228
  for(i=0; i<SEQ_HWCFG_NUM_GP; ++i)
1229
    if( pin == seq_hwcfg_button.gp[i] )
1230
      return SEQ_UI_Button_GP(pin_value, i);
134 tk 1231
 
492 tk 1232
  for(i=0; i<SEQ_HWCFG_NUM_TRACK; ++i)
1233
    if( pin == seq_hwcfg_button.track[i] )
1234
      return SEQ_UI_Button_Track(pin_value, i);
513 tk 1235
  if( pin == seq_hwcfg_button.track_sel )
1236
    return SEQ_UI_Button_TrackSel(pin_value);
134 tk 1237
 
492 tk 1238
  for(i=0; i<SEQ_HWCFG_NUM_GROUP; ++i)
1239
    if( pin == seq_hwcfg_button.group[i] )
1240
      return SEQ_UI_Button_Group(pin_value, i);
134 tk 1241
 
492 tk 1242
  for(i=0; i<SEQ_HWCFG_NUM_PAR_LAYER; ++i)
1243
    if( pin == seq_hwcfg_button.par_layer[i] )
1244
      return SEQ_UI_Button_ParLayer(pin_value, i);
513 tk 1245
  if( pin == seq_hwcfg_button.par_layer_sel )
1246
    return SEQ_UI_Button_ParLayerSel(pin_value);
134 tk 1247
 
492 tk 1248
  for(i=0; i<SEQ_HWCFG_NUM_TRG_LAYER; ++i)
1249
    if( pin == seq_hwcfg_button.trg_layer[i] )
1250
      return SEQ_UI_Button_TrgLayer(pin_value, i);
513 tk 1251
  if( pin == seq_hwcfg_button.trg_layer_sel )
1252
    return SEQ_UI_Button_TrgLayerSel(pin_value);
134 tk 1253
 
492 tk 1254
  if( pin == seq_hwcfg_button.left )
1255
    return SEQ_UI_Button_Left(pin_value);
1256
  if( pin == seq_hwcfg_button.right )
1257
    return SEQ_UI_Button_Right(pin_value);
1258
  if( pin == seq_hwcfg_button.down )
1259
    return SEQ_UI_Button_Down(pin_value);
1260
  if( pin == seq_hwcfg_button.up )
1261
    return SEQ_UI_Button_Up(pin_value);
134 tk 1262
 
492 tk 1263
  if( pin == seq_hwcfg_button.scrub )
1264
    return SEQ_UI_Button_Scrub(pin_value);
1265
  if( pin == seq_hwcfg_button.metronome )
1266
    return SEQ_UI_Button_Metronome(pin_value);
134 tk 1267
 
630 tk 1268
  if( pin == seq_hwcfg_button.record )
1269
    return SEQ_UI_Button_Record(pin_value);
1270
 
492 tk 1271
  if( pin == seq_hwcfg_button.stop )
1272
    return SEQ_UI_Button_Stop(pin_value);
1273
  if( pin == seq_hwcfg_button.pause )
1274
    return SEQ_UI_Button_Pause(pin_value);
1275
  if( pin == seq_hwcfg_button.play )
1276
    return SEQ_UI_Button_Play(pin_value);
1277
  if( pin == seq_hwcfg_button.rew )
1278
    return SEQ_UI_Button_Rew(pin_value);
1279
  if( pin == seq_hwcfg_button.fwd )
1280
    return SEQ_UI_Button_Fwd(pin_value);
596 tk 1281
  if( pin == seq_hwcfg_button.loop )
1282
    return SEQ_UI_Button_Loop(pin_value);
134 tk 1283
 
492 tk 1284
  if( pin == seq_hwcfg_button.utility )
1285
    return SEQ_UI_Button_Utility(pin_value);
1286
  if( pin == seq_hwcfg_button.copy )
1287
    return SEQ_UI_Button_Copy(pin_value);
1288
  if( pin == seq_hwcfg_button.paste )
1289
    return SEQ_UI_Button_Paste(pin_value);
1290
  if( pin == seq_hwcfg_button.clear )
1291
    return SEQ_UI_Button_Clear(pin_value);
134 tk 1292
 
492 tk 1293
  if( pin == seq_hwcfg_button.menu )
1294
    return SEQ_UI_Button_Menu(pin_value);
1295
  if( pin == seq_hwcfg_button.select )
1296
    return SEQ_UI_Button_Select(pin_value);
1297
  if( pin == seq_hwcfg_button.exit )
1298
    return SEQ_UI_Button_Exit(pin_value);
134 tk 1299
 
513 tk 1300
  if( pin == seq_hwcfg_button.tap_tempo )
1301
    return SEQ_UI_Button_TapTempo(pin_value);
1302
  if( pin == seq_hwcfg_button.tempo_preset )
1303
    return SEQ_UI_Button_TempoPreset(pin_value);
524 tk 1304
  if( pin == seq_hwcfg_button.ext_restart )
1305
    return SEQ_UI_Button_ExtRestart(pin_value);
134 tk 1306
 
492 tk 1307
  if( pin == seq_hwcfg_button.edit )
1308
    return SEQ_UI_Button_Edit(pin_value);
1309
  if( pin == seq_hwcfg_button.mute )
1310
    return SEQ_UI_Button_Mute(pin_value);
1311
  if( pin == seq_hwcfg_button.pattern )
1312
    return SEQ_UI_Button_Pattern(pin_value);
1313
  if( pin == seq_hwcfg_button.song )
1314
    return SEQ_UI_Button_Song(pin_value);
134 tk 1315
 
492 tk 1316
  if( pin == seq_hwcfg_button.solo )
1317
    return SEQ_UI_Button_Solo(pin_value);
1318
  if( pin == seq_hwcfg_button.fast )
1319
    return SEQ_UI_Button_Fast(pin_value);
1320
  if( pin == seq_hwcfg_button.all )
1321
    return SEQ_UI_Button_All(pin_value);
134 tk 1322
 
492 tk 1323
  if( pin == seq_hwcfg_button.step_view )
1324
    return SEQ_UI_Button_StepView(pin_value);
1325
  if( pin == seq_hwcfg_button.tap_tempo )
1326
    return SEQ_UI_Button_TapTempo(pin_value);
134 tk 1327
 
544 tk 1328
  if( pin == seq_hwcfg_button.morph )
1329
    return SEQ_UI_Button_Morph(pin_value);
1330
  if( pin == seq_hwcfg_button.mixer )
1331
    return SEQ_UI_Button_Mixer(pin_value);
1332
  if( pin == seq_hwcfg_button.transpose )
1333
    return SEQ_UI_Button_Transpose(pin_value);
1334
 
625 tk 1335
  // always print debugging message
1336
#if 1
1337
  MIOS32_MIDI_SendDebugMessage("[SEQ_UI_Button_Handler] Button SR:%d, Pin:%d not mapped, it has been %s.\n",
1338
                   (pin >> 3) + 1,
1339
                   pin & 7,
1340
                   pin_value ? "depressed" : "pressed");
1341
#endif
1342
 
492 tk 1343
  return -1; // button not mapped
134 tk 1344
}
1345
 
1346
 
1347
/////////////////////////////////////////////////////////////////////////////
524 tk 1348
// BLM Button handler
1349
/////////////////////////////////////////////////////////////////////////////
1350
s32 SEQ_UI_BLM_Button_Handler(u32 row, u32 pin, u32 pin_value)
1351
{
625 tk 1352
  // send MIDI event in remote mode and exit
626 tk 1353
  if( seq_ui_remote_active_mode == SEQ_UI_REMOTE_MODE_CLIENT )
625 tk 1354
    return SEQ_MIDI_SYSEX_REMOTE_Client_Send_BLM_Button(row, pin, pin_value);
1355
 
524 tk 1356
  // ignore so long hardware config hasn't been read
1357
  if( !SEQ_FILE_HW_ConfigLocked() )
1358
    return -1;
1359
 
740 tk 1360
  // ignore during a backup or format is created
1361
  if( seq_ui_backup_req || seq_ui_format_req )
634 tk 1362
    return -1;
1363
 
524 tk 1364
  if( row >= SEQ_CORE_NUM_TRACKS_PER_GROUP )
1365
    return -1; // more than 4 tracks not supported (yet) - could be done in this function w/o much effort
1366
 
1367
  if( pin >= 16 )
1368
    return -1; // more than 16 step buttons not supported (yet) - could be done by selecting the step view
1369
 
1370
  // select track depending on row
1371
  ui_selected_tracks = 1 << (row + 4*ui_selected_group);
1372
 
1373
  // ensure that selections are matching with track constraints
1374
  SEQ_UI_CheckSelections();
1375
 
1376
  // request display update
1377
  seq_ui_display_update_req = 1;
1378
 
1379
  // emulate general purpose button
758 tk 1380
  if( seq_hwcfg_blm.buttons_no_ui ) {
763 tk 1381
    s32 status = SEQ_UI_EDIT_Button_Handler(pin, pin_value);
758 tk 1382
    ui_cursor_flash_ctr = ui_cursor_flash_overrun_ctr = 0;
1383
    return status;
763 tk 1384
  }
1385
 
1386
  return SEQ_UI_Button_GP(pin_value, pin); // no error, pin_value and pin are swapped for this function due to consistency reasons
524 tk 1387
}
1388
 
1389
 
1390
/////////////////////////////////////////////////////////////////////////////
134 tk 1391
// Encoder handler
1392
/////////////////////////////////////////////////////////////////////////////
1393
s32 SEQ_UI_Encoder_Handler(u32 encoder, s32 incrementer)
1394
{
625 tk 1395
  // send MIDI event in remote mode and exit
626 tk 1396
  if( seq_ui_remote_active_mode == SEQ_UI_REMOTE_MODE_CLIENT )
625 tk 1397
    return SEQ_MIDI_SYSEX_REMOTE_Client_SendEncoder(encoder, incrementer);
1398
 
496 tk 1399
  // ignore so long hardware config hasn't been read
1400
  if( !SEQ_FILE_HW_ConfigLocked() )
1401
    return -1;
1402
 
740 tk 1403
  // ignore during a backup or format is created
1404
  if( seq_ui_backup_req || seq_ui_format_req )
634 tk 1405
    return -1;
1406
 
134 tk 1407
  if( encoder > 16 )
1408
    return -1; // encoder doesn't exist
1409
 
328 tk 1410
  // ensure that selections are matching with track constraints
1411
  SEQ_UI_CheckSelections();
1412
 
596 tk 1413
  // stop current message
1414
  SEQ_UI_MsgStop();
1415
 
134 tk 1416
  // limit incrementer
1417
  if( incrementer > 3 )
1418
    incrementer = 3;
1419
  else if( incrementer < -3 )
1420
    incrementer = -3;
1421
 
607 tk 1422
  if( seq_ui_button_state.SCRUB && encoder == 0 ) {
1423
    // if sequencer isn't already running, continue it (don't restart)
1424
    if( !SEQ_BPM_IsRunning() )
1425
      SEQ_BPM_Cont();
1426
    ui_seq_pause = 0; // clear pause mode
1427
    // Scrub sequence back or forth
1428
    portENTER_CRITICAL(); // should be atomic
1429
    SEQ_CORE_Scrub(incrementer);
1430
    portEXIT_CRITICAL();
1431
  } else if( !seq_ui_button_state.MENU_PRESSED && ui_encoder_callback != NULL ) {
173 tk 1432
    ui_encoder_callback((encoder == 0) ? SEQ_UI_ENCODER_Datawheel : (encoder-1), incrementer);
740 tk 1433
    ui_cursor_flash_ctr = ui_cursor_flash_overrun_ctr = 0; // ensure that value is visible when it has been changed
134 tk 1434
  }
1435
 
1436
  // request display update
159 tk 1437
  seq_ui_display_update_req = 1;
134 tk 1438
 
1439
  return 0; // no error
1440
}
1441
 
1442
 
353 tk 1443
 
134 tk 1444
/////////////////////////////////////////////////////////////////////////////
626 tk 1445
// Receives a MIDI package from APP_NotifyReceivedEvent (-> app.c)
1446
/////////////////////////////////////////////////////////////////////////////
1447
s32 SEQ_UI_REMOTE_MIDI_Receive(mios32_midi_port_t port, mios32_midi_package_t midi_package)
1448
{
1449
#if 1
1450
  // check for active remote mode
1451
  if( seq_ui_remote_active_mode != SEQ_UI_REMOTE_MODE_SERVER )
1452
    return 0; // no error
1453
#endif
1454
 
1455
  if( (seq_ui_remote_port == DEFAULT && seq_ui_remote_active_port != port) ||
1456
      (seq_ui_remote_port != DEFAULT && port != seq_ui_remote_port) )
1457
    return 0; // wrong port
1458
 
1459
  // for easier parsing: convert Note Off -> Note On with velocity 0
1460
  if( midi_package.event == NoteOff ) {
1461
    midi_package.event = NoteOn;
1462
    midi_package.velocity = 0;
1463
  }
1464
 
1465
  switch( midi_package.event ) {
1466
    case NoteOn: {
1467
      switch( midi_package.chn ) {
1468
        case Chn1:
1469
      SEQ_UI_Button_Handler(midi_package.note + 0x00, midi_package.velocity ? 0 : 1);
1470
      break;
1471
        case Chn2:
1472
      SEQ_UI_Button_Handler(midi_package.note + 0x80, midi_package.velocity ? 0 : 1);
1473
      break;
1474
        case Chn3:
1475
      SEQ_UI_BLM_Button_Handler(midi_package.note >> 5, midi_package.note & 0x1f, midi_package.velocity ? 0 : 1);
1476
      break;
1477
      }
1478
    } break;
1479
 
1480
    case CC: {
1481
      if( midi_package.cc_number >= 15 && midi_package.cc_number <= 31 )
1482
    SEQ_UI_Encoder_Handler(midi_package.cc_number-15, (int)midi_package.value - 0x40);
1483
    } break;
1484
  }
1485
 
632 tk 1486
  return 1; // MIDI event has been taken for remote function -> don't forward to router/MIDI event parser
626 tk 1487
}
1488
 
1489
 
1490
/////////////////////////////////////////////////////////////////////////////
741 tk 1491
// MIDI Remote Keyboard Function (called from SEQ_MIDI_IN)
1492
/////////////////////////////////////////////////////////////////////////////
742 tk 1493
s32 SEQ_UI_REMOTE_MIDI_Keyboard(u8 key, u8 depressed)
741 tk 1494
{
742 tk 1495
#if 0
1496
  MIOS32_MIDI_SendDebugMessage("SEQ_UI_REMOTE_MIDI_Keyboard(%d, %d)\n", key, depressed);
741 tk 1497
#endif
742 tk 1498
 
1499
  switch( key ) {
1500
    case 0x24: // C-2
1501
      return SEQ_UI_Button_GP(depressed, 0);
1502
    case 0x25: // C#2
1503
      return SEQ_UI_Button_Track(depressed, 0);
1504
    case 0x26: // D-2
1505
      return SEQ_UI_Button_GP(depressed, 1);
1506
    case 0x27: // D#2
1507
      return SEQ_UI_Button_Track(depressed, 1);
1508
    case 0x28: // E-2
1509
      return SEQ_UI_Button_GP(depressed, 2);
1510
    case 0x29: // F-2
1511
      return SEQ_UI_Button_GP(depressed, 3);
1512
    case 0x2a: // F#2
1513
      return SEQ_UI_Button_Track(depressed, 2);
1514
    case 0x2b: // G-2
1515
      return SEQ_UI_Button_GP(depressed, 4);
1516
    case 0x2c: // G#2
1517
      return SEQ_UI_Button_Track(depressed, 3);
1518
    case 0x2d: // A-2
1519
      return SEQ_UI_Button_GP(depressed, 5);
1520
    case 0x2e: // A#2
1521
      return SEQ_UI_Button_ParLayer(depressed, 0);
1522
    case 0x2f: // B-2
1523
      return SEQ_UI_Button_GP(depressed, 6);
1524
 
1525
    case 0x30: // C-3
1526
      return SEQ_UI_Button_GP(depressed, 7);
1527
    case 0x31: // C#3
1528
      return SEQ_UI_Button_ParLayer(depressed, 1);
1529
    case 0x32: // D-3
1530
      return SEQ_UI_Button_GP(depressed, 8);
1531
    case 0x33: // D#3
1532
      return SEQ_UI_Button_ParLayer(depressed, 2);
1533
    case 0x34: // E-3
1534
      return SEQ_UI_Button_GP(depressed, 9);
1535
    case 0x35: // F-3
1536
      return SEQ_UI_Button_GP(depressed, 10);
1537
    case 0x36: // F#3
1538
      return SEQ_UI_Button_TrgLayer(depressed, 0);
1539
    case 0x37: // G-3
1540
      return SEQ_UI_Button_GP(depressed, 11);
1541
    case 0x38: // G#3
1542
      return SEQ_UI_Button_TrgLayer(depressed, 1);
1543
    case 0x39: // A-3
1544
      return SEQ_UI_Button_GP(depressed, 12);
1545
    case 0x3a: // A#3
1546
      return SEQ_UI_Button_TrgLayer(depressed, 2);
1547
    case 0x3b: // B-3
1548
      return SEQ_UI_Button_GP(depressed, 13);
1549
 
1550
    case 0x3c: // C-4
1551
      return SEQ_UI_Button_GP(depressed, 14);
1552
    case 0x3d: // C#4
1553
      return SEQ_UI_Button_Group(depressed, 0);
1554
    case 0x3e: // D-4
1555
      return SEQ_UI_Button_GP(depressed, 15);
1556
    case 0x3f: // D#4
1557
      return SEQ_UI_Button_Group(depressed, 1);
1558
    case 0x40: // E-4
1559
      return 0; // ignore
1560
    case 0x41: // F-4
1561
      return SEQ_UI_Button_StepView(depressed);
1562
    case 0x42: // F#4
1563
      return SEQ_UI_Button_Group(depressed, 1);
1564
    case 0x43: // G-4
1565
      return 0; // ignore
1566
    case 0x44: // G#4
1567
      return SEQ_UI_Button_Group(depressed, 2);
1568
    case 0x45: // A-4
1569
      return SEQ_UI_Button_Left(depressed);
1570
    case 0x46: // A#4
1571
      return SEQ_UI_Button_ToggleGate(depressed);
1572
    case 0x47: // B-4
1573
      return SEQ_UI_Button_Right(depressed);
1574
 
1575
    case 0x48: // C-5
1576
      return SEQ_UI_Button_Edit(depressed);
1577
    case 0x49: // C#5
1578
      return SEQ_UI_Button_Solo(depressed);
1579
    case 0x4a: // D-5
1580
      return SEQ_UI_Button_Mute(depressed);
1581
    case 0x4b: // D#5
1582
      return SEQ_UI_Button_All(depressed);
1583
    case 0x4c: // E-5
1584
      return SEQ_UI_Button_Pattern(depressed);
1585
    case 0x4d: // F-5
1586
      return SEQ_UI_Button_Song(depressed);
1587
    case 0x4e: // F#5
1588
      return SEQ_UI_Button_Fast(depressed);
1589
    case 0x4f: // G-5
1590
      return SEQ_UI_Button_Metronome(depressed);
1591
    case 0x50: // G#5
1592
      return SEQ_UI_Button_ExtRestart(depressed);
1593
    case 0x51: // A-5
1594
      return SEQ_UI_Button_ParLayerSel(depressed);
1595
    case 0x52: // A#5
1596
      return SEQ_UI_Button_TrackSel(depressed);
1597
    case 0x53: // B-5
1598
      return SEQ_UI_Button_Stop(depressed);
1599
 
1600
    case 0x54: // C-6
1601
      return SEQ_UI_Button_Play(depressed);
1602
    case 0x55: // C#6
1603
      return SEQ_UI_Button_Pause(depressed);
1604
    case 0x56: // D-6
1605
      return SEQ_UI_Button_Rew(depressed);
1606
    case 0x57: // D#6
1607
      return SEQ_UI_Button_Fwd(depressed);
1608
    case 0x58: // E-6
1609
      return SEQ_UI_Button_Utility(depressed);
1610
    case 0x59: // F-6
1611
      return SEQ_UI_Button_TempoPreset(depressed);
1612
    case 0x5a: // F#6
1613
      return 0; // ignore
1614
    case 0x5b: // G-6
1615
      return SEQ_UI_Button_Menu(depressed);
1616
    case 0x5c: // G#6
1617
      return SEQ_UI_Button_Select(depressed);
1618
    case 0x5d: // A-6
1619
      return SEQ_UI_Button_Exit(depressed);
1620
    case 0x5e: // A#6
1621
      return SEQ_UI_Button_Down(depressed);
1622
    case 0x5f: // B-6
1623
      return SEQ_UI_Button_Up(depressed);
1624
  }
1625
 
741 tk 1626
  return 0; // no error
1627
}
1628
 
1629
 
1630
/////////////////////////////////////////////////////////////////////////////
134 tk 1631
// Update LCD messages
1632
// Usually called from background task
1633
/////////////////////////////////////////////////////////////////////////////
1634
s32 SEQ_UI_LCD_Handler(void)
1635
{
728 tk 1636
  static u8 boot_animation_wait_ctr = 0;
1637
  static u8 boot_animation_lcd_pos = 0;
1638
 
625 tk 1639
  // special handling in remote client mode
626 tk 1640
  if( seq_ui_remote_active_mode == SEQ_UI_REMOTE_MODE_CLIENT )
625 tk 1641
    return SEQ_UI_LCD_Update();
1642
 
159 tk 1643
  if( seq_ui_display_init_req ) {
1644
    seq_ui_display_init_req = 0; // clear request
134 tk 1645
 
278 tk 1646
    // clear force update of LCD
1647
    SEQ_LCD_Clear();
1648
    SEQ_LCD_Update(1);
134 tk 1649
 
168 tk 1650
    // select first menu item
1651
    ui_selected_item = 0;
1652
 
167 tk 1653
    // call init function of current page
272 tk 1654
    if( ui_menu_pages[ui_page].init_callback != NULL )
1655
      ui_menu_pages[ui_page].init_callback(0); // mode
134 tk 1656
 
1657
    // request display update
159 tk 1658
    seq_ui_display_update_req = 1;
134 tk 1659
  }
1660
 
496 tk 1661
  // print boot screen so long hardware config hasn't been read
1662
  if( !SEQ_FILE_HW_ConfigLocked() ) {
728 tk 1663
    if( boot_animation_lcd_pos < (40-3) ) {
1664
      if( ++boot_animation_wait_ctr >= 75 ) {
1665
    boot_animation_wait_ctr = 0;
637 tk 1666
 
728 tk 1667
    if( boot_animation_lcd_pos == 0 ) {
1668
      SEQ_LCD_Clear();
1669
      SEQ_LCD_CursorSet(0, 0);
1670
      SEQ_LCD_PrintString(MIOS32_LCD_BOOT_MSG_LINE1 " " MIOS32_LCD_BOOT_MSG_LINE2);
1671
      SEQ_LCD_CursorSet(0, 1);
1672
      SEQ_LCD_PrintString("Searching for SD Card...");
1673
    }
1674
 
1675
    // logo is print on second LCD
1676
    SEQ_LCD_LOGO_Print(boot_animation_lcd_pos++);
1677
      }
1678
    }
740 tk 1679
  } else if( seq_ui_backup_req || seq_ui_format_req ) {
634 tk 1680
    SEQ_LCD_Clear();
1681
    SEQ_LCD_CursorSet(0, 0);
740 tk 1682
    //                     <-------------------------------------->
1683
    //                     0123456789012345678901234567890123456789
1684
    if( seq_ui_backup_req )
1685
      SEQ_LCD_PrintString("Creating File Backup - be patient!!!");
1686
    else if( seq_ui_format_req )
1687
      SEQ_LCD_PrintString("Formatting Files - be patient!!!");
1688
    else
1689
      SEQ_LCD_PrintString("Don't know what I'm doing! :-/");
637 tk 1690
 
1691
    if( seq_file_backup_notification != NULL ) {
1692
      int i;
1693
 
1694
      SEQ_LCD_CursorSet(0, 1);
634 tk 1695
      SEQ_LCD_PrintFormattedString("Creating '%s'", seq_file_backup_notification);
637 tk 1696
 
1697
      SEQ_LCD_CursorSet(40+3, 0);
1698
      SEQ_LCD_PrintString("Total: [");
1699
      for(i=0; i<20; ++i)
1700
    SEQ_LCD_PrintChar((i>(seq_file_backup_percentage/5)) ? ' ' : '#');
1701
      SEQ_LCD_PrintFormattedString("] %3d%%", seq_file_backup_percentage);
1702
 
1703
      SEQ_LCD_CursorSet(40+3, 1);
1704
      SEQ_LCD_PrintString("File:  [");
1705
      for(i=0; i<20; ++i)
1706
    SEQ_LCD_PrintChar((i>(seq_file_copy_percentage/5)) ? ' ' : '#');
1707
      SEQ_LCD_PrintFormattedString("] %3d%%", seq_file_copy_percentage);
1708
    }
1709
 
496 tk 1710
  } else if( seq_ui_button_state.MENU_PRESSED && !seq_ui_button_state.MENU_FIRST_PAGE_SELECTED ) {
1711
    SEQ_LCD_CursorSet(0, 0);
1712
    //                   <-------------------------------------->
1713
    //                   0123456789012345678901234567890123456789
306 tk 1714
    SEQ_LCD_PrintString("Menu Shortcuts:");
1715
    SEQ_LCD_PrintSpaces(25 + 40);
1716
    SEQ_LCD_CursorSet(0, 1);
1717
    SEQ_LCD_PrintString(UI_SHORTCUT_STR); // defined in seq_ui_pages.inc
1718
  } else {
1719
    // perform high priority LCD update request
1720
    if( ui_lcd_callback != NULL )
1721
      ui_lcd_callback(1); // high_prio
167 tk 1722
 
306 tk 1723
    // perform low priority LCD update request if requested
1724
    if( seq_ui_display_update_req ) {
1725
      seq_ui_display_update_req = 0; // clear request
328 tk 1726
 
1727
      // ensure that selections are matching with track constraints
1728
      SEQ_UI_CheckSelections();
1729
 
306 tk 1730
      if( ui_lcd_callback != NULL )
1731
    ui_lcd_callback(0); // no high_prio
1732
    }
134 tk 1733
  }
1734
 
353 tk 1735
  // transfer all changed characters to LCD
1736
  SEQ_UI_LCD_Update();
1737
 
1738
  return 0; // no error
1739
}
1740
 
1741
 
1742
/////////////////////////////////////////////////////////////////////////////
1743
// Called from SEQ_UI_LCD_Handler(), but optionally also from other tasks
1744
// to update the LCD screen immediately
1745
/////////////////////////////////////////////////////////////////////////////
1746
s32 SEQ_UI_LCD_Update(void)
1747
{
625 tk 1748
  // special handling in remote client mode
626 tk 1749
  if( seq_ui_remote_active_mode == SEQ_UI_REMOTE_MODE_CLIENT ) {
1750
    MIOS32_IRQ_Disable();
1751
    u8 force = seq_ui_remote_force_lcd_update;
1752
    seq_ui_remote_force_lcd_update = 0;
1753
    MIOS32_IRQ_Enable();
1754
    return SEQ_LCD_Update(force);
625 tk 1755
  }
1756
 
596 tk 1757
  // if UI message active: copy over the text
1758
  if( ui_msg_ctr ) {
1759
    const char *animation_l_ptr;
1760
    const char *animation_r_ptr;
1761
    u8 msg_x = 0;
1762
    u8 right_aligned = 0;
740 tk 1763
    u8 disable_message = 0;
299 tk 1764
 
596 tk 1765
    switch( ui_msg_type ) {
1766
      case SEQ_UI_MSG_SDCARD: {
1767
    //                             00112233
1768
    const char animation_l[2*4] = "   >>>> ";
1769
    //                             00112233
1770
    const char animation_r[2*4] = "  < << <";
1771
    animation_l_ptr = animation_l;
1772
    animation_r_ptr = animation_r;
1773
    msg_x = 0; // MEMO: print such important information at first LCD for the case the user hasn't connected the second LCD yet
1774
    right_aligned = 0;
1775
      } break;
1776
 
756 tk 1777
      case SEQ_UI_MSG_DELAYED_ACTION:
1778
      case SEQ_UI_MSG_DELAYED_ACTION_R: {
740 tk 1779
    //                             00112233
1780
    const char animation_l[2*4] = "   )))) ";
1781
    //                             00112233
1782
    const char animation_r[2*4] = "  ( (( (";
1783
    animation_l_ptr = animation_l;
1784
    animation_r_ptr = animation_r;
756 tk 1785
    if( ui_msg_type == SEQ_UI_MSG_DELAYED_ACTION_R ) {
1786
      msg_x = 40; // right LCD
1787
      right_aligned = 0;
1788
    } else {
1789
      msg_x = 39; // left LCD
1790
      right_aligned = 1;
1791
    }
740 tk 1792
 
1793
    if( ui_delayed_action_callback == NULL ) {
1794
      disable_message = 1; // button has been depressed before delay
1795
    } else {
1796
      int seconds = (ui_delayed_action_ctr / 1000) + 1;
1797
      if( seconds == 1 )
1798
        sprintf(ui_msg[0], "Hold for 1 second ");
1799
      else
1800
        sprintf(ui_msg[0], "Hold for %d seconds", seconds);
1801
    }
1802
      } break;
1803
 
741 tk 1804
      case SEQ_UI_MSG_USER_R: {
1805
    //                             00112233
1806
    const char animation_l[2*4] = "   **** ";
1807
    //                             00112233
1808
    const char animation_r[2*4] = "  * ** *";
1809
    animation_l_ptr = animation_l;
1810
    animation_r_ptr = animation_r;
1811
    msg_x = 40; // right display
1812
    right_aligned = 0;
1813
      } break;
1814
 
596 tk 1815
      default: {
1816
    //                             00112233
1817
    const char animation_l[2*4] = "   **** ";
1818
    //                             00112233
1819
    const char animation_r[2*4] = "  * ** *";
1820
    animation_l_ptr = animation_l;
1821
    animation_r_ptr = animation_r;
1822
    msg_x = 39;
1823
    right_aligned = 1;
1824
      } break;
1825
 
1826
    }
1827
 
740 tk 1828
    if( !disable_message ) {
1829
      int anum = (ui_msg_ctr % 1000) / 250;
596 tk 1830
 
740 tk 1831
      int len[2];
1832
      len[0] = strlen((char *)ui_msg[0]);
1833
      len[1] = strlen((char *)ui_msg[1]);
1834
      int len_max = len[0];
1835
      if( len[1] > len_max )
1836
    len_max = len[1];
596 tk 1837
 
740 tk 1838
      if( right_aligned )
1839
    msg_x -= (9 + len_max);
596 tk 1840
 
740 tk 1841
      int line;
1842
      for(line=0; line<2; ++line) {
1843
    SEQ_LCD_CursorSet(msg_x, line);
596 tk 1844
 
740 tk 1845
    // ensure that both lines are padded with same number of spaces
1846
    int end_pos = len[line];
1847
    while( end_pos < len_max )
1848
      ui_msg[line][end_pos++] = ' ';
1849
    ui_msg[line][end_pos] = 0;
1850
 
1851
    SEQ_LCD_PrintFormattedString(" %c%c| %s |%c%c ",
1852
                     *(animation_l_ptr + 2*anum + 0), *(animation_l_ptr + 2*anum + 1),
1853
                     (char *)ui_msg[line],
1854
                     *(animation_r_ptr + 2*anum + 0), *(animation_r_ptr + 2*anum + 1));
1855
      }
299 tk 1856
    }
1857
  }
1858
 
637 tk 1859
  // MSD USB notification at right corner if not in Disk page
1860
  // to warn user that USB MIDI is disabled and seq performance is bad now!
638 tk 1861
  if( (TASK_MSD_EnableGet() > 0) && ui_page != SEQ_UI_PAGE_DISK ) {
637 tk 1862
    SEQ_LCD_CursorSet(80-11, 0);
1863
    if( ui_cursor_flash ) SEQ_LCD_PrintSpaces(13); else SEQ_LCD_PrintString(" [MSD USB] ");
1864
    SEQ_LCD_CursorSet(80-11, 1);
1865
    char str[5];
1866
    TASK_MSD_FlagStrGet(str);
1867
    if( ui_cursor_flash ) SEQ_LCD_PrintSpaces(13); else SEQ_LCD_PrintFormattedString(" [ %s  ] ", str);
1868
  }
596 tk 1869
 
278 tk 1870
  // transfer all changed characters to LCD
353 tk 1871
  // SEQ_LCD_Update provides a MUTEX handling to allow updates from different tasks
1872
  return SEQ_LCD_Update(0);
134 tk 1873
}
1874
 
1875
 
1876
/////////////////////////////////////////////////////////////////////////////
1877
// Update all LEDs
1878
// Usually called from background task
1879
/////////////////////////////////////////////////////////////////////////////
1880
s32 SEQ_UI_LED_Handler(void)
1881
{
626 tk 1882
  static u8 remote_led_sr[SEQ_LED_NUM_SR];
1883
 
524 tk 1884
  int i;
1885
 
625 tk 1886
  // ignore in remote client mode
626 tk 1887
  if( seq_ui_remote_active_mode == SEQ_UI_REMOTE_MODE_CLIENT )
625 tk 1888
    return 0; // no error
1889
 
496 tk 1890
  // ignore so long hardware config hasn't been read
1891
  if( !SEQ_FILE_HW_ConfigLocked() )
1892
    return -1;
1893
 
134 tk 1894
  // track LEDs
484 tk 1895
  u8 selected_tracks = ui_selected_tracks >> (4*ui_selected_group);
492 tk 1896
  SEQ_LED_PinSet(seq_hwcfg_led.track[0], (selected_tracks & (1 << 0)));
1897
  SEQ_LED_PinSet(seq_hwcfg_led.track[1], (selected_tracks & (1 << 1)));
1898
  SEQ_LED_PinSet(seq_hwcfg_led.track[2], (selected_tracks & (1 << 2)));
1899
  SEQ_LED_PinSet(seq_hwcfg_led.track[3], (selected_tracks & (1 << 3)));
678 tk 1900
  SEQ_LED_PinSet(seq_hwcfg_led.track_sel, ui_page == SEQ_UI_PAGE_TRACKSEL);
134 tk 1901
 
1902
  // parameter layer LEDs
492 tk 1903
  SEQ_LED_PinSet(seq_hwcfg_led.par_layer[0], (ui_selected_par_layer == 0));
1904
  SEQ_LED_PinSet(seq_hwcfg_led.par_layer[1], (ui_selected_par_layer == 1));
1905
  SEQ_LED_PinSet(seq_hwcfg_led.par_layer[2], (ui_selected_par_layer >= 2) || seq_ui_button_state.PAR_LAYER_SEL);
678 tk 1906
  SEQ_LED_PinSet(seq_hwcfg_led.par_layer_sel, ui_page == SEQ_UI_PAGE_PARSEL);
134 tk 1907
 
1908
  // group LEDs
492 tk 1909
  SEQ_LED_PinSet(seq_hwcfg_led.group[0], (ui_selected_group == 0));
1910
  SEQ_LED_PinSet(seq_hwcfg_led.group[1], (ui_selected_group == 1));
1911
  SEQ_LED_PinSet(seq_hwcfg_led.group[2], (ui_selected_group == 2));
1912
  SEQ_LED_PinSet(seq_hwcfg_led.group[3], (ui_selected_group == 3));
134 tk 1913
 
1914
  // trigger layer LEDs
492 tk 1915
  SEQ_LED_PinSet(seq_hwcfg_led.trg_layer[0], (ui_selected_trg_layer == 0));
1916
  SEQ_LED_PinSet(seq_hwcfg_led.trg_layer[1], (ui_selected_trg_layer == 1));
1917
  SEQ_LED_PinSet(seq_hwcfg_led.trg_layer[2], (ui_selected_trg_layer >= 2) || seq_ui_button_state.TRG_LAYER_SEL);
678 tk 1918
  SEQ_LED_PinSet(seq_hwcfg_led.trg_layer_sel, ui_page == SEQ_UI_PAGE_TRGSEL);
134 tk 1919
 
1920
  // remaining LEDs
492 tk 1921
  SEQ_LED_PinSet(seq_hwcfg_led.edit, ui_page == SEQ_UI_PAGE_EDIT);
1922
  SEQ_LED_PinSet(seq_hwcfg_led.mute, ui_page == SEQ_UI_PAGE_MUTE);
1923
  SEQ_LED_PinSet(seq_hwcfg_led.pattern, ui_page == SEQ_UI_PAGE_PATTERN);
399 tk 1924
  if( SEQ_SONG_ActiveGet() )
492 tk 1925
    SEQ_LED_PinSet(seq_hwcfg_led.song, 1);
399 tk 1926
  else
492 tk 1927
    SEQ_LED_PinSet(seq_hwcfg_led.song, ui_cursor_flash ? 0 : (ui_page == SEQ_UI_PAGE_SONG));
743 tk 1928
  SEQ_LED_PinSet(seq_hwcfg_led.morph, ui_page == SEQ_UI_PAGE_TRKMORPH);
1929
  SEQ_LED_PinSet(seq_hwcfg_led.mixer, ui_page == SEQ_UI_PAGE_MIXER);
1930
  SEQ_LED_PinSet(seq_hwcfg_led.transpose, ui_page == SEQ_UI_PAGE_TRKTRAN);
134 tk 1931
 
492 tk 1932
  SEQ_LED_PinSet(seq_hwcfg_led.solo, seq_ui_button_state.SOLO);
1933
  SEQ_LED_PinSet(seq_hwcfg_led.fast, seq_ui_button_state.FAST_ENCODERS);
1934
  SEQ_LED_PinSet(seq_hwcfg_led.all, seq_ui_button_state.CHANGE_ALL_STEPS);
134 tk 1935
 
492 tk 1936
  SEQ_LED_PinSet(seq_hwcfg_led.play, SEQ_BPM_IsRunning());
1937
  SEQ_LED_PinSet(seq_hwcfg_led.stop, !SEQ_BPM_IsRunning() && !ui_seq_pause);
1938
  SEQ_LED_PinSet(seq_hwcfg_led.pause, ui_seq_pause);
240 tk 1939
 
492 tk 1940
  SEQ_LED_PinSet(seq_hwcfg_led.rew, seq_ui_button_state.REW);
1941
  SEQ_LED_PinSet(seq_hwcfg_led.fwd, seq_ui_button_state.FWD);
596 tk 1942
 
1943
  SEQ_LED_PinSet(seq_hwcfg_led.loop, seq_core_state.LOOP);
134 tk 1944
 
678 tk 1945
  SEQ_LED_PinSet(seq_hwcfg_led.step_view, ui_page == SEQ_UI_PAGE_STEPSEL);
134 tk 1946
 
743 tk 1947
  SEQ_LED_PinSet(seq_hwcfg_led.exit, ui_page == SEQ_UI_PAGE_MENU);
591 tk 1948
  SEQ_LED_PinSet(seq_hwcfg_led.select, seq_ui_button_state.SELECT_PRESSED);
492 tk 1949
  SEQ_LED_PinSet(seq_hwcfg_led.menu, seq_ui_button_state.MENU_PRESSED);
134 tk 1950
 
596 tk 1951
  // handle double functions
1952
  if( seq_ui_button_state.MENU_PRESSED ) {
1953
    SEQ_LED_PinSet(seq_hwcfg_led.scrub, seq_core_state.LOOP);
1954
    SEQ_LED_PinSet(seq_hwcfg_led.metronome, seq_core_state.EXT_RESTART_REQ);
1955
  } else {
1956
    SEQ_LED_PinSet(seq_hwcfg_led.scrub, seq_ui_button_state.SCRUB);
1957
    SEQ_LED_PinSet(seq_hwcfg_led.metronome, seq_core_state.METRONOME);
1958
  }
1959
 
630 tk 1960
  SEQ_LED_PinSet(seq_hwcfg_led.record, ui_page == SEQ_UI_PAGE_TRKREC);
1961
 
492 tk 1962
  SEQ_LED_PinSet(seq_hwcfg_led.utility, ui_page == SEQ_UI_PAGE_UTIL);
1963
  SEQ_LED_PinSet(seq_hwcfg_led.copy, seq_ui_button_state.COPY);
1964
  SEQ_LED_PinSet(seq_hwcfg_led.paste, seq_ui_button_state.PASTE);
1965
  SEQ_LED_PinSet(seq_hwcfg_led.clear, seq_ui_button_state.CLEAR);
240 tk 1966
 
513 tk 1967
  SEQ_LED_PinSet(seq_hwcfg_led.tap_tempo, seq_ui_button_state.TAP_TEMPO);
678 tk 1968
  SEQ_LED_PinSet(seq_hwcfg_led.tempo_preset, ui_page == SEQ_UI_PAGE_BPM_PRESETS);
524 tk 1969
  SEQ_LED_PinSet(seq_hwcfg_led.ext_restart, seq_core_state.EXT_RESTART_REQ);
240 tk 1970
 
492 tk 1971
  SEQ_LED_PinSet(seq_hwcfg_led.down, seq_ui_button_state.DOWN);
1972
  SEQ_LED_PinSet(seq_hwcfg_led.up, seq_ui_button_state.UP);
240 tk 1973
 
167 tk 1974
 
306 tk 1975
  // in MENU page: overrule GP LEDs so long MENU button is pressed/active
1976
  if( seq_ui_button_state.MENU_PRESSED ) {
1977
    if( ui_cursor_flash ) // if flashing flag active: no LED flag set
1978
      ui_gp_leds = 0x0000;
1979
    else {
1980
      int i;
1981
      u16 new_ui_gp_leds = 0x0000;
1982
      for(i=0; i<16; ++i)
1983
    if( ui_page == ui_shortcut_menu_pages[i] )
1984
      new_ui_gp_leds |= (1 << i);
1985
      ui_gp_leds = new_ui_gp_leds;
1986
    }
1987
  } else {
1988
    // note: the background function is permanently interrupted - therefore we write the GP pattern
1989
    // into a temporary variable, and take it over once completed
1990
    u16 new_ui_gp_leds = 0x0000;
1991
    // request GP LED values from current menu page
1992
    // will be transfered to DOUT registers in SEQ_UI_LED_Handler_Periodic
1993
    new_ui_gp_leds = 0x0000;
206 tk 1994
 
306 tk 1995
    if( ui_led_callback != NULL )
1996
      ui_led_callback(&new_ui_gp_leds);
167 tk 1997
 
306 tk 1998
    ui_gp_leds = new_ui_gp_leds;
1999
  }
2000
 
524 tk 2001
 
2002
  // BLM LEDs
2003
  u8 visible_track0 = 4*ui_selected_group;
2004
  u8 visible_sr0  = 2*ui_selected_step_view;
2005
 
2006
  for(i=0; i<NUM_BLM_LED_SRS; ++i)
2007
    ui_blm_leds[i] = SEQ_TRG_Get8(visible_track0+(i>>1), visible_sr0+(i&1), ui_selected_trg_layer, ui_selected_instrument);
2008
 
626 tk 2009
 
2010
  // send LED changes in remote server mode
2011
  if( seq_ui_remote_mode == SEQ_UI_REMOTE_MODE_SERVER || seq_ui_remote_active_mode == SEQ_UI_REMOTE_MODE_SERVER ) {
2012
    int first_sr = -1;
2013
    int last_sr = -1;
2014
    int sr;
2015
    for(sr=0; sr<SEQ_LED_NUM_SR; ++sr) {
2016
      u8 value = SEQ_LED_SRGet(sr);
2017
      if( value != remote_led_sr[sr] ) {
2018
    if( first_sr == -1 )
2019
      first_sr = sr;
2020
    last_sr = sr;
2021
    remote_led_sr[sr] = value;
2022
      }
2023
    }
2024
 
2025
    MIOS32_IRQ_Disable();
2026
    if( seq_ui_remote_force_led_update ) {
2027
      first_sr = 0;
2028
      last_sr = SEQ_LED_NUM_SR-1;
2029
    }
2030
    seq_ui_remote_force_led_update = 0;
2031
    MIOS32_IRQ_Enable();
2032
 
2033
    if( first_sr >= 0 )
2034
      SEQ_MIDI_SYSEX_REMOTE_Server_SendLED(first_sr, (u8 *)&remote_led_sr[first_sr], last_sr-first_sr+1);
2035
  }
2036
 
2037
 
134 tk 2038
  return 0; // no error
2039
}
2040
 
2041
 
2042
/////////////////////////////////////////////////////////////////////////////
2043
// updates high-prio LED functions (GP LEDs and Beat LED)
168 tk 2044
// called each mS
134 tk 2045
/////////////////////////////////////////////////////////////////////////////
2046
s32 SEQ_UI_LED_Handler_Periodic()
2047
{
625 tk 2048
  // ignore in remote client mode
626 tk 2049
  if( seq_ui_remote_active_mode == SEQ_UI_REMOTE_MODE_CLIENT )
625 tk 2050
    return 0; // no error
2051
 
496 tk 2052
  // ignore so long hardware config hasn't been read
2053
  if( !SEQ_FILE_HW_ConfigLocked() )
2054
    return -1;
2055
 
752 tk 2056
  // GP LEDs are updated when ui_gp_leds has changed
134 tk 2057
  static u16 prev_ui_gp_leds = 0x0000;
2058
 
755 tk 2059
  // beat LED
193 tk 2060
  u8 sequencer_running = SEQ_BPM_IsRunning();
492 tk 2061
  SEQ_LED_PinSet(seq_hwcfg_led.beat, sequencer_running && ((seq_core_state.ref_step & 3) == 0));
134 tk 2062
 
755 tk 2063
 
2064
  // don't continue if no new step has been generated and GP LEDs haven't changed
758 tk 2065
  if( !seq_core_step_update_req && prev_ui_gp_leds == ui_gp_leds && sequencer_running ) // sequencer running check: workaround - so long sequencer not running, we won't get an step update request!
755 tk 2066
    return 0;
2067
  seq_core_step_update_req = 0; // requested from SEQ_CORE if any step has been changed
2068
  prev_ui_gp_leds = ui_gp_leds; // take over new GP pattern
2069
 
134 tk 2070
  // for song position marker (supports 16 LEDs, check for selected step view)
2071
  u16 pos_marker_mask = 0x0000;
325 tk 2072
  u8 visible_track = SEQ_UI_VisibleTrackGet();
2073
  u8 played_step = seq_core_trk[visible_track].step;
754 tk 2074
 
513 tk 2075
  if( seq_ui_button_state.STEP_VIEW ) {
2076
    // if STEP_VIEW button pressed: pos marker correlated to zoom ratio
325 tk 2077
    if( sequencer_running )
2078
      pos_marker_mask = 1 << (played_step / (SEQ_TRG_NumStepsGet(visible_track)/16));
2079
  } else {
2080
    if( sequencer_running && (played_step >> 4) == ui_selected_step_view )
2081
      pos_marker_mask = 1 << (played_step & 0xf);
2082
  }
134 tk 2083
 
752 tk 2084
 
2085
  // follow step position if enabled
2086
  if( seq_core_options.FOLLOW_SONG ) {
2087
    u8 trk_step = seq_core_trk[visible_track].step;
2088
    if( (trk_step & 0xf0) != (16*ui_selected_step_view) ) {
2089
      ui_selected_step_view = trk_step / 16;
2090
      ui_selected_step = (ui_selected_step % 16) + 16*ui_selected_step_view;
2091
      seq_ui_display_update_req = 1;
2092
    }
2093
  }
134 tk 2094
 
2095
  // transfer to GP LEDs
514 tk 2096
  if( seq_hwcfg_led.gp_dout_l_sr ) {
2097
    if( seq_hwcfg_led.gp_dout_l2_sr )
2098
      SEQ_LED_SRSet(seq_hwcfg_led.gp_dout_l_sr-1, (ui_gp_leds >> 0) & 0xff);
496 tk 2099
    else
514 tk 2100
      SEQ_LED_SRSet(seq_hwcfg_led.gp_dout_l_sr-1, ((ui_gp_leds ^ pos_marker_mask) >> 0) & 0xff);
496 tk 2101
  }
134 tk 2102
 
514 tk 2103
  if( seq_hwcfg_led.gp_dout_r_sr ) {
2104
    if( seq_hwcfg_led.gp_dout_r2_sr )
2105
      SEQ_LED_SRSet(seq_hwcfg_led.gp_dout_r_sr-1, (ui_gp_leds >> 8) & 0xff);
496 tk 2106
    else
514 tk 2107
      SEQ_LED_SRSet(seq_hwcfg_led.gp_dout_r_sr-1, ((ui_gp_leds ^ pos_marker_mask) >> 8) & 0xff);
496 tk 2108
  }
134 tk 2109
 
514 tk 2110
  if( seq_hwcfg_led.gp_dout_l2_sr )
2111
    SEQ_LED_SRSet(seq_hwcfg_led.gp_dout_l2_sr-1, (pos_marker_mask >> 0) & 0xff);
2112
  if( seq_hwcfg_led.gp_dout_r2_sr )
2113
    SEQ_LED_SRSet(seq_hwcfg_led.gp_dout_r2_sr-1, (pos_marker_mask >> 8) & 0xff);
496 tk 2114
 
524 tk 2115
 
2116
  if( seq_hwcfg_blm.enabled ) {
2117
    // Red LEDs (position marker)
2118
    int track_ix;
2119
    for(track_ix=0; track_ix<(NUM_BLM_LED_SRS/2); ++track_ix) {
2120
      u16 pos_marker_mask = 0x0000;
2121
      if( sequencer_running ) {
749 tk 2122
    u8 track = 4*ui_selected_group + track_ix;
524 tk 2123
    u8 played_step = seq_core_trk[track].step;
2124
    if( (played_step >> 4) == ui_selected_step_view )
2125
      pos_marker_mask = 1 << (played_step & 0xf);
2126
      }
2127
 
2128
      // Prepare Green LEDs (triggers)
2129
      u8 green_l = ui_blm_leds[2*track_ix+0];
2130
      u8 green_r = ui_blm_leds[2*track_ix+1];
2131
 
2132
      // Red LEDs (position marker)
2133
      if( seq_hwcfg_blm.dout_duocolour ) {
2134
    BLM_DOUT_SRSet(1, 2*track_ix+0, pos_marker_mask & 0xff);
2135
    BLM_DOUT_SRSet(1, 2*track_ix+1, (pos_marker_mask >> 8) & 0xff);
2136
 
2137
    if( seq_hwcfg_blm.dout_duocolour == 2 ) {
2138
      // Colour Mode 2: clear green LED, so that only one LED is lit
2139
      if( pos_marker_mask & 0x00ff )
2140
        green_l &= ~pos_marker_mask;
2141
      else if( pos_marker_mask & 0xff00 )
2142
        green_r &= ~(pos_marker_mask >> 8);
2143
    }
2144
      } else {
2145
    // If Duo-LEDs not enabled: invert Green LEDs
2146
    if( pos_marker_mask & 0x00ff )
2147
      green_l ^= pos_marker_mask;
2148
    else if( pos_marker_mask & 0xff00 )
2149
      green_r ^= (pos_marker_mask >> 8);
2150
      }
2151
 
2152
      // Set Green LEDs
2153
      BLM_DOUT_SRSet(0, 2*track_ix+0, green_l);
2154
      BLM_DOUT_SRSet(0, 2*track_ix+1, green_r);
2155
    }
2156
  }
2157
 
2158
 
514 tk 2159
  if( seq_hwcfg_blm8x8.enabled && seq_hwcfg_blm8x8.dout_gp_mapping ) {
513 tk 2160
    // for wilba's frontpanel
134 tk 2161
 
513 tk 2162
    // BLM_X DOUT -> GP LED mapping
2163
    // 0 = 15,16    1 = 13,14   2 = 11,12   3 = 9,10
2164
    // 4 = 1,2  5 = 3,4     6 = 5,6     7 = 7,8
134 tk 2165
 
513 tk 2166
    // bit 7: first green (i.e. GP1-G)
2167
    // bit 6: first red (i.e. GP1-R)
2168
    // bit 5: second green (i.e. GP2-G)
2169
    // bit 4: second red (i.e. GP2-R)
134 tk 2170
 
513 tk 2171
    // this mapping routine takes ca. 5 uS
2172
    // since it's only executed when ui_gp_leds or gp_mask has changed, it doesn't really hurt
134 tk 2173
 
513 tk 2174
    u16 modified_gp_leds = ui_gp_leds;
134 tk 2175
#if 1
513 tk 2176
    // extra: red LED is lit exclusively for higher contrast
2177
    modified_gp_leds &= ~pos_marker_mask;
134 tk 2178
#endif
2179
 
513 tk 2180
    int sr;
2181
    const u8 blm_x_sr_map[8] = {4, 5, 6, 7, 3, 2, 1, 0};
2182
    u16 gp_mask = 1 << 0;
2183
    for(sr=0; sr<8; ++sr) {
2184
      u8 pattern = 0;
134 tk 2185
 
513 tk 2186
      if( modified_gp_leds & gp_mask )
2187
    pattern |= 0x80;
2188
      if( pos_marker_mask & gp_mask )
2189
    pattern |= 0x40;
2190
      gp_mask <<= 1;
2191
      if( modified_gp_leds & gp_mask )
2192
    pattern |= 0x20;
2193
      if( pos_marker_mask & gp_mask )
2194
    pattern |= 0x10;
2195
      gp_mask <<= 1;
134 tk 2196
 
513 tk 2197
      u8 mapped_sr = blm_x_sr_map[sr];
2198
      BLM_X_LED_rows[mapped_sr][0] = (BLM_X_LED_rows[mapped_sr][0] & 0x0f) | pattern;
2199
    }
134 tk 2200
  }
2201
 
184 tk 2202
  return 0; // no error
134 tk 2203
}
2204
 
2205
 
2206
/////////////////////////////////////////////////////////////////////////////
168 tk 2207
// for menu handling (e.g. flashing cursor, doubleclick counter, etc...)
2208
// called each mS
2209
/////////////////////////////////////////////////////////////////////////////
2210
s32 SEQ_UI_MENU_Handler_Periodic()
2211
{
625 tk 2212
  // ignore in remote client mode
626 tk 2213
  if( seq_ui_remote_active_mode == SEQ_UI_REMOTE_MODE_CLIENT )
625 tk 2214
    return 0; // no error
2215
 
740 tk 2216
  if( ++ui_cursor_flash_ctr >= SEQ_UI_CURSOR_FLASH_CTR_MAX ) {
168 tk 2217
    ui_cursor_flash_ctr = 0;
740 tk 2218
    ++ui_cursor_flash_overrun_ctr;
2219
    seq_ui_display_update_req = 1;
2220
  }
2221
 
173 tk 2222
  // important: flash flag has to be recalculated on each invocation of this
2223
  // handler, since counter could also be reseted outside this function
652 tk 2224
  u8 old_ui_cursor_flash = ui_cursor_flash;
173 tk 2225
  ui_cursor_flash = ui_cursor_flash_ctr >= SEQ_UI_CURSOR_FLASH_CTR_LED_OFF;
652 tk 2226
  if( old_ui_cursor_flash != ui_cursor_flash )
2227
    seq_ui_display_update_req = 1;
168 tk 2228
 
240 tk 2229
  // used in some pages for temporary messages
416 tk 2230
  if( ui_hold_msg_ctr ) {
240 tk 2231
    --ui_hold_msg_ctr;
2232
 
416 tk 2233
    if( !ui_hold_msg_ctr )
2234
      seq_ui_display_update_req = 1;
2235
  }
2236
 
596 tk 2237
  // used for temporary messages
2238
  if( ui_msg_ctr )
2239
    --ui_msg_ctr;
299 tk 2240
 
184 tk 2241
  // VU meters (used in MUTE menu, could also be available as LED matrix...)
2242
  static u8 vu_meter_prediv = 0; // predivider for VU meters
2243
 
2244
  if( ++vu_meter_prediv >= 4 ) {
2245
    vu_meter_prediv = 0;
2246
 
333 tk 2247
 
2248
    portENTER_CRITICAL();
2249
 
184 tk 2250
    u8 track;
2251
    seq_core_trk_t *t = &seq_core_trk[0];
2252
    for(track=0; track<SEQ_CORE_NUM_TRACKS; ++t, ++track)
2253
      if( t->vu_meter )
2254
    --t->vu_meter;
333 tk 2255
 
2256
    int i;
2257
    u8 *vu_meter = (u8 *)&seq_layer_vu_meter[0];
2258
    for(i=0; i<sizeof(seq_layer_vu_meter); ++i, ++vu_meter) {
2259
      if( *vu_meter && !(*vu_meter & 0x80) ) // if bit 7 set: static value
2260
    *vu_meter -= 1;
2261
    }
2262
 
318 tk 2263
    portEXIT_CRITICAL();
184 tk 2264
  }
2265
 
740 tk 2266
  // delayed action will be triggered once counter reached 0
2267
  if( !ui_delayed_action_callback )
2268
    ui_delayed_action_ctr = 0;
2269
  else if( ui_delayed_action_ctr ) {
2270
    if( --ui_delayed_action_ctr == 0 ) {
752 tk 2271
      // must be atomic
2272
      MIOS32_IRQ_Disable();
2273
      s32 (*_ui_delayed_action_callback)(u32 parameter);
2274
      _ui_delayed_action_callback = ui_delayed_action_callback;
2275
      u32 parameter = ui_delayed_action_parameter;
740 tk 2276
      ui_delayed_action_callback = NULL;
752 tk 2277
      MIOS32_IRQ_Enable();
2278
      _ui_delayed_action_callback(parameter); // note: it's allowed that the delayed action generates a new delayed action
740 tk 2279
    }
2280
  }
2281
 
168 tk 2282
  return 0;
2283
}
2284
 
2285
 
2286
/////////////////////////////////////////////////////////////////////////////
328 tk 2287
// Should be regulary called to check if the layer/instrument/step selection
2288
// is valid for the current track
2289
// At least executed before button/encoder and LCD function calls
2290
/////////////////////////////////////////////////////////////////////////////
2291
s32 SEQ_UI_CheckSelections(void)
2292
{
492 tk 2293
  if( (ui_selected_tracks >> (4*ui_selected_group)) == 0 )
2294
    ui_selected_tracks = 1 << (4*ui_selected_group);
2295
 
328 tk 2296
  u8 visible_track = SEQ_UI_VisibleTrackGet();
2297
 
2298
  if( ui_selected_instrument >= SEQ_PAR_NumInstrumentsGet(visible_track) )
2299
    ui_selected_instrument = 0;
2300
 
2301
  if( ui_selected_par_layer >= SEQ_PAR_NumLayersGet(visible_track) )
2302
    ui_selected_par_layer = 0;
2303
 
2304
  if( ui_selected_trg_layer >= SEQ_TRG_NumLayersGet(visible_track) )
2305
    ui_selected_trg_layer = 0;
2306
 
2307
  if( ui_selected_step >= SEQ_TRG_NumStepsGet(visible_track) )
2308
    ui_selected_step = 0;
2309
 
2310
  if( ui_selected_step_view >= (SEQ_TRG_NumStepsGet(visible_track)/16) ) {
2311
    ui_selected_step_view = 0;
2312
    ui_selected_step %= 16;
2313
  }
762 tk 2314
 
336 tk 2315
  if( ui_selected_step < (16*ui_selected_step_view) ||
2316
      ui_selected_step >= (16*(ui_selected_step_view+1)) )
328 tk 2317
    ui_selected_step_view = ui_selected_step / 16;
2318
 
2319
  return 0; // no error
2320
}
2321
 
2322
 
2323
/////////////////////////////////////////////////////////////////////////////
134 tk 2324
// Returns the currently visible track
2325
/////////////////////////////////////////////////////////////////////////////
2326
u8 SEQ_UI_VisibleTrackGet(void)
2327
{
2328
  u8 offset = 0;
2329
 
484 tk 2330
  u8 selected_tracks = ui_selected_tracks >> (4*ui_selected_group);
2331
  if( selected_tracks & (1 << 3) )
134 tk 2332
    offset = 3;
484 tk 2333
  if( selected_tracks & (1 << 2) )
134 tk 2334
    offset = 2;
484 tk 2335
  if( selected_tracks & (1 << 1) )
134 tk 2336
    offset = 1;
484 tk 2337
  if( selected_tracks & (1 << 0) )
134 tk 2338
    offset = 0;
2339
 
2340
  return 4*ui_selected_group + offset;
2341
}
2342
 
168 tk 2343
 
2344
/////////////////////////////////////////////////////////////////////////////
178 tk 2345
// Returns 1 if 'track' is selected
2346
/////////////////////////////////////////////////////////////////////////////
2347
s32 SEQ_UI_IsSelectedTrack(u8 track)
2348
{
484 tk 2349
  return (ui_selected_tracks & (1 << track)) ? 1 : 0;
178 tk 2350
}
2351
 
2352
 
2353
/////////////////////////////////////////////////////////////////////////////
240 tk 2354
// Sets a new selected step and updates the step view
2355
/////////////////////////////////////////////////////////////////////////////
2356
s32 SEQ_UI_SelectedStepSet(u8 step)
2357
{
2358
  ui_selected_step = step;
328 tk 2359
  SEQ_UI_CheckSelections();
2360
 
240 tk 2361
  return 0; // no error
2362
}
2363
 
2364
 
2365
/////////////////////////////////////////////////////////////////////////////
168 tk 2366
// Increments the selected tracks/groups
2367
// OUT: 1 if value has been changed, otherwise 0
2368
/////////////////////////////////////////////////////////////////////////////
2369
s32 SEQ_UI_GxTyInc(s32 incrementer)
2370
{
2371
  int gxty = SEQ_UI_VisibleTrackGet();
2372
  int prev_gxty = gxty;
2373
 
2374
  if( incrementer >= 0 ) {
2375
    if( (gxty += incrementer) >= SEQ_CORE_NUM_TRACKS )
2376
      gxty = SEQ_CORE_NUM_TRACKS-1;
2377
  } else {
2378
    if( (gxty += incrementer) < 0 )
2379
      gxty = 0;
2380
  }
2381
 
2382
  if( gxty == prev_gxty )
2383
    return 0; // no change
2384
 
484 tk 2385
  ui_selected_tracks = 1 << gxty;
168 tk 2386
  ui_selected_group = gxty / 4;
2387
 
2388
  return 1; // value changed
2389
}
2390
 
2391
 
2392
/////////////////////////////////////////////////////////////////////////////
236 tk 2393
// Increments a 16bit variable within given min/max range
2394
// OUT: 1 if value has been changed, otherwise 0
2395
/////////////////////////////////////////////////////////////////////////////
240 tk 2396
s32 SEQ_UI_Var16_Inc(u16 *value, u16 min, u16 max, s32 incrementer)
236 tk 2397
{
2398
  int new_value = *value;
2399
  int prev_value = new_value;
2400
 
2401
  if( incrementer >= 0 ) {
2402
    if( (new_value += incrementer) >= max )
2403
      new_value = max;
2404
  } else {
2405
    if( (new_value += incrementer) < min )
2406
      new_value = min;
2407
  }
2408
 
2409
  if( new_value == prev_value )
2410
    return 0; // no change
2411
 
2412
  *value = new_value;
2413
 
2414
  return 1; // value changed
2415
}
2416
 
240 tk 2417
/////////////////////////////////////////////////////////////////////////////
2418
// Increments an 8bit variable within given min/max range
2419
// OUT: 1 if value has been changed, otherwise 0
2420
/////////////////////////////////////////////////////////////////////////////
2421
s32 SEQ_UI_Var8_Inc(u8 *value, u16 min, u16 max, s32 incrementer)
2422
{
2423
  u16 tmp = *value;
2424
  if( SEQ_UI_Var16_Inc(&tmp, min, max, incrementer) ) {
2425
    *value = tmp;
2426
    return 1; // value changed
2427
  }
236 tk 2428
 
240 tk 2429
  return 0; // value hasn't been changed
2430
}
2431
 
2432
 
236 tk 2433
/////////////////////////////////////////////////////////////////////////////
168 tk 2434
// Increments a CC within given min/max range
2435
// OUT: 1 if value has been changed, otherwise 0
2436
/////////////////////////////////////////////////////////////////////////////
248 tk 2437
s32 SEQ_UI_CC_Inc(u8 cc, u8 min, u8 max, s32 incrementer)
168 tk 2438
{
2439
  u8 visible_track = SEQ_UI_VisibleTrackGet();
173 tk 2440
  int new_value = SEQ_CC_Get(visible_track, cc);
2441
  int prev_value = new_value;
168 tk 2442
 
2443
  if( incrementer >= 0 ) {
173 tk 2444
    if( (new_value += incrementer) >= max )
2445
      new_value = max;
168 tk 2446
  } else {
173 tk 2447
    if( (new_value += incrementer) < min )
2448
      new_value = min;
168 tk 2449
  }
2450
 
173 tk 2451
  if( new_value == prev_value )
168 tk 2452
    return 0; // no change
2453
 
173 tk 2454
  SEQ_CC_Set(visible_track, cc, new_value);
168 tk 2455
 
179 tk 2456
  // set same value for all selected tracks
2457
  u8 track;
2458
  for(track=0; track<SEQ_CORE_NUM_TRACKS; ++track)
2459
    if( track != visible_track && SEQ_UI_IsSelectedTrack(track) )
2460
      SEQ_CC_Set(track, cc, new_value);
2461
 
168 tk 2462
  return 1; // value changed
2463
}
2464
 
173 tk 2465
 
2466
/////////////////////////////////////////////////////////////////////////////
179 tk 2467
// Sets a CC value on all selected tracks
2468
// OUT: 1 if value has been changed, otherwise 0
2469
/////////////////////////////////////////////////////////////////////////////
248 tk 2470
s32 SEQ_UI_CC_Set(u8 cc, u8 value)
179 tk 2471
{
2472
  u8 visible_track = SEQ_UI_VisibleTrackGet();
2473
  int prev_value = SEQ_CC_Get(visible_track, cc);
2474
 
2475
  if( value == prev_value )
2476
    return 0; // no change
2477
 
2478
  SEQ_CC_Set(visible_track, cc, value);
2479
 
2480
  // set same value for all selected tracks
2481
  u8 track;
2482
  for(track=0; track<SEQ_CORE_NUM_TRACKS; ++track)
2483
    if( track != visible_track && SEQ_UI_IsSelectedTrack(track) )
2484
      SEQ_CC_Set(track, cc, value);
2485
 
2486
  return 1; // value changed
2487
}
2488
 
2489
/////////////////////////////////////////////////////////////////////////////
173 tk 2490
// Modifies a bitfield in a CC value to a given value
2491
// OUT: 1 if value has been changed, otherwise 0
2492
/////////////////////////////////////////////////////////////////////////////
248 tk 2493
s32 SEQ_UI_CC_SetFlags(u8 cc, u8 flag_mask, u8 value)
173 tk 2494
{
2495
  u8 visible_track = SEQ_UI_VisibleTrackGet();
2496
  int new_value = SEQ_CC_Get(visible_track, cc);
2497
  int prev_value = new_value;
2498
 
2499
  new_value = (new_value & ~flag_mask) | value;
2500
 
2501
  if( new_value == prev_value )
2502
    return 0; // no change
2503
 
2504
  SEQ_CC_Set(visible_track, cc, new_value);
2505
 
179 tk 2506
  // do same modification for all selected tracks
2507
  u8 track;
2508
  for(track=0; track<SEQ_CORE_NUM_TRACKS; ++track)
2509
    if( track != visible_track && SEQ_UI_IsSelectedTrack(track) ) {
2510
      int new_value = SEQ_CC_Get(track, cc);
2511
      new_value = (new_value & ~flag_mask) | value;
2512
      SEQ_CC_Set(track, cc, new_value);
2513
    }
2514
 
173 tk 2515
  return 1; // value changed
2516
}
2517
 
2518
 
299 tk 2519
/////////////////////////////////////////////////////////////////////////////
596 tk 2520
// Print temporary user messages (e.g. warnings, errors)
299 tk 2521
// expects mS delay and two lines, each up to 20 characters
2522
/////////////////////////////////////////////////////////////////////////////
596 tk 2523
s32 SEQ_UI_Msg(seq_ui_msg_type_t msg_type, u16 delay, char *line1, char *line2)
299 tk 2524
{
596 tk 2525
  ui_msg_type = msg_type;
2526
  ui_msg_ctr = delay;
2527
  strncpy((char *)ui_msg[0], line1, UI_MSG_MAX_CHAR);
2528
  strncpy((char *)ui_msg[1], line2, UI_MSG_MAX_CHAR);
299 tk 2529
 
2530
  return 0; // no error
2531
}
2532
 
2533
/////////////////////////////////////////////////////////////////////////////
596 tk 2534
// Stops temporary message if no SD card warning
2535
/////////////////////////////////////////////////////////////////////////////
2536
s32 SEQ_UI_MsgStop(void)
2537
{
2538
  if( ui_msg_type != SEQ_UI_MSG_SDCARD )
2539
    ui_msg_ctr = 0;
2540
 
2541
  return 0; // no error
2542
}
2543
 
2544
/////////////////////////////////////////////////////////////////////////////
299 tk 2545
// Prints a temporary error messages after file operation
2546
// Expects error status number (as defined in seq_file.h)
2547
/////////////////////////////////////////////////////////////////////////////
2548
s32 SEQ_UI_SDCardErrMsg(u16 delay, s32 status)
2549
{
2550
  // TODO: add more verbose error messages, they are clearly defined in seq_file.h)
2551
  char str[21];
2552
  sprintf(str, "E%3d (DOSFS: D%3d)", -status, seq_file_dfs_errno < 1000 ? seq_file_dfs_errno : 999);
596 tk 2553
  return SEQ_UI_Msg(SEQ_UI_MSG_SDCARD, delay, "!! SD Card Error !!!", str);
299 tk 2554
}
744 tk 2555
 
2556
 
2557
/////////////////////////////////////////////////////////////////////////////
2558
// Help function to browse through a list (e.g. directory)
2559
// incrementer: forwarded from encoder handler
2560
// num_items: number of items in list
2561
// max_items_on_screen: how many items are displayed on screen?
2562
// *selected_item_on_screen: selected item on screen
2563
// *view_offset: pointer to view offset variable
2564
//
2565
// Returns 1 if list has to be updated due to new offset
2566
// Returns 0 if no update required
2567
/////////////////////////////////////////////////////////////////////////////
2568
s32 SEQ_UI_SelectListItem(s32 incrementer, u8 num_items, u8 max_items_on_screen, u8 *selected_item_on_screen, u8 *view_offset)
2569
{
2570
  u8 prev_view_offset = *view_offset;
2571
  int prev_cursor = *view_offset + *selected_item_on_screen;
2572
  int new_cursor = prev_cursor + incrementer;
2573
 
2574
  if( incrementer > 0 ) {
2575
    if( new_cursor >= num_items ) {
2576
#if 0
2577
      // with overrun
2578
      *selected_item_on_screen = 0;
2579
      *view_offset = 0;
2580
#else
2581
      // no overrun
2582
      if( num_items > max_items_on_screen ) {
2583
    *view_offset = num_items - max_items_on_screen;
2584
    *selected_item_on_screen = max_items_on_screen - 1;
2585
      } else {
2586
    *view_offset = 0;
2587
    *selected_item_on_screen = num_items - 1;
2588
      }
2589
#endif
2590
    } else if( (new_cursor - *view_offset) >= max_items_on_screen ) {
2591
      *selected_item_on_screen = max_items_on_screen - 1;
2592
      *view_offset = new_cursor - *selected_item_on_screen;
2593
    } else {
2594
      *selected_item_on_screen = new_cursor - *view_offset;
2595
    }
2596
  } else if( incrementer < 0 ) {
2597
    if( new_cursor < 0 ) {
2598
#if 0
2599
      // with overrun
2600
      *selected_item_on_screen = max_items_on_screen - 1;
2601
      if( *selected_item_on_screen >= (num_items-1) ) {
2602
    *selected_item_on_screen = num_items - 1;
2603
    *view_offset = 0;
2604
      } else {
2605
    *view_offset = num_items - max_items_on_screen - 1;
2606
      }
2607
#else
2608
      // without overrun
2609
      *selected_item_on_screen = 0;
2610
      *view_offset = 0;
2611
#endif
2612
    } else if( new_cursor < *view_offset ) {
2613
      *selected_item_on_screen = 0;
2614
      *view_offset = new_cursor;
2615
    } else {
2616
      *selected_item_on_screen = new_cursor - *view_offset;
2617
    }
2618
  }
2619
 
2620
  return prev_view_offset != *view_offset;
2621
}
752 tk 2622
 
2623
 
2624
/////////////////////////////////////////////////////////////////////////////
2625
// Help functions for the "keypad" editor to edit names
2626
/////////////////////////////////////////////////////////////////////////////
2627
 
2628
static const char ui_keypad_charsets_upper[10][6] = {
2629
  ".,!1~",
2630
  "ABC2~",
2631
  "DEF3~",
2632
  "GHI4~",
2633
  "JKL5~",
2634
  "MNO6~",
2635
  "PQRS7",
2636
  "TUV8~",
2637
  "WXYZ9",
759 tk 2638
  "-_ 0~",
752 tk 2639
};
2640
 
2641
 
2642
static const char ui_keypad_charsets_lower[10][6] = {
2643
  ".,!1~",
2644
  "abc2~",
2645
  "def3~",
2646
  "ghi4~",
2647
  "jkl5~",
2648
  "mno6~",
2649
  "pqrs7",
2650
  "tuv8~",
2651
  "wxyz9",
759 tk 2652
  "-_ 0~",
752 tk 2653
};
2654
 
2655
static u8 ui_keypad_select_charset_lower;
2656
 
2657
s32 SEQ_UI_KeyPad_Init(void)
2658
{
2659
  ui_keypad_select_charset_lower = 0;
2660
  ui_edit_name_cursor = 0;
2661
}
2662
 
2663
// called by delayed action (after 0.75 second) to increment cursor after keypad entry
2664
static s32 SEQ_UI_KeyPad_IncCursor(u32 len)
2665
{
2666
  if( ++ui_edit_name_cursor >= len )
2667
    ui_edit_name_cursor = len - 1;
2668
 
753 tk 2669
  ui_keypad_select_charset_lower = 1;
2670
 
752 tk 2671
  ui_cursor_flash_ctr = ui_cursor_flash_overrun_ctr = 0;
2672
}
2673
 
2674
// handles the 16 GP buttons/encoders
2675
s32 SEQ_UI_KeyPad_Handler(seq_ui_encoder_t encoder, s32 incrementer, char *edit_str, u8 len)
2676
{
2677
  char *edit_char = (char *)&edit_str[ui_edit_name_cursor];
2678
 
2679
  if( encoder <= SEQ_UI_ENCODER_GP10 ) {
2680
    char *charset = ui_keypad_select_charset_lower
2681
      ? (char *)&ui_keypad_charsets_lower[encoder]
2682
      : (char *)&ui_keypad_charsets_upper[encoder];
2683
 
2684
    int pos;
2685
    if( incrementer >= 0 ) {
2686
      for(pos=0; pos<5; ++pos) {
2687
    if( *edit_char == charset[pos] ) {
2688
      ++pos;
2689
      break;
2690
    }
2691
      }
2692