|
| 1 | +// Adafruit Bluefruit LE Feather Holiday Lights |
| 2 | +// See the full guide at: |
| 3 | +// https://learn.adafruit.com/feather-holiday-lights/overview |
| 4 | +// Author: Tony DiCola |
| 5 | +// Based on the neopixel_picker example from the Adafruit Bluefruit nRF51 library. |
| 6 | +// Released under a MIT license: |
| 7 | +// https://opensource.org/licenses/MIT |
| 8 | +#include "Adafruit_BLE.h" |
| 9 | +#include "Adafruit_BluefruitLE_SPI.h" |
| 10 | +#include "Adafruit_NeoPixel.h" |
| 11 | +#include "BluefruitConfig.h" |
| 12 | +#include "SoftwareSerial.h" |
| 13 | +#include "SPI.h" |
| 14 | + |
| 15 | + |
| 16 | +#define PIXEL_COUNT 60 // The number of NeoPixels connected to the board. |
| 17 | + |
| 18 | +#define PIXEL_PIN 6 // The pin connected to the input of the NeoPixels. |
| 19 | + |
| 20 | +#define PIXEL_TYPE NEO_GRB + NEO_KHZ800 // The type of NeoPixels, see the NeoPixel |
| 21 | + // strandtest example for more options. |
| 22 | + |
| 23 | +#define ANIMATION_PERIOD_MS 300 // The amount of time (in milliseconds) that each |
| 24 | + // animation frame lasts. Decrease this to speed |
| 25 | + // up the animation, and increase it to slow it down. |
| 26 | + |
| 27 | + |
| 28 | +// Create NeoPixel strip from above parameters. |
| 29 | +Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE); |
| 30 | + |
| 31 | +// Create the Bluefruit object using hardware SPI (for Bluefruit LE feather). |
| 32 | +Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_CS, BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST); |
| 33 | + |
| 34 | +// Global variable to hold the current pixel color. Starts out red but will be |
| 35 | +// changed by the BLE color picker. |
| 36 | +int r = 255; |
| 37 | +int g = 0; |
| 38 | +int b = 0; |
| 39 | + |
| 40 | +// Function prototypes and data over in packetparser.cpp |
| 41 | +uint8_t readPacket(Adafruit_BLE *ble, uint16_t timeout); |
| 42 | +float parsefloat(uint8_t *buffer); |
| 43 | +void printHex(const uint8_t * data, const uint32_t numBytes); |
| 44 | +extern uint8_t packetbuffer[]; |
| 45 | + |
| 46 | + |
| 47 | +void setup(void) |
| 48 | +{ |
| 49 | + Serial.begin(115200); |
| 50 | + Serial.println(F("Adafruit Bluefruit LE Holiday Lights")); |
| 51 | + |
| 52 | + // Initialize NeoPixels. |
| 53 | + strip.begin(); |
| 54 | + strip.show(); |
| 55 | + |
| 56 | + // Initialize BLE library. |
| 57 | + if (!ble.begin(false)) |
| 58 | + { |
| 59 | + Serial.println(F("Couldn't find Bluefruit LE module!")); |
| 60 | + while (1); |
| 61 | + } |
| 62 | + ble.echo(false); |
| 63 | + |
| 64 | + Serial.println(F("Please use Adafruit Bluefruit LE app to connect in Controller mode")); |
| 65 | + Serial.println(F("Then activate/use the color picker to change color.")); |
| 66 | + Serial.println(); |
| 67 | + |
| 68 | + // Wait for connection. |
| 69 | + while (!ble.isConnected()) { |
| 70 | + // Make sure to animate the pixels while waiting! |
| 71 | + animatePixels(strip, r, g, b, ANIMATION_PERIOD_MS); |
| 72 | + delay(50); |
| 73 | + } |
| 74 | + ble.setMode(BLUEFRUIT_MODE_DATA); |
| 75 | +} |
| 76 | + |
| 77 | +void loop(void) |
| 78 | +{ |
| 79 | + // Animate the pixels. |
| 80 | + animatePixels(strip, r, g, b, ANIMATION_PERIOD_MS); |
| 81 | + |
| 82 | + // Grab a BLE controller packet if available. |
| 83 | + uint8_t len = readPacket(&ble, BLE_READPACKET_TIMEOUT); |
| 84 | + if (len == 0) return; |
| 85 | + |
| 86 | + // Parse a color packet. |
| 87 | + if (packetbuffer[1] == 'C') { |
| 88 | + // Grab the RGB values from the packet and change the light color. |
| 89 | + r = packetbuffer[2]; |
| 90 | + g = packetbuffer[3]; |
| 91 | + b = packetbuffer[4]; |
| 92 | + // Print out the color that was received too: |
| 93 | + Serial.print ("RGB #"); |
| 94 | + if (r < 0x10) Serial.print("0"); |
| 95 | + Serial.print(r, HEX); |
| 96 | + if (g < 0x10) Serial.print("0"); |
| 97 | + Serial.print(g, HEX); |
| 98 | + if (b < 0x10) Serial.print("0"); |
| 99 | + Serial.println(b, HEX); |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +void animatePixels(Adafruit_NeoPixel& strip, uint8_t r, uint8_t g, uint8_t b, int periodMS) { |
| 104 | + // Animate the NeoPixels with a simple theater chase/marquee animation. |
| 105 | + // Must provide a NeoPixel object, a color, and a period (in milliseconds) that controls how |
| 106 | + // long an animation frame will last. |
| 107 | + // First determine if it's an odd or even period. |
| 108 | + int mode = millis()/periodMS % 2; |
| 109 | + // Now light all the pixels and set odd and even pixels to different colors. |
| 110 | + // By alternating the odd/even pixel colors they'll appear to march along the strip. |
| 111 | + for (int i = 0; i < strip.numPixels(); ++i) { |
| 112 | + if (i%2 == mode) { |
| 113 | + strip.setPixelColor(i, r, g, b); // Full bright color. |
| 114 | + } |
| 115 | + else { |
| 116 | + strip.setPixelColor(i, r/4, g/4, b/4); // Quarter intensity color. |
| 117 | + } |
| 118 | + } |
| 119 | + strip.show(); |
| 120 | +} |
0 commit comments