Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: FATFileSystem N5110_mod SDFileSystem USBDevice mbed
main.h
00001 /** 00002 @file main.h 00003 @brief Header file containing functions prototypes, defines, and global variables 00004 @brief K64F drum machine project, for ELEC2645 00005 @author Peter J.G. Nye 00006 @date May 2016 00007 */ 00008 00009 #ifndef MAIN_H 00010 #define MAIN_H 00011 00012 #define DEBOUNCE 0.2 /*!< Debounce time for tap tempo */ 00013 00014 #include "mbed.h" 00015 #include "N5110.h" 00016 #include "USBMIDI.h" 00017 #include "USBSerial.h" 00018 #include "USBAudio.h" 00019 #include "SDFileSystem.h" 00020 #include <fstream> 00021 00022 /** @defgroup group1 Button matrix 00023 * used for the scanning of the UI buttons 00024 * @{ 00025 */ 00026 00027 00028 /** # IO & interrupts */ 00029 00030 /** @brief Output bus used for scanning button matrix */ 00031 BusOut ButtonOut(PTE26,PTE25,PTD3,PTC8); 00032 00033 /** @brief interrupt pins connected to button matrix outputs */ 00034 InterruptIn ButtonIn_A(PTB11); 00035 InterruptIn ButtonIn_B(PTB3); 00036 InterruptIn ButtonIn_C(PTB2); 00037 InterruptIn ButtonIn_D(PTB10); 00038 InterruptIn ButtonIn_E(PTB20); 00039 00040 /** @brief ticker to rotate ButtonOut bus */ 00041 Ticker Button_scan_tkr; 00042 00043 /** # Flags, maps & variables */ 00044 00045 /** @brief generates boolean flags for the matrix buttons */ 00046 struct buttons_struct { 00047 bool steps[8]; 00048 bool start; 00049 bool skip; 00050 bool edit; 00051 bool pattern; 00052 bool inst; 00053 bool save; 00054 bool shift; 00055 bool load; 00056 bool back; 00057 bool down; 00058 bool enter; 00059 bool up; 00060 }; 00061 buttons_struct buttons; 00062 00063 /** 00064 @namespace keyMap 00065 @brief indicates name of button pressed 00066 @brief used for serial debugging 00067 */ 00068 char *keyMap[][4] { 00069 {"Step 7", "Step 6", "Step 5", "Step 4" }, 00070 {"Step 3", "Step 2", "Step 1", "Step 0" }, 00071 {"Load", "save", "Edit", "Instrument"}, 00072 {"Pattern", "Skip", "Shift", "Start", }, 00073 {"Back", "Enter", "Down", "Up" } 00074 }; 00075 00076 /** @brief used to map the button presses on to the boolean flags */ 00077 bool *keyMap_point[][4] { 00078 {&buttons.steps[7], &buttons.steps[6], &buttons.steps[5], &buttons.steps[4] }, 00079 {&buttons.steps[3], &buttons.steps[2], &buttons.steps[1], &buttons.steps[0] }, 00080 {&buttons.load, &buttons.save, &buttons.edit, &buttons.inst }, 00081 {&buttons.pattern, &buttons.skip, &buttons.shift, &buttons.start }, 00082 {&buttons.back, &buttons.enter, &buttons.down, &buttons.up } 00083 }; 00084 00085 /** 00086 @namespace ButtonOut_val 00087 @brief Used to set the value of the ButtonOut bus 00088 @brief When scanned through, sets a different value low each cycle 00089 */ 00090 uint8_t ButtonOut_val[]= { 00091 0xE, 00092 0xD, 00093 0xB, 00094 0x7 00095 }; 00096 00097 /** 00098 @namespace ButtonOut_pos 00099 @brief stores the current position of the ButtonOut scan, in order to select the correct value for the output from the ButtonOut_val array 00100 @brief increments upwards from 0 to 3, and then resets 00101 */ 00102 uint8_t ButtonOut_pos = 0; 00103 00104 /** @brief used to indicate if any button has been pressed */ 00105 bool ButtonFlag_zero = 0; 00106 00107 /** @brief used to indicate if the input on the column during the last scan was equal to zero */ 00108 bool ButtonFlag_cols[4] = {0,0,0,0}; 00109 bool ButtonFlag_press = 0; 00110 00111 /** # Functions */ 00112 00113 /** 00114 @namespace Button_init 00115 @brief function used to initialise the button matrix 00116 */ 00117 void Button_init(); 00118 00119 /** 00120 @namespace Button_scan_ISR 00121 @brief function called by Button_scan_tkr 00122 @brief rotates the output of ButtonOut 00123 */ 00124 void Button_scan_ISR (); 00125 00126 /** 00127 @namespace Button_update 00128 @brief Used to update the boolean flags within Buttons_struct 00129 @brief called by ButtonIn_*_ISR 00130 @param row - Used to indicate which ButtonIn interrupt it was called by 00131 */ 00132 void Button_update (int row); 00133 00134 /** @brief interrupt subroutines for ButtonIn_* input */ 00135 void ButtonIn_A_ISR(); 00136 void ButtonIn_B_ISR (); 00137 void ButtonIn_C_ISR (); 00138 void ButtonIn_D_ISR (); 00139 void ButtonIn_E_ISR (); 00140 00141 /** @} */ // end of group1 00142 00143 /** @defgroup group2 LED matrix 00144 * Variables and functions used to adress the step LEDs 00145 * @{ 00146 */ 00147 00148 /** # IO & interrupts */ 00149 00150 /** @brief bus used to adress the LED matrix */ 00151 BusOut LED_bus(PTC0,PTC9,PTC5,PTC7 , PTC2,PTA2,PTB23,PTA1); 00152 00153 /** @brief ticker used to scan through the LED matrix values */ 00154 Ticker LED_scan_tkr; 00155 00156 /** 00157 @namespace shift_LED 00158 @brief LED above the shift button on the PCB 00159 @brief not connected to the LED matrix 00160 */ 00161 DigitalOut shift_LED(PTE24); 00162 00163 /** # Flags, maps & variables */ 00164 00165 /** 00166 @namespace LED_buffer 00167 @brief array used to store the requred ouput values for LED_bus 00168 @brief used to reduce the amount of functions to be run within the LED_scan_ISR 00169 */ 00170 int LED_buffer[4] = {0x7,0xB,0xD,0xE}; 00171 00172 //uint16_t LED_loadBuffer = 0; 00173 00174 /** 00175 @namespace LED_pos 00176 @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 00177 @brief increments upwards from 0 to 3, and then resets 00178 */ 00179 int LED_pos = 0; 00180 00181 /** @brief */ 00182 //uint8_t LED_scan_row = 0x1; 00183 00184 /** # LED functions */ 00185 00186 /** @brief ISR for LED matrix scan */ 00187 void LED_scan_ISR(); 00188 00189 /** 00190 @namespace LED_write 00191 @brief updates the values within LED_buffer to those required for the current output 00192 @param value - data to be written to the LED matrix 00193 */ 00194 void LED_write(uint16_t value); //Loads buffer 00195 void LED_init(); 00196 00197 /** @} */ // end of group2 00198 00199 /** @defgroup group3 LCD 00200 * Variables and functions used to adress the nokia 5110 LCD 00201 * @{ 00202 */ 00203 00204 /** # IO */ 00205 00206 /** @brief defines LCD pins */ 00207 N5110 lcd (PTE26 , PTA0 , PTC4 , PTD0 , PTD2 , PTD1 , PTC3); 00208 00209 /** # LCD variables */ 00210 00211 /** @brief current backlight brightness */ 00212 float brightness = 0.32; 00213 00214 /** @brief current backlight PWM frequency */ 00215 float PWM_freq = 50; 00216 00217 /** @brief boolean to keep track of screen colour inversion */ 00218 bool inverse = false; 00219 00220 /** # LCD functions */ 00221 00222 /** @brief function to initialise screen */ 00223 void LCD_init(); 00224 00225 /** @brief function to set brightness and PWM frequency values */ 00226 void LCD_set(); 00227 /** @brief function to print main screen to display */ 00228 void LCD_printMain(); 00229 00230 /** @} */ // end of group3 00231 00232 /** @defgroup group4 Sequencing 00233 * Variables and functions used to adress the step sequencer 00234 * @{ 00235 */ 00236 00237 /** # Sequence data */ 00238 00239 /** @brief channel names (to be displayed to LCD) */ 00240 char *inst_names[]= { 00241 "Kick", 00242 "Snare", 00243 "Crash", 00244 "HH Cl", 00245 "HH Op", 00246 "Clap", 00247 "Ride", 00248 "Cowbl", 00249 "Skip" 00250 }; 00251 00252 /** 00253 @namespace Sequence_struct 00254 @brief generates the variables requred for a sequence 00255 @brief Output_type indicates the type of output set for each channel 00256 @brief Midi_note indicates the note ouput via midi for each channel 00257 @brief Trig_chan indicates the hardware trigger output for each channel 00258 @brief gate contains the sequence data for each channel 00259 @brief final channel of the gate array contains step skip points 00260 @brief the data for each part of these arrays had to be set individually, due to limitations of the MBed IDE 00261 */ 00262 union Sequence_struct { 00263 struct structure { 00264 char Output_type[8]; 00265 float MIDI_note[8]; 00266 float Trig_chan[8]; 00267 uint16_t gate[9]; 00268 } structure; 00269 char* sequence; 00270 Sequence_struct::Sequence_struct() { 00271 structure.Output_type[0] = 0; 00272 structure.Output_type[1] = 0; 00273 structure.Output_type[2] = 0; 00274 structure.Output_type[3] = 0; 00275 structure.Output_type[4] = 0; 00276 structure.Output_type[5] = 0; 00277 structure.Output_type[6] = 0; 00278 structure.Output_type[7] = 0; 00279 structure.MIDI_note[0] = 36; 00280 structure.MIDI_note[1] = 38; 00281 structure.MIDI_note[2] = 49; 00282 structure.MIDI_note[3] = 42; 00283 structure.MIDI_note[4] = 56; 00284 structure.MIDI_note[5] = 39; 00285 structure.MIDI_note[6] = 37; 00286 structure.MIDI_note[7] = 51; 00287 structure.Trig_chan[0] = 0; 00288 structure.Trig_chan[1] = 1; 00289 structure.Trig_chan[2] = 2; 00290 structure.Trig_chan[3] = 3; 00291 structure.Trig_chan[4] = 3; 00292 structure.Trig_chan[5] = 4; 00293 structure.Trig_chan[6] = 5; 00294 structure.Trig_chan[7] = 6; 00295 }; 00296 } Seq_array[8]; 00297 00298 /** # Sequence IO & interrupts */ 00299 00300 /** @brief ticker to increment sequence position */ 00301 Ticker Sequence_increment_tkr; 00302 00303 /** # flags & variables */ 00304 00305 /** @brief currently selected instrument channel */ 00306 int Seq_current_inst = 0; 00307 /** @brief currently selected sequence */ 00308 int Seq_current_seq = 0; 00309 /** @brief previously selected instrument channel (used when skip button is pressed) */ 00310 int Seq_prev_inst = 0; 00311 /** @brief current sequence step */ 00312 int Seq_current_step = 15; 00313 /** @brief flag to indicate step increment */ 00314 volatile int Seq_flag = 0; 00315 /** @brief current tempo value */ 00316 float tempo = 60; 00317 /** @brief flag to indicate if the sequence is currently running */ 00318 bool running = 1; 00319 /** @brief indicates whether system is waiting for instrument channel selection */ 00320 bool inst_flag = 0; 00321 /** @brief indicates whether system is waiting for sequence pattern selection */ 00322 bool pattern_flag = 0; 00323 /** @brief indicates whether in skip setup mode */ 00324 bool skip_flag = 0; 00325 00326 /** # Sequence functions */ 00327 00328 /** @brief initialises sequencer */ 00329 void Sequence_init(); 00330 00331 /** @brief writes input data to sequence gate */ 00332 void Sequence_write(); 00333 /** @brief interrupt call for sequence increment */ 00334 void Sequence_increment_ISR(); 00335 /** @brief increments sequencer */ 00336 void Sequence_increment(); 00337 00338 /** @} */ // end of group4 00339 00340 /** @defgroup group5 Tap tempo 00341 * Variables and functions used to control the tap-tempo 00342 * @{ 00343 */ 00344 00345 /** # IO & interrupts */ 00346 00347 /** @brief timer to keep track of time between button presses */ 00348 Timer tempo_time; 00349 /** @brief interrupt for tap tempo button */ 00350 InterruptIn tempo_tapIn(PTB9); 00351 00352 /** # flags & variables */ 00353 00354 /** @brief set when tap tempo button is pressed */ 00355 volatile int tempo_flag; 00356 /** @brief indicates how many times tap tempo button has been pressed */ 00357 int tempo_timerState; 00358 /** @brief keeps track of button bounces */ 00359 float tempo_debounce = 0; 00360 /** @brief indicates that tempo has been changed */ 00361 volatile int tempo_update_flag = 0; 00362 00363 /** # functions */ 00364 00365 /** @brief initialises tap tempo */ 00366 void tempo_init(); 00367 /** @brief interrupt called by tap tempo input */ 00368 void tempo_ISR(); 00369 /** @brief tempo set routine */ 00370 void tempo_set(); 00371 /** @brief Indicates tempo to be updated (used when tempo is set via menu) */ 00372 void tempo_update(); 00373 00374 /** @} */ // end of group5 00375 00376 /** @defgroup group6 Audio output 00377 * Variables and functions used in the playback of wav files (not currently functional) 00378 * @{ 00379 */ 00380 00381 /** # flags & variables */ 00382 00383 /** @brief buffer to contain audio file data */ 00384 char * audio_buffer[8]; 00385 00386 /** @brief map to indicate positions of RAW WAV files on SD card */ 00387 char* audioFile_map[8] { 00388 "/sd/samples/808/Bass.wav", 00389 "/sd/samples/808/Snare.wav", 00390 "/sd/samples/808/Crash.wav", 00391 "/sd/samples/808/HiHat_closed.wav", 00392 "/sd/samples/808/HiHat_open.wav", 00393 "/sd/samples/808/Clap.wav", 00394 "/sd/samples/808/Ride.wav", 00395 "/sd/samples/808/CowBell.wav" 00396 }; 00397 /** @brief indicates whether file is currently playing */ 00398 bool audio_flag[8] {0,0,0,0,0,0,0,0}; 00399 /** @brief indicates file length */ 00400 int audio_length[8] {0,0,0,0,0,0,0,0}; 00401 /** @brief indicates current position within file */ 00402 int audio_pos[8] {0,0,0,0,0,0,0,0}; 00403 00404 /** # functions */ 00405 /** @brief loads file data into buffers */ 00406 void Audio_init(); 00407 /** @brief updates speaker output */ 00408 void Audio_ISR(); 00409 00410 /** @} */ // end of group6 00411 00412 /** @defgroup group7 USB 00413 * Variables and functions used to control the USB interfacing 00414 * @{ 00415 */ 00416 00417 /** # flags & variables */ 00418 /** @brief keeps track of current USB mode (0 = midi, 1 = serial, 2 = speaker, 3 = none) */ 00419 int USB_mode = 3; 00420 /** @brief indicates whether the output needs clearing */ 00421 volatile int decay_flag = 0; 00422 00423 /** # IO & interrupts */ 00424 00425 /** @brief pointer to midi function */ 00426 USBMIDI *midi = 0; 00427 /** @brief pointer to serial function */ 00428 USBSerial *pc = 0; 00429 /** @brief pointer to audio function */ 00430 USBAudio *audio = 0; 00431 /** @brief ticker for note decay */ 00432 Ticker decay_tkr; 00433 00434 00435 /** # functions */ 00436 00437 /** @brief initialises USB */ 00438 void USB_init(); 00439 /** @brief called by decay ticker */ 00440 void decay_ISR(); 00441 00442 /** @} */ // end of group7 00443 00444 /** @defgroup group8 USB audio 00445 * Variables and functions used to act as a USB soundcard 00446 * (heavily based on existing code, [see here](https://developer.mbed.org/users/samux/code/USBAUDIO_speaker/)) 00447 * @{ 00448 */ 00449 00450 /** # flags & variables */ 00451 00452 /** @brief ticker for audio output */ 00453 Ticker audio_tkr; 00454 /** @brief buffer to contain incoming audio packets */ 00455 int16_t USBaudio_buffer[96/2]; 00456 /** @brief indicates availability of audio data */ 00457 volatile bool USBaudio_packet_available = false; 00458 /** @brief indicates current position within USBaudio_buffer */ 00459 int USBaudio_index_buffer = 0; 00460 /** @brief maintains previous speaker value until more data is available */ 00461 uint16_t audio_prev_val = 0; 00462 00463 /** # USB audio functions */ 00464 /** @brief ISR for audio ouput */ 00465 void audio_tkr_ISR(); 00466 00467 /** @} */ // end of group8 00468 00469 /** @defgroup group9 Menu system 00470 * Variables and functions used to control menus 00471 * based on menu system from first-year project 00472 * @{ 00473 */ 00474 00475 /** # Menu maps */ 00476 00477 /** @brief map for Main menu */ 00478 const char *mainMenu[]= { 00479 "Tempo", 00480 "Display", 00481 "USB mode",0 00482 }; 00483 /** @brief map for display submenu */ 00484 const char *displayMenu[]= { 00485 "Brightness", 00486 "PWM frequency", 00487 "Invert",0 00488 }; 00489 /** @brief map for USB mode submenu */ 00490 const char *usbMenu[]= { 00491 "MIDI", 00492 "Serial", 00493 "Speaker", 00494 "None",0 00495 }; 00496 /** @brief map for output mode submenu */ 00497 const char *outputMenu[] = { 00498 "Midi", 00499 "WAV", 00500 "Trigger", 00501 "Muted",0 00502 }; 00503 /** @brief map for edit menu */ 00504 const char *editMenu[] = { 00505 "Output type", 00506 "Midi Note", 00507 "Trig chan", 00508 "WAV load",0 00509 }; 00510 00511 /** # functions */ 00512 00513 /** @brief function to control main menu navigation */ 00514 void mainMenu_draw(); 00515 /** @brief function to control edit menu navigation */ 00516 void editMenu_draw(); 00517 /** 00518 @namespace drawMenu 00519 @brief draws menu to LCD, and returns selected value 00520 @param array[] - menu map to be drawn 00521 */ 00522 int drawMenu(const char* array[]); 00523 /** 00524 @namespace drawVariable 00525 @brief draws variable to LCD 00526 @param variableName - name of variable being edited 00527 @param variable - pointer to variable being edited 00528 @param step_size - amount to increment variable per buttonpress 00529 @param max - maximum value of variable 00530 @param min - minimum value of variable 00531 @param function - function to be called to update variables 00532 */ 00533 void drawVariable (const char* variableName,float *variable, float step_size, float max, float min, void (*function)()); 00534 00535 /** @} */ // end of group9 00536 00537 /** @defgroup group10 Filesystem 00538 * Variables and functions used to control file management 00539 * (only partially functional) 00540 * @{ 00541 */ 00542 /** # IO */ 00543 00544 /** @brief IO for SD card interfacting */ 00545 SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); //SD card 00546 00547 /** # Functions */ 00548 00549 /** @brief loads current sequence data from SD card */ 00550 void load(); 00551 /** @brief saves current sequence data to SD card */ 00552 void save(); 00553 00554 /** @} */ // end of group10 00555 00556 /** @defgroup group11 general 00557 * General purpose variables and functions 00558 * @{ 00559 */ 00560 00561 /** # IO */ 00562 00563 /** @brief speaker output */ 00564 AnalogOut SOUND_OUT_1(DAC0_OUT); 00565 /** @brief trigger channels */ 00566 DigitalOut *trigOut[7] { 00567 new DigitalOut(PTC10), 00568 new DigitalOut(PTC11), 00569 new DigitalOut(PTC1), 00570 new DigitalOut(PTC19), 00571 new DigitalOut(PTC18), 00572 new DigitalOut(PTC17), 00573 new DigitalOut(PTC16) 00574 }; 00575 00576 /** # functions */ 00577 00578 /** @brief initialises system */ 00579 void init(); 00580 /** @brief initialises general IO*/ 00581 void IO_init(); 00582 /** 00583 @namespace bool_to_int 00584 @brief converts boolean array to integer 00585 @param array - boolean input 00586 @param shifter - integer output 00587 */ 00588 int bool_to_int (bool *array, int shifter); 00589 /** 00590 @namespace selector 00591 @brief selects value between 0 and 7 based on step button press 00592 @param flag - flag to reset after selection is made 00593 @param shifter - variable to be changed 00594 */ 00595 void selector (bool *flag, int *variable); 00596 00597 /** @brief checks interrupt flags */ 00598 void check_flags(); 00599 /** @brief null function; used to pad drawVariable when no repeated function call is needed 00600 void null(); 00601 00602 /** @} */ // end of group11 00603 00604 #endif
Generated on Tue Jul 12 2022 13:31:54 by
1.7.2