diff options
author | lonkaars <loek@pipeframe.xyz> | 2022-06-26 17:40:36 +0200 |
---|---|---|
committer | lonkaars <loek@pipeframe.xyz> | 2022-06-26 17:40:36 +0200 |
commit | 2a0270f3ba6eb993fb39ed3564f626d724156654 (patch) | |
tree | d8d310eb35768f84d25453eae4b8cd4721bd8ddf /robot/io.c | |
parent | 55fe6aaeee49894dc07516f0c4e21f692b2950fe (diff) |
implement battery measurement and target area switching
Diffstat (limited to 'robot/io.c')
-rw-r--r-- | robot/io.c | 34 |
1 files changed, 31 insertions, 3 deletions
@@ -1,11 +1,16 @@ -#include "io.h" +#include <stdio.h> + #include "../shared/consts.h" #include "../shared/errcatch.h" +#include "../shared/util.h" #include "hypervisor.h" +#include "io.h" #include "modes.h" #include "orangutan_shim.h" -bool g_w2_io_object_detected = false; +uint16_t g_w2_io_battery_mv = 0; +uint8_t g_w2_io_battery_percentage = 0; +bool g_w2_io_object_detected = false; void w2_io_object_detection() { unsigned int front_distance = analog_read(W2_SIDE_SENSOR_PIN); @@ -27,9 +32,32 @@ void w2_io_object_detection() { } } +void w2_io_battery_logic() { + if (w2_hypervisor_time_end(W2_TIMER_BATTERY_MEASUREMENT) <= W2_BATTERY_MEAS_FREQ) return; + w2_hypervisor_time_start(W2_TIMER_BATTERY_MEASUREMENT); + + g_w2_io_battery_mv = 0; + for (int i = 0; i < W2_BATTERY_SAMPLES; i++) + g_w2_io_battery_mv += read_battery_millivolts() / W2_BATTERY_SAMPLES; + g_w2_io_battery_percentage = W2_RANGE( + 0, (g_w2_io_battery_mv - W2_BATTERY_EMPTY) * 100 / (W2_BATTERY_FULL - W2_BATTERY_EMPTY), + 100); + + char battery_percent[9]; + sprintf(battery_percent, "%i%% ", g_w2_io_battery_percentage); + lcd_goto_xy(0, 1); + print(battery_percent); + + if (g_w2_io_battery_percentage <= W2_BATTERY_PERCENTAGE_LOW && + g_w2_target_area != W2_AREA_CHRG) { + w2_errcatch_throw(W2_E_WARN_BATTERY_LOW); + g_w2_target_area = W2_AREA_CHRG; + } +} + void w2_io_main() { w2_io_object_detection(); - // TODO: battery status + w2_io_battery_logic(); return; } |