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