This project allows for the sending of MIDI messages, and setting of variable resistances, controlled by a distance sensor. MIDI input messages can also be used to set the variable resistance.

Dependencies:   N5110 PinDetect SRF08 USBDevice mbed PowerControl

Committer:
el13tjoc
Date:
Sun May 10 01:58:51 2015 +0000
Revision:
0:39399720eaeb
Child:
1:802453187acf
The final version of the distance sensor to MIDI and resistance project

Who changed what in which revision?

UserRevisionLine numberNew contents of line
el13tjoc 0:39399720eaeb 1 /**
el13tjoc 0:39399720eaeb 2 @file main.cpp
el13tjoc 0:39399720eaeb 3
el13tjoc 0:39399720eaeb 4 @brief Program implementation
el13tjoc 0:39399720eaeb 5
el13tjoc 0:39399720eaeb 6 */
el13tjoc 0:39399720eaeb 7 #include "main.h"
el13tjoc 0:39399720eaeb 8
el13tjoc 0:39399720eaeb 9 int main() {
el13tjoc 0:39399720eaeb 10
el13tjoc 0:39399720eaeb 11 ///Read parameter variables from VALS.csv
el13tjoc 0:39399720eaeb 12 readNumberValuesFromFile();
el13tjoc 0:39399720eaeb 13 ///Resise the smoothing values vector
el13tjoc 0:39399720eaeb 14 smoothingValues.resize(numberValues[distanceSmoothing]+1);
el13tjoc 0:39399720eaeb 15
el13tjoc 0:39399720eaeb 16 ///Set MCP4151 chip select high initially
el13tjoc 0:39399720eaeb 17 CS = 1;
el13tjoc 0:39399720eaeb 18 /// Set the MCP4151 SPI for 16 bit mode 3
el13tjoc 0:39399720eaeb 19 MCP4151.format(16,3);
el13tjoc 0:39399720eaeb 20 /// Set MCP4151 clock frequency to 1000000
el13tjoc 0:39399720eaeb 21 MCP4151.frequency(1000000);
el13tjoc 0:39399720eaeb 22
el13tjoc 0:39399720eaeb 23 ///Initialise the screen and print splash screen
el13tjoc 0:39399720eaeb 24 lcd.init();
el13tjoc 0:39399720eaeb 25 lcd.printString("EXPRESSIONATOR",0,1);
el13tjoc 0:39399720eaeb 26 lcd.printString("version 1.0",0,2);
el13tjoc 0:39399720eaeb 27 lcd.printString("Toby O'Connell",0,4);
el13tjoc 0:39399720eaeb 28 wait(0.5);
el13tjoc 0:39399720eaeb 29
el13tjoc 0:39399720eaeb 30 ///Setup callback function for range finder ticker
el13tjoc 0:39399720eaeb 31 startRangingTicker.attach(&startRangingFlag, 0.1);
el13tjoc 0:39399720eaeb 32
el13tjoc 0:39399720eaeb 33 ///Set the initial range finder distance
el13tjoc 0:39399720eaeb 34 SRF08.setRangeRegister((int)((numberValues[maxDistance]-43.0)/43.0));
el13tjoc 0:39399720eaeb 35
el13tjoc 0:39399720eaeb 36 ///Setup callback function for when MIDI messages are received
el13tjoc 0:39399720eaeb 37 midi.attach(receivedMIDI);
el13tjoc 0:39399720eaeb 38
el13tjoc 0:39399720eaeb 39 ///Setup callbacks functions for button presses
el13tjoc 0:39399720eaeb 40 buttonA.attach_asserted(&aPressed); //Attach functions to button presses
el13tjoc 0:39399720eaeb 41 buttonB.attach_asserted(&bPressed);
el13tjoc 0:39399720eaeb 42 buttonC.attach_asserted(&cPressed);
el13tjoc 0:39399720eaeb 43 buttonD.attach_asserted(&dPressed);
el13tjoc 0:39399720eaeb 44
el13tjoc 0:39399720eaeb 45 ///Set button press doubounce freqencies to the default of 20ms
el13tjoc 0:39399720eaeb 46 buttonA.setSampleFrequency();
el13tjoc 0:39399720eaeb 47 buttonB.setSampleFrequency();
el13tjoc 0:39399720eaeb 48 buttonC.setSampleFrequency();
el13tjoc 0:39399720eaeb 49 buttonD.setSampleFrequency();
el13tjoc 0:39399720eaeb 50
el13tjoc 0:39399720eaeb 51 ///Open the buttons.bmp image in binary, skip the header and copy into a buffer before closing
el13tjoc 0:39399720eaeb 52 img = fopen("/local/BUTTONS.BMP", "rb");
el13tjoc 0:39399720eaeb 53 fseek (img , 54 , SEEK_SET);
el13tjoc 0:39399720eaeb 54 fread(imgbuffer, (84*3*48), 1, img);
el13tjoc 0:39399720eaeb 55 fclose(img);
el13tjoc 0:39399720eaeb 56
el13tjoc 0:39399720eaeb 57 ///Show initial menu
el13tjoc 0:39399720eaeb 58 showScreen();
el13tjoc 0:39399720eaeb 59 ///Create infinite while loop to run program repeatedly
el13tjoc 0:39399720eaeb 60 while(1) {
el13tjoc 0:39399720eaeb 61 if(aButtonFlag){
el13tjoc 0:39399720eaeb 62 if (menu[state].screenType == menuType || digitArrowPosition + 1 >= maxValLen) {
el13tjoc 0:39399720eaeb 63 ///screenSelect() if button A is pressed and the screen type is a menu or a page type that is confirmed
el13tjoc 0:39399720eaeb 64 screenSelect();
el13tjoc 0:39399720eaeb 65 } else {
el13tjoc 0:39399720eaeb 66 ///incrementDigitArrowPos() if button A is pressed and the screen type is not a menu or a page type that is confirmed
el13tjoc 0:39399720eaeb 67 incrementDigitArrowPos();
el13tjoc 0:39399720eaeb 68 }
el13tjoc 0:39399720eaeb 69 aButtonFlag = 0;
el13tjoc 0:39399720eaeb 70 }
el13tjoc 0:39399720eaeb 71 if(bButtonFlag){
el13tjoc 0:39399720eaeb 72 if (menu[state].screenType == menuType || digitArrowPosition - 1 < 0) {
el13tjoc 0:39399720eaeb 73 ///screenBack() if button B is pressed and the screen type is a menu or a page type that is reverted
el13tjoc 0:39399720eaeb 74 screenBack();
el13tjoc 0:39399720eaeb 75 } else {
el13tjoc 0:39399720eaeb 76 ///decrementDigitArrowPos() if button B is pressed and the screen type is not a menu or a page type that is confirmed
el13tjoc 0:39399720eaeb 77 decrementDigitArrowPos();
el13tjoc 0:39399720eaeb 78 }
el13tjoc 0:39399720eaeb 79 bButtonFlag = 0;
el13tjoc 0:39399720eaeb 80 }
el13tjoc 0:39399720eaeb 81 if(cButtonFlag){
el13tjoc 0:39399720eaeb 82 if (menu[state].screenType == menuType) {
el13tjoc 0:39399720eaeb 83 ///menuUp() if button C is pressed and the screen type is a menu
el13tjoc 0:39399720eaeb 84 menuUp();
el13tjoc 0:39399720eaeb 85 } else {
el13tjoc 0:39399720eaeb 86 ///incrementValue() if button C is pressed and the screen type is not a menu
el13tjoc 0:39399720eaeb 87 incrementValue();
el13tjoc 0:39399720eaeb 88 }
el13tjoc 0:39399720eaeb 89 cButtonFlag = 0;
el13tjoc 0:39399720eaeb 90 }
el13tjoc 0:39399720eaeb 91 if(dButtonFlag){
el13tjoc 0:39399720eaeb 92 if (menu[state].screenType == menuType) {
el13tjoc 0:39399720eaeb 93 ///menuDown() if button D is pressed and the screen type is a menu
el13tjoc 0:39399720eaeb 94 menuDown();
el13tjoc 0:39399720eaeb 95 } else {
el13tjoc 0:39399720eaeb 96 ///decrementValue() if button D is pressed and the screen type is not a menu
el13tjoc 0:39399720eaeb 97 decrementValue();
el13tjoc 0:39399720eaeb 98 }
el13tjoc 0:39399720eaeb 99 dButtonFlag = 0;
el13tjoc 0:39399720eaeb 100 }
el13tjoc 0:39399720eaeb 101 if(rangingStartFlag) {
el13tjoc 0:39399720eaeb 102 ///Start ranging if the startRangingTicker has fired
el13tjoc 0:39399720eaeb 103 rangingStart();
el13tjoc 0:39399720eaeb 104 rangingStartFlag = 0;
el13tjoc 0:39399720eaeb 105 }
el13tjoc 0:39399720eaeb 106 if(rangeGetFlag) {
el13tjoc 0:39399720eaeb 107 ///Start ranging if the getRangeTimeout has fired
el13tjoc 0:39399720eaeb 108 rangeGet();
el13tjoc 0:39399720eaeb 109 rangeGetFlag = 0;
el13tjoc 0:39399720eaeb 110 }
el13tjoc 0:39399720eaeb 111 if(measuredDistanceFlag) {
el13tjoc 0:39399720eaeb 112 if(numberValues[MIDIOutputOn]) {
el13tjoc 0:39399720eaeb 113 ///writeMIDI() if the distance has just been measured and MIDI output is on
el13tjoc 0:39399720eaeb 114 writeMIDI(distanceFraction);
el13tjoc 0:39399720eaeb 115 }
el13tjoc 0:39399720eaeb 116 if(numberValues[resistanceSourceIndex] == sensorSource) {
el13tjoc 0:39399720eaeb 117 ///writeMCP4151() if the distance has just been measured and the resistance source is the ultrasonic sensor
el13tjoc 0:39399720eaeb 118 writeMCP4151(distanceFraction);
el13tjoc 0:39399720eaeb 119 }
el13tjoc 0:39399720eaeb 120 ///showScreen() if the distance has just been measured and the screen type is the display screen
el13tjoc 0:39399720eaeb 121 if(menu[state].screenType == displayType) showScreen();
el13tjoc 0:39399720eaeb 122 measuredDistanceFlag = 0;
el13tjoc 0:39399720eaeb 123 }
el13tjoc 0:39399720eaeb 124 if(receivedMIDIFlag) {
el13tjoc 0:39399720eaeb 125 if(numberValues[resistanceSourceIndex] == MIDISource) {
el13tjoc 0:39399720eaeb 126 ///writeMCP4151() if a MIDI message has been received and the resistance source is the MIDI input
el13tjoc 0:39399720eaeb 127 writeMCP4151(receivedMIDIValue/127.0);
el13tjoc 0:39399720eaeb 128 }
el13tjoc 0:39399720eaeb 129 ///showScreen() if a MIDI message has been received and the screen type is the display screen
el13tjoc 0:39399720eaeb 130 if(menu[state].screenType == displayType) showScreen();
el13tjoc 0:39399720eaeb 131 receivedMIDIFlag = 0;
el13tjoc 0:39399720eaeb 132 }
el13tjoc 0:39399720eaeb 133 }
el13tjoc 0:39399720eaeb 134 }
el13tjoc 0:39399720eaeb 135
el13tjoc 0:39399720eaeb 136
el13tjoc 0:39399720eaeb 137 void aPressed() { aButtonFlag = 1; } //raise button flags
el13tjoc 0:39399720eaeb 138 void bPressed() { bButtonFlag = 1; }
el13tjoc 0:39399720eaeb 139 void cPressed() { cButtonFlag = 1; }
el13tjoc 0:39399720eaeb 140 void dPressed() { dButtonFlag = 1; }
el13tjoc 0:39399720eaeb 141
el13tjoc 0:39399720eaeb 142 void startRangingFlag() { rangingStartFlag = 1;} //raise ranging star flag
el13tjoc 0:39399720eaeb 143 void getRangeFlag() { rangeGetFlag = 1;} //raise range get flag
el13tjoc 0:39399720eaeb 144
el13tjoc 0:39399720eaeb 145 void screenSelect() {
el13tjoc 0:39399720eaeb 146 if (menu[state].screenType == pageType) {
el13tjoc 0:39399720eaeb 147 ///If exiting a page type screen checkValidity() and do the immediateUpdates()
el13tjoc 0:39399720eaeb 148 checkValidity();
el13tjoc 0:39399720eaeb 149 writeNumberValuesToFile();
el13tjoc 0:39399720eaeb 150 immediateUpdates();
el13tjoc 0:39399720eaeb 151 }
el13tjoc 0:39399720eaeb 152 ///Change to the next state based on the menu arrow position
el13tjoc 0:39399720eaeb 153 state = menu[state].nextState[menuArrowPosition]; //Set new menu state basted on arrow position
el13tjoc 0:39399720eaeb 154 if (menu[state].screenType == pageType) {
el13tjoc 0:39399720eaeb 155 ///if entering a page type screen save the associated value to oldValue for reverting back
el13tjoc 0:39399720eaeb 156 oldValue = numberValues[menu[state].associatedValueIndex];
el13tjoc 0:39399720eaeb 157 }
el13tjoc 0:39399720eaeb 158 ///Reset menu and digit arrow posiitons
el13tjoc 0:39399720eaeb 159 menuArrowPosition = 0;
el13tjoc 0:39399720eaeb 160 digitArrowPosition = 0;
el13tjoc 0:39399720eaeb 161 ///Update the display
el13tjoc 0:39399720eaeb 162 showScreen();
el13tjoc 0:39399720eaeb 163 }
el13tjoc 0:39399720eaeb 164 void screenBack() {
el13tjoc 0:39399720eaeb 165 if (menu[state].screenType == pageType) {
el13tjoc 0:39399720eaeb 166 ///If reverting back out of a page type screen set the assoicated value to its previous value
el13tjoc 0:39399720eaeb 167 numberValues[menu[state].associatedValueIndex] = oldValue;
el13tjoc 0:39399720eaeb 168 }
el13tjoc 0:39399720eaeb 169 ///Go to FSM parent state
el13tjoc 0:39399720eaeb 170 state = menu[state].previousState;
el13tjoc 0:39399720eaeb 171 ///Reset menu and digit arrow posiitons
el13tjoc 0:39399720eaeb 172 menuArrowPosition = 0;
el13tjoc 0:39399720eaeb 173 digitArrowPosition = 0;
el13tjoc 0:39399720eaeb 174 ///Update the display
el13tjoc 0:39399720eaeb 175 showScreen();
el13tjoc 0:39399720eaeb 176 }
el13tjoc 0:39399720eaeb 177 void menuUp() {
el13tjoc 0:39399720eaeb 178 ///Decrement the menu arrow position
el13tjoc 0:39399720eaeb 179 menuArrowPosition = (menuArrowPosition + menu[state].listLength - 1) % menu[state].listLength;
el13tjoc 0:39399720eaeb 180 ///Update the display
el13tjoc 0:39399720eaeb 181 showScreen();
el13tjoc 0:39399720eaeb 182 }
el13tjoc 0:39399720eaeb 183 void menuDown() {
el13tjoc 0:39399720eaeb 184 ///Increment the menu arrow position
el13tjoc 0:39399720eaeb 185 menuArrowPosition = (menuArrowPosition + 1) % menu[state].listLength;
el13tjoc 0:39399720eaeb 186 ///Update the display
el13tjoc 0:39399720eaeb 187 showScreen();
el13tjoc 0:39399720eaeb 188 }
el13tjoc 0:39399720eaeb 189
el13tjoc 0:39399720eaeb 190 void incrementDigitArrowPos() {
el13tjoc 0:39399720eaeb 191 ///Increment the digit arrow position
el13tjoc 0:39399720eaeb 192 digitArrowPosition += 1;
el13tjoc 0:39399720eaeb 193 ///Update the display
el13tjoc 0:39399720eaeb 194 showScreen();
el13tjoc 0:39399720eaeb 195 }
el13tjoc 0:39399720eaeb 196 void decrementDigitArrowPos() {
el13tjoc 0:39399720eaeb 197 ///Increment the digit arrow position
el13tjoc 0:39399720eaeb 198 digitArrowPosition -= 1;
el13tjoc 0:39399720eaeb 199 ///Update the display
el13tjoc 0:39399720eaeb 200 showScreen();
el13tjoc 0:39399720eaeb 201 }
el13tjoc 0:39399720eaeb 202
el13tjoc 0:39399720eaeb 203 void incrementValue() {
el13tjoc 0:39399720eaeb 204 ///Set the increment to '1'
el13tjoc 0:39399720eaeb 205 increment = 1;
el13tjoc 0:39399720eaeb 206 ///amend the value
el13tjoc 0:39399720eaeb 207 amendValue();
el13tjoc 0:39399720eaeb 208 }
el13tjoc 0:39399720eaeb 209
el13tjoc 0:39399720eaeb 210 void decrementValue() {
el13tjoc 0:39399720eaeb 211 ///Set the increment to '-1'
el13tjoc 0:39399720eaeb 212 increment = -1;
el13tjoc 0:39399720eaeb 213 ///amend the value
el13tjoc 0:39399720eaeb 214 amendValue();
el13tjoc 0:39399720eaeb 215 }
el13tjoc 0:39399720eaeb 216
el13tjoc 0:39399720eaeb 217 int amendNumberValue(int value) {
el13tjoc 0:39399720eaeb 218 ///Get the digit at the current arrow position
el13tjoc 0:39399720eaeb 219 int currentDigit = (int)(value / pow(10.0,maxValLen-1-digitArrowPosition))%10;
el13tjoc 0:39399720eaeb 220
el13tjoc 0:39399720eaeb 221 if((currentDigit == 9 && increment == + 1) || (currentDigit == 0 && increment == - 1)) {
el13tjoc 0:39399720eaeb 222 ///If the digit is will become greater than 9 or less then 0, wrap around
el13tjoc 0:39399720eaeb 223 value = value - increment * 9 * pow(10.0,maxValLen-1-digitArrowPosition);
el13tjoc 0:39399720eaeb 224 } else {
el13tjoc 0:39399720eaeb 225 ///If not, increment / decrement the value
el13tjoc 0:39399720eaeb 226 value = value + increment * pow(10.0,maxValLen-1-digitArrowPosition);
el13tjoc 0:39399720eaeb 227 }
el13tjoc 0:39399720eaeb 228
el13tjoc 0:39399720eaeb 229 ///return the value
el13tjoc 0:39399720eaeb 230 return value;
el13tjoc 0:39399720eaeb 231 }
el13tjoc 0:39399720eaeb 232
el13tjoc 0:39399720eaeb 233 void amendValue() {
el13tjoc 0:39399720eaeb 234
el13tjoc 0:39399720eaeb 235 switch(menu[state].associatedValueType) {
el13tjoc 0:39399720eaeb 236 case INT:
el13tjoc 0:39399720eaeb 237 ///If the associated is an int style number amendNumberValue()
el13tjoc 0:39399720eaeb 238 numberValues[menu[state].associatedValueIndex] = amendNumberValue(numberValues[menu[state].associatedValueIndex]);
el13tjoc 0:39399720eaeb 239 break;
el13tjoc 0:39399720eaeb 240 case INDEX:
el13tjoc 0:39399720eaeb 241 ///If the associated is an index style number increment / decrement it
el13tjoc 0:39399720eaeb 242 numberValues[menu[state].associatedValueIndex] = (maxNumberValues[menu[state].associatedMaxValueIndex] + numberValues[menu[state].associatedValueIndex] + increment) % maxNumberValues[menu[state].associatedMaxValueIndex];
el13tjoc 0:39399720eaeb 243 break;
el13tjoc 0:39399720eaeb 244 case BOOL:
el13tjoc 0:39399720eaeb 245 ///If the associated is a boolean style number invert it
el13tjoc 0:39399720eaeb 246 numberValues[menu[state].associatedValueIndex] = !numberValues[menu[state].associatedValueIndex];
el13tjoc 0:39399720eaeb 247 break;
el13tjoc 0:39399720eaeb 248 }
el13tjoc 0:39399720eaeb 249 ///Update the display
el13tjoc 0:39399720eaeb 250 showScreen();
el13tjoc 0:39399720eaeb 251 }
el13tjoc 0:39399720eaeb 252
el13tjoc 0:39399720eaeb 253
el13tjoc 0:39399720eaeb 254
el13tjoc 0:39399720eaeb 255 void showScreen() {
el13tjoc 0:39399720eaeb 256 ///Clear the screen
el13tjoc 0:39399720eaeb 257 lcd.clear();
el13tjoc 0:39399720eaeb 258 ///Show the title
el13tjoc 0:39399720eaeb 259 lcd.printString(menu[state].title,0,0);
el13tjoc 0:39399720eaeb 260 if(menu[state].screenType == menuType) {
el13tjoc 0:39399720eaeb 261 for (int i = 0;i <= 3; i++) {
el13tjoc 0:39399720eaeb 262 ///If the screen type is a menu, print the items in its list
el13tjoc 0:39399720eaeb 263 lcd.printString(menu[state].list[i],6,i+1);
el13tjoc 0:39399720eaeb 264 }
el13tjoc 0:39399720eaeb 265 if(menu[state].listLength > 0) {
el13tjoc 0:39399720eaeb 266 ///if the list has items in it, then show show the menu arrow pointer
el13tjoc 0:39399720eaeb 267 lcd.printString(">",0,menuArrowPosition+1);
el13tjoc 0:39399720eaeb 268 }
el13tjoc 0:39399720eaeb 269 } else if(menu[state].screenType == pageType) {
el13tjoc 0:39399720eaeb 270 switch (menu[state].associatedValueType) {
el13tjoc 0:39399720eaeb 271 case INT:
el13tjoc 0:39399720eaeb 272 ///If the screen type is a parameter editing page and the associated value is an int type number, displayNumber() and show the digit arrow pointer
el13tjoc 0:39399720eaeb 273 displayNumber(numberValues[menu[state].associatedValueIndex],maxNumberValues[menu[state].associatedMaxValueIndex]);
el13tjoc 0:39399720eaeb 274 lcd.printString("^",digitArrowPosition*6+7,3);
el13tjoc 0:39399720eaeb 275 break;
el13tjoc 0:39399720eaeb 276 case INDEX:
el13tjoc 0:39399720eaeb 277 ///If the screen type is a parameter editing page and the associated value is an index type number, displayList()
el13tjoc 0:39399720eaeb 278 displayList(valueLists[menu[state].associatedValueList],numberValues[menu[state].associatedValueIndex]);
el13tjoc 0:39399720eaeb 279 break;
el13tjoc 0:39399720eaeb 280 case BOOL:
el13tjoc 0:39399720eaeb 281 ///If the screen type is a parameter editing page and the associated value is a boolean type number, displayBool()
el13tjoc 0:39399720eaeb 282 displayBool(numberValues[menu[state].associatedValueIndex]);
el13tjoc 0:39399720eaeb 283 break;
el13tjoc 0:39399720eaeb 284 }
el13tjoc 0:39399720eaeb 285 } else {
el13tjoc 0:39399720eaeb 286 ///If the screen type is the display screen, displayMeasured()
el13tjoc 0:39399720eaeb 287 displayMeasured();
el13tjoc 0:39399720eaeb 288 }
el13tjoc 0:39399720eaeb 289 ///Draw the buttons.bmp image to the screen using drawBMP()
el13tjoc 0:39399720eaeb 290 drawBMP();
el13tjoc 0:39399720eaeb 291 }
el13tjoc 0:39399720eaeb 292
el13tjoc 0:39399720eaeb 293 void displayNumber(int value, int maxValue) {
el13tjoc 0:39399720eaeb 294 char buffer[14];
el13tjoc 0:39399720eaeb 295 ///Find the maximum length the incoming value could be
el13tjoc 0:39399720eaeb 296 maxValLen = sprintf(buffer,"%d",maxValue);
el13tjoc 0:39399720eaeb 297 ///Find the actual length of the incoming value
el13tjoc 0:39399720eaeb 298 valLen = sprintf(buffer,"%d",value);
el13tjoc 0:39399720eaeb 299 ///Print the value to the screen, filling any unused digit spaces with zeros
el13tjoc 0:39399720eaeb 300 for(int i = 0; i <= maxValLen - valLen; i++) {
el13tjoc 0:39399720eaeb 301 lcd.printString("0",6*i+7,2);
el13tjoc 0:39399720eaeb 302 }
el13tjoc 0:39399720eaeb 303 lcd.printString(buffer,6*(maxValLen-valLen)+7,2);
el13tjoc 0:39399720eaeb 304 }
el13tjoc 0:39399720eaeb 305
el13tjoc 0:39399720eaeb 306 void displayList(string list[], int listIndex) {
el13tjoc 0:39399720eaeb 307 char buffer[14];
el13tjoc 0:39399720eaeb 308 maxValLen = 1;
el13tjoc 0:39399720eaeb 309 ///Display the list element at the list index
el13tjoc 0:39399720eaeb 310 valLen = sprintf(buffer,"%s",list[listIndex].c_str());
el13tjoc 0:39399720eaeb 311 lcd.printString(buffer,7,2);
el13tjoc 0:39399720eaeb 312 }
el13tjoc 0:39399720eaeb 313
el13tjoc 0:39399720eaeb 314 void displayBool(bool toggle) {
el13tjoc 0:39399720eaeb 315 char buffer[14];
el13tjoc 0:39399720eaeb 316 maxValLen = 1;
el13tjoc 0:39399720eaeb 317 ///Print "On" if the boolean value is 1 and "Off" if the boolean value is 0
el13tjoc 0:39399720eaeb 318 valLen = sprintf(buffer,"%s", toggle ? "On" : "Off");
el13tjoc 0:39399720eaeb 319 lcd.printString(buffer,7,2);
el13tjoc 0:39399720eaeb 320 }
el13tjoc 0:39399720eaeb 321
el13tjoc 0:39399720eaeb 322 void displayMeasured() {
el13tjoc 0:39399720eaeb 323 char buffer[14];
el13tjoc 0:39399720eaeb 324 ///Print the distance measured by the SRF08
el13tjoc 0:39399720eaeb 325 sprintf(buffer,"D: %d",measuredDistance);
el13tjoc 0:39399720eaeb 326 lcd.printString(buffer,7,1);
el13tjoc 0:39399720eaeb 327 ///If the MIDI output is on, print the MIDI output value, else print "OFF"
el13tjoc 0:39399720eaeb 328 if(numberValues[MIDIOutputOn] == 1) {
el13tjoc 0:39399720eaeb 329 sprintf(buffer,"MO: %d",(int)(distanceFraction*127));
el13tjoc 0:39399720eaeb 330 } else {
el13tjoc 0:39399720eaeb 331 sprintf(buffer,"MO: OFF");
el13tjoc 0:39399720eaeb 332 }
el13tjoc 0:39399720eaeb 333 lcd.printString(buffer,7,2);
el13tjoc 0:39399720eaeb 334 ///Print the input MIDI value
el13tjoc 0:39399720eaeb 335 sprintf(buffer,"MI: %d",receivedMIDIValue);
el13tjoc 0:39399720eaeb 336 lcd.printString(buffer,7,3);
el13tjoc 0:39399720eaeb 337 if(numberValues[resistanceSourceIndex] == sensorSource) {
el13tjoc 0:39399720eaeb 338 ///If the resistance source is the SRF08, print the resistance based on that distance
el13tjoc 0:39399720eaeb 339 sprintf(buffer,"R: %d",(int)(numberValues[minResistance]+distanceFraction*(numberValues[maxResistance]-numberValues[minResistance])));
el13tjoc 0:39399720eaeb 340 } else if(numberValues[resistanceSourceIndex] == MIDISource) {
el13tjoc 0:39399720eaeb 341 ///If the resistance source is the the MIDI input, print the resistance based on the MIDI value
el13tjoc 0:39399720eaeb 342 sprintf(buffer,"R: %d",(int)(numberValues[minResistance]+(receivedMIDIValue/127.0)*(numberValues[maxResistance]-numberValues[minResistance])));
el13tjoc 0:39399720eaeb 343 } else {
el13tjoc 0:39399720eaeb 344 ///If the resistance source is set to OFF, print "OFF"
el13tjoc 0:39399720eaeb 345 sprintf(buffer,"R: OFF");
el13tjoc 0:39399720eaeb 346 }
el13tjoc 0:39399720eaeb 347 lcd.printString(buffer,7,4);
el13tjoc 0:39399720eaeb 348 }
el13tjoc 0:39399720eaeb 349
el13tjoc 0:39399720eaeb 350 void rangingStart() {
el13tjoc 0:39399720eaeb 351 ///detach the getRangeTimeout
el13tjoc 0:39399720eaeb 352 getRangeTimeout.detach();
el13tjoc 0:39399720eaeb 353 ///start the SRF08 ranging
el13tjoc 0:39399720eaeb 354 SRF08.startRanging(CM);
el13tjoc 0:39399720eaeb 355 ///reattach the getRangeTimeout
el13tjoc 0:39399720eaeb 356 getRangeTimeout.attach(&getRangeFlag,0.07);
el13tjoc 0:39399720eaeb 357 }
el13tjoc 0:39399720eaeb 358
el13tjoc 0:39399720eaeb 359 void rangeGet() {
el13tjoc 0:39399720eaeb 360 ///Get the measured distance from the SRF08
el13tjoc 0:39399720eaeb 361 measuredDistance = SRF08.getRange();
el13tjoc 0:39399720eaeb 362 //Convert the measuredDistance to a float between 0 and 1
el13tjoc 0:39399720eaeb 363 distanceFraction = (measuredDistance * 10 - numberValues[minDistance]) / (float)(numberValues[maxDistance]-numberValues[minDistance]);
el13tjoc 0:39399720eaeb 364 ///If the distanceFraction is > 1 or < 0, limit it to 1 or 0
el13tjoc 0:39399720eaeb 365 if(distanceFraction > 1) distanceFraction = 1;
el13tjoc 0:39399720eaeb 366 if(distanceFraction < 0) distanceFraction = 0;
el13tjoc 0:39399720eaeb 367 ///smooth() the distance fraction
el13tjoc 0:39399720eaeb 368 distanceFraction = smooth(distanceFraction);
el13tjoc 0:39399720eaeb 369 measuredDistanceFlag = 1;
el13tjoc 0:39399720eaeb 370 }
el13tjoc 0:39399720eaeb 371
el13tjoc 0:39399720eaeb 372 void receivedMIDI(MIDIMessage msg) {
el13tjoc 0:39399720eaeb 373 if(msg.type() == MIDIMessage::ControlChangeType && msg.controller() == numberValues[MIDIInputController] && msg.channel() == numberValues[MIDIInputChannel]) {
el13tjoc 0:39399720eaeb 374 ///If the MIDI message is the right controller change on the right channel, save the value
el13tjoc 0:39399720eaeb 375 receivedMIDIValue = msg.value();
el13tjoc 0:39399720eaeb 376 receivedMIDIFlag = 1;
el13tjoc 0:39399720eaeb 377 }
el13tjoc 0:39399720eaeb 378 }
el13tjoc 0:39399720eaeb 379
el13tjoc 0:39399720eaeb 380
el13tjoc 0:39399720eaeb 381 float smooth(float value) {
el13tjoc 0:39399720eaeb 382 ///Erase the first element in the smoothing vector
el13tjoc 0:39399720eaeb 383 smoothingValues.erase(smoothingValues.begin());
el13tjoc 0:39399720eaeb 384 ///Add the value to the end of the smoothing vector
el13tjoc 0:39399720eaeb 385 smoothingValues.push_back(value);
el13tjoc 0:39399720eaeb 386 ///Sum the values in the smoothing vector
el13tjoc 0:39399720eaeb 387 float sum = 0;
el13tjoc 0:39399720eaeb 388 for(int i = 0; i < smoothingValues.size(); i++) {
el13tjoc 0:39399720eaeb 389 sum += smoothingValues[i];
el13tjoc 0:39399720eaeb 390 }
el13tjoc 0:39399720eaeb 391 ///Return the mean average of the smoothing vector contents
el13tjoc 0:39399720eaeb 392 return sum/(smoothingValues.size());
el13tjoc 0:39399720eaeb 393 }
el13tjoc 0:39399720eaeb 394
el13tjoc 0:39399720eaeb 395
el13tjoc 0:39399720eaeb 396
el13tjoc 0:39399720eaeb 397 void writeMCP4151(float value) {
el13tjoc 0:39399720eaeb 398 ///Scale the max and min resistances for 0 to 10000 down to 0 to 255.
el13tjoc 0:39399720eaeb 399 int scaledMax = (int)(numberValues[maxResistance]/(10000/255.0));
el13tjoc 0:39399720eaeb 400 int scaledMin = (int)(numberValues[minResistance]/(10000/255.0));
el13tjoc 0:39399720eaeb 401 ///Convert the 0 to 1 float value to a 0 to 255 int value for the MCP4151 SPI digital potentiometer
el13tjoc 0:39399720eaeb 402 int writeValue = (int)(1 + scaledMin + value * (scaledMax - scaledMin));
el13tjoc 0:39399720eaeb 403 ///Chip enable
el13tjoc 0:39399720eaeb 404 CS = 0;
el13tjoc 0:39399720eaeb 405 ///Write the new value
el13tjoc 0:39399720eaeb 406 MCP4151.write(writeValue);
el13tjoc 0:39399720eaeb 407 ///Chip disable
el13tjoc 0:39399720eaeb 408 CS = 1;
el13tjoc 0:39399720eaeb 409 }
el13tjoc 0:39399720eaeb 410
el13tjoc 0:39399720eaeb 411 void writeMIDI(float value) {
el13tjoc 0:39399720eaeb 412 ///Convert the 0 to 1 float valye to 0 to 127 int value for the MIDI message
el13tjoc 0:39399720eaeb 413 int writeValue = (int)(value * 127);
el13tjoc 0:39399720eaeb 414 ///Write the 0 to 255 value to the MIDI output
el13tjoc 0:39399720eaeb 415 midi.write(MIDIMessage::ControlChange(numberValues[MIDIOutputController],writeValue,numberValues[MIDIOutputChannel]));
el13tjoc 0:39399720eaeb 416 }
el13tjoc 0:39399720eaeb 417
el13tjoc 0:39399720eaeb 418 void drawBMP() {
el13tjoc 0:39399720eaeb 419 ///Iterate through each pixel in the image buffer
el13tjoc 0:39399720eaeb 420 for (int i = 0; i < 84*48 ; i++) {
el13tjoc 0:39399720eaeb 421 if (imgbuffer[i*3] == 0) {
el13tjoc 0:39399720eaeb 422 ///If the pixel is not white, write it to the screen
el13tjoc 0:39399720eaeb 423 lcd.setPixel(i%84,47-(int)(i/84));
el13tjoc 0:39399720eaeb 424 }
el13tjoc 0:39399720eaeb 425 }
el13tjoc 0:39399720eaeb 426 lcd.refresh();
el13tjoc 0:39399720eaeb 427 }
el13tjoc 0:39399720eaeb 428
el13tjoc 0:39399720eaeb 429 void readNumberValuesFromFile() {
el13tjoc 0:39399720eaeb 430 ///Open the VALS.csv file
el13tjoc 0:39399720eaeb 431 ifstream csvValuesRead("/local/VALS.csv");
el13tjoc 0:39399720eaeb 432 for(int i = 0; i < 11; i++) {
el13tjoc 0:39399720eaeb 433 ///Copy each line of the csv file to a each element in the numberValues array
el13tjoc 0:39399720eaeb 434 string line;
el13tjoc 0:39399720eaeb 435 getline(csvValuesRead, line);
el13tjoc 0:39399720eaeb 436 stringstream convertor(line);
el13tjoc 0:39399720eaeb 437 convertor >> numberValues[i];
el13tjoc 0:39399720eaeb 438 }
el13tjoc 0:39399720eaeb 439 ///Close the VALS.csv file
el13tjoc 0:39399720eaeb 440 csvValuesRead.close();
el13tjoc 0:39399720eaeb 441 }
el13tjoc 0:39399720eaeb 442
el13tjoc 0:39399720eaeb 443 void writeNumberValuesToFile() {
el13tjoc 0:39399720eaeb 444 ///Open the VALS.csv file
el13tjoc 0:39399720eaeb 445 FILE *csvValuesWrite = fopen("/local/VALS.csv","w");
el13tjoc 0:39399720eaeb 446 for(int i = 0; i < 11; i++) {
el13tjoc 0:39399720eaeb 447 ///Write each element in the numberValues array to each line in the csv file
el13tjoc 0:39399720eaeb 448 fprintf(csvValuesWrite,"%d\n",numberValues[i]);
el13tjoc 0:39399720eaeb 449 }
el13tjoc 0:39399720eaeb 450 ///Close the VALS.csv file
el13tjoc 0:39399720eaeb 451 fclose(csvValuesWrite);
el13tjoc 0:39399720eaeb 452 }
el13tjoc 0:39399720eaeb 453
el13tjoc 0:39399720eaeb 454 void checkValidity() {
el13tjoc 0:39399720eaeb 455 if(numberValues[menu[state].associatedValueIndex] > maxNumberValues[menu[state].associatedMaxValueIndex]) {
el13tjoc 0:39399720eaeb 456 ///If the associated value is greater than the associated max value after being altered, revert back to the original
el13tjoc 0:39399720eaeb 457 numberValues[menu[state].associatedValueIndex] = oldValue;
el13tjoc 0:39399720eaeb 458 }
el13tjoc 0:39399720eaeb 459 if(numberValues[minResistance] >= numberValues[maxResistance]) {
el13tjoc 0:39399720eaeb 460 if(menu[state].associatedValueIndex == minResistance) {
el13tjoc 0:39399720eaeb 461 ///If the min resistance is greater than the max resistance make it 1 less than the maximum
el13tjoc 0:39399720eaeb 462 numberValues[minResistance] = numberValues[maxResistance] - 1;
el13tjoc 0:39399720eaeb 463 } else {
el13tjoc 0:39399720eaeb 464 ///If the max resistance is less than the max resistance make it 1 more than the minimum
el13tjoc 0:39399720eaeb 465 numberValues[maxResistance] = numberValues[minResistance] + 1;
el13tjoc 0:39399720eaeb 466 }
el13tjoc 0:39399720eaeb 467 }
el13tjoc 0:39399720eaeb 468 if(numberValues[minDistance] >= numberValues[maxDistance]) {
el13tjoc 0:39399720eaeb 469 if(menu[state].associatedValueIndex == minDistance) {
el13tjoc 0:39399720eaeb 470 ///If the min distance is greater than the max distance make it 1 less than the maximum
el13tjoc 0:39399720eaeb 471 numberValues[minDistance] = numberValues[maxDistance] - 1;
el13tjoc 0:39399720eaeb 472 } else {
el13tjoc 0:39399720eaeb 473 ///If the max distance is less than the max distance make it 1 more than the minimum
el13tjoc 0:39399720eaeb 474 numberValues[maxDistance] = numberValues[minDistance] + 1;
el13tjoc 0:39399720eaeb 475 }
el13tjoc 0:39399720eaeb 476 }
el13tjoc 0:39399720eaeb 477 }
el13tjoc 0:39399720eaeb 478
el13tjoc 0:39399720eaeb 479 void immediateUpdates() {
el13tjoc 0:39399720eaeb 480 if(menu[state].associatedValueIndex == maxDistance) {
el13tjoc 0:39399720eaeb 481 ///If the associated value is the max distance, set the range of the SRF08
el13tjoc 0:39399720eaeb 482 SRF08.setRangeRegister((int)((numberValues[maxDistance]-43.0)/43.0));
el13tjoc 0:39399720eaeb 483 }
el13tjoc 0:39399720eaeb 484 if(menu[state].associatedValueIndex == distanceSmoothing) {
el13tjoc 0:39399720eaeb 485 ///If the associated value is the distanceSmoothing value, resize the smoothingValues vector
el13tjoc 0:39399720eaeb 486 smoothingValues.resize(numberValues[distanceSmoothing]+1);
el13tjoc 0:39399720eaeb 487 }
el13tjoc 0:39399720eaeb 488 }