From b5d6c691114c9f4a25e371790d2e95b0c118d233 Mon Sep 17 00:00:00 2001 From: weshatheleopard Date: Fri, 25 Jan 2013 14:06:26 -0600 Subject: [PATCH 1/4] Adding comments, moving some stuff to parameters --- hexbright_factory.ino | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/hexbright_factory.ino b/hexbright_factory.ino index 8a6a8db..2abd2bc 100755 --- a/hexbright_factory.ino +++ b/hexbright_factory.ino @@ -10,14 +10,15 @@ // Settings #define OVERTEMP 340 +#define TEMP_CHECK_INTERVAL 1000 // Pin assignments #define DPIN_RLED_SW 2 -#define DPIN_GLED 5 +#define DPIN_GLED 5 // Green LED in the pushbutton #define DPIN_PWR 8 #define DPIN_DRV_MODE 9 #define DPIN_DRV_EN 10 -#define APIN_TEMP 0 -#define APIN_CHARGE 3 +#define APIN_TEMP 0 // Temperature sensor +#define APIN_CHARGE 3 // Charge meter // Modes #define MODE_OFF 0 #define MODE_LOW 1 @@ -79,7 +80,7 @@ void loop() } // Check the temperature sensor - if (time-lastTempTime > 1000) + if (time-lastTempTime > TEMP_CHECK_INTERVAL) { lastTempTime = time; int temperature = analogRead(APIN_TEMP); From f3ea7fed07c7672636907a40e17ad8bb4903a2c3 Mon Sep 17 00:00:00 2001 From: weshatheleopard Date: Fri, 25 Jan 2013 15:35:32 -0600 Subject: [PATCH 2/4] Extracting more stuff into separate methods --- hexbright_factory.ino | 97 +++++++++++++++++++++++++------------------ 1 file changed, 56 insertions(+), 41 deletions(-) diff --git a/hexbright_factory.ino b/hexbright_factory.ino index 2abd2bc..52cdd52 100755 --- a/hexbright_factory.ino +++ b/hexbright_factory.ino @@ -11,8 +11,10 @@ // Settings #define OVERTEMP 340 #define TEMP_CHECK_INTERVAL 1000 +#define CHARGE_LEVEL_LOW 128 +#define CHARGE_LEVEL_HIGH 768 // Pin assignments -#define DPIN_RLED_SW 2 +#define DPIN_RLED_SW 2 // Pushbutton switch #define DPIN_GLED 5 // Green LED in the pushbutton #define DPIN_PWR 8 #define DPIN_DRV_MODE 9 @@ -32,55 +34,29 @@ byte mode = 0; unsigned long btnTime = 0; boolean btnDown = false; - -void setup() +void blinkOnCharge(unsigned long time) { - // We just powered on! That means either we got plugged - // into USB, or the user is pressing the power button. - pinMode(DPIN_PWR, INPUT); - digitalWrite(DPIN_PWR, LOW); - - // Initialize GPIO - pinMode(DPIN_RLED_SW, INPUT); - pinMode(DPIN_GLED, OUTPUT); - pinMode(DPIN_DRV_MODE, OUTPUT); - pinMode(DPIN_DRV_EN, OUTPUT); - digitalWrite(DPIN_DRV_MODE, LOW); - digitalWrite(DPIN_DRV_EN, LOW); - - // Initialize serial busses - Serial.begin(9600); - Wire.begin(); - - btnTime = millis(); - btnDown = digitalRead(DPIN_RLED_SW); - mode = MODE_OFF; - - Serial.println("Powered up!"); -} - -void loop() -{ - static unsigned long lastTempTime; - unsigned long time = millis(); - // Check the state of the charge controller int chargeState = analogRead(APIN_CHARGE); - if (chargeState < 128) // Low - charging + if (chargeState < CHARGE_LEVEL_LOW) { - digitalWrite(DPIN_GLED, (time&0x0100)?LOW:HIGH); + digitalWrite(DPIN_GLED, (time & 0x0100) ? LOW : HIGH); } - else if (chargeState > 768) // High - charged + else if (chargeState > CHARGE_LEVEL_HIGH) { digitalWrite(DPIN_GLED, HIGH); } else // Hi-Z - shutdown { - digitalWrite(DPIN_GLED, LOW); + digitalWrite(DPIN_GLED, LOW); } - +} + +void preventOverheating(unsigned long time) +{ + static unsigned long lastTempTime; // Check the temperature sensor - if (time-lastTempTime > TEMP_CHECK_INTERVAL) + if (time - lastTempTime > TEMP_CHECK_INTERVAL) { lastTempTime = time; int temperature = analogRead(APIN_TEMP); @@ -102,20 +78,59 @@ void loop() mode = MODE_LOW; } } +} + +void initSwitch() { + pinMode(DPIN_RLED_SW, OUTPUT); + pinMode(DPIN_RLED_SW, INPUT); +} + + +void setup() +{ + // We just powered on! That means either we got plugged + // into USB, or the user is pressing the power button. + pinMode(DPIN_PWR, INPUT); + digitalWrite(DPIN_PWR, LOW); + + // Initialize GPIO + initSwitch(); + pinMode(DPIN_GLED, OUTPUT); + pinMode(DPIN_DRV_MODE, OUTPUT); + pinMode(DPIN_DRV_EN, OUTPUT); + digitalWrite(DPIN_DRV_MODE, LOW); + digitalWrite(DPIN_DRV_EN, LOW); + + // Initialize serial busses + Serial.begin(9600); + Wire.begin(); + + btnTime = millis(); + btnDown = digitalRead(DPIN_RLED_SW); + mode = MODE_OFF; + + Serial.println("Powered up!"); +} + +void loop() +{ + unsigned long time = millis(); + + blinkOnCharge(time); + preventOverheating(time); // Do whatever this mode does switch (mode) { case MODE_BLINKING: case MODE_BLINKING_PREVIEW: - digitalWrite(DPIN_DRV_EN, (time%300)<75); + digitalWrite(DPIN_DRV_EN, (time % 300) < 75); break; } // Periodically pull down the button's pin, since // in certain hardware revisions it can float. - pinMode(DPIN_RLED_SW, OUTPUT); - pinMode(DPIN_RLED_SW, INPUT); + initSwitch(); // Check for mode changes byte newMode = mode; From 22ab891427e33c1d14c5dad4c4d9e1e97377416b Mon Sep 17 00:00:00 2001 From: weshatheleopard Date: Fri, 25 Jan 2013 17:16:40 -0600 Subject: [PATCH 3/4] Moving more stuff into functions that can be later extracted into the lobrary --- hexbright_factory.ino | 180 ++++++++++++++++++++++++------------------ 1 file changed, 103 insertions(+), 77 deletions(-) diff --git a/hexbright_factory.ino b/hexbright_factory.ino index 52cdd52..dbb9e19 100755 --- a/hexbright_factory.ino +++ b/hexbright_factory.ino @@ -16,9 +16,9 @@ // Pin assignments #define DPIN_RLED_SW 2 // Pushbutton switch #define DPIN_GLED 5 // Green LED in the pushbutton -#define DPIN_PWR 8 -#define DPIN_DRV_MODE 9 -#define DPIN_DRV_EN 10 +#define DPIN_PWR 8 // Main LED power on/off +#define DPIN_DRV_MODE 9 // Main LED brightness mode (low/high) +#define DPIN_DRV_EN 10 // Main LED brightness value #define APIN_TEMP 0 // Temperature sensor #define APIN_CHARGE 3 // Charge meter // Modes @@ -34,79 +34,111 @@ byte mode = 0; unsigned long btnTime = 0; boolean btnDown = false; +/* ------------------- HexBright Hardware Library Functions ------------------- */ + +void setGreenLED(bool on) +{ + pinMode(DPIN_GLED, OUTPUT); + digitalWrite(DPIN_GLED, on ? HIGH : LOW); +} + +void setMainLED(bool on) +{ + pinMode(DPIN_PWR, OUTPUT); + digitalWrite(DPIN_PWR, on ? HIGH : LOW); +} + +void setHighBrightness(bool on) +{ + pinMode(DPIN_DRV_MODE, OUTPUT); + digitalWrite(DPIN_DRV_MODE, on ? HIGH : LOW); +} + +void setBrightnessValue(byte value) +{ + pinMode(DPIN_DRV_EN, OUTPUT); + analogWrite(DPIN_DRV_EN, value); +} + +int getChargeLevel(void) +{ + return(analogRead(APIN_CHARGE)); +} + +int getTemperature(void) +{ + return(analogRead(APIN_TEMP)); +} + +int getButtonState(void) +{ + // Pull down the button's pin, since + // in certain hardware revisions it can float. + pinMode(DPIN_RLED_SW, OUTPUT); + pinMode(DPIN_RLED_SW, INPUT); + + return(digitalRead(DPIN_RLED_SW)); +} + +/* ------------ Common methods that can be reused in other firmwares ----------- */ + void blinkOnCharge(unsigned long time) { - // Check the state of the charge controller - int chargeState = analogRead(APIN_CHARGE); - if (chargeState < CHARGE_LEVEL_LOW) - { - digitalWrite(DPIN_GLED, (time & 0x0100) ? LOW : HIGH); - } - else if (chargeState > CHARGE_LEVEL_HIGH) - { - digitalWrite(DPIN_GLED, HIGH); - } + int chargeLevel = getChargeLevel(); + + if (chargeLevel < CHARGE_LEVEL_LOW) // Low charge; blink + setGreenLED(time & 0x0100); + else if (chargeLevel > CHARGE_LEVEL_HIGH) // Full charge: steady + setGreenLED(true); else // Hi-Z - shutdown - { - digitalWrite(DPIN_GLED, LOW); - } + setGreenLED(false); } void preventOverheating(unsigned long time) { static unsigned long lastTempTime; - // Check the temperature sensor - if (time - lastTempTime > TEMP_CHECK_INTERVAL) + + // If it's too early to check the temp, return + if (time - lastTempTime < TEMP_CHECK_INTERVAL) return; + + lastTempTime = time; + int temperature = getTemperature(); + Serial.print("Temp: "); + Serial.println(temperature); + + if (mode == MODE_OFF) return; // We are off, nothing to do + if (temperature < OVERTEMP) return; // Temperature nominal, carry on. + + Serial.println("Overheating!"); + + for (int i = 0; i < 6; i++) { - lastTempTime = time; - int temperature = analogRead(APIN_TEMP); - Serial.print("Temp: "); - Serial.println(temperature); - if (temperature > OVERTEMP && mode != MODE_OFF) - { - Serial.println("Overheating!"); - - for (int i = 0; i < 6; i++) - { - digitalWrite(DPIN_DRV_MODE, LOW); - delay(100); - digitalWrite(DPIN_DRV_MODE, HIGH); - delay(100); - } - digitalWrite(DPIN_DRV_MODE, LOW); - - mode = MODE_LOW; - } + setHighBrightness(false); + delay(100); + setHighBrightness(true); + delay(100); } -} + setHighBrightness(false); -void initSwitch() { - pinMode(DPIN_RLED_SW, OUTPUT); - pinMode(DPIN_RLED_SW, INPUT); + mode = MODE_LOW; } - void setup() { // We just powered on! That means either we got plugged // into USB, or the user is pressing the power button. - pinMode(DPIN_PWR, INPUT); - digitalWrite(DPIN_PWR, LOW); + setMainLED(false); // Initialize GPIO - initSwitch(); - pinMode(DPIN_GLED, OUTPUT); - pinMode(DPIN_DRV_MODE, OUTPUT); - pinMode(DPIN_DRV_EN, OUTPUT); - digitalWrite(DPIN_DRV_MODE, LOW); - digitalWrite(DPIN_DRV_EN, LOW); + setHighBrightness(false); + setBrightnessValue(0); // Initialize serial busses Serial.begin(9600); Wire.begin(); btnTime = millis(); - btnDown = digitalRead(DPIN_RLED_SW); + btnDown = getButtonState(); mode = MODE_OFF; Serial.println("Powered up!"); @@ -116,7 +148,7 @@ void loop() { unsigned long time = millis(); - blinkOnCharge(time); + blinkOnCharge(time); preventOverheating(time); // Do whatever this mode does @@ -124,17 +156,16 @@ void loop() { case MODE_BLINKING: case MODE_BLINKING_PREVIEW: - digitalWrite(DPIN_DRV_EN, (time % 300) < 75); + if ((time % 300) < 75) + setBrightnessValue(0); + else + setBrightnessValue(255); break; } - // Periodically pull down the button's pin, since - // in certain hardware revisions it can float. - initSwitch(); - // Check for mode changes byte newMode = mode; - byte newBtnDown = digitalRead(DPIN_RLED_SW); + byte newBtnDown = getButtonState(); switch (mode) { case MODE_OFF: @@ -173,38 +204,33 @@ void loop() { case MODE_OFF: Serial.println("Mode = off"); - pinMode(DPIN_PWR, OUTPUT); - digitalWrite(DPIN_PWR, LOW); - digitalWrite(DPIN_DRV_MODE, LOW); - digitalWrite(DPIN_DRV_EN, LOW); + setMainLED(false); + setHighBrightness(false); + setBrightnessValue(0); break; case MODE_LOW: Serial.println("Mode = low"); - pinMode(DPIN_PWR, OUTPUT); - digitalWrite(DPIN_PWR, HIGH); - digitalWrite(DPIN_DRV_MODE, LOW); - analogWrite(DPIN_DRV_EN, 64); + setMainLED(true); + setHighBrightness(false); + setBrightnessValue(64); break; case MODE_MED: Serial.println("Mode = medium"); - pinMode(DPIN_PWR, OUTPUT); - digitalWrite(DPIN_PWR, HIGH); - digitalWrite(DPIN_DRV_MODE, LOW); - analogWrite(DPIN_DRV_EN, 255); + setMainLED(true); + setHighBrightness(false); + setBrightnessValue(255); break; case MODE_HIGH: Serial.println("Mode = high"); - pinMode(DPIN_PWR, OUTPUT); - digitalWrite(DPIN_PWR, HIGH); - digitalWrite(DPIN_DRV_MODE, HIGH); - analogWrite(DPIN_DRV_EN, 255); + setMainLED(true); + setHighBrightness(true); + setBrightnessValue(255); break; case MODE_BLINKING: case MODE_BLINKING_PREVIEW: Serial.println("Mode = blinking"); - pinMode(DPIN_PWR, OUTPUT); - digitalWrite(DPIN_PWR, HIGH); - digitalWrite(DPIN_DRV_MODE, HIGH); + setMainLED(true); + setHighBrightness(true); break; } From fdafbb42a75427897a1a470980dab021c6935a90 Mon Sep 17 00:00:00 2001 From: weshatheleopard Date: Fri, 25 Jan 2013 18:26:09 -0600 Subject: [PATCH 4/4] * Created class HexBright that hides all the hardware mumbo-jumbo and offers human readable control methods; * Added handling of the red LED in the pushbutton cap. * TODO: add accelerometer interface. --- hexbright_factory.ino | 139 ++++++++++++++++++++++++++---------------- 1 file changed, 87 insertions(+), 52 deletions(-) diff --git a/hexbright_factory.ino b/hexbright_factory.ino index dbb9e19..b2d4bc1 100755 --- a/hexbright_factory.ino +++ b/hexbright_factory.ino @@ -11,16 +11,6 @@ // Settings #define OVERTEMP 340 #define TEMP_CHECK_INTERVAL 1000 -#define CHARGE_LEVEL_LOW 128 -#define CHARGE_LEVEL_HIGH 768 -// Pin assignments -#define DPIN_RLED_SW 2 // Pushbutton switch -#define DPIN_GLED 5 // Green LED in the pushbutton -#define DPIN_PWR 8 // Main LED power on/off -#define DPIN_DRV_MODE 9 // Main LED brightness mode (low/high) -#define DPIN_DRV_EN 10 // Main LED brightness value -#define APIN_TEMP 0 // Temperature sensor -#define APIN_CHARGE 3 // Charge meter // Modes #define MODE_OFF 0 #define MODE_LOW 1 @@ -36,62 +26,108 @@ boolean btnDown = false; /* ------------------- HexBright Hardware Library Functions ------------------- */ -void setGreenLED(bool on) +class HexBright +{ + public: + HexBright(void); + void setGreenLED(bool); + void setRedLED(bool); + void setMainLED(bool); + void setHighBrightness(bool); + void setBrightnessValue(byte); + int getChargeStatus(void); + int getTemperature(void); + int getButtonState(void); + private: + bool redLEDState; + + // Pin assignments + static const byte DPIN_RLED_SW = 2; // Red LED in the pushbutton / Pushbutton switch + static const byte DPIN_GLED = 5; // Green LED in the pushbutton + static const byte DPIN_PWR = 8; // Main LED power on/off + static const byte DPIN_DRV_MODE = 9; // Main LED brightness mode (low/high) + static const byte DPIN_DRV_EN = 10; // Main LED brightness value + static const byte APIN_TEMP = 0; // Temperature sensor + static const byte APIN_CHARGE = 3; // Charge status +}; + +HexBright::HexBright(void) +{ + redLEDState = false; +} + +void HexBright::setGreenLED(bool on) { pinMode(DPIN_GLED, OUTPUT); digitalWrite(DPIN_GLED, on ? HIGH : LOW); } -void setMainLED(bool on) +void HexBright::setRedLED(bool on) +{ + pinMode(DPIN_RLED_SW, OUTPUT); + redLEDState = on; + digitalWrite(DPIN_RLED_SW, on ? HIGH : LOW); +} + +void HexBright::setMainLED(bool on) { pinMode(DPIN_PWR, OUTPUT); digitalWrite(DPIN_PWR, on ? HIGH : LOW); } -void setHighBrightness(bool on) +void HexBright::setHighBrightness(bool on) { pinMode(DPIN_DRV_MODE, OUTPUT); digitalWrite(DPIN_DRV_MODE, on ? HIGH : LOW); } -void setBrightnessValue(byte value) +void HexBright::setBrightnessValue(byte value) { pinMode(DPIN_DRV_EN, OUTPUT); analogWrite(DPIN_DRV_EN, value); } -int getChargeLevel(void) +int HexBright::getChargeStatus(void) { return(analogRead(APIN_CHARGE)); } -int getTemperature(void) +int HexBright::getTemperature(void) { return(analogRead(APIN_TEMP)); } -int getButtonState(void) +int HexBright::getButtonState(void) { - // Pull down the button's pin, since - // in certain hardware revisions it can float. + // Must disable the red LED first, otherwise button read + // will read the state of the LED instead. pinMode(DPIN_RLED_SW, OUTPUT); + digitalWrite(DPIN_RLED_SW, LOW); + + // Read the button state pinMode(DPIN_RLED_SW, INPUT); + int val = digitalRead(DPIN_RLED_SW); - return(digitalRead(DPIN_RLED_SW)); + // Set the red LED back to its original state + setRedLED(redLEDState); + + return(val); } +HexBright flashlight; + /* ------------ Common methods that can be reused in other firmwares ----------- */ void blinkOnCharge(unsigned long time) { - int chargeLevel = getChargeLevel(); + int chargeStatus = flashlight.getChargeStatus(); - if (chargeLevel < CHARGE_LEVEL_LOW) // Low charge; blink - setGreenLED(time & 0x0100); - else if (chargeLevel > CHARGE_LEVEL_HIGH) // Full charge: steady - setGreenLED(true); + if (chargeStatus < 128) // Charging: blink + flashlight.setGreenLED(time & 0x0100); + else if (chargeStatus > 768) // Charged: steady + flashlight.setGreenLED(true); else // Hi-Z - shutdown - setGreenLED(false); + flashlight.setGreenLED(false); } void preventOverheating(unsigned long time) @@ -102,7 +138,7 @@ void preventOverheating(unsigned long time) if (time - lastTempTime < TEMP_CHECK_INTERVAL) return; lastTempTime = time; - int temperature = getTemperature(); + int temperature = flashlight.getTemperature(); Serial.print("Temp: "); Serial.println(temperature); @@ -113,12 +149,12 @@ void preventOverheating(unsigned long time) for (int i = 0; i < 6; i++) { - setHighBrightness(false); + flashlight.setHighBrightness(false); delay(100); - setHighBrightness(true); + flashlight.setHighBrightness(true); delay(100); } - setHighBrightness(false); + flashlight.setHighBrightness(false); mode = MODE_LOW; } @@ -127,18 +163,18 @@ void setup() { // We just powered on! That means either we got plugged // into USB, or the user is pressing the power button. - setMainLED(false); + flashlight.setMainLED(false); // Initialize GPIO - setHighBrightness(false); - setBrightnessValue(0); + flashlight.setHighBrightness(false); + flashlight.setBrightnessValue(0); // Initialize serial busses Serial.begin(9600); Wire.begin(); btnTime = millis(); - btnDown = getButtonState(); + btnDown = flashlight.getButtonState(); mode = MODE_OFF; Serial.println("Powered up!"); @@ -157,15 +193,15 @@ void loop() case MODE_BLINKING: case MODE_BLINKING_PREVIEW: if ((time % 300) < 75) - setBrightnessValue(0); + flashlight.setBrightnessValue(0); else - setBrightnessValue(255); + flashlight.setBrightnessValue(255); break; } // Check for mode changes byte newMode = mode; - byte newBtnDown = getButtonState(); + byte newBtnDown = flashlight.getButtonState(); switch (mode) { case MODE_OFF: @@ -204,33 +240,33 @@ void loop() { case MODE_OFF: Serial.println("Mode = off"); - setMainLED(false); - setHighBrightness(false); - setBrightnessValue(0); + flashlight.setMainLED(false); + flashlight.setHighBrightness(false); + flashlight.setBrightnessValue(0); break; case MODE_LOW: Serial.println("Mode = low"); - setMainLED(true); - setHighBrightness(false); - setBrightnessValue(64); + flashlight.setMainLED(true); + flashlight.setHighBrightness(false); + flashlight.setBrightnessValue(64); break; case MODE_MED: Serial.println("Mode = medium"); - setMainLED(true); - setHighBrightness(false); - setBrightnessValue(255); + flashlight.setMainLED(true); + flashlight.setHighBrightness(false); + flashlight.setBrightnessValue(255); break; case MODE_HIGH: Serial.println("Mode = high"); - setMainLED(true); - setHighBrightness(true); - setBrightnessValue(255); + flashlight.setMainLED(true); + flashlight.setHighBrightness(true); + flashlight.setBrightnessValue(255); break; case MODE_BLINKING: case MODE_BLINKING_PREVIEW: Serial.println("Mode = blinking"); - setMainLED(true); - setHighBrightness(true); + flashlight.setMainLED(true); + flashlight.setHighBrightness(true); break; } @@ -245,4 +281,3 @@ void loop() delay(50); } } -