116 lines
3.4 KiB
Arduino
116 lines
3.4 KiB
Arduino
/*********************************************************************
|
|
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.start();
|
|
|
|
// Set up Advertising Packet
|
|
setupAdv();
|
|
|
|
// Start Advertising
|
|
Bluefruit.startAdvertising();
|
|
}
|
|
|
|
void setupAdv(void)
|
|
{
|
|
//Bluefruit.addAdvTxPower();
|
|
Bluefruit.addAdvFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
|
|
Bluefruit.addAdvTxPower();
|
|
|
|
// Include bleuart 128-bit uuid
|
|
Bluefruit.addAdvService(bleuart);
|
|
|
|
// There is no room for Name in Advertising packet
|
|
// Use Scan response for Name
|
|
Bluefruit.addScanRespName();
|
|
}
|
|
|
|
/**************************************************************************/
|
|
/*!
|
|
@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.
|
|
}
|
|
|
|
}
|