319 lines
7.5 KiB
Arduino
319 lines
7.5 KiB
Arduino
/*********************************************************************
|
|
This is an example for our nRF52 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
|
|
*********************************************************************/
|
|
|
|
// This sketch is intended to be used with the NeoPixel control
|
|
// surface in Adafruit's Bluefruit LE Connect mobile application.
|
|
//
|
|
// - Compile and flash this sketch to the nRF52 Feather
|
|
// - Open the Bluefruit LE Connect app
|
|
// - Switch to the NeoPixel utility
|
|
// - Click the 'connect' button to establish a connection and
|
|
// send the meta-data about the pixel layout
|
|
// - Use the NeoPixel utility to update the pixels on your device
|
|
|
|
/* NOTE: This sketch required at least version 1.1.0 of Adafruit_Neopixel !!! */
|
|
|
|
#include <Arduino.h>
|
|
#include <Adafruit_NeoPixel.h>
|
|
#include <bluefruit.h>
|
|
|
|
#define PIN 30 /* 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();
|
|
|
|
// BLE Service
|
|
BLEDis bledis;
|
|
BLEUart bleuart;
|
|
|
|
void setup()
|
|
{
|
|
Serial.begin(115200);
|
|
Serial.println("Adafruit Bluefruit Neopixel Test");
|
|
Serial.println("--------------------------------");
|
|
|
|
Serial.println();
|
|
Serial.println("Please connect using the Bluefruit Connect LE application");
|
|
|
|
// Config Neopixels
|
|
pixels.begin();
|
|
|
|
// Init Bluefruit
|
|
Bluefruit.begin();
|
|
Bluefruit.setName("Bluefruit52");
|
|
Bluefruit.setConnectCallback(connect_callback);
|
|
|
|
// Configure and Start Device Information Service
|
|
bledis.setManufacturer("Adafruit Industries");
|
|
bledis.setModel("Bluefruit Feather52");
|
|
bledis.begin();
|
|
|
|
// Configure and start BLE UART service
|
|
bleuart.begin();
|
|
|
|
// Set up the 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 connect_callback(void)
|
|
{
|
|
Serial.println("Connected !! Please select the 'Neopixels' tab, click 'Connect' and have fun");
|
|
}
|
|
|
|
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));
|
|
}
|
|
|