mattia griotti / Mbed 2 deprecated HelloWorld_IKS01A1

Dependencies:   BLE_API X_NUCLEO_IDB0XA1 X_NUCLEO_IKS01A1 mbed

Fork of HelloWorld_IKS01A1 by ST

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "x_nucleo_iks01a1.h"
00003 #include "mbed.h"
00004 #include "ble/BLE.h"
00005 #include "ble/services/HeartRateService.h"
00006 #include "ble/services/BatteryService.h"
00007 #include "ble/services/DeviceInformationService.h"
00008 //#include "stm32f4xx_conf.h"
00009 //#include "stm32f4xx.h"
00010 #include "led_RGB.h"
00011 #include "main.h"
00012 
00013 BLE ble;
00014 DigitalOut led1(LED1);
00015 
00016 const static char DEVICE_NAME[] = "HRM1";
00017 static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE,
00018 GattService::UUID_DEVICE_INFORMATION_SERVICE};
00019 static volatile bool triggerSensorPolling = false;
00020 
00021 uint8_t clock_source;
00022 
00023 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
00024 {
00025     ble.gap().startAdvertising(); // restart advertising
00026 }
00027 
00028 void periodicCallback(void)
00029 {
00030     led1 = !led1;
00031     triggerSensorPolling = true;
00032 }
00033 
00034 
00035 static X_NUCLEO_IKS01A1 *mems_expansion_board = X_NUCLEO_IKS01A1::Instance(D14, D15);
00036 static MagneticSensor *magnetometer = mems_expansion_board->magnetometer;
00037 
00038 
00039 
00040 Serial pc(USBTX, USBRX);
00041 /* Simple main function */
00042 int main() {
00043   uint8_t id;
00044   int32_t axes[3];
00045   pc.baud(115200);
00046   printf("\r\n--- Starting new run ---\r\n");
00047   
00048     RCC_DeInit();
00049     RCC_HSICmd(ENABLE);
00050     while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET);
00051     RCC_PLLConfig(RCC_PLLSource_HSI, 8, 336, 4, 7);
00052     RCC_PLLCmd(ENABLE);
00053     while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)==RESET);
00054     RCC_HCLKConfig(RCC_SYSCLK_Div1);
00055     RCC_PCLK2Config(RCC_HCLK_Div2); //APB2 84MHz/2 = 42MHz
00056     RCC_PCLK1Config(RCC_HCLK_Div2);
00057     clock_source = RCC_GetSYSCLKSource();
00058     if (clock_source !=0)
00059         RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
00060     RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOA |
00061                             RCC_AHB1Periph_GPIOB,
00062                             ENABLE);
00063     RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM3 |
00064                             RCC_APB1Periph_TIM4 |
00065                             RCC_APB1Periph_TIM2,
00066                             ENABLE);
00067     RCC_APB2PeriphClockCmd( RCC_APB2Periph_USART1,
00068                             ENABLE);
00069                             
00070     Blue_Red_Setup();
00071     Green_Setup();
00072   
00073   
00074   
00075   magnetometer->ReadID(&id);
00076   printf("LIS3MDL magnetometer              = 0x%X\r\n", id);
00077   
00078     led1 = 1;
00079     Ticker ticker;
00080     ticker.attach(periodicCallback, 1); // blink LED every second
00081     
00082     ble.init();
00083     ble.gap().onDisconnection(disconnectionCallback);
00084     
00085     /* Setup primary service. */
00086     uint8_t hrmCounter = 0; // init HRM to 100bps
00087     HeartRateService hrService(ble, hrmCounter, HeartRateService::LOCATION_FINGER);
00088     
00089     /* Setup auxiliary service. */
00090     DeviceInformationService deviceInfo(ble, "ARM", "Model1", "SN1", "hw-rev1", "fw-rev1", "soft-rev1");
00091     
00092     /* Setup advertising. */
00093     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
00094     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
00095     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR);
00096     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
00097     ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00098     ble.gap().setAdvertisingInterval(1000); /* 1000ms */
00099   
00100     ble.gap().startAdvertising();
00101   
00102 
00103   uint8_t  note = 0;
00104   while(1) {
00105     if (triggerSensorPolling && ble.getGapState().connected) {
00106         triggerSensorPolling = false;
00107 
00108         magnetometer->Get_M_Axes(axes);
00109         //printf("%6ld,\t %6ld,\t %6ld\r\n", axes[0], axes[1], axes[2]);
00110         
00111         note = 0;
00112         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)
00113             note = 1;
00114         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)
00115             note = 2;
00116         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)
00117             note = 3;
00118         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)
00119             note = 4;
00120         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)
00121             note = 5;
00122         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)
00123             note = 6;
00124         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)
00125             note = 7;
00126         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)
00127             note = 8;
00128             
00129         printf("%d\n", note);
00130     
00131         hrService.updateHeartRate(note);
00132         wait(0.3);
00133     } else {
00134         ble.waitForEvent(); // low power wait for event
00135     }
00136     switch (note){
00137             case DO:    TIM_SetCompare4(TIM3, 255); //RED
00138                         TIM_SetCompare1(TIM4, 0);   //GREEN
00139                         TIM_SetCompare3(TIM3, 0);   //BLU
00140                 break;
00141             case RE:    TIM_SetCompare4(TIM3, 255); //RED
00142                         TIM_SetCompare1(TIM4, 153); //GREEN
00143                         TIM_SetCompare3(TIM3, 0);   //BLU
00144                 break;
00145             case MI:    TIM_SetCompare4(TIM3, 255); //RED
00146                         TIM_SetCompare1(TIM4, 255); //GREEN
00147                         TIM_SetCompare3(TIM3, 0);   //BLU
00148                 break;
00149             case FA:    TIM_SetCompare4(TIM3, 0);   //RED
00150                         TIM_SetCompare1(TIM4, 255); //GREEN
00151                         TIM_SetCompare3(TIM3, 0);   //BLU
00152                 break;
00153             case SOL:   TIM_SetCompare4(TIM3, 0);   //RED
00154                         TIM_SetCompare1(TIM4, 0);   //GREEN
00155                         TIM_SetCompare3(TIM3, 255);     //BLU
00156                 break;
00157             case LA:    TIM_SetCompare4(TIM3, 51);  //RED
00158                         TIM_SetCompare1(TIM4, 0);   //GREEN
00159                         TIM_SetCompare3(TIM3, 153);     //BLU
00160                 break;
00161             case SI:    TIM_SetCompare4(TIM3, 255); //RED
00162                         TIM_SetCompare1(TIM4, 0);   //GREEN
00163                         TIM_SetCompare3(TIM3, 255);     //BLU
00164                 break;
00165             case DO1:   TIM_SetCompare4(TIM3, 255); //RED
00166                         TIM_SetCompare1(TIM4, 255); //GREEN
00167                         TIM_SetCompare3(TIM3, 255);     //BLU
00168                 break;
00169             case default:
00170                         TIM_SetCompare4(TIM3, 0);   //RED
00171                         TIM_SetCompare1(TIM4, 0);   //GREEN
00172                         TIM_SetCompare3(TIM3, 0);   //BLU
00173   }
00174 }