Example of temperature limit detection for STTS751 in X-NUCLEO-IKS01A3

Dependencies:   X_NUCLEO_IKS01A3

Temperature Limit Demo Application with STTS751 based on sensor expansion board X-NUCLEO-IKS01A3

Main function is to show how to detect exceeding of temperature limits using the sensor expansion board and send a notification using UART to a connected PC or Desktop and display it on terminal applications like TeraTerm.
After connection has been established:
- the user can heat up or cool down the board and view the data using an hyper terminal.

Committer:
cparata
Date:
Fri May 24 14:41:02 2019 +0000
Revision:
3:3c8bce3cdf2f
Parent:
0:1399adb8f248
Child:
5:b747a8948606
Update mbed library to support mbed-os

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cparata 0:1399adb8f248 1 /**
cparata 0:1399adb8f248 2 ******************************************************************************
cparata 0:1399adb8f248 3 * @file main.cpp
cparata 0:1399adb8f248 4 * @author SRA
cparata 0:1399adb8f248 5 * @version V1.0.0
cparata 0:1399adb8f248 6 * @date 5-March-2019
cparata 0:1399adb8f248 7 * @brief Simple Example application for using the X_NUCLEO_IKS01A3
cparata 0:1399adb8f248 8 * MEMS Inertial & Environmental Sensor Nucleo expansion board.
cparata 0:1399adb8f248 9 ******************************************************************************
cparata 0:1399adb8f248 10 * @attention
cparata 0:1399adb8f248 11 *
cparata 0:1399adb8f248 12 * <h2><center>&copy; COPYRIGHT(c) 2019 STMicroelectronics</center></h2>
cparata 0:1399adb8f248 13 *
cparata 0:1399adb8f248 14 * Redistribution and use in source and binary forms, with or without modification,
cparata 0:1399adb8f248 15 * are permitted provided that the following conditions are met:
cparata 0:1399adb8f248 16 * 1. Redistributions of source code must retain the above copyright notice,
cparata 0:1399adb8f248 17 * this list of conditions and the following disclaimer.
cparata 0:1399adb8f248 18 * 2. Redistributions in binary form must reproduce the above copyright notice,
cparata 0:1399adb8f248 19 * this list of conditions and the following disclaimer in the documentation
cparata 0:1399adb8f248 20 * and/or other materials provided with the distribution.
cparata 0:1399adb8f248 21 * 3. Neither the name of STMicroelectronics nor the names of its contributors
cparata 0:1399adb8f248 22 * may be used to endorse or promote products derived from this software
cparata 0:1399adb8f248 23 * without specific prior written permission.
cparata 0:1399adb8f248 24 *
cparata 0:1399adb8f248 25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
cparata 0:1399adb8f248 26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
cparata 0:1399adb8f248 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
cparata 0:1399adb8f248 28 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
cparata 0:1399adb8f248 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
cparata 0:1399adb8f248 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
cparata 0:1399adb8f248 31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
cparata 0:1399adb8f248 32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
cparata 0:1399adb8f248 33 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
cparata 0:1399adb8f248 34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
cparata 0:1399adb8f248 35 *
cparata 0:1399adb8f248 36 ******************************************************************************
cparata 0:1399adb8f248 37 */
cparata 0:1399adb8f248 38
cparata 0:1399adb8f248 39 /* Includes */
cparata 0:1399adb8f248 40 #include "mbed.h"
cparata 0:1399adb8f248 41 #include "XNucleoIKS01A3.h"
cparata 0:1399adb8f248 42
cparata 0:1399adb8f248 43 /* Instantiate the expansion board */
cparata 0:1399adb8f248 44 static XNucleoIKS01A3 *mems_expansion_board = XNucleoIKS01A3::instance(D14, D15, D4, D5, A3, D6, A4);
cparata 0:1399adb8f248 45
cparata 0:1399adb8f248 46 /* Retrieve the composing elements of the expansion board */
cparata 0:1399adb8f248 47 static STTS751Sensor *t_sensor = mems_expansion_board->t_sensor;
cparata 0:1399adb8f248 48
cparata 0:1399adb8f248 49 DigitalOut myled(LED1);
cparata 0:1399adb8f248 50
cparata 0:1399adb8f248 51 volatile int mems_event = 0;
cparata 0:1399adb8f248 52 uint32_t previous_tick = 0;
cparata 0:1399adb8f248 53 uint32_t current_tick = 0;
cparata 0:1399adb8f248 54 uint8_t high = 0, low = 0;
cparata 0:1399adb8f248 55 float temperature = 0.0f;
cparata 0:1399adb8f248 56 char buffer[32];
cparata 0:1399adb8f248 57
cparata 0:1399adb8f248 58 void INT_cb();
cparata 0:1399adb8f248 59
cparata 0:1399adb8f248 60 /* Helper function for printing floats & doubles */
cparata 0:1399adb8f248 61 static char *print_double(char* str, double v, int decimalDigits=2)
cparata 0:1399adb8f248 62 {
cparata 0:1399adb8f248 63 int i = 1;
cparata 0:1399adb8f248 64 int intPart, fractPart;
cparata 0:1399adb8f248 65 int len;
cparata 0:1399adb8f248 66 char *ptr;
cparata 0:1399adb8f248 67
cparata 0:1399adb8f248 68 /* prepare decimal digits multiplicator */
cparata 0:1399adb8f248 69 for (;decimalDigits!=0; i*=10, decimalDigits--);
cparata 0:1399adb8f248 70
cparata 0:1399adb8f248 71 /* calculate integer & fractinal parts */
cparata 0:1399adb8f248 72 intPart = (int)v;
cparata 0:1399adb8f248 73 fractPart = (int)((v-(double)(int)v)*i);
cparata 0:1399adb8f248 74
cparata 0:1399adb8f248 75 /* fill in integer part */
cparata 0:1399adb8f248 76 sprintf(str, "%i.", intPart);
cparata 0:1399adb8f248 77
cparata 0:1399adb8f248 78 /* prepare fill in of fractional part */
cparata 0:1399adb8f248 79 len = strlen(str);
cparata 0:1399adb8f248 80 ptr = &str[len];
cparata 0:1399adb8f248 81
cparata 0:1399adb8f248 82 /* fill in leading fractional zeros */
cparata 0:1399adb8f248 83 for (i/=10;i>1; i/=10, ptr++) {
cparata 0:1399adb8f248 84 if (fractPart >= i) {
cparata 0:1399adb8f248 85 break;
cparata 0:1399adb8f248 86 }
cparata 0:1399adb8f248 87 *ptr = '0';
cparata 0:1399adb8f248 88 }
cparata 0:1399adb8f248 89
cparata 0:1399adb8f248 90 /* fill in (rest of) fractional part */
cparata 0:1399adb8f248 91 sprintf(ptr, "%i", fractPart);
cparata 0:1399adb8f248 92
cparata 0:1399adb8f248 93 return str;
cparata 0:1399adb8f248 94 }
cparata 0:1399adb8f248 95
cparata 0:1399adb8f248 96 /* Simple main function */
cparata 0:1399adb8f248 97 int main() {
cparata 0:1399adb8f248 98 /* Attach callback to STTS751 INT */
cparata 0:1399adb8f248 99 t_sensor->attach_int_irq(&INT_cb);
cparata 0:1399adb8f248 100
cparata 3:3c8bce3cdf2f 101 /* Enable STTS751 temperature sensor */
cparata 0:1399adb8f248 102 t_sensor->enable();
cparata 0:1399adb8f248 103 /* Set ODR to 4Hz */
cparata 0:1399adb8f248 104 t_sensor->set_odr(4.0f);
cparata 0:1399adb8f248 105 /* Set Low Temperature Threshold */
cparata 0:1399adb8f248 106 t_sensor->set_low_temp_thr(22.0f);
cparata 0:1399adb8f248 107 /* Set High Temperature Threshold */
cparata 0:1399adb8f248 108 t_sensor->set_high_temp_thr(28.0f);
cparata 0:1399adb8f248 109 /* Enable Event pin */
cparata 0:1399adb8f248 110 t_sensor->set_event_pin(1);
cparata 0:1399adb8f248 111 /* Get beginning status */
cparata 0:1399adb8f248 112 t_sensor->get_temp_limit_status(NULL, NULL, NULL);
cparata 0:1399adb8f248 113
cparata 0:1399adb8f248 114 previous_tick = clock();
cparata 0:1399adb8f248 115
cparata 0:1399adb8f248 116 printf("\r\n--- Starting new run ---\r\n");
cparata 0:1399adb8f248 117
cparata 0:1399adb8f248 118 while(1) {
cparata 0:1399adb8f248 119 if (mems_event) {
cparata 0:1399adb8f248 120 mems_event=0;
cparata 0:1399adb8f248 121 uint8_t high_temp = 0, low_temp = 0;
cparata 0:1399adb8f248 122 t_sensor->get_temp_limit_status(&high_temp, &low_temp, NULL);
cparata 0:1399adb8f248 123 if (high_temp){
cparata 0:1399adb8f248 124 high = 1;
cparata 0:1399adb8f248 125 low = 0;
cparata 0:1399adb8f248 126 }
cparata 0:1399adb8f248 127 if (low_temp){
cparata 0:1399adb8f248 128 low = 1;
cparata 0:1399adb8f248 129 high = 0;
cparata 0:1399adb8f248 130 }
cparata 0:1399adb8f248 131
cparata 0:1399adb8f248 132 t_sensor->get_temperature(&temperature);
cparata 0:1399adb8f248 133 myled = 1;
cparata 0:1399adb8f248 134 wait(0.1);
cparata 0:1399adb8f248 135 myled = 0;
cparata 0:1399adb8f248 136 }
cparata 0:1399adb8f248 137
cparata 0:1399adb8f248 138 current_tick = clock();
cparata 0:1399adb8f248 139 if (((current_tick - previous_tick)/CLOCKS_PER_SEC) >= 2){
cparata 0:1399adb8f248 140 if (!high && !low){
cparata 0:1399adb8f248 141 t_sensor->get_temperature(&temperature);
cparata 0:1399adb8f248 142 }
cparata 0:1399adb8f248 143 printf("Temp[C]: ");
cparata 0:1399adb8f248 144 printf("%7s C", print_double(buffer, temperature));
cparata 0:1399adb8f248 145 if (high){
cparata 0:1399adb8f248 146 printf(" High temperature detected!(>28C) \r\n");
cparata 0:1399adb8f248 147 high = 0;
cparata 0:1399adb8f248 148 } else if (low) {
cparata 0:1399adb8f248 149 printf(" Low temperature detected!(<22C) \r\n");
cparata 0:1399adb8f248 150 low = 0;
cparata 0:1399adb8f248 151 } else {
cparata 0:1399adb8f248 152 printf("\r\n");
cparata 0:1399adb8f248 153 }
cparata 0:1399adb8f248 154 previous_tick = clock();
cparata 0:1399adb8f248 155 }
cparata 0:1399adb8f248 156 }
cparata 0:1399adb8f248 157 }
cparata 0:1399adb8f248 158
cparata 0:1399adb8f248 159 void INT_cb() {
cparata 0:1399adb8f248 160 mems_event = 1;
cparata 0:1399adb8f248 161 }