Eugene Mwangi
/
mbed-os-example-ble-WEIGHT-public
test code
Diff: source/main.cpp
- Revision:
- 76:a3df66cb69e6
- Parent:
- 74:51fde11a771f
- Child:
- 80:167f8aabffca
--- a/source/main.cpp Mon Jan 14 11:00:46 2019 +0000 +++ b/source/main.cpp Mon Feb 11 15:05:13 2019 +0000 @@ -17,34 +17,38 @@ #include <events/mbed_events.h> #include <mbed.h> #include "ble/BLE.h" -#include "LEDService.h" +#include "WEIGHTService.h" #include "pretty_printer.h" +#include "hx711.h" -const static char DEVICE_NAME[] = "LED"; +HX711 scale(D5, D4); +float calibration_factor = 20; -static EventQueue event_queue(/* event count */ 10 * EVENTS_EVENT_SIZE); +Serial pc(USBTX, USBRX); // USB Serial Terminal + +const static char DEVICE_NAME[] = "WEIGHT"; -class LEDDemo : ble::Gap::EventHandler { +static EventQueue event_queue(/* event count */ 16 * EVENTS_EVENT_SIZE); + +class WEIGHTDemo : ble::Gap::EventHandler { public: - LEDDemo(BLE &ble, events::EventQueue &event_queue) : + WEIGHTDemo(BLE &ble, events::EventQueue &event_queue) : _ble(ble), _event_queue(event_queue), - _alive_led(LED1, 1), - _actuated_led(LED2, 0), - _led_uuid(LEDService::LED_SERVICE_UUID), - _led_service(NULL), + _weight_uuid(WEIGHTService::WEIGHT_SERVICE_UUID), + _weight_service(NULL), _adv_data_builder(_adv_buffer) { } - ~LEDDemo() { - delete _led_service; - } +// ~WEIGHTDemo() { +// delete _weight_service; +// } void start() { _ble.gap().setEventHandler(this); - _ble.init(this, &LEDDemo::on_init_complete); + _ble.init(this, &WEIGHTDemo::on_init_complete); - _event_queue.call_every(500, this, &LEDDemo::blink); + _event_queue.call_every(1000, this, &WEIGHTDemo::update_sensor_value); _event_queue.dispatch_forever(); } @@ -57,11 +61,15 @@ return; } - _led_service = new LEDService(_ble, false); + _weight_service = new WEIGHTService(_ble, _current_weight); - _ble.gattServer().onDataWritten(this, &LEDDemo::on_data_written); +// _ble.gattServer().onDataWritten(this, &WEIGHTDemo::on_data_written); print_mac_address(); + + pc.printf("HX711 Demo\n"); + + scale.tare(); start_advertising(); } @@ -75,7 +83,7 @@ ); _adv_data_builder.setFlags(); - _adv_data_builder.setLocalServiceList(mbed::make_Span(&_led_uuid, 1)); + _adv_data_builder.setLocalServiceList(mbed::make_Span(&_weight_uuid, 1)); _adv_data_builder.setName(DEVICE_NAME); /* Setup advertising */ @@ -110,19 +118,19 @@ } } - /** - * This callback allows the LEDService to receive updates to the ledState Characteristic. - * - * @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 blink() { - _alive_led = !_alive_led; + void update_sensor_value() + { + if (_ble.gap().getState().connected) + { + scale.setScale(calibration_factor); + _current_weight = scale.getGram(); + //if(_current_weight <= 0.00) +// { +// _current_weight = 0.00; +// } + _weight_service->updateWeight(_current_weight); + pc.printf("Reading: %.2f\n", _current_weight); + } } private: @@ -135,11 +143,11 @@ private: BLE &_ble; events::EventQueue &_event_queue; - DigitalOut _alive_led; - DigitalOut _actuated_led; - UUID _led_uuid; - LEDService *_led_service; + UUID _weight_uuid; + + float _current_weight; + WEIGHTService *_weight_service; uint8_t _adv_buffer[ble::LEGACY_ADVERTISING_MAX_SIZE]; ble::AdvertisingDataBuilder _adv_data_builder; @@ -152,10 +160,12 @@ int main() { + //scale.setScale(calibration_factor); //This value is obtained by using the SparkFun_HX711_Calibration sketch + //scale.tare(); BLE &ble = BLE::Instance(); ble.onEventsToProcess(schedule_ble_events); - LEDDemo demo(ble, event_queue); + WEIGHTDemo demo(ble, event_queue); demo.start(); return 0;