Nelson Santos / Mbed 2 deprecated Coursework_copy

Dependencies:   X_NUCLEO_IKS01A1 mbed-rtos mbed

Fork of HelloWorld_IKS01A1 by ST

Committer:
Jacinta
Date:
Fri May 06 15:25:58 2016 +0000
Revision:
18:bf6578e82712
Parent:
17:37294fc50c80
Child:
19:d442ff60498c
Implemented a few more methods and fixed the previous errors.; An example of how to call the struct and save the data in the mailbox is also available on main.cppf

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Wolfgang Betz 0:c71c9af137dd 1 /**
Wolfgang Betz 0:c71c9af137dd 2 ******************************************************************************
Wolfgang Betz 0:c71c9af137dd 3 * @file main.cpp
nlsantos 14:9542d3b656ff 4 * @author ANG Group (Nelson Santos; Irina Baptista; Pierluigi Urro)
Wolfgang Betz 0:c71c9af137dd 5 * @version V0.0.1
nlsantos 13:569b49092661 6 * @date 05-May-2016
nlsantos 14:9542d3b656ff 7 * @brief Simple Example application for using the X_NUCLEO_IKS01A1
Wolfgang Betz 0:c71c9af137dd 8 * MEMS Inertial & Environmental Sensor Nucleo expansion board.
Wolfgang Betz 0:c71c9af137dd 9 ******************************************************************************
Wolfgang Betz 0:c71c9af137dd 10 * @attention
Wolfgang Betz 0:c71c9af137dd 11 *
Wolfgang Betz 0:c71c9af137dd 12 * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
Wolfgang Betz 0:c71c9af137dd 13 *
Wolfgang Betz 0:c71c9af137dd 14 * Redistribution and use in source and binary forms, with or without modification,
Wolfgang Betz 0:c71c9af137dd 15 * are permitted provided that the following conditions are met:
Wolfgang Betz 0:c71c9af137dd 16 * 1. Redistributions of source code must retain the above copyright notice,
Wolfgang Betz 0:c71c9af137dd 17 * this list of conditions and the following disclaimer.
Wolfgang Betz 0:c71c9af137dd 18 * 2. Redistributions in binary form must reproduce the above copyright notice,
Wolfgang Betz 0:c71c9af137dd 19 * this list of conditions and the following disclaimer in the documentation
Wolfgang Betz 0:c71c9af137dd 20 * and/or other materials provided with the distribution.
Wolfgang Betz 0:c71c9af137dd 21 * 3. Neither the name of STMicroelectronics nor the names of its contributors
Wolfgang Betz 0:c71c9af137dd 22 * may be used to endorse or promote products derived from this software
Wolfgang Betz 0:c71c9af137dd 23 * without specific prior written permission.
Wolfgang Betz 0:c71c9af137dd 24 *
Wolfgang Betz 0:c71c9af137dd 25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Wolfgang Betz 0:c71c9af137dd 26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Wolfgang Betz 0:c71c9af137dd 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Wolfgang Betz 0:c71c9af137dd 28 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
Wolfgang Betz 0:c71c9af137dd 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Wolfgang Betz 0:c71c9af137dd 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
Wolfgang Betz 0:c71c9af137dd 31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
Wolfgang Betz 0:c71c9af137dd 32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
Wolfgang Betz 0:c71c9af137dd 33 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Wolfgang Betz 0:c71c9af137dd 34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Wolfgang Betz 0:c71c9af137dd 35 *
Wolfgang Betz 0:c71c9af137dd 36 ******************************************************************************
nlsantos 14:9542d3b656ff 37 */
Wolfgang Betz 0:c71c9af137dd 38
Wolfgang Betz 0:c71c9af137dd 39 /* Includes */
Wolfgang Betz 0:c71c9af137dd 40 #include "mbed.h"
Wolfgang Betz 0:c71c9af137dd 41 #include "x_nucleo_iks01a1.h"
Jacinta 18:bf6578e82712 42 #include "mailBox.cpp"
Wolfgang Betz 0:c71c9af137dd 43
Wolfgang Betz 0:c71c9af137dd 44 /* Instantiate the expansion board */
Wolfgang Betz 4:b1526d074d83 45 static X_NUCLEO_IKS01A1 *mems_expansion_board = X_NUCLEO_IKS01A1::Instance(D14, D15);
Wolfgang Betz 0:c71c9af137dd 46
Wolfgang Betz 0:c71c9af137dd 47 /* Retrieve the composing elements of the expansion board */
Wolfgang Betz 0:c71c9af137dd 48 static GyroSensor *gyroscope = mems_expansion_board->GetGyroscope();
Wolfgang Betz 0:c71c9af137dd 49 static MotionSensor *accelerometer = mems_expansion_board->GetAccelerometer();
Wolfgang Betz 0:c71c9af137dd 50 static MagneticSensor *magnetometer = mems_expansion_board->magnetometer;
Wolfgang Betz 0:c71c9af137dd 51 static HumiditySensor *humidity_sensor = mems_expansion_board->ht_sensor;
Wolfgang Betz 3:a2d2342526db 52 static PressureSensor *pressure_sensor = mems_expansion_board->pt_sensor;
Wolfgang Betz 0:c71c9af137dd 53 static TempSensor *temp_sensor1 = mems_expansion_board->ht_sensor;
Wolfgang Betz 3:a2d2342526db 54 static TempSensor *temp_sensor2 = mems_expansion_board->pt_sensor;
Wolfgang Betz 0:c71c9af137dd 55
nlsantos 16:b9cfb5171af6 56 //sensor time
nlsantos 16:b9cfb5171af6 57 struct tm t;
nlsantos 16:b9cfb5171af6 58
Wolfgang Betz 0:c71c9af137dd 59 /* Helper function for printing floats & doubles */
Wolfgang Betz 0:c71c9af137dd 60 static char *printDouble(char* str, double v, int decimalDigits=2)
Wolfgang Betz 0:c71c9af137dd 61 {
nlsantos 14:9542d3b656ff 62 int i = 1;
nlsantos 14:9542d3b656ff 63 int intPart, fractPart;
nlsantos 14:9542d3b656ff 64 int len;
nlsantos 14:9542d3b656ff 65 char *ptr;
Wolfgang Betz 0:c71c9af137dd 66
nlsantos 14:9542d3b656ff 67 /* prepare decimal digits multiplicator */
nlsantos 14:9542d3b656ff 68 for (; decimalDigits!=0; i*=10, decimalDigits--);
Wolfgang Betz 0:c71c9af137dd 69
nlsantos 14:9542d3b656ff 70 /* calculate integer & fractinal parts */
nlsantos 14:9542d3b656ff 71 intPart = (int)v;
nlsantos 14:9542d3b656ff 72 fractPart = (int)((v-(double)(int)v)*i);
Wolfgang Betz 0:c71c9af137dd 73
nlsantos 14:9542d3b656ff 74 /* fill in integer part */
nlsantos 14:9542d3b656ff 75 sprintf(str, "%i.", intPart);
Wolfgang Betz 0:c71c9af137dd 76
nlsantos 14:9542d3b656ff 77 /* prepare fill in of fractional part */
nlsantos 14:9542d3b656ff 78 len = strlen(str);
nlsantos 14:9542d3b656ff 79 ptr = &str[len];
Wolfgang Betz 0:c71c9af137dd 80
nlsantos 14:9542d3b656ff 81 /* fill in leading fractional zeros */
nlsantos 14:9542d3b656ff 82 for (i/=10; i>1; i/=10, ptr++) {
nlsantos 14:9542d3b656ff 83 if(fractPart >= i) break;
nlsantos 14:9542d3b656ff 84 *ptr = '0';
nlsantos 14:9542d3b656ff 85 }
Wolfgang Betz 0:c71c9af137dd 86
nlsantos 14:9542d3b656ff 87 /* fill in (rest of) fractional part */
nlsantos 14:9542d3b656ff 88 sprintf(ptr, "%i", fractPart);
Wolfgang Betz 0:c71c9af137dd 89
nlsantos 14:9542d3b656ff 90 return str;
Wolfgang Betz 0:c71c9af137dd 91 }
Wolfgang Betz 0:c71c9af137dd 92
nlsantos 14:9542d3b656ff 93 int readAll()
nlsantos 14:9542d3b656ff 94 {
nlsantos 14:9542d3b656ff 95 int numRecords;
nlsantos 14:9542d3b656ff 96
nlsantos 14:9542d3b656ff 97 return numRecords;
Jacinta 18:bf6578e82712 98 }
nlsantos 14:9542d3b656ff 99
nlsantos 14:9542d3b656ff 100 int deleteAll()
nlsantos 14:9542d3b656ff 101 {
nlsantos 14:9542d3b656ff 102 int numRecords;
nlsantos 14:9542d3b656ff 103 return numRecords;
Jacinta 18:bf6578e82712 104 }
nlsantos 14:9542d3b656ff 105
nlsantos 14:9542d3b656ff 106 int readN(int n)
nlsantos 14:9542d3b656ff 107 {
nlsantos 14:9542d3b656ff 108 int numRecords;
nlsantos 14:9542d3b656ff 109 return numRecords;
nlsantos 14:9542d3b656ff 110 }
nlsantos 14:9542d3b656ff 111
nlsantos 14:9542d3b656ff 112 int deleteN( int n)
nlsantos 14:9542d3b656ff 113 {
nlsantos 14:9542d3b656ff 114 int numRecords;
nlsantos 14:9542d3b656ff 115 return numRecords;
Jacinta 18:bf6578e82712 116 }
nlsantos 14:9542d3b656ff 117
Jacinta 18:bf6578e82712 118 bool setDate()
nlsantos 14:9542d3b656ff 119 {
nlsantos 16:b9cfb5171af6 120 bool success;
nlsantos 16:b9cfb5171af6 121 printf("Enter current date : \n");
nlsantos 16:b9cfb5171af6 122 printf( "YYYY MM DD [enter] \n");
nlsantos 16:b9cfb5171af6 123 scanf ("%d %d %d", &t.tm_year, &t.tm_mon, &t.tm_mday);
nlsantos 16:b9cfb5171af6 124
nlsantos 16:b9cfb5171af6 125 // adjust for tm structure required values
nlsantos 16:b9cfb5171af6 126 t.tm_year = t.tm_year - 1900;
nlsantos 16:b9cfb5171af6 127 t.tm_mon = t.tm_mon - 1;
nlsantos 16:b9cfb5171af6 128 // set the time
Jacinta 18:bf6578e82712 129 set_time(mktime(&t));
nlsantos 16:b9cfb5171af6 130 success= true;
Jacinta 18:bf6578e82712 131
nlsantos 14:9542d3b656ff 132 return success;
Jacinta 18:bf6578e82712 133 }
nlsantos 14:9542d3b656ff 134
Jacinta 18:bf6578e82712 135 bool setTime ()
nlsantos 14:9542d3b656ff 136 {
nlsantos 16:b9cfb5171af6 137 bool success = false;
nlsantos 16:b9cfb5171af6 138
Jacinta 18:bf6578e82712 139 // get the current time from the terminal
nlsantos 16:b9cfb5171af6 140 printf("Enter current time:\n");
nlsantos 16:b9cfb5171af6 141 printf("HH MM SS[enter]\n");
nlsantos 16:b9cfb5171af6 142 scanf("%d %d %d", &t.tm_hour, &t.tm_min, &t.tm_sec);
nlsantos 16:b9cfb5171af6 143
Jacinta 18:bf6578e82712 144 printf("TIME UPDATED TO %d:%d:%d\n", t.tm_hour, t.tm_min, t.tm_sec);
Jacinta 18:bf6578e82712 145
nlsantos 16:b9cfb5171af6 146 // set the time
nlsantos 16:b9cfb5171af6 147 set_time(mktime(&t));
nlsantos 16:b9cfb5171af6 148 success = true;
nlsantos 16:b9cfb5171af6 149
Jacinta 18:bf6578e82712 150 return success;
Jacinta 18:bf6578e82712 151 }
nlsantos 16:b9cfb5171af6 152
Jacinta 18:bf6578e82712 153 void setT(int timePeriod){
Jacinta 18:bf6578e82712 154 scanf("Enter sampling period: %d\n", &timePeriod);
Jacinta 18:bf6578e82712 155 }
Jacinta 18:bf6578e82712 156 /*
Jacinta 18:bf6578e82712 157 void state(char x){
Jacinta 18:bf6578e82712 158 printf("Sampling is %s\n", x);
Jacinta 18:bf6578e82712 159 printf("Enter state:\n");
Jacinta 18:bf6578e82712 160 scanf("%s", x);
Jacinta 18:bf6578e82712 161 }*/
nlsantos 14:9542d3b656ff 162
nlsantos 14:9542d3b656ff 163 bool logTime (int time, bool state)
nlsantos 14:9542d3b656ff 164 {
nlsantos 14:9542d3b656ff 165 return state;
nlsantos 14:9542d3b656ff 166 }
Jacinta 18:bf6578e82712 167
nlsantos 14:9542d3b656ff 168 bool debugMessages (bool state)
nlsantos 14:9542d3b656ff 169 {
Jacinta 18:bf6578e82712 170 return state;
Jacinta 18:bf6578e82712 171 }
nlsantos 14:9542d3b656ff 172
nlsantos 14:9542d3b656ff 173 bool logging(bool state)
nlsantos 14:9542d3b656ff 174 {
nlsantos 14:9542d3b656ff 175 return state;
nlsantos 14:9542d3b656ff 176 }
Wolfgang Betz 0:c71c9af137dd 177
Wolfgang Betz 0:c71c9af137dd 178 /* Simple main function */
nlsantos 14:9542d3b656ff 179 int main()
nlsantos 14:9542d3b656ff 180 {
nlsantos 14:9542d3b656ff 181 uint8_t id;
nlsantos 14:9542d3b656ff 182 float value1, value2;
nlsantos 14:9542d3b656ff 183 char buffer1[32], buffer2[32];
nlsantos 14:9542d3b656ff 184 int32_t axes[3];
nlsantos 15:9de308b656f5 185 int userInstruction;
nlsantos 17:37294fc50c80 186 int n;
Jacinta 18:bf6578e82712 187 int samplePeriod = 15;
Jacinta 18:bf6578e82712 188 char* state = "ON";
Jacinta 18:bf6578e82712 189 bool logState = true;
Jacinta 18:bf6578e82712 190 MailBox mail;
Jacinta 18:bf6578e82712 191 MailBox::log_data log;
nlsantos 14:9542d3b656ff 192
nlsantos 14:9542d3b656ff 193 printf("\r\n--- Starting new run ---\r\n");
Wolfgang Betz 0:c71c9af137dd 194
nlsantos 14:9542d3b656ff 195 humidity_sensor->ReadID(&id);
nlsantos 14:9542d3b656ff 196 printf("HTS221 humidity & temperature = 0x%X\r\n", id);
nlsantos 14:9542d3b656ff 197 pressure_sensor->ReadID(&id);
nlsantos 14:9542d3b656ff 198 printf("LPS25H pressure & temperature = 0x%X\r\n", id);
nlsantos 14:9542d3b656ff 199 magnetometer->ReadID(&id);
nlsantos 14:9542d3b656ff 200 printf("LIS3MDL magnetometer = 0x%X\r\n", id);
nlsantos 14:9542d3b656ff 201 gyroscope->ReadID(&id);
nlsantos 14:9542d3b656ff 202 printf("LSM6DS0 accelerometer & gyroscope = 0x%X\r\n", id);
nlsantos 14:9542d3b656ff 203
nlsantos 14:9542d3b656ff 204 wait(3);
nlsantos 15:9de308b656f5 205
nlsantos 15:9de308b656f5 206 while (userInstruction !=0 || userInstruction <=9)
nlsantos 15:9de308b656f5 207 {
nlsantos 17:37294fc50c80 208 printf ("Please select and option: \n" );
nlsantos 17:37294fc50c80 209 scanf ("%d", &userInstruction);
nlsantos 17:37294fc50c80 210
Jacinta 18:bf6578e82712 211 switch (userInstruction){
nlsantos 17:37294fc50c80 212 case 1:
Jacinta 18:bf6578e82712 213 readAll();
Jacinta 18:bf6578e82712 214 break;
nlsantos 17:37294fc50c80 215 case 2:
Jacinta 18:bf6578e82712 216 deleteAll();
Jacinta 18:bf6578e82712 217 break;
nlsantos 17:37294fc50c80 218 case 3:
Jacinta 18:bf6578e82712 219 readN(n);
Jacinta 18:bf6578e82712 220 break;
nlsantos 17:37294fc50c80 221 case 4:
Jacinta 18:bf6578e82712 222 deleteN(n);
Jacinta 18:bf6578e82712 223 break;
Jacinta 18:bf6578e82712 224 case 5:
Jacinta 18:bf6578e82712 225 setDate();
Jacinta 18:bf6578e82712 226 break;
Jacinta 18:bf6578e82712 227 case 6:
Jacinta 18:bf6578e82712 228 setTime();
Jacinta 18:bf6578e82712 229 break;
Jacinta 18:bf6578e82712 230 case 7:
Jacinta 18:bf6578e82712 231 setT(samplePeriod);
Jacinta 18:bf6578e82712 232 printf("T UPDATED TO %d\n", samplePeriod);
Jacinta 18:bf6578e82712 233 break;
Jacinta 18:bf6578e82712 234 case 8:
Jacinta 18:bf6578e82712 235 //state(state);
Jacinta 18:bf6578e82712 236 break;
Jacinta 18:bf6578e82712 237 case 9:
Jacinta 18:bf6578e82712 238 logging(logState);
Jacinta 18:bf6578e82712 239 break;
Jacinta 18:bf6578e82712 240 }
nlsantos 15:9de308b656f5 241 }
nlsantos 14:9542d3b656ff 242
Jacinta 18:bf6578e82712 243
nlsantos 14:9542d3b656ff 244 while(1) {
nlsantos 14:9542d3b656ff 245 printf("\r\n");
Wolfgang Betz 0:c71c9af137dd 246
nlsantos 14:9542d3b656ff 247 temp_sensor1->GetTemperature(&value1);
nlsantos 14:9542d3b656ff 248 humidity_sensor->GetHumidity(&value2);
nlsantos 14:9542d3b656ff 249 printf("HTS221: [temp] %7s°C, [hum] %s%%\r\n", printDouble(buffer1, value1), printDouble(buffer2, value2));
Wolfgang Betz 0:c71c9af137dd 250
nlsantos 14:9542d3b656ff 251 temp_sensor2->GetFahrenheit(&value1);
nlsantos 14:9542d3b656ff 252 pressure_sensor->GetPressure(&value2);
nlsantos 14:9542d3b656ff 253 printf("LPS25H: [temp] %7s°F, [press] %smbar\r\n", printDouble(buffer1, value1), printDouble(buffer2, value2));
nlsantos 14:9542d3b656ff 254
nlsantos 14:9542d3b656ff 255 printf("---\r\n");
Wolfgang Betz 0:c71c9af137dd 256
nlsantos 14:9542d3b656ff 257 magnetometer->Get_M_Axes(axes);
nlsantos 14:9542d3b656ff 258 printf("LIS3MDL [mag/mgauss]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
Wolfgang Betz 0:c71c9af137dd 259
nlsantos 14:9542d3b656ff 260 accelerometer->Get_X_Axes(axes);
nlsantos 14:9542d3b656ff 261 printf("LSM6DS0 [acc/mg]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
Wolfgang Betz 0:c71c9af137dd 262
nlsantos 14:9542d3b656ff 263 gyroscope->Get_G_Axes(axes);
nlsantos 14:9542d3b656ff 264 printf("LSM6DS0 [gyro/mdps]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
Wolfgang Betz 0:c71c9af137dd 265
Jacinta 18:bf6578e82712 266 wait(samplePeriod);
nlsantos 14:9542d3b656ff 267 }
Jacinta 18:bf6578e82712 268
Jacinta 18:bf6578e82712 269
Jacinta 18:bf6578e82712 270 while(state == "ON"){
Jacinta 18:bf6578e82712 271
Jacinta 18:bf6578e82712 272 //JUST AN EXAMPLE
Jacinta 18:bf6578e82712 273 log.pressure = 4;
Jacinta 18:bf6578e82712 274 log.tempCelcius = 3;
Jacinta 18:bf6578e82712 275
Jacinta 18:bf6578e82712 276 mail.send_thread(log);
Jacinta 18:bf6578e82712 277
Jacinta 18:bf6578e82712 278 wait(samplePeriod);
Jacinta 18:bf6578e82712 279 }
Jacinta 18:bf6578e82712 280
Jacinta 18:bf6578e82712 281
Wolfgang Betz 0:c71c9af137dd 282 }