-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbleaccel.cpp
More file actions
146 lines (120 loc) · 4.37 KB
/
Copy pathbleaccel.cpp
File metadata and controls
146 lines (120 loc) · 4.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <CurieBLE.h>
#include "bleaccel.h"
#include "string.h"
#define ACCEL_BYTES 2
#define LED_PIN 13
void blePeripheralConnectHandler(BLECentral& central) ;
void blePeripheralDisconnectHandler(BLECentral& central);
void bleCharacteristicWritten(BLECentral& central, BLECharacteristic& characteristic);
void accelCharacteristicWritten(BLECentral& central, BLECharacteristic& characteristic);
int period = 1000;
int config_value = 0;
bool isConnectedFlag = false;
BLEPeripheral blePeripheral;
BLEService accelService("AA10"); // ACCEL service
// The Accel data is 3 signed 16 bit ints.
BLECharacteristic accelDataChar("AA11", BLERead | BLENotify, ACCEL_BYTES * 3);
BLEDescriptor accelDataCharUserDesc("2901","Accel Data");
// Enables converts and sets the "Gs"
BLEUnsignedCharCharacteristic accelConfigChar("AA12", BLERead | BLEWrite);
BLEDescriptor accelConfigCharUserDesc("2901","Accel Conf");
// The sample period, in milliseconds defaluts to 1000
BLECharacteristic accelPeriodChar("AA13", BLERead | BLEWrite, 2); // This set the sample perion
BLEDescriptor accelPeriodCharUserDesc("2901","Acc Period");
// Default bogus data for testing
unsigned char accel_data[ACCEL_BYTES * 3]={0x01,0x01,
0x02,0x01,
0x03,0x01,
};
void initAccelBLE()
{
pinMode(LED_PIN, OUTPUT); // initialize the LED on pin 13 to indicate when a central is connected
// set the local name peripheral advertises
blePeripheral.setLocalName("ACCEL");
// set the UUID for the service this peripheral advertises
blePeripheral.setAdvertisedServiceUuid(accelService.uuid()); // Add the service ID.
blePeripheral.addAttribute(accelService);
// Add characteristics and descriptions
blePeripheral.addAttribute(accelDataChar);
blePeripheral.addAttribute(accelDataCharUserDesc);
accelDataChar.setValue(&accel_data[0], ACCEL_BYTES * 3);
blePeripheral.addAttribute(accelConfigChar);
blePeripheral.addAttribute(accelConfigCharUserDesc);
accelConfigChar.setEventHandler(BLEWritten, accelCharacteristicWritten);
blePeripheral.addAttribute(accelPeriodChar);
blePeripheral.addAttribute(accelPeriodCharUserDesc);
accelPeriodChar.setEventHandler(BLEWritten, accelCharacteristicWritten);
blePeripheral.setEventHandler(BLEConnected, blePeripheralConnectHandler);
blePeripheral.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler);
blePeripheral.begin();
Serial.println("BLE Set up complete");
}
bool isConnected()
{
return isConnectedFlag;
}
void blePeripheralConnectHandler(BLECentral& central)
{
// central connected event handler
Serial.print("Connected event, central: ");
Serial.println(central.address());
digitalWrite(LED_PIN, HIGH);
isConnectedFlag = true;
}
void blePeripheralDisconnectHandler(BLECentral& central)
{
// central disconnected event handler
Serial.print("Disconnected event, central: ");
Serial.println(central.address());
digitalWrite(LED_PIN, LOW);
isConnectedFlag = false;
period = 1000;
config_value = 0;
}
void accelCharacteristicWritten(BLECentral& central, BLECharacteristic& characteristic)
{
const char* uuid_notify = characteristic.uuid();
Serial.print("Characteristic event, written uuid = ");
Serial.print(uuid_notify);
// Test which characteristic was written to.
if( !strcmp(uuid_notify,accelConfigChar.uuid()) )
{
config_value = accelConfigChar.value();
Serial.print(" accelConfigChar was written value = ");
Serial.println(accelConfigChar.value() );
return;
}
if( !strcmp(uuid_notify,accelPeriodChar.uuid()) )
{
Serial.print(" accelPeriodChar was written value = ");
const unsigned char *v = accelPeriodChar.value();
period = (v[0] * 256) + v[1];
Serial.println(period );
return;
}
Serial.println(" Unknown UUID");
}
void convertToBytes( unsigned char *b, unsigned int v)
{
int m = (ACCEL_BYTES - 1) * 8;
for( int i = 0 ; i < ACCEL_BYTES ; ++i )
{
b[i] = v >> m;;
m -= 8;
}
}
void setAccelData(int x, int y, int z)
{
convertToBytes( &accel_data[0], x);
convertToBytes( &accel_data[ACCEL_BYTES], y);
convertToBytes( &accel_data[ACCEL_BYTES * 2], z);
accelDataChar.setValue(&accel_data[0], ACCEL_BYTES * 3);
}
int getCongfig()
{
return config_value;
}
int getPeriod()
{
return period;
}