Bluetooth Low Energy and Stepper Motor enabled device, compatible with the BlueST Protocol.
Dependencies: X_NUCLEO_IHM01A1
Bluetooth Low Energy and Stepper Motor enabled device, compatible with the BlueST Protocol.
source/CustomService.h@2:b51fb6efcd55, 2018-08-06 (annotated)
- Committer:
- Davidroid
- Date:
- Mon Aug 06 09:59:56 2018 +0000
- Revision:
- 2:b51fb6efcd55
- Parent:
- 1:84935552ca4d
Correct Custom Service UUID
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Davidroid | 0:f87cdc08dcd1 | 1 | /** |
Davidroid | 0:f87cdc08dcd1 | 2 | ******************************************************************************* |
Davidroid | 0:f87cdc08dcd1 | 3 | * @file CustomService.h |
Davidroid | 0:f87cdc08dcd1 | 4 | * @author Davide Aliprandi, STMicroelectronics |
Davidroid | 0:f87cdc08dcd1 | 5 | * @version V1.0.0 |
Davidroid | 0:f87cdc08dcd1 | 6 | * @date October 31st, 2017 |
Davidroid | 0:f87cdc08dcd1 | 7 | * @brief mbed test application for the STMicroelectronics X-NUCLEO-IHM01A1 |
Davidroid | 0:f87cdc08dcd1 | 8 | * Motor Control Expansion Board and the X-NUCLEO-IDB05A1 Bluetooth |
Davidroid | 0:f87cdc08dcd1 | 9 | * Low energy Expansion Board. |
Davidroid | 0:f87cdc08dcd1 | 10 | ******************************************************************************* |
Davidroid | 0:f87cdc08dcd1 | 11 | * @attention |
Davidroid | 0:f87cdc08dcd1 | 12 | * |
Davidroid | 0:f87cdc08dcd1 | 13 | * <h2><center>© COPYRIGHT(c) 2018 STMicroelectronics</center></h2> |
Davidroid | 0:f87cdc08dcd1 | 14 | * |
Davidroid | 0:f87cdc08dcd1 | 15 | * Redistribution and use in source and binary forms, with or without |
Davidroid | 0:f87cdc08dcd1 | 16 | * modification, are permitted provided that the following conditions are met: |
Davidroid | 0:f87cdc08dcd1 | 17 | * 1. Redistributions of source code must retain the above copyright notice, |
Davidroid | 0:f87cdc08dcd1 | 18 | * this list of conditions and the following disclaimer. |
Davidroid | 0:f87cdc08dcd1 | 19 | * 2. Redistributions in binary form must reproduce the above copyright |
Davidroid | 0:f87cdc08dcd1 | 20 | * notice, this list of conditions and the following disclaimer in the |
Davidroid | 0:f87cdc08dcd1 | 21 | * documentation and/or other materials provided with the distribution. |
Davidroid | 0:f87cdc08dcd1 | 22 | * 3. Neither the name of STMicroelectronics nor the names of its |
Davidroid | 0:f87cdc08dcd1 | 23 | * contributors may be used to endorse or promote products derived from |
Davidroid | 0:f87cdc08dcd1 | 24 | * this software without specific prior written permission. |
Davidroid | 0:f87cdc08dcd1 | 25 | * |
Davidroid | 0:f87cdc08dcd1 | 26 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
Davidroid | 0:f87cdc08dcd1 | 27 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
Davidroid | 0:f87cdc08dcd1 | 28 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
Davidroid | 0:f87cdc08dcd1 | 29 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS |
Davidroid | 0:f87cdc08dcd1 | 30 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
Davidroid | 0:f87cdc08dcd1 | 31 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
Davidroid | 0:f87cdc08dcd1 | 32 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
Davidroid | 0:f87cdc08dcd1 | 33 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
Davidroid | 0:f87cdc08dcd1 | 34 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
Davidroid | 0:f87cdc08dcd1 | 35 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
Davidroid | 0:f87cdc08dcd1 | 36 | * POSSIBILITY OF SUCH DAMAGE. |
Davidroid | 0:f87cdc08dcd1 | 37 | * |
Davidroid | 0:f87cdc08dcd1 | 38 | ******************************************************************************* |
Davidroid | 0:f87cdc08dcd1 | 39 | */ |
Davidroid | 0:f87cdc08dcd1 | 40 | |
Davidroid | 0:f87cdc08dcd1 | 41 | |
Davidroid | 0:f87cdc08dcd1 | 42 | #ifndef __BLE_CUSTOM_SERVICE_H__ |
Davidroid | 0:f87cdc08dcd1 | 43 | #define __BLE_CUSTOM_SERVICE_H__ |
Davidroid | 0:f87cdc08dcd1 | 44 | |
Davidroid | 0:f87cdc08dcd1 | 45 | #include "ble_utils.h" |
Davidroid | 0:f87cdc08dcd1 | 46 | |
Davidroid | 0:f87cdc08dcd1 | 47 | #define STATE_DATA_LENGTH (sizeof(uint16_t) + sizeof(uint8_t)) |
Davidroid | 0:f87cdc08dcd1 | 48 | #define COMMAND_DATA_LENGTH (sizeof(uint8_t) + sizeof(uint32_t)) |
Davidroid | 0:f87cdc08dcd1 | 49 | #define MAX_DATA_LENGTH (COMMAND_DATA_LENGTH) |
Davidroid | 0:f87cdc08dcd1 | 50 | |
Davidroid | 2:b51fb6efcd55 | 51 | const UUID::LongUUIDBytes_t CUSTOM_SERVICE_UUID = {0x00,0x00,0x00,0x00,0x00,0x01,0x11,0xe1,0x9a,0xb4,0x00,0x02,0xa5,0xd5,0xc5,0x1b}; |
Davidroid | 0:f87cdc08dcd1 | 52 | const UUID::LongUUIDBytes_t CUSTOM_STEPPER_MOTOR_CHARACTERISTIC_UUID = {0x00,0x00,0x20,0x00,0x00,0x01,0x11,0xe1,0xac,0x36,0x00,0x02,0xa5,0xd5,0xc5,0x1b}; |
Davidroid | 0:f87cdc08dcd1 | 53 | |
Davidroid | 0:f87cdc08dcd1 | 54 | class CustomService { |
Davidroid | 0:f87cdc08dcd1 | 55 | public: |
Davidroid | 0:f87cdc08dcd1 | 56 | |
Davidroid | 0:f87cdc08dcd1 | 57 | /* Motor states. */ |
Davidroid | 0:f87cdc08dcd1 | 58 | typedef enum |
Davidroid | 0:f87cdc08dcd1 | 59 | { |
Davidroid | 0:f87cdc08dcd1 | 60 | MOTOR_INACTIVE = 0, // Motor not running. |
Davidroid | 0:f87cdc08dcd1 | 61 | MOTOR_RUNNING // Motor running. |
Davidroid | 0:f87cdc08dcd1 | 62 | } motor_state_t; |
Davidroid | 0:f87cdc08dcd1 | 63 | |
Davidroid | 0:f87cdc08dcd1 | 64 | /* Motor commands. */ |
Davidroid | 0:f87cdc08dcd1 | 65 | typedef enum |
Davidroid | 0:f87cdc08dcd1 | 66 | { |
Davidroid | 0:f87cdc08dcd1 | 67 | MOTOR_STOP_RUNNING_WITHOUT_TORQUE = 0, // Stops running with HiZ. |
Davidroid | 0:f87cdc08dcd1 | 68 | MOTOR_STOP_RUNNING_WITH_TORQUE, // Stops running with torque applied. |
Davidroid | 0:f87cdc08dcd1 | 69 | MOTOR_RUN_FORWARD, // Runs forward indefinitely. |
Davidroid | 0:f87cdc08dcd1 | 70 | MOTOR_RUN_BACKWARD, // Runs backward indefinitely. |
Davidroid | 0:f87cdc08dcd1 | 71 | MOTOR_MOVE_STEPS_FORWARD, // Moves steps forward. |
Davidroid | 0:f87cdc08dcd1 | 72 | MOTOR_MOVE_STEPS_BACKWARD // Moves steps backward. |
Davidroid | 0:f87cdc08dcd1 | 73 | } motor_command_t; |
Davidroid | 0:f87cdc08dcd1 | 74 | |
Davidroid | 0:f87cdc08dcd1 | 75 | CustomService(BLEDevice &_ble) : |
Davidroid | 0:f87cdc08dcd1 | 76 | ble(_ble), |
Davidroid | 0:f87cdc08dcd1 | 77 | state_command(CUSTOM_STEPPER_MOTOR_CHARACTERISTIC_UUID, packed_state_command, MAX_DATA_LENGTH, MAX_DATA_LENGTH, |
Davidroid | 0:f87cdc08dcd1 | 78 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE) |
Davidroid | 0:f87cdc08dcd1 | 79 | { |
Davidroid | 0:f87cdc08dcd1 | 80 | GattCharacteristic *char_table[] = {&state_command}; |
Davidroid | 2:b51fb6efcd55 | 81 | GattService motor_service(CUSTOM_SERVICE_UUID, char_table, sizeof(char_table) / sizeof(GattCharacteristic *)); |
Davidroid | 0:f87cdc08dcd1 | 82 | ble.addService(motor_service); |
Davidroid | 0:f87cdc08dcd1 | 83 | memset (packed_state_command, 0, MAX_DATA_LENGTH); |
Davidroid | 0:f87cdc08dcd1 | 84 | } |
Davidroid | 0:f87cdc08dcd1 | 85 | |
Davidroid | 0:f87cdc08dcd1 | 86 | void send_state(uint16_t time_stamp, uint8_t current_state) { |
Davidroid | 0:f87cdc08dcd1 | 87 | memset (packed_state_command, 0, MAX_DATA_LENGTH); |
Davidroid | 0:f87cdc08dcd1 | 88 | STORE_LE_16(packed_state_command, time_stamp); |
Davidroid | 0:f87cdc08dcd1 | 89 | packed_state_command[2] = current_state; |
Davidroid | 1:84935552ca4d | 90 | ble.gattServer().write(state_command.getValueAttribute().getHandle(), (uint8_t *) &packed_state_command, STATE_DATA_LENGTH, 0); |
Davidroid | 0:f87cdc08dcd1 | 91 | } |
Davidroid | 0:f87cdc08dcd1 | 92 | |
Davidroid | 0:f87cdc08dcd1 | 93 | GattAttribute::Handle_t getValueHandle() const |
Davidroid | 0:f87cdc08dcd1 | 94 | { |
Davidroid | 1:84935552ca4d | 95 | return state_command.getValueAttribute().getHandle(); |
Davidroid | 0:f87cdc08dcd1 | 96 | } |
Davidroid | 0:f87cdc08dcd1 | 97 | |
Davidroid | 0:f87cdc08dcd1 | 98 | private: |
Davidroid | 0:f87cdc08dcd1 | 99 | BLEDevice &ble; |
Davidroid | 0:f87cdc08dcd1 | 100 | GattCharacteristic state_command; |
Davidroid | 0:f87cdc08dcd1 | 101 | uint8_t packed_state_command[MAX_DATA_LENGTH]; |
Davidroid | 0:f87cdc08dcd1 | 102 | }; |
Davidroid | 0:f87cdc08dcd1 | 103 | |
Davidroid | 0:f87cdc08dcd1 | 104 | #endif /* #ifndef __BLE_CUSTOM_SERVICE_H__ */ |