diff --git a/neopixel_bluefruit/BluefruitConfig.h b/neopixel_bluefruit/BluefruitConfig.h index f63b427..e0cf970 100644 --- a/neopixel_bluefruit/BluefruitConfig.h +++ b/neopixel_bluefruit/BluefruitConfig.h @@ -4,7 +4,7 @@ // ---------------------------------------------------------------------------------------------- #define BUFSIZE 128 // Size of the read buffer for incoming data #define VERBOSE_MODE true // If set to 'true' enables debug output -#define BLE_READPACKET_TIMEOUT 500 // Timeout in ms waiting to read a response +#define BLE_READPACKET_TIMEOUT 5 // Timeout in ms waiting to read a response // SOFTWARE UART SETTINGS @@ -12,10 +12,10 @@ // The following macros declare the pins that will be used for 'SW' serial. // You should use this option if you are connecting the UART Friend to an UNO // ---------------------------------------------------------------------------------------------- -#define BLUEFRUIT_SWUART_RXD_PIN 9 // Required for software serial! -#define BLUEFRUIT_SWUART_TXD_PIN 10 // Required for software serial! -#define BLUEFRUIT_UART_CTS_PIN -1//11 // Required for software serial! -#define BLUEFRUIT_UART_RTS_PIN -1 // Optional, set to -1 if unused +//#define BLUEFRUIT_SWUART_RXD_PIN 9 // Required for software serial! +//#define BLUEFRUIT_SWUART_TXD_PIN 10 // Required for software serial! +//#define BLUEFRUIT_UART_CTS_PIN -1//11 // Required for software serial! +//#define BLUEFRUIT_UART_RTS_PIN -1 // Optional, set to -1 if unused // HARDWARE UART SETTINGS diff --git a/neopixel_bluefruit/neopixel_bluefruit.ino b/neopixel_bluefruit/neopixel_bluefruit.ino index 4c447ce..16f3298 100644 --- a/neopixel_bluefruit/neopixel_bluefruit.ino +++ b/neopixel_bluefruit/neopixel_bluefruit.ino @@ -1,3 +1,31 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + /********************************************************************* This is an example for our nRF51822 based Bluefruit LE modules @@ -15,7 +43,6 @@ #include #include #include -#include #include "Adafruit_BLE.h" #include "Adafruit_BluefruitLE_SPI.h" #include "Adafruit_BluefruitLE_UART.h" @@ -55,36 +82,50 @@ -----------------------------------------------------------------------*/ #define FACTORYRESET_ENABLE 1 - #define PIN 9 - #define NUMPIXELS 10 +/*-----------------------------------------------------------------------*/ +#define STRING_PIN 9 +#define ONBOARD_PIN 8 +#define NUM_LED_STRING 9 +#define NUM_LED_ONBOARD 1 +#define NUM_LED NUM_LED_STRING + NUM_LED_ONBOARD + +#define MIN_BRIGHTNESS 10 +#define MAX_BRIGHTNESS 200 +#define HUE_SPREAD 15 +#define HUE_RATE_MODULO 10 //96 // increasing this value makes the hue change slower +#define BT_CHECK_CONN_MODULO 20 +#define MIN_SATURATION 192 +#define MAX_SATURATION 255 +#define MIN_RATE 1 +#define MAX_RATE 3 +#define SPEED 20 + +CRGB leds[NUM_LED]; + +byte clock = 0; +byte globalHue = 0; +byte hues[NUM_LED + 1]; +int brightnesses[NUM_LED + 1]; +int saturations[NUM_LED + 1]; +int rates[NUM_LED + 1]; +bool isInDataMode = false; +bool fixedHue = false; /*=========================================================================*/ -Adafruit_NeoPixel pixel = Adafruit_NeoPixel(NUMPIXELS, PIN); - -// Create the bluefruit object, either software serial...uncomment these lines -/* -SoftwareSerial bluefruitSS = SoftwareSerial(BLUEFRUIT_SWUART_TXD_PIN, BLUEFRUIT_SWUART_RXD_PIN); - -Adafruit_BluefruitLE_UART ble(bluefruitSS, BLUEFRUIT_UART_MODE_PIN, - BLUEFRUIT_UART_CTS_PIN, BLUEFRUIT_UART_RTS_PIN); -*/ - -/* ...or hardware serial, which does not need the RTS/CTS pins. Uncomment this line */ +// Create the bluefruit object Adafruit_BluefruitLE_UART ble(BLUEFRUIT_HWSERIAL_NAME, BLUEFRUIT_UART_MODE_PIN); -/* ...hardware SPI, using SCK/MOSI/MISO hardware SPI pins and then user selected CS/IRQ/RST */ -//Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_CS, BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST); - -/* ...software SPI, using SCK/MOSI/MISO user-defined SPI pins and then user selected CS/IRQ/RST */ -//Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_SCK, BLUEFRUIT_SPI_MISO, -// BLUEFRUIT_SPI_MOSI, BLUEFRUIT_SPI_CS, -// BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST); - - // A small helper void error(const __FlashStringHelper*err) { Serial.println(err); - while (1); + + while (1) { + // flash red led + digitalWrite(7, HIGH); // turn the LED on (HIGH is the voltage level) + delay(1000); // wait 1 second + digitalWrite(7, LOW); // turn the LED off by making the voltage LOW + delay(1000); // wait 1 second + } } // function prototypes over in packetparser.cpp @@ -107,13 +148,15 @@ void setup(void) //while (!Serial); // required for Flora & Micro delay(500); - // turn off neopixel - pixel.begin(); // This initializes the NeoPixel library. - for(uint8_t i=0; i(leds, NUM_LED_STRING); + FastLED.addLeds(leds, NUM_LED_STRING, NUM_LED_ONBOARD); + for (int i=0; i= MAX_BRIGHTNESS) + { + // Flip the sign of the rate & start decaying light. + rates[i] *= -1; + brightnesses[i] += rates[i]; + } + // Rate is negative but the new brightness is bigger, so we underflowed. + else if (brightnesses[i] <= MIN_BRIGHTNESS) + { + // reset this pixel to a different color and start anew. + hues[i] = globalHue + random(0,HUE_SPREAD); + saturations[i] = random(MIN_SATURATION,MAX_SATURATION); + rates[i] = random(MIN_RATE,MAX_RATE); + brightnesses[i] = MIN_BRIGHTNESS; + } + else + { + brightnesses[i] += rates[i]; + } + + leds[i] = CHSV(hues[i], saturations[i], brightnesses[i]); + } + FastLED.show(); + + clock++; + if (clock % HUE_RATE_MODULO == 0 && !fixedHue) + { + globalHue++; + } + + delay(SPEED); + + if (!isInDataMode) { + if (clock % BT_CHECK_CONN_MODULO == 0 && ble.isConnected()) { // ble.isConnected() takes time, so don't do it every loop + isInDataMode = true; + Serial.println(F("***********************")); + // Set Bluefruit to DATA mode + Serial.println( F("Switching to DATA mode!") ); + ble.setMode(BLUEFRUIT_MODE_DATA); + Serial.println(F("***********************")); + ble.verbose(false); // debug info is a little annoying after this point! + } + return; + } + /* Wait for new data to arrive */ uint8_t len = readPacket(&ble, BLE_READPACKET_TIMEOUT); if (len == 0) return; @@ -192,10 +270,34 @@ void loop(void) if (blue < 0x10) Serial.print("0"); Serial.println(blue, HEX); + if (red == 0 && blue == 0 && green == 0) { + // receiving #000000 is a special control color that enables the hue cycle. + fixedHue = false; + } + else { + CRGB color(red, green, blue); + CHSV hsv = rgb2hsv_approximate(color); + globalHue = hsv.hue; + fixedHue = true; + } + + // reset them all + for (int i=0; i(leds, NUM_LED); + FastLED.addLeds(leds, NUM_LED); for (int i=0; i