Template code Support mbed-os 5.x
Dependencies: X_NUCLEO_IKS01A3
main.cpp@0:3c909a9f5c1f, 2019-08-15 (annotated)
- Committer:
- noutram
- Date:
- Thu Aug 15 13:41:34 2019 +0000
- Revision:
- 0:3c909a9f5c1f
Please try this!
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
noutram | 0:3c909a9f5c1f | 1 | /* mbed Microcontroller Library |
noutram | 0:3c909a9f5c1f | 2 | * Copyright (c) 2018 ARM Limited |
noutram | 0:3c909a9f5c1f | 3 | * SPDX-License-Identifier: Apache-2.0 |
noutram | 0:3c909a9f5c1f | 4 | */ |
noutram | 0:3c909a9f5c1f | 5 | |
noutram | 0:3c909a9f5c1f | 6 | #include "mbed.h" |
noutram | 0:3c909a9f5c1f | 7 | #include "stats_report.h" |
noutram | 0:3c909a9f5c1f | 8 | #include "XNucleoIKS01A3.h" |
noutram | 0:3c909a9f5c1f | 9 | #include "USBMouse.h" |
noutram | 0:3c909a9f5c1f | 10 | |
noutram | 0:3c909a9f5c1f | 11 | DigitalOut led1(LED1); |
noutram | 0:3c909a9f5c1f | 12 | |
noutram | 0:3c909a9f5c1f | 13 | #define SLEEP_TIME 500 // (msec) |
noutram | 0:3c909a9f5c1f | 14 | #define PRINT_AFTER_N_LOOPS 20 |
noutram | 0:3c909a9f5c1f | 15 | /* Instantiate the expansion board */ |
noutram | 0:3c909a9f5c1f | 16 | static XNucleoIKS01A3 *mems_expansion_board = XNucleoIKS01A3::instance(D14, D15, D4, D5, A3, D6, A4); |
noutram | 0:3c909a9f5c1f | 17 | |
noutram | 0:3c909a9f5c1f | 18 | /* Retrieve the composing elements of the expansion board */ |
noutram | 0:3c909a9f5c1f | 19 | |
noutram | 0:3c909a9f5c1f | 20 | static LIS2MDLSensor *magnetometer = mems_expansion_board->magnetometer; |
noutram | 0:3c909a9f5c1f | 21 | static HTS221Sensor *hum_temp = mems_expansion_board->ht_sensor; |
noutram | 0:3c909a9f5c1f | 22 | static LPS22HHSensor *press_temp = mems_expansion_board->pt_sensor; |
noutram | 0:3c909a9f5c1f | 23 | static LSM6DSOSensor *acc_gyro = mems_expansion_board->acc_gyro; |
noutram | 0:3c909a9f5c1f | 24 | static LIS2DW12Sensor *accelerometer = mems_expansion_board->accelerometer; |
noutram | 0:3c909a9f5c1f | 25 | static STTS751Sensor *temp = mems_expansion_board->t_sensor; |
noutram | 0:3c909a9f5c1f | 26 | |
noutram | 0:3c909a9f5c1f | 27 | Semaphore one_slot(1); |
noutram | 0:3c909a9f5c1f | 28 | |
noutram | 0:3c909a9f5c1f | 29 | static char *print_double(char* str, double v, int decimalDigits=2) |
noutram | 0:3c909a9f5c1f | 30 | { |
noutram | 0:3c909a9f5c1f | 31 | int i = 1; |
noutram | 0:3c909a9f5c1f | 32 | int intPart, fractPart; |
noutram | 0:3c909a9f5c1f | 33 | int len; |
noutram | 0:3c909a9f5c1f | 34 | char *ptr; |
noutram | 0:3c909a9f5c1f | 35 | |
noutram | 0:3c909a9f5c1f | 36 | /* prepare decimal digits multiplicator */ |
noutram | 0:3c909a9f5c1f | 37 | for (;decimalDigits!=0; i*=10, decimalDigits--); |
noutram | 0:3c909a9f5c1f | 38 | |
noutram | 0:3c909a9f5c1f | 39 | /* calculate integer & fractinal parts */ |
noutram | 0:3c909a9f5c1f | 40 | intPart = (int)v; |
noutram | 0:3c909a9f5c1f | 41 | fractPart = (int)((v-(double)(int)v)*i); |
noutram | 0:3c909a9f5c1f | 42 | |
noutram | 0:3c909a9f5c1f | 43 | /* fill in integer part */ |
noutram | 0:3c909a9f5c1f | 44 | sprintf(str, "%i.", intPart); |
noutram | 0:3c909a9f5c1f | 45 | |
noutram | 0:3c909a9f5c1f | 46 | /* prepare fill in of fractional part */ |
noutram | 0:3c909a9f5c1f | 47 | len = strlen(str); |
noutram | 0:3c909a9f5c1f | 48 | ptr = &str[len]; |
noutram | 0:3c909a9f5c1f | 49 | |
noutram | 0:3c909a9f5c1f | 50 | /* fill in leading fractional zeros */ |
noutram | 0:3c909a9f5c1f | 51 | for (i/=10;i>1; i/=10, ptr++) { |
noutram | 0:3c909a9f5c1f | 52 | if (fractPart >= i) { |
noutram | 0:3c909a9f5c1f | 53 | break; |
noutram | 0:3c909a9f5c1f | 54 | } |
noutram | 0:3c909a9f5c1f | 55 | *ptr = '0'; |
noutram | 0:3c909a9f5c1f | 56 | } |
noutram | 0:3c909a9f5c1f | 57 | |
noutram | 0:3c909a9f5c1f | 58 | /* fill in (rest of) fractional part */ |
noutram | 0:3c909a9f5c1f | 59 | sprintf(ptr, "%i", fractPart); |
noutram | 0:3c909a9f5c1f | 60 | |
noutram | 0:3c909a9f5c1f | 61 | return str; |
noutram | 0:3c909a9f5c1f | 62 | } |
noutram | 0:3c909a9f5c1f | 63 | USBMouse mouse; |
noutram | 0:3c909a9f5c1f | 64 | |
noutram | 0:3c909a9f5c1f | 65 | int main() { |
noutram | 0:3c909a9f5c1f | 66 | uint8_t id; |
noutram | 0:3c909a9f5c1f | 67 | float value1, value2; |
noutram | 0:3c909a9f5c1f | 68 | char buffer1[32], buffer2[32]; |
noutram | 0:3c909a9f5c1f | 69 | int32_t axes[3]; |
noutram | 0:3c909a9f5c1f | 70 | |
noutram | 0:3c909a9f5c1f | 71 | int16_t x = 0; |
noutram | 0:3c909a9f5c1f | 72 | int16_t y = 0; |
noutram | 0:3c909a9f5c1f | 73 | int32_t radius = 10; |
noutram | 0:3c909a9f5c1f | 74 | int32_t angle = 0; |
noutram | 0:3c909a9f5c1f | 75 | |
noutram | 0:3c909a9f5c1f | 76 | /* Enable all sensors */ |
noutram | 0:3c909a9f5c1f | 77 | |
noutram | 0:3c909a9f5c1f | 78 | |
noutram | 0:3c909a9f5c1f | 79 | hum_temp->enable(); |
noutram | 0:3c909a9f5c1f | 80 | press_temp->enable(); |
noutram | 0:3c909a9f5c1f | 81 | temp->enable(); |
noutram | 0:3c909a9f5c1f | 82 | magnetometer->enable(); |
noutram | 0:3c909a9f5c1f | 83 | accelerometer->enable_x(); |
noutram | 0:3c909a9f5c1f | 84 | acc_gyro->enable_x(); |
noutram | 0:3c909a9f5c1f | 85 | acc_gyro->enable_g(); |
noutram | 0:3c909a9f5c1f | 86 | |
noutram | 0:3c909a9f5c1f | 87 | |
noutram | 0:3c909a9f5c1f | 88 | printf("\r\n--- Starting new run ---\r\n"); |
noutram | 0:3c909a9f5c1f | 89 | |
noutram | 0:3c909a9f5c1f | 90 | |
noutram | 0:3c909a9f5c1f | 91 | hum_temp->read_id(&id); |
noutram | 0:3c909a9f5c1f | 92 | printf("HTS221 humidity & temperature = 0x%X\r\n", id); |
noutram | 0:3c909a9f5c1f | 93 | press_temp->read_id(&id); |
noutram | 0:3c909a9f5c1f | 94 | printf("LPS22HH pressure & temperature = 0x%X\r\n", id); |
noutram | 0:3c909a9f5c1f | 95 | temp->read_id(&id); |
noutram | 0:3c909a9f5c1f | 96 | printf("STTS751 temperature = 0x%X\r\n", id); |
noutram | 0:3c909a9f5c1f | 97 | magnetometer->read_id(&id); |
noutram | 0:3c909a9f5c1f | 98 | printf("LIS2MDL magnetometer = 0x%X\r\n", id); |
noutram | 0:3c909a9f5c1f | 99 | accelerometer->read_id(&id); |
noutram | 0:3c909a9f5c1f | 100 | printf("LIS2DW12 accelerometer = 0x%X\r\n", id); |
noutram | 0:3c909a9f5c1f | 101 | acc_gyro->read_id(&id); |
noutram | 0:3c909a9f5c1f | 102 | printf("LSM6DSO accelerometer & gyroscope = 0x%X\r\n", id); |
noutram | 0:3c909a9f5c1f | 103 | |
noutram | 0:3c909a9f5c1f | 104 | |
noutram | 0:3c909a9f5c1f | 105 | while(1) { |
noutram | 0:3c909a9f5c1f | 106 | printf("\r\n"); |
noutram | 0:3c909a9f5c1f | 107 | |
noutram | 0:3c909a9f5c1f | 108 | hum_temp->get_temperature(&value1); |
noutram | 0:3c909a9f5c1f | 109 | hum_temp->get_humidity(&value2); |
noutram | 0:3c909a9f5c1f | 110 | printf("HTS221: [temp] %7s C, [hum] %s%%\r\n", print_double(buffer1, value1), print_double(buffer2, value2)); |
noutram | 0:3c909a9f5c1f | 111 | |
noutram | 0:3c909a9f5c1f | 112 | press_temp->get_temperature(&value1); |
noutram | 0:3c909a9f5c1f | 113 | press_temp->get_pressure(&value2); |
noutram | 0:3c909a9f5c1f | 114 | printf("LPS22HH: [temp] %7s C, [press] %s mbar\r\n", print_double(buffer1, value1), print_double(buffer2, value2)); |
noutram | 0:3c909a9f5c1f | 115 | |
noutram | 0:3c909a9f5c1f | 116 | temp->get_temperature(&value1); |
noutram | 0:3c909a9f5c1f | 117 | printf("STTS751: [temp] %7s C\r\n", print_double(buffer1, value1)); |
noutram | 0:3c909a9f5c1f | 118 | |
noutram | 0:3c909a9f5c1f | 119 | printf("---\r\n"); |
noutram | 0:3c909a9f5c1f | 120 | |
noutram | 0:3c909a9f5c1f | 121 | magnetometer->get_m_axes(axes); |
noutram | 0:3c909a9f5c1f | 122 | printf("LIS2MDL [mag/mgauss]: %6d, %6d, %6d\r\n", axes[0], axes[1], axes[2]); |
noutram | 0:3c909a9f5c1f | 123 | |
noutram | 0:3c909a9f5c1f | 124 | accelerometer->get_x_axes(axes); |
noutram | 0:3c909a9f5c1f | 125 | printf("LIS2DW12 [acc/mg]: %6d, %6d, %6d\r\n", axes[0], axes[1], axes[2]); |
noutram | 0:3c909a9f5c1f | 126 | |
noutram | 0:3c909a9f5c1f | 127 | acc_gyro->get_x_axes(axes); |
noutram | 0:3c909a9f5c1f | 128 | printf("LSM6DSO [acc/mg]: %6d, %6d, %6d\r\n", axes[0], axes[1], axes[2]); |
noutram | 0:3c909a9f5c1f | 129 | |
noutram | 0:3c909a9f5c1f | 130 | acc_gyro->get_g_axes(axes); |
noutram | 0:3c909a9f5c1f | 131 | printf("LSM6DSO [gyro/mdps]: %6d, %6d, %6d\r\n", axes[0], axes[1], axes[2]); |
noutram | 0:3c909a9f5c1f | 132 | |
noutram | 0:3c909a9f5c1f | 133 | x = cos((double)angle*3.14/180.0)*radius; |
noutram | 0:3c909a9f5c1f | 134 | y = sin((double)angle*3.14/180.0)*radius; |
noutram | 0:3c909a9f5c1f | 135 | |
noutram | 0:3c909a9f5c1f | 136 | //will move mouse x, y away from its previous position on the screen |
noutram | 0:3c909a9f5c1f | 137 | mouse.move(x, y); |
noutram | 0:3c909a9f5c1f | 138 | angle += 3; |
noutram | 0:3c909a9f5c1f | 139 | |
noutram | 0:3c909a9f5c1f | 140 | wait(0.001); |
noutram | 0:3c909a9f5c1f | 141 | } |
noutram | 0:3c909a9f5c1f | 142 | } |