Refactor / code cleanup

This commit is contained in:
Mike C 2015-08-09 19:54:35 -04:00
parent 29f1048ec7
commit 03cca7f08e

View file

@ -1,4 +1,6 @@
// //////////
// System includes for libraries
// //////////
#include "Arduino.h"
#include "Wire.h"
#include "MAX1704.h"
@ -8,21 +10,26 @@
#include <avr/power.h>
#endif
// //////////
// Various defines / pins / etc
// TWEAK THESE ACCORDINGLY
// //////////
#define DEBUG true
#define INTERACTIVE_DEBUG false
#define USE_SLEEP false
#define SLEEP_INTERVAL 1000 // miliseconds
#define ALERT_LEVEL 15 // battery alert level (%)
#define ALERT_LEVEL 15 // battery alert level (%) [1-32% are valid values here]
#define POT_BRIGHT A0
#define POT_COLOR A1
#define PIN_ALWAYS_ON 9 // Switch that enables always on/off of pixels
#define PIN_WHITE 12 // Short Vout to this pin to change color to white
#define PIN_WHITE 12 // Switch that switches between color wiper and white color
#define PIN_DOOR 11 // Door sensor pin
#define PIN_NEO 6 // Pin with the pixel comm line
#define NEO_PIX_NUM 2 // FIXME: this should be 5 for final build for safe lighting
// //////////
// Objects / values / etc
// //////////
MAX1704 fuelGauge;
float charge_percent;
uint8_t brightness;
@ -34,17 +41,27 @@ uint8_t rgb[3]; // index 0 = red / index 1 = green / index 2 = blue
bool white = false;
bool max_found = false;
bool door_open = false;
bool lights_on = false;
// //////////
// Various function definitions
// //////////
void hsvToRgb(int h, double s, double v); // See end of file for implementation
void print_debug(); // Print debugging info if debug if debugging output is enabled
void read_values(); // Read values from various sensors/devices
void do_low_power(); // Enable sleep / low power mode(s) as appropriate
void pixel_update(); // Update pixel colors, on/off state, etc as appropriate
ISR(WDT_vect) { Sleepy::watchdogEvent(); } // Setup the watchdog -- For sleepy
// //////////
// Standard setup method for Arduino IDE
// //////////
void setup() {
Serial.begin(9600);
Wire.begin();
// Setup serial and wait for console
if (DEBUG) {
Serial.begin(9600);
if (INTERACTIVE_DEBUG) {
while(!Serial);
}
@ -75,10 +92,12 @@ void setup() {
if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
#endif
// End of trinket special code
// Setup pixels, off to begin
neopix.begin();
neopix.show(); // Init to "off"
neopix.setBrightness(0);
neopix.show();
// Various startup info (debugging)
// Various debugging output
if (DEBUG) {
fuelGauge.showConfig();
Serial.println("----------");
@ -89,7 +108,20 @@ void setup() {
}
}
// //////////
// Standard loop for Arduino IDE
// //////////
void loop() {
read_values();
pixel_update();
print_debug();
do_low_power();
}
// //////////
// Read values from inputs/devices
// ///////////
void read_values() {
// Read various values needed
if (max_found) {
charge_percent = fuelGauge.stateOfCharge(); // Battery level
@ -99,24 +131,67 @@ void loop() {
always_on = (digitalRead(PIN_ALWAYS_ON) == LOW); // Toggle button pressed == ALWAYS ON
white = (digitalRead(PIN_WHITE) == LOW); // Shorted pin to enable white color for LEDs
door_open = (digitalRead(PIN_DOOR) == HIGH); // SparkFun door sensor is indicating door is open (NO for door closed state, set as input_pullup so the logic is inverted)
lights_on = always_on || (door_open); // Take a look at all appropriate values and set lights on/off as appropriate
}
// ///////////
// Update pixels appropriately (on/off, color, etc)
// ///////////
void pixel_update() {
// Set RGB array to values as appropriate
if (white) {
rgb[0] = rgb[1] = rgb[2] = 255;
}
else {
// Convert raw color value to position on a circle and then use the circle position to figure out color on a color wheel
// Convert raw color value to position on a circle and then use the circle position to figure out color on a color wheel
color = map(raw_color, 0, 1024, 0, 360);
hsvToRgb(color, 1, 1); // set rgb byte array
}
// NeoPixel related
// Set color accordingly
for (uint8_t i=0; i<NEO_PIX_NUM; i++) {
neopix.setPixelColor(i, neopix.Color(rgb[0], rgb[1], rgb[2]));
}
neopix.show();
// Deal with brightness
if (!lights_on) {
brightness = 0;
}
neopix.setBrightness(brightness);
// Apply changes
neopix.show();
}
// //////////
// Setup / run low power mode if appropriate
// //////////
void do_low_power() {
// Verify sleep mode is enabled
// Don't sleep if the lights are on (pixels eat way more power than an AVR chip / board, don't worry about "excess" battery use just yet)
// This is inverted logic + return to make life easier and not having to wrap contents of method in an if block
if (!USE_SLEEP || lights_on) {
return;
}
// Carry on with sleep stuff if we get this far
if (DEBUG) {
Serial.println("SLEEPING!");
}
if (max_found) {
fuelGauge.sleep();
}
delay(250); // Be sure that everything is settled before going to sleep
Sleepy::loseSomeTime(SLEEP_INTERVAL);
if (max_found) {
fuelGauge.awake();
}
}
// //////////
// Print debugging info
// //////////
void print_debug() {
// Debugging code
if (DEBUG) {
if (max_found) {
@ -149,28 +224,12 @@ void loop() {
Serial.println("----------");
delay(250);
}
// Sleep mode enabled
if (USE_SLEEP && !always_on) { // Don't enable sleep if always on toggle is set
if (DEBUG) {
Serial.println("SLEEPING!");
}
if (max_found) {
fuelGauge.sleep();
}
neopix.setBrightness(0);
neopix.show();
delay(250); // Be sure that everything is settled before going to sleep
Sleepy::loseSomeTime(SLEEP_INTERVAL);
if (max_found) {
fuelGauge.awake();
}
neopix.setBrightness(brightness);
}
}
// //////////
// Convert degrees on a circle to color values
// http://www.skylark-software.com/2011/01/arduino-notebook-rgb-led-and-color_21.html
// //////////
void hsvToRgb(int h, double s, double v) {
// Make sure our arguments stay in-range
h = max(0, min(360, h));