Skip to content

Commit 4c89a18

Browse files
committed
Add test sketches
1 parent ade23f0 commit 4c89a18

File tree

11 files changed

+1833
-0
lines changed

11 files changed

+1833
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright (c) 2016 Intel Corporation. All rights reserved.
3+
* See the bottom of this file for the license terms.
4+
*/
5+
6+
#include "CurieBLE.h"
7+
8+
const int ledPin = 13; // set ledPin to use on-board LED
9+
BLEPeripheral blePeripheral; // create peripheral instance
10+
11+
BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // create service
12+
13+
// create switch characteristic and allow remote device to read and write
14+
BLECharCharacteristic switchChar("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
15+
16+
void setup() {
17+
Serial.begin(9600);
18+
pinMode(ledPin, OUTPUT); // use the LED on pin 13 as an output
19+
20+
// set the local name peripheral advertises
21+
blePeripheral.setLocalName("LEDCB");
22+
// set the UUID for the service this peripheral advertises
23+
blePeripheral.setAdvertisedServiceUuid(ledService.uuid());
24+
25+
// add service and characteristic
26+
blePeripheral.addAttribute(ledService);
27+
blePeripheral.addAttribute(switchChar);
28+
29+
// assign event handlers for connected, disconnected to peripheral
30+
blePeripheral.setEventHandler(BLEConnected, blePeripheralConnectHandler);
31+
blePeripheral.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler);
32+
33+
// assign event handlers for characteristic
34+
switchChar.setEventHandler(BLEWritten, switchCharacteristicWritten);
35+
// set an initial value for the characteristic
36+
switchChar.setValue(0);
37+
38+
// advertise the service
39+
blePeripheral.begin();
40+
Serial.println(("Bluetooth device active, waiting for connections..."));
41+
}
42+
43+
void loop() {
44+
// poll peripheral
45+
blePeripheral.poll();
46+
}
47+
48+
void blePeripheralConnectHandler(BLECentral& central) {
49+
// central connected event handler
50+
Serial.print("Connected event, central: ");
51+
Serial.println(central.address());
52+
}
53+
54+
void blePeripheralDisconnectHandler(BLECentral& central) {
55+
// central disconnected event handler
56+
Serial.print("Disconnected event, central: ");
57+
Serial.println(central.address());
58+
}
59+
60+
void switchCharacteristicWritten(BLEDevice central, BLECharacteristic characteristic) {
61+
// central wrote new value to characteristic, update LED
62+
Serial.print("Characteristic event, written: ");
63+
64+
if (switchChar.value()) {
65+
Serial.println("LED on");
66+
digitalWrite(ledPin, HIGH);
67+
} else {
68+
Serial.println("LED off");
69+
digitalWrite(ledPin, LOW);
70+
}
71+
}
72+
73+
/*
74+
Copyright (c) 2016 Intel Corporation. All rights reserved.
75+
76+
This library is free software; you can redistribute it and/or
77+
modify it under the terms of the GNU Lesser General Public
78+
License as published by the Free Software Foundation; either
79+
version 2.1 of the License, or (at your option) any later version.
80+
81+
This library is distributed in the hope that it will be useful,
82+
but WITHOUT ANY WARRANTY; without even the implied warranty of
83+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
84+
Lesser General Public License for more details.
85+
86+
You should have received a copy of the GNU Lesser General Public
87+
License along with this library; if not, write to the Free Software
88+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-
89+
1301 USA
90+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#include <CurieBLE.h>
2+
#include "CurieIMU.h"
3+
4+
5+
BLEPeripheral blePeripheral; // BLE Peripheral Device (the board you're programming)
6+
BLEService accelService("19B10010-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service
7+
8+
// BLE accelerometer Characteristic - custom 128-bit UUID, read by central
9+
BLEFloatCharacteristic accelX("19B10011-E8F2-537E-4F6C-D104768A1214", BLERead | BLENotify);
10+
BLEFloatCharacteristic accelY("19B10012-E8F2-537E-4F6C-D104768A1214", BLERead | BLENotify);
11+
BLEFloatCharacteristic accelZ("19B10013-E8F2-537E-4F6C-D104768A1214", BLERead | BLENotify);
12+
13+
long lastUpdate = 0;
14+
15+
void setup() {
16+
Serial.begin(9600);
17+
18+
// set advertised local name and service UUID:
19+
blePeripheral.setLocalName("tigoeAcc");
20+
blePeripheral.setAdvertisedServiceUuid(accelService.uuid());
21+
22+
// add service and characteristic:
23+
blePeripheral.addAttribute(accelService);
24+
blePeripheral.addAttribute(accelX);
25+
blePeripheral.addAttribute(accelY);
26+
blePeripheral.addAttribute(accelZ);
27+
28+
CurieIMU.begin();
29+
30+
// Set the accelerometer range to 2G
31+
CurieIMU.setAccelerometerRange(2);
32+
// set the initial value for the characeristic:
33+
accelX.setValue(0);
34+
accelY.setValue(0);
35+
accelZ.setValue(0);
36+
37+
// begin advertising BLE service:
38+
blePeripheral.begin();
39+
pinMode(13, OUTPUT);
40+
Serial.println("Starting");
41+
}
42+
43+
void loop() {
44+
// listen for BLE peripherals to connect:
45+
BLECentral central = blePeripheral.central();
46+
47+
// if a central is connected to peripheral:
48+
if (central) {
49+
digitalWrite(13, HIGH);
50+
Serial.print("Connected to central: ");
51+
// print the central's MAC address:
52+
Serial.println(central.address());
53+
54+
// while the central is still connected to peripheral:
55+
while (central.connected()) {
56+
long now = millis();
57+
if (now - lastUpdate > 1000) {
58+
updateAccelerometer();
59+
lastUpdate = now;
60+
}
61+
}
62+
// when the central disconnects, print it out:
63+
Serial.print("Disconnected from central: ");
64+
Serial.println(central.address());
65+
digitalWrite(13, LOW);
66+
67+
}
68+
}
69+
70+
void updateAccelerometer() {
71+
int axRaw, ayRaw, azRaw; // raw accelerometer values
72+
float ax, ay, az;
73+
74+
// read raw accelerometer measurements from device
75+
CurieIMU.readAccelerometer(axRaw, ayRaw, azRaw);
76+
77+
// convert the raw accelerometer data to G's
78+
ax = convertRawAcceleration(axRaw);
79+
ay = convertRawAcceleration(ayRaw);
80+
az = convertRawAcceleration(azRaw);
81+
82+
accelX.setValue(ax);
83+
accelY.setValue(ay);
84+
accelZ.setValue(az);
85+
}
86+
87+
float convertRawAcceleration(int aRaw) {
88+
// since we are using 2G range
89+
// -2g maps to a raw value of -32768
90+
// +2g maps to a raw value of 32767
91+
92+
float a = (aRaw * 2.0) / 32768.0;
93+
return a;
94+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
Copyright (c) 2015 Intel Corporation. All rights reserved.
3+
4+
This library is free software; you can redistribute it and/or
5+
modify it under the terms of the GNU Lesser General Public
6+
License as published by the Free Software Foundation; either
7+
version 2.1 of the License, or (at your option) any later version.
8+
9+
This library is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
Lesser General Public License for more details.
13+
14+
You should have received a copy of the GNU Lesser General Public
15+
License along with this library; if not, write to the Free Software
16+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17+
*/
18+
19+
/*
20+
This sketch example partially implements the standard Bluetooth Low-Energy Heart Rate service.
21+
For more information: https://developer.bluetooth.org/gatt/services/Pages/ServicesHome.aspx
22+
*/
23+
24+
#include <CurieBLE.h>
25+
26+
BLEPeripheral blePeripheral; // BLE Peripheral Device (the board you're programming)
27+
BLEService heartRateService("180D"); // BLE Heart Rate Service
28+
29+
// BLE Heart Rate Measurement Characteristic"
30+
BLECharacteristic heartRateChar("2A37", // standard 16-bit characteristic UUID
31+
BLERead | BLENotify, 2); // remote clients will be able to get notifications if this characteristic changes
32+
// the characteristic is 2 bytes long as the first field needs to be "Flags" as per BLE specifications
33+
// https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml
34+
35+
int oldHeartRate = 0; // last heart rate reading from analog input
36+
long previousMillis = 0; // last time the heart rate was checked, in ms
37+
38+
void setup() {
39+
Serial.begin(9600); // initialize serial communication
40+
pinMode(13, OUTPUT); // initialize the LED on pin 13 to indicate when a central is connected
41+
42+
/* Set a local name for the BLE device
43+
This name will appear in advertising packets
44+
and can be used by remote devices to identify this BLE device
45+
The name can be changed but maybe be truncated based on space left in advertisement packet */
46+
blePeripheral.setLocalName("HeartRateSketch");
47+
blePeripheral.setAdvertisedServiceUuid(heartRateService.uuid()); // add the service UUID
48+
blePeripheral.addAttribute(heartRateService); // Add the BLE Heart Rate service
49+
blePeripheral.addAttribute(heartRateChar); // add the Heart Rate Measurement characteristic
50+
51+
/* Now activate the BLE device. It will start continuously transmitting BLE
52+
advertising packets and will be visible to remote BLE central devices
53+
until it receives a new connection */
54+
blePeripheral.begin();
55+
Serial.println("Bluetooth device active, waiting for connections...");
56+
}
57+
58+
void loop() {
59+
// listen for BLE peripherals to connect:
60+
BLECentral central = blePeripheral.central();
61+
62+
// if a central is connected to peripheral:
63+
if (central) {
64+
Serial.print("Connected to central: ");
65+
// print the central's MAC address:
66+
Serial.println(central.address());
67+
// turn on the LED to indicate the connection:
68+
digitalWrite(13, HIGH);
69+
70+
// check the heart rate measurement every 200ms
71+
// as long as the central is still connected:
72+
while (central.connected()) {
73+
long currentMillis = millis();
74+
// if 200ms have passed, check the heart rate measurement:
75+
if (currentMillis - previousMillis >= 200) {
76+
previousMillis = currentMillis;
77+
updateHeartRate();
78+
}
79+
}
80+
// when the central disconnects, turn off the LED:
81+
digitalWrite(13, LOW);
82+
Serial.print("Disconnected from central: ");
83+
Serial.println(central.address());
84+
}
85+
}
86+
87+
void updateHeartRate() {
88+
/* Read the current voltage level on the A0 analog input pin.
89+
This is used here to simulate the heart rate's measurement.
90+
*/
91+
int heartRateMeasurement = analogRead(A0);
92+
int heartRate = map(heartRateMeasurement, 0, 1023, 0, 100);
93+
if (heartRate != oldHeartRate) { // if the heart rate has changed
94+
Serial.print("Heart Rate is now: "); // print it
95+
Serial.println(heartRate);
96+
const unsigned char heartRateCharArray[2] = { 0, (char)heartRate };
97+
heartRateChar.setValue(heartRateCharArray, 2); // and update the heart rate measurement characteristic
98+
oldHeartRate = heartRate; // save the level for next comparison
99+
}
100+
}

0 commit comments

Comments
 (0)