aboutsummaryrefslogtreecommitdiff
path: root/hw/hank/emisar-d3aa/hwdef.c
diff options
context:
space:
mode:
authorSelene ToyKeeper2024-03-25 04:25:43 -0600
committerSelene ToyKeeper2024-03-25 04:25:43 -0600
commita87812f436e08b14a7cede83e30306d779774872 (patch)
tree42ea09d981ade4d582a01f9c03dc996dc0d3f57b /hw/hank/emisar-d3aa/hwdef.c
parentd3aa fine-tuning: (diff)
downloadanduril-a87812f436e08b14a7cede83e30306d779774872.tar.gz
anduril-a87812f436e08b14a7cede83e30306d779774872.tar.bz2
anduril-a87812f436e08b14a7cede83e30306d779774872.zip
dammit, got alkaline detection half working and then my flashing adapter died
(saving progress here so I can work on a different branch)
Diffstat (limited to '')
-rw-r--r--hw/hank/emisar-d3aa/hwdef.c64
1 files changed, 64 insertions, 0 deletions
diff --git a/hw/hank/emisar-d3aa/hwdef.c b/hw/hank/emisar-d3aa/hwdef.c
index e2fd315..f6cf94e 100644
--- a/hw/hank/emisar-d3aa/hwdef.c
+++ b/hw/hank/emisar-d3aa/hwdef.c
@@ -4,6 +4,8 @@
#pragma once
#include "fsm/chan-rgbaux.c"
+#include "fsm/ramping.h"
+#include "ui/anduril/misc.h"
void set_level_zero();
@@ -110,3 +112,65 @@ uint8_t voltage_raw2cooked(uint16_t measurement) {
}
#endif
+#ifdef USE_WEAK_BATTERY_PROTECTION
+uint8_t quick_volt_measurement() {
+ // take the average of a few samples
+ // (assumes the ADC is in voltage mode and running continuously)
+ uint16_t total = 0;
+ for (uint8_t i=0; i<8; i++) {
+ uint16_t m = adc_raw[0];
+ total += voltage_raw2cooked(m);
+ delay_zero();
+ }
+ uint8_t v = total / 8;
+ return v;
+}
+
+void detect_weak_battery() {
+ // guess at the cell strength with a load test...
+ // - measure voltage while LEDs off
+ // - measure again with LEDs on
+ // - determine how much to limit power
+ // - blink to indicate weak battery mode, if active
+
+ uint16_t resting, loaded;
+
+ set_level(0);
+ resting = quick_volt_measurement();
+
+ set_level(WEAK_BATTERY_CHECK_LEVEL);
+ loaded = quick_volt_measurement();
+ set_level(0);
+
+ int16_t diff = resting - loaded;
+ uint8_t extra_blinks = 0;
+
+ if (loaded <= DUAL_VOLTAGE_LOW_LOW) {
+ // weak or empty AA battery has a low limit
+ ramp_level_hard_limit = WEAK_BATTERY_LOWEST_LIMIT;
+ extra_blinks = 2;
+ } else if (loaded >= VOLTAGE_RED) {
+ // reasonably strong li-ion battery
+ ramp_level_hard_limit = WEAK_BATTERY_HIGHEST_LIMIT;
+ } else if (diff <= (-5 * 4)) {
+ // marginal battery, dropped a lot under mild load
+ ramp_level_hard_limit = WEAK_BATTERY_MEDIUM_LIMIT;
+ extra_blinks = 1;
+ }
+
+ for (uint8_t i=0; i<extra_blinks; i++) {
+ delay_4ms(300/4);
+ blink_once();
+ }
+
+ voltage = resting;
+ battcheck();
+
+ voltage = loaded;
+ battcheck();
+
+ //if (diff < 0)
+ // blink_num(-diff);
+}
+#endif
+