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

Dependencies:   X_NUCLEO_IKS01A3

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

Go to the documentation of this file.
00001 /**
00002  ******************************************************************************
00003  * @file    main.cpp
00004  * @author  SRA
00005  * @version V1.0.0
00006  * @date    5-March-2019
00007  * @brief   Simple Example application for using the X_NUCLEO_IKS01A3
00008  *          MEMS Inertial & Environmental Sensor Nucleo expansion board.
00009  ******************************************************************************
00010  * @attention
00011  *
00012  * <h2><center>&copy; COPYRIGHT(c) 2019 STMicroelectronics</center></h2>
00013  *
00014  * Redistribution and use in source and binary forms, with or without modification,
00015  * are permitted provided that the following conditions are met:
00016  *   1. Redistributions of source code must retain the above copyright notice,
00017  *      this list of conditions and the following disclaimer.
00018  *   2. Redistributions in binary form must reproduce the above copyright notice,
00019  *      this list of conditions and the following disclaimer in the documentation
00020  *      and/or other materials provided with the distribution.
00021  *   3. Neither the name of STMicroelectronics nor the names of its contributors
00022  *      may be used to endorse or promote products derived from this software
00023  *      without specific prior written permission.
00024  *
00025  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00026  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00028  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00029  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00030  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00031  *  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00032  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00033  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00034  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00035  *
00036  ******************************************************************************
00037 */
00038 
00039 /* Includes */
00040 #include "mbed.h"
00041 #include "rtos.h"
00042 #include "XNucleoIKS01A3.h"
00043 
00044 /* Instantiate the expansion board */
00045 static XNucleoIKS01A3 *mems_expansion_board = XNucleoIKS01A3::instance(D14, D15, D4, D5, A3, D6, A4);
00046 
00047 /* Retrieve the composing elements of the expansion board */
00048 static STTS751Sensor *t_sensor = mems_expansion_board->t_sensor;
00049 
00050 DigitalOut myled(LED1);
00051 
00052 volatile int mems_event = 0;
00053 uint32_t previous_tick = 0;
00054 uint32_t current_tick = 0;
00055 uint8_t high = 0, low = 0;
00056 float temperature = 0.0f;
00057 char buffer[32];
00058 
00059 void INT_cb();
00060 
00061 /* Helper function for printing floats & doubles */
00062 static char *print_double(char *str, double v, int decimalDigits = 2)
00063 {
00064     int i = 1;
00065     int intPart, fractPart;
00066     int len;
00067     char *ptr;
00068 
00069     /* prepare decimal digits multiplicator */
00070     for (; decimalDigits != 0; i *= 10, decimalDigits--);
00071 
00072     /* calculate integer & fractinal parts */
00073     intPart = (int)v;
00074     fractPart = (int)((v - (double)(int)v) * i);
00075 
00076     /* fill in integer part */
00077     sprintf(str, "%i.", intPart);
00078 
00079     /* prepare fill in of fractional part */
00080     len = strlen(str);
00081     ptr = &str[len];
00082 
00083     /* fill in leading fractional zeros */
00084     for (i /= 10; i > 1; i /= 10, ptr++) {
00085         if (fractPart >= i) {
00086             break;
00087         }
00088         *ptr = '0';
00089     }
00090 
00091     /* fill in (rest of) fractional part */
00092     sprintf(ptr, "%i", fractPart);
00093 
00094     return str;
00095 }
00096 
00097 /* Simple main function */
00098 int main()
00099 {
00100     /* Attach callback to STTS751 INT */
00101     t_sensor->attach_int_irq(&INT_cb);
00102 
00103     /* Enable STTS751 temperature sensor */
00104     t_sensor->enable();
00105     /* Set ODR to 4Hz */
00106     t_sensor->set_odr(4.0f);
00107     /* Set Low Temperature Threshold */
00108     t_sensor->set_low_temp_thr(22.0f);
00109     /* Set High Temperature Threshold */
00110     t_sensor->set_high_temp_thr(28.0f);
00111     /* Enable Event pin */
00112     t_sensor->set_event_pin(1);
00113     /* Get beginning status */
00114     t_sensor->get_temp_limit_status(NULL, NULL, NULL);
00115 
00116     previous_tick = clock();
00117 
00118     printf("\r\n--- Starting new run ---\r\n");
00119 
00120     while (1) {
00121         if (mems_event) {
00122             mems_event = 0;
00123             uint8_t high_temp = 0, low_temp = 0;
00124             t_sensor->get_temp_limit_status(&high_temp, &low_temp, NULL);
00125             if (high_temp) {
00126                 high = 1;
00127                 low = 0;
00128             }
00129             if (low_temp) {
00130                 low = 1;
00131                 high = 0;
00132             }
00133 
00134             t_sensor->get_temperature(&temperature);
00135             myled = 1;
00136             ThisThread::sleep_for(100);
00137             myled = 0;
00138         }
00139 
00140         current_tick = clock();
00141         if (((current_tick - previous_tick) / CLOCKS_PER_SEC) >= 2) {
00142             if (!high && !low) {
00143                 t_sensor->get_temperature(&temperature);
00144             }
00145             printf("Temp[C]: ");
00146             printf("%7s C", print_double(buffer, temperature));
00147             if (high) {
00148                 printf(" High temperature detected!(>28C) \r\n");
00149                 high = 0;
00150             } else if (low) {
00151                 printf(" Low temperature detected!(<22C) \r\n");
00152                 low = 0;
00153             } else {
00154                 printf("\r\n");
00155             }
00156             previous_tick = clock();
00157         }
00158     }
00159 }
00160 
00161 void INT_cb()
00162 {
00163     mems_event = 1;
00164 }