add Neopixel special sketch that work with bluefruit LE app
This commit is contained in:
1 parent
01be1304b1
commit
ec678d4024
3 files changed
+280
-248
No files matched your search
@@ -0,0 +1,280 @@
|
||||
// Include Bluetooth
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <Adafruit_NeoPixel.h>
|
||||
#include <bluefruit.h>
|
||||
|
||||
// Neopixel
|
||||
#define PIN 23 /* Pin used to drive the NeoPixels */
|
||||
|
||||
#define MAXCOMPONENTS 4
|
||||
uint8_t *pixelBuffer = NULL;
|
||||
uint8_t width = 0;
|
||||
uint8_t height = 0;
|
||||
uint8_t components = 3; // only 3 and 4 are valid values
|
||||
uint8_t stride;
|
||||
|
||||
Adafruit_NeoPixel pixels = Adafruit_NeoPixel();
|
||||
|
||||
BLEUart bleuart;
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
Serial.println(F("Adafruit Bluefruit Neopixel Test"));
|
||||
Serial.println(F("------------------------------------"));
|
||||
|
||||
// Neopixels
|
||||
pixels.begin();
|
||||
|
||||
Bluefruit.begin();
|
||||
Bluefruit.setName("Bluefruit52");
|
||||
|
||||
// Configure and Start BLE Uart Service
|
||||
bleuart.begin();
|
||||
|
||||
// Set up Advertising Packet
|
||||
setupAdv();
|
||||
|
||||
// Start Advertising
|
||||
Bluefruit.Advertising.start();
|
||||
|
||||
}
|
||||
|
||||
void setupAdv(void)
|
||||
{
|
||||
//Bluefruit.Advertising.addTxPower();
|
||||
Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
|
||||
Bluefruit.Advertising.addTxPower();
|
||||
|
||||
// Include bleuart 128-bit uuid
|
||||
Bluefruit.Advertising.addService(bleuart);
|
||||
|
||||
// There is no room for Name in Advertising packet
|
||||
// Use Scan response for Name
|
||||
Bluefruit.ScanResponse.addName();
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
// Echo received data
|
||||
if ( Bluefruit.connected() && bleuart.notifyEnabled() )
|
||||
{
|
||||
int command = bleuart.read();
|
||||
|
||||
switch (command) {
|
||||
case 'V': { // Get Version
|
||||
commandVersion();
|
||||
break;
|
||||
}
|
||||
|
||||
case 'S': { // Setup dimensions, components, stride...
|
||||
commandSetup();
|
||||
break;
|
||||
}
|
||||
|
||||
case 'C': { // Clear with color
|
||||
commandClearColor();
|
||||
break;
|
||||
}
|
||||
|
||||
case 'B': { // Set Brightness
|
||||
commandSetBrightness();
|
||||
break;
|
||||
}
|
||||
|
||||
case 'P': { // Set Pixel
|
||||
commandSetPixel();
|
||||
break;
|
||||
}
|
||||
|
||||
case 'I': { // Receive new image
|
||||
commandImage();
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void swapBuffers()
|
||||
{
|
||||
uint8_t *base_addr = pixelBuffer;
|
||||
int pixelIndex = 0;
|
||||
for (int j = 0; j < height; j++)
|
||||
{
|
||||
for (int i = 0; i < width; i++) {
|
||||
if (components == 3) {
|
||||
pixels.setPixelColor(pixelIndex, pixels.Color(*base_addr, *(base_addr+1), *(base_addr+2)));
|
||||
}
|
||||
else {
|
||||
Serial.println(F("TODO: implement me"));
|
||||
}
|
||||
base_addr+=components;
|
||||
pixelIndex++;
|
||||
}
|
||||
pixelIndex += stride - width; // move pixelIndex to the next row (take into account the stride)
|
||||
}
|
||||
pixels.show();
|
||||
|
||||
}
|
||||
|
||||
void commandVersion() {
|
||||
Serial.println(F("Command: Version check"));
|
||||
sendResponse("Neopixel v1.0");
|
||||
}
|
||||
|
||||
void commandSetup() {
|
||||
Serial.println(F("Command: Setup"));
|
||||
|
||||
width = bleuart.read();
|
||||
height = bleuart.read();
|
||||
components = bleuart.read();
|
||||
stride = bleuart.read();
|
||||
neoPixelType pixelType;
|
||||
pixelType = bleuart.read();
|
||||
pixelType += bleuart.read()<<8;
|
||||
|
||||
Serial.printf("\tsize: %dx%d\n", width, height);
|
||||
Serial.printf("\tcomponents: %d\n", components);
|
||||
Serial.printf("\tstride: %d\n", stride);
|
||||
Serial.printf("\tpixelType %d\n", pixelType );
|
||||
|
||||
|
||||
if (pixelBuffer != NULL) {
|
||||
delete[] pixelBuffer;
|
||||
}
|
||||
|
||||
uint32_t size = width*height;
|
||||
pixelBuffer = new uint8_t[size*components];
|
||||
pixels.updateLength(size);
|
||||
pixels.updateType(pixelType);
|
||||
pixels.setPin(PIN);
|
||||
|
||||
// Done
|
||||
sendResponse("OK");
|
||||
}
|
||||
|
||||
void commandSetBrightness() {
|
||||
Serial.println(F("Command: SetBrightness"));
|
||||
|
||||
// Read value
|
||||
uint8_t brightness = bleuart.read();
|
||||
|
||||
// Set brightness
|
||||
pixels.setBrightness(brightness);
|
||||
|
||||
// Refresh pixels
|
||||
swapBuffers();
|
||||
|
||||
// Done
|
||||
sendResponse("OK");
|
||||
}
|
||||
|
||||
void commandClearColor() {
|
||||
Serial.println(F("Command: ClearColor"));
|
||||
|
||||
// Read color
|
||||
uint8_t color[MAXCOMPONENTS];
|
||||
for (int j = 0; j < components;) {
|
||||
if (bleuart.available()) {
|
||||
color[j] = bleuart.read();
|
||||
j++;
|
||||
}
|
||||
}
|
||||
|
||||
// Set all leds to color
|
||||
int size = width * height;
|
||||
uint8_t *base_addr = pixelBuffer;
|
||||
for (int i = 0; i < size; i++) {
|
||||
for (int j = 0; j < components; j++) {
|
||||
*base_addr = color[j];
|
||||
base_addr++;
|
||||
}
|
||||
}
|
||||
|
||||
// Swap buffers
|
||||
Serial.println(F("ClearColor completed"));
|
||||
swapBuffers();
|
||||
|
||||
|
||||
if (components == 3) {
|
||||
Serial.printf("\tcolor (%d, %d, %d)\n", color[0], color[1], color[2] );
|
||||
}
|
||||
|
||||
// Done
|
||||
sendResponse("OK");
|
||||
}
|
||||
|
||||
void commandSetPixel() {
|
||||
Serial.println(F("Command: SetPixel"));
|
||||
|
||||
// Read position
|
||||
uint8_t x = bleuart.read();
|
||||
uint8_t y = bleuart.read();
|
||||
|
||||
// Read colors
|
||||
uint32_t pixelIndex = y*width+x;
|
||||
uint32_t pixelComponentOffset = pixelIndex*components;
|
||||
uint8_t *base_addr = pixelBuffer+pixelComponentOffset;
|
||||
for (int j = 0; j < components;) {
|
||||
if (bleuart.available()) {
|
||||
*base_addr = bleuart.read();
|
||||
base_addr++;
|
||||
j++;
|
||||
}
|
||||
}
|
||||
|
||||
// Set colors
|
||||
if (components == 3) {
|
||||
uint32_t pixelIndex = y*stride+x;
|
||||
pixels.setPixelColor(pixelIndex, pixels.Color(pixelBuffer[pixelComponentOffset], pixelBuffer[pixelComponentOffset+1], pixelBuffer[pixelComponentOffset+2]));
|
||||
|
||||
Serial.printf("\tcolor (%d, %d, %d)\n", pixelBuffer[pixelComponentOffset], pixelBuffer[pixelComponentOffset+1], pixelBuffer[pixelComponentOffset+2] );
|
||||
}
|
||||
else {
|
||||
Serial.println(F("TODO: implement me"));
|
||||
}
|
||||
pixels.show();
|
||||
|
||||
// Done
|
||||
sendResponse("OK");
|
||||
}
|
||||
|
||||
void commandImage() {
|
||||
Serial.printf("Command: Image %dx%d, %d, %d\n", width, height, components, stride);
|
||||
|
||||
// Receive new pixel buffer
|
||||
int size = width * height;
|
||||
uint8_t *base_addr = pixelBuffer;
|
||||
for (int i = 0; i < size; i++) {
|
||||
for (int j = 0; j < components;) {
|
||||
if (bleuart.available()) {
|
||||
*base_addr = bleuart.read();
|
||||
base_addr++;
|
||||
j++;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
if (components == 3) {
|
||||
uint32_t index = i*components;
|
||||
Serial.printf("\tp%d (%d, %d, %d)\n", i, pixelBuffer[index], pixelBuffer[index+1], pixelBuffer[index+2] );
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
// Swap buffers
|
||||
Serial.println(F("Image received"));
|
||||
swapBuffers();
|
||||
|
||||
// Done
|
||||
sendResponse("OK");
|
||||
}
|
||||
|
||||
void sendResponse(char const *response) {
|
||||
Serial.printf("Send Response: %s\n", response);
|
||||
bleuart.write(response, strlen(response)*sizeof(char));
|
||||
}
|
||||
|
||||
@@ -1,115 +0,0 @@
|
||||
/*********************************************************************
|
||||
This is an example for our nRF51822 based Bluefruit LE modules
|
||||
|
||||
Pick one up today in the adafruit shop!
|
||||
|
||||
Adafruit invests time and resources providing this open source code,
|
||||
please support Adafruit and open-source hardware by purchasing
|
||||
products from Adafruit!
|
||||
|
||||
MIT license, check LICENSE for more information
|
||||
All text above, and the splash screen below must be included in
|
||||
any redistribution
|
||||
*********************************************************************/
|
||||
|
||||
#include <string.h>
|
||||
#include <Arduino.h>
|
||||
#include <bluefruit.h>
|
||||
#include <Adafruit_NeoPixel.h>
|
||||
|
||||
/*=========================================================================
|
||||
PIN Which pin on the Arduino is connected to the NeoPixels?
|
||||
NUMPIXELS How many NeoPixels are attached to the Arduino?
|
||||
-----------------------------------------------------------------------*/
|
||||
#define FACTORYRESET_ENABLE 1
|
||||
|
||||
#define PIN 5
|
||||
#define NUMPIXELS 1
|
||||
/*=========================================================================*/
|
||||
|
||||
Adafruit_NeoPixel pixel = Adafruit_NeoPixel(NUMPIXELS, PIN);
|
||||
|
||||
BLEUart bleuart;
|
||||
|
||||
// function prototypes over in packetparser.cpp
|
||||
uint8_t readPacket(BLEUart *ble_uart, uint16_t timeout);
|
||||
float parsefloat(uint8_t *buffer);
|
||||
void printHex(const uint8_t * data, const uint32_t numBytes);
|
||||
|
||||
// the packet buffer
|
||||
extern uint8_t packetbuffer[];
|
||||
|
||||
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@brief Sets up the HW an the BLE module (this function is called
|
||||
automatically on startup)
|
||||
*/
|
||||
/**************************************************************************/
|
||||
void setup(void)
|
||||
{
|
||||
Serial.begin(115200);
|
||||
Serial.println(F("Adafruit Bluefruit52 App Controller Example"));
|
||||
Serial.println(F("-------------------------------------------"));
|
||||
|
||||
Bluefruit.begin();
|
||||
Bluefruit.setName("Bluefruit52");
|
||||
|
||||
// Configure and Start BLE Uart Service
|
||||
bleuart.begin();
|
||||
|
||||
// Set up Advertising Packet
|
||||
setupAdv();
|
||||
|
||||
// Start Advertising
|
||||
Bluefruit.Advertising.start();
|
||||
}
|
||||
|
||||
void setupAdv(void)
|
||||
{
|
||||
//Bluefruit.Advertising.addTxPower();
|
||||
Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
|
||||
Bluefruit.Advertising.addTxPower();
|
||||
|
||||
// Include bleuart 128-bit uuid
|
||||
Bluefruit.Advertising.addService(bleuart);
|
||||
|
||||
// There is no room for Name in Advertising packet
|
||||
// Use Scan response for Name
|
||||
Bluefruit.ScanResponse.addName();
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@brief Constantly poll for new command or response data
|
||||
*/
|
||||
/**************************************************************************/
|
||||
void loop(void)
|
||||
{
|
||||
/* Wait for new data to arrive */
|
||||
uint8_t len = readPacket(&bleuart, 500);
|
||||
if (len == 0) return;
|
||||
|
||||
/* Got a packet! */
|
||||
// printHex(packetbuffer, len);
|
||||
|
||||
// Color
|
||||
if (packetbuffer[1] == 'C') {
|
||||
uint8_t red = packetbuffer[2];
|
||||
uint8_t green = packetbuffer[3];
|
||||
uint8_t blue = packetbuffer[4];
|
||||
Serial.print ("RGB #");
|
||||
if (red < 0x10) Serial.print("0");
|
||||
Serial.print(red, HEX);
|
||||
if (green < 0x10) Serial.print("0");
|
||||
Serial.print(green, HEX);
|
||||
if (blue < 0x10) Serial.print("0");
|
||||
Serial.println(blue, HEX);
|
||||
|
||||
for(uint8_t i=0; i<NUMPIXELS; i++) {
|
||||
pixel.setPixelColor(i, pixel.Color(red,green,blue));
|
||||
}
|
||||
pixel.show(); // This sends the updated pixel color to the hardware.
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,133 +0,0 @@
|
||||
#include <string.h>
|
||||
#include <Arduino.h>
|
||||
#include <bluefruit.h>
|
||||
|
||||
|
||||
#define PACKET_ACC_LEN (15)
|
||||
#define PACKET_GYRO_LEN (15)
|
||||
#define PACKET_MAG_LEN (15)
|
||||
#define PACKET_QUAT_LEN (19)
|
||||
#define PACKET_BUTTON_LEN (5)
|
||||
#define PACKET_COLOR_LEN (6)
|
||||
#define PACKET_LOCATION_LEN (15)
|
||||
|
||||
// READ_BUFSIZE Size of the read buffer for incoming packets
|
||||
#define READ_BUFSIZE (20)
|
||||
|
||||
|
||||
/* Buffer to hold incoming characters */
|
||||
uint8_t packetbuffer[READ_BUFSIZE+1];
|
||||
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@brief Casts the four bytes at the specified address to a float
|
||||
*/
|
||||
/**************************************************************************/
|
||||
float parsefloat(uint8_t *buffer)
|
||||
{
|
||||
float f = ((float *)buffer)[0];
|
||||
return f;
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@brief Prints a hexadecimal value in plain characters
|
||||
@param data Pointer to the byte data
|
||||
@param numBytes Data length in bytes
|
||||
*/
|
||||
/**************************************************************************/
|
||||
void printHex(const uint8_t * data, const uint32_t numBytes)
|
||||
{
|
||||
uint32_t szPos;
|
||||
for (szPos=0; szPos < numBytes; szPos++)
|
||||
{
|
||||
Serial.print(F("0x"));
|
||||
// Append leading 0 for small values
|
||||
if (data[szPos] <= 0xF)
|
||||
{
|
||||
Serial.print(F("0"));
|
||||
Serial.print(data[szPos] & 0xf, HEX);
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.print(data[szPos] & 0xff, HEX);
|
||||
}
|
||||
// Add a trailing space if appropriate
|
||||
if ((numBytes > 1) && (szPos != numBytes - 1))
|
||||
{
|
||||
Serial.print(F(" "));
|
||||
}
|
||||
}
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@brief Waits for incoming data and parses it
|
||||
*/
|
||||
/**************************************************************************/
|
||||
uint8_t readPacket(BLEUart *ble_uart, uint16_t timeout)
|
||||
{
|
||||
uint16_t origtimeout = timeout, replyidx = 0;
|
||||
|
||||
memset(packetbuffer, 0, READ_BUFSIZE);
|
||||
|
||||
while (timeout--) {
|
||||
if (replyidx >= 20) break;
|
||||
if ((packetbuffer[1] == 'A') && (replyidx == PACKET_ACC_LEN))
|
||||
break;
|
||||
if ((packetbuffer[1] == 'G') && (replyidx == PACKET_GYRO_LEN))
|
||||
break;
|
||||
if ((packetbuffer[1] == 'M') && (replyidx == PACKET_MAG_LEN))
|
||||
break;
|
||||
if ((packetbuffer[1] == 'Q') && (replyidx == PACKET_QUAT_LEN))
|
||||
break;
|
||||
if ((packetbuffer[1] == 'B') && (replyidx == PACKET_BUTTON_LEN))
|
||||
break;
|
||||
if ((packetbuffer[1] == 'C') && (replyidx == PACKET_COLOR_LEN))
|
||||
break;
|
||||
if ((packetbuffer[1] == 'L') && (replyidx == PACKET_LOCATION_LEN))
|
||||
break;
|
||||
|
||||
while (ble_uart->available()) {
|
||||
char c = ble_uart->read();
|
||||
if (c == '!') {
|
||||
replyidx = 0;
|
||||
}
|
||||
packetbuffer[replyidx] = c;
|
||||
replyidx++;
|
||||
timeout = origtimeout;
|
||||
}
|
||||
|
||||
if (timeout == 0) break;
|
||||
delay(1);
|
||||
}
|
||||
|
||||
packetbuffer[replyidx] = 0; // null term
|
||||
|
||||
if (!replyidx) // no data or timeout
|
||||
return 0;
|
||||
if (packetbuffer[0] != '!') // doesn't start with '!' packet beginning
|
||||
return 0;
|
||||
|
||||
// check checksum!
|
||||
uint8_t xsum = 0;
|
||||
uint8_t checksum = packetbuffer[replyidx-1];
|
||||
|
||||
for (uint8_t i=0; i<replyidx-1; i++) {
|
||||
xsum += packetbuffer[i];
|
||||
}
|
||||
xsum = ~xsum;
|
||||
|
||||
// Throw an error message if the checksum's don't match
|
||||
if (xsum != checksum)
|
||||
{
|
||||
Serial.print("Checksum mismatch in packet : ");
|
||||
printHex(packetbuffer, replyidx+1);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// checksum passed!
|
||||
return replyidx;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user