| 1 |
// Copyright 2019-2020, Collabora, Ltd. |
| 2 |
// SPDX-License-Identifier: BSL-1.0 |
| 3 |
/*! |
| 4 |
* @file |
| 5 |
* @brief Arduino based felxable input device code. |
| 6 |
* @author Pete Black <[email protected]> |
| 7 |
* @author Jakob Bornecrantz <[email protected]> |
| 8 |
* @ingroup drv_arduino |
| 9 |
*/ |
| 10 |
|
| 11 |
#include "mbed.h" |
| 12 |
#include <stdint.h> |
| 13 |
#include <ArduinoBLE.h> |
| 14 |
#include <Arduino_LSM9DS1.h> |
| 15 |
|
| 16 |
|
| 17 |
#define USE_SERIAL |
| 18 |
#ifdef USE_SERIAL |
| 19 |
#define LOG(...) Serial.print(__VA_ARGS__) |
| 20 |
#define LOG_LN(...) Serial.println(__VA_ARGS__) |
| 21 |
#else |
| 22 |
#define LOG(...) (void)NULL |
| 23 |
#define LOG_LN(...) (void)NULL |
| 24 |
#endif |
| 25 |
|
| 26 |
#define SET(o, v) \ |
| 27 |
do { \ |
| 28 |
uint32_t val = v; \ |
| 29 |
buffer[o + 0] = val >> 8; \ |
| 30 |
buffer[o + 1] = val; \ |
| 31 |
} while (false) |
| 32 |
|
| 33 |
struct Sample |
| 34 |
{ |
| 35 |
int32_t acc_x = 0; |
| 36 |
int32_t acc_y = 0; |
| 37 |
int32_t acc_z = 0; |
| 38 |
int32_t gyr_x = 0; |
| 39 |
int32_t gyr_y = 0; |
| 40 |
int32_t gyr_z = 0; |
| 41 |
uint32_t time = 0; |
| 42 |
}; |
| 43 |
|
| 44 |
|
| 45 |
/* |
| 46 |
* |
| 47 |
* Global variables |
| 48 |
* |
| 49 |
*/ |
| 50 |
|
| 51 |
volatile bool isConnected; |
| 52 |
|
| 53 |
BLEService service("00004242-0000-1000-8000-004242424242"); // create service |
| 54 |
|
| 55 |
// create switch characteristic and allow remote device to read and write |
| 56 |
BLECharacteristic notify("00000001-1000-1000-8000-004242424242", |
| 57 |
BLERead | BLENotify, |
| 58 |
20, |
| 59 |
true); |
| 60 |
|
| 61 |
rtos::Mutex mutex; |
| 62 |
rtos::Thread thread; |
| 63 |
rtos::Mail<Sample, 100> mail; |
| 64 |
|
| 65 |
|
| 66 |
/* |
| 67 |
* |
| 68 |
* IMU functions. |
| 69 |
* |
| 70 |
*/ |
| 71 |
|
| 72 |
void |
| 73 |
imu_loop() |
| 74 |
{ |
| 75 |
if (isConnected == false) { |
| 76 |
return; |
| 77 |
} |
| 78 |
|
| 79 |
if (IMU.accelerationAvailable() && IMU.gyroscopeAvailable()) { |
| 80 |
uint32_t time = micros(); |
| 81 |
float gyr_x, gyr_y, gyr_z; |
| 82 |
float acc_x, acc_y, acc_z; |
| 83 |
IMU.readGyroscope(gyr_x, gyr_y, gyr_z); |
| 84 |
IMU.readAcceleration(acc_x, acc_y, acc_z); |
| 85 |
|
| 86 |
Sample *sample = mail.alloc(); |
| 87 |
sample->time = time; |
| 88 |
sample->acc_x = acc_x * (32768.0 / 4.0); |
| 89 |
sample->acc_y = acc_y * (32768.0 / 4.0); |
| 90 |
sample->acc_z = acc_z * (32768.0 / 4.0); |
| 91 |
|
| 92 |
sample->gyr_x = gyr_x * (32768.0 / 2000.0); |
| 93 |
sample->gyr_y = gyr_y * (32768.0 / 2000.0); |
| 94 |
sample->gyr_z = gyr_z * (32768.0 / 2000.0); |
| 95 |
mail.put(sample); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
void |
| 100 |
imu_thread() |
| 101 |
{ |
| 102 |
while (true) { |
| 103 |
imu_loop(); |
| 104 |
delay(1); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
/* |
| 110 |
* |
| 111 |
* BLE functions. |
| 112 |
* |
| 113 |
*/ |
| 114 |
|
| 115 |
void |
| 116 |
loop() |
| 117 |
{ |
| 118 |
bool got_mail = false; |
| 119 |
int32_t acc_x = 0, acc_y = 0, acc_z = 0; |
| 120 |
int32_t gyr_x = 0, gyr_y = 0, gyr_z = 0; |
| 121 |
int32_t count = 0; |
| 122 |
uint32_t time; |
| 123 |
|
| 124 |
uint8_t buffer[20]; |
| 125 |
static int countPacket = 0; |
| 126 |
|
| 127 |
BLE.poll(); |
| 128 |
if (isConnected == false) { |
| 129 |
return; |
| 130 |
} |
| 131 |
|
| 132 |
while (true) { |
| 133 |
osEvent evt = mail.get(0); |
| 134 |
if (evt.status != osEventMail) { |
| 135 |
break; |
| 136 |
} |
| 137 |
|
| 138 |
Sample *sample = (Sample *)evt.value.p; |
| 139 |
acc_x += sample->acc_x; |
| 140 |
acc_y += sample->acc_y; |
| 141 |
acc_z += sample->acc_z; |
| 142 |
gyr_x += sample->gyr_x; |
| 143 |
gyr_y += sample->gyr_y; |
| 144 |
gyr_z += sample->gyr_z; |
| 145 |
time = sample->time; |
| 146 |
mail.free(sample); |
| 147 |
|
| 148 |
count++; |
| 149 |
got_mail = true; |
| 150 |
} |
| 151 |
|
| 152 |
if (!got_mail) { |
| 153 |
delay(1); |
| 154 |
return; |
| 155 |
} |
| 156 |
|
| 157 |
buffer[0] = countPacket++; |
| 158 |
buffer[1] = count; |
| 159 |
buffer[2] = 0; |
| 160 |
|
| 161 |
buffer[3] = time >> 16; |
| 162 |
buffer[4] = time >> 8; |
| 163 |
buffer[5] = time; |
| 164 |
|
| 165 |
SET(6, (acc_x / count)); |
| 166 |
SET(8, (acc_y / count)); |
| 167 |
SET(10, (acc_z / count)); |
| 168 |
SET(12, (gyr_x / count)); |
| 169 |
SET(14, (gyr_y / count)); |
| 170 |
SET(16, (gyr_z / count)); |
| 171 |
buffer[18] = 0; |
| 172 |
buffer[19] = 0; |
| 173 |
|
| 174 |
notify.writeValue(buffer, 20); |
| 175 |
} |
| 176 |
|
| 177 |
void |
| 178 |
ble_peripheral_connect_handler(BLEDevice peer) |
| 179 |
{ |
| 180 |
LOG("Connected event, central: "); |
| 181 |
LOG_LN(peer.address()); |
| 182 |
isConnected = true; |
| 183 |
BLE.poll(); |
| 184 |
} |
| 185 |
|
| 186 |
void |
| 187 |
ble_peripheral_disconnect_handler(BLEDevice peer) |
| 188 |
{ |
| 189 |
LOG("Disconnected event, central: "); |
| 190 |
LOG_LN(peer.address()); |
| 191 |
isConnected = false; |
| 192 |
BLE.poll(); |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
/* |
| 197 |
* |
| 198 |
* Main functions. |
| 199 |
* |
| 200 |
*/ |
| 201 |
|
| 202 |
void |
| 203 |
block() |
| 204 |
{ |
| 205 |
while (true) { |
| 206 |
LOG_LN("BLOCKED"); |
| 207 |
delay(1000); |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
void |
| 212 |
setup() |
| 213 |
{ |
| 214 |
#ifdef USE_SERIAL |
| 215 |
Serial.begin(9600); |
| 216 |
while (!Serial) { |
| 217 |
} |
| 218 |
#endif |
| 219 |
|
| 220 |
if (!BLE.begin()) { |
| 221 |
LOG_LN("BLE initialisation failed!"); |
| 222 |
block(); |
| 223 |
} |
| 224 |
|
| 225 |
if (!IMU.begin()) { |
| 226 |
LOG_LN("IMU initialisation failed!"); |
| 227 |
block(); |
| 228 |
} |
| 229 |
|
| 230 |
BLE.setLocalName("Monado Flexible Controller"); |
| 231 |
BLE.setAdvertisedService(service); |
| 232 |
service.addCharacteristic(notify); |
| 233 |
BLE.addService(service); |
| 234 |
|
| 235 |
// Assign event handlers for connected, disconnected to peripheral. |
| 236 |
BLE.setEventHandler(BLEConnected, ble_peripheral_connect_handler); |
| 237 |
BLE.setEventHandler(BLEDisconnected, ble_peripheral_disconnect_handler); |
| 238 |
|
| 239 |
notify.setValue(0); |
| 240 |
|
| 241 |
// Start advertising. |
| 242 |
BLE.advertise(); |
| 243 |
|
| 244 |
isConnected = false; |
| 245 |
|
| 246 |
// IMU has its own thread. |
| 247 |
thread.start(imu_thread); |
| 248 |
} |