Skip to content

Commit

Permalink
chore: Convert to Google C++ Style
Browse files Browse the repository at this point in the history
  • Loading branch information
13rac1 committed Jan 16, 2018
1 parent fb82598 commit f7b33fc
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 98 deletions.
3 changes: 3 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
Language: Cpp
BasedOnStyle: Google
103 changes: 51 additions & 52 deletions src/PFOC-LED-Belts.ino
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
// Print Debug to Serial
#define DEBUG 1
#define DEBUG 1
// Bluetooth
#include "Adafruit_BLE.h"
#include "Adafruit_BluefruitLE_SPI.h"
#include "Adafruit_BluefruitLE_UART.h"

// Bluetooth setup
#define FACTORYRESET_ENABLE 1 // Reset BLE NVRAM
#define BUFSIZE 128 // Size of the read buffer for incoming data
#define BLUEFRUIT_SPI_CS 8
#define BLUEFRUIT_SPI_IRQ 7
#define BLUEFRUIT_SPI_RST 4
Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_CS, BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST);
#define FACTORYRESET_ENABLE 1 // Reset BLE NVRAM
#define BUFSIZE 128 // Size of the read buffer for incoming data
#define BLUEFRUIT_SPI_CS 8
#define BLUEFRUIT_SPI_IRQ 7
#define BLUEFRUIT_SPI_RST 4
Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_CS, BLUEFRUIT_SPI_IRQ,
BLUEFRUIT_SPI_RST);

// FastLED
#include "FastLED.h"
Expand All @@ -24,45 +25,47 @@ FASTLED_USING_NAMESPACE
#include "beltConfig.h"

// BELT SELECT! See beltConfig.h
beltColor currentBelt = PURPLE;
beltColor currentBelt = ORANGE;

#define RECOVERY_DELAY 2000
#define BRIGHTNESS 48
#define FRAMES_PER_SECOND 120
#define RECOVERY_DELAY 2000
#define BRIGHTNESS 48
#define FRAMES_PER_SECOND 120

// Battery
#define VBATPIN 9
#define VBATMIN 3.4
float measuredVBat = 0;
#define VBATPIN 9
#define VBATMIN 3.4
float measuredVBat = 0;

// Animation color settings
uint8_t gHue = belts[currentBelt].hue;
uint8_t range = 24;
uint8_t gHueMin = gHue - range/2;
uint8_t gHueMin = gHue - range / 2;

// State Management
enum states {
START,
RUN,
LOWBATTERY
};
enum states { START, RUN, LOWBATTERY };
states state = START;

// Store last and current frame times to find frame interval
unsigned long lastMillis = 0;
unsigned long lastMillis = 0;
unsigned long currentMillis = 0;
unsigned long frameMillis = 0;
unsigned long frameMillis = 0;

// Main Setup
void setup() {
// Setupl FastLED LED strip configuration
FastLED.addLeds<BUCKLE_LED_TYPE,BUCKLE_DATA_PIN,BUCKLE_COLOR_ORDER>(buckle_leds, BUCKLE_NUM_LEDS).setCorrection(TypicalLEDStrip);
FastLED.addLeds<BELT_LED_TYPE,BELT_DATA_PIN,BELT_CLK_PIN,BELT_COLOR_ORDER>(belt_leds, belts[currentBelt].num).setCorrection(TypicalLEDStrip);
FastLED
.addLeds<BUCKLE_LED_TYPE, BUCKLE_DATA_PIN, BUCKLE_COLOR_ORDER>(
buckle_leds, BUCKLE_NUM_LEDS)
.setCorrection(TypicalLEDStrip);
FastLED
.addLeds<BELT_LED_TYPE, BELT_DATA_PIN, BELT_CLK_PIN, BELT_COLOR_ORDER>(
belt_leds, belts[currentBelt].num)
.setCorrection(TypicalLEDStrip);

// Set master brightness control.
FastLED.setBrightness(BRIGHTNESS);
belt_leds[0] = CRGB::Green;
belt_leds[belts[currentBelt].num-1] = CRGB::Green;
belt_leds[belts[currentBelt].num - 1] = CRGB::Green;
setBuckle(0, CRGB::Green);

// Delay for recovery mode
Expand All @@ -71,24 +74,24 @@ void setup() {

// Setup serial, no wait.
Serial.begin(115200);
Serial.println(F("PFOC-Belt Serial")); //F() stores string in Flash memory
Serial.println(F("PFOC-Belt Serial")); // F() stores string in Flash memory
setBuckle(2, CRGB::Green);

// Start BLE
if (ble.begin(false)) {
// Found Bluefruit
setBuckle(3, CRGB::Green);
}
else setBuckle(3, CRGB::Red);
} else
setBuckle(3, CRGB::Red);

// Factory Reset
if (FACTORYRESET_ENABLE) {
/* Perform a factory reset to make sure everything is in a known state */
if (ble.factoryReset(true)) {
// Reset Success
setBuckle(4, CRGB::Green);
}
else setBuckle(4, CRGB::Red);
} else
setBuckle(4, CRGB::Red);
}
// Set BLE Device name
ble.atcommand(belts[currentBelt].name);
Expand All @@ -114,15 +117,15 @@ void setBuckle(uint8_t led, CRGB color) {
// Main Loop
void loop() {
// Delay to keep the framerate modest
FastLED.delay(1000/FRAMES_PER_SECOND);
//ble.update(100);
FastLED.delay(1000 / FRAMES_PER_SECOND);
// ble.update(100);

// Calculate time since last frame.
currentMillis = millis();
frameMillis = currentMillis - lastMillis;

BleUartRX();
EVERY_N_MILLISECONDS(500){checkBattery();}
EVERY_N_MILLISECONDS(500) { checkBattery(); }
findState();
drawFrames();

Expand Down Expand Up @@ -151,27 +154,27 @@ void BleUartRX() {
int command = ble.read();
if (DEBUG) Serial.write(command);
switch (command) {
case 'V': // Get Version
case 'V': // Get Version
commandVersion();
break;

case 'S': // Setup dimensions, components, stride...
case 'S': // Setup dimensions, components, stride...
commandSetup();
break;

case 'C': // Clear with color
case 'C': // Clear with color
commandClearColor();
break;

case 'B': // Set Brightness
case 'B': // Set Brightness
commandSetBrightness();
break;

case 'P': // Set Pixel
case 'P': // Set Pixel
commandSetPixel();
break;

case 'I': // Receive new image
case 'I': // Receive new image
commandImage();
break;
}
Expand All @@ -181,9 +184,9 @@ void BleUartRX() {
// Check the battery voltage
void checkBattery() {
measuredVBat = analogRead(VBATPIN);
measuredVBat *= 2; // we divided by 2, so multiply back
measuredVBat *= 3.3; // Multiply by 3.3V, our reference voltage
measuredVBat /= 1024; // convert to voltage
measuredVBat *= 2; // we divided by 2, so multiply back
measuredVBat *= 3.3; // Multiply by 3.3V, our reference voltage
measuredVBat /= 1024; // convert to voltage
}

// Updates the global state variable.
Expand Down Expand Up @@ -215,10 +218,10 @@ void drawFrames() {

// A test to draw at startup.
void drawTest() {
for(int i = 0; i < BUCKLE_NUM_LEDS; i++) {
for (int i = 0; i < BUCKLE_NUM_LEDS; i++) {
buckle_leds[i] = CRGB::White;
}
for(int i = 0; i < belts[currentBelt].num; i++) {
for (int i = 0; i < belts[currentBelt].num; i++) {
belt_leds[i] = CRGB::White;
}
}
Expand All @@ -233,22 +236,18 @@ void drawConfetti() {
// White only for WHITE
pos = random16(BUCKLE_NUM_LEDS);
buckle_leds[pos] += CRGB::White;
for (int i = 0; i<4; i++) {
for (int i = 0; i < 4; i++) {
pos = random16(belts[currentBelt].num);
belt_leds[pos] += CRGB::White;
}
}
else {
} else {
pos = random16(BUCKLE_NUM_LEDS);
buckle_leds[pos] += CHSV(gHueMin + random8(range), 255, 255);
for (int i = 0; i<4; i++) {
for (int i = 0; i < 4; i++) {
pos = random16(belts[currentBelt].num);
belt_leds[pos] += CHSV(gHueMin + random8(range), 255, 255);
}
}
}

void drawLowBattery() {
buckle_leds[0] = CRGB(255, 0, 0);
}

void drawLowBattery() { buckle_leds[0] = CRGB(255, 0, 0); }
57 changes: 25 additions & 32 deletions src/beltConfig.h
Original file line number Diff line number Diff line change
@@ -1,54 +1,47 @@
enum beltColor {
RED = 0,
ORANGE,
YELLOW,
GREEN,
BLUE,
PURPLE,
PINK,
WHITE
};
enum beltColor { RED = 0, ORANGE, YELLOW, GREEN, BLUE, PURPLE, PINK, WHITE };

struct belt {
uint8_t hue;
uint8_t num;
const char *name;
};

#define NUM_BELTS 8
#define NUM_BELTS 8
belt belts[NUM_BELTS] = {
{0, 28, "AT+GAPDEVNAME=PFOC Red"}, // Confirmed length.
{16, 28, "AT+GAPDEVNAME=PFOC Orange"}, // Confirmed length.
{64, 28, "AT+GAPDEVNAME=PFOC Yellow"}, // Confirmed length.
{96, 26, "AT+GAPDEVNAME=PFOC Green"}, // Confirmed length.
{160, 26, "AT+GAPDEVNAME=PFOC Blue"}, // Confirmed length.
{192, 32, "AT+GAPDEVNAME=PFOC Purple"}, // Confirmed length.
{224, 24, "AT+GAPDEVNAME=PFOC Pink"}, // Confirmed length.
{192, 34, "AT+GAPDEVNAME=PFOC White"}, // Confirmed length, no hue, special case
{0, 28, "AT+GAPDEVNAME=PFOC Red"}, // Confirmed length.
{16, 28, "AT+GAPDEVNAME=PFOC Orange"}, // Confirmed length.
{64, 28, "AT+GAPDEVNAME=PFOC Yellow"}, // Confirmed length.
{96, 26, "AT+GAPDEVNAME=PFOC Green"}, // Confirmed length.
{160, 26, "AT+GAPDEVNAME=PFOC Blue"}, // Confirmed length.
{192, 32, "AT+GAPDEVNAME=PFOC Purple"}, // Confirmed length.
{224, 24, "AT+GAPDEVNAME=PFOC Pink"}, // Confirmed length.
{192, 34,
"AT+GAPDEVNAME=PFOC White"}, // Confirmed length, no hue, special case
};

#define BUCKLE_DATA_PIN 6
#define BUCKLE_LED_TYPE WS2812
#define BUCKLE_COLOR_ORDER GRB
#define BUCKLE_NUM_LEDS 8
#define BUCKLE_DATA_PIN 6
#define BUCKLE_LED_TYPE WS2812
#define BUCKLE_COLOR_ORDER GRB
#define BUCKLE_NUM_LEDS 8
CRGB buckle_leds[BUCKLE_NUM_LEDS];

#define BELT_DATA_PIN 11
#define BELT_DATA_PIN 11
// Check board type.
// Reference for compile-type variables: https://github.com/arduino/Arduino/wiki/Arduino-IDE-1.5-3rd-party-Hardware-specification#boardstxt
// Reference for compile-type variables:
// https://github.com/arduino/Arduino/wiki/Arduino-IDE-1.5-3rd-party-Hardware-specification#boardstxt

//Adafruit Feather 32u4
// Adafruit Feather 32u4
#ifdef ARDUINO_AVR_FEATHER32U4
#pragma message "Found ARDUINO_AVR_FEATHER32U4"
#define BELT_CLK_PIN 3
#define BELT_CLK_PIN 3
#endif
//Adafruit Feather M0
// Adafruit Feather M0
#ifdef ARDUINO_SAMD_FEATHER_M0
#pragma message "Found ARDUINO_SAMD_FEATHER_M0"
#define BELT_CLK_PIN 21
#define BELT_CLK_PIN 21
#endif

#define BELT_LED_TYPE LPD8806
#define BELT_COLOR_ORDER GRB
#define BELT_NUM_LEDS_MAX 40
#define BELT_LED_TYPE LPD8806
#define BELT_COLOR_ORDER GRB
#define BELT_NUM_LEDS_MAX 40
CRGB belt_leds[BELT_NUM_LEDS_MAX];
28 changes: 14 additions & 14 deletions src/commands.h
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
// Modified from: https://github.com/adafruit/Bluefruit_LE_Connect_Android/blob/master/app/src/main/assets/neopixel/Neopixel_Arduino.zip
// Modified from:
// https://github.com/adafruit/Bluefruit_LE_Connect_Android/blob/master/app/src/main/assets/neopixel/Neopixel_Arduino.zip

// Little wrapper for ble writes.
void sendResponse(char *response) {
ble.write(response, strlen(response)*sizeof(char));
ble.write(response, strlen(response) * sizeof(char));
}

void commandVersion() {
if (DEBUG) Serial.println(F("Command: Version check"));
sendResponse("Neopixel v1.0"); // Cake to work with the Bluefruit LE app
sendResponse("Neopixel v1.0"); // Cake to work with the Bluefruit LE app
}

// Stores values from the app.
uint8_t width = 0;
uint8_t height = 0;
uint8_t components = 3; // only 3 and 4 are valid values
uint8_t components = 3; // only 3 and 4 are valid values
uint8_t stride;
void commandSetup() {
if (DEBUG) Serial.println(F("Command: Setup"));
Expand All @@ -23,15 +24,16 @@ void commandSetup() {
components = ble.read();
stride = ble.read();
// Throw some out
ble.read(); // PixelType > /dev/null
ble.read(); // PixelType<<8 > /dev/null
ble.read(); // PixelType > /dev/null
ble.read(); // PixelType<<8 > /dev/null

sendResponse("OK");
}

// Sets all pixel colors to the provided values until overwritten by an animation.
// Sets all pixel colors to the provided values until overwritten by an
// animation.
// TODO: Something else?
#define MAXCOMPONENTS 4
#define MAXCOMPONENTS 4
void commandClearColor() {
if (DEBUG) Serial.println(F("Command: ClearColor"));

Expand All @@ -48,7 +50,7 @@ void commandClearColor() {
CHSV hsv = rgb2hsv_approximate(CRGB(color[0], color[1], color[2]));

gHue = hsv.h;
gHueMin = gHue - range/2;
gHueMin = gHue - range / 2;

sendResponse("OK");
}
Expand All @@ -70,7 +72,7 @@ void commandSetPixel() {
// Read position
uint8_t x = ble.read();
uint8_t y = ble.read();
uint32_t pixelIndex = y*width+x;
uint32_t pixelIndex = y * width + x;

// Read color
uint8_t color[MAXCOMPONENTS];
Expand Down Expand Up @@ -98,13 +100,11 @@ void commandImage() {
for (int i = 0; i < size; i++) {
for (int j = 0; j < components;) {
if (ble.available()) {
ble.read(); // > /dev/null
ble.read(); // > /dev/null
j++;
}
}
}

sendResponse("OK"); // Cake
sendResponse("OK"); // Cake
}


0 comments on commit f7b33fc

Please sign in to comment.