ESE project: Portable Camera Safe Box
Dependencies: Freetronics_16x2_LCD X_NUCLEO_IKS01A1 mbed
Fork of HelloWorld_IKS01A1 by
main.cpp@9:0098e603178d, 2016-09-15 (annotated)
- Committer:
- hitzqt
- Date:
- Thu Sep 15 10:36:59 2016 +0000
- Revision:
- 9:0098e603178d
- Parent:
- 4:b1526d074d83
ESE project: Portable Camera Safe Box
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Wolfgang Betz |
0:c71c9af137dd | 1 | /* Includes */ |
Wolfgang Betz |
0:c71c9af137dd | 2 | #include "mbed.h" |
Wolfgang Betz |
0:c71c9af137dd | 3 | #include "x_nucleo_iks01a1.h" |
hitzqt | 9:0098e603178d | 4 | #include "freetronicsLCDShield.h" |
hitzqt | 9:0098e603178d | 5 | |
hitzqt | 9:0098e603178d | 6 | freetronicsLCDShield lcd(D12, D11, D10, D9, D8, D7, D3, A0); |
hitzqt | 9:0098e603178d | 7 | |
hitzqt | 9:0098e603178d | 8 | |
Wolfgang Betz |
0:c71c9af137dd | 9 | |
Wolfgang Betz |
0:c71c9af137dd | 10 | /* Instantiate the expansion board */ |
Wolfgang Betz |
4:b1526d074d83 | 11 | static X_NUCLEO_IKS01A1 *mems_expansion_board = X_NUCLEO_IKS01A1::Instance(D14, D15); |
Wolfgang Betz |
0:c71c9af137dd | 12 | |
Wolfgang Betz |
0:c71c9af137dd | 13 | /* Retrieve the composing elements of the expansion board */ |
Wolfgang Betz |
0:c71c9af137dd | 14 | static GyroSensor *gyroscope = mems_expansion_board->GetGyroscope(); |
Wolfgang Betz |
0:c71c9af137dd | 15 | static MotionSensor *accelerometer = mems_expansion_board->GetAccelerometer(); |
hitzqt | 9:0098e603178d | 16 | //static MagneticSensor *magnetometer = mems_expansion_board->magnetometer; |
Wolfgang Betz |
0:c71c9af137dd | 17 | static HumiditySensor *humidity_sensor = mems_expansion_board->ht_sensor; |
hitzqt | 9:0098e603178d | 18 | //static PressureSensor *pressure_sensor = mems_expansion_board->pt_sensor; |
Wolfgang Betz |
0:c71c9af137dd | 19 | static TempSensor *temp_sensor1 = mems_expansion_board->ht_sensor; |
Wolfgang Betz |
3:a2d2342526db | 20 | static TempSensor *temp_sensor2 = mems_expansion_board->pt_sensor; |
Wolfgang Betz |
0:c71c9af137dd | 21 | |
Wolfgang Betz |
0:c71c9af137dd | 22 | /* Helper function for printing floats & doubles */ |
Wolfgang Betz |
0:c71c9af137dd | 23 | static char *printDouble(char* str, double v, int decimalDigits=2) |
Wolfgang Betz |
0:c71c9af137dd | 24 | { |
Wolfgang Betz |
0:c71c9af137dd | 25 | int i = 1; |
Wolfgang Betz |
0:c71c9af137dd | 26 | int intPart, fractPart; |
Wolfgang Betz |
0:c71c9af137dd | 27 | int len; |
Wolfgang Betz |
0:c71c9af137dd | 28 | char *ptr; |
Wolfgang Betz |
0:c71c9af137dd | 29 | |
Wolfgang Betz |
0:c71c9af137dd | 30 | /* prepare decimal digits multiplicator */ |
Wolfgang Betz |
0:c71c9af137dd | 31 | for (;decimalDigits!=0; i*=10, decimalDigits--); |
Wolfgang Betz |
0:c71c9af137dd | 32 | |
Wolfgang Betz |
0:c71c9af137dd | 33 | /* calculate integer & fractinal parts */ |
Wolfgang Betz |
0:c71c9af137dd | 34 | intPart = (int)v; |
Wolfgang Betz |
0:c71c9af137dd | 35 | fractPart = (int)((v-(double)(int)v)*i); |
Wolfgang Betz |
0:c71c9af137dd | 36 | |
Wolfgang Betz |
0:c71c9af137dd | 37 | /* fill in integer part */ |
Wolfgang Betz |
0:c71c9af137dd | 38 | sprintf(str, "%i.", intPart); |
Wolfgang Betz |
0:c71c9af137dd | 39 | |
Wolfgang Betz |
0:c71c9af137dd | 40 | /* prepare fill in of fractional part */ |
Wolfgang Betz |
0:c71c9af137dd | 41 | len = strlen(str); |
Wolfgang Betz |
0:c71c9af137dd | 42 | ptr = &str[len]; |
Wolfgang Betz |
0:c71c9af137dd | 43 | |
Wolfgang Betz |
0:c71c9af137dd | 44 | /* fill in leading fractional zeros */ |
Wolfgang Betz |
0:c71c9af137dd | 45 | for (i/=10;i>1; i/=10, ptr++) { |
Wolfgang Betz |
0:c71c9af137dd | 46 | if(fractPart >= i) break; |
Wolfgang Betz |
0:c71c9af137dd | 47 | *ptr = '0'; |
Wolfgang Betz |
0:c71c9af137dd | 48 | } |
Wolfgang Betz |
0:c71c9af137dd | 49 | |
Wolfgang Betz |
0:c71c9af137dd | 50 | /* fill in (rest of) fractional part */ |
Wolfgang Betz |
0:c71c9af137dd | 51 | sprintf(ptr, "%i", fractPart); |
Wolfgang Betz |
0:c71c9af137dd | 52 | |
Wolfgang Betz |
0:c71c9af137dd | 53 | return str; |
Wolfgang Betz |
0:c71c9af137dd | 54 | } |
Wolfgang Betz |
0:c71c9af137dd | 55 | |
Wolfgang Betz |
0:c71c9af137dd | 56 | |
Wolfgang Betz |
0:c71c9af137dd | 57 | /* Simple main function */ |
Wolfgang Betz |
0:c71c9af137dd | 58 | int main() { |
Wolfgang Betz |
0:c71c9af137dd | 59 | uint8_t id; |
Wolfgang Betz |
0:c71c9af137dd | 60 | float value1, value2; |
Wolfgang Betz |
0:c71c9af137dd | 61 | char buffer1[32], buffer2[32]; |
Wolfgang Betz |
0:c71c9af137dd | 62 | int32_t axes[3]; |
hitzqt | 9:0098e603178d | 63 | int32_t m[3]; |
hitzqt | 9:0098e603178d | 64 | int32_t n[3]; |
hitzqt | 9:0098e603178d | 65 | int32_t r; |
hitzqt | 9:0098e603178d | 66 | int32_t i; |
hitzqt | 9:0098e603178d | 67 | int32_t lock_unlock=0; |
hitzqt | 9:0098e603178d | 68 | int temp=0; |
Wolfgang Betz |
0:c71c9af137dd | 69 | |
hitzqt | 9:0098e603178d | 70 | // turn on the back light (it's off by default) |
hitzqt | 9:0098e603178d | 71 | lcd.setBackLight(true); |
hitzqt | 9:0098e603178d | 72 | |
hitzqt | 9:0098e603178d | 73 | // print the first line and wait 3 sec |
hitzqt | 9:0098e603178d | 74 | lcd.printf("Starting ESEproc"); |
hitzqt | 9:0098e603178d | 75 | wait(2); |
hitzqt | 9:0098e603178d | 76 | |
hitzqt | 9:0098e603178d | 77 | |
hitzqt | 9:0098e603178d | 78 | printf("\r\n--- Starting ESEproc ---\r\n"); |
hitzqt | 9:0098e603178d | 79 | |
hitzqt | 9:0098e603178d | 80 | |
hitzqt | 9:0098e603178d | 81 | |
hitzqt | 9:0098e603178d | 82 | |
hitzqt | 9:0098e603178d | 83 | // print the counter prefix; the number will be printed in the while loop |
hitzqt | 9:0098e603178d | 84 | lcd.setCursorPosition(1, 0); |
hitzqt | 9:0098e603178d | 85 | lcd.printf("Initializing..."); |
Wolfgang Betz |
0:c71c9af137dd | 86 | |
Wolfgang Betz |
0:c71c9af137dd | 87 | humidity_sensor->ReadID(&id); |
Wolfgang Betz |
0:c71c9af137dd | 88 | printf("HTS221 humidity & temperature = 0x%X\r\n", id); |
hitzqt | 9:0098e603178d | 89 | //pressure_sensor->ReadID(&id); |
hitzqt | 9:0098e603178d | 90 | // printf("LPS25H pressure & temperature = 0x%X\r\n", id); |
hitzqt | 9:0098e603178d | 91 | //magnetometer->ReadID(&id); |
hitzqt | 9:0098e603178d | 92 | //printf("LIS3MDL magnetometer = 0x%X\r\n", id); |
Wolfgang Betz |
0:c71c9af137dd | 93 | gyroscope->ReadID(&id); |
Wolfgang Betz |
0:c71c9af137dd | 94 | printf("LSM6DS0 accelerometer & gyroscope = 0x%X\r\n", id); |
Wolfgang Betz |
0:c71c9af137dd | 95 | |
hitzqt | 9:0098e603178d | 96 | DigitalOut myled(LED1); |
hitzqt | 9:0098e603178d | 97 | DigitalIn mybutton(USER_BUTTON); |
hitzqt | 9:0098e603178d | 98 | |
hitzqt | 9:0098e603178d | 99 | wait(2); |
hitzqt | 9:0098e603178d | 100 | |
hitzqt | 9:0098e603178d | 101 | // turn off the back light |
hitzqt | 9:0098e603178d | 102 | lcd.setBackLight(false); |
Wolfgang Betz |
0:c71c9af137dd | 103 | |
Wolfgang Betz |
0:c71c9af137dd | 104 | while(1) { |
hitzqt | 9:0098e603178d | 105 | wait(0.001); |
Wolfgang Betz |
0:c71c9af137dd | 106 | printf("\r\n"); |
hitzqt | 9:0098e603178d | 107 | |
hitzqt | 9:0098e603178d | 108 | temp_sensor1->GetTemperature(&value1); |
hitzqt | 9:0098e603178d | 109 | humidity_sensor->GetHumidity(&value2); |
hitzqt | 9:0098e603178d | 110 | |
hitzqt | 9:0098e603178d | 111 | // print the Temperature and Humidity |
hitzqt | 9:0098e603178d | 112 | lcd.cls(); |
hitzqt | 9:0098e603178d | 113 | lcd.setBackLight(false); |
hitzqt | 9:0098e603178d | 114 | lcd.printf("T:%4s C", printDouble(buffer1, value1)); |
hitzqt | 9:0098e603178d | 115 | |
hitzqt | 9:0098e603178d | 116 | |
hitzqt | 9:0098e603178d | 117 | lcd.setCursorPosition(1, 0); |
hitzqt | 9:0098e603178d | 118 | lcd.printf("H:%s %%", printDouble(buffer2, value2)); |
hitzqt | 9:0098e603178d | 119 | if ((value1>37) && (value2>80)) |
hitzqt | 9:0098e603178d | 120 | { |
hitzqt | 9:0098e603178d | 121 | myled=1; |
Wolfgang Betz |
0:c71c9af137dd | 122 | |
hitzqt | 9:0098e603178d | 123 | printf("Excessive Temperature & Excessive Humidity\r\n"); |
hitzqt | 9:0098e603178d | 124 | lcd.setBackLight(true); |
hitzqt | 9:0098e603178d | 125 | lcd.setCursorPosition(0, 12); |
hitzqt | 9:0098e603178d | 126 | lcd.printf("T"); |
hitzqt | 9:0098e603178d | 127 | lcd.setCursorPosition(1, 12); |
hitzqt | 9:0098e603178d | 128 | lcd.printf("H"); |
hitzqt | 9:0098e603178d | 129 | wait(0.1); |
hitzqt | 9:0098e603178d | 130 | myled=0; |
hitzqt | 9:0098e603178d | 131 | } |
hitzqt | 9:0098e603178d | 132 | if ((value1>37) && (value2<=80)) |
hitzqt | 9:0098e603178d | 133 | { |
hitzqt | 9:0098e603178d | 134 | myled=1; |
hitzqt | 9:0098e603178d | 135 | |
hitzqt | 9:0098e603178d | 136 | printf("Excessive Temperature\r\n"); |
hitzqt | 9:0098e603178d | 137 | lcd.setBackLight(true); |
hitzqt | 9:0098e603178d | 138 | lcd.setCursorPosition(0, 12); |
hitzqt | 9:0098e603178d | 139 | lcd.printf("T"); |
hitzqt | 9:0098e603178d | 140 | wait(0.4); |
hitzqt | 9:0098e603178d | 141 | myled=0; |
hitzqt | 9:0098e603178d | 142 | } |
hitzqt | 9:0098e603178d | 143 | if ((value1<=37) && (value2>80)) |
hitzqt | 9:0098e603178d | 144 | { |
hitzqt | 9:0098e603178d | 145 | myled=1; |
hitzqt | 9:0098e603178d | 146 | |
hitzqt | 9:0098e603178d | 147 | printf("Excessive Humidity\r\n"); |
hitzqt | 9:0098e603178d | 148 | lcd.setBackLight(true); |
hitzqt | 9:0098e603178d | 149 | lcd.setCursorPosition(1, 12); |
hitzqt | 9:0098e603178d | 150 | lcd.printf("H"); |
hitzqt | 9:0098e603178d | 151 | wait(0.8); |
hitzqt | 9:0098e603178d | 152 | myled=0; |
hitzqt | 9:0098e603178d | 153 | } |
hitzqt | 9:0098e603178d | 154 | |
hitzqt | 9:0098e603178d | 155 | |
Wolfgang Betz |
0:c71c9af137dd | 156 | printf("HTS221: [temp] %7s°C, [hum] %s%%\r\n", printDouble(buffer1, value1), printDouble(buffer2, value2)); |
Wolfgang Betz |
0:c71c9af137dd | 157 | |
Wolfgang Betz |
0:c71c9af137dd | 158 | |
hitzqt | 9:0098e603178d | 159 | |
hitzqt | 9:0098e603178d | 160 | // temp_sensor2->GetFahrenheit(&value1); |
hitzqt | 9:0098e603178d | 161 | //pressure_sensor->GetPressure(&value2); |
hitzqt | 9:0098e603178d | 162 | //printf("LPS25H: [temp] %7s°F, [press] %smbar\r\n", printDouble(buffer1, value1), printDouble(buffer2, value2)); |
Wolfgang Betz |
0:c71c9af137dd | 163 | |
hitzqt | 9:0098e603178d | 164 | |
hitzqt | 9:0098e603178d | 165 | // if (temp>=100){ |
hitzqt | 9:0098e603178d | 166 | // lock_unlock =! lock_unlock; |
hitzqt | 9:0098e603178d | 167 | // myled=1; |
hitzqt | 9:0098e603178d | 168 | // wait(0.2); |
hitzqt | 9:0098e603178d | 169 | // myled=0; |
hitzqt | 9:0098e603178d | 170 | // wait(0.2); |
hitzqt | 9:0098e603178d | 171 | // myled=1; |
hitzqt | 9:0098e603178d | 172 | // wait(0.2); |
hitzqt | 9:0098e603178d | 173 | // myled=0; |
hitzqt | 9:0098e603178d | 174 | // } |
hitzqt | 9:0098e603178d | 175 | |
hitzqt | 9:0098e603178d | 176 | if (lock_unlock == 0) |
hitzqt | 9:0098e603178d | 177 | { |
hitzqt | 9:0098e603178d | 178 | printf("SecuritySystemON\r\n"); |
hitzqt | 9:0098e603178d | 179 | lcd.setCursorPosition(0, 15); |
hitzqt | 9:0098e603178d | 180 | lcd.printf("S"); |
hitzqt | 9:0098e603178d | 181 | accelerometer->Get_X_Axes(axes); |
hitzqt | 9:0098e603178d | 182 | for (i=0;i<3;i++) |
hitzqt | 9:0098e603178d | 183 | { |
hitzqt | 9:0098e603178d | 184 | m[i]=axes[i]; |
hitzqt | 9:0098e603178d | 185 | } |
hitzqt | 9:0098e603178d | 186 | wait(0.2); |
hitzqt | 9:0098e603178d | 187 | accelerometer->Get_X_Axes(axes); |
hitzqt | 9:0098e603178d | 188 | for (i=0;i<3;i++) |
hitzqt | 9:0098e603178d | 189 | { |
hitzqt | 9:0098e603178d | 190 | n[i]=axes[i]; |
hitzqt | 9:0098e603178d | 191 | } |
hitzqt | 9:0098e603178d | 192 | r=(m[0]-n[0])*(m[0]-n[0])+(m[1]-n[1])*(m[1]-n[1])+(m[2]-n[2])*(m[2]-n[2]); |
hitzqt | 9:0098e603178d | 193 | if(r>1500) |
hitzqt | 9:0098e603178d | 194 | { |
hitzqt | 9:0098e603178d | 195 | myled=1; |
hitzqt | 9:0098e603178d | 196 | printf("Security Alert\r\n"); |
hitzqt | 9:0098e603178d | 197 | lcd.cls(); |
hitzqt | 9:0098e603178d | 198 | lcd.setBackLight(true); |
hitzqt | 9:0098e603178d | 199 | lcd.printf("Security Alert"); |
hitzqt | 9:0098e603178d | 200 | lcd.setCursorPosition(1, 0); |
hitzqt | 9:0098e603178d | 201 | lcd.printf("BUZZING..."); |
hitzqt | 9:0098e603178d | 202 | wait(5); |
Wolfgang Betz |
0:c71c9af137dd | 203 | |
hitzqt | 9:0098e603178d | 204 | } |
hitzqt | 9:0098e603178d | 205 | printf("LSM6DS0 [acc/mg]:%6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]); |
hitzqt | 9:0098e603178d | 206 | } |
hitzqt | 9:0098e603178d | 207 | if (lock_unlock != 0) |
hitzqt | 9:0098e603178d | 208 | { |
hitzqt | 9:0098e603178d | 209 | printf("UNLOCKED\r\n"); |
hitzqt | 9:0098e603178d | 210 | lcd.setCursorPosition(0, 15); |
hitzqt | 9:0098e603178d | 211 | lcd.printf(" ");//means unlock by removing the s on LCD |
hitzqt | 9:0098e603178d | 212 | printf("SecuritySystemOFF\r\n"); |
hitzqt | 9:0098e603178d | 213 | } |
hitzqt | 9:0098e603178d | 214 | if(mybutton == 0) |
hitzqt | 9:0098e603178d | 215 | { |
hitzqt | 9:0098e603178d | 216 | temp=0; |
hitzqt | 9:0098e603178d | 217 | lcd.setBackLight(true); |
hitzqt | 9:0098e603178d | 218 | while(mybutton==0 and temp<500){ |
hitzqt | 9:0098e603178d | 219 | wait(0.01); |
hitzqt | 9:0098e603178d | 220 | temp++; |
hitzqt | 9:0098e603178d | 221 | } |
hitzqt | 9:0098e603178d | 222 | if (temp==500){ |
hitzqt | 9:0098e603178d | 223 | lock_unlock =! lock_unlock; |
hitzqt | 9:0098e603178d | 224 | if (lock_unlock==0){ |
hitzqt | 9:0098e603178d | 225 | lcd.setCursorPosition(0, 15); |
hitzqt | 9:0098e603178d | 226 | lcd.printf("S");//means lock by adding a s on LCD |
hitzqt | 9:0098e603178d | 227 | } |
hitzqt | 9:0098e603178d | 228 | if (lock_unlock==1){ |
hitzqt | 9:0098e603178d | 229 | lcd.setCursorPosition(0, 15); |
hitzqt | 9:0098e603178d | 230 | lcd.printf(" ");//means unlock by removing the s on LCD |
hitzqt | 9:0098e603178d | 231 | } |
hitzqt | 9:0098e603178d | 232 | } |
hitzqt | 9:0098e603178d | 233 | |
hitzqt | 9:0098e603178d | 234 | wait(0.2); |
hitzqt | 9:0098e603178d | 235 | lcd.setBackLight(false); |
hitzqt | 9:0098e603178d | 236 | } |
hitzqt | 9:0098e603178d | 237 | lcd.setBackLight(false); |
hitzqt | 9:0098e603178d | 238 | wait(0.3); |
Wolfgang Betz |
0:c71c9af137dd | 239 | } |
Wolfgang Betz |
0:c71c9af137dd | 240 | } |