Matthew Lister
/
exporttest
test
main.cpp@1:ae7fdbddbf14, 2019-07-10 (annotated)
- Committer:
- chocolatepatty
- Date:
- Wed Jul 10 09:53:36 2019 +0000
- Revision:
- 1:ae7fdbddbf14
- Parent:
- 0:16560d6a2529
uhp;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
chocolatepatty | 1:ae7fdbddbf14 | 1 | /** |
chocolatepatty | 1:ae7fdbddbf14 | 2 | ****************************************************************************** |
chocolatepatty | 1:ae7fdbddbf14 | 3 | * @file main.cpp |
chocolatepatty | 1:ae7fdbddbf14 | 4 | * @author AST / EST |
chocolatepatty | 1:ae7fdbddbf14 | 5 | * @version V0.0.1 |
chocolatepatty | 1:ae7fdbddbf14 | 6 | * @date 14-August-2015 |
chocolatepatty | 1:ae7fdbddbf14 | 7 | * @brief Simple Example application for using the X_NUCLEO_IKS01A1 |
chocolatepatty | 1:ae7fdbddbf14 | 8 | * MEMS Inertial & Environmental Sensor Nucleo expansion board. |
chocolatepatty | 1:ae7fdbddbf14 | 9 | ****************************************************************************** |
chocolatepatty | 1:ae7fdbddbf14 | 10 | * @attention |
chocolatepatty | 1:ae7fdbddbf14 | 11 | * |
chocolatepatty | 1:ae7fdbddbf14 | 12 | * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2> |
chocolatepatty | 1:ae7fdbddbf14 | 13 | * |
chocolatepatty | 1:ae7fdbddbf14 | 14 | * Redistribution and use in source and binary forms, with or without modification, |
chocolatepatty | 1:ae7fdbddbf14 | 15 | * are permitted provided that the following conditions are met: |
chocolatepatty | 1:ae7fdbddbf14 | 16 | * 1. Redistributions of source code must retain the above copyright notice, |
chocolatepatty | 1:ae7fdbddbf14 | 17 | * this list of conditions and the following disclaimer. |
chocolatepatty | 1:ae7fdbddbf14 | 18 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
chocolatepatty | 1:ae7fdbddbf14 | 19 | * this list of conditions and the following disclaimer in the documentation |
chocolatepatty | 1:ae7fdbddbf14 | 20 | * and/or other materials provided with the distribution. |
chocolatepatty | 1:ae7fdbddbf14 | 21 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
chocolatepatty | 1:ae7fdbddbf14 | 22 | * may be used to endorse or promote products derived from this software |
chocolatepatty | 1:ae7fdbddbf14 | 23 | * without specific prior written permission. |
chocolatepatty | 1:ae7fdbddbf14 | 24 | * |
chocolatepatty | 1:ae7fdbddbf14 | 25 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
chocolatepatty | 1:ae7fdbddbf14 | 26 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
chocolatepatty | 1:ae7fdbddbf14 | 27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
chocolatepatty | 1:ae7fdbddbf14 | 28 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
chocolatepatty | 1:ae7fdbddbf14 | 29 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
chocolatepatty | 1:ae7fdbddbf14 | 30 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
chocolatepatty | 1:ae7fdbddbf14 | 31 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
chocolatepatty | 1:ae7fdbddbf14 | 32 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
chocolatepatty | 1:ae7fdbddbf14 | 33 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
chocolatepatty | 1:ae7fdbddbf14 | 34 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
chocolatepatty | 1:ae7fdbddbf14 | 35 | * |
chocolatepatty | 1:ae7fdbddbf14 | 36 | ****************************************************************************** |
chocolatepatty | 1:ae7fdbddbf14 | 37 | */ |
chocolatepatty | 0:16560d6a2529 | 38 | |
chocolatepatty | 1:ae7fdbddbf14 | 39 | /* Includes */ |
chocolatepatty | 1:ae7fdbddbf14 | 40 | #include "mbed.h" |
chocolatepatty | 1:ae7fdbddbf14 | 41 | #include "x_nucleo_iks01a1.h" |
chocolatepatty | 1:ae7fdbddbf14 | 42 | |
chocolatepatty | 1:ae7fdbddbf14 | 43 | /* Instantiate the expansion board */ |
chocolatepatty | 1:ae7fdbddbf14 | 44 | static X_NUCLEO_IKS01A1 *mems_expansion_board = X_NUCLEO_IKS01A1::Instance(D14, D15); |
chocolatepatty | 1:ae7fdbddbf14 | 45 | |
chocolatepatty | 1:ae7fdbddbf14 | 46 | /* Retrieve the composing elements of the expansion board */ |
chocolatepatty | 1:ae7fdbddbf14 | 47 | static GyroSensor *gyroscope = mems_expansion_board->GetGyroscope(); |
chocolatepatty | 1:ae7fdbddbf14 | 48 | static MotionSensor *accelerometer = mems_expansion_board->GetAccelerometer(); |
chocolatepatty | 1:ae7fdbddbf14 | 49 | static MagneticSensor *magnetometer = mems_expansion_board->magnetometer; |
chocolatepatty | 1:ae7fdbddbf14 | 50 | static HumiditySensor *humidity_sensor = mems_expansion_board->ht_sensor; |
chocolatepatty | 1:ae7fdbddbf14 | 51 | static PressureSensor *pressure_sensor = mems_expansion_board->pt_sensor; |
chocolatepatty | 1:ae7fdbddbf14 | 52 | static TempSensor *temp_sensor1 = mems_expansion_board->ht_sensor; |
chocolatepatty | 1:ae7fdbddbf14 | 53 | static TempSensor *temp_sensor2 = mems_expansion_board->pt_sensor; |
chocolatepatty | 0:16560d6a2529 | 54 | |
chocolatepatty | 1:ae7fdbddbf14 | 55 | /* Helper function for printing floats & doubles */ |
chocolatepatty | 1:ae7fdbddbf14 | 56 | static char *printDouble(char* str, double v, int decimalDigits=2) |
chocolatepatty | 1:ae7fdbddbf14 | 57 | { |
chocolatepatty | 1:ae7fdbddbf14 | 58 | int i = 1; |
chocolatepatty | 1:ae7fdbddbf14 | 59 | int intPart, fractPart; |
chocolatepatty | 1:ae7fdbddbf14 | 60 | int len; |
chocolatepatty | 1:ae7fdbddbf14 | 61 | char *ptr; |
chocolatepatty | 1:ae7fdbddbf14 | 62 | |
chocolatepatty | 1:ae7fdbddbf14 | 63 | /* prepare decimal digits multiplicator */ |
chocolatepatty | 1:ae7fdbddbf14 | 64 | for (;decimalDigits!=0; i*=10, decimalDigits--); |
chocolatepatty | 1:ae7fdbddbf14 | 65 | |
chocolatepatty | 1:ae7fdbddbf14 | 66 | /* calculate integer & fractinal parts */ |
chocolatepatty | 1:ae7fdbddbf14 | 67 | intPart = (int)v; |
chocolatepatty | 1:ae7fdbddbf14 | 68 | fractPart = (int)((v-(double)(int)v)*i); |
chocolatepatty | 1:ae7fdbddbf14 | 69 | |
chocolatepatty | 1:ae7fdbddbf14 | 70 | /* fill in integer part */ |
chocolatepatty | 1:ae7fdbddbf14 | 71 | sprintf(str, "%i.", intPart); |
chocolatepatty | 1:ae7fdbddbf14 | 72 | |
chocolatepatty | 1:ae7fdbddbf14 | 73 | /* prepare fill in of fractional part */ |
chocolatepatty | 1:ae7fdbddbf14 | 74 | len = strlen(str); |
chocolatepatty | 1:ae7fdbddbf14 | 75 | ptr = &str[len]; |
chocolatepatty | 1:ae7fdbddbf14 | 76 | |
chocolatepatty | 1:ae7fdbddbf14 | 77 | /* fill in leading fractional zeros */ |
chocolatepatty | 1:ae7fdbddbf14 | 78 | for (i/=10;i>1; i/=10, ptr++) { |
chocolatepatty | 1:ae7fdbddbf14 | 79 | if(fractPart >= i) break; |
chocolatepatty | 1:ae7fdbddbf14 | 80 | *ptr = '0'; |
chocolatepatty | 1:ae7fdbddbf14 | 81 | } |
chocolatepatty | 1:ae7fdbddbf14 | 82 | |
chocolatepatty | 1:ae7fdbddbf14 | 83 | /* fill in (rest of) fractional part */ |
chocolatepatty | 1:ae7fdbddbf14 | 84 | sprintf(ptr, "%i", fractPart); |
chocolatepatty | 0:16560d6a2529 | 85 | |
chocolatepatty | 1:ae7fdbddbf14 | 86 | return str; |
chocolatepatty | 1:ae7fdbddbf14 | 87 | } |
chocolatepatty | 1:ae7fdbddbf14 | 88 | |
chocolatepatty | 1:ae7fdbddbf14 | 89 | |
chocolatepatty | 1:ae7fdbddbf14 | 90 | /* Simple main function */ |
chocolatepatty | 0:16560d6a2529 | 91 | int main() { |
chocolatepatty | 1:ae7fdbddbf14 | 92 | uint8_t id; |
chocolatepatty | 1:ae7fdbddbf14 | 93 | float value1, value2; |
chocolatepatty | 1:ae7fdbddbf14 | 94 | char buffer1[32], buffer2[32]; |
chocolatepatty | 1:ae7fdbddbf14 | 95 | int32_t axes[3]; |
chocolatepatty | 1:ae7fdbddbf14 | 96 | |
chocolatepatty | 1:ae7fdbddbf14 | 97 | printf("\r\n--- Starting new run ---\r\n"); |
chocolatepatty | 1:ae7fdbddbf14 | 98 | |
chocolatepatty | 1:ae7fdbddbf14 | 99 | humidity_sensor->read_id(&id); |
chocolatepatty | 1:ae7fdbddbf14 | 100 | printf("HTS221 humidity & temperature = 0x%X\r\n", id); |
chocolatepatty | 1:ae7fdbddbf14 | 101 | pressure_sensor->read_id(&id); |
chocolatepatty | 1:ae7fdbddbf14 | 102 | printf("LPS25H pressure & temperature = 0x%X\r\n", id); |
chocolatepatty | 1:ae7fdbddbf14 | 103 | magnetometer->read_id(&id); |
chocolatepatty | 1:ae7fdbddbf14 | 104 | printf("LIS3MDL magnetometer = 0x%X\r\n", id); |
chocolatepatty | 1:ae7fdbddbf14 | 105 | gyroscope->read_id(&id); |
chocolatepatty | 1:ae7fdbddbf14 | 106 | printf("LSM6DS0 accelerometer & gyroscope = 0x%X\r\n", id); |
chocolatepatty | 1:ae7fdbddbf14 | 107 | |
chocolatepatty | 1:ae7fdbddbf14 | 108 | wait(3); |
chocolatepatty | 1:ae7fdbddbf14 | 109 | |
chocolatepatty | 1:ae7fdbddbf14 | 110 | while(1) { |
chocolatepatty | 1:ae7fdbddbf14 | 111 | printf("\r\n"); |
chocolatepatty | 1:ae7fdbddbf14 | 112 | |
chocolatepatty | 1:ae7fdbddbf14 | 113 | temp_sensor1->get_temperature(&value1); |
chocolatepatty | 1:ae7fdbddbf14 | 114 | humidity_sensor->get_humidity(&value2); |
chocolatepatty | 1:ae7fdbddbf14 | 115 | printf("HTS221: [temp] %7s°C, [hum] %s%%\r\n", printDouble(buffer1, value1), printDouble(buffer2, value2)); |
chocolatepatty | 0:16560d6a2529 | 116 | |
chocolatepatty | 1:ae7fdbddbf14 | 117 | temp_sensor2->get_fahrenheit(&value1); |
chocolatepatty | 1:ae7fdbddbf14 | 118 | pressure_sensor->get_pressure(&value2); |
chocolatepatty | 1:ae7fdbddbf14 | 119 | printf("LPS25H: [temp] %7s°F, [press] %smbar\r\n", printDouble(buffer1, value1), printDouble(buffer2, value2)); |
chocolatepatty | 1:ae7fdbddbf14 | 120 | |
chocolatepatty | 1:ae7fdbddbf14 | 121 | printf("---\r\n"); |
chocolatepatty | 1:ae7fdbddbf14 | 122 | |
chocolatepatty | 1:ae7fdbddbf14 | 123 | magnetometer->get_m_axes(axes); |
chocolatepatty | 1:ae7fdbddbf14 | 124 | printf("LIS3MDL [mag/mgauss]: %7ld, %7ld, %7ld\r\n", axes[0], axes[1], axes[2]); |
chocolatepatty | 1:ae7fdbddbf14 | 125 | |
chocolatepatty | 1:ae7fdbddbf14 | 126 | accelerometer->get_x_axes(axes); |
chocolatepatty | 1:ae7fdbddbf14 | 127 | printf("LSM6DS0 [acc/mg]: %7ld, %7ld, %7ld\r\n", axes[0], axes[1], axes[2]); |
chocolatepatty | 1:ae7fdbddbf14 | 128 | |
chocolatepatty | 1:ae7fdbddbf14 | 129 | gyroscope->get_g_axes(axes); |
chocolatepatty | 1:ae7fdbddbf14 | 130 | printf("LSM6DS0 [gyro/mdps]: %7ld, %7ld, %7ld\r\n", axes[0], axes[1], axes[2]); |
chocolatepatty | 1:ae7fdbddbf14 | 131 | |
chocolatepatty | 1:ae7fdbddbf14 | 132 | wait(5); |
chocolatepatty | 1:ae7fdbddbf14 | 133 | } |
chocolatepatty | 0:16560d6a2529 | 134 | } |