Simple "hello world" style program for X-NUCLEO-IKS01A1 MEMS Inertial
Dependencies: BLE_API X_NUCLEO_IDB0XA1 X_NUCLEO_IKS01A1 mbed
Fork of HelloWorld_IKS01A1 by
main.cpp@7:4985455162fc, 2015-11-15 (annotated)
- Committer:
- n0tform3
- Date:
- Sun Nov 15 07:30:17 2015 +0000
- Revision:
- 7:4985455162fc
- Parent:
- 4:b1526d074d83
- Child:
- 8:1c6281289d67
first version working, no leds
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Wolfgang Betz |
0:c71c9af137dd | 1 | #include "mbed.h" |
Wolfgang Betz |
0:c71c9af137dd | 2 | #include "x_nucleo_iks01a1.h" |
n0tform3 | 7:4985455162fc | 3 | #include "mbed.h" |
n0tform3 | 7:4985455162fc | 4 | #include "ble/BLE.h" |
n0tform3 | 7:4985455162fc | 5 | #include "ble/services/HeartRateService.h" |
n0tform3 | 7:4985455162fc | 6 | #include "ble/services/BatteryService.h" |
n0tform3 | 7:4985455162fc | 7 | #include "ble/services/DeviceInformationService.h" |
Wolfgang Betz |
0:c71c9af137dd | 8 | |
n0tform3 | 7:4985455162fc | 9 | BLE ble; |
n0tform3 | 7:4985455162fc | 10 | DigitalOut led1(LED1); |
Wolfgang Betz |
0:c71c9af137dd | 11 | |
n0tform3 | 7:4985455162fc | 12 | const static char DEVICE_NAME[] = "HRM1"; |
n0tform3 | 7:4985455162fc | 13 | static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE, |
n0tform3 | 7:4985455162fc | 14 | GattService::UUID_DEVICE_INFORMATION_SERVICE}; |
n0tform3 | 7:4985455162fc | 15 | static volatile bool triggerSensorPolling = false; |
Wolfgang Betz |
0:c71c9af137dd | 16 | |
n0tform3 | 7:4985455162fc | 17 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
n0tform3 | 7:4985455162fc | 18 | { |
n0tform3 | 7:4985455162fc | 19 | ble.gap().startAdvertising(); // restart advertising |
n0tform3 | 7:4985455162fc | 20 | } |
Wolfgang Betz |
0:c71c9af137dd | 21 | |
n0tform3 | 7:4985455162fc | 22 | void periodicCallback(void) |
n0tform3 | 7:4985455162fc | 23 | { |
n0tform3 | 7:4985455162fc | 24 | led1 = !led1; |
n0tform3 | 7:4985455162fc | 25 | triggerSensorPolling = true; |
Wolfgang Betz |
0:c71c9af137dd | 26 | } |
Wolfgang Betz |
0:c71c9af137dd | 27 | |
Wolfgang Betz |
0:c71c9af137dd | 28 | |
n0tform3 | 7:4985455162fc | 29 | static X_NUCLEO_IKS01A1 *mems_expansion_board = X_NUCLEO_IKS01A1::Instance(D14, D15); |
n0tform3 | 7:4985455162fc | 30 | static MagneticSensor *magnetometer = mems_expansion_board->magnetometer; |
n0tform3 | 7:4985455162fc | 31 | |
n0tform3 | 7:4985455162fc | 32 | |
n0tform3 | 7:4985455162fc | 33 | |
n0tform3 | 7:4985455162fc | 34 | Serial pc(USBTX, USBRX); |
Wolfgang Betz |
0:c71c9af137dd | 35 | /* Simple main function */ |
Wolfgang Betz |
0:c71c9af137dd | 36 | int main() { |
Wolfgang Betz |
0:c71c9af137dd | 37 | uint8_t id; |
Wolfgang Betz |
0:c71c9af137dd | 38 | int32_t axes[3]; |
n0tform3 | 7:4985455162fc | 39 | pc.baud(115200); |
Wolfgang Betz |
0:c71c9af137dd | 40 | printf("\r\n--- Starting new run ---\r\n"); |
n0tform3 | 7:4985455162fc | 41 | |
Wolfgang Betz |
0:c71c9af137dd | 42 | magnetometer->ReadID(&id); |
Wolfgang Betz |
0:c71c9af137dd | 43 | printf("LIS3MDL magnetometer = 0x%X\r\n", id); |
Wolfgang Betz |
0:c71c9af137dd | 44 | |
n0tform3 | 7:4985455162fc | 45 | led1 = 1; |
n0tform3 | 7:4985455162fc | 46 | Ticker ticker; |
n0tform3 | 7:4985455162fc | 47 | ticker.attach(periodicCallback, 1); // blink LED every second |
n0tform3 | 7:4985455162fc | 48 | |
n0tform3 | 7:4985455162fc | 49 | ble.init(); |
n0tform3 | 7:4985455162fc | 50 | ble.gap().onDisconnection(disconnectionCallback); |
n0tform3 | 7:4985455162fc | 51 | |
n0tform3 | 7:4985455162fc | 52 | /* Setup primary service. */ |
n0tform3 | 7:4985455162fc | 53 | uint8_t hrmCounter = 0; // init HRM to 100bps |
n0tform3 | 7:4985455162fc | 54 | HeartRateService hrService(ble, hrmCounter, HeartRateService::LOCATION_FINGER); |
n0tform3 | 7:4985455162fc | 55 | |
n0tform3 | 7:4985455162fc | 56 | /* Setup auxiliary service. */ |
n0tform3 | 7:4985455162fc | 57 | DeviceInformationService deviceInfo(ble, "ARM", "Model1", "SN1", "hw-rev1", "fw-rev1", "soft-rev1"); |
Wolfgang Betz |
0:c71c9af137dd | 58 | |
n0tform3 | 7:4985455162fc | 59 | /* Setup advertising. */ |
n0tform3 | 7:4985455162fc | 60 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
n0tform3 | 7:4985455162fc | 61 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
n0tform3 | 7:4985455162fc | 62 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR); |
n0tform3 | 7:4985455162fc | 63 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
n0tform3 | 7:4985455162fc | 64 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
n0tform3 | 7:4985455162fc | 65 | ble.gap().setAdvertisingInterval(1000); /* 1000ms */ |
n0tform3 | 7:4985455162fc | 66 | |
n0tform3 | 7:4985455162fc | 67 | ble.gap().startAdvertising(); |
n0tform3 | 7:4985455162fc | 68 | |
Wolfgang Betz |
0:c71c9af137dd | 69 | |
n0tform3 | 7:4985455162fc | 70 | uint8_t note = 0; |
n0tform3 | 7:4985455162fc | 71 | while(1) { |
n0tform3 | 7:4985455162fc | 72 | if (triggerSensorPolling && ble.getGapState().connected) { |
n0tform3 | 7:4985455162fc | 73 | triggerSensorPolling = false; |
Wolfgang Betz |
0:c71c9af137dd | 74 | |
n0tform3 | 7:4985455162fc | 75 | magnetometer->Get_M_Axes(axes); |
n0tform3 | 7:4985455162fc | 76 | //printf("%6ld,\t %6ld,\t %6ld\r\n", axes[0], axes[1], axes[2]); |
n0tform3 | 7:4985455162fc | 77 | |
n0tform3 | 7:4985455162fc | 78 | note = 0; |
n0tform3 | 7:4985455162fc | 79 | if (axes[0]>-81-40 && axes[0]<23+40 && axes[1]>-259-40 && axes[1]<-167+40 && axes[2]>827-40 && axes[2]<981+40) |
n0tform3 | 7:4985455162fc | 80 | note = 1; |
n0tform3 | 7:4985455162fc | 81 | else if (axes[0]>163-40 && axes[0]<289+40 && axes[1]>-397-40 && axes[1]<-316+40 && axes[2]>311-40 && axes[2]<357+40) |
n0tform3 | 7:4985455162fc | 82 | note = 2; |
n0tform3 | 7:4985455162fc | 83 | else if (axes[0]>-73-40 && axes[0]<43+40 && axes[1]>-441-40 && axes[1]<-313+40 && axes[2]>335-40 && axes[2]<420+40) |
n0tform3 | 7:4985455162fc | 84 | note = 3; |
n0tform3 | 7:4985455162fc | 85 | else if (axes[0]>-45-40 && axes[0]<25+40 && axes[1]>-612-40 && axes[1]<-561+40 && axes[2]>632-40 && axes[2]<761+40) |
n0tform3 | 7:4985455162fc | 86 | note = 4; |
n0tform3 | 7:4985455162fc | 87 | else if (axes[0]>-64-40 && axes[0]<-6+40 && axes[1]>-7-40 && axes[1]<35+40 && axes[2]>524-40 && axes[2]<677+40) |
n0tform3 | 7:4985455162fc | 88 | note = 5; |
n0tform3 | 7:4985455162fc | 89 | else if (axes[0]>-210-40 && axes[0]<-137+40 && axes[1]>-431-40 && axes[1]<-294+40 && axes[2]>455-40 && axes[2]<615+40) |
n0tform3 | 7:4985455162fc | 90 | note = 6; |
n0tform3 | 7:4985455162fc | 91 | else if (axes[0]>429-40 && axes[0]<471+40 && axes[1]>-145-40 && axes[1]<-91+40 && axes[2]>480-40 && axes[2]<580+40) |
n0tform3 | 7:4985455162fc | 92 | note = 7; |
n0tform3 | 7:4985455162fc | 93 | else if (axes[0]>164-40 && axes[0]<264+40 && axes[1]>-650-40 && axes[1]<-604+40 && axes[2]>560-40 && axes[2]<700+40) |
n0tform3 | 7:4985455162fc | 94 | note = 8; |
n0tform3 | 7:4985455162fc | 95 | |
n0tform3 | 7:4985455162fc | 96 | printf("%d\n", note); |
n0tform3 | 7:4985455162fc | 97 | |
n0tform3 | 7:4985455162fc | 98 | hrService.updateHeartRate(note); |
n0tform3 | 7:4985455162fc | 99 | wait(0.3); |
n0tform3 | 7:4985455162fc | 100 | } else { |
n0tform3 | 7:4985455162fc | 101 | ble.waitForEvent(); // low power wait for event |
n0tform3 | 7:4985455162fc | 102 | } |
Wolfgang Betz |
0:c71c9af137dd | 103 | } |
Wolfgang Betz |
0:c71c9af137dd | 104 | } |