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