Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Lightblue Bean Serial/Scratch speed tests with and without accelerometer
uint8_t buffer[20];
String msg = "ABCDEFGHIJKLMNOPQRS";
unsigned long startTest = 20000;
unsigned long stopTest = 40000;
void setup() {
pinMode(A3, INPUT);
msg.toCharArray((char *)buffer,20);
}
void loop() {
unsigned long now = millis();
if (now > startTest && now < stopTest) {
AccelerationReading reading = Bean.getAcceleration();
Bean.setScratchData(1, buffer, 20);
}
delay(10);
}
int prevButtonState = LOW;
bool isEnabled = false;
uint8_t buffer[20];
String msg = "ABCDEFGHIJKLMNOPQRS";
void setup() {
pinMode(A3, INPUT);
msg.toCharArray((char *)buffer,20);
}
void loop() {
int buttonState = digitalRead(A3);
if (buttonState == HIGH && prevButtonState == LOW) {
isEnabled = !isEnabled;
}
prevButtonState = buttonState;
if (isEnabled) {
AccelerationReading reading = Bean.getAcceleration();
Bean.setScratchData(1, buffer, 20);
}
delay(10);
}
int prevButtonState = LOW;
bool isEnabled = false;
uint8_t buffer[20];
String msg = "ABCDEFGHIJKLMNOPQRS";
void setup() {
pinMode(A3, INPUT);
msg.toCharArray((char *)buffer,20);
}
void loop() {
int buttonState = digitalRead(A3);
if (buttonState == HIGH && prevButtonState == LOW) {
isEnabled = !isEnabled;
}
prevButtonState = buttonState;
if (isEnabled) {
AccelerationReading reading = Bean.getAcceleration();
Bean.setScratchData(1, buffer, 20);
}
delay(10);
}
void setup() {
Serial.begin(115200);
pinMode(A3, INPUT);
}
int prevButtonState = LOW;
bool isEnabled = false;
void loop() {
int buttonState = digitalRead(A3);
if (buttonState == HIGH && prevButtonState == LOW) {
isEnabled = !isEnabled;
}
prevButtonState = buttonState;
if (isEnabled) {
AccelerationReading reading = Bean.getAcceleration();
Serial.println("ABCDEFGHIJKLMNOPQR");
}
delay(10);
}
void setup() {
Serial.begin(115200);
pinMode(A3, INPUT);
}
int prevButtonState = LOW;
bool isEnabled = false;
void loop() {
int buttonState = digitalRead(A3);
if (buttonState == HIGH && prevButtonState == LOW) {
isEnabled = !isEnabled;
}
prevButtonState = buttonState;
if (isEnabled) {
Serial.println("1234567890123456789");
}
delay(10);
}
/*jslint node: true */
"use strict";
var Bean = require('ble-bean');
var intervalId;
var connectedBean;
var bytes = 0;
var msgs = 0;
function printReport() {
console.log(msgs + "," + bytes);
msgs = 0;
bytes = 0;
}
var SCRATCH_UUID = 'a495ff20c5b14b44b5121370f02d74de';
var SCRATCH_ONE = 'a495ff21c5b14b44b5121370f02d74de';
function onRead(buf) {
var bufStr = buf.toString('ascii');
if (bufStr !== 'ABCDEFGHIJKLMNOPQRS\0') {
console.log("Unexpected message " + bufStr);
}
msgs++;
bytes += buf.length;
}
Bean.discover(function(bean){
connectedBean = bean;
console.log("discover " + bean);
process.on('SIGINT', exitHandler.bind(this));
bean.on("disconnect", function(){
console.log("disconnect");
process.exit();
});
bean.connectAndSetup(function(){
console.log("connect " + bean);
bean.notifyCharacteristic(SCRATCH_UUID, SCRATCH_ONE, true, onRead, function(err) {
if (err) throw err;
});
setInterval(printReport, 1000);
});
});
process.stdin.resume();//so the program will not close instantly
var triedToExit = false;
//turns off led before disconnecting
var exitHandler = function exitHandler() {
var self = this;
if (connectedBean && !triedToExit) {
triedToExit = true;
console.log('Turning off led...');
clearInterval(intervalId);
connectedBean.setColor(new Buffer([0x0,0x0,0x0]), function(){});
//no way to know if succesful but often behind other commands going out, so just wait 2 seconds
console.log('Disconnecting from Device...');
setTimeout(connectedBean.disconnect.bind(connectedBean, function(){}), 2000);
} else {
process.exit();
}
};
/*jslint node: true */
"use strict";
var Bean = require('ble-bean');
var intervalId;
var connectedBean;
var bytes = 0;
var msgs = 0;
function printReport() {
console.log(msgs + "," + bytes);
msgs = 0;
bytes = 0;
}
function toHex(str) {
var hex = [];
for(var i=0;i<str.length;i++) {
hex.push(str.charCodeAt(i).toString(16));
}
return hex.join(' ');
}
Bean.discover(function(bean){
connectedBean = bean;
console.log("discover " + bean);
process.on('SIGINT', exitHandler.bind(this));
bean.on("serial", function(data, valid){
msgs++;
var dataStr = data.toString();
if (dataStr !== 'ABCDEFGHIJKLMNOPQR' && dataStr !== '\n' && dataStr !== '\r') {
console.log('Unexpected data ' + toHex(dataStr))
}
bytes += dataStr.length;
});
bean.on("disconnect", function(){
console.log("disconnect");
process.exit();
});
bean.connectAndSetup(function(){
console.log("connect " + bean);
setInterval(printReport, 1000);
});
});
process.stdin.resume();//so the program will not close instantly
var triedToExit = false;
//turns off led before disconnecting
var exitHandler = function exitHandler() {
var self = this;
if (connectedBean && !triedToExit) {
triedToExit = true;
console.log('Turning off led...');
clearInterval(intervalId);
connectedBean.setColor(new Buffer([0x0,0x0,0x0]), function(){});
//no way to know if succesful but often behind other commands going out, so just wait 2 seconds
console.log('Disconnecting from Device...');
setTimeout(connectedBean.disconnect.bind(connectedBean, function(){}), 2000);
} else {
process.exit();
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment