Browse Source

Initial implementation

master
Mike C 6 years ago
parent
commit
52be705ecd
  1. 1162
      src/Adafruit_NeoPixel.cpp
  2. 103
      src/Adafruit_NeoPixel.h
  3. 156
      src/MAX1704.cpp
  4. 54
      src/MAX1704.h
  5. 86
      src/src.ino

1162
src/Adafruit_NeoPixel.cpp

File diff suppressed because it is too large

103
src/Adafruit_NeoPixel.h

@ -0,0 +1,103 @@
/*--------------------------------------------------------------------
This file is part of the Adafruit NeoPixel library.
NeoPixel is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
NeoPixel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with NeoPixel. If not, see
<http://www.gnu.org/licenses/>.
--------------------------------------------------------------------*/
#ifndef ADAFRUIT_NEOPIXEL_H
#define ADAFRUIT_NEOPIXEL_H
#if (ARDUINO >= 100)
#include <Arduino.h>
#else
#include <WProgram.h>
#include <pins_arduino.h>
#endif
// 'type' flags for LED pixels (third parameter to constructor):
#define NEO_RGB 0x00 // Wired for RGB data order
#define NEO_GRB 0x01 // Wired for GRB data order
#define NEO_BRG 0x04
#define NEO_RBG 0x08
#define NEO_COLMASK 0x01
#define NEO_KHZ800 0x02 // 800 KHz datastream
#define NEO_SPDMASK 0x02
// Trinket flash space is tight, v1 NeoPixels aren't handled by default.
// Remove the ifndef/endif to add support -- but code will be bigger.
// Conversely, can comment out the #defines to save space on other MCUs.
#ifndef __AVR_ATtiny85__
#define NEO_KHZ400 0x00 // 400 KHz datastream
#endif
class Adafruit_NeoPixel {
public:
// Constructor: number of LEDs, pin number, LED type
Adafruit_NeoPixel(uint16_t n, uint8_t p=6, uint8_t t=NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel(void);
~Adafruit_NeoPixel();
void
begin(void),
show(void),
setPin(uint8_t p),
setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b),
setPixelColor(uint16_t n, uint32_t c),
setBrightness(uint8_t),
clear(),
updateLength(uint16_t n),
updateType(uint8_t t);
uint8_t
*getPixels(void) const,
getBrightness(void) const;
uint16_t
numPixels(void) const;
static uint32_t
Color(uint8_t r, uint8_t g, uint8_t b);
uint32_t
getPixelColor(uint16_t n) const;
inline bool
canShow(void) { return (micros() - endTime) >= 50L; }
private:
boolean
begun; // true if begin() previously called
uint16_t
numLEDs, // Number of RGB LEDs in strip
numBytes; // Size of 'pixels' buffer below
int8_t
pin; // Output pin number (-1 if not yet set)
uint8_t
type, // Pixel flags (400 vs 800 KHz, RGB vs GRB color)
brightness,
*pixels, // Holds LED color values (3 bytes each)
rOffset, // Index of red byte within each 3-byte pixel
gOffset, // Index of green byte
bOffset; // Index of blue byte
uint32_t
endTime; // Latch timing reference
#ifdef __AVR__
volatile uint8_t
*port; // Output PORT register
uint8_t
pinMask; // Output PORT bitmask
#endif
};
#endif // ADAFRUIT_NEOPIXEL_H

156
src/MAX1704.cpp

@ -0,0 +1,156 @@
/**************************************************************************
* *
* MAX1704* Driver for Arduino *
* *
* Matthew Newberry *
* mattnewberry@me.com *
* *
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU License. *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU License V2 for more details. *
* *
***************************************************************************/
#include "Arduino.h"
#include "MAX1704.h"
#include "Wire.h"
float MAX1704::stateOfCharge(){
byte msb = 0;
byte lsb = 0;
readFrom(MAX1704_SOC,msb,lsb);
float fraction = lsb / 256.0;
float percentage = msb + fraction;
return percentage;
}
void MAX1704::showConfig(){
int threshold = alertThreshold();
boolean sleep = isSleeping();
boolean alert = isAlerting();
byte v = version();
Serial.print("Version = ");
Serial.println(v, HEX);
Serial.print("Sleep = ");
Serial.println(sleep, HEX);
Serial.print("Alert = ");
Serial.println(alert, HEX);
Serial.print("Alert Threshold = ");
Serial.print(threshold, DEC);
Serial.println("%");
}
void MAX1704::reset(){
performCommand(MAX1704_POWER_ON_RESET, 0x00);
}
void MAX1704::quickStart(){
performCommand(MAX1704_QUICK_START, 0x00);
}
char MAX1704::version(){
int value = 0;
byte msb = 0;
byte lsb = 0;
readFrom(MAX1704_VERSION, msb, lsb);
value = 0xFF00 & (msb<<8);
value |= 0xFF & lsb;
return value;
}
void MAX1704::setAlertThreshold(uint8_t level){
Wire.beginTransmission(MAX1704_ADDR);
Wire.write(MAX1704_CONFIG);
Wire.write(MAX1704_ALERT_LEVEL);
Wire.write(32 - level);
Wire.endTransmission();
}
int MAX1704::alertThreshold(){
byte msb = 0;
byte lsb = 0;
readFrom(MAX1704_CONFIG,msb,lsb);
byte threshold = lsb & 0x1f;
return 32 - threshold;
}
boolean MAX1704::isAlerting(){
byte msb = 0;
byte lsb = 0;
readFrom(MAX1704_CONFIG,msb,lsb);
byte alert = (lsb >>5) & 0x01;
return int(alert) == 1;
}
boolean MAX1704::isSleeping(){
byte msb = 0;
byte lsb = 0;
readFrom(MAX1704_CONFIG,msb,lsb);
byte sleep = (lsb >>7) & 0x01;
return int(sleep) == 1;
}
void MAX1704::sleep(){
}
void MAX1704::awake(){
}
void MAX1704::performCommand(byte address, int value){
Wire.beginTransmission(MAX1704_ADDR);
Wire.write(MAX1704_COMMAND);
Wire.write(address);
Wire.write(value);
Wire.endTransmission();
}
void MAX1704::readFrom(byte address, byte &msb, byte &lsb){
Wire.beginTransmission(MAX1704_ADDR);
Wire.write(address);
Wire.endTransmission();
Wire.requestFrom(MAX1704_ADDR, 2);
int numread = Wire.available();
//if (numread == 2) {
msb = Wire.read();
lsb = Wire.read();
//}
Wire.endTransmission();
}

54
src/MAX1704.h

@ -0,0 +1,54 @@
/**************************************************************************
* *
* MAX1704* Driver for Arduino *
* *
* Matthew Newberry *
* mattnewberry@me.com *
* *
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU License. *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU License V2 for more details. *
* *
***************************************************************************/
#include "Arduino.h"
#ifndef _MAX1704_h
#define _MAX1704_h
#define MAX1704_ADDR 0x36
#define MAX1704_SOC 0x04
#define MAX1704_VERSION 0x08
#define MAX1704_POWER_ON_RESET 0x54
#define MAX1704_QUICK_START 0x40
#define MAX1704_CONFIG 0x0C
#define MAX1704_COMMAND 0xFE
#define MAX1704_ALERT_LEVEL 0x97
class MAX1704{
public:
float stateOfCharge();
void showConfig();
void reset();
void quickStart();
char version();
void setAlertThreshold(uint8_t level);
int alertThreshold();
boolean isSleeping();
boolean isAlerting();
void sleep();
void awake();
private:
void performCommand(byte address, int value);
void readFrom(byte address, byte &msb, byte &lsb);
};
#endif

86
src/src.ino

@ -0,0 +1,86 @@
// System includes for libraries
#include "Arduino.h"
#include "Wire.h"
#include "MAX1704.h"
#include "Adafruit_NeoPixel.h"
#ifdef __AVR__
#include <avr/power.h>
#endif
// Various defines / pins / etc
#define DEBUG true
#define BUTTON_CAP_TOGGLE 9
#define POT_BRIGHT_PIN A0
#define POT_COLOR_PIN A1
#define NEO_PIN 6
#define NEO_PIX_NUM 2 // FIXME: this should be 5 for final build for safe lighting
// Objects / values / etc
MAX1704 fuelGauge;
float charge_percent;
uint16_t brightness;
uint16_t color;
bool always_on = false;
Adafruit_NeoPixel neopix = Adafruit_NeoPixel(NEO_PIX_NUM, NEO_PIN, NEO_GRB + NEO_KHZ800);
void setup() {
Serial.begin(9600);
Wire.begin();
// Setup serial and wait for console
if (DEBUG) {
while(!Serial);
Serial.println("Starting up...");
}
// Setup various digital/analog inputs
pinMode(BUTTON_CAP_TOGGLE, INPUT);
pinMode(POT_BRIGHT_PIN, INPUT);
pinMode(POT_COLOR_PIN, INPUT);
// Setup / config MAX1704
fuelGauge.reset();
fuelGauge.quickStart();
// AdaFruit NeoPixel init
// Begin trinket special code (per NeoPixel examples)
#if defined (__AVR_ATtiny85__)
if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
#endif
// End of trinket special code
neopix.begin();
neopix.show(); // Init to "off"
// Various startup info (debugging)
if (DEBUG) {
fuelGauge.showConfig();
Serial.println("----------");
}
}
void loop() {
// Read various values needed
charge_percent = fuelGauge.stateOfCharge(); // Battery level
brightness = analogRead(POT_BRIGHT_PIN); // Brightness (0-1023)
color = analogRead(POT_COLOR_PIN); // Color (0-1023 mapped to color wheel)
always_on = (digitalRead(BUTTON_CAP_TOGGLE) == HIGH); // Toggle button pressed == ALWAYS ON
// NeoPixel related
neopix.setPixelColor(0, neopix.Color(0, 0, 0)); // White
neopix.setPixelColor(1, neopix.Color(0, 255, 0)); // Green
neopix.show();
// Debugging code
if (DEBUG) {
Serial.print("Battery %: ");
Serial.println(charge_percent);
Serial.print("Brightness: ");
Serial.println(brightness);
Serial.print("Color: ");
Serial.println(color);
Serial.print("Always on: ");
Serial.println(always_on);
Serial.println("----------");
delay(1000);
}
}
Loading…
Cancel
Save