Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed X_NUCLEO_IKS01A2
main.cpp@18:04cf351e841f, 2020-12-22 (annotated)
- Committer:
- evanjp05
- Date:
- Tue Dec 22 23:56:27 2020 +0000
- Revision:
- 18:04cf351e841f
- Parent:
- 13:fc873da5b445
works;
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| cparata | 0:69566eea0fba | 1 | /** |
| cparata | 0:69566eea0fba | 2 | ****************************************************************************** |
| cparata | 0:69566eea0fba | 3 | * @file main.cpp |
| cparata | 8:8f495e604424 | 4 | * @author CLab |
| cparata | 8:8f495e604424 | 5 | * @version V1.0.0 |
| cparata | 8:8f495e604424 | 6 | * @date 2-December-2016 |
| cparata | 0:69566eea0fba | 7 | * @brief Simple Example application for using the X_NUCLEO_IKS01A1 |
| cparata | 0:69566eea0fba | 8 | * MEMS Inertial & Environmental Sensor Nucleo expansion board. |
| cparata | 0:69566eea0fba | 9 | ****************************************************************************** |
| cparata | 0:69566eea0fba | 10 | * @attention |
| cparata | 0:69566eea0fba | 11 | * |
| cparata | 0:69566eea0fba | 12 | * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2> |
| cparata | 0:69566eea0fba | 13 | * |
| cparata | 0:69566eea0fba | 14 | * Redistribution and use in source and binary forms, with or without modification, |
| cparata | 0:69566eea0fba | 15 | * are permitted provided that the following conditions are met: |
| cparata | 0:69566eea0fba | 16 | * 1. Redistributions of source code must retain the above copyright notice, |
| cparata | 0:69566eea0fba | 17 | * this list of conditions and the following disclaimer. |
| cparata | 0:69566eea0fba | 18 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| cparata | 0:69566eea0fba | 19 | * this list of conditions and the following disclaimer in the documentation |
| cparata | 0:69566eea0fba | 20 | * and/or other materials provided with the distribution. |
| cparata | 0:69566eea0fba | 21 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
| cparata | 0:69566eea0fba | 22 | * may be used to endorse or promote products derived from this software |
| cparata | 0:69566eea0fba | 23 | * without specific prior written permission. |
| cparata | 0:69566eea0fba | 24 | * |
| cparata | 0:69566eea0fba | 25 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| cparata | 0:69566eea0fba | 26 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| cparata | 0:69566eea0fba | 27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| cparata | 0:69566eea0fba | 28 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| cparata | 0:69566eea0fba | 29 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| cparata | 0:69566eea0fba | 30 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| cparata | 0:69566eea0fba | 31 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| cparata | 0:69566eea0fba | 32 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| cparata | 0:69566eea0fba | 33 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| cparata | 0:69566eea0fba | 34 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| cparata | 0:69566eea0fba | 35 | * |
| cparata | 0:69566eea0fba | 36 | ****************************************************************************** |
| cparata | 0:69566eea0fba | 37 | */ |
| cparata | 0:69566eea0fba | 38 | |
| cparata | 0:69566eea0fba | 39 | /* Includes */ |
| cparata | 0:69566eea0fba | 40 | #include "mbed.h" |
| davide.aliprandi@st.com | 13:fc873da5b445 | 41 | #include "XNucleoIKS01A2.h" |
| evanjp05 | 18:04cf351e841f | 42 | #include <stdio.h> |
| evanjp05 | 18:04cf351e841f | 43 | #include <time.h> |
| cparata | 0:69566eea0fba | 44 | |
| cparata | 0:69566eea0fba | 45 | /* Instantiate the expansion board */ |
| davide.aliprandi@st.com | 13:fc873da5b445 | 46 | static XNucleoIKS01A2 *mems_expansion_board = XNucleoIKS01A2::instance(D14, D15, D4, D5); |
| cparata | 0:69566eea0fba | 47 | |
| cparata | 0:69566eea0fba | 48 | /* Retrieve the composing elements of the expansion board */ |
| evanjp05 | 18:04cf351e841f | 49 | //static LSM303AGRMagSensor *magnetometer = mems_expansion_board->magnetometer; |
| cparata | 0:69566eea0fba | 50 | static HTS221Sensor *hum_temp = mems_expansion_board->ht_sensor; |
| cparata | 0:69566eea0fba | 51 | static LPS22HBSensor *press_temp = mems_expansion_board->pt_sensor; |
| evanjp05 | 18:04cf351e841f | 52 | |
| cparata | 0:69566eea0fba | 53 | |
| cparata | 0:69566eea0fba | 54 | /* Helper function for printing floats & doubles */ |
| davide.aliprandi@st.com | 13:fc873da5b445 | 55 | static char *print_double(char* str, double v, int decimalDigits=2) |
| cparata | 0:69566eea0fba | 56 | { |
| cparata | 0:69566eea0fba | 57 | int i = 1; |
| cparata | 0:69566eea0fba | 58 | int intPart, fractPart; |
| cparata | 0:69566eea0fba | 59 | int len; |
| cparata | 0:69566eea0fba | 60 | char *ptr; |
| cparata | 0:69566eea0fba | 61 | |
| cparata | 0:69566eea0fba | 62 | /* prepare decimal digits multiplicator */ |
| cparata | 0:69566eea0fba | 63 | for (;decimalDigits!=0; i*=10, decimalDigits--); |
| cparata | 0:69566eea0fba | 64 | |
| cparata | 0:69566eea0fba | 65 | /* calculate integer & fractinal parts */ |
| cparata | 0:69566eea0fba | 66 | intPart = (int)v; |
| cparata | 0:69566eea0fba | 67 | fractPart = (int)((v-(double)(int)v)*i); |
| cparata | 0:69566eea0fba | 68 | |
| cparata | 0:69566eea0fba | 69 | /* fill in integer part */ |
| cparata | 0:69566eea0fba | 70 | sprintf(str, "%i.", intPart); |
| cparata | 0:69566eea0fba | 71 | |
| cparata | 0:69566eea0fba | 72 | /* prepare fill in of fractional part */ |
| cparata | 0:69566eea0fba | 73 | len = strlen(str); |
| cparata | 0:69566eea0fba | 74 | ptr = &str[len]; |
| cparata | 0:69566eea0fba | 75 | |
| cparata | 0:69566eea0fba | 76 | /* fill in leading fractional zeros */ |
| cparata | 0:69566eea0fba | 77 | for (i/=10;i>1; i/=10, ptr++) { |
| davide.aliprandi@st.com | 13:fc873da5b445 | 78 | if (fractPart >= i) { |
| davide.aliprandi@st.com | 13:fc873da5b445 | 79 | break; |
| davide.aliprandi@st.com | 13:fc873da5b445 | 80 | } |
| cparata | 0:69566eea0fba | 81 | *ptr = '0'; |
| cparata | 0:69566eea0fba | 82 | } |
| cparata | 0:69566eea0fba | 83 | |
| cparata | 0:69566eea0fba | 84 | /* fill in (rest of) fractional part */ |
| cparata | 0:69566eea0fba | 85 | sprintf(ptr, "%i", fractPart); |
| cparata | 0:69566eea0fba | 86 | |
| cparata | 0:69566eea0fba | 87 | return str; |
| cparata | 0:69566eea0fba | 88 | } |
| cparata | 0:69566eea0fba | 89 | |
| cparata | 0:69566eea0fba | 90 | /* Simple main function */ |
| cparata | 0:69566eea0fba | 91 | int main() { |
| evanjp05 | 18:04cf351e841f | 92 | float F; |
| cparata | 0:69566eea0fba | 93 | uint8_t id; |
| evanjp05 | 18:04cf351e841f | 94 | int offset = 5; //in order to calibrate the temp sensor |
| cparata | 0:69566eea0fba | 95 | float value1, value2; |
| evanjp05 | 18:04cf351e841f | 96 | //char buffer1[32], buffer2[32]; |
| evanjp05 | 18:04cf351e841f | 97 | //int32_t axes[3]; |
| cparata | 0:69566eea0fba | 98 | |
| cparata | 0:69566eea0fba | 99 | /* Enable all sensors */ |
| davide.aliprandi@st.com | 13:fc873da5b445 | 100 | hum_temp->enable(); |
| davide.aliprandi@st.com | 13:fc873da5b445 | 101 | press_temp->enable(); |
| evanjp05 | 18:04cf351e841f | 102 | |
| evanjp05 | 18:04cf351e841f | 103 | |
| evanjp05 | 18:04cf351e841f | 104 | |
| cparata | 0:69566eea0fba | 105 | |
| cparata | 0:69566eea0fba | 106 | printf("\r\n--- Starting new run ---\r\n"); |
| evanjp05 | 18:04cf351e841f | 107 | |
| evanjp05 | 18:04cf351e841f | 108 | /* Time */ |
| evanjp05 | 18:04cf351e841f | 109 | //Timer t; |
| evanjp05 | 18:04cf351e841f | 110 | set_time(1608670310); |
| evanjp05 | 18:04cf351e841f | 111 | |
| evanjp05 | 18:04cf351e841f | 112 | |
| davide.aliprandi@st.com | 13:fc873da5b445 | 113 | hum_temp->read_id(&id); |
| evanjp05 | 18:04cf351e841f | 114 | |
| davide.aliprandi@st.com | 13:fc873da5b445 | 115 | press_temp->read_id(&id); |
| evanjp05 | 18:04cf351e841f | 116 | |
| evanjp05 | 18:04cf351e841f | 117 | |
| cparata | 0:69566eea0fba | 118 | |
| cparata | 0:69566eea0fba | 119 | while(1) { |
| cparata | 0:69566eea0fba | 120 | printf("\r\n"); |
| evanjp05 | 18:04cf351e841f | 121 | // t.start(); |
| davide.aliprandi@st.com | 13:fc873da5b445 | 122 | hum_temp->get_temperature(&value1); |
| evanjp05 | 18:04cf351e841f | 123 | time_t seconds = time(NULL); |
| cparata | 0:69566eea0fba | 124 | |
| evanjp05 | 18:04cf351e841f | 125 | F = ((value1 * 9.0) / 5.0 + 32) - offset; |
| evanjp05 | 18:04cf351e841f | 126 | |
| cparata | 0:69566eea0fba | 127 | |
| evanjp05 | 18:04cf351e841f | 128 | printf("%u, ", (unsigned int)seconds); |
| evanjp05 | 18:04cf351e841f | 129 | //printf("%f, ", t.read()); |
| evanjp05 | 18:04cf351e841f | 130 | printf(" %7s", print_double(buffer1, F)); |
| evanjp05 | 18:04cf351e841f | 131 | //printf(" %s", buffer); |
| cparata | 0:69566eea0fba | 132 | |
| evanjp05 | 18:04cf351e841f | 133 | wait(1); |
| evanjp05 | 18:04cf351e841f | 134 | // wait(1200); |
| cparata | 0:69566eea0fba | 135 | } |
| cparata | 0:69566eea0fba | 136 | } |