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@0:06fc5a68ead2, 2017-04-08 (annotated)
- Committer:
- mfiore
- Date:
- Sat Apr 08 04:15:22 2017 +0000
- Revision:
- 0:06fc5a68ead2
- Child:
- 1:a2c5a8a74527
both sensor boards not compiling ATM
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 "rtos.h" |
mfiore | 0:06fc5a68ead2 | 3 | #include "x_nucleo_iks01a1.h" |
mfiore | 0:06fc5a68ead2 | 4 | #include "x_nucleo_53l0a1.h" |
mfiore | 0:06fc5a68ead2 | 5 | |
mfiore | 0:06fc5a68ead2 | 6 | Thread mems_thread; |
mfiore | 0:06fc5a68ead2 | 7 | Thread range_thread; |
mfiore | 0:06fc5a68ead2 | 8 | Mutex serial_mutex; |
mfiore | 0:06fc5a68ead2 | 9 | |
mfiore | 0:06fc5a68ead2 | 10 | DigitalOut led(D3); |
mfiore | 0:06fc5a68ead2 | 11 | |
mfiore | 0:06fc5a68ead2 | 12 | DevI2C i2c(D14, D15); |
mfiore | 0:06fc5a68ead2 | 13 | |
mfiore | 0:06fc5a68ead2 | 14 | /* Instantiate the expansion board */ |
mfiore | 0:06fc5a68ead2 | 15 | static X_NUCLEO_IKS01A1 *mems_expansion_board = X_NUCLEO_IKS01A1::Instance(&i2c); |
mfiore | 0:06fc5a68ead2 | 16 | // MAKE SURE YOU JUMPER PIN D7 TO PIN D8! |
mfiore | 0:06fc5a68ead2 | 17 | static X_NUCLEO_53L0A1 *range = X_NUCLEO_53L0A1::Instance(&i2c, A2, D7, D2); |
mfiore | 0:06fc5a68ead2 | 18 | |
mfiore | 0:06fc5a68ead2 | 19 | /* Retrieve the composing elements of the expansion board */ |
mfiore | 0:06fc5a68ead2 | 20 | static GyroSensor *gyroscope = mems_expansion_board->GetGyroscope(); |
mfiore | 0:06fc5a68ead2 | 21 | static MotionSensor *accelerometer = mems_expansion_board->GetAccelerometer(); |
mfiore | 0:06fc5a68ead2 | 22 | static MagneticSensor *magnetometer = mems_expansion_board->magnetometer; |
mfiore | 0:06fc5a68ead2 | 23 | static HumiditySensor *humidity_sensor = mems_expansion_board->ht_sensor; |
mfiore | 0:06fc5a68ead2 | 24 | static PressureSensor *pressure_sensor = mems_expansion_board->pt_sensor; |
mfiore | 0:06fc5a68ead2 | 25 | static TempSensor *temp_sensor1 = mems_expansion_board->ht_sensor; |
mfiore | 0:06fc5a68ead2 | 26 | static TempSensor *temp_sensor2 = mems_expansion_board->pt_sensor; |
mfiore | 0:06fc5a68ead2 | 27 | |
mfiore | 0:06fc5a68ead2 | 28 | /* Helper function for printing from multiple threads */ |
mfiore | 0:06fc5a68ead2 | 29 | void ts_printf() { |
mfiore | 0:06fc5a68ead2 | 30 | serial_mutex.lock(); |
mfiore | 0:06fc5a68ead2 | 31 | printf(); |
mfiore | 0:06fc5a68ead2 | 32 | serial_mutex.unlock(); |
mfiore | 0:06fc5a68ead2 | 33 | } |
mfiore | 0:06fc5a68ead2 | 34 | |
mfiore | 0:06fc5a68ead2 | 35 | /* Helper function for printing floats & doubles */ |
mfiore | 0:06fc5a68ead2 | 36 | static char *printDouble(char* str, double v, int decimalDigits=2) { |
mfiore | 0:06fc5a68ead2 | 37 | int i = 1; |
mfiore | 0:06fc5a68ead2 | 38 | int intPart, fractPart; |
mfiore | 0:06fc5a68ead2 | 39 | int len; |
mfiore | 0:06fc5a68ead2 | 40 | char *ptr; |
mfiore | 0:06fc5a68ead2 | 41 | |
mfiore | 0:06fc5a68ead2 | 42 | /* prepare decimal digits multiplicator */ |
mfiore | 0:06fc5a68ead2 | 43 | for (;decimalDigits!=0; i*=10, decimalDigits--); |
mfiore | 0:06fc5a68ead2 | 44 | |
mfiore | 0:06fc5a68ead2 | 45 | /* calculate integer & fractinal parts */ |
mfiore | 0:06fc5a68ead2 | 46 | intPart = (int)v; |
mfiore | 0:06fc5a68ead2 | 47 | fractPart = (int)((v-(double)(int)v)*i); |
mfiore | 0:06fc5a68ead2 | 48 | |
mfiore | 0:06fc5a68ead2 | 49 | /* fill in integer part */ |
mfiore | 0:06fc5a68ead2 | 50 | sprintf(str, "%i.", intPart); |
mfiore | 0:06fc5a68ead2 | 51 | |
mfiore | 0:06fc5a68ead2 | 52 | /* prepare fill in of fractional part */ |
mfiore | 0:06fc5a68ead2 | 53 | len = strlen(str); |
mfiore | 0:06fc5a68ead2 | 54 | ptr = &str[len]; |
mfiore | 0:06fc5a68ead2 | 55 | |
mfiore | 0:06fc5a68ead2 | 56 | /* fill in leading fractional zeros */ |
mfiore | 0:06fc5a68ead2 | 57 | for (i/=10;i>1; i/=10, ptr++) { |
mfiore | 0:06fc5a68ead2 | 58 | if(fractPart >= i) break; |
mfiore | 0:06fc5a68ead2 | 59 | *ptr = '0'; |
mfiore | 0:06fc5a68ead2 | 60 | } |
mfiore | 0:06fc5a68ead2 | 61 | |
mfiore | 0:06fc5a68ead2 | 62 | /* fill in (rest of) fractional part */ |
mfiore | 0:06fc5a68ead2 | 63 | sprintf(ptr, "%i", fractPart); |
mfiore | 0:06fc5a68ead2 | 64 | |
mfiore | 0:06fc5a68ead2 | 65 | return str; |
mfiore | 0:06fc5a68ead2 | 66 | } |
mfiore | 0:06fc5a68ead2 | 67 | |
mfiore | 0:06fc5a68ead2 | 68 | /* Function to run the mems board */ |
mfiore | 0:06fc5a68ead2 | 69 | void mems_func() { |
mfiore | 0:06fc5a68ead2 | 70 | uint8_t id; |
mfiore | 0:06fc5a68ead2 | 71 | float value1, value2; |
mfiore | 0:06fc5a68ead2 | 72 | char buffer1[32], buffer2[32]; |
mfiore | 0:06fc5a68ead2 | 73 | int32_t axes[3]; |
mfiore | 0:06fc5a68ead2 | 74 | |
mfiore | 0:06fc5a68ead2 | 75 | ts_printf("\r\n--- Starting mems board ---\r\n"); |
mfiore | 0:06fc5a68ead2 | 76 | |
mfiore | 0:06fc5a68ead2 | 77 | humidity_sensor->read_id(&id); |
mfiore | 0:06fc5a68ead2 | 78 | ts_printf("HTS221 humidity & temperature = 0x%X\r\n", id); |
mfiore | 0:06fc5a68ead2 | 79 | pressure_sensor->read_id(&id); |
mfiore | 0:06fc5a68ead2 | 80 | ts_printf("LPS25H pressure & temperature = 0x%X\r\n", id); |
mfiore | 0:06fc5a68ead2 | 81 | magnetometer->read_id(&id); |
mfiore | 0:06fc5a68ead2 | 82 | ts_printf("LIS3MDL magnetometer = 0x%X\r\n", id); |
mfiore | 0:06fc5a68ead2 | 83 | gyroscope->read_id(&id); |
mfiore | 0:06fc5a68ead2 | 84 | ts_printf("LSM6DS0 accelerometer & gyroscope = 0x%X\r\n", id); |
mfiore | 0:06fc5a68ead2 | 85 | |
mfiore | 0:06fc5a68ead2 | 86 | wait(3); |
mfiore | 0:06fc5a68ead2 | 87 | |
mfiore | 0:06fc5a68ead2 | 88 | while (true) { |
mfiore | 0:06fc5a68ead2 | 89 | temp_sensor1->get_temperature(&value1); |
mfiore | 0:06fc5a68ead2 | 90 | humidity_sensor->get_humidity(&value2); |
mfiore | 0:06fc5a68ead2 | 91 | ts_printf("HTS221: [temp] %7s°C, [hum] %s%%\r\n", printDouble(buffer1, value1), printDouble(buffer2, value2)); |
mfiore | 0:06fc5a68ead2 | 92 | |
mfiore | 0:06fc5a68ead2 | 93 | temp_sensor2->get_fahrenheit(&value1); |
mfiore | 0:06fc5a68ead2 | 94 | pressure_sensor->get_pressure(&value2); |
mfiore | 0:06fc5a68ead2 | 95 | ts_printf("LPS25H: [temp] %7s°F, [press] %smbar\r\n", printDouble(buffer1, value1), printDouble(buffer2, value2)); |
mfiore | 0:06fc5a68ead2 | 96 | |
mfiore | 0:06fc5a68ead2 | 97 | magnetometer->get_m_axes(axes); |
mfiore | 0:06fc5a68ead2 | 98 | ts_printf("LIS3MDL [mag/mgauss]: %7ld, %7ld, %7ld\r\n", axes[0], axes[1], axes[2]); |
mfiore | 0:06fc5a68ead2 | 99 | |
mfiore | 0:06fc5a68ead2 | 100 | accelerometer->get_x_axes(axes); |
mfiore | 0:06fc5a68ead2 | 101 | ts_printf("LSM6DS0 [acc/mg]: %7ld, %7ld, %7ld\r\n", axes[0], axes[1], axes[2]); |
mfiore | 0:06fc5a68ead2 | 102 | |
mfiore | 0:06fc5a68ead2 | 103 | gyroscope->get_g_axes(axes); |
mfiore | 0:06fc5a68ead2 | 104 | ts_printf("LSM6DS0 [gyro/mdps]: %7ld, %7ld, %7ld\r\n", axes[0], axes[1], axes[2]); |
mfiore | 0:06fc5a68ead2 | 105 | |
mfiore | 0:06fc5a68ead2 | 106 | wait(1.5); |
mfiore | 0:06fc5a68ead2 | 107 | } |
mfiore | 0:06fc5a68ead2 | 108 | } |
mfiore | 0:06fc5a68ead2 | 109 | |
mfiore | 0:06fc5a68ead2 | 110 | /* Function to run the range board */ |
mfiore | 0:06fc5a68ead2 | 111 | void range_func() { |
mfiore | 0:06fc5a68ead2 | 112 | int status; |
mfiore | 0:06fc5a68ead2 | 113 | uint32_t distance; |
mfiore | 0:06fc5a68ead2 | 114 | |
mfiore | 0:06fc5a68ead2 | 115 | ts_printf("\r\n--- Starting range board ---\r\n"); |
mfiore | 0:06fc5a68ead2 | 116 | |
mfiore | 0:06fc5a68ead2 | 117 | /* init the 53L0A1 expansion board with default values */ |
mfiore | 0:06fc5a68ead2 | 118 | while (true) { |
mfiore | 0:06fc5a68ead2 | 119 | status=board->InitBoard(); |
mfiore | 0:06fc5a68ead2 | 120 | if (status) { |
mfiore | 0:06fc5a68ead2 | 121 | ts_printf("Failed to init range board!\r\n"); |
mfiore | 0:06fc5a68ead2 | 122 | wait(1); |
mfiore | 0:06fc5a68ead2 | 123 | } else { |
mfiore | 0:06fc5a68ead2 | 124 | ts_printf("Range board initialized\r\n"); |
mfiore | 0:06fc5a68ead2 | 125 | break; |
mfiore | 0:06fc5a68ead2 | 126 | } |
mfiore | 0:06fc5a68ead2 | 127 | } |
mfiore | 0:06fc5a68ead2 | 128 | |
mfiore | 0:06fc5a68ead2 | 129 | while (true) { |
mfiore | 0:06fc5a68ead2 | 130 | status = board->sensor_centre->GetDistance(&distance); |
mfiore | 0:06fc5a68ead2 | 131 | if (status == VL53L0X_ERROR_NONE) { |
mfiore | 0:06fc5a68ead2 | 132 | ts_printf("Distance : %ld\n", distance); |
mfiore | 0:06fc5a68ead2 | 133 | } |
mfiore | 0:06fc5a68ead2 | 134 | |
mfiore | 0:06fc5a68ead2 | 135 | wait(0.5); |
mfiore | 0:06fc5a68ead2 | 136 | } |
mfiore | 0:06fc5a68ead2 | 137 | } |
mfiore | 0:06fc5a68ead2 | 138 | |
mfiore | 0:06fc5a68ead2 | 139 | int main() { |
mfiore | 0:06fc5a68ead2 | 140 | ts_printf("\r\n--- Application starting up ---\r\n"); |
mfiore | 0:06fc5a68ead2 | 141 | |
mfiore | 0:06fc5a68ead2 | 142 | mems_thread.start(mems_func); |
mfiore | 0:06fc5a68ead2 | 143 | range_thread.start(range_func); |
mfiore | 0:06fc5a68ead2 | 144 | |
mfiore | 0:06fc5a68ead2 | 145 | while (true) { |
mfiore | 0:06fc5a68ead2 | 146 | led = !led; |
mfiore | 0:06fc5a68ead2 | 147 | wait(0.25); |
mfiore | 0:06fc5a68ead2 | 148 | } |
mfiore | 0:06fc5a68ead2 | 149 | } |