wanjun chen / Mbed 2 deprecated Nucleo_BLE_Demo

Dependencies:   Nucleo_BLE_API_IOTIO Nucleo_BLE_BlueNRG Nucleo_BLE_DemoApp Nucleo_Sensor_Shield mbed

Dependents:   Nucleo_BLE_Demo_IOTIO

Fork of Nucleo_BLE_Demo by Cortex Challenge Team

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* Bluetooth low energy demo application
00002  * Copyright (c) 2015 Adam Berlinger
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "mbed.h"
00018 #include "DemoAppService.h"
00019 #include "x_cube_mems.h"
00020 #define PI 3.1415169f
00021 float TEMPERATURE_C = 20;
00022 float HUMIDITY = 50;
00023 float PRESSURE = 1000;
00024 float WIND_DIRECTION = 0;
00025 int16_t MagRaw[3];
00026 AxesRaw_TypeDef MAGNETIC;
00027 
00028 extern bool user_button_pressed;
00029 bool connected = false;
00030 bool UpdatedEnabled = false;
00031 
00032 DemoAppService *demoService;
00033 PwmOut led(LED1);
00034 
00035 void demoCallback(uint8_t event){
00036     /* Nastaveni stridy PWM signalu */
00037     if(event & DemoAppService::EVENT_SLIDER1_CHANGED){
00038         int x = 1 << (demoService->getSlider1Value() / 10);
00039         led.write((float)(x / (float)(1 << 10)));
00040     }
00041     /* Nastaveni periody PWM signalu */
00042     if(event & DemoAppService::EVENT_SLIDER2_CHANGED){
00043         led.period(1.0f / (demoService->getSlider2Value() + 1));
00044     }
00045 }
00046 
00047 int main(void)
00048 {
00049   demoService = startDemoBLE("BLE-12345678");
00050   demoService->setCallback(demoCallback);
00051 
00052   led = 0.0f;
00053   
00054   static X_CUBE_MEMS *Sensors = X_CUBE_MEMS::Instance();
00055  
00056   
00057   while (true) 
00058   {
00059        
00060             /* Read the environmental sensors  */
00061                   //  Sensors->hts221.GetTemperature((float *)&TEMPERATURE_C);
00062 //                    Sensors->hts221.GetHumidity((float *)&HUMIDITY);
00063 //                    Sensors->lps25h.GetPressure((float *)&PRESSURE);
00064                     
00065                     Sensors->hts221.GetTemperature((float *)&TEMPERATURE_C);
00066                     Sensors->hts221.GetHumidity((float *)&HUMIDITY);
00067                     Sensors->lps25h.GetPressure((float *)&PRESSURE);
00068                     Sensors->lis3mdl.GetAxes((AxesRaw_TypeDef *)&MAGNETIC);
00069                  
00070                     
00071                                             TEMPERATURE_C = TEMPERATURE_C*100;  //2 decimals
00072                                             HUMIDITY = HUMIDITY*100;                        //2 decimals
00073                                             PRESSURE = PRESSURE*1000;           //hPa to Pa + 1 decimal
00074                                                     
00075                                                     //Calcule the direction where the system is pointing relative to North.
00076                     //I have used a simple empirical method to distinguish between 8 directions. 
00077                     if (MAGNETIC.AXIS_X < 140) WIND_DIRECTION = 0; //North
00078                     else if (MAGNETIC.AXIS_X >= 140 && MAGNETIC.AXIS_X < 200 && -MAGNETIC.AXIS_Y > 250 ) WIND_DIRECTION = 45;  //Northeast
00079                     else if (MAGNETIC.AXIS_X >= 140 && MAGNETIC.AXIS_X < 200 && -MAGNETIC.AXIS_Y < 250 ) WIND_DIRECTION = 315; //Northwest
00080                     else if (MAGNETIC.AXIS_X >= 200 && MAGNETIC.AXIS_X < 280 && -MAGNETIC.AXIS_Y > 250 ) WIND_DIRECTION = 90;  //East
00081                     else if (MAGNETIC.AXIS_X >= 200 && MAGNETIC.AXIS_X < 280 && -MAGNETIC.AXIS_Y < 250 ) WIND_DIRECTION = 270; //Weast
00082                     else if (MAGNETIC.AXIS_X >= 280 && MAGNETIC.AXIS_X < 380 && -MAGNETIC.AXIS_Y > 250 ) WIND_DIRECTION = 135; //Southeast
00083                     else if (MAGNETIC.AXIS_X >= 280 && MAGNETIC.AXIS_X < 380 && -MAGNETIC.AXIS_Y < 250 ) WIND_DIRECTION = 225; //Soutwest           
00084                     else if (MAGNETIC.AXIS_X >= 380) WIND_DIRECTION = 180; //South
00085                     
00086                      WIND_DIRECTION *=100;                             //2 decimals
00087                                                     
00088                                                          demoService->updateHumidity(HUMIDITY);
00089                                                          demoService->updateTemperature(TEMPERATURE_C);
00090                                                          demoService->updateWinddirection(WIND_DIRECTION);
00091                                                          demoService->updatePressure(PRESSURE);
00092             demoService->waitForEvent();
00093          
00094     
00095   }
00096 }