Subversion Repositories svn.mios32

Rev

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

Rev Author Line No. Line
525 tk 1
// $Id: app.c 2425 2016-11-03 00:44:22Z tk $
2
/*
3
 * Checks the handling of a button/LED matrix
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
 
18
#include <mios32.h>
19
#include "app.h"
20
 
21
#include <blm.h>
22
 
23
#include <FreeRTOS.h>
24
#include <task.h>
25
#include <queue.h>
26
 
27
 
28
/////////////////////////////////////////////////////////////////////////////
29
// for optional debugging messages
30
/////////////////////////////////////////////////////////////////////////////
31
 
32
#define DEBUG_VERBOSE_LEVEL 2
33
#define DEBUG_MSG MIOS32_MIDI_SendDebugMessage
34
 
35
 
36
/////////////////////////////////////////////////////////////////////////////
37
// Local definitions
38
/////////////////////////////////////////////////////////////////////////////
39
 
40
#define PRIORITY_TASK_BLM_CHECK     ( tskIDLE_PRIORITY + 2 )
41
 
42
#define BLM_MIDI_STARTNOTE 24
43
 
44
 
45
/////////////////////////////////////////////////////////////////////////////
46
// Prototypes
47
/////////////////////////////////////////////////////////////////////////////
48
static void TASK_BLM_Check(void *pvParameters);
49
 
50
 
51
/////////////////////////////////////////////////////////////////////////////
52
// Global Variables
53
/////////////////////////////////////////////////////////////////////////////
54
 
55
u8 last_din_pin = 0;
56
u8 last_din_value = 1;
57
u8 last_dout_pin = 0;
58
u8 last_dout_value = 1;
59
 
60
 
61
/////////////////////////////////////////////////////////////////////////////
62
// This hook is called after startup to initialize the application
63
/////////////////////////////////////////////////////////////////////////////
64
void APP_Init(void)
65
{
66
  // initialize all LEDs
67
  MIOS32_BOARD_LED_Init(0xffffffff);
68
 
69
  // initialize BLM driver
70
  BLM_Init(0);
71
 
72
  // Debounce delay can be changed here
73
  blm_config_t config = BLM_ConfigGet();
74
  config.debounce_delay = 0;
75
  BLM_ConfigSet(config);
76
 
77
  // start BLM check task
2425 tk 78
  xTaskCreate(TASK_BLM_Check, "BLM_Check", configMINIMAL_STACK_SIZE, NULL, PRIORITY_TASK_BLM_CHECK, NULL);
525 tk 79
 
80
  // send welcome message to MIOS terminal
81
#if DEBUG_VERBOSE_LEVEL >= 1
82
  // print welcome message on MIOS terminal
83
  DEBUG_MSG("\n");
84
  DEBUG_MSG("====================\n");
85
  DEBUG_MSG("%s\n", MIOS32_LCD_BOOT_MSG_LINE1);
86
  DEBUG_MSG("====================\n");
87
  DEBUG_MSG("\n");
88
  DEBUG_MSG("Debounce Delay: %d\n", config.debounce_delay);
89
  DEBUG_MSG("\n");
90
  DEBUG_MSG("Play MIDI Notes over Channel #1 with different velocities\n");
91
  DEBUG_MSG("or press BLM buttons to control the LEDs.\n");
92
  DEBUG_MSG("\n");
93
#endif
94
 
95
}
96
 
97
 
98
/////////////////////////////////////////////////////////////////////////////
99
// This task is running endless in background
100
/////////////////////////////////////////////////////////////////////////////
101
void APP_Background(void)
102
{
103
  // init LCD
104
  MIOS32_LCD_Clear();
105
 
106
  // endless loop: print status information on LCD
107
  while( 1 ) {
108
    // toggle the state of all LEDs (allows to measure the execution speed with a scope)
109
    MIOS32_BOARD_LED_Set(0xffffffff, ~MIOS32_BOARD_LED_Get());
110
 
111
    // print text on LCD screen
112
    MIOS32_LCD_CursorSet(0, 0);
113
    MIOS32_LCD_PrintFormattedString("DIN  Pin #%3d %c", last_din_pin, last_din_value ? 'o' : '*');
114
 
115
    // print text on LCD screen
116
    MIOS32_LCD_CursorSet(0, 1);
117
    MIOS32_LCD_PrintFormattedString("DOUT Pin #%3d %c", last_dout_pin, last_dout_value ? 'o' : '*');
118
  }
119
}
120
 
121
 
122
/////////////////////////////////////////////////////////////////////////////
674 tk 123
// This hook is called when a MIDI package has been received
525 tk 124
/////////////////////////////////////////////////////////////////////////////
674 tk 125
void APP_MIDI_NotifyPackage(mios32_midi_port_t port, mios32_midi_package_t midi_package)
525 tk 126
{
127
  unsigned char pin, pin_state;
128
 
129
#if DEBUG_VERBOSE_LEVEL >= 2
130
  DEBUG_MSG("Received MIDI Event: %02X %02X %02X\n",
131
        midi_package.evnt0, midi_package.evnt1, midi_package.evnt2);
132
#endif
133
 
134
  pin = (midi_package.note - BLM_MIDI_STARTNOTE) & 0x7f;
135
  if( midi_package.chn == Chn1 && ((midi_package.event == NoteOff) || (midi_package.event == NoteOn)) && (pin < 0x40 ) )
136
  {
137
    // 90 xx 00 is the same like a note off event!
138
    // (-> http://www.borg.com/~jglatt/tech/midispec.htm)
139
    pin_state = (midi_package.event == NoteOff) || (midi_package.velocity == 0x00);
140
 
141
    // store last pin number and value
142
    last_dout_pin = pin;
143
    last_dout_value = pin_state;
144
 
145
    // set LEDs
146
#if BLM_NUM_COLOURS >= 1
147
    BLM_DOUT_PinSet(0, pin, pin_state ? 0 : 1); // red, pin, value
148
#endif
149
#if BLM_NUM_COLOURS >= 2
150
    BLM_DOUT_PinSet(1, pin, pin_state ? 0 : (midi_package.velocity > 64)); // green, pin, value
151
#endif
152
#if BLM_NUM_COLOURS >= 3
153
    BLM_DOUT_PinSet(2, pin, pin_state ? 0 : (midi_package.velocity > 110)); // blue, pin, value
154
#endif  
155
  }
156
}
157
 
158
 
159
/////////////////////////////////////////////////////////////////////////////
160
// This hook is called before the shift register chain is scanned
161
/////////////////////////////////////////////////////////////////////////////
162
void APP_SRIO_ServicePrepare(void)
163
{
164
  // prepare DOUT registers to drive the column
165
  BLM_PrepareCol();
166
}
167
 
168
 
169
/////////////////////////////////////////////////////////////////////////////
170
// This hook is called after the shift register chain has been scanned
171
/////////////////////////////////////////////////////////////////////////////
172
void APP_SRIO_ServiceFinish(void)
173
{
174
  // call the BLM_GetRow function after scan is finished to capture the read DIN values
175
  BLM_GetRow();
176
}
177
 
178
 
179
/////////////////////////////////////////////////////////////////////////////
180
// This hook is called when a button has been toggled
181
// pin_value is 1 when button released, and 0 when button pressed
182
/////////////////////////////////////////////////////////////////////////////
183
void APP_DIN_NotifyToggle(u32 pin, u32 pin_value)
184
{
185
}
186
 
187
 
188
/////////////////////////////////////////////////////////////////////////////
189
// This hook is called when an encoder has been moved
190
// incrementer is positive when encoder has been turned clockwise, else
191
// it is negative
192
/////////////////////////////////////////////////////////////////////////////
193
void APP_ENC_NotifyChange(u32 encoder, s32 incrementer)
194
{
195
}
196
 
197
 
198
/////////////////////////////////////////////////////////////////////////////
199
// This hook is called when a pot has been moved
200
/////////////////////////////////////////////////////////////////////////////
201
void APP_AIN_NotifyChange(u32 pin, u32 pin_value)
202
{
203
}
204
 
205
 
206
/////////////////////////////////////////////////////////////////////////////
207
// This task is called each mS to check the BLM button states
208
/////////////////////////////////////////////////////////////////////////////
209
 
210
// will be called on BLM pin changes (see TASK_BLM_Check)
211
void DIN_BLM_NotifyToggle(u32 pin, u32 pin_value)
212
{
213
#if DEBUG_VERBOSE_LEVEL >= 2
214
  DEBUG_MSG("DIN_BLM_NotifyToggle(%d, %d)\n", pin, pin_value);
215
#endif
216
 
217
  // remember pin and value
218
  last_din_pin = pin;
219
  last_din_value = pin_value;
220
 
221
  // map pin and value, invert DIN value (so that LED lit when button pressed)
222
#if BLM_NUM_COLOURS >= 1
223
  BLM_DOUT_PinSet(0, pin, pin_value ? 0 : 1); // red, pin, value
224
#endif
225
#if BLM_NUM_COLOURS >= 2
226
  BLM_DOUT_PinSet(1, pin, pin_value ? 0 : 1); // green, pin, value
227
#endif
228
#if BLM_NUM_COLOURS >= 3
229
  BLM_DOUT_PinSet(2, pin, pin_value ? 0 : 1); // blue, pin, value
230
#endif
231
 
232
  // send MIDI event
233
  MIOS32_MIDI_SendNoteOn(DEFAULT, Chn1, (pin + BLM_MIDI_STARTNOTE) & 0x7f, pin_value ? 0x00 : 0x7f);
234
}
235
 
236
static void TASK_BLM_Check(void *pvParameters)
237
{
238
  portTickType xLastExecutionTime;
239
 
240
  // Initialise the xLastExecutionTime variable on task entry
241
  xLastExecutionTime = xTaskGetTickCount();
242
 
243
  while( 1 ) {
244
    vTaskDelayUntil(&xLastExecutionTime, 1 / portTICK_RATE_MS);
245
 
246
    // check for BLM pin changes, call DIN_BLM_NotifyToggle on each toggled pin
247
    BLM_ButtonHandler(DIN_BLM_NotifyToggle);
248
  }
249
}