Last active
February 17, 2016 22:23
-
-
Save edsonsoares/d5467819f345da833fbd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define NEO_PIN 2 // RedBear Blend & RFduino | |
// Import libraries (BLEPeripheral depends on SPI) | |
#include <Adafruit_NeoPixel.h> | |
#include <SPI.h> | |
#include <BLEPeripheral.h> | |
/*NEOPIXELS*/ | |
Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, NEO_PIN, NEO_GRB + NEO_KHZ800); | |
// define pin\\s (varies per shield/board) | |
// https://github.com/sandeepmistry/arduino-BLEPeripheral#pinouts | |
// Blend | |
#define BLE_REQ -1 | |
#define BLE_RDY -1 | |
#define BLE_RST -1 | |
// create peripheral instance, see pinouts above | |
BLEPeripheral blePeripheral = BLEPeripheral(BLE_REQ, BLE_RDY, BLE_RST); | |
// create service | |
BLEService neoService = BLEService("AB10"); | |
// create switch characteristic | |
BLECharCharacteristic switchCharacteristic = BLECharCharacteristic("AB11", BLERead | BLEWrite); | |
BLEDescriptor switchDescriptor = BLEDescriptor("2901", "Switch"); | |
void setup() { | |
Serial.begin(9600); | |
strip.begin(); | |
strip.show(); | |
// set LED pin to output mode | |
pinMode(NEO_PIN, OUTPUT); | |
// set advertised local name and service UUID | |
blePeripheral.setLocalName("NEO"); | |
blePeripheral.setDeviceName("NEO"); | |
blePeripheral.setAdvertisedServiceUuid(neoService.uuid()); | |
// add service and characteristics | |
blePeripheral.addAttribute(neoService); | |
blePeripheral.addAttribute(switchCharacteristic); | |
blePeripheral.addAttribute(switchDescriptor); | |
// assign event handlers for characteristic | |
switchCharacteristic.setEventHandler(BLEWritten, switchCharacteristicWritten); | |
Serial.println("Passed event handler"); | |
// begin initialization | |
blePeripheral.begin(); | |
Serial.println(F("Bluetooth LED")); | |
} | |
void loop() { | |
// Tell the bluetooth radio to do whatever it should be working on | |
blePeripheral.poll(); | |
} | |
void switchCharacteristicWritten(BLECentral& central, BLECharacteristic& characteristic) { | |
// central wrote new value to characteristic, update LED | |
Serial.print(F("Characteristic event, written: ")); | |
if (switchCharacteristic.value()) { | |
Serial.println(F("LED on")); | |
//digitalWrite(NEO_PIN, HIGH); | |
colorWipe(strip.Color(255, 0, 0), 50); // Red | |
} else { | |
Serial.println(F("LED off")); | |
//digitalWrite(NEO_PIN, LOW); | |
colorWipe(strip.Color(0, 255, 0), 50); // Green | |
} | |
Serial.println(switchCharacteristic.value()); | |
} | |
void colorWipe(uint32_t c, uint8_t wait) { | |
for(uint16_t i=0; i<strip.numPixels(); i++) { | |
strip.setPixelColor(i, c); | |
strip.show(); | |
delay(wait); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment