Subversion Repositories svn.mios32

Rev

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