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:
Thu Oct 29 13:51:32 2020 +0000
Revision:
7:83916fab9585
Parent:
6:acde79c5a18a
Update LSM6DSO library

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 5:b747a8948606 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 5:b747a8948606 37 */
cparata 5:b747a8948606 38
cparata 0:1399adb8f248 39 /* Includes */
cparata 0:1399adb8f248 40 #include "mbed.h"
cparata 6:acde79c5a18a 41 #include "rtos.h"
cparata 0:1399adb8f248 42 #include "XNucleoIKS01A3.h"
cparata 5:b747a8948606 43
cparata 0:1399adb8f248 44 /* Instantiate the expansion board */
cparata 0:1399adb8f248 45 static XNucleoIKS01A3 *mems_expansion_board = XNucleoIKS01A3::instance(D14, D15, D4, D5, A3, D6, A4);
cparata 5:b747a8948606 46
cparata 0:1399adb8f248 47 /* Retrieve the composing elements of the expansion board */
cparata 0:1399adb8f248 48 static STTS751Sensor *t_sensor = mems_expansion_board->t_sensor;
cparata 0:1399adb8f248 49
cparata 0:1399adb8f248 50 DigitalOut myled(LED1);
cparata 5:b747a8948606 51
cparata 0:1399adb8f248 52 volatile int mems_event = 0;
cparata 0:1399adb8f248 53 uint32_t previous_tick = 0;
cparata 0:1399adb8f248 54 uint32_t current_tick = 0;
cparata 0:1399adb8f248 55 uint8_t high = 0, low = 0;
cparata 0:1399adb8f248 56 float temperature = 0.0f;
cparata 0:1399adb8f248 57 char buffer[32];
cparata 0:1399adb8f248 58
cparata 0:1399adb8f248 59 void INT_cb();
cparata 0:1399adb8f248 60
cparata 0:1399adb8f248 61 /* Helper function for printing floats & doubles */
cparata 5:b747a8948606 62 static char *print_double(char *str, double v, int decimalDigits = 2)
cparata 0:1399adb8f248 63 {
cparata 5:b747a8948606 64 int i = 1;
cparata 5:b747a8948606 65 int intPart, fractPart;
cparata 5:b747a8948606 66 int len;
cparata 5:b747a8948606 67 char *ptr;
cparata 0:1399adb8f248 68
cparata 5:b747a8948606 69 /* prepare decimal digits multiplicator */
cparata 5:b747a8948606 70 for (; decimalDigits != 0; i *= 10, decimalDigits--);
cparata 0:1399adb8f248 71
cparata 5:b747a8948606 72 /* calculate integer & fractinal parts */
cparata 5:b747a8948606 73 intPart = (int)v;
cparata 5:b747a8948606 74 fractPart = (int)((v - (double)(int)v) * i);
cparata 0:1399adb8f248 75
cparata 5:b747a8948606 76 /* fill in integer part */
cparata 5:b747a8948606 77 sprintf(str, "%i.", intPart);
cparata 0:1399adb8f248 78
cparata 5:b747a8948606 79 /* prepare fill in of fractional part */
cparata 5:b747a8948606 80 len = strlen(str);
cparata 5:b747a8948606 81 ptr = &str[len];
cparata 0:1399adb8f248 82
cparata 5:b747a8948606 83 /* fill in leading fractional zeros */
cparata 5:b747a8948606 84 for (i /= 10; i > 1; i /= 10, ptr++) {
cparata 5:b747a8948606 85 if (fractPart >= i) {
cparata 5:b747a8948606 86 break;
cparata 5:b747a8948606 87 }
cparata 5:b747a8948606 88 *ptr = '0';
cparata 0:1399adb8f248 89 }
cparata 0:1399adb8f248 90
cparata 5:b747a8948606 91 /* fill in (rest of) fractional part */
cparata 5:b747a8948606 92 sprintf(ptr, "%i", fractPart);
cparata 0:1399adb8f248 93
cparata 5:b747a8948606 94 return str;
cparata 0:1399adb8f248 95 }
cparata 0:1399adb8f248 96
cparata 0:1399adb8f248 97 /* Simple main function */
cparata 5:b747a8948606 98 int main()
cparata 5:b747a8948606 99 {
cparata 5:b747a8948606 100 /* Attach callback to STTS751 INT */
cparata 5:b747a8948606 101 t_sensor->attach_int_irq(&INT_cb);
cparata 5:b747a8948606 102
cparata 5:b747a8948606 103 /* Enable STTS751 temperature sensor */
cparata 5:b747a8948606 104 t_sensor->enable();
cparata 5:b747a8948606 105 /* Set ODR to 4Hz */
cparata 5:b747a8948606 106 t_sensor->set_odr(4.0f);
cparata 5:b747a8948606 107 /* Set Low Temperature Threshold */
cparata 5:b747a8948606 108 t_sensor->set_low_temp_thr(22.0f);
cparata 5:b747a8948606 109 /* Set High Temperature Threshold */
cparata 5:b747a8948606 110 t_sensor->set_high_temp_thr(28.0f);
cparata 5:b747a8948606 111 /* Enable Event pin */
cparata 5:b747a8948606 112 t_sensor->set_event_pin(1);
cparata 5:b747a8948606 113 /* Get beginning status */
cparata 5:b747a8948606 114 t_sensor->get_temp_limit_status(NULL, NULL, NULL);
cparata 5:b747a8948606 115
cparata 5:b747a8948606 116 previous_tick = clock();
cparata 5:b747a8948606 117
cparata 5:b747a8948606 118 printf("\r\n--- Starting new run ---\r\n");
cparata 0:1399adb8f248 119
cparata 5:b747a8948606 120 while (1) {
cparata 5:b747a8948606 121 if (mems_event) {
cparata 5:b747a8948606 122 mems_event = 0;
cparata 5:b747a8948606 123 uint8_t high_temp = 0, low_temp = 0;
cparata 5:b747a8948606 124 t_sensor->get_temp_limit_status(&high_temp, &low_temp, NULL);
cparata 5:b747a8948606 125 if (high_temp) {
cparata 5:b747a8948606 126 high = 1;
cparata 5:b747a8948606 127 low = 0;
cparata 5:b747a8948606 128 }
cparata 5:b747a8948606 129 if (low_temp) {
cparata 5:b747a8948606 130 low = 1;
cparata 5:b747a8948606 131 high = 0;
cparata 5:b747a8948606 132 }
cparata 5:b747a8948606 133
cparata 5:b747a8948606 134 t_sensor->get_temperature(&temperature);
cparata 5:b747a8948606 135 myled = 1;
cparata 6:acde79c5a18a 136 ThisThread::sleep_for(100);
cparata 5:b747a8948606 137 myled = 0;
cparata 5:b747a8948606 138 }
cparata 0:1399adb8f248 139
cparata 5:b747a8948606 140 current_tick = clock();
cparata 5:b747a8948606 141 if (((current_tick - previous_tick) / CLOCKS_PER_SEC) >= 2) {
cparata 5:b747a8948606 142 if (!high && !low) {
cparata 5:b747a8948606 143 t_sensor->get_temperature(&temperature);
cparata 5:b747a8948606 144 }
cparata 5:b747a8948606 145 printf("Temp[C]: ");
cparata 5:b747a8948606 146 printf("%7s C", print_double(buffer, temperature));
cparata 5:b747a8948606 147 if (high) {
cparata 5:b747a8948606 148 printf(" High temperature detected!(>28C) \r\n");
cparata 5:b747a8948606 149 high = 0;
cparata 5:b747a8948606 150 } else if (low) {
cparata 5:b747a8948606 151 printf(" Low temperature detected!(<22C) \r\n");
cparata 5:b747a8948606 152 low = 0;
cparata 5:b747a8948606 153 } else {
cparata 5:b747a8948606 154 printf("\r\n");
cparata 5:b747a8948606 155 }
cparata 5:b747a8948606 156 previous_tick = clock();
cparata 5:b747a8948606 157 }
cparata 0:1399adb8f248 158 }
cparata 0:1399adb8f248 159 }
cparata 5:b747a8948606 160
cparata 5:b747a8948606 161 void INT_cb()
cparata 5:b747a8948606 162 {
cparata 5:b747a8948606 163 mems_event = 1;
cparata 0:1399adb8f248 164 }