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@8:1c6281289d67, 2015-11-15 (annotated)
- Committer:
- n0tform3
- Date:
- Sun Nov 15 09:00:40 2015 +0000
- Revision:
- 8:1c6281289d67
- Parent:
- 7:4985455162fc
test with led
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" |
n0tform3 | 8:1c6281289d67 | 8 | //#include "stm32f4xx_conf.h" |
n0tform3 | 8:1c6281289d67 | 9 | //#include "stm32f4xx.h" |
n0tform3 | 8:1c6281289d67 | 10 | #include "led_RGB.h" |
n0tform3 | 8:1c6281289d67 | 11 | #include "main.h" |
Wolfgang Betz |
0:c71c9af137dd | 12 | |
n0tform3 | 7:4985455162fc | 13 | BLE ble; |
n0tform3 | 7:4985455162fc | 14 | DigitalOut led1(LED1); |
Wolfgang Betz |
0:c71c9af137dd | 15 | |
n0tform3 | 7:4985455162fc | 16 | const static char DEVICE_NAME[] = "HRM1"; |
n0tform3 | 7:4985455162fc | 17 | static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE, |
n0tform3 | 7:4985455162fc | 18 | GattService::UUID_DEVICE_INFORMATION_SERVICE}; |
n0tform3 | 7:4985455162fc | 19 | static volatile bool triggerSensorPolling = false; |
Wolfgang Betz |
0:c71c9af137dd | 20 | |
n0tform3 | 8:1c6281289d67 | 21 | uint8_t clock_source; |
n0tform3 | 8:1c6281289d67 | 22 | |
n0tform3 | 7:4985455162fc | 23 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
n0tform3 | 7:4985455162fc | 24 | { |
n0tform3 | 7:4985455162fc | 25 | ble.gap().startAdvertising(); // restart advertising |
n0tform3 | 7:4985455162fc | 26 | } |
Wolfgang Betz |
0:c71c9af137dd | 27 | |
n0tform3 | 7:4985455162fc | 28 | void periodicCallback(void) |
n0tform3 | 7:4985455162fc | 29 | { |
n0tform3 | 7:4985455162fc | 30 | led1 = !led1; |
n0tform3 | 7:4985455162fc | 31 | triggerSensorPolling = true; |
Wolfgang Betz |
0:c71c9af137dd | 32 | } |
Wolfgang Betz |
0:c71c9af137dd | 33 | |
Wolfgang Betz |
0:c71c9af137dd | 34 | |
n0tform3 | 7:4985455162fc | 35 | static X_NUCLEO_IKS01A1 *mems_expansion_board = X_NUCLEO_IKS01A1::Instance(D14, D15); |
n0tform3 | 7:4985455162fc | 36 | static MagneticSensor *magnetometer = mems_expansion_board->magnetometer; |
n0tform3 | 7:4985455162fc | 37 | |
n0tform3 | 7:4985455162fc | 38 | |
n0tform3 | 7:4985455162fc | 39 | |
n0tform3 | 7:4985455162fc | 40 | Serial pc(USBTX, USBRX); |
Wolfgang Betz |
0:c71c9af137dd | 41 | /* Simple main function */ |
Wolfgang Betz |
0:c71c9af137dd | 42 | int main() { |
Wolfgang Betz |
0:c71c9af137dd | 43 | uint8_t id; |
Wolfgang Betz |
0:c71c9af137dd | 44 | int32_t axes[3]; |
n0tform3 | 7:4985455162fc | 45 | pc.baud(115200); |
Wolfgang Betz |
0:c71c9af137dd | 46 | printf("\r\n--- Starting new run ---\r\n"); |
n0tform3 | 7:4985455162fc | 47 | |
n0tform3 | 8:1c6281289d67 | 48 | RCC_DeInit(); |
n0tform3 | 8:1c6281289d67 | 49 | RCC_HSICmd(ENABLE); |
n0tform3 | 8:1c6281289d67 | 50 | while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET); |
n0tform3 | 8:1c6281289d67 | 51 | RCC_PLLConfig(RCC_PLLSource_HSI, 8, 336, 4, 7); |
n0tform3 | 8:1c6281289d67 | 52 | RCC_PLLCmd(ENABLE); |
n0tform3 | 8:1c6281289d67 | 53 | while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)==RESET); |
n0tform3 | 8:1c6281289d67 | 54 | RCC_HCLKConfig(RCC_SYSCLK_Div1); |
n0tform3 | 8:1c6281289d67 | 55 | RCC_PCLK2Config(RCC_HCLK_Div2); //APB2 84MHz/2 = 42MHz |
n0tform3 | 8:1c6281289d67 | 56 | RCC_PCLK1Config(RCC_HCLK_Div2); |
n0tform3 | 8:1c6281289d67 | 57 | clock_source = RCC_GetSYSCLKSource(); |
n0tform3 | 8:1c6281289d67 | 58 | if (clock_source !=0) |
n0tform3 | 8:1c6281289d67 | 59 | RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); |
n0tform3 | 8:1c6281289d67 | 60 | RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOA | |
n0tform3 | 8:1c6281289d67 | 61 | RCC_AHB1Periph_GPIOB, |
n0tform3 | 8:1c6281289d67 | 62 | ENABLE); |
n0tform3 | 8:1c6281289d67 | 63 | RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM3 | |
n0tform3 | 8:1c6281289d67 | 64 | RCC_APB1Periph_TIM4 | |
n0tform3 | 8:1c6281289d67 | 65 | RCC_APB1Periph_TIM2, |
n0tform3 | 8:1c6281289d67 | 66 | ENABLE); |
n0tform3 | 8:1c6281289d67 | 67 | RCC_APB2PeriphClockCmd( RCC_APB2Periph_USART1, |
n0tform3 | 8:1c6281289d67 | 68 | ENABLE); |
n0tform3 | 8:1c6281289d67 | 69 | |
n0tform3 | 8:1c6281289d67 | 70 | Blue_Red_Setup(); |
n0tform3 | 8:1c6281289d67 | 71 | Green_Setup(); |
n0tform3 | 8:1c6281289d67 | 72 | |
n0tform3 | 8:1c6281289d67 | 73 | |
n0tform3 | 8:1c6281289d67 | 74 | |
Wolfgang Betz |
0:c71c9af137dd | 75 | magnetometer->ReadID(&id); |
Wolfgang Betz |
0:c71c9af137dd | 76 | printf("LIS3MDL magnetometer = 0x%X\r\n", id); |
Wolfgang Betz |
0:c71c9af137dd | 77 | |
n0tform3 | 8:1c6281289d67 | 78 | led1 = 1; |
n0tform3 | 7:4985455162fc | 79 | Ticker ticker; |
n0tform3 | 7:4985455162fc | 80 | ticker.attach(periodicCallback, 1); // blink LED every second |
n0tform3 | 7:4985455162fc | 81 | |
n0tform3 | 7:4985455162fc | 82 | ble.init(); |
n0tform3 | 7:4985455162fc | 83 | ble.gap().onDisconnection(disconnectionCallback); |
n0tform3 | 7:4985455162fc | 84 | |
n0tform3 | 7:4985455162fc | 85 | /* Setup primary service. */ |
n0tform3 | 7:4985455162fc | 86 | uint8_t hrmCounter = 0; // init HRM to 100bps |
n0tform3 | 7:4985455162fc | 87 | HeartRateService hrService(ble, hrmCounter, HeartRateService::LOCATION_FINGER); |
n0tform3 | 7:4985455162fc | 88 | |
n0tform3 | 7:4985455162fc | 89 | /* Setup auxiliary service. */ |
n0tform3 | 7:4985455162fc | 90 | DeviceInformationService deviceInfo(ble, "ARM", "Model1", "SN1", "hw-rev1", "fw-rev1", "soft-rev1"); |
Wolfgang Betz |
0:c71c9af137dd | 91 | |
n0tform3 | 7:4985455162fc | 92 | /* Setup advertising. */ |
n0tform3 | 7:4985455162fc | 93 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
n0tform3 | 7:4985455162fc | 94 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
n0tform3 | 7:4985455162fc | 95 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR); |
n0tform3 | 7:4985455162fc | 96 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
n0tform3 | 7:4985455162fc | 97 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
n0tform3 | 7:4985455162fc | 98 | ble.gap().setAdvertisingInterval(1000); /* 1000ms */ |
n0tform3 | 7:4985455162fc | 99 | |
n0tform3 | 7:4985455162fc | 100 | ble.gap().startAdvertising(); |
n0tform3 | 7:4985455162fc | 101 | |
Wolfgang Betz |
0:c71c9af137dd | 102 | |
n0tform3 | 7:4985455162fc | 103 | uint8_t note = 0; |
n0tform3 | 7:4985455162fc | 104 | while(1) { |
n0tform3 | 7:4985455162fc | 105 | if (triggerSensorPolling && ble.getGapState().connected) { |
n0tform3 | 7:4985455162fc | 106 | triggerSensorPolling = false; |
Wolfgang Betz |
0:c71c9af137dd | 107 | |
n0tform3 | 7:4985455162fc | 108 | magnetometer->Get_M_Axes(axes); |
n0tform3 | 7:4985455162fc | 109 | //printf("%6ld,\t %6ld,\t %6ld\r\n", axes[0], axes[1], axes[2]); |
n0tform3 | 7:4985455162fc | 110 | |
n0tform3 | 7:4985455162fc | 111 | note = 0; |
n0tform3 | 7:4985455162fc | 112 | 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 | 113 | note = 1; |
n0tform3 | 7:4985455162fc | 114 | 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 | 115 | note = 2; |
n0tform3 | 7:4985455162fc | 116 | 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 | 117 | note = 3; |
n0tform3 | 7:4985455162fc | 118 | 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 | 119 | note = 4; |
n0tform3 | 7:4985455162fc | 120 | 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 | 121 | note = 5; |
n0tform3 | 7:4985455162fc | 122 | 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 | 123 | note = 6; |
n0tform3 | 7:4985455162fc | 124 | 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 | 125 | note = 7; |
n0tform3 | 7:4985455162fc | 126 | 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 | 127 | note = 8; |
n0tform3 | 7:4985455162fc | 128 | |
n0tform3 | 7:4985455162fc | 129 | printf("%d\n", note); |
n0tform3 | 7:4985455162fc | 130 | |
n0tform3 | 7:4985455162fc | 131 | hrService.updateHeartRate(note); |
n0tform3 | 7:4985455162fc | 132 | wait(0.3); |
n0tform3 | 7:4985455162fc | 133 | } else { |
n0tform3 | 7:4985455162fc | 134 | ble.waitForEvent(); // low power wait for event |
n0tform3 | 7:4985455162fc | 135 | } |
n0tform3 | 8:1c6281289d67 | 136 | switch (note){ |
n0tform3 | 8:1c6281289d67 | 137 | case DO: TIM_SetCompare4(TIM3, 255); //RED |
n0tform3 | 8:1c6281289d67 | 138 | TIM_SetCompare1(TIM4, 0); //GREEN |
n0tform3 | 8:1c6281289d67 | 139 | TIM_SetCompare3(TIM3, 0); //BLU |
n0tform3 | 8:1c6281289d67 | 140 | break; |
n0tform3 | 8:1c6281289d67 | 141 | case RE: TIM_SetCompare4(TIM3, 255); //RED |
n0tform3 | 8:1c6281289d67 | 142 | TIM_SetCompare1(TIM4, 153); //GREEN |
n0tform3 | 8:1c6281289d67 | 143 | TIM_SetCompare3(TIM3, 0); //BLU |
n0tform3 | 8:1c6281289d67 | 144 | break; |
n0tform3 | 8:1c6281289d67 | 145 | case MI: TIM_SetCompare4(TIM3, 255); //RED |
n0tform3 | 8:1c6281289d67 | 146 | TIM_SetCompare1(TIM4, 255); //GREEN |
n0tform3 | 8:1c6281289d67 | 147 | TIM_SetCompare3(TIM3, 0); //BLU |
n0tform3 | 8:1c6281289d67 | 148 | break; |
n0tform3 | 8:1c6281289d67 | 149 | case FA: TIM_SetCompare4(TIM3, 0); //RED |
n0tform3 | 8:1c6281289d67 | 150 | TIM_SetCompare1(TIM4, 255); //GREEN |
n0tform3 | 8:1c6281289d67 | 151 | TIM_SetCompare3(TIM3, 0); //BLU |
n0tform3 | 8:1c6281289d67 | 152 | break; |
n0tform3 | 8:1c6281289d67 | 153 | case SOL: TIM_SetCompare4(TIM3, 0); //RED |
n0tform3 | 8:1c6281289d67 | 154 | TIM_SetCompare1(TIM4, 0); //GREEN |
n0tform3 | 8:1c6281289d67 | 155 | TIM_SetCompare3(TIM3, 255); //BLU |
n0tform3 | 8:1c6281289d67 | 156 | break; |
n0tform3 | 8:1c6281289d67 | 157 | case LA: TIM_SetCompare4(TIM3, 51); //RED |
n0tform3 | 8:1c6281289d67 | 158 | TIM_SetCompare1(TIM4, 0); //GREEN |
n0tform3 | 8:1c6281289d67 | 159 | TIM_SetCompare3(TIM3, 153); //BLU |
n0tform3 | 8:1c6281289d67 | 160 | break; |
n0tform3 | 8:1c6281289d67 | 161 | case SI: TIM_SetCompare4(TIM3, 255); //RED |
n0tform3 | 8:1c6281289d67 | 162 | TIM_SetCompare1(TIM4, 0); //GREEN |
n0tform3 | 8:1c6281289d67 | 163 | TIM_SetCompare3(TIM3, 255); //BLU |
n0tform3 | 8:1c6281289d67 | 164 | break; |
n0tform3 | 8:1c6281289d67 | 165 | case DO1: TIM_SetCompare4(TIM3, 255); //RED |
n0tform3 | 8:1c6281289d67 | 166 | TIM_SetCompare1(TIM4, 255); //GREEN |
n0tform3 | 8:1c6281289d67 | 167 | TIM_SetCompare3(TIM3, 255); //BLU |
n0tform3 | 8:1c6281289d67 | 168 | break; |
n0tform3 | 8:1c6281289d67 | 169 | case default: |
n0tform3 | 8:1c6281289d67 | 170 | TIM_SetCompare4(TIM3, 0); //RED |
n0tform3 | 8:1c6281289d67 | 171 | TIM_SetCompare1(TIM4, 0); //GREEN |
n0tform3 | 8:1c6281289d67 | 172 | TIM_SetCompare3(TIM3, 0); //BLU |
Wolfgang Betz |
0:c71c9af137dd | 173 | } |
Wolfgang Betz |
0:c71c9af137dd | 174 | } |