3rd revision of my mbedOs ble weight program

Files at this revision

API Documentation at this revision

Comitter:
Eugene0469
Date:
Mon Jun 24 07:02:50 2019 +0000
Parent:
78:08c7eae77646
Commit message:
Customize LED Service to work as the weight service;

Changed in this revision

source/LEDService.h Show annotated file Show diff for this revision Revisions of this file
source/hx711.cpp Show annotated file Show diff for this revision Revisions of this file
source/hx711.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
diff -r 08c7eae77646 -r 8ea0d8374ab6 source/LEDService.h
--- a/source/LEDService.h	Wed Feb 27 13:02:06 2019 +0000
+++ b/source/LEDService.h	Mon Jun 24 07:02:50 2019 +0000
@@ -17,12 +17,17 @@
 #ifndef __BLE_LED_SERVICE_H__
 #define __BLE_LED_SERVICE_H__
 
+typedef struct  {
+        uint16_t integer;
+        uint8_t decimal;
+    }WeightType_t;
+    
 class LEDService {
 public:
     const static uint16_t LED_SERVICE_UUID              = 0xA000;
     const static uint16_t LED_STATE_CHARACTERISTIC_UUID = 0xA001;
 
-    LEDService(BLEDevice &_ble, bool initialValueForLEDCharacteristic) :
+    LEDService(BLEDevice &_ble, WeightType_t initialValueForLEDCharacteristic) :
         ble(_ble), ledState(LED_STATE_CHARACTERISTIC_UUID, &initialValueForLEDCharacteristic)
     {
         GattCharacteristic *charTable[] = {&ledState};
@@ -31,14 +36,26 @@
         ble.gattServer().addService(ledService);
     }
 
-    GattAttribute::Handle_t getValueHandle() const
+//    GattAttribute::Handle_t getValueHandle() const
+//    {
+//        return ledState.getValueHandle();
+//    }
+    void updateWeight(float newValue)
     {
-        return ledState.getValueHandle();
+//        float newWeight = (float) newValue;
+//        ble.gattServer().write(ledState.getValueHandle(), 
+//        (uint8_t *)&newWeight, sizeof(float));
+        WeightType_t *weight;
+        weight->integer = (uint16_t)newValue;
+        float f_decimal = newValue - weight->integer;
+        weight->decimal = (uint8_t)(f_decimal* 100.0f);
+        ble.gattServer().write(ledState.getValueHandle(), 
+        (uint8_t *)weight, sizeof(WeightType_t));
     }
-
+    
 private:
     BLEDevice                         &ble;
-    ReadWriteGattCharacteristic<bool> ledState;
+    ReadOnlyGattCharacteristic<WeightType_t> ledState;
 };
 
 #endif /* #ifndef __BLE_LED_SERVICE_H__ */
diff -r 08c7eae77646 -r 8ea0d8374ab6 source/hx711.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/source/hx711.cpp	Mon Jun 24 07:02:50 2019 +0000
@@ -0,0 +1,157 @@
+/*
+ * FILE: HX711.cpp
+ * 
+ * VERSION: 0.1
+ * PURPOSE: HX711 weight library for Nucleo STM32
+ * AUTHOR: Bertrand Bouvier
+ * LICENSE: GPL v3 (http://www.gnu.org/licenses/gpl.html)
+ *
+ * DATASHEET: http://www.dfrobot.com/image/data/SEN0160/hx711_english.pdf
+ * URL: 
+ *
+ * HISTORY:
+ * 24/05/2015 - Bertrand Bouvier - Original version
+ * see HX711.h
+ *
+ * SPECIAL THANKS:
+ *  Inspiré du travail de Weihong Guan (@aguegu)
+ *  https://github.com/aguegu/Arduino
+ *  http://aguegu.net
+ *  
+ *  Inspiré du travail de bodge
+ *  https://github.com/bogde/HX711
+ *
+ */
+ 
+#include "hx711.h"
+#include "mbed.h"
+ 
+#define SCALE_VALUE 259.79 // multiple specific to each hardware
+ 
+HX711::HX711(PinName pinData, PinName pinSck, uint8_t gain)
+{
+    _pinData = pinData;
+    _pinSck = pinSck;
+    
+    this->setGain(gain);
+    
+    DigitalOut sck(_pinSck);
+    DigitalIn data(_pinData);
+    
+    sck = 1;        //Initialisation HX711
+    wait_us(100);
+    sck = 0;
+    
+    this->setOffset(averageValue(10)); //TARE from the balance
+    this->setScale(SCALE_VALUE);        //Setting the value of SCALE
+}
+ 
+HX711::~HX711()
+{
+ 
+}
+ 
+int HX711::averageValue(uint8_t times) //Calculates an average over several measures
+{
+    int sum = 0;
+    for (int i = 0; i < times; i++)
+    {
+        sum += getValue();
+    }
+ 
+    return sum / times;
+}
+ 
+int HX711::getValue() //Get the raw value of the controller
+{
+    int buffer; 
+    buffer = 0 ;
+    
+    DigitalOut sck(_pinSck);
+    DigitalIn data(_pinData);
+    
+    while (data.read()==1) //wait is ready
+    ;
+    
+    for (uint8_t i = 24; i--;) //read 24 bit 1 per 1 and save to buffer
+    {
+        sck=1;
+ 
+        buffer = buffer << 1 ;
+    
+        if (data.read())
+        {
+            buffer ++;
+        }
+        
+        sck=0;
+    }
+    
+    for (int i = 0; i < _gain; i++)
+    {
+        sck=1; 
+        sck=0;
+    }
+    
+    buffer = buffer ^ 0x800000;
+ 
+    return buffer;
+}
+ 
+void HX711::setOffset(int offset)
+{
+    _offset = offset;
+}
+ 
+void HX711::setScale(float scale)
+{
+    _scale = scale;
+}
+ 
+float HX711::getGram()
+{
+    long val = (averageValue(2) - _offset);
+    return (float) val / _scale;
+}
+ 
+void HX711::setGain(uint8_t  gain) 
+{
+    switch (gain) 
+    { 
+        case 128:       // channel A, gain factor 128 
+            _gain = 1; 
+            break; 
+            
+        case 64:        // channel A, gain factor 64 
+            _gain = 3; 
+            break; 
+            
+        case 32:        // channel B, gain factor 32 
+            _gain = 2; 
+            break; 
+    } 
+    DigitalOut sck(_pinSck);
+    sck = 0;
+    getValue();
+}
+ 
+void HX711::powerDown() 
+{
+    DigitalOut sck(_pinSck);
+    sck = 0;
+    sck = 1;
+}
+ 
+void HX711::powerUp() 
+{
+    DigitalOut sck(_pinSck);
+    sck = 0;
+}
+ 
+void HX711::tare(uint8_t times) 
+{
+    int sum = averageValue(times);
+    setOffset(sum);
+}
+  
+ 
\ No newline at end of file
diff -r 08c7eae77646 -r 8ea0d8374ab6 source/hx711.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/source/hx711.h	Mon Jun 24 07:02:50 2019 +0000
@@ -0,0 +1,58 @@
+/*
+ * FILE: HX711.h
+ * 
+ * VERSION: 0.1
+ * PURPOSE: HX711 weight library for Nucleo STM32
+ * AUTHOR: Bertrand Bouvier
+ * LICENSE: GPL v3 (http://www.gnu.org/licenses/gpl.html)
+ *
+ * DATASHEET: http://www.dfrobot.com/image/data/SEN0160/hx711_english.pdf
+ * URL: 
+ *
+ * HISTORY:
+ * 24/05/2015 - Bertrand Bouvier - Original version
+ * see HX711.cpp
+ *
+ * SPECIAL THANKS:
+ *  Inspiré du travail de Weihong Guan (@aguegu)
+ *  https://github.com/aguegu/Arduino
+ *  http://aguegu.net
+ *  
+ *  Inspiré du travail de bodge
+ *  https://github.com/bogde/HX711
+ *
+ */
+ 
+#ifndef HX711_H
+#define HX711_H
+ 
+#include "mbed.h"
+ 
+class HX711
+{
+ 
+public:
+    HX711(PinName pinData, PinName pinSck,uint8_t gain = 128);
+    ~HX711();
+    int getValue(void);
+    int averageValue(uint8_t times);
+    void setOffset(int offset);
+    void setScale(float scale);
+    float getGram();
+    void setGain(uint8_t gain);
+    void powerDown();
+    void powerUp();
+    void tare(uint8_t times = 10);
+ 
+ 
+private:
+    PinName _pinData;
+    PinName _pinSck;
+    int _offset;
+    float _scale;
+    uint8_t _gain; //[128|32|64]
+    
+ 
+};
+ 
+#endif 
\ No newline at end of file
diff -r 08c7eae77646 -r 8ea0d8374ab6 source/main.cpp
--- a/source/main.cpp	Wed Feb 27 13:02:06 2019 +0000
+++ b/source/main.cpp	Mon Jun 24 07:02:50 2019 +0000
@@ -18,8 +18,14 @@
 #include <mbed.h>
 #include "ble/BLE.h"
 #include "LEDService.h"
+#include "hx711.h"
 #include "pretty_printer.h"
 
+HX711 scale(D5, D4);
+float calibration_factor = 100;
+
+Serial pc(USBTX, USBRX);    // USB Serial Terminal
+
 const static char DEVICE_NAME[] = "LED";
 
 static EventQueue event_queue(/* event count */ 10 * EVENTS_EVENT_SIZE);
@@ -30,7 +36,7 @@
         _ble(ble),
         _event_queue(event_queue),
         _alive_led(LED1, 1),
-        _actuated_led(LED2, 0),
+//        _actuated_led(LED2, 0),
         _led_uuid(LEDService::LED_SERVICE_UUID),
         _led_service(NULL),
         _adv_data_builder(_adv_buffer) { }
@@ -57,9 +63,9 @@
             return;
         }
 
-        _led_service = new LEDService(_ble, false);
+        _led_service = new LEDService(_ble, _initial_weight);
 
-        _ble.gattServer().onDataWritten(this, &LEDDemo::on_data_written);
+//        _ble.gattServer().onDataWritten(this, &LEDDemo::on_data_written);
 
         print_mac_address();
 
@@ -115,13 +121,30 @@
      *
      * @param[in] params Information about the characterisitc being updated.
      */
-    void on_data_written(const GattWriteCallbackParams *params) {
-        if ((params->handle == _led_service->getValueHandle()) && (params->len == 1)) {
-            _actuated_led = *(params->data);
-        }
-    }
+//    void on_data_written(const GattWriteCallbackParams *params) {
+//        if ((params->handle == _led_service->getValueHandle()) && (params->len == 1)) {
+//            _actuated_led = *(params->data);
+//        }
+//    }
 
     void blink() {
+       if (_ble.gap().getState().connected) 
+        {
+                scale.setScale(calibration_factor);
+                _current_weight = scale.getGram();
+                WeightType_t weight;
+                weight.integer = (uint16_t)_current_weight;
+                float f_decimal = _current_weight - weight.integer;
+                weight.decimal = (uint8_t)(f_decimal* 100.0f);
+        //                if(_current_weight <= 0.00f)
+        //                {
+        //                    _current_weight = 0.00;
+        //                }
+                _led_service->updateWeight(_current_weight);
+                pc.printf("Reading: %.2f\n", _current_weight);
+                pc.printf("Reading: %d.%d\n", weight.integer,weight.decimal);
+        
+         }
         _alive_led = !_alive_led;
     }
 
@@ -136,7 +159,11 @@
     BLE &_ble;
     events::EventQueue &_event_queue;
     DigitalOut _alive_led;
-    DigitalOut _actuated_led;
+//    DigitalOut _actuated_led;
+    float _current_weight;
+    WeightType_t _initial_weight;
+//    _initial_weight.integer = 0;
+//    _initial_weight.decimal = 0;
 
     UUID _led_uuid;
     LEDService *_led_service;