士鈞 林 / Mbed OS 12_1

Dependencies:   X_NUCLEO_IKS01A2

Files at this revision

API Documentation at this revision

Comitter:
jim_lsj
Date:
Wed Apr 29 16:18:37 2020 +0000
Parent:
0:9c0e0ac79e75
Commit message:
...;

Changed in this revision

source/ESS.h Show annotated file Show diff for this revision Revisions of this file
source/ESS2.h Show annotated file Show diff for this revision Revisions of this file
source/main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/source/ESS.h	Wed Apr 29 10:42:44 2020 +0000
+++ b/source/ESS.h	Wed Apr 29 16:18:37 2020 +0000
@@ -9,7 +9,7 @@
 #ifndef __BLE_ENVIRONMENTAL_SENSING_SERVICE_H__
 #define __BLE_ENVIRONMENTAL_SENSING_SERVICE_H__
 
-#include "mbed.h"
+#include <mbed.h>
 #include "blecommon.h"
 #include "Gap.h"
 #include "GattServer.h"
@@ -53,18 +53,18 @@
 
                 GattService      EnvironmentalService(0x181A, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
 
-        ble.addService(EnvironmentalService);
+        ble.gattServer().addService(EnvironmentalService);
     }
 
 
     /* Set a new 16-bit value for the humidity measurement.  */
     void updateHumidity(uint16_t humidity) {
-        ble.updateCharacteristicValue(humiditychar.getValueAttribute().getHandle(), (uint8_t *)&humidity, sizeof(uint16_t));
+        ble.gattServer().write(humiditychar.getValueAttribute().getHandle(), (uint8_t *)&humidity, sizeof(uint16_t));
     }
 
                  /* Set a new 16-bit value for the temperature measurement.  */
     void updateTemperature(int16_t temperature) {
-        ble.updateCharacteristicValue(temperaturechar.getValueAttribute().getHandle(), (uint8_t *)&temperature, sizeof(int16_t));
+        ble.gattServer().write(temperaturechar.getValueAttribute().getHandle(), (uint8_t *)&temperature, sizeof(int16_t));
     }
 
 
--- a/source/ESS2.h	Wed Apr 29 10:42:44 2020 +0000
+++ b/source/ESS2.h	Wed Apr 29 16:18:37 2020 +0000
@@ -9,7 +9,7 @@
 #ifndef __BLE_ENVIRONMENTAL_SENSING_SERVICE2_H__
 #define __BLE_ENVIRONMENTAL_SENSING_SERVICE2_H__
 
-#include "mbed.h"
+#include <mbed.h>
 #include "blecommon.h"
 #include "Gap.h"
 #include "GattServer.h"
@@ -55,17 +55,17 @@
 
                 GattService      EnvironmentalService(0x181A, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
 
-        ble.addService(EnvironmentalService);
+        ble.gattServer().addService(EnvironmentalService);
     }
 
 
         /* Set a new 16-bit value for the wind direction measurement.  */
     void updateWinddirection(uint16_t winddirection) {
-        ble.updateCharacteristicValue(winddirectionchar.getValueAttribute().getHandle(), (uint8_t *)&winddirection, sizeof(uint16_t));
+        ble.gattServer().write(winddirectionchar.getValueAttribute().getHandle(), (uint8_t *)&winddirection, sizeof(uint16_t));
     }       
     /* Set a new 32-bit value for the pressure measurement.  */
     void updatePressure(uint32_t pressure) {
-        ble.updateCharacteristicValue(pressurechar.getValueAttribute().getHandle(), (uint8_t *)&pressure, sizeof(uint32_t));
+        ble.gattServer().write(pressurechar.getValueAttribute().getHandle(), (uint8_t *)&pressure, sizeof(uint32_t));
     }
 
         
--- a/source/main.cpp	Wed Apr 29 10:42:44 2020 +0000
+++ b/source/main.cpp	Wed Apr 29 16:18:37 2020 +0000
@@ -34,20 +34,27 @@
 #include "ESS.h"
 #include "ESS2.h"
 
-const static char DEVICE_NAME[] = "Heartrate";
+//const static char DEVICE_NAME[] = "Heartrate";
 
 
 
 // new code
-static XNucleoIKS01A2 *Sensors = XNucleoIKS01A2::instance(D14, D15, D4, D5);
-float TEMPERATURE_C = 20;
-float HUMIDITY = 50;
-float PRESSURE = 1000;
-float WIND_DIRECTION = 0;
+Serial pc(USBTX, USBRX);
+static XNucleoIKS01A2 *mems_expansion_board = XNucleoIKS01A2::instance(D14, D15, D4, D5);
+static LSM303AGRMagSensor *magnetometer = mems_expansion_board->magnetometer;
+static HTS221Sensor *hum_temp = mems_expansion_board->ht_sensor;
+static LPS22HBSensor *press_temp = mems_expansion_board->pt_sensor;
+static LSM6DSLSensor *acc_gyro = mems_expansion_board->acc_gyro;
+static LSM303AGRAccSensor *accelerometer = mems_expansion_board->accelerometer;
+volatile float TEMPERATURE_C=20;
+volatile float TEMPERATURE_F;
+volatile float TEMPERATURE_K;
+volatile float HUMIDITY=50;
+volatile float PRESSURE=1000;
+volatile float WIND_DIRECTION=0;
 int16_t MagRaw[3];
-AxesRaw_TypeDef MAGNETIC;
+int16_t * MAGNETIC = new int16_t[3];
 const static char     DEVICE_NAME[]        = "WEATHER";
-static const uint16_t uuid16_list[]        = {0x181A};
 
 static volatile bool  triggerSensorPolling = false;
 //end
@@ -63,20 +70,21 @@
         _connected(false),
         _hr_counter(100),
         _bt_service(ble, 25),
-        _hr_service(ble, _hr_counter, HeartRateService::LOCATION_FINGER),
+        //_hr_service(ble, _hr_counter, HeartRateService::LOCATION_FINGER),
         _deviceInfo(ble, "ST", "Nucleo", "SN1" ),
         _adv_data_builder(_adv_buffer),
         
         _air (ble, (uint16_t) HUMIDITY, (int16_t) TEMPERATURE_C ),
         _wind (ble, (uint16_t) WIND_DIRECTION, (uint32_t) PRESSURE)
         { 
+            uuid16_list = new UUID[1]{0x181A};
             //_uuid_list = new UUID(3);
 //            _uuid_list[0] = GattService::UUID_HEART_RATE_SERVICE;
 //            _uuid_list[1] = GattService::UUID_BATTERY_SERVICE;
 //            _uuid_list[2] = GattService::UUID_DEVICE_INFORMATION_SERVICE;
         }
     ~HeartrateDemo(){
-        delete [] _uuid_list;
+        delete [] uuid16_list;
     }
 
     void start() {
@@ -113,7 +121,7 @@
 
         _adv_data_builder.setFlags();
         _adv_data_builder.setAppearance(ble::adv_data_appearance_t::GENERIC_THERMOMETER);
-        _adv_data_builder.setLocalServiceList(mbed::make_Span(&uuid16_list, 1));
+        _adv_data_builder.setLocalServiceList(mbed::make_Span(uuid16_list, 1));
         _adv_data_builder.setName(DEVICE_NAME);
 
         /* Setup advertising */
@@ -152,12 +160,41 @@
         if (_connected) {
             // Do blocking calls or whatever is necessary for sensor polling.
             // In our case, we simply update the HRM measurement.
-            Sensors->hts221.GetTemperature((float *)&TEMPERATURE_C);
-            Sensors->hts221.GetHumidity((float *)&HUMIDITY);
-            Sensors->lps25h.GetPressure((float *)&PRESSURE);
-            Sensors->lis3mdl.GetAxes((AxesRaw_TypeDef *)&MAGNETIC);
-
-            _hr_service.updateHeartRate(_hr_counter);
+            hum_temp->get_temperature((float *)&TEMPERATURE_C);
+            hum_temp->get_humidity((float *)&HUMIDITY);
+            press_temp->get_pressure((float *)&PRESSURE);
+            magnetometer->get_m_axes_raw((int16_t *)MAGNETIC);
+            TEMPERATURE_C = TEMPERATURE_C*100;  //2 decimals
+            HUMIDITY = HUMIDITY*100;                        //2 decimals
+            PRESSURE = PRESSURE*1000;           //hPa to Pa + 1 decimal
+            
+            
+            //Calcule the direction where the system is pointing relative to North.
+            //I have used a simple empirical method to distinguish between 8 directions. 
+            if (MAGNETIC[0] < 140) WIND_DIRECTION = 0; //North
+            else if (MAGNETIC[0] >= 140 && MAGNETIC[0] < 200 && -MAGNETIC[1] > 250 ) WIND_DIRECTION = 45;  //Northeast
+            else if (MAGNETIC[0] >= 140 && MAGNETIC[0] < 200 && -MAGNETIC[1] < 250 ) WIND_DIRECTION = 315; //Northwest
+            else if (MAGNETIC[0] >= 200 && MAGNETIC[0] < 280 && -MAGNETIC[1] > 250 ) WIND_DIRECTION = 90;  //East
+            else if (MAGNETIC[0] >= 200 && MAGNETIC[0] < 280 && -MAGNETIC[1] < 250 ) WIND_DIRECTION = 270; //Weast
+            else if (MAGNETIC[0] >= 280 && MAGNETIC[0] < 380 && -MAGNETIC[1] > 250 ) WIND_DIRECTION = 135; //Southeast
+            else if (MAGNETIC[0] >= 280 && MAGNETIC[0] < 380 && -MAGNETIC[1] < 250 ) WIND_DIRECTION = 225; //Soutwest           
+            else if (MAGNETIC[0] >= 380) WIND_DIRECTION = 180; //South
+            
+            WIND_DIRECTION *=100;                             //2 decimals
+            
+            _air.updateTemperature((uint16_t)TEMPERATURE_C);   
+            _air.updateHumidity((uint16_t)HUMIDITY);
+            _wind.updateWinddirection((uint16_t)WIND_DIRECTION);
+            _wind.updatePressure((uint16_t)PRESSURE);
+            
+            
+            TEMPERATURE_F = (TEMPERATURE_C * 1.8f) + 32.0f; //Convert the temperature from Celsius to Fahrenheit
+            TEMPERATURE_K = (TEMPERATURE_C + 273.15f);          //Convert the temperature from Celsius to Kelvin
+            pc.printf("Temperature:\t %.2f C / %.2f F / %.2f K\r\n", TEMPERATURE_C, TEMPERATURE_F, TEMPERATURE_K);
+            pc.printf("Humidity:\t %.2f%%\r\n", HUMIDITY);
+            pc.printf("Pressure:\t %.2f hPa\r\n", PRESSURE); 
+            pc.printf("\r\n");
+    
         }
     }
 
@@ -186,17 +223,16 @@
 
     bool _connected;
 
-    UUID * _uuid_list;
-
     uint8_t _hr_counter;
-    HeartRateService _hr_service;
+    //HeartRateService _hr_service;
     
     uint8_t _battery_level;
     BatteryService _bt_service;
     
-    ESS _air;
-    ESS2 _wind;
-
+    EnvironmentalSensingService _air;
+    EnvironmentalSensingService2 _wind;
+    
+    UUID * uuid16_list;
     
     DeviceInformationService _deviceInfo;    
 
@@ -212,9 +248,16 @@
 int main()
 {
     static XNucleoIKS01A2 *Sensors = XNucleoIKS01A2::instance(D14, D15, D4, D5);
+    hum_temp->enable();
+    press_temp->enable();
+    magnetometer->enable();
+    accelerometer->enable();
+    acc_gyro->enable_x();
+    acc_gyro->enable_g();
+    
     BLE &ble = BLE::Instance();
     ble.onEventsToProcess(schedule_ble_events);
-
+    
     HeartrateDemo demo(ble, event_queue);
     demo.start();