share

Dependencies:   GraphicsDisplay X_NUCLEO_IKS01A1 HelloWorld_IKS01A1 mbed-src mbed

Fork of HelloWorld_IKS01A1 by ST

Committer:
istheworldreal
Date:
Wed May 18 19:43:23 2016 +0000
Revision:
7:c820cac010d6
Parent:
4:b1526d074d83
share

Who changed what in which revision?

UserRevisionLine numberNew 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"
Wolfgang Betz 0:c71c9af137dd 4
Wolfgang Betz 0:c71c9af137dd 5 /* Instantiate the expansion board */
Wolfgang Betz 4:b1526d074d83 6 static X_NUCLEO_IKS01A1 *mems_expansion_board = X_NUCLEO_IKS01A1::Instance(D14, D15);
Wolfgang Betz 0:c71c9af137dd 7
Wolfgang Betz 0:c71c9af137dd 8 /* Retrieve the composing elements of the expansion board */
Wolfgang Betz 0:c71c9af137dd 9 static GyroSensor *gyroscope = mems_expansion_board->GetGyroscope();
Wolfgang Betz 0:c71c9af137dd 10 static MotionSensor *accelerometer = mems_expansion_board->GetAccelerometer();
Wolfgang Betz 0:c71c9af137dd 11 static MagneticSensor *magnetometer = mems_expansion_board->magnetometer;
Wolfgang Betz 0:c71c9af137dd 12 static HumiditySensor *humidity_sensor = mems_expansion_board->ht_sensor;
Wolfgang Betz 3:a2d2342526db 13 static PressureSensor *pressure_sensor = mems_expansion_board->pt_sensor;
Wolfgang Betz 0:c71c9af137dd 14 static TempSensor *temp_sensor1 = mems_expansion_board->ht_sensor;
Wolfgang Betz 3:a2d2342526db 15 static TempSensor *temp_sensor2 = mems_expansion_board->pt_sensor;
Wolfgang Betz 0:c71c9af137dd 16
Wolfgang Betz 0:c71c9af137dd 17 /* Helper function for printing floats & doubles */
Wolfgang Betz 0:c71c9af137dd 18 static char *printDouble(char* str, double v, int decimalDigits=2)
Wolfgang Betz 0:c71c9af137dd 19 {
Wolfgang Betz 0:c71c9af137dd 20 int i = 1;
Wolfgang Betz 0:c71c9af137dd 21 int intPart, fractPart;
Wolfgang Betz 0:c71c9af137dd 22 int len;
Wolfgang Betz 0:c71c9af137dd 23 char *ptr;
Wolfgang Betz 0:c71c9af137dd 24
Wolfgang Betz 0:c71c9af137dd 25 /* prepare decimal digits multiplicator */
Wolfgang Betz 0:c71c9af137dd 26 for (;decimalDigits!=0; i*=10, decimalDigits--);
Wolfgang Betz 0:c71c9af137dd 27
Wolfgang Betz 0:c71c9af137dd 28 /* calculate integer & fractinal parts */
Wolfgang Betz 0:c71c9af137dd 29 intPart = (int)v;
Wolfgang Betz 0:c71c9af137dd 30 fractPart = (int)((v-(double)(int)v)*i);
Wolfgang Betz 0:c71c9af137dd 31
Wolfgang Betz 0:c71c9af137dd 32 /* fill in integer part */
Wolfgang Betz 0:c71c9af137dd 33 sprintf(str, "%i.", intPart);
Wolfgang Betz 0:c71c9af137dd 34
Wolfgang Betz 0:c71c9af137dd 35 /* prepare fill in of fractional part */
Wolfgang Betz 0:c71c9af137dd 36 len = strlen(str);
Wolfgang Betz 0:c71c9af137dd 37 ptr = &str[len];
Wolfgang Betz 0:c71c9af137dd 38
Wolfgang Betz 0:c71c9af137dd 39 /* fill in leading fractional zeros */
Wolfgang Betz 0:c71c9af137dd 40 for (i/=10;i>1; i/=10, ptr++) {
Wolfgang Betz 0:c71c9af137dd 41 if(fractPart >= i) break;
Wolfgang Betz 0:c71c9af137dd 42 *ptr = '0';
Wolfgang Betz 0:c71c9af137dd 43 }
Wolfgang Betz 0:c71c9af137dd 44
Wolfgang Betz 0:c71c9af137dd 45 /* fill in (rest of) fractional part */
Wolfgang Betz 0:c71c9af137dd 46 sprintf(ptr, "%i", fractPart);
Wolfgang Betz 0:c71c9af137dd 47
Wolfgang Betz 0:c71c9af137dd 48 return str;
Wolfgang Betz 0:c71c9af137dd 49 }
Wolfgang Betz 0:c71c9af137dd 50
Wolfgang Betz 0:c71c9af137dd 51
Wolfgang Betz 0:c71c9af137dd 52 /* Simple main function */
Wolfgang Betz 0:c71c9af137dd 53 int main() {
Wolfgang Betz 0:c71c9af137dd 54 uint8_t id;
Wolfgang Betz 0:c71c9af137dd 55 float value1, value2;
Wolfgang Betz 0:c71c9af137dd 56 char buffer1[32], buffer2[32];
Wolfgang Betz 0:c71c9af137dd 57 int32_t axes[3];
istheworldreal 7:c820cac010d6 58 int32_t m[3];
istheworldreal 7:c820cac010d6 59 int32_t n[3];
istheworldreal 7:c820cac010d6 60 int32_t r;
istheworldreal 7:c820cac010d6 61 int32_t i;
istheworldreal 7:c820cac010d6 62 int32_t lock_unlock=0;
Wolfgang Betz 0:c71c9af137dd 63 printf("\r\n--- Starting new run ---\r\n");
Wolfgang Betz 0:c71c9af137dd 64
Wolfgang Betz 0:c71c9af137dd 65 humidity_sensor->ReadID(&id);
Wolfgang Betz 0:c71c9af137dd 66 printf("HTS221 humidity & temperature = 0x%X\r\n", id);
istheworldreal 7:c820cac010d6 67 //pressure_sensor->ReadID(&id);
istheworldreal 7:c820cac010d6 68 // printf("LPS25H pressure & temperature = 0x%X\r\n", id);
istheworldreal 7:c820cac010d6 69 //magnetometer->ReadID(&id);
istheworldreal 7:c820cac010d6 70 //printf("LIS3MDL magnetometer = 0x%X\r\n", id);
Wolfgang Betz 0:c71c9af137dd 71 gyroscope->ReadID(&id);
Wolfgang Betz 0:c71c9af137dd 72 printf("LSM6DS0 accelerometer & gyroscope = 0x%X\r\n", id);
Wolfgang Betz 0:c71c9af137dd 73
istheworldreal 7:c820cac010d6 74 DigitalOut myled(LED1);
istheworldreal 7:c820cac010d6 75 DigitalIn mybutton(USER_BUTTON);
istheworldreal 7:c820cac010d6 76
Wolfgang Betz 0:c71c9af137dd 77 wait(3);
Wolfgang Betz 0:c71c9af137dd 78
Wolfgang Betz 0:c71c9af137dd 79 while(1) {
istheworldreal 7:c820cac010d6 80 wait(1);
Wolfgang Betz 0:c71c9af137dd 81 printf("\r\n");
istheworldreal 7:c820cac010d6 82
istheworldreal 7:c820cac010d6 83 temp_sensor1->GetTemperature(&value1);
Wolfgang Betz 0:c71c9af137dd 84 humidity_sensor->GetHumidity(&value2);
istheworldreal 7:c820cac010d6 85 if ((value1>27) && (value2>70))
istheworldreal 7:c820cac010d6 86 {
istheworldreal 7:c820cac010d6 87 myled=1;
istheworldreal 7:c820cac010d6 88 wait(1);
istheworldreal 7:c820cac010d6 89 myled=0;
istheworldreal 7:c820cac010d6 90 printf("Excessive Temperature & Excessive Humidity\r\n");
istheworldreal 7:c820cac010d6 91 }
istheworldreal 7:c820cac010d6 92 if ((value1>27) && (value2<=70))
istheworldreal 7:c820cac010d6 93 {
istheworldreal 7:c820cac010d6 94 myled=1;
istheworldreal 7:c820cac010d6 95 wait(0.3);
istheworldreal 7:c820cac010d6 96 myled=0;
istheworldreal 7:c820cac010d6 97 printf("Excessive Temperature\r\n");
istheworldreal 7:c820cac010d6 98 }
istheworldreal 7:c820cac010d6 99 if ((value1<=27) && (value2>70))
istheworldreal 7:c820cac010d6 100 {
istheworldreal 7:c820cac010d6 101 myled=1;
istheworldreal 7:c820cac010d6 102 wait(0.6);
istheworldreal 7:c820cac010d6 103 myled=0;
istheworldreal 7:c820cac010d6 104 printf("Excessive Humidity\r\n");
istheworldreal 7:c820cac010d6 105 }
istheworldreal 7:c820cac010d6 106
istheworldreal 7:c820cac010d6 107
Wolfgang Betz 0:c71c9af137dd 108 printf("HTS221: [temp] %7s°C, [hum] %s%%\r\n", printDouble(buffer1, value1), printDouble(buffer2, value2));
Wolfgang Betz 0:c71c9af137dd 109 temp_sensor2->GetFahrenheit(&value1);
istheworldreal 7:c820cac010d6 110 //pressure_sensor->GetPressure(&value2);
istheworldreal 7:c820cac010d6 111 //printf("LPS25H: [temp] %7s°F, [press] %smbar\r\n", printDouble(buffer1, value1), printDouble(buffer2, value2));
Wolfgang Betz 0:c71c9af137dd 112
istheworldreal 7:c820cac010d6 113 if(mybutton == 0)
istheworldreal 7:c820cac010d6 114 {
istheworldreal 7:c820cac010d6 115 lock_unlock =! lock_unlock;
istheworldreal 7:c820cac010d6 116 myled=1;
istheworldreal 7:c820cac010d6 117 wait(0.2);
istheworldreal 7:c820cac010d6 118 myled=0;
istheworldreal 7:c820cac010d6 119 wait(0.2);
istheworldreal 7:c820cac010d6 120 myled=1;
istheworldreal 7:c820cac010d6 121 wait(0.2);
istheworldreal 7:c820cac010d6 122 myled=0;
istheworldreal 7:c820cac010d6 123 }
istheworldreal 7:c820cac010d6 124
istheworldreal 7:c820cac010d6 125 if (lock_unlock == 0)
istheworldreal 7:c820cac010d6 126 {
istheworldreal 7:c820cac010d6 127 printf("SecuritySystemON\r\n");
istheworldreal 7:c820cac010d6 128 accelerometer->Get_X_Axes(axes);
istheworldreal 7:c820cac010d6 129 for (i=0;i<3;i++)
istheworldreal 7:c820cac010d6 130 {
istheworldreal 7:c820cac010d6 131 m[i]=axes[i];
istheworldreal 7:c820cac010d6 132 }
istheworldreal 7:c820cac010d6 133 wait(0.2);
istheworldreal 7:c820cac010d6 134 accelerometer->Get_X_Axes(axes);
istheworldreal 7:c820cac010d6 135 for (i=0;i<3;i++)
istheworldreal 7:c820cac010d6 136 {
istheworldreal 7:c820cac010d6 137 n[i]=axes[i];
istheworldreal 7:c820cac010d6 138 }
istheworldreal 7:c820cac010d6 139 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]);
istheworldreal 7:c820cac010d6 140 if(r>70)
istheworldreal 7:c820cac010d6 141 {
istheworldreal 7:c820cac010d6 142 myled=1;
istheworldreal 7:c820cac010d6 143 printf("Security Alert\r\n");
istheworldreal 7:c820cac010d6 144 }
istheworldreal 7:c820cac010d6 145 printf("LSM6DS0 [acc/mg]:%6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
istheworldreal 7:c820cac010d6 146 }
istheworldreal 7:c820cac010d6 147 if (lock_unlock != 0)
istheworldreal 7:c820cac010d6 148 {
istheworldreal 7:c820cac010d6 149 printf("UNLOCKED\r\n");
istheworldreal 7:c820cac010d6 150 printf("SecuritySystemOFF\r\n");
istheworldreal 7:c820cac010d6 151 }
Wolfgang Betz 0:c71c9af137dd 152
Wolfgang Betz 0:c71c9af137dd 153 wait(1.5);
Wolfgang Betz 0:c71c9af137dd 154 }
Wolfgang Betz 0:c71c9af137dd 155 }