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: MMA8452 N5110 PowerControl mbed
main.cpp@0:28fa9993bcf9, 2015-05-11 (annotated)
- Committer:
- ml13emds
- Date:
- Mon May 11 03:38:15 2015 +0000
- Revision:
- 0:28fa9993bcf9
Final version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ml13emds | 0:28fa9993bcf9 | 1 | /** |
ml13emds | 0:28fa9993bcf9 | 2 | @file main.cpp |
ml13emds | 0:28fa9993bcf9 | 3 | |
ml13emds | 0:28fa9993bcf9 | 4 | @brief Pedometer implementation using the sensor Triple Axis Accelerometer Breakout - MMA8452Q |
ml13emds | 0:28fa9993bcf9 | 5 | |
ml13emds | 0:28fa9993bcf9 | 6 | */ |
ml13emds | 0:28fa9993bcf9 | 7 | |
ml13emds | 0:28fa9993bcf9 | 8 | #include "main.h" |
ml13emds | 0:28fa9993bcf9 | 9 | |
ml13emds | 0:28fa9993bcf9 | 10 | int main() |
ml13emds | 0:28fa9993bcf9 | 11 | { |
ml13emds | 0:28fa9993bcf9 | 12 | /// Finite State Machine that cotrols the whole system |
ml13emds | 0:28fa9993bcf9 | 13 | //Setting the initial state. Avoiding rubish values |
ml13emds | 0:28fa9993bcf9 | 14 | state = 0; |
ml13emds | 0:28fa9993bcf9 | 15 | I1_flag = 0; |
ml13emds | 0:28fa9993bcf9 | 16 | I2_flag = 0; |
ml13emds | 0:28fa9993bcf9 | 17 | |
ml13emds | 0:28fa9993bcf9 | 18 | // Adressing the rise of pin 15(which is connected to I1 of Sensor)to the interruption routine 1 |
ml13emds | 0:28fa9993bcf9 | 19 | I1.rise(&Interrupt); |
ml13emds | 0:28fa9993bcf9 | 20 | |
ml13emds | 0:28fa9993bcf9 | 21 | // Adressing the rise of pin 16(which is connected to I2 of Sensor)to the interruption routine 2 |
ml13emds | 0:28fa9993bcf9 | 22 | I2.rise(&Interrupt2); |
ml13emds | 0:28fa9993bcf9 | 23 | |
ml13emds | 0:28fa9993bcf9 | 24 | while(1) |
ml13emds | 0:28fa9993bcf9 | 25 | { |
ml13emds | 0:28fa9993bcf9 | 26 | // Main state machine |
ml13emds | 0:28fa9993bcf9 | 27 | switch (state) |
ml13emds | 0:28fa9993bcf9 | 28 | { |
ml13emds | 0:28fa9993bcf9 | 29 | // Keep the device sleeping if no interruption is generated |
ml13emds | 0:28fa9993bcf9 | 30 | case 0: |
ml13emds | 0:28fa9993bcf9 | 31 | { |
ml13emds | 0:28fa9993bcf9 | 32 | wait(0.7); |
ml13emds | 0:28fa9993bcf9 | 33 | // If an interruption is generated, check where it came from |
ml13emds | 0:28fa9993bcf9 | 34 | if(I1_flag) |
ml13emds | 0:28fa9993bcf9 | 35 | { |
ml13emds | 0:28fa9993bcf9 | 36 | // Reading the cause of the interruption |
ml13emds | 0:28fa9993bcf9 | 37 | Int_SourceSystem = mma8452.readByteFromRegister(INT_SOURCE); |
ml13emds | 0:28fa9993bcf9 | 38 | // Checking if the Transient detection is the cause of the interruption generated |
ml13emds | 0:28fa9993bcf9 | 39 | if ((Int_SourceSystem&0x20)==0x20) |
ml13emds | 0:28fa9993bcf9 | 40 | { |
ml13emds | 0:28fa9993bcf9 | 41 | // Clearing the interrupt system for Transient Detection |
ml13emds | 0:28fa9993bcf9 | 42 | Int_SourceTrans = mma8452.readByteFromRegister(TRANSIENT_SRC); |
ml13emds | 0:28fa9993bcf9 | 43 | // Clearing the interrupt system for Pulse Detection |
ml13emds | 0:28fa9993bcf9 | 44 | Int_SourceTrans = mma8452.readByteFromRegister(PULSE_SRC); |
ml13emds | 0:28fa9993bcf9 | 45 | Int_SourceSystem = 0; |
ml13emds | 0:28fa9993bcf9 | 46 | // Wait 200ms to make sure the intrrupt system is cleaned |
ml13emds | 0:28fa9993bcf9 | 47 | wait(0.2); |
ml13emds | 0:28fa9993bcf9 | 48 | I1_flag = 0; |
ml13emds | 0:28fa9993bcf9 | 49 | I2_flag = 0; |
ml13emds | 0:28fa9993bcf9 | 50 | lcd.init(); |
ml13emds | 0:28fa9993bcf9 | 51 | // Choose the normal colour mode |
ml13emds | 0:28fa9993bcf9 | 52 | lcd.normalMode(); |
ml13emds | 0:28fa9993bcf9 | 53 | // Set the LED backlight to 10% |
ml13emds | 0:28fa9993bcf9 | 54 | lcd.setBrightness(0.1); |
ml13emds | 0:28fa9993bcf9 | 55 | // Clear the screen |
ml13emds | 0:28fa9993bcf9 | 56 | lcd.clear(); |
ml13emds | 0:28fa9993bcf9 | 57 | // Starts the execution timer for next Menu |
ml13emds | 0:28fa9993bcf9 | 58 | timer1.attach(&TimerExpired1,20.0); |
ml13emds | 0:28fa9993bcf9 | 59 | timerFlag1 = 0; |
ml13emds | 0:28fa9993bcf9 | 60 | // Going to the initial screen |
ml13emds | 0:28fa9993bcf9 | 61 | state = 1; |
ml13emds | 0:28fa9993bcf9 | 62 | |
ml13emds | 0:28fa9993bcf9 | 63 | } |
ml13emds | 0:28fa9993bcf9 | 64 | } |
ml13emds | 0:28fa9993bcf9 | 65 | // If no interruption is generated, it keeps mbed sleeping and display turned off |
ml13emds | 0:28fa9993bcf9 | 66 | else |
ml13emds | 0:28fa9993bcf9 | 67 | { |
ml13emds | 0:28fa9993bcf9 | 68 | lcd.turnOff(); |
ml13emds | 0:28fa9993bcf9 | 69 | // Standard initialisation used here due to different initialisations settings used through the code |
ml13emds | 0:28fa9993bcf9 | 70 | // Sets the scale to 4g, 100Hz of ODR and set the Transient and Pulse Detection |
ml13emds | 0:28fa9993bcf9 | 71 | mma8452.init(); |
ml13emds | 0:28fa9993bcf9 | 72 | timer1.detach(); |
ml13emds | 0:28fa9993bcf9 | 73 | timer2.detach(); |
ml13emds | 0:28fa9993bcf9 | 74 | timerFlag1 = 0; |
ml13emds | 0:28fa9993bcf9 | 75 | timerFlag2 = 0; |
ml13emds | 0:28fa9993bcf9 | 76 | Sleep(); |
ml13emds | 0:28fa9993bcf9 | 77 | } |
ml13emds | 0:28fa9993bcf9 | 78 | break; |
ml13emds | 0:28fa9993bcf9 | 79 | } |
ml13emds | 0:28fa9993bcf9 | 80 | // Wait for an user command befor the timer counting ends |
ml13emds | 0:28fa9993bcf9 | 81 | case 1: |
ml13emds | 0:28fa9993bcf9 | 82 | { |
ml13emds | 0:28fa9993bcf9 | 83 | lcd.printString("Welcome!",0,0); |
ml13emds | 0:28fa9993bcf9 | 84 | lcd.printString("Tap for graph",0,2); |
ml13emds | 0:28fa9993bcf9 | 85 | lcd.printString("Or",0,3); |
ml13emds | 0:28fa9993bcf9 | 86 | lcd.printString("Shake for",0,4); |
ml13emds | 0:28fa9993bcf9 | 87 | lcd.printString("Counting steps",0,5); |
ml13emds | 0:28fa9993bcf9 | 88 | wait(1); |
ml13emds | 0:28fa9993bcf9 | 89 | |
ml13emds | 0:28fa9993bcf9 | 90 | // Checking if an Transient Detection interrupt is generated (Countinuos shake) |
ml13emds | 0:28fa9993bcf9 | 91 | if(I1_flag) |
ml13emds | 0:28fa9993bcf9 | 92 | { |
ml13emds | 0:28fa9993bcf9 | 93 | // Reading the cause of the interruption |
ml13emds | 0:28fa9993bcf9 | 94 | Int_SourceSystem = mma8452.readByteFromRegister(INT_SOURCE); |
ml13emds | 0:28fa9993bcf9 | 95 | // If the Transient detection is the cause of the interrupt generated |
ml13emds | 0:28fa9993bcf9 | 96 | if ((Int_SourceSystem&0x20)==0x20) |
ml13emds | 0:28fa9993bcf9 | 97 | { |
ml13emds | 0:28fa9993bcf9 | 98 | // Goes to step counting screen |
ml13emds | 0:28fa9993bcf9 | 99 | state = 2; |
ml13emds | 0:28fa9993bcf9 | 100 | } |
ml13emds | 0:28fa9993bcf9 | 101 | } |
ml13emds | 0:28fa9993bcf9 | 102 | |
ml13emds | 0:28fa9993bcf9 | 103 | // Checking if an Pulse Detection interrupt is generated (Just a tap to left or right) |
ml13emds | 0:28fa9993bcf9 | 104 | else if((I2_flag)&&(!I1_flag)) |
ml13emds | 0:28fa9993bcf9 | 105 | { |
ml13emds | 0:28fa9993bcf9 | 106 | // Reading the cause of the interrupt |
ml13emds | 0:28fa9993bcf9 | 107 | Int_SourceSystem = mma8452.readByteFromRegister(INT_SOURCE); |
ml13emds | 0:28fa9993bcf9 | 108 | // If the Pulse detection is the cause of the interrupt generated |
ml13emds | 0:28fa9993bcf9 | 109 | if ((Int_SourceSystem&0x08)==0x08) |
ml13emds | 0:28fa9993bcf9 | 110 | { |
ml13emds | 0:28fa9993bcf9 | 111 | // Clearing the Transient interrupt system |
ml13emds | 0:28fa9993bcf9 | 112 | Int_SourceTrans = mma8452.readByteFromRegister(TRANSIENT_SRC); |
ml13emds | 0:28fa9993bcf9 | 113 | // Clearing the Transient interrupt system |
ml13emds | 0:28fa9993bcf9 | 114 | Int_SourceTrans = mma8452.readByteFromRegister(PULSE_SRC); |
ml13emds | 0:28fa9993bcf9 | 115 | Int_SourceSystem = 0; |
ml13emds | 0:28fa9993bcf9 | 116 | // Wait 200ms to make sure the intrrupt system is cleaned |
ml13emds | 0:28fa9993bcf9 | 117 | wait(0.2); |
ml13emds | 0:28fa9993bcf9 | 118 | I1_flag = 0; |
ml13emds | 0:28fa9993bcf9 | 119 | I2_flag = 0; |
ml13emds | 0:28fa9993bcf9 | 120 | timer1.attach(&TimerExpired1,20.0); |
ml13emds | 0:28fa9993bcf9 | 121 | timerFlag1 = 0; |
ml13emds | 0:28fa9993bcf9 | 122 | lcd.clear(); |
ml13emds | 0:28fa9993bcf9 | 123 | // Going to the Km/day graph screen |
ml13emds | 0:28fa9993bcf9 | 124 | state = 3; |
ml13emds | 0:28fa9993bcf9 | 125 | } |
ml13emds | 0:28fa9993bcf9 | 126 | } |
ml13emds | 0:28fa9993bcf9 | 127 | // If no interrupt is generated and the timer finishes the counting, turn off the device again |
ml13emds | 0:28fa9993bcf9 | 128 | else if(timerFlag1) |
ml13emds | 0:28fa9993bcf9 | 129 | { |
ml13emds | 0:28fa9993bcf9 | 130 | timerFlag1 = 0; |
ml13emds | 0:28fa9993bcf9 | 131 | Int_SourceTrans = mma8452.readByteFromRegister(TRANSIENT_SRC); |
ml13emds | 0:28fa9993bcf9 | 132 | Int_SourceTrans = mma8452.readByteFromRegister(PULSE_SRC); |
ml13emds | 0:28fa9993bcf9 | 133 | Int_SourceSystem = 0; |
ml13emds | 0:28fa9993bcf9 | 134 | wait(0.2); |
ml13emds | 0:28fa9993bcf9 | 135 | I1_flag = 0; |
ml13emds | 0:28fa9993bcf9 | 136 | I2_flag = 0; |
ml13emds | 0:28fa9993bcf9 | 137 | state = 0; |
ml13emds | 0:28fa9993bcf9 | 138 | } |
ml13emds | 0:28fa9993bcf9 | 139 | break; |
ml13emds | 0:28fa9993bcf9 | 140 | } |
ml13emds | 0:28fa9993bcf9 | 141 | // Steps counting Screen. Also save data. |
ml13emds | 0:28fa9993bcf9 | 142 | case 2: |
ml13emds | 0:28fa9993bcf9 | 143 | { |
ml13emds | 0:28fa9993bcf9 | 144 | // Changes the scale to 2g, the ODR to 800Hz and set the output data to be read from the High Pass Filter |
ml13emds | 0:28fa9993bcf9 | 145 | mma8452.transient_counting(); |
ml13emds | 0:28fa9993bcf9 | 146 | lcd.clear(); |
ml13emds | 0:28fa9993bcf9 | 147 | buzzer = 0.5; |
ml13emds | 0:28fa9993bcf9 | 148 | lcd.printString("Calibrating...",0,0); |
ml13emds | 0:28fa9993bcf9 | 149 | wait(5); |
ml13emds | 0:28fa9993bcf9 | 150 | // Take a average of the 50 values of the device before counting steps. Kind of calibration |
ml13emds | 0:28fa9993bcf9 | 151 | acc_avg = mma8452.average(); |
ml13emds | 0:28fa9993bcf9 | 152 | buzzer = 0; |
ml13emds | 0:28fa9993bcf9 | 153 | step = 0; |
ml13emds | 0:28fa9993bcf9 | 154 | km = 0; |
ml13emds | 0:28fa9993bcf9 | 155 | leds = 0x04; |
ml13emds | 0:28fa9993bcf9 | 156 | aux = 0; |
ml13emds | 0:28fa9993bcf9 | 157 | timerFlag2 = 0; |
ml13emds | 0:28fa9993bcf9 | 158 | timer3.attach(&TimerExpired3,1); |
ml13emds | 0:28fa9993bcf9 | 159 | // While the timer does not end, keep counting |
ml13emds | 0:28fa9993bcf9 | 160 | while(!timerFlag2) |
ml13emds | 0:28fa9993bcf9 | 161 | { |
ml13emds | 0:28fa9993bcf9 | 162 | acceleration = mma8452.readValues(); // read current values |
ml13emds | 0:28fa9993bcf9 | 163 | sub_x = acceleration.x - acc_avg.x; |
ml13emds | 0:28fa9993bcf9 | 164 | sub_y = acceleration.y - acc_avg.y; |
ml13emds | 0:28fa9993bcf9 | 165 | sub_z = acceleration.z - acc_avg.z; |
ml13emds | 0:28fa9993bcf9 | 166 | acc_vector = (pow(sub_x,2.0)+pow(sub_y,2.0)+pow(sub_z,2.0)); |
ml13emds | 0:28fa9993bcf9 | 167 | acc_vector = sqrt(acc_vector); |
ml13emds | 0:28fa9993bcf9 | 168 | // If the acceleration vector is greater than 0.15, add the steps |
ml13emds | 0:28fa9993bcf9 | 169 | if(acc_vector > 0.15) |
ml13emds | 0:28fa9993bcf9 | 170 | { |
ml13emds | 0:28fa9993bcf9 | 171 | step = step + 2; |
ml13emds | 0:28fa9993bcf9 | 172 | // Runing |
ml13emds | 0:28fa9993bcf9 | 173 | if (acc_vector > 1.0) |
ml13emds | 0:28fa9993bcf9 | 174 | km = km + 0.002; |
ml13emds | 0:28fa9993bcf9 | 175 | // Walking |
ml13emds | 0:28fa9993bcf9 | 176 | else |
ml13emds | 0:28fa9993bcf9 | 177 | km = km + 0.001; |
ml13emds | 0:28fa9993bcf9 | 178 | } |
ml13emds | 0:28fa9993bcf9 | 179 | |
ml13emds | 0:28fa9993bcf9 | 180 | lcd.clear(); |
ml13emds | 0:28fa9993bcf9 | 181 | |
ml13emds | 0:28fa9993bcf9 | 182 | length = sprintf(buffer,"%6u steps",step); |
ml13emds | 0:28fa9993bcf9 | 183 | if (length <= 14) |
ml13emds | 0:28fa9993bcf9 | 184 | lcd.printString(buffer,0,0); |
ml13emds | 0:28fa9993bcf9 | 185 | |
ml13emds | 0:28fa9993bcf9 | 186 | length = sprintf(buffer,"%6.3f km",km); |
ml13emds | 0:28fa9993bcf9 | 187 | if (length <= 14) |
ml13emds | 0:28fa9993bcf9 | 188 | lcd.printString(buffer,0,1); |
ml13emds | 0:28fa9993bcf9 | 189 | |
ml13emds | 0:28fa9993bcf9 | 190 | length = sprintf(buffer,"%2u:%2u:%2u",hour, minute, second); |
ml13emds | 0:28fa9993bcf9 | 191 | if (length <= 14) |
ml13emds | 0:28fa9993bcf9 | 192 | lcd.printString(buffer,0,2); |
ml13emds | 0:28fa9993bcf9 | 193 | |
ml13emds | 0:28fa9993bcf9 | 194 | // get current date |
ml13emds | 0:28fa9993bcf9 | 195 | time_t seconds = time(NULL); |
ml13emds | 0:28fa9993bcf9 | 196 | // Convert date to a string |
ml13emds | 0:28fa9993bcf9 | 197 | strftime(buffer, 14 , "%a-%d/%m/%Y", localtime(&seconds)); |
ml13emds | 0:28fa9993bcf9 | 198 | lcd.printString(buffer,0,3); |
ml13emds | 0:28fa9993bcf9 | 199 | |
ml13emds | 0:28fa9993bcf9 | 200 | // Avoiding counting the same steps twice |
ml13emds | 0:28fa9993bcf9 | 201 | wait(0.65); |
ml13emds | 0:28fa9993bcf9 | 202 | |
ml13emds | 0:28fa9993bcf9 | 203 | // If one stops the activity, starts the timer |
ml13emds | 0:28fa9993bcf9 | 204 | if ((acc_vector <0.15)&& (aux==0)) |
ml13emds | 0:28fa9993bcf9 | 205 | { |
ml13emds | 0:28fa9993bcf9 | 206 | timer2.attach(&TimerExpired2,20.0); |
ml13emds | 0:28fa9993bcf9 | 207 | aux = 1; |
ml13emds | 0:28fa9993bcf9 | 208 | } |
ml13emds | 0:28fa9993bcf9 | 209 | // If one walks again, reset the timer |
ml13emds | 0:28fa9993bcf9 | 210 | else if ((acc_vector >0.15)&&(aux == 1)) |
ml13emds | 0:28fa9993bcf9 | 211 | { |
ml13emds | 0:28fa9993bcf9 | 212 | timer2.detach(); |
ml13emds | 0:28fa9993bcf9 | 213 | aux = 0; |
ml13emds | 0:28fa9993bcf9 | 214 | } |
ml13emds | 0:28fa9993bcf9 | 215 | |
ml13emds | 0:28fa9993bcf9 | 216 | } |
ml13emds | 0:28fa9993bcf9 | 217 | buzzer = 0.5; |
ml13emds | 0:28fa9993bcf9 | 218 | wait(3); |
ml13emds | 0:28fa9993bcf9 | 219 | buzzer = 0; |
ml13emds | 0:28fa9993bcf9 | 220 | Int_SourceTrans = mma8452.readByteFromRegister(TRANSIENT_SRC); |
ml13emds | 0:28fa9993bcf9 | 221 | Int_SourceTrans = mma8452.readByteFromRegister(PULSE_SRC); |
ml13emds | 0:28fa9993bcf9 | 222 | Int_SourceSystem = 0; |
ml13emds | 0:28fa9993bcf9 | 223 | wait(0.2); |
ml13emds | 0:28fa9993bcf9 | 224 | timer3.detach(); |
ml13emds | 0:28fa9993bcf9 | 225 | second = 0; |
ml13emds | 0:28fa9993bcf9 | 226 | minute = 0; |
ml13emds | 0:28fa9993bcf9 | 227 | hour = 0; |
ml13emds | 0:28fa9993bcf9 | 228 | I1_flag = 0; |
ml13emds | 0:28fa9993bcf9 | 229 | timerFlag2 = 0; |
ml13emds | 0:28fa9993bcf9 | 230 | // Saving data to local system File |
ml13emds | 0:28fa9993bcf9 | 231 | writeDataToFile(buffer,step,km); |
ml13emds | 0:28fa9993bcf9 | 232 | // Accumulating the steps per day value |
ml13emds | 0:28fa9993bcf9 | 233 | time_t seconds = time(NULL); |
ml13emds | 0:28fa9993bcf9 | 234 | strftime(buffer, 3 , "%u", localtime(&seconds)); |
ml13emds | 0:28fa9993bcf9 | 235 | int value = atoi(buffer); |
ml13emds | 0:28fa9993bcf9 | 236 | km_day[value]=km_day[value]+km; |
ml13emds | 0:28fa9993bcf9 | 237 | state = 0; |
ml13emds | 0:28fa9993bcf9 | 238 | break; |
ml13emds | 0:28fa9993bcf9 | 239 | } |
ml13emds | 0:28fa9993bcf9 | 240 | // Graph Km/day Screen |
ml13emds | 0:28fa9993bcf9 | 241 | case 3: |
ml13emds | 0:28fa9993bcf9 | 242 | { |
ml13emds | 0:28fa9993bcf9 | 243 | lcd.printString("Km/day",10,0); |
ml13emds | 0:28fa9993bcf9 | 244 | lcd.printString("8",10,1); |
ml13emds | 0:28fa9993bcf9 | 245 | lcd.printString("6",10,2); |
ml13emds | 0:28fa9993bcf9 | 246 | lcd.printString("4",10,3); |
ml13emds | 0:28fa9993bcf9 | 247 | lcd.printString("2",10,4); |
ml13emds | 0:28fa9993bcf9 | 248 | lcd.printString("0",10,5); |
ml13emds | 0:28fa9993bcf9 | 249 | lcd.drawRect(17,7,60,40,0); |
ml13emds | 0:28fa9993bcf9 | 250 | // Logic to print the graph with all days |
ml13emds | 0:28fa9993bcf9 | 251 | for (int x = 1; x < 30; x++) |
ml13emds | 0:28fa9993bcf9 | 252 | { |
ml13emds | 0:28fa9993bcf9 | 253 | float n_pix = 8.0/36.0; |
ml13emds | 0:28fa9993bcf9 | 254 | int last_point = ((8.0-km_day[x-1])/(n_pix)); |
ml13emds | 0:28fa9993bcf9 | 255 | int point = ((8.0-km_day[x])/(n_pix)); |
ml13emds | 0:28fa9993bcf9 | 256 | lcd.drawLine((x-1)+18,last_point+9,x+18,point+9,1); |
ml13emds | 0:28fa9993bcf9 | 257 | } |
ml13emds | 0:28fa9993bcf9 | 258 | lcd.refresh(); |
ml13emds | 0:28fa9993bcf9 | 259 | // If the system detects the tap again, it goes back to initial screen |
ml13emds | 0:28fa9993bcf9 | 260 | if(I2_flag) |
ml13emds | 0:28fa9993bcf9 | 261 | { |
ml13emds | 0:28fa9993bcf9 | 262 | Int_SourceSystem = mma8452.readByteFromRegister(INT_SOURCE); |
ml13emds | 0:28fa9993bcf9 | 263 | if ((Int_SourceSystem&0x08)==0x08) |
ml13emds | 0:28fa9993bcf9 | 264 | { |
ml13emds | 0:28fa9993bcf9 | 265 | Int_SourceTrans = mma8452.readByteFromRegister(TRANSIENT_SRC); |
ml13emds | 0:28fa9993bcf9 | 266 | Int_SourceTrans = mma8452.readByteFromRegister(PULSE_SRC); |
ml13emds | 0:28fa9993bcf9 | 267 | Int_SourceSystem = 0; |
ml13emds | 0:28fa9993bcf9 | 268 | wait(0.2); |
ml13emds | 0:28fa9993bcf9 | 269 | I1_flag = 0; |
ml13emds | 0:28fa9993bcf9 | 270 | I2_flag = 0; |
ml13emds | 0:28fa9993bcf9 | 271 | timer1.attach(&TimerExpired1,20.0); |
ml13emds | 0:28fa9993bcf9 | 272 | timerFlag1 = 0; |
ml13emds | 0:28fa9993bcf9 | 273 | state = 1; |
ml13emds | 0:28fa9993bcf9 | 274 | } |
ml13emds | 0:28fa9993bcf9 | 275 | } |
ml13emds | 0:28fa9993bcf9 | 276 | // If the user does not perform any action in 20 seconds, turn off the device |
ml13emds | 0:28fa9993bcf9 | 277 | else if(timerFlag1) |
ml13emds | 0:28fa9993bcf9 | 278 | { |
ml13emds | 0:28fa9993bcf9 | 279 | timerFlag1 = 0; |
ml13emds | 0:28fa9993bcf9 | 280 | Int_SourceTrans = mma8452.readByteFromRegister(TRANSIENT_SRC); |
ml13emds | 0:28fa9993bcf9 | 281 | Int_SourceTrans = mma8452.readByteFromRegister(PULSE_SRC); |
ml13emds | 0:28fa9993bcf9 | 282 | Int_SourceSystem = 0; |
ml13emds | 0:28fa9993bcf9 | 283 | wait(0.2); |
ml13emds | 0:28fa9993bcf9 | 284 | I1_flag = 0; |
ml13emds | 0:28fa9993bcf9 | 285 | I2_flag = 0; |
ml13emds | 0:28fa9993bcf9 | 286 | state = 0; |
ml13emds | 0:28fa9993bcf9 | 287 | } |
ml13emds | 0:28fa9993bcf9 | 288 | wait(1); |
ml13emds | 0:28fa9993bcf9 | 289 | lcd.clear(); |
ml13emds | 0:28fa9993bcf9 | 290 | break; |
ml13emds | 0:28fa9993bcf9 | 291 | } |
ml13emds | 0:28fa9993bcf9 | 292 | |
ml13emds | 0:28fa9993bcf9 | 293 | default: |
ml13emds | 0:28fa9993bcf9 | 294 | //invalid state - call error routine |
ml13emds | 0:28fa9993bcf9 | 295 | error(); |
ml13emds | 0:28fa9993bcf9 | 296 | break; |
ml13emds | 0:28fa9993bcf9 | 297 | } |
ml13emds | 0:28fa9993bcf9 | 298 | } |
ml13emds | 0:28fa9993bcf9 | 299 | } |
ml13emds | 0:28fa9993bcf9 | 300 | |
ml13emds | 0:28fa9993bcf9 | 301 | void Interrupt() |
ml13emds | 0:28fa9993bcf9 | 302 | { |
ml13emds | 0:28fa9993bcf9 | 303 | /// Controls the Transient Detection Interrupt flag |
ml13emds | 0:28fa9993bcf9 | 304 | I1_flag = 1; |
ml13emds | 0:28fa9993bcf9 | 305 | } |
ml13emds | 0:28fa9993bcf9 | 306 | |
ml13emds | 0:28fa9993bcf9 | 307 | void Interrupt2() |
ml13emds | 0:28fa9993bcf9 | 308 | { |
ml13emds | 0:28fa9993bcf9 | 309 | /// Controls the Pulse(Tap)Detection Interrupt flag |
ml13emds | 0:28fa9993bcf9 | 310 | I2_flag = 1; |
ml13emds | 0:28fa9993bcf9 | 311 | } |
ml13emds | 0:28fa9993bcf9 | 312 | |
ml13emds | 0:28fa9993bcf9 | 313 | void error() |
ml13emds | 0:28fa9993bcf9 | 314 | { |
ml13emds | 0:28fa9993bcf9 | 315 | /// Error function. In case of error of the state machine |
ml13emds | 0:28fa9993bcf9 | 316 | while(1) |
ml13emds | 0:28fa9993bcf9 | 317 | { |
ml13emds | 0:28fa9993bcf9 | 318 | lcd.clear(); |
ml13emds | 0:28fa9993bcf9 | 319 | lcd.printString("FSM Error!",0,0); |
ml13emds | 0:28fa9993bcf9 | 320 | } |
ml13emds | 0:28fa9993bcf9 | 321 | } |
ml13emds | 0:28fa9993bcf9 | 322 | |
ml13emds | 0:28fa9993bcf9 | 323 | void TimerExpired1() |
ml13emds | 0:28fa9993bcf9 | 324 | { |
ml13emds | 0:28fa9993bcf9 | 325 | /// Timer 1 flag |
ml13emds | 0:28fa9993bcf9 | 326 | timerFlag1 = 1; |
ml13emds | 0:28fa9993bcf9 | 327 | } |
ml13emds | 0:28fa9993bcf9 | 328 | |
ml13emds | 0:28fa9993bcf9 | 329 | void TimerExpired2() |
ml13emds | 0:28fa9993bcf9 | 330 | { |
ml13emds | 0:28fa9993bcf9 | 331 | /// Timer 2 Flag |
ml13emds | 0:28fa9993bcf9 | 332 | timerFlag2 = 1; |
ml13emds | 0:28fa9993bcf9 | 333 | } |
ml13emds | 0:28fa9993bcf9 | 334 | |
ml13emds | 0:28fa9993bcf9 | 335 | void TimerExpired3() |
ml13emds | 0:28fa9993bcf9 | 336 | { |
ml13emds | 0:28fa9993bcf9 | 337 | /// Calculates the chronometer time |
ml13emds | 0:28fa9993bcf9 | 338 | second = second + 1; |
ml13emds | 0:28fa9993bcf9 | 339 | if (second > 60) |
ml13emds | 0:28fa9993bcf9 | 340 | { |
ml13emds | 0:28fa9993bcf9 | 341 | second = 0; |
ml13emds | 0:28fa9993bcf9 | 342 | minute = minute + 1; |
ml13emds | 0:28fa9993bcf9 | 343 | if (minute > 60) |
ml13emds | 0:28fa9993bcf9 | 344 | { |
ml13emds | 0:28fa9993bcf9 | 345 | hour = hour + 1; |
ml13emds | 0:28fa9993bcf9 | 346 | minute = 0; |
ml13emds | 0:28fa9993bcf9 | 347 | } |
ml13emds | 0:28fa9993bcf9 | 348 | |
ml13emds | 0:28fa9993bcf9 | 349 | } |
ml13emds | 0:28fa9993bcf9 | 350 | } |
ml13emds | 0:28fa9993bcf9 | 351 | |
ml13emds | 0:28fa9993bcf9 | 352 | void writeDataToFile(char *date,int data1,float data2) |
ml13emds | 0:28fa9993bcf9 | 353 | { |
ml13emds | 0:28fa9993bcf9 | 354 | /// Saves the km and steps data to flash disk |
ml13emds | 0:28fa9993bcf9 | 355 | FILE *fp = fopen("/local/log.txt", "a"); |
ml13emds | 0:28fa9993bcf9 | 356 | // Create the txt file |
ml13emds | 0:28fa9993bcf9 | 357 | fprintf(fp,"Date: %s\n",date); |
ml13emds | 0:28fa9993bcf9 | 358 | fprintf(fp,"Steps = %6u\n",data1); |
ml13emds | 0:28fa9993bcf9 | 359 | fprintf(fp,"Km = %6.3f\n \n",data2); |
ml13emds | 0:28fa9993bcf9 | 360 | fclose(fp); |
ml13emds | 0:28fa9993bcf9 | 361 | } |