IOTIO
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
main.cpp@1:4bdfa7d7e8bf, 2016-10-29 (annotated)
- Committer:
- 16038618
- Date:
- Sat Oct 29 15:11:28 2016 +0000
- Revision:
- 1:4bdfa7d7e8bf
- Parent:
- 0:d450c9945f96
- Child:
- 2:c69117fc79da
IOTIO
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
berlingeradam | 0:d450c9945f96 | 1 | /* Bluetooth low energy demo application |
berlingeradam | 0:d450c9945f96 | 2 | * Copyright (c) 2015 Adam Berlinger |
berlingeradam | 0:d450c9945f96 | 3 | * |
berlingeradam | 0:d450c9945f96 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
berlingeradam | 0:d450c9945f96 | 5 | * you may not use this file except in compliance with the License. |
berlingeradam | 0:d450c9945f96 | 6 | * You may obtain a copy of the License at |
berlingeradam | 0:d450c9945f96 | 7 | * |
berlingeradam | 0:d450c9945f96 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
berlingeradam | 0:d450c9945f96 | 9 | * |
berlingeradam | 0:d450c9945f96 | 10 | * Unless required by applicable law or agreed to in writing, software |
berlingeradam | 0:d450c9945f96 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
berlingeradam | 0:d450c9945f96 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
berlingeradam | 0:d450c9945f96 | 13 | * See the License for the specific language governing permissions and |
berlingeradam | 0:d450c9945f96 | 14 | * limitations under the License. |
berlingeradam | 0:d450c9945f96 | 15 | */ |
berlingeradam | 0:d450c9945f96 | 16 | |
berlingeradam | 0:d450c9945f96 | 17 | #include "mbed.h" |
berlingeradam | 0:d450c9945f96 | 18 | #include "DemoAppService.h" |
16038618 | 1:4bdfa7d7e8bf | 19 | #include "x_cube_mems.h" |
16038618 | 1:4bdfa7d7e8bf | 20 | #define PI 3.1415169f |
16038618 | 1:4bdfa7d7e8bf | 21 | float TEMPERATURE_C = 20; |
16038618 | 1:4bdfa7d7e8bf | 22 | float HUMIDITY = 50; |
16038618 | 1:4bdfa7d7e8bf | 23 | float PRESSURE = 1000; |
16038618 | 1:4bdfa7d7e8bf | 24 | float WIND_DIRECTION = 0; |
16038618 | 1:4bdfa7d7e8bf | 25 | int16_t MagRaw[3]; |
16038618 | 1:4bdfa7d7e8bf | 26 | AxesRaw_TypeDef MAGNETIC; |
berlingeradam | 0:d450c9945f96 | 27 | |
berlingeradam | 0:d450c9945f96 | 28 | extern bool user_button_pressed; |
berlingeradam | 0:d450c9945f96 | 29 | bool connected = false; |
berlingeradam | 0:d450c9945f96 | 30 | bool UpdatedEnabled = false; |
berlingeradam | 0:d450c9945f96 | 31 | |
berlingeradam | 0:d450c9945f96 | 32 | DemoAppService *demoService; |
berlingeradam | 0:d450c9945f96 | 33 | PwmOut led(LED1); |
berlingeradam | 0:d450c9945f96 | 34 | |
berlingeradam | 0:d450c9945f96 | 35 | void demoCallback(uint8_t event){ |
berlingeradam | 0:d450c9945f96 | 36 | /* Nastaveni stridy PWM signalu */ |
berlingeradam | 0:d450c9945f96 | 37 | if(event & DemoAppService::EVENT_SLIDER1_CHANGED){ |
berlingeradam | 0:d450c9945f96 | 38 | int x = 1 << (demoService->getSlider1Value() / 10); |
berlingeradam | 0:d450c9945f96 | 39 | led.write((float)(x / (float)(1 << 10))); |
berlingeradam | 0:d450c9945f96 | 40 | } |
berlingeradam | 0:d450c9945f96 | 41 | /* Nastaveni periody PWM signalu */ |
berlingeradam | 0:d450c9945f96 | 42 | if(event & DemoAppService::EVENT_SLIDER2_CHANGED){ |
berlingeradam | 0:d450c9945f96 | 43 | led.period(1.0f / (demoService->getSlider2Value() + 1)); |
berlingeradam | 0:d450c9945f96 | 44 | } |
berlingeradam | 0:d450c9945f96 | 45 | } |
berlingeradam | 0:d450c9945f96 | 46 | |
berlingeradam | 0:d450c9945f96 | 47 | int main(void) |
berlingeradam | 0:d450c9945f96 | 48 | { |
16038618 | 1:4bdfa7d7e8bf | 49 | demoService = startDemoBLE("BLE-1234567"); |
berlingeradam | 0:d450c9945f96 | 50 | demoService->setCallback(demoCallback); |
berlingeradam | 0:d450c9945f96 | 51 | |
berlingeradam | 0:d450c9945f96 | 52 | led = 0.0f; |
16038618 | 1:4bdfa7d7e8bf | 53 | |
16038618 | 1:4bdfa7d7e8bf | 54 | static X_CUBE_MEMS *Sensors = X_CUBE_MEMS::Instance(); |
16038618 | 1:4bdfa7d7e8bf | 55 | |
16038618 | 1:4bdfa7d7e8bf | 56 | |
berlingeradam | 0:d450c9945f96 | 57 | while (true) |
berlingeradam | 0:d450c9945f96 | 58 | { |
16038618 | 1:4bdfa7d7e8bf | 59 | |
16038618 | 1:4bdfa7d7e8bf | 60 | /* Read the environmental sensors */ |
16038618 | 1:4bdfa7d7e8bf | 61 | // Sensors->hts221.GetTemperature((float *)&TEMPERATURE_C); |
16038618 | 1:4bdfa7d7e8bf | 62 | // Sensors->hts221.GetHumidity((float *)&HUMIDITY); |
16038618 | 1:4bdfa7d7e8bf | 63 | // Sensors->lps25h.GetPressure((float *)&PRESSURE); |
16038618 | 1:4bdfa7d7e8bf | 64 | |
16038618 | 1:4bdfa7d7e8bf | 65 | Sensors->hts221.GetTemperature((float *)&TEMPERATURE_C); |
16038618 | 1:4bdfa7d7e8bf | 66 | Sensors->hts221.GetHumidity((float *)&HUMIDITY); |
16038618 | 1:4bdfa7d7e8bf | 67 | Sensors->lps25h.GetPressure((float *)&PRESSURE); |
16038618 | 1:4bdfa7d7e8bf | 68 | Sensors->lis3mdl.GetAxes((AxesRaw_TypeDef *)&MAGNETIC); |
16038618 | 1:4bdfa7d7e8bf | 69 | |
16038618 | 1:4bdfa7d7e8bf | 70 | |
16038618 | 1:4bdfa7d7e8bf | 71 | TEMPERATURE_C = TEMPERATURE_C*100; //2 decimals |
16038618 | 1:4bdfa7d7e8bf | 72 | HUMIDITY = HUMIDITY*100; //2 decimals |
16038618 | 1:4bdfa7d7e8bf | 73 | PRESSURE = PRESSURE*1000; //hPa to Pa + 1 decimal |
16038618 | 1:4bdfa7d7e8bf | 74 | |
16038618 | 1:4bdfa7d7e8bf | 75 | //Calcule the direction where the system is pointing relative to North. |
16038618 | 1:4bdfa7d7e8bf | 76 | //I have used a simple empirical method to distinguish between 8 directions. |
16038618 | 1:4bdfa7d7e8bf | 77 | if (MAGNETIC.AXIS_X < 140) WIND_DIRECTION = 0; //North |
16038618 | 1:4bdfa7d7e8bf | 78 | else if (MAGNETIC.AXIS_X >= 140 && MAGNETIC.AXIS_X < 200 && -MAGNETIC.AXIS_Y > 250 ) WIND_DIRECTION = 45; //Northeast |
16038618 | 1:4bdfa7d7e8bf | 79 | else if (MAGNETIC.AXIS_X >= 140 && MAGNETIC.AXIS_X < 200 && -MAGNETIC.AXIS_Y < 250 ) WIND_DIRECTION = 315; //Northwest |
16038618 | 1:4bdfa7d7e8bf | 80 | else if (MAGNETIC.AXIS_X >= 200 && MAGNETIC.AXIS_X < 280 && -MAGNETIC.AXIS_Y > 250 ) WIND_DIRECTION = 90; //East |
16038618 | 1:4bdfa7d7e8bf | 81 | else if (MAGNETIC.AXIS_X >= 200 && MAGNETIC.AXIS_X < 280 && -MAGNETIC.AXIS_Y < 250 ) WIND_DIRECTION = 270; //Weast |
16038618 | 1:4bdfa7d7e8bf | 82 | else if (MAGNETIC.AXIS_X >= 280 && MAGNETIC.AXIS_X < 380 && -MAGNETIC.AXIS_Y > 250 ) WIND_DIRECTION = 135; //Southeast |
16038618 | 1:4bdfa7d7e8bf | 83 | else if (MAGNETIC.AXIS_X >= 280 && MAGNETIC.AXIS_X < 380 && -MAGNETIC.AXIS_Y < 250 ) WIND_DIRECTION = 225; //Soutwest |
16038618 | 1:4bdfa7d7e8bf | 84 | else if (MAGNETIC.AXIS_X >= 380) WIND_DIRECTION = 180; //South |
16038618 | 1:4bdfa7d7e8bf | 85 | |
16038618 | 1:4bdfa7d7e8bf | 86 | WIND_DIRECTION *=100; //2 decimals |
16038618 | 1:4bdfa7d7e8bf | 87 | |
16038618 | 1:4bdfa7d7e8bf | 88 | demoService->updateHumidity(HUMIDITY); |
16038618 | 1:4bdfa7d7e8bf | 89 | demoService->updateTemperature(TEMPERATURE_C); |
16038618 | 1:4bdfa7d7e8bf | 90 | demoService->updateWinddirection(WIND_DIRECTION); |
16038618 | 1:4bdfa7d7e8bf | 91 | demoService->updatePressure(PRESSURE); |
16038618 | 1:4bdfa7d7e8bf | 92 | demoService->waitForEvent(); |
16038618 | 1:4bdfa7d7e8bf | 93 | |
16038618 | 1:4bdfa7d7e8bf | 94 | |
berlingeradam | 0:d450c9945f96 | 95 | } |
berlingeradam | 0:d450c9945f96 | 96 | } |