Skip to content

Commit

Permalink
added "low battery" warning icon
Browse files Browse the repository at this point in the history
  • Loading branch information
barry-ha committed Mar 11, 2024
1 parent 013bacf commit 585f0d2
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 29 deletions.
5 changes: 5 additions & 0 deletions constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ const int BRIGHT = 32; // = tolerably bright indoors
const int HALFBR = 20; // = half of tolerably bright
const int OFF = 0; // = turned off

// ------- Coin Battery good/bad thresholds ---------
const float GOOD_BATTERY_MINIMUM = (2.25); // green, if above this voltage
const float WARNING_BATTERY_MINIMUM = (2.00); // yellow, if above this voltage
const float BAD_BATTERY_MAXIMUM = (2.00); // red, if below this voltage

// ----- Griduino color scheme
// RGB 565 true color: https://chrishewett.com/blog/true-rgb565-colour-picker/
#define BACKGROUND 0x00A // a little darker than ILI9341_NAVY
Expand Down
7 changes: 7 additions & 0 deletions icons.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,10 @@ const unsigned char iconRightArrow18[] PROGMEM = {
0xfc, 0x00, 0x7e, 0x00, 0x3f, 0x00, 0x1f, 0x80, 0x0f, 0xc0, 0x07, 0xe0, 0x03, 0xf0, 0x01, 0xf8,
0x00, 0xfc, 0x00, 0xfc, 0x01, 0xf8, 0x03, 0xf0, 0x07, 0xe0, 0x0f, 0xc0, 0x1f, 0x80, 0x3f, 0x00,
0x7e, 0x00, 0xfc, 0x00};

// '2024-03-09-low-battery-icon', 14x20px
const unsigned char iconLowBattery [] PROGMEM = {
0x1f, 0x80, 0xff, 0xf0, 0x80, 0x10, 0x80, 0x10, 0x80, 0x10, 0x80, 0x10, 0x80, 0x10, 0x80, 0x10,
0x80, 0x10, 0x80, 0x10, 0x80, 0x10, 0x80, 0x10, 0x80, 0x10, 0x80, 0x10, 0x80, 0x10, 0xff, 0xf0,
0xff, 0xf0, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00
};
27 changes: 19 additions & 8 deletions model_adc.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,25 @@ class BatteryVoltage {
void setup() {
}

float getCoinBatteryVoltage() {
#if defined(ARDUINO_ADAFRUIT_FEATHER_RP2040)
coinBattery = analogRead(A1);
float coinVoltage = coinBattery * voltsPerSample;
return coinVoltage;
#else
return -1.0; // indicate no coin battery voltage sensor
#endif
uint16_t getBatteryColor(float v) {
if (v >= GOOD_BATTERY_MINIMUM) {
return ILI9341_GREEN;
} else if (v >= WARNING_BATTERY_MINIMUM) {
return ILI9341_YELLOW;
} else {
return ILI9341_RED;
}
}

float readCoinBatteryVoltage() {
const float analogRef = 3.3; // analog reference voltage
const uint16_t analogBits = 1024; // ADC resolution is 10 bits = 2^10 = 1024

int coin_adc = analogRead(BATTERY_ADC);
float coin_voltage = (float)coin_adc * analogRef / analogBits;
// return GOOD_BATTERY_MINIMUM - 0.1; // debug - show yellow icon
return WARNING_BATTERY_MINIMUM - 0.1; // debug - show red icon
return coin_voltage; // production release
}

}; // end class BatteryVoltage
18 changes: 14 additions & 4 deletions view.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
#include "constants.h" // Griduino constants and colors
#include "logger.h" // conditional printing to Serial port
#include "icons.h" // bitmaps for icons
#include "model_adc.h" // Model of the analog-digital converter

extern bool showTouchTargets; // Griduino.ino
extern bool showCenterline; // Griduino.ino
extern bool showTouchTargets; // Griduino.ino
extern bool showCenterline; // Griduino.ino
extern BatteryVoltage gpsBattery; // model_adc.h

// ========== abstract base class ViewCfgAudioType ================
class View {
Expand Down Expand Up @@ -92,15 +94,23 @@ class View {

void drawAllIcons() {
// draw gear (settings) and arrow (next screen)
// ul x,y w,h color
tft->drawBitmap(5, 5, iconGear20, 20, 20, cFAINT); // "settings" upper left
// ul x,y w,h color
tft->drawBitmap(05, 05, iconGear20, 20, 20, cFAINT); // "settings" upper left
tft->drawBitmap(300, 5, iconRightArrow18, 14, 18, cFAINT); // "next screen" upper right

float volts = gpsBattery.readCoinBatteryVoltage();
if (volts < GOOD_BATTERY_MINIMUM) {
int iconColor = gpsBattery.getBatteryColor(volts);
tft->drawBitmap(36, 06, iconLowBattery, 14, 20, iconColor); // "low battery" warning
}
}

void showScreenBorder() { // optionally outline visible area
#ifdef SHOW_SCREEN_BORDER
tft->drawRect(0, 0, gScreenWidth, gScreenHeight, ILI9341_BLUE); // debug: border around screen
#endif
}

void showScreenCenterline() {
if (showCenterline) {
// show centerline at x1,y1 x2,y2 color
Expand Down
22 changes: 5 additions & 17 deletions view_battery.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@
#include "logger.h" // conditional printing to Serial port
#include "TextField.h" // Optimize TFT display text for proportional fonts
#include "view.h" // Base class for all views
#include "model_adc.h" // Model of the analog-digital converter

// ========== extern ===========================================
extern Logger logger; // Griduino.ino
void floatToCharArray(char *result, int maxlen, double fValue, int decimalPlaces); // Griduino.ino
extern BatteryVoltage gpsBattery; // model_adc.h

// ========== class ViewBattery =================================
class ViewBattery : public View {
Expand Down Expand Up @@ -68,7 +70,7 @@ class ViewBattery : public View {

const int xV = 150; // left-align values

const int xVolts = 218; // "2.951" voltage display
const int xVolts = 218; // "2.951" voltage display, pixels
const int yVolts = (gScreenHeight / 2) - 40; //

// ----- screen text
Expand Down Expand Up @@ -113,28 +115,14 @@ class ViewBattery : public View {

int previous_y0 = 0; // (pixels) optimize screen draw

// local functions
uint16_t getColor(float v) {
if (v >= 2.25) {
return ILI9341_GREEN;
} else if (v >= 2.0) {
return ILI9341_YELLOW;
} else {
return ILI9341_RED;
}
}
}; // end class ViewBattery

// ============== implement public interface ================
void ViewBattery::updateScreen() {
// called on every pass through main()

// update measured voltage
const float analogRef = 3.3; // analog reference voltage
const uint16_t analogBits = 1024; // ADC resolution is 10 bits

int coin_adc = analogRead(BATTERY_ADC);
float coin_voltage = (float)coin_adc * analogRef / analogBits;
float coin_voltage = gpsBattery.readCoinBatteryVoltage();
char sVolts[12];
floatToCharArray(sVolts, sizeof(sVolts), coin_voltage, 3);
txtValues[MEASUREMENT].print(sVolts);
Expand All @@ -143,7 +131,7 @@ void ViewBattery::updateScreen() {
int percent = (int)((coin_voltage - 1.5) / (3.5 - 1.5) * 100);
int y0 = map(percent, 0, 100, gyLR, gyUL);
y0 = constrain(y0, gyUL, gyLR - 1);
int color = getColor(coin_voltage);
int color = gpsBattery.getBatteryColor(coin_voltage);

// ----- if no change since last pass, do nothing
if (true) { // (y0 != previous_y0) {
Expand Down

0 comments on commit 585f0d2

Please sign in to comment.