Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // charging constants
- #define CHARGING 1
- #define BATTERY 7
- #define CHARGED 3
- // returns CHARGING, CHARGED, or BATTERY
- // This reads the charge state twice with a small delay, then returning
- // the actual charge state. BATTERY will never be returned if we are
- // plugged in.
- // Use this if you take actions based on the charge state (example: you
- // turn on when you stop charging). Takes up 56 bytes (34+22).
- static byte get_definite_charge_state();
- // returns CHARGING, CHARGED, or BATTERY
- // This reads and returns the charge state, without any verification.
- // As a result, it may report BATTERY when switching between CHARGED
- // and CHARGING.
- // Use this if you don't care if the value is sometimes wrong (charging
- // notification). Takes up 34 bytes.
- static byte get_charge_state();
- byte hexbright::get_charge_state() {
- int charge_value = analogRead(APIN_CHARGE);
- #if (DEBUG==DEBUG_CHARGE)
- Serial.print("Current charge reading: ");
- Serial.println(charge_value);
- #endif
- // <128 charging, >768 charged, battery
- if(charge_value<128)
- return CHARGING;
- else if (charge_value>768)
- return CHARGED;
- return BATTERY;
- }
- // reading twice costs us 28 bytes, but improves reliability.
- // The root problem is when the charge value goes from <128 to >768 (or the
- // reverse, from topping off), it passes through the middle range. If we
- // read at the wrong time, we can get a BATTERY value while we are still
- // plugged in.
- // Reading twice with a sufficient delay, we can guarantee that our state is correct.
- byte hexbright::get_definite_charge_state() {
- byte val1 = get_charge_state();
- // do something that will take some time...
- // delayMicroseconds costs an extra 20 bytes (because nowhere else is it called)
- // If other code needs delayMicroseconds, switch to it.
- //delayMicroseconds(30);
- read_thermal_sensor(); // delay a little...
- byte val2 = get_charge_state();
- // BATTERY & CHARGING = CHARGING, BATTERY & CHARGED = CHARGED, CHARGED & CHARGING = CHARGING
- // In essence, only return the middle value (BATTERY) if two reads report the same thing.
- return val1 & val2;
- }
Advertisement
Add Comment
Please, Sign In to add comment