example using LOF sensor board and MEMS sensor board. not using senet packet format
Dependencies: ST_INTERFACES Senet_Packet X_NUCLEO_53L0A1 X_NUCLEO_COMMON X_NUCLEO_IKS01A1 libmDot-mbed5
Fork of unh-hackathon-example by
main.cpp@2:a0fb0b785bd6, 2017-04-08 (annotated)
- Committer:
- Mike Fiore
- Date:
- Sat Apr 08 08:44:52 2017 -0400
- Revision:
- 2:a0fb0b785bd6
- Parent:
- 1:a2c5a8a74527
- Child:
- 5:1a52bf3f0fb9
switch to a single threaded implementation - libraries didn't like multithreading
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mfiore | 0:06fc5a68ead2 | 1 | #include "mbed.h" |
mfiore | 0:06fc5a68ead2 | 2 | #include "x_nucleo_iks01a1.h" |
mfiore | 0:06fc5a68ead2 | 3 | #include "x_nucleo_53l0a1.h" |
mfiore | 0:06fc5a68ead2 | 4 | |
mfiore | 0:06fc5a68ead2 | 5 | DevI2C i2c(D14, D15); |
mfiore | 0:06fc5a68ead2 | 6 | |
mfiore | 0:06fc5a68ead2 | 7 | /* Instantiate the expansion board */ |
mfiore | 0:06fc5a68ead2 | 8 | static X_NUCLEO_IKS01A1 *mems_expansion_board = X_NUCLEO_IKS01A1::Instance(&i2c); |
mfiore | 0:06fc5a68ead2 | 9 | // MAKE SURE YOU JUMPER PIN D7 TO PIN D8! |
Mike Fiore |
1:a2c5a8a74527 | 10 | static X_NUCLEO_53L0A1 *range_board = X_NUCLEO_53L0A1::Instance(&i2c, A2, D7, D2); |
mfiore | 0:06fc5a68ead2 | 11 | |
mfiore | 0:06fc5a68ead2 | 12 | /* Retrieve the composing elements of the expansion board */ |
mfiore | 0:06fc5a68ead2 | 13 | static GyroSensor *gyroscope = mems_expansion_board->GetGyroscope(); |
mfiore | 0:06fc5a68ead2 | 14 | static MotionSensor *accelerometer = mems_expansion_board->GetAccelerometer(); |
mfiore | 0:06fc5a68ead2 | 15 | static MagneticSensor *magnetometer = mems_expansion_board->magnetometer; |
mfiore | 0:06fc5a68ead2 | 16 | static HumiditySensor *humidity_sensor = mems_expansion_board->ht_sensor; |
mfiore | 0:06fc5a68ead2 | 17 | static PressureSensor *pressure_sensor = mems_expansion_board->pt_sensor; |
mfiore | 0:06fc5a68ead2 | 18 | static TempSensor *temp_sensor1 = mems_expansion_board->ht_sensor; |
mfiore | 0:06fc5a68ead2 | 19 | static TempSensor *temp_sensor2 = mems_expansion_board->pt_sensor; |
mfiore | 0:06fc5a68ead2 | 20 | |
mfiore | 0:06fc5a68ead2 | 21 | /* Helper function for printing floats & doubles */ |
mfiore | 0:06fc5a68ead2 | 22 | static char *printDouble(char* str, double v, int decimalDigits=2) { |
mfiore | 0:06fc5a68ead2 | 23 | int i = 1; |
mfiore | 0:06fc5a68ead2 | 24 | int intPart, fractPart; |
mfiore | 0:06fc5a68ead2 | 25 | int len; |
mfiore | 0:06fc5a68ead2 | 26 | char *ptr; |
mfiore | 0:06fc5a68ead2 | 27 | |
mfiore | 0:06fc5a68ead2 | 28 | /* prepare decimal digits multiplicator */ |
mfiore | 0:06fc5a68ead2 | 29 | for (;decimalDigits!=0; i*=10, decimalDigits--); |
mfiore | 0:06fc5a68ead2 | 30 | |
mfiore | 0:06fc5a68ead2 | 31 | /* calculate integer & fractinal parts */ |
mfiore | 0:06fc5a68ead2 | 32 | intPart = (int)v; |
mfiore | 0:06fc5a68ead2 | 33 | fractPart = (int)((v-(double)(int)v)*i); |
mfiore | 0:06fc5a68ead2 | 34 | |
mfiore | 0:06fc5a68ead2 | 35 | /* fill in integer part */ |
mfiore | 0:06fc5a68ead2 | 36 | sprintf(str, "%i.", intPart); |
mfiore | 0:06fc5a68ead2 | 37 | |
mfiore | 0:06fc5a68ead2 | 38 | /* prepare fill in of fractional part */ |
mfiore | 0:06fc5a68ead2 | 39 | len = strlen(str); |
mfiore | 0:06fc5a68ead2 | 40 | ptr = &str[len]; |
mfiore | 0:06fc5a68ead2 | 41 | |
mfiore | 0:06fc5a68ead2 | 42 | /* fill in leading fractional zeros */ |
mfiore | 0:06fc5a68ead2 | 43 | for (i/=10;i>1; i/=10, ptr++) { |
mfiore | 0:06fc5a68ead2 | 44 | if(fractPart >= i) break; |
mfiore | 0:06fc5a68ead2 | 45 | *ptr = '0'; |
mfiore | 0:06fc5a68ead2 | 46 | } |
mfiore | 0:06fc5a68ead2 | 47 | |
mfiore | 0:06fc5a68ead2 | 48 | /* fill in (rest of) fractional part */ |
mfiore | 0:06fc5a68ead2 | 49 | sprintf(ptr, "%i", fractPart); |
mfiore | 0:06fc5a68ead2 | 50 | |
mfiore | 0:06fc5a68ead2 | 51 | return str; |
mfiore | 0:06fc5a68ead2 | 52 | } |
mfiore | 0:06fc5a68ead2 | 53 | |
Mike Fiore |
2:a0fb0b785bd6 | 54 | void start_mems_board() { |
mfiore | 0:06fc5a68ead2 | 55 | uint8_t id; |
Mike Fiore |
2:a0fb0b785bd6 | 56 | |
Mike Fiore |
2:a0fb0b785bd6 | 57 | humidity_sensor->read_id(&id); |
Mike Fiore |
2:a0fb0b785bd6 | 58 | printf("HTS221 humidity & temperature = 0x%X\r\n", id); |
Mike Fiore |
2:a0fb0b785bd6 | 59 | |
Mike Fiore |
2:a0fb0b785bd6 | 60 | pressure_sensor->read_id(&id); |
Mike Fiore |
2:a0fb0b785bd6 | 61 | printf("LPS25H pressure & temperature = 0x%X\r\n", id); |
Mike Fiore |
2:a0fb0b785bd6 | 62 | |
Mike Fiore |
2:a0fb0b785bd6 | 63 | magnetometer->read_id(&id); |
Mike Fiore |
2:a0fb0b785bd6 | 64 | printf("LIS3MDL magnetometer = 0x%X\r\n", id); |
Mike Fiore |
2:a0fb0b785bd6 | 65 | |
Mike Fiore |
2:a0fb0b785bd6 | 66 | gyroscope->read_id(&id); |
Mike Fiore |
2:a0fb0b785bd6 | 67 | printf("LSM6DS0 accelerometer & gyroscope = 0x%X\r\n", id); |
Mike Fiore |
2:a0fb0b785bd6 | 68 | |
Mike Fiore |
2:a0fb0b785bd6 | 69 | wait(3); |
Mike Fiore |
2:a0fb0b785bd6 | 70 | } |
Mike Fiore |
2:a0fb0b785bd6 | 71 | |
Mike Fiore |
2:a0fb0b785bd6 | 72 | void start_range_board() { |
Mike Fiore |
2:a0fb0b785bd6 | 73 | range_board->InitBoard(); |
Mike Fiore |
2:a0fb0b785bd6 | 74 | |
Mike Fiore |
2:a0fb0b785bd6 | 75 | wait(1); |
Mike Fiore |
2:a0fb0b785bd6 | 76 | } |
Mike Fiore |
2:a0fb0b785bd6 | 77 | |
Mike Fiore |
2:a0fb0b785bd6 | 78 | int main() { |
mfiore | 0:06fc5a68ead2 | 79 | float value1, value2; |
mfiore | 0:06fc5a68ead2 | 80 | char buffer1[32], buffer2[32]; |
mfiore | 0:06fc5a68ead2 | 81 | int32_t axes[3]; |
Mike Fiore |
1:a2c5a8a74527 | 82 | int ret; |
mfiore | 0:06fc5a68ead2 | 83 | int status; |
mfiore | 0:06fc5a68ead2 | 84 | uint32_t distance; |
mfiore | 0:06fc5a68ead2 | 85 | |
Mike Fiore |
2:a0fb0b785bd6 | 86 | printf("\r\n--- Application starting up ---\r\n"); |
mfiore | 0:06fc5a68ead2 | 87 | |
Mike Fiore |
2:a0fb0b785bd6 | 88 | printf("\r\n--- Initializing mems board ---\r\n"); |
Mike Fiore |
2:a0fb0b785bd6 | 89 | start_mems_board(); |
Mike Fiore |
2:a0fb0b785bd6 | 90 | |
Mike Fiore |
2:a0fb0b785bd6 | 91 | printf("\r\n--- Initializing range board ---\r\n"); |
Mike Fiore |
2:a0fb0b785bd6 | 92 | start_range_board(); |
mfiore | 0:06fc5a68ead2 | 93 | |
mfiore | 0:06fc5a68ead2 | 94 | while (true) { |
Mike Fiore |
2:a0fb0b785bd6 | 95 | |
Mike Fiore |
2:a0fb0b785bd6 | 96 | ret = temp_sensor1->get_temperature(&value1); |
Mike Fiore |
2:a0fb0b785bd6 | 97 | if (ret) { |
Mike Fiore |
2:a0fb0b785bd6 | 98 | printf("failed to get temp C\r\n"); |
Mike Fiore |
2:a0fb0b785bd6 | 99 | } |
Mike Fiore |
2:a0fb0b785bd6 | 100 | ret = humidity_sensor->get_humidity(&value2); |
Mike Fiore |
2:a0fb0b785bd6 | 101 | if (ret) { |
Mike Fiore |
2:a0fb0b785bd6 | 102 | printf("failed to get humidity\r\n"); |
Mike Fiore |
2:a0fb0b785bd6 | 103 | } |
Mike Fiore |
2:a0fb0b785bd6 | 104 | printf("HTS221: [temp] %7s°C, [hum] %s%%\r\n", printDouble(buffer1, value1), printDouble(buffer2, value2)); |
Mike Fiore |
2:a0fb0b785bd6 | 105 | //printf("HTS221: [temp] %f°C\r\n", value1); |
Mike Fiore |
2:a0fb0b785bd6 | 106 | //printf("HTS221: [hum] %f%%\r\n", value2); |
Mike Fiore |
2:a0fb0b785bd6 | 107 | |
Mike Fiore |
2:a0fb0b785bd6 | 108 | ret = temp_sensor2->get_fahrenheit(&value1); |
Mike Fiore |
2:a0fb0b785bd6 | 109 | if (ret) { |
Mike Fiore |
2:a0fb0b785bd6 | 110 | printf("failed to get temp F\r\n"); |
Mike Fiore |
2:a0fb0b785bd6 | 111 | } |
Mike Fiore |
2:a0fb0b785bd6 | 112 | ret = pressure_sensor->get_pressure(&value2); |
Mike Fiore |
2:a0fb0b785bd6 | 113 | if (ret) { |
Mike Fiore |
2:a0fb0b785bd6 | 114 | printf("failed to get pressure F\r\n"); |
Mike Fiore |
2:a0fb0b785bd6 | 115 | } |
Mike Fiore |
2:a0fb0b785bd6 | 116 | printf("LPS25H: [temp] %7s°F, [press] %smbar\r\n", printDouble(buffer1, value1), printDouble(buffer2, value2)); |
Mike Fiore |
2:a0fb0b785bd6 | 117 | //printf("LPS25H: [temp] %f°F\r\n", value1); |
Mike Fiore |
2:a0fb0b785bd6 | 118 | //printf("LPS25H: [press] %fmbar\r\n", value2); |
Mike Fiore |
2:a0fb0b785bd6 | 119 | |
Mike Fiore |
2:a0fb0b785bd6 | 120 | ret = magnetometer->get_m_axes(axes); |
Mike Fiore |
2:a0fb0b785bd6 | 121 | if (ret) { |
Mike Fiore |
2:a0fb0b785bd6 | 122 | printf("failed to get magnetometer\r\n"); |
Mike Fiore |
2:a0fb0b785bd6 | 123 | } |
Mike Fiore |
2:a0fb0b785bd6 | 124 | printf("LIS3MDL [mag/mgauss]: %7ld, %7ld, %7ld\r\n", axes[0], axes[1], axes[2]); |
Mike Fiore |
2:a0fb0b785bd6 | 125 | |
Mike Fiore |
2:a0fb0b785bd6 | 126 | ret = accelerometer->get_x_axes(axes); |
Mike Fiore |
2:a0fb0b785bd6 | 127 | if (ret) { |
Mike Fiore |
2:a0fb0b785bd6 | 128 | printf("failed to get accelerometer\r\n"); |
Mike Fiore |
2:a0fb0b785bd6 | 129 | } |
Mike Fiore |
2:a0fb0b785bd6 | 130 | printf("LSM6DS0 [acc/mg]: %7ld, %7ld, %7ld\r\n", axes[0], axes[1], axes[2]); |
Mike Fiore |
2:a0fb0b785bd6 | 131 | |
Mike Fiore |
2:a0fb0b785bd6 | 132 | ret = gyroscope->get_g_axes(axes); |
Mike Fiore |
2:a0fb0b785bd6 | 133 | if (ret) { |
Mike Fiore |
2:a0fb0b785bd6 | 134 | printf("failed to get gyroscope\r\n"); |
Mike Fiore |
2:a0fb0b785bd6 | 135 | } |
Mike Fiore |
2:a0fb0b785bd6 | 136 | printf("LSM6DS0 [gyro/mdps]: %7ld, %7ld, %7ld\r\n", axes[0], axes[1], axes[2]); |
Mike Fiore |
2:a0fb0b785bd6 | 137 | |
Mike Fiore |
1:a2c5a8a74527 | 138 | status = range_board->sensor_centre->GetDistance(&distance); |
mfiore | 0:06fc5a68ead2 | 139 | if (status == VL53L0X_ERROR_NONE) { |
Mike Fiore |
2:a0fb0b785bd6 | 140 | printf("Distance : %ld\n", distance); |
Mike Fiore |
2:a0fb0b785bd6 | 141 | } else { |
Mike Fiore |
2:a0fb0b785bd6 | 142 | printf("failed to get distance - possibly out of range!\r\n"); |
mfiore | 0:06fc5a68ead2 | 143 | } |
mfiore | 0:06fc5a68ead2 | 144 | |
Mike Fiore |
2:a0fb0b785bd6 | 145 | wait(1); |
mfiore | 0:06fc5a68ead2 | 146 | } |
mfiore | 0:06fc5a68ead2 | 147 | } |