This is project one
Dependencies: MMA8451Q8_1 SLCD mbed
Fork of LCD_punch_mtr_8_v5 by
LCD_punch_mtr8_v5.cpp@6:c2ff55220a05, 2015-03-30 (annotated)
- Committer:
- sphasim
- Date:
- Mon Mar 30 19:29:26 2015 +0000
- Revision:
- 6:c2ff55220a05
- Parent:
- 5:6ec1dac45861
Done
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
scohennm | 4:89ae38dc05a9 | 1 | #include "mbed.h" |
scohennm | 4:89ae38dc05a9 | 2 | #include "MMA8451Q8.h" |
scohennm | 4:89ae38dc05a9 | 3 | #include "SLCD.h" |
scohennm | 4:89ae38dc05a9 | 4 | /* |
scohennm | 4:89ae38dc05a9 | 5 | Test of the accelerometer, digital I/O, on-board LCD screen, and 16-bit ADC. |
scohennm | 4:89ae38dc05a9 | 6 | Looing at vector product of the x-y components of the accelerometer. |
scohennm | 4:89ae38dc05a9 | 7 | Works pretty well. Still rough, program wise - sc 140710 |
scohennm | 4:89ae38dc05a9 | 8 | Addiing touch sensor to replace potentiometer. |
scohennm | 4:89ae38dc05a9 | 9 | Show x and y components |
scohennm | 4:89ae38dc05a9 | 10 | */ |
scohennm | 4:89ae38dc05a9 | 11 | |
scohennm | 4:89ae38dc05a9 | 12 | #define BLINKTIME 0.7 |
sphasim | 6:c2ff55220a05 | 13 | #define DATATIME 0.5 |
scohennm | 4:89ae38dc05a9 | 14 | #define DATADISPDWELL 0.2 |
scohennm | 4:89ae38dc05a9 | 15 | #define NUMLEDS 2 |
scohennm | 4:89ae38dc05a9 | 16 | #define LEDON 0 |
scohennm | 4:89ae38dc05a9 | 17 | #define LEDOFF 1 |
scohennm | 4:89ae38dc05a9 | 18 | #define SCALING 13 |
scohennm | 4:89ae38dc05a9 | 19 | #define LCDLEN 10 |
scohennm | 4:89ae38dc05a9 | 20 | #define RSTARTMESS "PNCH" |
scohennm | 4:89ae38dc05a9 | 21 | #define MAXVECT "MAXV" |
scohennm | 4:89ae38dc05a9 | 22 | #define XCOMP "XCMP" |
scohennm | 4:89ae38dc05a9 | 23 | #define YCOMP "YCMP" |
scohennm | 4:89ae38dc05a9 | 24 | #define ZCOMP "ZCMP" |
scohennm | 4:89ae38dc05a9 | 25 | #define ANALTOVOLTS 3.3 |
scohennm | 4:89ae38dc05a9 | 26 | #define LEDDELAY 0.400 |
scohennm | 4:89ae38dc05a9 | 27 | //define states |
scohennm | 4:89ae38dc05a9 | 28 | #define NUMSTATES 4 |
scohennm | 4:89ae38dc05a9 | 29 | #define XCOMPD 0 |
scohennm | 4:89ae38dc05a9 | 30 | #define YCOMPD 1 |
scohennm | 4:89ae38dc05a9 | 31 | #define ZCOMPD 2 |
scohennm | 4:89ae38dc05a9 | 32 | #define VMAXD 3 |
scohennm | 4:89ae38dc05a9 | 33 | #define MAXGS 8.0 |
scohennm | 4:89ae38dc05a9 | 34 | |
scohennm | 4:89ae38dc05a9 | 35 | #define REG_WHO_AM_I 0x0D |
scohennm | 4:89ae38dc05a9 | 36 | #define XYZ_DATA_CFG 0x0E |
scohennm | 4:89ae38dc05a9 | 37 | |
scohennm | 4:89ae38dc05a9 | 38 | #define MAX_2G 0x00 |
scohennm | 4:89ae38dc05a9 | 39 | #define MAX_4G 0x01 |
scohennm | 4:89ae38dc05a9 | 40 | #define MAX_8G 0x02 |
scohennm | 4:89ae38dc05a9 | 41 | |
scohennm | 4:89ae38dc05a9 | 42 | |
scohennm | 4:89ae38dc05a9 | 43 | #define PROGNAME "LCD_punch_mtr 8 v5\r/n" |
scohennm | 4:89ae38dc05a9 | 44 | #define LCDNAME "PC.V5" |
scohennm | 4:89ae38dc05a9 | 45 | |
scohennm | 5:6ec1dac45861 | 46 | |
scohennm | 4:89ae38dc05a9 | 47 | //#define PRINTDBUG |
scohennm | 4:89ae38dc05a9 | 48 | // Accelerometer SPI pins |
scohennm | 4:89ae38dc05a9 | 49 | #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z) |
scohennm | 4:89ae38dc05a9 | 50 | PinName const SDA = PTE25; |
scohennm | 4:89ae38dc05a9 | 51 | PinName const SCL = PTE24; |
scohennm | 4:89ae38dc05a9 | 52 | #elif defined (TARGET_KL05Z) |
scohennm | 4:89ae38dc05a9 | 53 | PinName const SDA = PTB4; |
scohennm | 4:89ae38dc05a9 | 54 | PinName const SCL = PTB3; |
scohennm | 4:89ae38dc05a9 | 55 | #else |
scohennm | 4:89ae38dc05a9 | 56 | #error TARGET NOT DEFINED |
scohennm | 4:89ae38dc05a9 | 57 | #endif |
scohennm | 4:89ae38dc05a9 | 58 | |
scohennm | 4:89ae38dc05a9 | 59 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
scohennm | 4:89ae38dc05a9 | 60 | |
scohennm | 4:89ae38dc05a9 | 61 | SLCD slcd; //define LCD display |
scohennm | 4:89ae38dc05a9 | 62 | Timer blinkTimer; |
scohennm | 4:89ae38dc05a9 | 63 | Timer dataTimer; |
scohennm | 4:89ae38dc05a9 | 64 | Timer displayTimer; |
scohennm | 4:89ae38dc05a9 | 65 | MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); |
scohennm | 4:89ae38dc05a9 | 66 | AnalogIn TMP01(PTB1); |
scohennm | 4:89ae38dc05a9 | 67 | DigitalOut LEDs[NUMLEDS]={LED_RED, LED_GREEN}; //Indicator LEDs |
scohennm | 4:89ae38dc05a9 | 68 | Serial pc(USBTX, USBRX); |
scohennm | 4:89ae38dc05a9 | 69 | char displayTitles[NUMSTATES][LCDLEN] = {XCOMP,YCOMP,ZCOMP,MAXVECT}; |
scohennm | 4:89ae38dc05a9 | 70 | float accaxisdata[NUMSTATES]; |
scohennm | 4:89ae38dc05a9 | 71 | char displayformats[NUMSTATES][LCDLEN] = {"x.%3.2f","y.%3.2f","z.%3.2f","v.%3.2f"}; |
scohennm | 4:89ae38dc05a9 | 72 | //char displayformats[NUMSTATES][LCDLEN] = {"%4.3f","%4.3f","%4.3f","%4.3f"}; |
sphasim | 6:c2ff55220a05 | 73 | #include "mbed.h" |
sphasim | 6:c2ff55220a05 | 74 | #include "MMA8451Q8.h" |
sphasim | 6:c2ff55220a05 | 75 | #include "SLCD.h" |
sphasim | 6:c2ff55220a05 | 76 | /* |
sphasim | 6:c2ff55220a05 | 77 | Test of the accelerometer, digital I/O, on-board LCD screen, and 16-bit ADC. |
sphasim | 6:c2ff55220a05 | 78 | Looing at vector product of the x-y components of the accelerometer. |
sphasim | 6:c2ff55220a05 | 79 | Works pretty well. Still rough, program wise - sc 140710 |
sphasim | 6:c2ff55220a05 | 80 | Addiing touch sensor to replace potentiometer. |
sphasim | 6:c2ff55220a05 | 81 | Show x and y components |
sphasim | 6:c2ff55220a05 | 82 | */ |
scohennm | 4:89ae38dc05a9 | 83 | |
sphasim | 6:c2ff55220a05 | 84 | #define BLINKTIME 0.7 |
sphasim | 6:c2ff55220a05 | 85 | #define DATATIME 0.1 |
sphasim | 6:c2ff55220a05 | 86 | #define DATADISPDWELL 0.2 |
sphasim | 6:c2ff55220a05 | 87 | #define NUMLEDS 2 |
sphasim | 6:c2ff55220a05 | 88 | #define LEDON 0 |
sphasim | 6:c2ff55220a05 | 89 | #define LEDOFF 1 |
sphasim | 6:c2ff55220a05 | 90 | #define SCALING 13 |
sphasim | 6:c2ff55220a05 | 91 | #define LCDLEN 10 |
sphasim | 6:c2ff55220a05 | 92 | #define RSTARTMESS "PNCH" |
sphasim | 6:c2ff55220a05 | 93 | #define MAXVECT "MAXV" |
sphasim | 6:c2ff55220a05 | 94 | #define XCOMP "XCMP" |
sphasim | 6:c2ff55220a05 | 95 | #define YCOMP "YCMP" |
sphasim | 6:c2ff55220a05 | 96 | #define ZCOMP "ZCMP" |
sphasim | 6:c2ff55220a05 | 97 | #define ANALTOVOLTS 3.3 |
sphasim | 6:c2ff55220a05 | 98 | #define LEDDELAY 0.400 |
sphasim | 6:c2ff55220a05 | 99 | //define states |
sphasim | 6:c2ff55220a05 | 100 | #define NUMSTATES 4 |
sphasim | 6:c2ff55220a05 | 101 | #define XCOMPD 0 |
sphasim | 6:c2ff55220a05 | 102 | #define YCOMPD 1 |
sphasim | 6:c2ff55220a05 | 103 | #define ZCOMPD 2 |
sphasim | 6:c2ff55220a05 | 104 | #define VMAXD 3 |
sphasim | 6:c2ff55220a05 | 105 | #define MAXGS 8 |
sphasim | 6:c2ff55220a05 | 106 | #define COUNTSCALE 1 |
sphasim | 6:c2ff55220a05 | 107 | #define REG_WHO_AM_I 0x0D |
sphasim | 6:c2ff55220a05 | 108 | #define XYZ_DATA_CFG 0x0E |
sphasim | 6:c2ff55220a05 | 109 | |
sphasim | 6:c2ff55220a05 | 110 | #define REG_OUT_X_MSB 0x01 |
sphasim | 6:c2ff55220a05 | 111 | #define REG_OUT_Y_MSB 0x03 |
sphasim | 6:c2ff55220a05 | 112 | #define REG_OUT_Z_MSB 0x05 |
sphasim | 6:c2ff55220a05 | 113 | |
sphasim | 6:c2ff55220a05 | 114 | #define MAX_2G 0x00 |
sphasim | 6:c2ff55220a05 | 115 | #define MAX_4G 0x01 |
sphasim | 6:c2ff55220a05 | 116 | #define MAX_8G 0x02 |
sphasim | 6:c2ff55220a05 | 117 | |
sphasim | 6:c2ff55220a05 | 118 | |
sphasim | 6:c2ff55220a05 | 119 | #define PROGNAME "LCD_punch_mtr 8 v5\r/n" |
sphasim | 6:c2ff55220a05 | 120 | #define LCDNAME "PC.V5" |
sphasim | 6:c2ff55220a05 | 121 | |
sphasim | 6:c2ff55220a05 | 122 | |
sphasim | 6:c2ff55220a05 | 123 | //#define PRINTDBUG |
sphasim | 6:c2ff55220a05 | 124 | // Accelerometer SPI pins |
sphasim | 6:c2ff55220a05 | 125 | #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z) |
sphasim | 6:c2ff55220a05 | 126 | PinName const SDA = PTE25; |
sphasim | 6:c2ff55220a05 | 127 | PinName const SCL = PTE24; |
sphasim | 6:c2ff55220a05 | 128 | #elif defined (TARGET_KL05Z) |
sphasim | 6:c2ff55220a05 | 129 | PinName const SDA = PTB4; |
sphasim | 6:c2ff55220a05 | 130 | PinName const SCL = PTB3; |
sphasim | 6:c2ff55220a05 | 131 | #else |
sphasim | 6:c2ff55220a05 | 132 | #error TARGET NOT DEFINED |
sphasim | 6:c2ff55220a05 | 133 | #endif |
sphasim | 6:c2ff55220a05 | 134 | |
sphasim | 6:c2ff55220a05 | 135 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
sphasim | 6:c2ff55220a05 | 136 | |
sphasim | 6:c2ff55220a05 | 137 | SLCD slcd; //define LCD display |
sphasim | 6:c2ff55220a05 | 138 | Timer blinkTimer; |
sphasim | 6:c2ff55220a05 | 139 | Timer dataTimer; |
sphasim | 6:c2ff55220a05 | 140 | Timer displayTimer; |
sphasim | 6:c2ff55220a05 | 141 | MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); |
sphasim | 6:c2ff55220a05 | 142 | AnalogIn TMP01(PTB1); |
sphasim | 6:c2ff55220a05 | 143 | DigitalOut LEDs[NUMLEDS]={LED_RED, LED_GREEN}; //Indicator LEDs |
sphasim | 6:c2ff55220a05 | 144 | Serial pc(USBTX, USBRX); |
sphasim | 6:c2ff55220a05 | 145 | char displayTitles[NUMSTATES][LCDLEN] = {XCOMP,YCOMP,ZCOMP,MAXVECT}; |
sphasim | 6:c2ff55220a05 | 146 | float accaxisdata[NUMSTATES]; |
sphasim | 6:c2ff55220a05 | 147 | char displayformats[NUMSTATES][LCDLEN] = {"%4.0f","y.%3.2f","z.%3.2f","v.%3.2f"}; |
sphasim | 6:c2ff55220a05 | 148 | |
sphasim | 6:c2ff55220a05 | 149 | void LCDMess(char *lMess, float dWait){ |
sphasim | 6:c2ff55220a05 | 150 | slcd.Home(); |
sphasim | 6:c2ff55220a05 | 151 | slcd.clear(); |
sphasim | 6:c2ff55220a05 | 152 | slcd.printf(lMess); |
sphasim | 6:c2ff55220a05 | 153 | wait(dWait); |
sphasim | 6:c2ff55220a05 | 154 | } |
sphasim | 6:c2ff55220a05 | 155 | void LCDMessNoDwell(char *lMess){ |
sphasim | 6:c2ff55220a05 | 156 | slcd.Home(); |
sphasim | 6:c2ff55220a05 | 157 | slcd.clear(); |
sphasim | 6:c2ff55220a05 | 158 | slcd.printf(lMess); |
sphasim | 6:c2ff55220a05 | 159 | } |
sphasim | 6:c2ff55220a05 | 160 | |
sphasim | 6:c2ff55220a05 | 161 | |
sphasim | 6:c2ff55220a05 | 162 | void allLEDsOFF(int numberOfLEDS) { |
sphasim | 6:c2ff55220a05 | 163 | int i; |
sphasim | 6:c2ff55220a05 | 164 | for (i=0;i<numberOfLEDS; i++){ |
sphasim | 6:c2ff55220a05 | 165 | LEDs[i] = LEDOFF; |
sphasim | 6:c2ff55220a05 | 166 | } |
sphasim | 6:c2ff55220a05 | 167 | |
sphasim | 6:c2ff55220a05 | 168 | } |
sphasim | 6:c2ff55220a05 | 169 | |
sphasim | 6:c2ff55220a05 | 170 | void runLEDs(int numberOfLEDS) { |
sphasim | 6:c2ff55220a05 | 171 | int i; |
sphasim | 6:c2ff55220a05 | 172 | for (i=0;i<numberOfLEDS; i++){ |
sphasim | 6:c2ff55220a05 | 173 | LEDs[i] = LEDON; |
sphasim | 6:c2ff55220a05 | 174 | wait(0.2); |
sphasim | 6:c2ff55220a05 | 175 | } |
sphasim | 6:c2ff55220a05 | 176 | allLEDsOFF(numberOfLEDS); |
sphasim | 6:c2ff55220a05 | 177 | } |
sphasim | 6:c2ff55220a05 | 178 | |
sphasim | 6:c2ff55220a05 | 179 | |
sphasim | 6:c2ff55220a05 | 180 | |
sphasim | 6:c2ff55220a05 | 181 | int main() { |
sphasim | 6:c2ff55220a05 | 182 | |
sphasim | 6:c2ff55220a05 | 183 | int RButtonState; |
sphasim | 6:c2ff55220a05 | 184 | int LButtonState; |
sphasim | 6:c2ff55220a05 | 185 | DigitalIn RtButton(PTC12); |
sphasim | 6:c2ff55220a05 | 186 | DigitalIn LftButton(PTC3); |
sphasim | 6:c2ff55220a05 | 187 | int displayState = XCOMPD; |
sphasim | 6:c2ff55220a05 | 188 | float xAcc = 0.0; |
sphasim | 6:c2ff55220a05 | 189 | float yAcc = 0.0; |
sphasim | 6:c2ff55220a05 | 190 | float zAcc = 0.0; |
sphasim | 6:c2ff55220a05 | 191 | float vector; |
sphasim | 6:c2ff55220a05 | 192 | float vMax = 0.0; |
sphasim | 6:c2ff55220a05 | 193 | float DisplayTime = DATADISPDWELL; |
sphasim | 6:c2ff55220a05 | 194 | float LEDDwell = BLINKTIME; |
sphasim | 6:c2ff55220a05 | 195 | int outState = false; |
sphasim | 6:c2ff55220a05 | 196 | char lcdData[LCDLEN]; //buffer needs places dor decimal pt and colon |
sphasim | 6:c2ff55220a05 | 197 | uint8_t regData = MAX_4G; // test value must change after G setting |
sphasim | 6:c2ff55220a05 | 198 | int16_t xCounts; |
sphasim | 6:c2ff55220a05 | 199 | int16_t yCounts; |
sphasim | 6:c2ff55220a05 | 200 | int16_t zCounts; |
sphasim | 6:c2ff55220a05 | 201 | |
sphasim | 6:c2ff55220a05 | 202 | #ifdef PRINTDBUG |
sphasim | 6:c2ff55220a05 | 203 | pc.printf(PROGNAME); |
sphasim | 6:c2ff55220a05 | 204 | #endif |
sphasim | 6:c2ff55220a05 | 205 | LCDMess(LCDNAME, BLINKTIME); |
sphasim | 6:c2ff55220a05 | 206 | // Initialze readings and sequence the LED's for dramtic effect. |
sphasim | 6:c2ff55220a05 | 207 | allLEDsOFF(NUMLEDS); |
sphasim | 6:c2ff55220a05 | 208 | blinkTimer.start(); |
sphasim | 6:c2ff55220a05 | 209 | blinkTimer.reset(); |
sphasim | 6:c2ff55220a05 | 210 | displayTimer.start(); |
sphasim | 6:c2ff55220a05 | 211 | displayTimer.reset(); |
sphasim | 6:c2ff55220a05 | 212 | dataTimer.start(); |
sphasim | 6:c2ff55220a05 | 213 | dataTimer.reset(); |
sphasim | 6:c2ff55220a05 | 214 | LCDMess(RSTARTMESS, BLINKTIME); |
sphasim | 6:c2ff55220a05 | 215 | // Go into registers ofn Accelerometer, change sensitivity then test. |
sphasim | 6:c2ff55220a05 | 216 | // http://cache.freescale.com/files/sensors/doc/data_sheet/MMA8451Q.pdf |
sphasim | 6:c2ff55220a05 | 217 | // See page 26 |
sphasim | 6:c2ff55220a05 | 218 | // Set 2 g max limit ****************** Note limits |
sphasim | 6:c2ff55220a05 | 219 | acc.setGLimit(MAX_2G); // For now set to 2g |
sphasim | 6:c2ff55220a05 | 220 | acc.readRegs(XYZ_DATA_CFG, ®Data, 1); |
sphasim | 6:c2ff55220a05 | 221 | sprintf (lcdData,"%d",regData); |
sphasim | 6:c2ff55220a05 | 222 | LCDMess(lcdData,BLINKTIME); |
sphasim | 6:c2ff55220a05 | 223 | acc.readRegs(REG_WHO_AM_I, ®Data, 1); |
sphasim | 6:c2ff55220a05 | 224 | sprintf (lcdData,"%d",regData); |
sphasim | 6:c2ff55220a05 | 225 | LCDMess(lcdData,BLINKTIME); |
sphasim | 6:c2ff55220a05 | 226 | // main loop forever |
sphasim | 6:c2ff55220a05 | 227 | while(true) { |
sphasim | 6:c2ff55220a05 | 228 | |
sphasim | 6:c2ff55220a05 | 229 | // Handle user input for display selections |
sphasim | 6:c2ff55220a05 | 230 | RButtonState = !RtButton; // button is pulled up so false is when button is pushed it's inverted to avoid confusion downstream |
sphasim | 6:c2ff55220a05 | 231 | if (RButtonState){ |
sphasim | 6:c2ff55220a05 | 232 | vMax = 0.0; // Clear vMax |
sphasim | 6:c2ff55220a05 | 233 | LCDMess(RSTARTMESS, BLINKTIME); |
sphasim | 6:c2ff55220a05 | 234 | } |
sphasim | 6:c2ff55220a05 | 235 | LButtonState = !LftButton; |
sphasim | 6:c2ff55220a05 | 236 | if (LButtonState) { //Change data that is displayed |
sphasim | 6:c2ff55220a05 | 237 | displayState = (displayState + 1) % NUMSTATES; |
sphasim | 6:c2ff55220a05 | 238 | // Change to switch/case soon. |
sphasim | 6:c2ff55220a05 | 239 | LCDMess(displayTitles[displayState],BLINKTIME); |
sphasim | 6:c2ff55220a05 | 240 | } |
sphasim | 6:c2ff55220a05 | 241 | |
sphasim | 6:c2ff55220a05 | 242 | // -------------------------------------------- |
sphasim | 6:c2ff55220a05 | 243 | while (dataTimer.read() > DATATIME){ |
sphasim | 6:c2ff55220a05 | 244 | |
sphasim | 6:c2ff55220a05 | 245 | // No offset |
sphasim | 6:c2ff55220a05 | 246 | xAcc = abs(acc.getAccX()); |
sphasim | 6:c2ff55220a05 | 247 | xCounts = acc.getAccAxis(REG_OUT_X_MSB); |
sphasim | 6:c2ff55220a05 | 248 | yAcc = abs(acc.getAccY()); |
sphasim | 6:c2ff55220a05 | 249 | yCounts = acc.getAccAxis(REG_OUT_Y_MSB); |
sphasim | 6:c2ff55220a05 | 250 | zAcc = abs(acc.getAccZ()); |
sphasim | 6:c2ff55220a05 | 251 | zCounts = acc.getAccAxis(REG_OUT_Z_MSB); |
sphasim | 6:c2ff55220a05 | 252 | // Calulate vector sum of x,y and z reading. |
sphasim | 6:c2ff55220a05 | 253 | vector = sqrt(pow(xAcc,2) + pow(zAcc,2)); |
sphasim | 6:c2ff55220a05 | 254 | vector = zAcc; |
sphasim | 6:c2ff55220a05 | 255 | if (vector > vMax) { |
sphasim | 6:c2ff55220a05 | 256 | vMax = vector; |
sphasim | 6:c2ff55220a05 | 257 | } |
sphasim | 6:c2ff55220a05 | 258 | //Prepare data for LCD display |
sphasim | 6:c2ff55220a05 | 259 | accaxisdata[XCOMPD] = abs((float)xCounts/COUNTSCALE); // scalling is set to 1 at this point |
sphasim | 6:c2ff55220a05 | 260 | accaxisdata[YCOMPD] = abs((float)yCounts/COUNTSCALE); |
sphasim | 6:c2ff55220a05 | 261 | accaxisdata[ZCOMPD] = abs((float)zCounts/COUNTSCALE); |
sphasim | 6:c2ff55220a05 | 262 | accaxisdata[VMAXD] = vMax; |
sphasim | 6:c2ff55220a05 | 263 | dataTimer.reset(); |
sphasim | 6:c2ff55220a05 | 264 | LEDDwell = 1.1 - vMax/MAXGS; |
sphasim | 6:c2ff55220a05 | 265 | } |
sphasim | 6:c2ff55220a05 | 266 | |
sphasim | 6:c2ff55220a05 | 267 | // Display the appropriate data on the LCD based upon what mode was chosen |
sphasim | 6:c2ff55220a05 | 268 | while (displayTimer.read() > DisplayTime){ |
sphasim | 6:c2ff55220a05 | 269 | sprintf (lcdData,displayformats[displayState],accaxisdata[displayState]); |
sphasim | 6:c2ff55220a05 | 270 | LCDMessNoDwell(lcdData); |
sphasim | 6:c2ff55220a05 | 271 | displayTimer.reset(); |
sphasim | 6:c2ff55220a05 | 272 | } // displaytimer |
sphasim | 6:c2ff55220a05 | 273 | // Wait then do the whole thing again. |
sphasim | 6:c2ff55220a05 | 274 | // Alive LEDs |
sphasim | 6:c2ff55220a05 | 275 | while(blinkTimer.read()> LEDDwell) { |
sphasim | 6:c2ff55220a05 | 276 | LEDs[0].write(outState); |
sphasim | 6:c2ff55220a05 | 277 | LEDs[1].write(!outState); |
sphasim | 6:c2ff55220a05 | 278 | outState = !outState; |
sphasim | 6:c2ff55220a05 | 279 | blinkTimer.reset(); |
sphasim | 6:c2ff55220a05 | 280 | } |
sphasim | 6:c2ff55220a05 | 281 | }//forever loop |
sphasim | 6:c2ff55220a05 | 282 | }// main |
scohennm | 4:89ae38dc05a9 | 283 | void LCDMess(char *lMess, float dWait){ |
scohennm | 4:89ae38dc05a9 | 284 | slcd.Home(); |
scohennm | 4:89ae38dc05a9 | 285 | slcd.clear(); |
scohennm | 4:89ae38dc05a9 | 286 | slcd.printf(lMess); |
scohennm | 4:89ae38dc05a9 | 287 | wait(dWait); |
scohennm | 4:89ae38dc05a9 | 288 | } |
scohennm | 4:89ae38dc05a9 | 289 | void LCDMessNoDwell(char *lMess){ |
scohennm | 4:89ae38dc05a9 | 290 | slcd.Home(); |
scohennm | 4:89ae38dc05a9 | 291 | slcd.clear(); |
scohennm | 4:89ae38dc05a9 | 292 | slcd.printf(lMess); |
scohennm | 4:89ae38dc05a9 | 293 | } |
scohennm | 4:89ae38dc05a9 | 294 | |
scohennm | 4:89ae38dc05a9 | 295 | |
scohennm | 4:89ae38dc05a9 | 296 | void allLEDsOFF(int numberOfLEDS) { |
scohennm | 4:89ae38dc05a9 | 297 | int i; |
scohennm | 4:89ae38dc05a9 | 298 | for (i=0;i<numberOfLEDS; i++){ |
scohennm | 4:89ae38dc05a9 | 299 | LEDs[i] = LEDOFF; |
scohennm | 4:89ae38dc05a9 | 300 | } |
scohennm | 4:89ae38dc05a9 | 301 | |
scohennm | 4:89ae38dc05a9 | 302 | } |
scohennm | 4:89ae38dc05a9 | 303 | |
scohennm | 4:89ae38dc05a9 | 304 | void runLEDs(int numberOfLEDS) { |
scohennm | 4:89ae38dc05a9 | 305 | int i; |
scohennm | 4:89ae38dc05a9 | 306 | for (i=0;i<numberOfLEDS; i++){ |
scohennm | 4:89ae38dc05a9 | 307 | LEDs[i] = LEDON; |
scohennm | 4:89ae38dc05a9 | 308 | wait(0.2); |
scohennm | 4:89ae38dc05a9 | 309 | } |
scohennm | 4:89ae38dc05a9 | 310 | allLEDsOFF(numberOfLEDS); |
scohennm | 4:89ae38dc05a9 | 311 | } |
scohennm | 4:89ae38dc05a9 | 312 | |
scohennm | 4:89ae38dc05a9 | 313 | |
scohennm | 4:89ae38dc05a9 | 314 | |
scohennm | 4:89ae38dc05a9 | 315 | int main() { |
scohennm | 4:89ae38dc05a9 | 316 | |
scohennm | 4:89ae38dc05a9 | 317 | int RButtonState; |
scohennm | 4:89ae38dc05a9 | 318 | int LButtonState; |
scohennm | 4:89ae38dc05a9 | 319 | DigitalIn RtButton(PTC12); |
scohennm | 4:89ae38dc05a9 | 320 | DigitalIn LftButton(PTC3); |
scohennm | 4:89ae38dc05a9 | 321 | int displayState = XCOMPD; |
scohennm | 4:89ae38dc05a9 | 322 | float xAcc = 0.0; |
scohennm | 4:89ae38dc05a9 | 323 | float yAcc = 0.0; |
scohennm | 4:89ae38dc05a9 | 324 | float zAcc = 0.0; |
scohennm | 4:89ae38dc05a9 | 325 | float vector; |
scohennm | 4:89ae38dc05a9 | 326 | float vMax = 0.0; |
scohennm | 4:89ae38dc05a9 | 327 | float DisplayTime = DATADISPDWELL; |
scohennm | 4:89ae38dc05a9 | 328 | float LEDDwell = BLINKTIME; |
scohennm | 4:89ae38dc05a9 | 329 | int outState = false; |
scohennm | 4:89ae38dc05a9 | 330 | char lcdData[LCDLEN]; //buffer needs places dor decimal pt and colon |
scohennm | 4:89ae38dc05a9 | 331 | uint8_t regData = MAX_4G; // test value must change after G setting |
scohennm | 4:89ae38dc05a9 | 332 | |
scohennm | 4:89ae38dc05a9 | 333 | #ifdef PRINTDBUG |
scohennm | 4:89ae38dc05a9 | 334 | pc.printf(PROGNAME); |
scohennm | 4:89ae38dc05a9 | 335 | #endif |
scohennm | 4:89ae38dc05a9 | 336 | LCDMess(LCDNAME, BLINKTIME); |
scohennm | 4:89ae38dc05a9 | 337 | // Initialze readings and sequence the LED's for dramtic effect. |
scohennm | 4:89ae38dc05a9 | 338 | allLEDsOFF(NUMLEDS); |
scohennm | 4:89ae38dc05a9 | 339 | blinkTimer.start(); |
scohennm | 4:89ae38dc05a9 | 340 | blinkTimer.reset(); |
scohennm | 4:89ae38dc05a9 | 341 | displayTimer.start(); |
scohennm | 4:89ae38dc05a9 | 342 | displayTimer.reset(); |
scohennm | 4:89ae38dc05a9 | 343 | dataTimer.start(); |
scohennm | 4:89ae38dc05a9 | 344 | dataTimer.reset(); |
scohennm | 4:89ae38dc05a9 | 345 | LCDMess(RSTARTMESS, BLINKTIME); |
scohennm | 5:6ec1dac45861 | 346 | // Go into registers ofn Accelerometer, change sensitivity then test. |
scohennm | 5:6ec1dac45861 | 347 | // http://cache.freescale.com/files/sensors/doc/data_sheet/MMA8451Q.pdf |
scohennm | 5:6ec1dac45861 | 348 | // See page 26 |
scohennm | 5:6ec1dac45861 | 349 | // Set 8 g max limit |
sphasim | 6:c2ff55220a05 | 350 | acc.setGLimit(MAX_2G); // For now set to 8g |
scohennm | 4:89ae38dc05a9 | 351 | acc.readRegs(XYZ_DATA_CFG, ®Data, 1); |
scohennm | 4:89ae38dc05a9 | 352 | sprintf (lcdData,"%d",regData); |
scohennm | 4:89ae38dc05a9 | 353 | LCDMess(lcdData,BLINKTIME); |
scohennm | 4:89ae38dc05a9 | 354 | acc.readRegs(REG_WHO_AM_I, ®Data, 1); |
scohennm | 4:89ae38dc05a9 | 355 | sprintf (lcdData,"%d",regData); |
scohennm | 4:89ae38dc05a9 | 356 | LCDMess(lcdData,BLINKTIME); |
scohennm | 4:89ae38dc05a9 | 357 | // main loop forever |
scohennm | 4:89ae38dc05a9 | 358 | while(true) { |
scohennm | 4:89ae38dc05a9 | 359 | |
scohennm | 4:89ae38dc05a9 | 360 | // Handle user input for display selections |
scohennm | 4:89ae38dc05a9 | 361 | RButtonState = !RtButton; // button is pulled up so false is when button is pushed it's inverted to avoid confusion downstream |
scohennm | 4:89ae38dc05a9 | 362 | if (RButtonState){ |
scohennm | 4:89ae38dc05a9 | 363 | vMax = 0.0; // Clear vMax |
scohennm | 4:89ae38dc05a9 | 364 | LCDMess(RSTARTMESS, BLINKTIME); |
scohennm | 4:89ae38dc05a9 | 365 | } |
scohennm | 4:89ae38dc05a9 | 366 | LButtonState = !LftButton; |
scohennm | 4:89ae38dc05a9 | 367 | if (LButtonState) { //Change data that is displayed |
scohennm | 4:89ae38dc05a9 | 368 | displayState = (displayState + 1) % NUMSTATES; |
scohennm | 4:89ae38dc05a9 | 369 | // Change to switch/case soon. |
scohennm | 4:89ae38dc05a9 | 370 | LCDMess(displayTitles[displayState],BLINKTIME); |
scohennm | 4:89ae38dc05a9 | 371 | } |
scohennm | 4:89ae38dc05a9 | 372 | |
scohennm | 4:89ae38dc05a9 | 373 | // -------------------------------------------- |
scohennm | 4:89ae38dc05a9 | 374 | while (dataTimer.read() > DATATIME){ |
scohennm | 4:89ae38dc05a9 | 375 | //Get accelerometer data - tilt angles minus offset for zero mark. |
scohennm | 4:89ae38dc05a9 | 376 | // No offset |
scohennm | 4:89ae38dc05a9 | 377 | xAcc = abs(acc.getAccX()); |
scohennm | 4:89ae38dc05a9 | 378 | yAcc = abs(acc.getAccY()); |
scohennm | 4:89ae38dc05a9 | 379 | zAcc = abs(acc.getAccZ()); |
scohennm | 4:89ae38dc05a9 | 380 | // Calulate vector sum of x,y and z reading. |
scohennm | 4:89ae38dc05a9 | 381 | vector = sqrt(pow(xAcc,2) + pow(zAcc,2)); |
scohennm | 4:89ae38dc05a9 | 382 | vector = zAcc; |
scohennm | 4:89ae38dc05a9 | 383 | if (vector > vMax) { |
scohennm | 4:89ae38dc05a9 | 384 | vMax = vector; |
scohennm | 4:89ae38dc05a9 | 385 | } |
scohennm | 4:89ae38dc05a9 | 386 | accaxisdata[XCOMPD] = xAcc; |
scohennm | 4:89ae38dc05a9 | 387 | accaxisdata[YCOMPD] = yAcc; |
scohennm | 4:89ae38dc05a9 | 388 | accaxisdata[ZCOMPD] = zAcc; |
scohennm | 4:89ae38dc05a9 | 389 | accaxisdata[VMAXD] = vMax; |
scohennm | 4:89ae38dc05a9 | 390 | dataTimer.reset(); |
scohennm | 4:89ae38dc05a9 | 391 | LEDDwell = 1.1 - vMax/MAXGS; |
scohennm | 4:89ae38dc05a9 | 392 | } |
scohennm | 4:89ae38dc05a9 | 393 | #ifdef PRINTDBUG |
scohennm | 4:89ae38dc05a9 | 394 | pc.printf("vector = %f\r\n", xAcc); |
scohennm | 4:89ae38dc05a9 | 395 | pc.printf("scaling = %f\r\n", scaleExpansion); |
scohennm | 4:89ae38dc05a9 | 396 | pc.printf("RawTemp = %f\r\n", FDeg); |
scohennm | 4:89ae38dc05a9 | 397 | #endif |
scohennm | 4:89ae38dc05a9 | 398 | // Display the appropriate data on the LCD based upon what mode was chosen |
scohennm | 4:89ae38dc05a9 | 399 | while (displayTimer.read() > DisplayTime){ |
scohennm | 4:89ae38dc05a9 | 400 | sprintf (lcdData,displayformats[displayState],accaxisdata[displayState]); |
scohennm | 4:89ae38dc05a9 | 401 | LCDMessNoDwell(lcdData); |
scohennm | 4:89ae38dc05a9 | 402 | displayTimer.reset(); |
scohennm | 4:89ae38dc05a9 | 403 | } // displaytimer |
scohennm | 4:89ae38dc05a9 | 404 | // Wait then do the whole thing again. |
scohennm | 4:89ae38dc05a9 | 405 | // Alive LEDs |
scohennm | 4:89ae38dc05a9 | 406 | while(blinkTimer.read()> LEDDwell) { |
scohennm | 4:89ae38dc05a9 | 407 | LEDs[0].write(outState); |
scohennm | 4:89ae38dc05a9 | 408 | LEDs[1].write(!outState); |
scohennm | 4:89ae38dc05a9 | 409 | outState = !outState; |
scohennm | 4:89ae38dc05a9 | 410 | blinkTimer.reset(); |
scohennm | 4:89ae38dc05a9 | 411 | } |
scohennm | 4:89ae38dc05a9 | 412 | }//forever loop |
scohennm | 4:89ae38dc05a9 | 413 | }// main |