Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Bluetooth Low Energy enabled device with "Switch" feature, compatible with BlueST Protocol.
Revision 4:67ff1ddd11e2, committed 2018-05-31
- Comitter:
- Davidroid
- Date:
- Thu May 31 18:30:29 2018 +0000
- Parent:
- 3:b880b9a9ccdf
- Child:
- 5:1651815d2a74
- Commit message:
- Send switch_state rather than toggle_state via BLE
Changed in this revision
| source/CustomService.h | Show annotated file Show diff for this revision Revisions of this file |
| source/main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/source/CustomService.h Thu May 10 11:59:17 2018 +0000
+++ b/source/CustomService.h Thu May 31 18:30:29 2018 +0000
@@ -43,9 +43,8 @@
#include "ble_utils.h"
-#define STATE_DATA_LENGTH (sizeof(uint16_t) + sizeof(uint8_t))
-#define COMMAND_DATA_LENGTH (sizeof(uint8_t))
-#define MAX_DATA_LENGTH (STATE_DATA_LENGTH)
+#define SWITCH_DATA_LENGTH (sizeof(uint16_t) + sizeof(uint8_t))
+#define SWITCH_DATA_INDEX (sizeof(uint16_t))
const UUID::LongUUIDBytes_t CUSTOM_SWITCH_SERVICE_UUID = {0x00,0x00,0x00,0x00,0x00,0x01,0x11,0xe1,0xac,0x36,0x00,0x02,0xa5,0xd5,0xc5,0x1b};
const UUID::LongUUIDBytes_t CUSTOM_SWITCH_CHARACTERISTIC_UUID = {0x20,0x00,0x00,0x00,0x00,0x01,0x11,0xe1,0xac,0x36,0x00,0x02,0xa5,0xd5,0xc5,0x1b};
@@ -62,20 +61,20 @@
CustomService(BLEDevice &_ble) :
ble(_ble),
- state_command(CUSTOM_SWITCH_CHARACTERISTIC_UUID, packed_state_command, MAX_DATA_LENGTH, MAX_DATA_LENGTH,
+ state_command(CUSTOM_SWITCH_CHARACTERISTIC_UUID, packed_state_command, SWITCH_DATA_LENGTH, SWITCH_DATA_LENGTH,
/*GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | */GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)
{
GattCharacteristic *char_table[] = {&state_command};
GattService switch_service(CUSTOM_SWITCH_SERVICE_UUID, char_table, sizeof(char_table) / sizeof(GattCharacteristic *));
ble.addService(switch_service);
- memset (packed_state_command, 0, MAX_DATA_LENGTH);
+ memset (packed_state_command, 0, SWITCH_DATA_LENGTH);
}
void send_state(uint16_t time_stamp, uint8_t current_state) {
- memset (packed_state_command, 0, MAX_DATA_LENGTH);
+ memset (packed_state_command, 0, SWITCH_DATA_LENGTH);
STORE_LE_16(packed_state_command, time_stamp);
- packed_state_command[2] = current_state;
- ble.gattServer().write(state_command.getValueAttribute().getHandle(), (uint8_t *) &packed_state_command, STATE_DATA_LENGTH, 0);
+ packed_state_command[SWITCH_DATA_INDEX] = current_state;
+ ble.gattServer().write(state_command.getValueAttribute().getHandle(), (uint8_t *) &packed_state_command, SWITCH_DATA_LENGTH, 0);
}
GattAttribute::Handle_t getValueHandle() const
@@ -86,7 +85,7 @@
private:
BLEDevice &ble;
GattCharacteristic state_command;
- uint8_t packed_state_command[MAX_DATA_LENGTH];
+ uint8_t packed_state_command[SWITCH_DATA_LENGTH];
};
#endif /* #ifndef __BLE_CUSTOM_SERVICE_H__ */
--- a/source/main.cpp Thu May 10 11:59:17 2018 +0000
+++ b/source/main.cpp Thu May 31 18:30:29 2018 +0000
@@ -54,19 +54,10 @@
#define DELAY_1 100
-/* Types ---------------------------------------------------------------------*/
-
-/* Toggle state. */
-typedef enum
-{
- TOGGLE_OFF = 0,
- TOGGLE_ON
-} toggle_t;
-
-
/* Variables -----------------------------------------------------------------*/
-/* Button state. */
+/* Switch and toggle state. */
+CustomService::switch_state_t switch_state;
bool toggle_state;
/* LED to indicate system state. */
@@ -103,8 +94,8 @@
if (params->handle == custom_service->getValueHandle()) {
// Reading led state and sending it via bluetooth.
if (BLE::Instance().getGapState().connected) {
- //event_queue.call(Callback<void(uint16_t, uint8_t)>(custom_service, &CustomService::send_state), time_stamp++, led_state.read() ? CustomService::SWITCH_ON : CustomService::SWITCH_OFF);
- event_queue.call(Callback<void(uint16_t, uint8_t)>(custom_service, &CustomService::send_state), time_stamp++, TOGGLE_OFF); // To clean the BLE buffer.
+ switch_state = led_state.read() ? CustomService::SWITCH_ON : CustomService::SWITCH_OFF;
+ event_queue.call(Callback<void(uint16_t, uint8_t)>(custom_service, &CustomService::send_state), time_stamp++, switch_state);
}
}
}
@@ -120,7 +111,7 @@
if (params->handle == custom_service->getValueHandle()) {
/* Receiving command via bluetooth and writing it to the led state. */
- led_state.write(((CustomService::switch_state_t) ((uint8_t *) (params->data))[0]));
+ led_state.write(((CustomService::switch_state_t) ((uint8_t *) (params->data))[SWITCH_DATA_INDEX]));
}
}
@@ -134,7 +125,7 @@
/* Reading toggle command and sending it via bluetooth. */
if (toggle_state) {
if (BLE::Instance().getGapState().connected) {
- event_queue.call(Callback<void(uint16_t, uint8_t)>(custom_service, &CustomService::send_state), time_stamp++, toggle_state ? TOGGLE_ON : TOGGLE_OFF);
+ event_queue.call(Callback<void(uint16_t, uint8_t)>(custom_service, &CustomService::send_state), time_stamp++, switch_state);
toggle_state = false;
}
}
@@ -192,6 +183,7 @@
/* Custom service related functions ------------------------------------------*/
void button_callback(void) {
+ switch_state = (CustomService::switch_state_t) (CustomService::SWITCH_ON - switch_state);
toggle_state = true;
}
@@ -214,8 +206,10 @@
/* Attaching and enabling interrupt handlers. */
event.fall(button_callback);
- /* Setting up initial state: led OFF, no toggle state. */
- led_state.write(CustomService::SWITCH_OFF);
+ /* Setting up initial state: switch feature OFF, i.e. led OFF, and no toggle
+ state triggered. */
+ switch_state = CustomService::SWITCH_OFF;
+ led_state.write(switch_state);
toggle_state = false;
/* Start. */