test code

Dependencies:   HX711_weight

Committer:
Eugene0469
Date:
Mon Jun 24 03:57:20 2019 +0000
Revision:
80:167f8aabffca
Parent:
76:a3df66cb69e6
Child:
81:883a1a95f20b
add a structure to store the weight value.;

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 80:167f8aabffca 25 float calibration_factor = 100;
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 80:167f8aabffca 64 _weight_service = new WEIGHTService(_ble, _initial_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 80:167f8aabffca 71 pc.printf("Ensure that there is no weight on the floor.\n");
Eugene0469 80:167f8aabffca 72
Eugene0469 76:a3df66cb69e6 73
Eugene0469 76:a3df66cb69e6 74 scale.tare();
mbed_official 74:51fde11a771f 75
mbed_official 74:51fde11a771f 76 start_advertising();
mbed_official 2:864ddfb70a9c 77 }
mbed_official 2:864ddfb70a9c 78
mbed_official 74:51fde11a771f 79 void start_advertising() {
mbed_official 74:51fde11a771f 80 /* Create advertising parameters and payload */
mbed_official 74:51fde11a771f 81
mbed_official 74:51fde11a771f 82 ble::AdvertisingParameters adv_parameters(
mbed_official 74:51fde11a771f 83 ble::advertising_type_t::CONNECTABLE_UNDIRECTED,
mbed_official 74:51fde11a771f 84 ble::adv_interval_t(ble::millisecond_t(1000))
mbed_official 74:51fde11a771f 85 );
mbed_official 74:51fde11a771f 86
mbed_official 74:51fde11a771f 87 _adv_data_builder.setFlags();
Eugene0469 76:a3df66cb69e6 88 _adv_data_builder.setLocalServiceList(mbed::make_Span(&_weight_uuid, 1));
mbed_official 74:51fde11a771f 89 _adv_data_builder.setName(DEVICE_NAME);
mbed_official 74:51fde11a771f 90
mbed_official 74:51fde11a771f 91 /* Setup advertising */
mbed_official 74:51fde11a771f 92
mbed_official 74:51fde11a771f 93 ble_error_t error = _ble.gap().setAdvertisingParameters(
mbed_official 74:51fde11a771f 94 ble::LEGACY_ADVERTISING_HANDLE,
mbed_official 74:51fde11a771f 95 adv_parameters
mbed_official 74:51fde11a771f 96 );
mbed_official 74:51fde11a771f 97
mbed_official 74:51fde11a771f 98 if (error) {
mbed_official 74:51fde11a771f 99 printf("_ble.gap().setAdvertisingParameters() failed\r\n");
mbed_official 74:51fde11a771f 100 return;
mbed_official 74:51fde11a771f 101 }
mbed_official 74:51fde11a771f 102
mbed_official 74:51fde11a771f 103 error = _ble.gap().setAdvertisingPayload(
mbed_official 74:51fde11a771f 104 ble::LEGACY_ADVERTISING_HANDLE,
mbed_official 74:51fde11a771f 105 _adv_data_builder.getAdvertisingData()
mbed_official 74:51fde11a771f 106 );
mbed_official 74:51fde11a771f 107
mbed_official 74:51fde11a771f 108 if (error) {
mbed_official 74:51fde11a771f 109 printf("_ble.gap().setAdvertisingPayload() failed\r\n");
mbed_official 74:51fde11a771f 110 return;
mbed_official 74:51fde11a771f 111 }
mbed_official 74:51fde11a771f 112
mbed_official 74:51fde11a771f 113 /* Start advertising */
mbed_official 74:51fde11a771f 114
mbed_official 74:51fde11a771f 115 error = _ble.gap().startAdvertising(ble::LEGACY_ADVERTISING_HANDLE);
mbed_official 74:51fde11a771f 116
mbed_official 74:51fde11a771f 117 if (error) {
mbed_official 74:51fde11a771f 118 printf("_ble.gap().startAdvertising() failed\r\n");
mbed_official 74:51fde11a771f 119 return;
mbed_official 74:51fde11a771f 120 }
mbed_official 2:864ddfb70a9c 121 }
mbed_official 2:864ddfb70a9c 122
Eugene0469 76:a3df66cb69e6 123 void update_sensor_value()
Eugene0469 76:a3df66cb69e6 124 {
Eugene0469 76:a3df66cb69e6 125 if (_ble.gap().getState().connected)
Eugene0469 76:a3df66cb69e6 126 {
Eugene0469 76:a3df66cb69e6 127 scale.setScale(calibration_factor);
Eugene0469 76:a3df66cb69e6 128 _current_weight = scale.getGram();
Eugene0469 80:167f8aabffca 129 WeightType_t *weight;
Eugene0469 80:167f8aabffca 130 weight->integer = (uint16_t)_current_weight;
Eugene0469 80:167f8aabffca 131 float f_decimal = _current_weight - weight->integer;
Eugene0469 80:167f8aabffca 132 weight->decimal = (uint8_t)(f_decimal* 100.0f);
Eugene0469 80:167f8aabffca 133 // if(_current_weight <= 0.00f)
Eugene0469 76:a3df66cb69e6 134 // {
Eugene0469 76:a3df66cb69e6 135 // _current_weight = 0.00;
Eugene0469 76:a3df66cb69e6 136 // }
Eugene0469 76:a3df66cb69e6 137 _weight_service->updateWeight(_current_weight);
Eugene0469 76:a3df66cb69e6 138 pc.printf("Reading: %.2f\n", _current_weight);
Eugene0469 80:167f8aabffca 139 pc.printf("Reading: %d.%d\n", weight->integer,weight->decimal);
Eugene0469 80:167f8aabffca 140
Eugene0469 76:a3df66cb69e6 141 }
mbed_official 74:51fde11a771f 142 }
mbed_official 74:51fde11a771f 143
mbed_official 74:51fde11a771f 144 private:
mbed_official 74:51fde11a771f 145 /* Event handler */
mbed_official 2:864ddfb70a9c 146
mbed_official 74:51fde11a771f 147 void onDisconnectionComplete(const ble::DisconnectionCompleteEvent&) {
mbed_official 74:51fde11a771f 148 _ble.gap().startAdvertising(ble::LEGACY_ADVERTISING_HANDLE);
mbed_official 74:51fde11a771f 149 }
mbed_official 74:51fde11a771f 150
mbed_official 74:51fde11a771f 151 private:
mbed_official 74:51fde11a771f 152 BLE &_ble;
mbed_official 74:51fde11a771f 153 events::EventQueue &_event_queue;
mbed_official 44:df8adb3bc797 154
Eugene0469 76:a3df66cb69e6 155 UUID _weight_uuid;
Eugene0469 76:a3df66cb69e6 156
Eugene0469 76:a3df66cb69e6 157 float _current_weight;
Eugene0469 80:167f8aabffca 158 WeightType_t _initial_weight;
Eugene0469 76:a3df66cb69e6 159 WEIGHTService *_weight_service;
mbed_official 2:864ddfb70a9c 160
mbed_official 74:51fde11a771f 161 uint8_t _adv_buffer[ble::LEGACY_ADVERTISING_MAX_SIZE];
mbed_official 74:51fde11a771f 162 ble::AdvertisingDataBuilder _adv_data_builder;
mbed_official 74:51fde11a771f 163 };
mbed_official 74:51fde11a771f 164
mbed_official 74:51fde11a771f 165 /** Schedule processing of events from the BLE middleware in the event queue. */
mbed_official 74:51fde11a771f 166 void schedule_ble_events(BLE::OnEventsToProcessCallbackContext *context) {
mbed_official 74:51fde11a771f 167 event_queue.call(Callback<void()>(&context->ble, &BLE::processEvents));
mbed_official 2:864ddfb70a9c 168 }
mbed_official 2:864ddfb70a9c 169
mbed_official 2:864ddfb70a9c 170 int main()
mbed_official 2:864ddfb70a9c 171 {
Eugene0469 76:a3df66cb69e6 172 //scale.setScale(calibration_factor); //This value is obtained by using the SparkFun_HX711_Calibration sketch
Eugene0469 76:a3df66cb69e6 173 //scale.tare();
mbed_official 74:51fde11a771f 174 BLE &ble = BLE::Instance();
mbed_official 74:51fde11a771f 175 ble.onEventsToProcess(schedule_ble_events);
mbed_official 2:864ddfb70a9c 176
Eugene0469 76:a3df66cb69e6 177 WEIGHTDemo demo(ble, event_queue);
mbed_official 74:51fde11a771f 178 demo.start();
mbed_official 2:864ddfb70a9c 179
mbed_official 2:864ddfb70a9c 180 return 0;
mbed_official 2:864ddfb70a9c 181 }
mbed_official 74:51fde11a771f 182