test code

Dependencies:   HX711_weight

Committer:
Eugene0469
Date:
Mon Feb 11 15:05:13 2019 +0000
Revision:
76:a3df66cb69e6
Parent:
74:51fde11a771f
Child:
80:167f8aabffca
any help will be greatly appreciated

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 2:864ddfb70a9c 1 /* mbed Microcontroller Library
mbed_official 2:864ddfb70a9c 2 * Copyright (c) 2006-2013 ARM Limited
mbed_official 2:864ddfb70a9c 3 *
mbed_official 2:864ddfb70a9c 4 * Licensed under the Apache License, Version 2.0 (the "License");
mbed_official 2:864ddfb70a9c 5 * you may not use this file except in compliance with the License.
mbed_official 2:864ddfb70a9c 6 * You may obtain a copy of the License at
mbed_official 2:864ddfb70a9c 7 *
mbed_official 2:864ddfb70a9c 8 * http://www.apache.org/licenses/LICENSE-2.0
mbed_official 2:864ddfb70a9c 9 *
mbed_official 2:864ddfb70a9c 10 * Unless required by applicable law or agreed to in writing, software
mbed_official 2:864ddfb70a9c 11 * distributed under the License is distributed on an "AS IS" BASIS,
mbed_official 2:864ddfb70a9c 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbed_official 2:864ddfb70a9c 13 * See the License for the specific language governing permissions and
mbed_official 2:864ddfb70a9c 14 * limitations under the License.
mbed_official 2:864ddfb70a9c 15 */
mbed_official 2:864ddfb70a9c 16
mbed_official 11:7404978b24e7 17 #include <events/mbed_events.h>
mbed_official 2:864ddfb70a9c 18 #include <mbed.h>
mbed_official 2:864ddfb70a9c 19 #include "ble/BLE.h"
Eugene0469 76:a3df66cb69e6 20 #include "WEIGHTService.h"
mbed_official 74:51fde11a771f 21 #include "pretty_printer.h"
Eugene0469 76:a3df66cb69e6 22 #include "hx711.h"
mbed_official 2:864ddfb70a9c 23
Eugene0469 76:a3df66cb69e6 24 HX711 scale(D5, D4);
Eugene0469 76:a3df66cb69e6 25 float calibration_factor = 20;
mbed_official 2:864ddfb70a9c 26
Eugene0469 76:a3df66cb69e6 27 Serial pc(USBTX, USBRX); // USB Serial Terminal
Eugene0469 76:a3df66cb69e6 28
Eugene0469 76:a3df66cb69e6 29 const static char DEVICE_NAME[] = "WEIGHT";
mbed_official 2:864ddfb70a9c 30
Eugene0469 76:a3df66cb69e6 31 static EventQueue event_queue(/* event count */ 16 * EVENTS_EVENT_SIZE);
Eugene0469 76:a3df66cb69e6 32
Eugene0469 76:a3df66cb69e6 33 class WEIGHTDemo : ble::Gap::EventHandler {
mbed_official 74:51fde11a771f 34 public:
Eugene0469 76:a3df66cb69e6 35 WEIGHTDemo(BLE &ble, events::EventQueue &event_queue) :
mbed_official 74:51fde11a771f 36 _ble(ble),
mbed_official 74:51fde11a771f 37 _event_queue(event_queue),
Eugene0469 76:a3df66cb69e6 38 _weight_uuid(WEIGHTService::WEIGHT_SERVICE_UUID),
Eugene0469 76:a3df66cb69e6 39 _weight_service(NULL),
mbed_official 74:51fde11a771f 40 _adv_data_builder(_adv_buffer) { }
mbed_official 2:864ddfb70a9c 41
Eugene0469 76:a3df66cb69e6 42 // ~WEIGHTDemo() {
Eugene0469 76:a3df66cb69e6 43 // delete _weight_service;
Eugene0469 76:a3df66cb69e6 44 // }
mbed_official 2:864ddfb70a9c 45
mbed_official 74:51fde11a771f 46 void start() {
mbed_official 74:51fde11a771f 47 _ble.gap().setEventHandler(this);
mbed_official 74:51fde11a771f 48
Eugene0469 76:a3df66cb69e6 49 _ble.init(this, &WEIGHTDemo::on_init_complete);
mbed_official 74:51fde11a771f 50
Eugene0469 76:a3df66cb69e6 51 _event_queue.call_every(1000, this, &WEIGHTDemo::update_sensor_value);
mbed_official 74:51fde11a771f 52
mbed_official 74:51fde11a771f 53 _event_queue.dispatch_forever();
mbed_official 74:51fde11a771f 54 }
mbed_official 2:864ddfb70a9c 55
mbed_official 74:51fde11a771f 56 private:
mbed_official 74:51fde11a771f 57 /** Callback triggered when the ble initialization process has finished */
mbed_official 74:51fde11a771f 58 void on_init_complete(BLE::InitializationCompleteCallbackContext *params) {
mbed_official 74:51fde11a771f 59 if (params->error != BLE_ERROR_NONE) {
mbed_official 74:51fde11a771f 60 printf("Ble initialization failed.");
mbed_official 74:51fde11a771f 61 return;
mbed_official 74:51fde11a771f 62 }
mbed_official 44:df8adb3bc797 63
Eugene0469 76:a3df66cb69e6 64 _weight_service = new WEIGHTService(_ble, _current_weight);
mbed_official 74:51fde11a771f 65
Eugene0469 76:a3df66cb69e6 66 // _ble.gattServer().onDataWritten(this, &WEIGHTDemo::on_data_written);
mbed_official 2:864ddfb70a9c 67
mbed_official 74:51fde11a771f 68 print_mac_address();
Eugene0469 76:a3df66cb69e6 69
Eugene0469 76:a3df66cb69e6 70 pc.printf("HX711 Demo\n");
Eugene0469 76:a3df66cb69e6 71
Eugene0469 76:a3df66cb69e6 72 scale.tare();
mbed_official 74:51fde11a771f 73
mbed_official 74:51fde11a771f 74 start_advertising();
mbed_official 2:864ddfb70a9c 75 }
mbed_official 2:864ddfb70a9c 76
mbed_official 74:51fde11a771f 77 void start_advertising() {
mbed_official 74:51fde11a771f 78 /* Create advertising parameters and payload */
mbed_official 74:51fde11a771f 79
mbed_official 74:51fde11a771f 80 ble::AdvertisingParameters adv_parameters(
mbed_official 74:51fde11a771f 81 ble::advertising_type_t::CONNECTABLE_UNDIRECTED,
mbed_official 74:51fde11a771f 82 ble::adv_interval_t(ble::millisecond_t(1000))
mbed_official 74:51fde11a771f 83 );
mbed_official 74:51fde11a771f 84
mbed_official 74:51fde11a771f 85 _adv_data_builder.setFlags();
Eugene0469 76:a3df66cb69e6 86 _adv_data_builder.setLocalServiceList(mbed::make_Span(&_weight_uuid, 1));
mbed_official 74:51fde11a771f 87 _adv_data_builder.setName(DEVICE_NAME);
mbed_official 74:51fde11a771f 88
mbed_official 74:51fde11a771f 89 /* Setup advertising */
mbed_official 74:51fde11a771f 90
mbed_official 74:51fde11a771f 91 ble_error_t error = _ble.gap().setAdvertisingParameters(
mbed_official 74:51fde11a771f 92 ble::LEGACY_ADVERTISING_HANDLE,
mbed_official 74:51fde11a771f 93 adv_parameters
mbed_official 74:51fde11a771f 94 );
mbed_official 74:51fde11a771f 95
mbed_official 74:51fde11a771f 96 if (error) {
mbed_official 74:51fde11a771f 97 printf("_ble.gap().setAdvertisingParameters() failed\r\n");
mbed_official 74:51fde11a771f 98 return;
mbed_official 74:51fde11a771f 99 }
mbed_official 74:51fde11a771f 100
mbed_official 74:51fde11a771f 101 error = _ble.gap().setAdvertisingPayload(
mbed_official 74:51fde11a771f 102 ble::LEGACY_ADVERTISING_HANDLE,
mbed_official 74:51fde11a771f 103 _adv_data_builder.getAdvertisingData()
mbed_official 74:51fde11a771f 104 );
mbed_official 74:51fde11a771f 105
mbed_official 74:51fde11a771f 106 if (error) {
mbed_official 74:51fde11a771f 107 printf("_ble.gap().setAdvertisingPayload() failed\r\n");
mbed_official 74:51fde11a771f 108 return;
mbed_official 74:51fde11a771f 109 }
mbed_official 74:51fde11a771f 110
mbed_official 74:51fde11a771f 111 /* Start advertising */
mbed_official 74:51fde11a771f 112
mbed_official 74:51fde11a771f 113 error = _ble.gap().startAdvertising(ble::LEGACY_ADVERTISING_HANDLE);
mbed_official 74:51fde11a771f 114
mbed_official 74:51fde11a771f 115 if (error) {
mbed_official 74:51fde11a771f 116 printf("_ble.gap().startAdvertising() failed\r\n");
mbed_official 74:51fde11a771f 117 return;
mbed_official 74:51fde11a771f 118 }
mbed_official 2:864ddfb70a9c 119 }
mbed_official 2:864ddfb70a9c 120
Eugene0469 76:a3df66cb69e6 121 void update_sensor_value()
Eugene0469 76:a3df66cb69e6 122 {
Eugene0469 76:a3df66cb69e6 123 if (_ble.gap().getState().connected)
Eugene0469 76:a3df66cb69e6 124 {
Eugene0469 76:a3df66cb69e6 125 scale.setScale(calibration_factor);
Eugene0469 76:a3df66cb69e6 126 _current_weight = scale.getGram();
Eugene0469 76:a3df66cb69e6 127 //if(_current_weight <= 0.00)
Eugene0469 76:a3df66cb69e6 128 // {
Eugene0469 76:a3df66cb69e6 129 // _current_weight = 0.00;
Eugene0469 76:a3df66cb69e6 130 // }
Eugene0469 76:a3df66cb69e6 131 _weight_service->updateWeight(_current_weight);
Eugene0469 76:a3df66cb69e6 132 pc.printf("Reading: %.2f\n", _current_weight);
Eugene0469 76:a3df66cb69e6 133 }
mbed_official 74:51fde11a771f 134 }
mbed_official 74:51fde11a771f 135
mbed_official 74:51fde11a771f 136 private:
mbed_official 74:51fde11a771f 137 /* Event handler */
mbed_official 2:864ddfb70a9c 138
mbed_official 74:51fde11a771f 139 void onDisconnectionComplete(const ble::DisconnectionCompleteEvent&) {
mbed_official 74:51fde11a771f 140 _ble.gap().startAdvertising(ble::LEGACY_ADVERTISING_HANDLE);
mbed_official 74:51fde11a771f 141 }
mbed_official 74:51fde11a771f 142
mbed_official 74:51fde11a771f 143 private:
mbed_official 74:51fde11a771f 144 BLE &_ble;
mbed_official 74:51fde11a771f 145 events::EventQueue &_event_queue;
mbed_official 44:df8adb3bc797 146
Eugene0469 76:a3df66cb69e6 147 UUID _weight_uuid;
Eugene0469 76:a3df66cb69e6 148
Eugene0469 76:a3df66cb69e6 149 float _current_weight;
Eugene0469 76:a3df66cb69e6 150 WEIGHTService *_weight_service;
mbed_official 2:864ddfb70a9c 151
mbed_official 74:51fde11a771f 152 uint8_t _adv_buffer[ble::LEGACY_ADVERTISING_MAX_SIZE];
mbed_official 74:51fde11a771f 153 ble::AdvertisingDataBuilder _adv_data_builder;
mbed_official 74:51fde11a771f 154 };
mbed_official 74:51fde11a771f 155
mbed_official 74:51fde11a771f 156 /** Schedule processing of events from the BLE middleware in the event queue. */
mbed_official 74:51fde11a771f 157 void schedule_ble_events(BLE::OnEventsToProcessCallbackContext *context) {
mbed_official 74:51fde11a771f 158 event_queue.call(Callback<void()>(&context->ble, &BLE::processEvents));
mbed_official 2:864ddfb70a9c 159 }
mbed_official 2:864ddfb70a9c 160
mbed_official 2:864ddfb70a9c 161 int main()
mbed_official 2:864ddfb70a9c 162 {
Eugene0469 76:a3df66cb69e6 163 //scale.setScale(calibration_factor); //This value is obtained by using the SparkFun_HX711_Calibration sketch
Eugene0469 76:a3df66cb69e6 164 //scale.tare();
mbed_official 74:51fde11a771f 165 BLE &ble = BLE::Instance();
mbed_official 74:51fde11a771f 166 ble.onEventsToProcess(schedule_ble_events);
mbed_official 2:864ddfb70a9c 167
Eugene0469 76:a3df66cb69e6 168 WEIGHTDemo demo(ble, event_queue);
mbed_official 74:51fde11a771f 169 demo.start();
mbed_official 2:864ddfb70a9c 170
mbed_official 2:864ddfb70a9c 171 return 0;
mbed_official 2:864ddfb70a9c 172 }
mbed_official 74:51fde11a771f 173