Eugene Mwangi
/
mbed-os-example-ble-WEIGHT-public
test code
source/main.cpp@83:dc516d289cc0, 2019-06-26 (annotated)
- Committer:
- Eugene0469
- Date:
- Wed Jun 26 03:34:13 2019 +0000
- Revision:
- 83:dc516d289cc0
- Parent:
- 82:d40c223b65f9
Modify the hx711 library.
Who changed what in which revision?
User | Revision | Line number | New 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 | 83:dc516d289cc0 | 22 | #include "Hx711.h" |
mbed_official | 2:864ddfb70a9c | 23 | |
Eugene0469 | 83:dc516d289cc0 | 24 | Hx711 scale(D4, D5); //D4 is connected to clock. D5 is conected to data. |
Eugene0469 | 83:dc516d289cc0 | 25 | float calibration_factor = -1.0f; |
mbed_official | 2:864ddfb70a9c | 26 | |
Eugene0469 | 76:a3df66cb69e6 | 27 | Serial pc(USBTX, USBRX); // USB Serial Terminal |
Eugene0469 | 76:a3df66cb69e6 | 28 | |
Eugene0469 | 82:d40c223b65f9 | 29 | const static char DEVICE_NAME[] = "TH2G_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 | 83:dc516d289cc0 | 38 | _alive_led(LED1, 0), |
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 | } |
Eugene0469 | 82:d40c223b65f9 | 64 | |
Eugene0469 | 82:d40c223b65f9 | 65 | initialWeightReading(&_initial_weight); |
mbed_official | 44:df8adb3bc797 | 66 | |
Eugene0469 | 80:167f8aabffca | 67 | _weight_service = new WEIGHTService(_ble, _initial_weight); |
mbed_official | 74:51fde11a771f | 68 | |
Eugene0469 | 76:a3df66cb69e6 | 69 | // _ble.gattServer().onDataWritten(this, &WEIGHTDemo::on_data_written); |
mbed_official | 2:864ddfb70a9c | 70 | |
mbed_official | 74:51fde11a771f | 71 | print_mac_address(); |
Eugene0469 | 76:a3df66cb69e6 | 72 | |
Eugene0469 | 76:a3df66cb69e6 | 73 | pc.printf("HX711 Demo\n"); |
Eugene0469 | 80:167f8aabffca | 74 | pc.printf("Ensure that there is no weight on the floor.\n"); |
Eugene0469 | 80:167f8aabffca | 75 | |
Eugene0469 | 76:a3df66cb69e6 | 76 | |
Eugene0469 | 83:dc516d289cc0 | 77 | int offset = (int)scale.read(); |
Eugene0469 | 83:dc516d289cc0 | 78 | scale.set_offset(offset); |
Eugene0469 | 83:dc516d289cc0 | 79 | printf("Reading: %d \n",offset); |
Eugene0469 | 83:dc516d289cc0 | 80 | scale.set_scale(calibration_factor); |
mbed_official | 74:51fde11a771f | 81 | |
mbed_official | 74:51fde11a771f | 82 | start_advertising(); |
mbed_official | 2:864ddfb70a9c | 83 | } |
mbed_official | 2:864ddfb70a9c | 84 | |
mbed_official | 74:51fde11a771f | 85 | void start_advertising() { |
mbed_official | 74:51fde11a771f | 86 | /* Create advertising parameters and payload */ |
mbed_official | 74:51fde11a771f | 87 | |
mbed_official | 74:51fde11a771f | 88 | ble::AdvertisingParameters adv_parameters( |
mbed_official | 74:51fde11a771f | 89 | ble::advertising_type_t::CONNECTABLE_UNDIRECTED, |
mbed_official | 74:51fde11a771f | 90 | ble::adv_interval_t(ble::millisecond_t(1000)) |
mbed_official | 74:51fde11a771f | 91 | ); |
mbed_official | 74:51fde11a771f | 92 | |
mbed_official | 74:51fde11a771f | 93 | _adv_data_builder.setFlags(); |
Eugene0469 | 76:a3df66cb69e6 | 94 | _adv_data_builder.setLocalServiceList(mbed::make_Span(&_weight_uuid, 1)); |
mbed_official | 74:51fde11a771f | 95 | _adv_data_builder.setName(DEVICE_NAME); |
mbed_official | 74:51fde11a771f | 96 | |
mbed_official | 74:51fde11a771f | 97 | /* Setup advertising */ |
mbed_official | 74:51fde11a771f | 98 | |
mbed_official | 74:51fde11a771f | 99 | ble_error_t error = _ble.gap().setAdvertisingParameters( |
mbed_official | 74:51fde11a771f | 100 | ble::LEGACY_ADVERTISING_HANDLE, |
mbed_official | 74:51fde11a771f | 101 | adv_parameters |
mbed_official | 74:51fde11a771f | 102 | ); |
mbed_official | 74:51fde11a771f | 103 | |
mbed_official | 74:51fde11a771f | 104 | if (error) { |
mbed_official | 74:51fde11a771f | 105 | printf("_ble.gap().setAdvertisingParameters() failed\r\n"); |
mbed_official | 74:51fde11a771f | 106 | return; |
mbed_official | 74:51fde11a771f | 107 | } |
mbed_official | 74:51fde11a771f | 108 | |
mbed_official | 74:51fde11a771f | 109 | error = _ble.gap().setAdvertisingPayload( |
mbed_official | 74:51fde11a771f | 110 | ble::LEGACY_ADVERTISING_HANDLE, |
mbed_official | 74:51fde11a771f | 111 | _adv_data_builder.getAdvertisingData() |
mbed_official | 74:51fde11a771f | 112 | ); |
mbed_official | 74:51fde11a771f | 113 | |
mbed_official | 74:51fde11a771f | 114 | if (error) { |
mbed_official | 74:51fde11a771f | 115 | printf("_ble.gap().setAdvertisingPayload() failed\r\n"); |
mbed_official | 74:51fde11a771f | 116 | return; |
mbed_official | 74:51fde11a771f | 117 | } |
mbed_official | 74:51fde11a771f | 118 | |
mbed_official | 74:51fde11a771f | 119 | /* Start advertising */ |
mbed_official | 74:51fde11a771f | 120 | |
mbed_official | 74:51fde11a771f | 121 | error = _ble.gap().startAdvertising(ble::LEGACY_ADVERTISING_HANDLE); |
mbed_official | 74:51fde11a771f | 122 | |
mbed_official | 74:51fde11a771f | 123 | if (error) { |
mbed_official | 74:51fde11a771f | 124 | printf("_ble.gap().startAdvertising() failed\r\n"); |
mbed_official | 74:51fde11a771f | 125 | return; |
mbed_official | 74:51fde11a771f | 126 | } |
mbed_official | 2:864ddfb70a9c | 127 | } |
mbed_official | 2:864ddfb70a9c | 128 | |
Eugene0469 | 76:a3df66cb69e6 | 129 | void update_sensor_value() |
Eugene0469 | 76:a3df66cb69e6 | 130 | { |
Eugene0469 | 76:a3df66cb69e6 | 131 | if (_ble.gap().getState().connected) |
Eugene0469 | 76:a3df66cb69e6 | 132 | { |
Eugene0469 | 83:dc516d289cc0 | 133 | _alive_led = !_alive_led; |
Eugene0469 | 83:dc516d289cc0 | 134 | _current_weight = scale.read(); |
Eugene0469 | 83:dc516d289cc0 | 135 | // _current_weight = 456.87; |
Eugene0469 | 81:883a1a95f20b | 136 | WeightType_t weight; |
Eugene0469 | 81:883a1a95f20b | 137 | weight.integer = (int32_t)_current_weight; |
Eugene0469 | 81:883a1a95f20b | 138 | float f_decimal = _current_weight - weight.integer; |
Eugene0469 | 81:883a1a95f20b | 139 | weight.decimal = (uint8_t)(f_decimal* 100.0f); |
Eugene0469 | 83:dc516d289cc0 | 140 | if(_current_weight <= 0.00f) |
Eugene0469 | 83:dc516d289cc0 | 141 | { |
Eugene0469 | 83:dc516d289cc0 | 142 | _current_weight = 0.00; |
Eugene0469 | 83:dc516d289cc0 | 143 | } |
Eugene0469 | 76:a3df66cb69e6 | 144 | _weight_service->updateWeight(_current_weight); |
Eugene0469 | 82:d40c223b65f9 | 145 | pc.printf("Reading of float: %.2f\n", _current_weight); |
Eugene0469 | 82:d40c223b65f9 | 146 | pc.printf("Reading of structure: %d.%d\n", weight.integer,weight.decimal); |
Eugene0469 | 80:167f8aabffca | 147 | |
Eugene0469 | 76:a3df66cb69e6 | 148 | } |
mbed_official | 74:51fde11a771f | 149 | } |
mbed_official | 74:51fde11a771f | 150 | |
mbed_official | 74:51fde11a771f | 151 | private: |
mbed_official | 74:51fde11a771f | 152 | /* Event handler */ |
mbed_official | 2:864ddfb70a9c | 153 | |
mbed_official | 74:51fde11a771f | 154 | void onDisconnectionComplete(const ble::DisconnectionCompleteEvent&) { |
mbed_official | 74:51fde11a771f | 155 | _ble.gap().startAdvertising(ble::LEGACY_ADVERTISING_HANDLE); |
mbed_official | 74:51fde11a771f | 156 | } |
Eugene0469 | 82:d40c223b65f9 | 157 | void initialWeightReading(WeightType_t *newValue){ |
Eugene0469 | 82:d40c223b65f9 | 158 | newValue->integer = 0; |
Eugene0469 | 82:d40c223b65f9 | 159 | newValue->decimal = 0; |
Eugene0469 | 82:d40c223b65f9 | 160 | } |
mbed_official | 74:51fde11a771f | 161 | |
mbed_official | 74:51fde11a771f | 162 | private: |
mbed_official | 74:51fde11a771f | 163 | BLE &_ble; |
mbed_official | 74:51fde11a771f | 164 | events::EventQueue &_event_queue; |
Eugene0469 | 83:dc516d289cc0 | 165 | DigitalOut _alive_led; |
Eugene0469 | 76:a3df66cb69e6 | 166 | UUID _weight_uuid; |
Eugene0469 | 76:a3df66cb69e6 | 167 | |
Eugene0469 | 76:a3df66cb69e6 | 168 | float _current_weight; |
Eugene0469 | 80:167f8aabffca | 169 | WeightType_t _initial_weight; |
Eugene0469 | 76:a3df66cb69e6 | 170 | WEIGHTService *_weight_service; |
mbed_official | 2:864ddfb70a9c | 171 | |
mbed_official | 74:51fde11a771f | 172 | uint8_t _adv_buffer[ble::LEGACY_ADVERTISING_MAX_SIZE]; |
mbed_official | 74:51fde11a771f | 173 | ble::AdvertisingDataBuilder _adv_data_builder; |
mbed_official | 74:51fde11a771f | 174 | }; |
mbed_official | 74:51fde11a771f | 175 | |
mbed_official | 74:51fde11a771f | 176 | /** Schedule processing of events from the BLE middleware in the event queue. */ |
mbed_official | 74:51fde11a771f | 177 | void schedule_ble_events(BLE::OnEventsToProcessCallbackContext *context) { |
mbed_official | 74:51fde11a771f | 178 | event_queue.call(Callback<void()>(&context->ble, &BLE::processEvents)); |
mbed_official | 2:864ddfb70a9c | 179 | } |
mbed_official | 2:864ddfb70a9c | 180 | |
mbed_official | 2:864ddfb70a9c | 181 | int main() |
mbed_official | 2:864ddfb70a9c | 182 | { |
Eugene0469 | 76:a3df66cb69e6 | 183 | //scale.setScale(calibration_factor); //This value is obtained by using the SparkFun_HX711_Calibration sketch |
Eugene0469 | 76:a3df66cb69e6 | 184 | //scale.tare(); |
mbed_official | 74:51fde11a771f | 185 | BLE &ble = BLE::Instance(); |
mbed_official | 74:51fde11a771f | 186 | ble.onEventsToProcess(schedule_ble_events); |
mbed_official | 2:864ddfb70a9c | 187 | |
Eugene0469 | 76:a3df66cb69e6 | 188 | WEIGHTDemo demo(ble, event_queue); |
mbed_official | 74:51fde11a771f | 189 | demo.start(); |
mbed_official | 2:864ddfb70a9c | 190 | |
mbed_official | 2:864ddfb70a9c | 191 | return 0; |
mbed_official | 2:864ddfb70a9c | 192 | } |
mbed_official | 74:51fde11a771f | 193 |