Skip to content

Commit b80abcb

Browse files
committed
added cplay_ble.ino
1 parent 6ac348f commit b80abcb

File tree

2 files changed

+318
-0
lines changed

2 files changed

+318
-0
lines changed
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
/*********************************************************************
2+
This is an example for our nRF52 based Bluefruit LE modules
3+
4+
Pick one up today in the adafruit shop!
5+
6+
Adafruit invests time and resources providing this open source code,
7+
please support Adafruit and open-source hardware by purchasing
8+
products from Adafruit!
9+
10+
MIT license, check LICENSE for more information
11+
All text above, and the splash screen below must be included in
12+
any redistribution
13+
*********************************************************************/
14+
#include <bluefruit.h>
15+
#include <Adafruit_LittleFS.h>
16+
#include <InternalFileSystem.h>
17+
#include <Adafruit_CircuitPlayground.h>
18+
19+
// BLE Service
20+
BLEDfu bledfu; // OTA DFU service
21+
BLEDis bledis; // device information
22+
BLEUart bleuart; // uart over ble
23+
BLEBas blebas; // battery
24+
25+
// Adafruit Service: ADAFxx-C332-42A8-93BD-25E905756CB8
26+
BLEAdafruitTemperature bleTemp;
27+
BLEAdafruitAccel bleAccel;
28+
BLEAdafruitLightSensor bleLight;
29+
BLEAdafruitButton bleButton;
30+
31+
uint16_t measure_button(uint8_t* buf, uint16_t bufsize)
32+
{
33+
uint32_t button = 0;
34+
35+
button |= ( CircuitPlayground.slideSwitch() ? 0x01 : 0x00 );
36+
button |= ( CircuitPlayground.leftButton() ? 0x02 : 0x00 );
37+
button |= ( CircuitPlayground.rightButton() ? 0x04 : 0x00 );
38+
39+
memcpy(buf, &button, 4);
40+
return 4;
41+
}
42+
43+
uint16_t measure_temperature(uint8_t* buf, uint16_t bufsize)
44+
{
45+
float temp = CircuitPlayground.temperature();
46+
memcpy(buf, &temp, 4);
47+
return 4;
48+
}
49+
50+
uint16_t measure_light_sensor(uint8_t* buf, uint16_t bufsize)
51+
{
52+
float lux = CircuitPlayground.lightSensor();
53+
memcpy(buf, &lux, 4);
54+
return 4;
55+
}
56+
57+
uint16_t measure_accel(uint8_t* buf, uint16_t bufsize)
58+
{
59+
float accel[3];
60+
accel[0] = CircuitPlayground.motionX();
61+
accel[1] = CircuitPlayground.motionY();
62+
accel[2] = CircuitPlayground.motionZ();
63+
64+
memcpy(buf, accel, sizeof(accel));
65+
return sizeof(accel);
66+
}
67+
68+
void setup()
69+
{
70+
Serial.begin(115200);
71+
//while ( !Serial ) delay(10); // for nrf52840 with native usb
72+
73+
Serial.println("Bluefruit52 BLEUART Example");
74+
Serial.println("---------------------------\n");
75+
76+
CircuitPlayground.begin();
77+
78+
// Setup the BLE LED to be enabled on CONNECT
79+
// Note: This is actually the default behaviour, but provided
80+
// here in case you want to control this LED manually via PIN 19
81+
Bluefruit.autoConnLed(false);
82+
83+
// Config the peripheral connection with maximum bandwidth
84+
// more SRAM required by SoftDevice
85+
// Note: All config***() function must be called before begin()
86+
Bluefruit.configPrphBandwidth(BANDWIDTH_MAX);
87+
88+
Bluefruit.begin();
89+
Bluefruit.setTxPower(4); // Check bluefruit.h for supported values
90+
Bluefruit.setName("Bluefruit52");
91+
//Bluefruit.setName(getMcuUniqueID()); // useful testing with multiple central connections
92+
Bluefruit.Periph.setConnectCallback(connect_callback);
93+
Bluefruit.Periph.setDisconnectCallback(disconnect_callback);
94+
95+
// To be consistent OTA DFU should be added first if it exists
96+
bledfu.begin();
97+
98+
// Configure and Start Device Information Service
99+
bledis.setManufacturer("Adafruit Industries");
100+
bledis.setModel("Bluefruit Feather52");
101+
bledis.begin();
102+
103+
// Configure and Start BLE Uart Service
104+
bleuart.begin();
105+
106+
// Start BLE Battery Service
107+
blebas.begin();
108+
blebas.write(100);
109+
110+
// Adafruit Service
111+
bleTemp.begin();
112+
bleTemp.setMeasureCallback(measure_temperature);
113+
114+
bleAccel.begin();
115+
bleAccel.setMeasureCallback(measure_accel);
116+
117+
bleLight.begin();
118+
bleLight.setMeasureCallback(measure_light_sensor);
119+
120+
bleButton.begin();
121+
bleButton.setMeasureCallback(measure_button);
122+
bleButton.setPeriod(0); // only notify if there is changes with buttons
123+
124+
// Set up and start advertising
125+
startAdv();
126+
127+
Serial.println("Please use Adafruit's Bluefruit LE app to connect in UART mode");
128+
Serial.println("Once connected, enter character(s) that you wish to send");
129+
}
130+
131+
void startAdv(void)
132+
{
133+
// Advertising packet
134+
Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
135+
Bluefruit.Advertising.addTxPower();
136+
137+
// Include bleuart 128-bit uuid
138+
Bluefruit.Advertising.addService(bleuart);
139+
140+
// Secondary Scan Response packet (optional)
141+
// Since there is no room for 'Name' in Advertising packet
142+
Bluefruit.ScanResponse.addName();
143+
144+
/* Start Advertising
145+
* - Enable auto advertising if disconnected
146+
* - Interval: fast mode = 20 ms, slow mode = 152.5 ms
147+
* - Timeout for fast mode is 30 seconds
148+
* - Start(timeout) with timeout = 0 will advertise forever (until connected)
149+
*
150+
* For recommended advertising interval
151+
* https://developer.apple.com/library/content/qa/qa1931/_index.html
152+
*/
153+
Bluefruit.Advertising.restartOnDisconnect(true);
154+
Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms
155+
Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode
156+
Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds
157+
}
158+
159+
void loop()
160+
{
161+
162+
}
163+
164+
// callback invoked when central connects
165+
void connect_callback(uint16_t conn_handle)
166+
{
167+
// Get the reference to current connection
168+
BLEConnection* connection = Bluefruit.Connection(conn_handle);
169+
170+
char central_name[32] = { 0 };
171+
connection->getPeerName(central_name, sizeof(central_name));
172+
173+
Serial.print("Connected to ");
174+
Serial.println(central_name);
175+
}
176+
177+
/**
178+
* Callback invoked when a connection is dropped
179+
* @param conn_handle connection where this event happens
180+
* @param reason is a BLE_HCI_STATUS_CODE which can be found in ble_hci.h
181+
*/
182+
void disconnect_callback(uint16_t conn_handle, uint8_t reason)
183+
{
184+
(void) conn_handle;
185+
(void) reason;
186+
187+
Serial.println();
188+
Serial.print("Disconnected, reason = 0x"); Serial.println(reason, HEX);
189+
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/* All Adafruit Service/Characteristic UUID128 share the same Base UUID:
2+
* ADAFxxx-C332-42A8-93BD-25E905756CB8
3+
*
4+
* Shared Characteristics
5+
* - Measurement Period 0001 | int32_t | Read + Write |
6+
* ms between measurements, -1: stop reading, 0: update when changes
7+
*
8+
* Temperature service 0100
9+
* - Temperature 0101 | float | Read + Notify | degree in Celsius
10+
* - Measurement Period 0001
11+
*
12+
* Accelerometer service 0200
13+
* - Accel Data 0201 | float (x, y, z) | Read + Notify | accel x, y, z
14+
* - Measurement Period 0001
15+
*
16+
* Light sensor service 0300
17+
* - Lux Data 0301 | uint16_t | Read + Notify
18+
* - Measurement Period 0001
19+
*
20+
* Button service 0600
21+
* - Pressed 0601 | uint32_t | Read + Notify | slide sw (b2), button B (b1), button A (b0)
22+
* - Measurement Period 0001
23+
*
24+
* Tone Service 0C00
25+
* - Frequency 0C01 | uint16_t | frequency in Hz
26+
* - Duration 0C01 |
27+
*
28+
* Sound sensor service 0400
29+
* - Sound Data 0401 | int16_t | Read + Notify
30+
* - Measurement Period 0001
31+
*
32+
* Captouch service 0500
33+
* - Sensitivity 0501 | uint32_t | difference that Capn should report immediately
34+
* - Cap0 0510 | int32_t | Read + Notify
35+
* - .........................
36+
* - Cap7 0517 | int32_t | Read + Notify
37+
* - Measurement Period 0001 | uint32_t | Read + Write
38+
*
39+
*
40+
* PIN I/O service 0700
41+
* - Pin Dir 0701 | uint64_t | Read + Write | bit 1 is In, 0 is Out
42+
* - Pin Data 0702 | uint64_t | Read + Write |
43+
* - PWM Control 0710 | struct { uint8_t pin, uint16_t value, uint32_t period_ms} | as microbit, value is 0-1024 for duty cycle
44+
*
45+
* PIN Analog 0800
46+
* - A0 0810 | uint16_t | Read + Notify
47+
* .........................
48+
* - A7 0817 | uint16_t | Read + Notify
49+
* - Measurement Period 0001 | uint32_t | Read + Write
50+
*
51+
* Neopixel Service 0900
52+
* - Pixel Pin 0901 | uint8_t | Read + Write
53+
* - Pixel Count 0902 | uint16_t | Read + Write
54+
* - Pixel Type 0903 | uint16_t | Read + Write | NEO_RGB, NEO_GRB etc ..
55+
* - Pixel Data 0904 | RGB array| Write
56+
*
57+
* Gamepad Service 0A00
58+
* - Gamepad Button 0A01 | uint16_t | Write
59+
* - Left XY 0A02 | int8_t[2] | Write
60+
* - Right XY 0A03 | int8_t[2] | Write
61+
*
62+
* Image Service 0B00
63+
* - Format 0B01 | uint16_t[3] | Write | width + height + type, type is enum value e.g RGB_655, RGB_888, JPG, GIF etc ...
64+
* - Data 0B02 | pixel[] | Write | will save to flash as `default_image`, and auto load on reset ?
65+
* - Fill 0B03 | uint8_t[3] | Write | Fill with solid color
66+
* - Load File 0B04 | utf8_t | Write | Load image from qspi flash ?
67+
*/
68+
69+
70+
71+
72+
{
73+
"_comment": {
74+
"comment": [
75+
"Add UUIDs for your services, characteristics, and descriptors in this file.",
76+
"The format is the UUID of the item, followed by an object containing the data.",
77+
"'name' is a mandatory handle, while 'format' is optional.",
78+
"Currently the only supported format is 'TEXT', default is 'NO_FORMAT'.",
79+
"The application must be reloaded to access changes in this file."
80+
]
81+
},
82+
"uuid16bitServiceDefinitions": {
83+
"180D": {
84+
"name": "Heart Rate"
85+
}
86+
},
87+
"uuid16bitCharacteristicDefinitions": {
88+
"2A37": {
89+
"name": "Heart Rate Measurement"
90+
},
91+
"2A29": {
92+
"name": "Manufacturer Name String",
93+
"format": "TEXT"
94+
}
95+
},
96+
"uuid16bitDescriptorDefinitions": {},
97+
"uuid128bitServiceDefinitions": {
98+
"6E400001B5A3F393E0A9E50E24DCCA9E": {
99+
"name": "UART over BLE"
100+
},
101+
102+
"ADAF0100C33242A893BD25E905756CB8": { "name": "Adafruit Temperature" },
103+
"ADAF0200C33242A893BD25E905756CB8": { "name": "Adafruit Accelerometer" },
104+
"ADAF0300C33242A893BD25E905756CB8": { "name": "Adafruit Light" },
105+
106+
"ADAF0400C33242A893BD25E905756CB8": { "name": "Adafruit Sound" },
107+
"ADAF0500C33242A893BD25E905756CB8": { "name": "Adafruit Capacitive Touch" },
108+
"ADAF0600C33242A893BD25E905756CB8": { "name": "Adafruit Buttons" }
109+
110+
"ADAF0700C33242A893BD25E905756CB8": { "name": "Adafruit Pin DIO" },
111+
"ADAF0800C33242A893BD25E905756CB8": { "name": "Adafruit Pin Analog" },
112+
"ADAF0900C33242A893BD25E905756CB8": { "name": "Adafruit RGB Pixel" }
113+
114+
"ADAF0A00C33242A893BD25E905756CB8": { "name": "Adafruit Gamepad" },
115+
"ADAF0B00C33242A893BD25E905756CB8": { "name": "Adafruit Image" },
116+
"ADAF0C00C33242A893BD25E905756CB8": { "name": "Adafruit Tone" }
117+
118+
},
119+
"uuid128bitCharacteristicDefinitions": {
120+
"6E400002B5A3F393E0A9E50E24DCCA9E": { "name": "UART RX" },
121+
"6E400003B5A3F393E0A9E50E24DCCA9E": { "name": "UART TX" },
122+
123+
"ADAF0001C33242A893BD25E905756CB8": { "name": "Measurement Period" },
124+
"ADAF0101C33242A893BD25E905756CB8": { "name": "Temperature" },
125+
"ADAF0201C33242A893BD25E905756CB8": { "name": "Accelerometer" },
126+
"ADAF0301C33242A893BD25E905756CB8": { "name": "Lux" }
127+
},
128+
"uuid128bitDescriptorDefinitions": {}
129+
}

0 commit comments

Comments
 (0)