Bluetooth Low Energy and Stepper Motor enabled device, compatible with the BlueST Protocol.

Dependencies:   X_NUCLEO_IHM01A1

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers CustomService.h Source File

CustomService.h

Go to the documentation of this file.
00001 /**
00002  *******************************************************************************
00003  * @file    CustomService.h
00004  * @author  Davide Aliprandi, STMicroelectronics
00005  * @version V1.0.0
00006  * @date    October 31st, 2017
00007  * @brief   mbed test application for the STMicroelectronics X-NUCLEO-IHM01A1
00008  *          Motor Control Expansion Board and the X-NUCLEO-IDB05A1 Bluetooth
00009  *          Low energy Expansion Board.
00010  *******************************************************************************
00011  * @attention
00012  *
00013  * <h2><center>&copy; COPYRIGHT(c) 2018 STMicroelectronics</center></h2>
00014  *
00015  * Redistribution and use in source and binary forms, with or without
00016  * modification, are permitted provided that the following conditions are met:
00017  *   1. Redistributions of source code must retain the above copyright notice,
00018  *      this list of conditions and the following disclaimer.
00019  *   2. Redistributions in binary form must reproduce the above copyright
00020  *      notice, this list of conditions and the following disclaimer in the
00021  *      documentation and/or other materials provided with the distribution.
00022  *   3. Neither the name of STMicroelectronics nor the names of its
00023  *      contributors may be used to endorse or promote products derived from
00024  *      this software without specific prior written permission.
00025  *
00026  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00027  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00029  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS 
00030  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00031  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00032  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00033  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00034  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00035  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00036  * POSSIBILITY OF SUCH DAMAGE.
00037  *
00038  *******************************************************************************
00039  */
00040  
00041 
00042 #ifndef __BLE_CUSTOM_SERVICE_H__
00043 #define __BLE_CUSTOM_SERVICE_H__
00044 
00045 #include "ble_utils.h"
00046 
00047 #define STATE_DATA_LENGTH   (sizeof(uint16_t) + sizeof(uint8_t))
00048 #define COMMAND_DATA_LENGTH (sizeof(uint8_t) + sizeof(uint32_t))
00049 #define MAX_DATA_LENGTH     (COMMAND_DATA_LENGTH)
00050 
00051 const UUID::LongUUIDBytes_t CUSTOM_SERVICE_UUID                      = {0x00,0x00,0x00,0x00,0x00,0x01,0x11,0xe1,0x9a,0xb4,0x00,0x02,0xa5,0xd5,0xc5,0x1b};
00052 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};
00053 
00054 class CustomService {
00055 public:
00056 
00057     /* Motor states. */
00058     typedef enum
00059     {
00060         MOTOR_INACTIVE = 0,  // Motor not running.
00061         MOTOR_RUNNING        // Motor running.
00062     } motor_state_t;
00063 
00064     /* Motor commands. */
00065     typedef enum
00066     {
00067         MOTOR_STOP_RUNNING_WITHOUT_TORQUE = 0,  // Stops running with HiZ.
00068         MOTOR_STOP_RUNNING_WITH_TORQUE,         // Stops running with torque applied.
00069         MOTOR_RUN_FORWARD,                      // Runs forward indefinitely.
00070         MOTOR_RUN_BACKWARD,                     // Runs backward indefinitely.
00071         MOTOR_MOVE_STEPS_FORWARD,               // Moves steps forward.
00072         MOTOR_MOVE_STEPS_BACKWARD               // Moves steps backward.
00073     } motor_command_t;
00074 
00075     CustomService(BLEDevice &_ble) :
00076         ble(_ble),
00077         state_command(CUSTOM_STEPPER_MOTOR_CHARACTERISTIC_UUID, packed_state_command, MAX_DATA_LENGTH, MAX_DATA_LENGTH,
00078             GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE)
00079     {
00080         GattCharacteristic *char_table[] = {&state_command};
00081         GattService motor_service(CUSTOM_SERVICE_UUID, char_table, sizeof(char_table) / sizeof(GattCharacteristic *));
00082         ble.addService(motor_service);
00083         memset (packed_state_command, 0, MAX_DATA_LENGTH);
00084     }
00085 
00086     void send_state(uint16_t time_stamp, uint8_t current_state) {
00087         memset (packed_state_command, 0, MAX_DATA_LENGTH);
00088         STORE_LE_16(packed_state_command, time_stamp);
00089         packed_state_command[2] = current_state;
00090         ble.gattServer().write(state_command.getValueAttribute().getHandle(), (uint8_t *) &packed_state_command, STATE_DATA_LENGTH, 0);
00091     }
00092 
00093     GattAttribute::Handle_t getValueHandle() const
00094     {
00095         return state_command.getValueAttribute().getHandle();
00096     }
00097 
00098 private:
00099     BLEDevice &ble;
00100     GattCharacteristic state_command;
00101     uint8_t packed_state_command[MAX_DATA_LENGTH];
00102 };
00103 
00104 #endif /* #ifndef __BLE_CUSTOM_SERVICE_H__ */