K64F drum machine, with RAW WAV sample playback, and USB midi functionality

Dependencies:   FATFileSystem N5110_mod SDFileSystem USBDevice mbed

Committer:
el14pjgn
Date:
Mon May 09 14:20:16 2016 +0000
Revision:
0:02a88f05d461
K64F drum machine, with sample playback and USB midi functionality

Who changed what in which revision?

UserRevisionLine numberNew contents of line
el14pjgn 0:02a88f05d461 1 /**
el14pjgn 0:02a88f05d461 2 @file main.h
el14pjgn 0:02a88f05d461 3 @brief Header file containing functions prototypes, defines, and global variables
el14pjgn 0:02a88f05d461 4 @brief K64F drum machine project, for ELEC2645
el14pjgn 0:02a88f05d461 5 @author Peter J.G. Nye
el14pjgn 0:02a88f05d461 6 @date May 2016
el14pjgn 0:02a88f05d461 7 */
el14pjgn 0:02a88f05d461 8
el14pjgn 0:02a88f05d461 9 #ifndef MAIN_H
el14pjgn 0:02a88f05d461 10 #define MAIN_H
el14pjgn 0:02a88f05d461 11
el14pjgn 0:02a88f05d461 12 #define DEBOUNCE 0.2 /*!< Debounce time for tap tempo */
el14pjgn 0:02a88f05d461 13
el14pjgn 0:02a88f05d461 14 #include "mbed.h"
el14pjgn 0:02a88f05d461 15 #include "N5110.h"
el14pjgn 0:02a88f05d461 16 #include "USBMIDI.h"
el14pjgn 0:02a88f05d461 17 #include "USBSerial.h"
el14pjgn 0:02a88f05d461 18 #include "USBAudio.h"
el14pjgn 0:02a88f05d461 19 #include "SDFileSystem.h"
el14pjgn 0:02a88f05d461 20 #include <fstream>
el14pjgn 0:02a88f05d461 21
el14pjgn 0:02a88f05d461 22 /** @defgroup group1 Button matrix
el14pjgn 0:02a88f05d461 23 * used for the scanning of the UI buttons
el14pjgn 0:02a88f05d461 24 * @{
el14pjgn 0:02a88f05d461 25 */
el14pjgn 0:02a88f05d461 26
el14pjgn 0:02a88f05d461 27
el14pjgn 0:02a88f05d461 28 /** # IO & interrupts */
el14pjgn 0:02a88f05d461 29
el14pjgn 0:02a88f05d461 30 /** @brief Output bus used for scanning button matrix */
el14pjgn 0:02a88f05d461 31 BusOut ButtonOut(PTE26,PTE25,PTD3,PTC8);
el14pjgn 0:02a88f05d461 32
el14pjgn 0:02a88f05d461 33 /** @brief interrupt pins connected to button matrix outputs */
el14pjgn 0:02a88f05d461 34 InterruptIn ButtonIn_A(PTB11);
el14pjgn 0:02a88f05d461 35 InterruptIn ButtonIn_B(PTB3);
el14pjgn 0:02a88f05d461 36 InterruptIn ButtonIn_C(PTB2);
el14pjgn 0:02a88f05d461 37 InterruptIn ButtonIn_D(PTB10);
el14pjgn 0:02a88f05d461 38 InterruptIn ButtonIn_E(PTB20);
el14pjgn 0:02a88f05d461 39
el14pjgn 0:02a88f05d461 40 /** @brief ticker to rotate ButtonOut bus */
el14pjgn 0:02a88f05d461 41 Ticker Button_scan_tkr;
el14pjgn 0:02a88f05d461 42
el14pjgn 0:02a88f05d461 43 /** # Flags, maps & variables */
el14pjgn 0:02a88f05d461 44
el14pjgn 0:02a88f05d461 45 /** @brief generates boolean flags for the matrix buttons */
el14pjgn 0:02a88f05d461 46 struct buttons_struct {
el14pjgn 0:02a88f05d461 47 bool steps[8];
el14pjgn 0:02a88f05d461 48 bool start;
el14pjgn 0:02a88f05d461 49 bool skip;
el14pjgn 0:02a88f05d461 50 bool edit;
el14pjgn 0:02a88f05d461 51 bool pattern;
el14pjgn 0:02a88f05d461 52 bool inst;
el14pjgn 0:02a88f05d461 53 bool save;
el14pjgn 0:02a88f05d461 54 bool shift;
el14pjgn 0:02a88f05d461 55 bool load;
el14pjgn 0:02a88f05d461 56 bool back;
el14pjgn 0:02a88f05d461 57 bool down;
el14pjgn 0:02a88f05d461 58 bool enter;
el14pjgn 0:02a88f05d461 59 bool up;
el14pjgn 0:02a88f05d461 60 };
el14pjgn 0:02a88f05d461 61 buttons_struct buttons;
el14pjgn 0:02a88f05d461 62
el14pjgn 0:02a88f05d461 63 /**
el14pjgn 0:02a88f05d461 64 @namespace keyMap
el14pjgn 0:02a88f05d461 65 @brief indicates name of button pressed
el14pjgn 0:02a88f05d461 66 @brief used for serial debugging
el14pjgn 0:02a88f05d461 67 */
el14pjgn 0:02a88f05d461 68 char *keyMap[][4] {
el14pjgn 0:02a88f05d461 69 {"Step 7", "Step 6", "Step 5", "Step 4" },
el14pjgn 0:02a88f05d461 70 {"Step 3", "Step 2", "Step 1", "Step 0" },
el14pjgn 0:02a88f05d461 71 {"Load", "save", "Edit", "Instrument"},
el14pjgn 0:02a88f05d461 72 {"Pattern", "Skip", "Shift", "Start", },
el14pjgn 0:02a88f05d461 73 {"Back", "Enter", "Down", "Up" }
el14pjgn 0:02a88f05d461 74 };
el14pjgn 0:02a88f05d461 75
el14pjgn 0:02a88f05d461 76 /** @brief used to map the button presses on to the boolean flags */
el14pjgn 0:02a88f05d461 77 bool *keyMap_point[][4] {
el14pjgn 0:02a88f05d461 78 {&buttons.steps[7], &buttons.steps[6], &buttons.steps[5], &buttons.steps[4] },
el14pjgn 0:02a88f05d461 79 {&buttons.steps[3], &buttons.steps[2], &buttons.steps[1], &buttons.steps[0] },
el14pjgn 0:02a88f05d461 80 {&buttons.load, &buttons.save, &buttons.edit, &buttons.inst },
el14pjgn 0:02a88f05d461 81 {&buttons.pattern, &buttons.skip, &buttons.shift, &buttons.start },
el14pjgn 0:02a88f05d461 82 {&buttons.back, &buttons.enter, &buttons.down, &buttons.up }
el14pjgn 0:02a88f05d461 83 };
el14pjgn 0:02a88f05d461 84
el14pjgn 0:02a88f05d461 85 /**
el14pjgn 0:02a88f05d461 86 @namespace ButtonOut_val
el14pjgn 0:02a88f05d461 87 @brief Used to set the value of the ButtonOut bus
el14pjgn 0:02a88f05d461 88 @brief When scanned through, sets a different value low each cycle
el14pjgn 0:02a88f05d461 89 */
el14pjgn 0:02a88f05d461 90 uint8_t ButtonOut_val[]= {
el14pjgn 0:02a88f05d461 91 0xE,
el14pjgn 0:02a88f05d461 92 0xD,
el14pjgn 0:02a88f05d461 93 0xB,
el14pjgn 0:02a88f05d461 94 0x7
el14pjgn 0:02a88f05d461 95 };
el14pjgn 0:02a88f05d461 96
el14pjgn 0:02a88f05d461 97 /**
el14pjgn 0:02a88f05d461 98 @namespace ButtonOut_pos
el14pjgn 0:02a88f05d461 99 @brief stores the current position of the ButtonOut scan, in order to select the correct value for the output from the ButtonOut_val array
el14pjgn 0:02a88f05d461 100 @brief increments upwards from 0 to 3, and then resets
el14pjgn 0:02a88f05d461 101 */
el14pjgn 0:02a88f05d461 102 uint8_t ButtonOut_pos = 0;
el14pjgn 0:02a88f05d461 103
el14pjgn 0:02a88f05d461 104 /** @brief used to indicate if any button has been pressed */
el14pjgn 0:02a88f05d461 105 bool ButtonFlag_zero = 0;
el14pjgn 0:02a88f05d461 106
el14pjgn 0:02a88f05d461 107 /** @brief used to indicate if the input on the column during the last scan was equal to zero */
el14pjgn 0:02a88f05d461 108 bool ButtonFlag_cols[4] = {0,0,0,0};
el14pjgn 0:02a88f05d461 109 bool ButtonFlag_press = 0;
el14pjgn 0:02a88f05d461 110
el14pjgn 0:02a88f05d461 111 /** # Functions */
el14pjgn 0:02a88f05d461 112
el14pjgn 0:02a88f05d461 113 /**
el14pjgn 0:02a88f05d461 114 @namespace Button_init
el14pjgn 0:02a88f05d461 115 @brief function used to initialise the button matrix
el14pjgn 0:02a88f05d461 116 */
el14pjgn 0:02a88f05d461 117 void Button_init();
el14pjgn 0:02a88f05d461 118
el14pjgn 0:02a88f05d461 119 /**
el14pjgn 0:02a88f05d461 120 @namespace Button_scan_ISR
el14pjgn 0:02a88f05d461 121 @brief function called by Button_scan_tkr
el14pjgn 0:02a88f05d461 122 @brief rotates the output of ButtonOut
el14pjgn 0:02a88f05d461 123 */
el14pjgn 0:02a88f05d461 124 void Button_scan_ISR();
el14pjgn 0:02a88f05d461 125
el14pjgn 0:02a88f05d461 126 /**
el14pjgn 0:02a88f05d461 127 @namespace Button_update
el14pjgn 0:02a88f05d461 128 @brief Used to update the boolean flags within Buttons_struct
el14pjgn 0:02a88f05d461 129 @brief called by ButtonIn_*_ISR
el14pjgn 0:02a88f05d461 130 @param row - Used to indicate which ButtonIn interrupt it was called by
el14pjgn 0:02a88f05d461 131 */
el14pjgn 0:02a88f05d461 132 void Button_update(int row);
el14pjgn 0:02a88f05d461 133
el14pjgn 0:02a88f05d461 134 /** @brief interrupt subroutines for ButtonIn_* input */
el14pjgn 0:02a88f05d461 135 void ButtonIn_A_ISR();
el14pjgn 0:02a88f05d461 136 void ButtonIn_B_ISR();
el14pjgn 0:02a88f05d461 137 void ButtonIn_C_ISR();
el14pjgn 0:02a88f05d461 138 void ButtonIn_D_ISR();
el14pjgn 0:02a88f05d461 139 void ButtonIn_E_ISR();
el14pjgn 0:02a88f05d461 140
el14pjgn 0:02a88f05d461 141 /** @} */ // end of group1
el14pjgn 0:02a88f05d461 142
el14pjgn 0:02a88f05d461 143 /** @defgroup group2 LED matrix
el14pjgn 0:02a88f05d461 144 * Variables and functions used to adress the step LEDs
el14pjgn 0:02a88f05d461 145 * @{
el14pjgn 0:02a88f05d461 146 */
el14pjgn 0:02a88f05d461 147
el14pjgn 0:02a88f05d461 148 /** # IO & interrupts */
el14pjgn 0:02a88f05d461 149
el14pjgn 0:02a88f05d461 150 /** @brief bus used to adress the LED matrix */
el14pjgn 0:02a88f05d461 151 BusOut LED_bus(PTC0,PTC9,PTC5,PTC7 , PTC2,PTA2,PTB23,PTA1);
el14pjgn 0:02a88f05d461 152
el14pjgn 0:02a88f05d461 153 /** @brief ticker used to scan through the LED matrix values */
el14pjgn 0:02a88f05d461 154 Ticker LED_scan_tkr;
el14pjgn 0:02a88f05d461 155
el14pjgn 0:02a88f05d461 156 /**
el14pjgn 0:02a88f05d461 157 @namespace shift_LED
el14pjgn 0:02a88f05d461 158 @brief LED above the shift button on the PCB
el14pjgn 0:02a88f05d461 159 @brief not connected to the LED matrix
el14pjgn 0:02a88f05d461 160 */
el14pjgn 0:02a88f05d461 161 DigitalOut shift_LED(PTE24);
el14pjgn 0:02a88f05d461 162
el14pjgn 0:02a88f05d461 163 /** # Flags, maps & variables */
el14pjgn 0:02a88f05d461 164
el14pjgn 0:02a88f05d461 165 /**
el14pjgn 0:02a88f05d461 166 @namespace LED_buffer
el14pjgn 0:02a88f05d461 167 @brief array used to store the requred ouput values for LED_bus
el14pjgn 0:02a88f05d461 168 @brief used to reduce the amount of functions to be run within the LED_scan_ISR
el14pjgn 0:02a88f05d461 169 */
el14pjgn 0:02a88f05d461 170 int LED_buffer[4] = {0x7,0xB,0xD,0xE};
el14pjgn 0:02a88f05d461 171
el14pjgn 0:02a88f05d461 172 //uint16_t LED_loadBuffer = 0;
el14pjgn 0:02a88f05d461 173
el14pjgn 0:02a88f05d461 174 /**
el14pjgn 0:02a88f05d461 175 @namespace LED_pos
el14pjgn 0:02a88f05d461 176 @brief stores the current position of the LED_bus scan, in order to select the correct value for the output from the LED_buffer array
el14pjgn 0:02a88f05d461 177 @brief increments upwards from 0 to 3, and then resets
el14pjgn 0:02a88f05d461 178 */
el14pjgn 0:02a88f05d461 179 int LED_pos = 0;
el14pjgn 0:02a88f05d461 180
el14pjgn 0:02a88f05d461 181 /** @brief */
el14pjgn 0:02a88f05d461 182 //uint8_t LED_scan_row = 0x1;
el14pjgn 0:02a88f05d461 183
el14pjgn 0:02a88f05d461 184 /** # LED functions */
el14pjgn 0:02a88f05d461 185
el14pjgn 0:02a88f05d461 186 /** @brief ISR for LED matrix scan */
el14pjgn 0:02a88f05d461 187 void LED_scan_ISR();
el14pjgn 0:02a88f05d461 188
el14pjgn 0:02a88f05d461 189 /**
el14pjgn 0:02a88f05d461 190 @namespace LED_write
el14pjgn 0:02a88f05d461 191 @brief updates the values within LED_buffer to those required for the current output
el14pjgn 0:02a88f05d461 192 @param value - data to be written to the LED matrix
el14pjgn 0:02a88f05d461 193 */
el14pjgn 0:02a88f05d461 194 void LED_write(uint16_t value); //Loads buffer
el14pjgn 0:02a88f05d461 195 void LED_init();
el14pjgn 0:02a88f05d461 196
el14pjgn 0:02a88f05d461 197 /** @} */ // end of group2
el14pjgn 0:02a88f05d461 198
el14pjgn 0:02a88f05d461 199 /** @defgroup group3 LCD
el14pjgn 0:02a88f05d461 200 * Variables and functions used to adress the nokia 5110 LCD
el14pjgn 0:02a88f05d461 201 * @{
el14pjgn 0:02a88f05d461 202 */
el14pjgn 0:02a88f05d461 203
el14pjgn 0:02a88f05d461 204 /** # IO */
el14pjgn 0:02a88f05d461 205
el14pjgn 0:02a88f05d461 206 /** @brief defines LCD pins */
el14pjgn 0:02a88f05d461 207 N5110 lcd (PTE26 , PTA0 , PTC4 , PTD0 , PTD2 , PTD1 , PTC3);
el14pjgn 0:02a88f05d461 208
el14pjgn 0:02a88f05d461 209 /** # LCD variables */
el14pjgn 0:02a88f05d461 210
el14pjgn 0:02a88f05d461 211 /** @brief current backlight brightness */
el14pjgn 0:02a88f05d461 212 float brightness = 0.32;
el14pjgn 0:02a88f05d461 213
el14pjgn 0:02a88f05d461 214 /** @brief current backlight PWM frequency */
el14pjgn 0:02a88f05d461 215 float PWM_freq = 50;
el14pjgn 0:02a88f05d461 216
el14pjgn 0:02a88f05d461 217 /** @brief boolean to keep track of screen colour inversion */
el14pjgn 0:02a88f05d461 218 bool inverse = false;
el14pjgn 0:02a88f05d461 219
el14pjgn 0:02a88f05d461 220 /** # LCD functions */
el14pjgn 0:02a88f05d461 221
el14pjgn 0:02a88f05d461 222 /** @brief function to initialise screen */
el14pjgn 0:02a88f05d461 223 void LCD_init();
el14pjgn 0:02a88f05d461 224
el14pjgn 0:02a88f05d461 225 /** @brief function to set brightness and PWM frequency values */
el14pjgn 0:02a88f05d461 226 void LCD_set();
el14pjgn 0:02a88f05d461 227 /** @brief function to print main screen to display */
el14pjgn 0:02a88f05d461 228 void LCD_printMain();
el14pjgn 0:02a88f05d461 229
el14pjgn 0:02a88f05d461 230 /** @} */ // end of group3
el14pjgn 0:02a88f05d461 231
el14pjgn 0:02a88f05d461 232 /** @defgroup group4 Sequencing
el14pjgn 0:02a88f05d461 233 * Variables and functions used to adress the step sequencer
el14pjgn 0:02a88f05d461 234 * @{
el14pjgn 0:02a88f05d461 235 */
el14pjgn 0:02a88f05d461 236
el14pjgn 0:02a88f05d461 237 /** # Sequence data */
el14pjgn 0:02a88f05d461 238
el14pjgn 0:02a88f05d461 239 /** @brief channel names (to be displayed to LCD) */
el14pjgn 0:02a88f05d461 240 char *inst_names[]= {
el14pjgn 0:02a88f05d461 241 "Kick",
el14pjgn 0:02a88f05d461 242 "Snare",
el14pjgn 0:02a88f05d461 243 "Crash",
el14pjgn 0:02a88f05d461 244 "HH Cl",
el14pjgn 0:02a88f05d461 245 "HH Op",
el14pjgn 0:02a88f05d461 246 "Clap",
el14pjgn 0:02a88f05d461 247 "Ride",
el14pjgn 0:02a88f05d461 248 "Cowbl",
el14pjgn 0:02a88f05d461 249 "Skip"
el14pjgn 0:02a88f05d461 250 };
el14pjgn 0:02a88f05d461 251
el14pjgn 0:02a88f05d461 252 /**
el14pjgn 0:02a88f05d461 253 @namespace Sequence_struct
el14pjgn 0:02a88f05d461 254 @brief generates the variables requred for a sequence
el14pjgn 0:02a88f05d461 255 @brief Output_type indicates the type of output set for each channel
el14pjgn 0:02a88f05d461 256 @brief Midi_note indicates the note ouput via midi for each channel
el14pjgn 0:02a88f05d461 257 @brief Trig_chan indicates the hardware trigger output for each channel
el14pjgn 0:02a88f05d461 258 @brief gate contains the sequence data for each channel
el14pjgn 0:02a88f05d461 259 @brief final channel of the gate array contains step skip points
el14pjgn 0:02a88f05d461 260 @brief the data for each part of these arrays had to be set individually, due to limitations of the MBed IDE
el14pjgn 0:02a88f05d461 261 */
el14pjgn 0:02a88f05d461 262 union Sequence_struct {
el14pjgn 0:02a88f05d461 263 struct structure {
el14pjgn 0:02a88f05d461 264 char Output_type[8];
el14pjgn 0:02a88f05d461 265 float MIDI_note[8];
el14pjgn 0:02a88f05d461 266 float Trig_chan[8];
el14pjgn 0:02a88f05d461 267 uint16_t gate[9];
el14pjgn 0:02a88f05d461 268 } structure;
el14pjgn 0:02a88f05d461 269 char* sequence;
el14pjgn 0:02a88f05d461 270 Sequence_struct::Sequence_struct() {
el14pjgn 0:02a88f05d461 271 structure.Output_type[0] = 0;
el14pjgn 0:02a88f05d461 272 structure.Output_type[1] = 0;
el14pjgn 0:02a88f05d461 273 structure.Output_type[2] = 0;
el14pjgn 0:02a88f05d461 274 structure.Output_type[3] = 0;
el14pjgn 0:02a88f05d461 275 structure.Output_type[4] = 0;
el14pjgn 0:02a88f05d461 276 structure.Output_type[5] = 0;
el14pjgn 0:02a88f05d461 277 structure.Output_type[6] = 0;
el14pjgn 0:02a88f05d461 278 structure.Output_type[7] = 0;
el14pjgn 0:02a88f05d461 279 structure.MIDI_note[0] = 36;
el14pjgn 0:02a88f05d461 280 structure.MIDI_note[1] = 38;
el14pjgn 0:02a88f05d461 281 structure.MIDI_note[2] = 49;
el14pjgn 0:02a88f05d461 282 structure.MIDI_note[3] = 42;
el14pjgn 0:02a88f05d461 283 structure.MIDI_note[4] = 56;
el14pjgn 0:02a88f05d461 284 structure.MIDI_note[5] = 39;
el14pjgn 0:02a88f05d461 285 structure.MIDI_note[6] = 37;
el14pjgn 0:02a88f05d461 286 structure.MIDI_note[7] = 51;
el14pjgn 0:02a88f05d461 287 structure.Trig_chan[0] = 0;
el14pjgn 0:02a88f05d461 288 structure.Trig_chan[1] = 1;
el14pjgn 0:02a88f05d461 289 structure.Trig_chan[2] = 2;
el14pjgn 0:02a88f05d461 290 structure.Trig_chan[3] = 3;
el14pjgn 0:02a88f05d461 291 structure.Trig_chan[4] = 3;
el14pjgn 0:02a88f05d461 292 structure.Trig_chan[5] = 4;
el14pjgn 0:02a88f05d461 293 structure.Trig_chan[6] = 5;
el14pjgn 0:02a88f05d461 294 structure.Trig_chan[7] = 6;
el14pjgn 0:02a88f05d461 295 };
el14pjgn 0:02a88f05d461 296 } Seq_array[8];
el14pjgn 0:02a88f05d461 297
el14pjgn 0:02a88f05d461 298 /** # Sequence IO & interrupts */
el14pjgn 0:02a88f05d461 299
el14pjgn 0:02a88f05d461 300 /** @brief ticker to increment sequence position */
el14pjgn 0:02a88f05d461 301 Ticker Sequence_increment_tkr;
el14pjgn 0:02a88f05d461 302
el14pjgn 0:02a88f05d461 303 /** # flags & variables */
el14pjgn 0:02a88f05d461 304
el14pjgn 0:02a88f05d461 305 /** @brief currently selected instrument channel */
el14pjgn 0:02a88f05d461 306 int Seq_current_inst = 0;
el14pjgn 0:02a88f05d461 307 /** @brief currently selected sequence */
el14pjgn 0:02a88f05d461 308 int Seq_current_seq = 0;
el14pjgn 0:02a88f05d461 309 /** @brief previously selected instrument channel (used when skip button is pressed) */
el14pjgn 0:02a88f05d461 310 int Seq_prev_inst = 0;
el14pjgn 0:02a88f05d461 311 /** @brief current sequence step */
el14pjgn 0:02a88f05d461 312 int Seq_current_step = 15;
el14pjgn 0:02a88f05d461 313 /** @brief flag to indicate step increment */
el14pjgn 0:02a88f05d461 314 volatile int Seq_flag = 0;
el14pjgn 0:02a88f05d461 315 /** @brief current tempo value */
el14pjgn 0:02a88f05d461 316 float tempo = 60;
el14pjgn 0:02a88f05d461 317 /** @brief flag to indicate if the sequence is currently running */
el14pjgn 0:02a88f05d461 318 bool running = 1;
el14pjgn 0:02a88f05d461 319 /** @brief indicates whether system is waiting for instrument channel selection */
el14pjgn 0:02a88f05d461 320 bool inst_flag = 0;
el14pjgn 0:02a88f05d461 321 /** @brief indicates whether system is waiting for sequence pattern selection */
el14pjgn 0:02a88f05d461 322 bool pattern_flag = 0;
el14pjgn 0:02a88f05d461 323 /** @brief indicates whether in skip setup mode */
el14pjgn 0:02a88f05d461 324 bool skip_flag = 0;
el14pjgn 0:02a88f05d461 325
el14pjgn 0:02a88f05d461 326 /** # Sequence functions */
el14pjgn 0:02a88f05d461 327
el14pjgn 0:02a88f05d461 328 /** @brief initialises sequencer */
el14pjgn 0:02a88f05d461 329 void Sequence_init();
el14pjgn 0:02a88f05d461 330
el14pjgn 0:02a88f05d461 331 /** @brief writes input data to sequence gate */
el14pjgn 0:02a88f05d461 332 void Sequence_write();
el14pjgn 0:02a88f05d461 333 /** @brief interrupt call for sequence increment */
el14pjgn 0:02a88f05d461 334 void Sequence_increment_ISR();
el14pjgn 0:02a88f05d461 335 /** @brief increments sequencer */
el14pjgn 0:02a88f05d461 336 void Sequence_increment();
el14pjgn 0:02a88f05d461 337
el14pjgn 0:02a88f05d461 338 /** @} */ // end of group4
el14pjgn 0:02a88f05d461 339
el14pjgn 0:02a88f05d461 340 /** @defgroup group5 Tap tempo
el14pjgn 0:02a88f05d461 341 * Variables and functions used to control the tap-tempo
el14pjgn 0:02a88f05d461 342 * @{
el14pjgn 0:02a88f05d461 343 */
el14pjgn 0:02a88f05d461 344
el14pjgn 0:02a88f05d461 345 /** # IO & interrupts */
el14pjgn 0:02a88f05d461 346
el14pjgn 0:02a88f05d461 347 /** @brief timer to keep track of time between button presses */
el14pjgn 0:02a88f05d461 348 Timer tempo_time;
el14pjgn 0:02a88f05d461 349 /** @brief interrupt for tap tempo button */
el14pjgn 0:02a88f05d461 350 InterruptIn tempo_tapIn(PTB9);
el14pjgn 0:02a88f05d461 351
el14pjgn 0:02a88f05d461 352 /** # flags & variables */
el14pjgn 0:02a88f05d461 353
el14pjgn 0:02a88f05d461 354 /** @brief set when tap tempo button is pressed */
el14pjgn 0:02a88f05d461 355 volatile int tempo_flag;
el14pjgn 0:02a88f05d461 356 /** @brief indicates how many times tap tempo button has been pressed */
el14pjgn 0:02a88f05d461 357 int tempo_timerState;
el14pjgn 0:02a88f05d461 358 /** @brief keeps track of button bounces */
el14pjgn 0:02a88f05d461 359 float tempo_debounce = 0;
el14pjgn 0:02a88f05d461 360 /** @brief indicates that tempo has been changed */
el14pjgn 0:02a88f05d461 361 volatile int tempo_update_flag = 0;
el14pjgn 0:02a88f05d461 362
el14pjgn 0:02a88f05d461 363 /** # functions */
el14pjgn 0:02a88f05d461 364
el14pjgn 0:02a88f05d461 365 /** @brief initialises tap tempo */
el14pjgn 0:02a88f05d461 366 void tempo_init();
el14pjgn 0:02a88f05d461 367 /** @brief interrupt called by tap tempo input */
el14pjgn 0:02a88f05d461 368 void tempo_ISR();
el14pjgn 0:02a88f05d461 369 /** @brief tempo set routine */
el14pjgn 0:02a88f05d461 370 void tempo_set();
el14pjgn 0:02a88f05d461 371 /** @brief Indicates tempo to be updated (used when tempo is set via menu) */
el14pjgn 0:02a88f05d461 372 void tempo_update();
el14pjgn 0:02a88f05d461 373
el14pjgn 0:02a88f05d461 374 /** @} */ // end of group5
el14pjgn 0:02a88f05d461 375
el14pjgn 0:02a88f05d461 376 /** @defgroup group6 Audio output
el14pjgn 0:02a88f05d461 377 * Variables and functions used in the playback of wav files (not currently functional)
el14pjgn 0:02a88f05d461 378 * @{
el14pjgn 0:02a88f05d461 379 */
el14pjgn 0:02a88f05d461 380
el14pjgn 0:02a88f05d461 381 /** # flags & variables */
el14pjgn 0:02a88f05d461 382
el14pjgn 0:02a88f05d461 383 /** @brief buffer to contain audio file data */
el14pjgn 0:02a88f05d461 384 char * audio_buffer[8];
el14pjgn 0:02a88f05d461 385
el14pjgn 0:02a88f05d461 386 /** @brief map to indicate positions of RAW WAV files on SD card */
el14pjgn 0:02a88f05d461 387 char* audioFile_map[8] {
el14pjgn 0:02a88f05d461 388 "/sd/samples/808/Bass.wav",
el14pjgn 0:02a88f05d461 389 "/sd/samples/808/Snare.wav",
el14pjgn 0:02a88f05d461 390 "/sd/samples/808/Crash.wav",
el14pjgn 0:02a88f05d461 391 "/sd/samples/808/HiHat_closed.wav",
el14pjgn 0:02a88f05d461 392 "/sd/samples/808/HiHat_open.wav",
el14pjgn 0:02a88f05d461 393 "/sd/samples/808/Clap.wav",
el14pjgn 0:02a88f05d461 394 "/sd/samples/808/Ride.wav",
el14pjgn 0:02a88f05d461 395 "/sd/samples/808/CowBell.wav"
el14pjgn 0:02a88f05d461 396 };
el14pjgn 0:02a88f05d461 397 /** @brief indicates whether file is currently playing */
el14pjgn 0:02a88f05d461 398 bool audio_flag[8] {0,0,0,0,0,0,0,0};
el14pjgn 0:02a88f05d461 399 /** @brief indicates file length */
el14pjgn 0:02a88f05d461 400 int audio_length[8] {0,0,0,0,0,0,0,0};
el14pjgn 0:02a88f05d461 401 /** @brief indicates current position within file */
el14pjgn 0:02a88f05d461 402 int audio_pos[8] {0,0,0,0,0,0,0,0};
el14pjgn 0:02a88f05d461 403
el14pjgn 0:02a88f05d461 404 /** # functions */
el14pjgn 0:02a88f05d461 405 /** @brief loads file data into buffers */
el14pjgn 0:02a88f05d461 406 void Audio_init();
el14pjgn 0:02a88f05d461 407 /** @brief updates speaker output */
el14pjgn 0:02a88f05d461 408 void Audio_ISR();
el14pjgn 0:02a88f05d461 409
el14pjgn 0:02a88f05d461 410 /** @} */ // end of group6
el14pjgn 0:02a88f05d461 411
el14pjgn 0:02a88f05d461 412 /** @defgroup group7 USB
el14pjgn 0:02a88f05d461 413 * Variables and functions used to control the USB interfacing
el14pjgn 0:02a88f05d461 414 * @{
el14pjgn 0:02a88f05d461 415 */
el14pjgn 0:02a88f05d461 416
el14pjgn 0:02a88f05d461 417 /** # flags & variables */
el14pjgn 0:02a88f05d461 418 /** @brief keeps track of current USB mode (0 = midi, 1 = serial, 2 = speaker, 3 = none) */
el14pjgn 0:02a88f05d461 419 int USB_mode = 3;
el14pjgn 0:02a88f05d461 420 /** @brief indicates whether the output needs clearing */
el14pjgn 0:02a88f05d461 421 volatile int decay_flag = 0;
el14pjgn 0:02a88f05d461 422
el14pjgn 0:02a88f05d461 423 /** # IO & interrupts */
el14pjgn 0:02a88f05d461 424
el14pjgn 0:02a88f05d461 425 /** @brief pointer to midi function */
el14pjgn 0:02a88f05d461 426 USBMIDI *midi = 0;
el14pjgn 0:02a88f05d461 427 /** @brief pointer to serial function */
el14pjgn 0:02a88f05d461 428 USBSerial *pc = 0;
el14pjgn 0:02a88f05d461 429 /** @brief pointer to audio function */
el14pjgn 0:02a88f05d461 430 USBAudio *audio = 0;
el14pjgn 0:02a88f05d461 431 /** @brief ticker for note decay */
el14pjgn 0:02a88f05d461 432 Ticker decay_tkr;
el14pjgn 0:02a88f05d461 433
el14pjgn 0:02a88f05d461 434
el14pjgn 0:02a88f05d461 435 /** # functions */
el14pjgn 0:02a88f05d461 436
el14pjgn 0:02a88f05d461 437 /** @brief initialises USB */
el14pjgn 0:02a88f05d461 438 void USB_init();
el14pjgn 0:02a88f05d461 439 /** @brief called by decay ticker */
el14pjgn 0:02a88f05d461 440 void decay_ISR();
el14pjgn 0:02a88f05d461 441
el14pjgn 0:02a88f05d461 442 /** @} */ // end of group7
el14pjgn 0:02a88f05d461 443
el14pjgn 0:02a88f05d461 444 /** @defgroup group8 USB audio
el14pjgn 0:02a88f05d461 445 * Variables and functions used to act as a USB soundcard
el14pjgn 0:02a88f05d461 446 * (heavily based on existing code, [see here](https://developer.mbed.org/users/samux/code/USBAUDIO_speaker/))
el14pjgn 0:02a88f05d461 447 * @{
el14pjgn 0:02a88f05d461 448 */
el14pjgn 0:02a88f05d461 449
el14pjgn 0:02a88f05d461 450 /** # flags & variables */
el14pjgn 0:02a88f05d461 451
el14pjgn 0:02a88f05d461 452 /** @brief ticker for audio output */
el14pjgn 0:02a88f05d461 453 Ticker audio_tkr;
el14pjgn 0:02a88f05d461 454 /** @brief buffer to contain incoming audio packets */
el14pjgn 0:02a88f05d461 455 int16_t USBaudio_buffer[96/2];
el14pjgn 0:02a88f05d461 456 /** @brief indicates availability of audio data */
el14pjgn 0:02a88f05d461 457 volatile bool USBaudio_packet_available = false;
el14pjgn 0:02a88f05d461 458 /** @brief indicates current position within USBaudio_buffer */
el14pjgn 0:02a88f05d461 459 int USBaudio_index_buffer = 0;
el14pjgn 0:02a88f05d461 460 /** @brief maintains previous speaker value until more data is available */
el14pjgn 0:02a88f05d461 461 uint16_t audio_prev_val = 0;
el14pjgn 0:02a88f05d461 462
el14pjgn 0:02a88f05d461 463 /** # USB audio functions */
el14pjgn 0:02a88f05d461 464 /** @brief ISR for audio ouput */
el14pjgn 0:02a88f05d461 465 void audio_tkr_ISR();
el14pjgn 0:02a88f05d461 466
el14pjgn 0:02a88f05d461 467 /** @} */ // end of group8
el14pjgn 0:02a88f05d461 468
el14pjgn 0:02a88f05d461 469 /** @defgroup group9 Menu system
el14pjgn 0:02a88f05d461 470 * Variables and functions used to control menus
el14pjgn 0:02a88f05d461 471 * based on menu system from first-year project
el14pjgn 0:02a88f05d461 472 * @{
el14pjgn 0:02a88f05d461 473 */
el14pjgn 0:02a88f05d461 474
el14pjgn 0:02a88f05d461 475 /** # Menu maps */
el14pjgn 0:02a88f05d461 476
el14pjgn 0:02a88f05d461 477 /** @brief map for Main menu */
el14pjgn 0:02a88f05d461 478 const char *mainMenu[]= {
el14pjgn 0:02a88f05d461 479 "Tempo",
el14pjgn 0:02a88f05d461 480 "Display",
el14pjgn 0:02a88f05d461 481 "USB mode",0
el14pjgn 0:02a88f05d461 482 };
el14pjgn 0:02a88f05d461 483 /** @brief map for display submenu */
el14pjgn 0:02a88f05d461 484 const char *displayMenu[]= {
el14pjgn 0:02a88f05d461 485 "Brightness",
el14pjgn 0:02a88f05d461 486 "PWM frequency",
el14pjgn 0:02a88f05d461 487 "Invert",0
el14pjgn 0:02a88f05d461 488 };
el14pjgn 0:02a88f05d461 489 /** @brief map for USB mode submenu */
el14pjgn 0:02a88f05d461 490 const char *usbMenu[]= {
el14pjgn 0:02a88f05d461 491 "MIDI",
el14pjgn 0:02a88f05d461 492 "Serial",
el14pjgn 0:02a88f05d461 493 "Speaker",
el14pjgn 0:02a88f05d461 494 "None",0
el14pjgn 0:02a88f05d461 495 };
el14pjgn 0:02a88f05d461 496 /** @brief map for output mode submenu */
el14pjgn 0:02a88f05d461 497 const char *outputMenu[] = {
el14pjgn 0:02a88f05d461 498 "Midi",
el14pjgn 0:02a88f05d461 499 "WAV",
el14pjgn 0:02a88f05d461 500 "Trigger",
el14pjgn 0:02a88f05d461 501 "Muted",0
el14pjgn 0:02a88f05d461 502 };
el14pjgn 0:02a88f05d461 503 /** @brief map for edit menu */
el14pjgn 0:02a88f05d461 504 const char *editMenu[] = {
el14pjgn 0:02a88f05d461 505 "Output type",
el14pjgn 0:02a88f05d461 506 "Midi Note",
el14pjgn 0:02a88f05d461 507 "Trig chan",
el14pjgn 0:02a88f05d461 508 "WAV load",0
el14pjgn 0:02a88f05d461 509 };
el14pjgn 0:02a88f05d461 510
el14pjgn 0:02a88f05d461 511 /** # functions */
el14pjgn 0:02a88f05d461 512
el14pjgn 0:02a88f05d461 513 /** @brief function to control main menu navigation */
el14pjgn 0:02a88f05d461 514 void mainMenu_draw();
el14pjgn 0:02a88f05d461 515 /** @brief function to control edit menu navigation */
el14pjgn 0:02a88f05d461 516 void editMenu_draw();
el14pjgn 0:02a88f05d461 517 /**
el14pjgn 0:02a88f05d461 518 @namespace drawMenu
el14pjgn 0:02a88f05d461 519 @brief draws menu to LCD, and returns selected value
el14pjgn 0:02a88f05d461 520 @param array[] - menu map to be drawn
el14pjgn 0:02a88f05d461 521 */
el14pjgn 0:02a88f05d461 522 int drawMenu(const char* array[]);
el14pjgn 0:02a88f05d461 523 /**
el14pjgn 0:02a88f05d461 524 @namespace drawVariable
el14pjgn 0:02a88f05d461 525 @brief draws variable to LCD
el14pjgn 0:02a88f05d461 526 @param variableName - name of variable being edited
el14pjgn 0:02a88f05d461 527 @param variable - pointer to variable being edited
el14pjgn 0:02a88f05d461 528 @param step_size - amount to increment variable per buttonpress
el14pjgn 0:02a88f05d461 529 @param max - maximum value of variable
el14pjgn 0:02a88f05d461 530 @param min - minimum value of variable
el14pjgn 0:02a88f05d461 531 @param function - function to be called to update variables
el14pjgn 0:02a88f05d461 532 */
el14pjgn 0:02a88f05d461 533 void drawVariable(const char* variableName,float *variable, float step_size, float max, float min, void (*function)());
el14pjgn 0:02a88f05d461 534
el14pjgn 0:02a88f05d461 535 /** @} */ // end of group9
el14pjgn 0:02a88f05d461 536
el14pjgn 0:02a88f05d461 537 /** @defgroup group10 Filesystem
el14pjgn 0:02a88f05d461 538 * Variables and functions used to control file management
el14pjgn 0:02a88f05d461 539 * (only partially functional)
el14pjgn 0:02a88f05d461 540 * @{
el14pjgn 0:02a88f05d461 541 */
el14pjgn 0:02a88f05d461 542 /** # IO */
el14pjgn 0:02a88f05d461 543
el14pjgn 0:02a88f05d461 544 /** @brief IO for SD card interfacting */
el14pjgn 0:02a88f05d461 545 SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); //SD card
el14pjgn 0:02a88f05d461 546
el14pjgn 0:02a88f05d461 547 /** # Functions */
el14pjgn 0:02a88f05d461 548
el14pjgn 0:02a88f05d461 549 /** @brief loads current sequence data from SD card */
el14pjgn 0:02a88f05d461 550 void load();
el14pjgn 0:02a88f05d461 551 /** @brief saves current sequence data to SD card */
el14pjgn 0:02a88f05d461 552 void save();
el14pjgn 0:02a88f05d461 553
el14pjgn 0:02a88f05d461 554 /** @} */ // end of group10
el14pjgn 0:02a88f05d461 555
el14pjgn 0:02a88f05d461 556 /** @defgroup group11 general
el14pjgn 0:02a88f05d461 557 * General purpose variables and functions
el14pjgn 0:02a88f05d461 558 * @{
el14pjgn 0:02a88f05d461 559 */
el14pjgn 0:02a88f05d461 560
el14pjgn 0:02a88f05d461 561 /** # IO */
el14pjgn 0:02a88f05d461 562
el14pjgn 0:02a88f05d461 563 /** @brief speaker output */
el14pjgn 0:02a88f05d461 564 AnalogOut SOUND_OUT_1(DAC0_OUT);
el14pjgn 0:02a88f05d461 565 /** @brief trigger channels */
el14pjgn 0:02a88f05d461 566 DigitalOut *trigOut[7] {
el14pjgn 0:02a88f05d461 567 new DigitalOut(PTC10),
el14pjgn 0:02a88f05d461 568 new DigitalOut(PTC11),
el14pjgn 0:02a88f05d461 569 new DigitalOut(PTC1),
el14pjgn 0:02a88f05d461 570 new DigitalOut(PTC19),
el14pjgn 0:02a88f05d461 571 new DigitalOut(PTC18),
el14pjgn 0:02a88f05d461 572 new DigitalOut(PTC17),
el14pjgn 0:02a88f05d461 573 new DigitalOut(PTC16)
el14pjgn 0:02a88f05d461 574 };
el14pjgn 0:02a88f05d461 575
el14pjgn 0:02a88f05d461 576 /** # functions */
el14pjgn 0:02a88f05d461 577
el14pjgn 0:02a88f05d461 578 /** @brief initialises system */
el14pjgn 0:02a88f05d461 579 void init();
el14pjgn 0:02a88f05d461 580 /** @brief initialises general IO*/
el14pjgn 0:02a88f05d461 581 void IO_init();
el14pjgn 0:02a88f05d461 582 /**
el14pjgn 0:02a88f05d461 583 @namespace bool_to_int
el14pjgn 0:02a88f05d461 584 @brief converts boolean array to integer
el14pjgn 0:02a88f05d461 585 @param array - boolean input
el14pjgn 0:02a88f05d461 586 @param shifter - integer output
el14pjgn 0:02a88f05d461 587 */
el14pjgn 0:02a88f05d461 588 int bool_to_int(bool *array, int shifter);
el14pjgn 0:02a88f05d461 589 /**
el14pjgn 0:02a88f05d461 590 @namespace selector
el14pjgn 0:02a88f05d461 591 @brief selects value between 0 and 7 based on step button press
el14pjgn 0:02a88f05d461 592 @param flag - flag to reset after selection is made
el14pjgn 0:02a88f05d461 593 @param shifter - variable to be changed
el14pjgn 0:02a88f05d461 594 */
el14pjgn 0:02a88f05d461 595 void selector(bool *flag, int *variable);
el14pjgn 0:02a88f05d461 596
el14pjgn 0:02a88f05d461 597 /** @brief checks interrupt flags */
el14pjgn 0:02a88f05d461 598 void check_flags();
el14pjgn 0:02a88f05d461 599 /** @brief null function; used to pad drawVariable when no repeated function call is needed
el14pjgn 0:02a88f05d461 600 void null();
el14pjgn 0:02a88f05d461 601
el14pjgn 0:02a88f05d461 602 /** @} */ // end of group11
el14pjgn 0:02a88f05d461 603
el14pjgn 0:02a88f05d461 604 #endif