test code

Dependencies:   HX711_weight

Committer:
Eugene0469
Date:
Mon Jun 24 14:17:40 2019 +0000
Revision:
81:883a1a95f20b
Parent:
80:167f8aabffca
Child:
82:d40c223b65f9
add a packed structure.;

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 81:883a1a95f20b 38 // _alive_led(LED1, 1),
Eugene0469 76:a3df66cb69e6 39 _weight_uuid(WEIGHTService::WEIGHT_SERVICE_UUID),
Eugene0469 76:a3df66cb69e6 40 _weight_service(NULL),
mbed_official 74:51fde11a771f 41 _adv_data_builder(_adv_buffer) { }
mbed_official 2:864ddfb70a9c 42
Eugene0469 76:a3df66cb69e6 43 // ~WEIGHTDemo() {
Eugene0469 76:a3df66cb69e6 44 // delete _weight_service;
Eugene0469 76:a3df66cb69e6 45 // }
mbed_official 2:864ddfb70a9c 46
mbed_official 74:51fde11a771f 47 void start() {
mbed_official 74:51fde11a771f 48 _ble.gap().setEventHandler(this);
mbed_official 74:51fde11a771f 49
Eugene0469 76:a3df66cb69e6 50 _ble.init(this, &WEIGHTDemo::on_init_complete);
mbed_official 74:51fde11a771f 51
Eugene0469 76:a3df66cb69e6 52 _event_queue.call_every(1000, this, &WEIGHTDemo::update_sensor_value);
mbed_official 74:51fde11a771f 53
mbed_official 74:51fde11a771f 54 _event_queue.dispatch_forever();
mbed_official 74:51fde11a771f 55 }
mbed_official 2:864ddfb70a9c 56
mbed_official 74:51fde11a771f 57 private:
mbed_official 74:51fde11a771f 58 /** Callback triggered when the ble initialization process has finished */
mbed_official 74:51fde11a771f 59 void on_init_complete(BLE::InitializationCompleteCallbackContext *params) {
mbed_official 74:51fde11a771f 60 if (params->error != BLE_ERROR_NONE) {
mbed_official 74:51fde11a771f 61 printf("Ble initialization failed.");
mbed_official 74:51fde11a771f 62 return;
mbed_official 74:51fde11a771f 63 }
mbed_official 44:df8adb3bc797 64
Eugene0469 80:167f8aabffca 65 _weight_service = new WEIGHTService(_ble, _initial_weight);
mbed_official 74:51fde11a771f 66
Eugene0469 76:a3df66cb69e6 67 // _ble.gattServer().onDataWritten(this, &WEIGHTDemo::on_data_written);
mbed_official 2:864ddfb70a9c 68
mbed_official 74:51fde11a771f 69 print_mac_address();
Eugene0469 76:a3df66cb69e6 70
Eugene0469 76:a3df66cb69e6 71 pc.printf("HX711 Demo\n");
Eugene0469 80:167f8aabffca 72 pc.printf("Ensure that there is no weight on the floor.\n");
Eugene0469 80:167f8aabffca 73
Eugene0469 76:a3df66cb69e6 74
Eugene0469 76:a3df66cb69e6 75 scale.tare();
mbed_official 74:51fde11a771f 76
mbed_official 74:51fde11a771f 77 start_advertising();
mbed_official 2:864ddfb70a9c 78 }
mbed_official 2:864ddfb70a9c 79
mbed_official 74:51fde11a771f 80 void start_advertising() {
mbed_official 74:51fde11a771f 81 /* Create advertising parameters and payload */
mbed_official 74:51fde11a771f 82
mbed_official 74:51fde11a771f 83 ble::AdvertisingParameters adv_parameters(
mbed_official 74:51fde11a771f 84 ble::advertising_type_t::CONNECTABLE_UNDIRECTED,
mbed_official 74:51fde11a771f 85 ble::adv_interval_t(ble::millisecond_t(1000))
mbed_official 74:51fde11a771f 86 );
mbed_official 74:51fde11a771f 87
mbed_official 74:51fde11a771f 88 _adv_data_builder.setFlags();
Eugene0469 76:a3df66cb69e6 89 _adv_data_builder.setLocalServiceList(mbed::make_Span(&_weight_uuid, 1));
mbed_official 74:51fde11a771f 90 _adv_data_builder.setName(DEVICE_NAME);
mbed_official 74:51fde11a771f 91
mbed_official 74:51fde11a771f 92 /* Setup advertising */
mbed_official 74:51fde11a771f 93
mbed_official 74:51fde11a771f 94 ble_error_t error = _ble.gap().setAdvertisingParameters(
mbed_official 74:51fde11a771f 95 ble::LEGACY_ADVERTISING_HANDLE,
mbed_official 74:51fde11a771f 96 adv_parameters
mbed_official 74:51fde11a771f 97 );
mbed_official 74:51fde11a771f 98
mbed_official 74:51fde11a771f 99 if (error) {
mbed_official 74:51fde11a771f 100 printf("_ble.gap().setAdvertisingParameters() failed\r\n");
mbed_official 74:51fde11a771f 101 return;
mbed_official 74:51fde11a771f 102 }
mbed_official 74:51fde11a771f 103
mbed_official 74:51fde11a771f 104 error = _ble.gap().setAdvertisingPayload(
mbed_official 74:51fde11a771f 105 ble::LEGACY_ADVERTISING_HANDLE,
mbed_official 74:51fde11a771f 106 _adv_data_builder.getAdvertisingData()
mbed_official 74:51fde11a771f 107 );
mbed_official 74:51fde11a771f 108
mbed_official 74:51fde11a771f 109 if (error) {
mbed_official 74:51fde11a771f 110 printf("_ble.gap().setAdvertisingPayload() failed\r\n");
mbed_official 74:51fde11a771f 111 return;
mbed_official 74:51fde11a771f 112 }
mbed_official 74:51fde11a771f 113
mbed_official 74:51fde11a771f 114 /* Start advertising */
mbed_official 74:51fde11a771f 115
mbed_official 74:51fde11a771f 116 error = _ble.gap().startAdvertising(ble::LEGACY_ADVERTISING_HANDLE);
mbed_official 74:51fde11a771f 117
mbed_official 74:51fde11a771f 118 if (error) {
mbed_official 74:51fde11a771f 119 printf("_ble.gap().startAdvertising() failed\r\n");
mbed_official 74:51fde11a771f 120 return;
mbed_official 74:51fde11a771f 121 }
mbed_official 2:864ddfb70a9c 122 }
mbed_official 2:864ddfb70a9c 123
Eugene0469 76:a3df66cb69e6 124 void update_sensor_value()
Eugene0469 76:a3df66cb69e6 125 {
Eugene0469 76:a3df66cb69e6 126 if (_ble.gap().getState().connected)
Eugene0469 76:a3df66cb69e6 127 {
Eugene0469 81:883a1a95f20b 128 // _alive_led = !_alive_led;
Eugene0469 76:a3df66cb69e6 129 scale.setScale(calibration_factor);
Eugene0469 81:883a1a95f20b 130 // _current_weight = scale.getGram();
Eugene0469 81:883a1a95f20b 131 _current_weight = 456.87;
Eugene0469 81:883a1a95f20b 132 WeightType_t weight;
Eugene0469 81:883a1a95f20b 133 weight.integer = (int32_t)_current_weight;
Eugene0469 81:883a1a95f20b 134 float f_decimal = _current_weight - weight.integer;
Eugene0469 81:883a1a95f20b 135 weight.decimal = (uint8_t)(f_decimal* 100.0f);
Eugene0469 80:167f8aabffca 136 // if(_current_weight <= 0.00f)
Eugene0469 76:a3df66cb69e6 137 // {
Eugene0469 76:a3df66cb69e6 138 // _current_weight = 0.00;
Eugene0469 76:a3df66cb69e6 139 // }
Eugene0469 76:a3df66cb69e6 140 _weight_service->updateWeight(_current_weight);
Eugene0469 76:a3df66cb69e6 141 pc.printf("Reading: %.2f\n", _current_weight);
Eugene0469 81:883a1a95f20b 142 pc.printf("Reading: %d.%d\n", weight.integer,weight.decimal);
Eugene0469 80:167f8aabffca 143
Eugene0469 76:a3df66cb69e6 144 }
mbed_official 74:51fde11a771f 145 }
mbed_official 74:51fde11a771f 146
mbed_official 74:51fde11a771f 147 private:
mbed_official 74:51fde11a771f 148 /* Event handler */
mbed_official 2:864ddfb70a9c 149
mbed_official 74:51fde11a771f 150 void onDisconnectionComplete(const ble::DisconnectionCompleteEvent&) {
mbed_official 74:51fde11a771f 151 _ble.gap().startAdvertising(ble::LEGACY_ADVERTISING_HANDLE);
mbed_official 74:51fde11a771f 152 }
mbed_official 74:51fde11a771f 153
mbed_official 74:51fde11a771f 154 private:
mbed_official 74:51fde11a771f 155 BLE &_ble;
mbed_official 74:51fde11a771f 156 events::EventQueue &_event_queue;
Eugene0469 81:883a1a95f20b 157 // DigitalOut _alive_led;
Eugene0469 76:a3df66cb69e6 158 UUID _weight_uuid;
Eugene0469 76:a3df66cb69e6 159
Eugene0469 76:a3df66cb69e6 160 float _current_weight;
Eugene0469 80:167f8aabffca 161 WeightType_t _initial_weight;
Eugene0469 76:a3df66cb69e6 162 WEIGHTService *_weight_service;
mbed_official 2:864ddfb70a9c 163
mbed_official 74:51fde11a771f 164 uint8_t _adv_buffer[ble::LEGACY_ADVERTISING_MAX_SIZE];
mbed_official 74:51fde11a771f 165 ble::AdvertisingDataBuilder _adv_data_builder;
mbed_official 74:51fde11a771f 166 };
mbed_official 74:51fde11a771f 167
mbed_official 74:51fde11a771f 168 /** Schedule processing of events from the BLE middleware in the event queue. */
mbed_official 74:51fde11a771f 169 void schedule_ble_events(BLE::OnEventsToProcessCallbackContext *context) {
mbed_official 74:51fde11a771f 170 event_queue.call(Callback<void()>(&context->ble, &BLE::processEvents));
mbed_official 2:864ddfb70a9c 171 }
mbed_official 2:864ddfb70a9c 172
mbed_official 2:864ddfb70a9c 173 int main()
mbed_official 2:864ddfb70a9c 174 {
Eugene0469 76:a3df66cb69e6 175 //scale.setScale(calibration_factor); //This value is obtained by using the SparkFun_HX711_Calibration sketch
Eugene0469 76:a3df66cb69e6 176 //scale.tare();
mbed_official 74:51fde11a771f 177 BLE &ble = BLE::Instance();
mbed_official 74:51fde11a771f 178 ble.onEventsToProcess(schedule_ble_events);
mbed_official 2:864ddfb70a9c 179
Eugene0469 76:a3df66cb69e6 180 WEIGHTDemo demo(ble, event_queue);
mbed_official 74:51fde11a771f 181 demo.start();
mbed_official 2:864ddfb70a9c 182
mbed_official 2:864ddfb70a9c 183 return 0;
mbed_official 2:864ddfb70a9c 184 }
mbed_official 74:51fde11a771f 185