Subversion Repositories svn.mios32

Rev

Rev 1919 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
687 tk 1
// $Id: app.c 2425 2016-11-03 00:44:22Z tk $
2
/*
3
 * MIOS32 Tutorial #017: A simple Sequencer
4
 *
5
 * ==========================================================================
6
 *
7
 *  Copyright (C) 2009 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
 
18
#include <mios32.h>
19
 
20
#include <FreeRTOS.h>
21
#include <portmacro.h>
22
#include <task.h>
23
 
24
#include <notestack.h>
25
#include <seq_bpm.h>
26
#include <seq_midi_out.h>
27
 
28
#include "seq.h"
29
#include "app.h"
30
 
31
 
32
/////////////////////////////////////////////////////////////////////////////
33
// Local definitions
34
/////////////////////////////////////////////////////////////////////////////
35
 
36
#define NOTESTACK_SIZE 16
37
 
38
 
39
/////////////////////////////////////////////////////////////////////////////
40
// Local definitions
41
/////////////////////////////////////////////////////////////////////////////
42
 
693 tk 43
#define PRIORITY_TASK_SEQ       ( tskIDLE_PRIORITY + 4 ) // higher priority than MIDI receive task!
687 tk 44
 
45
 
46
/////////////////////////////////////////////////////////////////////////////
47
// Local Prototypes
48
/////////////////////////////////////////////////////////////////////////////
49
static void TASK_SEQ(void *pvParameters);
50
static s32 NOTIFY_MIDI_Rx(mios32_midi_port_t port, u8 byte);
51
 
52
 
53
/////////////////////////////////////////////////////////////////////////////
54
// This hook is called after startup to initialize the application
55
/////////////////////////////////////////////////////////////////////////////
56
void APP_Init(void)
57
{
58
  // initialize all LEDs
59
  MIOS32_BOARD_LED_Init(0xffffffff);
60
 
61
  // turn off gate LED
62
  MIOS32_BOARD_LED_Set(1, 0);
63
 
64
  // initialize MIDI handler
65
  SEQ_MIDI_OUT_Init(0);
66
 
67
  // initialize sequencer
68
  SEQ_Init(0);
69
 
70
  // install MIDI Rx callback function
71
  MIOS32_MIDI_DirectRxCallback_Init(NOTIFY_MIDI_Rx);
72
 
73
  // install sequencer task
2425 tk 74
  xTaskCreate(TASK_SEQ, "SEQ", configMINIMAL_STACK_SIZE, NULL, PRIORITY_TASK_SEQ, NULL);
687 tk 75
}
76
 
77
 
78
/////////////////////////////////////////////////////////////////////////////
79
// This task is running endless in background
80
/////////////////////////////////////////////////////////////////////////////
81
void APP_Background(void)
82
{
1919 tk 83
}
84
 
85
 
86
/////////////////////////////////////////////////////////////////////////////
87
// This hook is called each mS from the main task which also handles DIN, ENC
88
// and AIN events. You could add more jobs here, but they shouldn't consume
89
// more than 300 uS to ensure the responsiveness of buttons, encoders, pots.
90
// Alternatively you could create a dedicated task for application specific
91
// jobs as explained in $MIOS32_PATH/apps/tutorials/006_rtos_tasks
92
/////////////////////////////////////////////////////////////////////////////
93
void APP_Tick(void)
94
{
687 tk 95
  // set LED depending on sequencer run state
96
  MIOS32_BOARD_LED_Set(1, SEQ_BPM_IsRunning() ? 1 : 0);
97
}
98
 
99
 
100
/////////////////////////////////////////////////////////////////////////////
1919 tk 101
// This hook is called each mS from the MIDI task which checks for incoming
102
// MIDI events. You could add more MIDI related jobs here, but they shouldn't
103
// consume more than 300 uS to ensure the responsiveness of incoming MIDI.
104
/////////////////////////////////////////////////////////////////////////////
105
void APP_MIDI_Tick(void)
106
{
107
}
108
 
109
 
110
/////////////////////////////////////////////////////////////////////////////
687 tk 111
// This hook is called when a MIDI package has been received
112
/////////////////////////////////////////////////////////////////////////////
113
void APP_MIDI_NotifyPackage(mios32_midi_port_t port, mios32_midi_package_t midi_package)
114
{
115
  // Note On received?
116
  if( midi_package.chn == Chn1 &&
117
      (midi_package.type == NoteOn || midi_package.type == NoteOff) ) {
118
 
119
    // branch depending on Note On/Off event
120
    if( midi_package.event == NoteOn && midi_package.velocity > 0 )
121
      SEQ_NotifyNoteOn(midi_package.note, midi_package.velocity);
122
    else
123
      SEQ_NotifyNoteOff(midi_package.note);
124
  }
125
}
126
 
127
 
128
/////////////////////////////////////////////////////////////////////////////
129
// This hook is called before the shift register chain is scanned
130
/////////////////////////////////////////////////////////////////////////////
131
void APP_SRIO_ServicePrepare(void)
132
{
133
}
134
 
135
 
136
/////////////////////////////////////////////////////////////////////////////
137
// This hook is called after the shift register chain has been scanned
138
/////////////////////////////////////////////////////////////////////////////
139
void APP_SRIO_ServiceFinish(void)
140
{
141
}
142
 
143
 
144
/////////////////////////////////////////////////////////////////////////////
145
// This hook is called when a button has been toggled
146
// pin_value is 1 when button released, and 0 when button pressed
147
/////////////////////////////////////////////////////////////////////////////
148
void APP_DIN_NotifyToggle(u32 pin, u32 pin_value)
149
{
150
}
151
 
152
 
153
/////////////////////////////////////////////////////////////////////////////
154
// This hook is called when an encoder has been moved
155
// incrementer is positive when encoder has been turned clockwise, else
156
// it is negative
157
/////////////////////////////////////////////////////////////////////////////
158
void APP_ENC_NotifyChange(u32 encoder, s32 incrementer)
159
{
160
}
161
 
162
 
163
/////////////////////////////////////////////////////////////////////////////
164
// This hook is called when a pot has been moved
165
/////////////////////////////////////////////////////////////////////////////
166
void APP_AIN_NotifyChange(u32 pin, u32 pin_value)
167
{
168
}
169
 
170
 
171
/////////////////////////////////////////////////////////////////////////////
172
// This task is called periodically each mS to handle sequencer requests
173
/////////////////////////////////////////////////////////////////////////////
174
static void TASK_SEQ(void *pvParameters)
175
{
176
  portTickType xLastExecutionTime;
177
 
178
  // Initialise the xLastExecutionTime variable on task entry
179
  xLastExecutionTime = xTaskGetTickCount();
180
 
181
  while( 1 ) {
182
    vTaskDelayUntil(&xLastExecutionTime, 1 / portTICK_RATE_MS);
183
 
184
    // execute sequencer handler
185
    SEQ_Handler();
186
 
187
    // send timestamped MIDI events
188
    SEQ_MIDI_OUT_Handler();
189
  }
190
}
191
 
192
/////////////////////////////////////////////////////////////////////////////
193
// Installed via MIOS32_MIDI_DirectRxCallback_Init
194
/////////////////////////////////////////////////////////////////////////////
195
static s32 NOTIFY_MIDI_Rx(mios32_midi_port_t port, u8 midi_byte)
196
{
197
  // here we could filter a certain port
198
  // The BPM generator will deliver inaccurate results if MIDI clock 
199
  // is received from multiple ports
200
  SEQ_BPM_NotifyMIDIRx(midi_byte);
201
 
202
  return 0; // no error, no filtering
203
}