"Simplified" the 7 segment to be less verbose.

- also made the formatting far less wide.
This commit is contained in:
Sir Garbagetruck 2023-05-29 21:40:49 +03:00
parent 06839fa806
commit cb360a8eae
2 changed files with 159 additions and 145 deletions

View file

@ -1,11 +1,24 @@
#include "SevenSegment.h" #include "SevenSegment.h"
SevenSegment::SevenSegment(uint16_t digitWidth, uint16_t digitHeight, uint16_t digitGap, uint16_t segmentThickness, uint16_t segmentGap) SevenSegment::SevenSegment(uint16_t digitWidth,
: m_digitWidth(digitWidth), m_digitHeight(digitHeight), m_digitGap(digitGap), m_segmentThickness(segmentThickness), m_segmentGap(segmentGap) uint16_t digitHeight,
uint16_t digitGap,
uint16_t segmentThickness,
uint16_t segmentGap)
: m_digitWidth(digitWidth),
m_digitHeight(digitHeight),
m_digitGap(digitGap),
m_segmentThickness(segmentThickness),
m_segmentGap(segmentGap)
{ {
} }
void SevenSegment::DrawDigit(GxEPD2_BW<WatchyDisplayBase, WatchyDisplayBase::HEIGHT> & display, uint8_t digit, uint16_t x, uint16_t y, uint8_t color) void SevenSegment::DrawDigit(GxEPD2_BW<WatchyDisplayBase,
WatchyDisplayBase::HEIGHT> & display,
uint8_t digit,
uint16_t x,
uint16_t y,
uint8_t color)
{ {
if (digit > 9) { if (digit > 9) {
return; return;
@ -13,6 +26,8 @@ void SevenSegment::DrawDigit(GxEPD2_BW<WatchyDisplayBase, WatchyDisplayBase::HEI
uint16_t digitX = x; uint16_t digitX = x;
uint16_t digitY = y; uint16_t digitY = y;
uint8_t drawcolors[] = { (uint8_t) (color ^ 0xFF) ,
(uint8_t) color };
// top top-right bottom-right bottom bottom-left top-left middle // top top-right bottom-right bottom bottom-left top-left middle
@ -32,51 +47,49 @@ void SevenSegment::DrawDigit(GxEPD2_BW<WatchyDisplayBase, WatchyDisplayBase::HEI
const bool * segments = drawSegments[digit]; const bool * segments = drawSegments[digit];
// Top // Top
if (segments[0]) { display.fillRect(digitX + m_segmentThickness,
display.fillRect(digitX + m_segmentThickness, digitY, m_digitWidth - 2 * m_segmentThickness, m_segmentThickness, color); digitY,
} else { m_digitWidth - 2 * m_segmentThickness,
display.fillRect(digitX + m_segmentThickness, digitY, m_digitWidth - 2 * m_segmentThickness, m_segmentThickness, color ^ 0xFF); m_segmentThickness,
} drawcolors[segments[0]]);
// Top-right // Top-right
if (segments[1]) { display.fillRect(digitX + m_digitWidth - m_segmentThickness,
display.fillRect(digitX + m_digitWidth - m_segmentThickness, digitY + m_segmentThickness, m_segmentThickness, m_digitHeight / 2 - m_segmentThickness * 2, color); digitY + m_segmentThickness,
} else { m_segmentThickness,
display.fillRect(digitX + m_digitWidth - m_segmentThickness, digitY + m_segmentThickness, m_segmentThickness, m_digitHeight / 2 - m_segmentThickness * 2, color ^ 0xFF); m_digitHeight / 2 - m_segmentThickness * 2,
} drawcolors[segments[1]]);
// Bottom-right // Bottom-right
if (segments[2]) { display.fillRect(digitX + m_digitWidth - m_segmentThickness,
display.fillRect(digitX + m_digitWidth - m_segmentThickness, digitY + m_digitHeight / 2, m_segmentThickness, m_digitHeight / 2 - m_segmentThickness * 2, color); digitY + m_digitHeight / 2, m_segmentThickness,
} else { m_digitHeight / 2 - m_segmentThickness * 2,
display.fillRect(digitX + m_digitWidth - m_segmentThickness, digitY + m_digitHeight / 2, m_segmentThickness, m_digitHeight / 2 - m_segmentThickness * 2, color ^ 0xFF); drawcolors[segments[2]]);
}
// Bottom // Bottom
if (segments[3]) { display.fillRect(digitX + m_segmentThickness,
display.fillRect(digitX + m_segmentThickness, digitY + m_digitHeight - m_segmentThickness * 2, m_digitWidth - 2 * m_segmentThickness, m_segmentThickness, color); digitY + m_digitHeight - m_segmentThickness * 2,
} else { m_digitWidth - 2 * m_segmentThickness,
display.fillRect(digitX + m_segmentThickness, digitY + m_digitHeight - m_segmentThickness * 2, m_digitWidth - 2 * m_segmentThickness, m_segmentThickness, color ^ 0xFF); m_segmentThickness,
} drawcolors[segments[3]]);
// Bottom-left // Bottom-left
if (segments[4]) { display.fillRect(digitX,
display.fillRect(digitX, digitY + m_digitHeight / 2, m_segmentThickness, m_digitHeight / 2 - m_segmentThickness * 2, color); digitY + m_digitHeight / 2,
} else { m_segmentThickness, m_digitHeight / 2 - m_segmentThickness * 2,
display.fillRect(digitX, digitY + m_digitHeight / 2, m_segmentThickness, m_digitHeight / 2 - m_segmentThickness * 2, color ^ 0xFF); drawcolors[segments[4]]);
}
// Top-left // Top-left
if (segments[5]) { display.fillRect(digitX,
display.fillRect(digitX, digitY + m_segmentThickness, m_segmentThickness, m_digitHeight / 2 - m_segmentThickness * 2, color); digitY + m_segmentThickness,
} else { m_segmentThickness,
display.fillRect(digitX, digitY + m_segmentThickness, m_segmentThickness, m_digitHeight / 2 - m_segmentThickness * 2, color ^ 0xFF); m_digitHeight / 2 - m_segmentThickness * 2,
} drawcolors[segments[5]]);
// Middle // Middle
if (segments[6]) { display.fillRect(digitX + m_segmentThickness,
display.fillRect(digitX + m_segmentThickness, digitY + m_digitHeight / 2 - m_segmentThickness, m_digitWidth - 2 * m_segmentThickness, m_segmentThickness, color); digitY + m_digitHeight / 2 - m_segmentThickness,
} else { m_digitWidth - 2 * m_segmentThickness,
display.fillRect(digitX + m_segmentThickness, digitY + m_digitHeight / 2 - m_segmentThickness, m_digitWidth - 2 * m_segmentThickness, m_segmentThickness, color ^ 0xFF); m_segmentThickness,
} drawcolors[segments[6]]);
} }

View file

@ -8,145 +8,146 @@ Menu WatchFace::m_menu;
void WatchFace::Setup() // Called after hardware is set up void WatchFace::Setup() // Called after hardware is set up
{ {
if (!m_menuSetup) { if (!m_menuSetup) {
m_menuSetup = true; m_menuSetup = true;
m_menu.Init(m_display); m_menu.Init(m_display);
} }
SetupVolatileMenuStuff(); SetupVolatileMenuStuff();
} }
void WatchFace::HandleButtonPress(uint64_t buttonMask) void WatchFace::HandleButtonPress(uint64_t buttonMask)
{ {
if (m_inMenu) { if (m_inMenu) {
m_menu.HandleButtonPress(buttonMask); m_menu.HandleButtonPress(buttonMask);
} else { } else {
if (buttonMask & MENU_BTN_MASK) { if (buttonMask & MENU_BTN_MASK) {
m_inMenu = true; m_inMenu = true;
m_menu.Redraw(false); m_menu.Redraw(false);
} }
} }
} }
void WatchFace::DrawWatchFace(bool partialRefresh) void WatchFace::DrawWatchFace(bool partialRefresh)
{ {
if (m_inMenu) { if (m_inMenu) {
return; return;
} }
m_display.setFullWindow(); m_display.setFullWindow();
if (!partialRefresh) { if (!partialRefresh) {
m_display.clearScreen(); m_display.clearScreen();
} }
m_display.fillScreen(GxEPD_WHITE); m_display.fillScreen(GxEPD_WHITE);
DrawBatteryIcon(); DrawBatteryIcon();
tmElements_t currentTime; tmElements_t currentTime;
m_RTC.read(currentTime, TZ_OFFSET); m_RTC.read(currentTime, TZ_OFFSET);
SevenSegment sevenSegment(30, 60, 6, 5, 5);
if (currentTime.Hour < 10) { SevenSegment sevenSegment(30, 60, 6, 5, 5);
sevenSegment.DrawDigit(m_display, 0, 10, 75, GxEPD_BLACK);
} else {
sevenSegment.DrawDigit(m_display, currentTime.Hour / 10, 10, 75, GxEPD_BLACK);
}
sevenSegment.DrawDigit(m_display, currentTime.Hour % 10, 50, 75, GxEPD_BLACK); if (currentTime.Hour < 10) {
sevenSegment.DrawDigit(m_display, 0, 10, 75, GxEPD_BLACK);
} else {
sevenSegment.DrawDigit(m_display, currentTime.Hour / 10, 10, 75, GxEPD_BLACK);
}
if (currentTime.Minute < 10) { sevenSegment.DrawDigit(m_display, currentTime.Hour % 10, 50, 75, GxEPD_BLACK);
sevenSegment.DrawDigit(m_display, 0, 100, 75, GxEPD_BLACK);
} else {
sevenSegment.DrawDigit(m_display, currentTime.Minute / 10, 100, 75, GxEPD_BLACK);
}
sevenSegment.DrawDigit(m_display, currentTime.Minute % 10, 140, 75, GxEPD_BLACK); if (currentTime.Minute < 10) {
sevenSegment.DrawDigit(m_display, 0, 100, 75, GxEPD_BLACK);
} else {
sevenSegment.DrawDigit(m_display, currentTime.Minute / 10, 100, 75, GxEPD_BLACK);
}
m_display.fillRect(87, 90, 5, 5, GxEPD_BLACK); sevenSegment.DrawDigit(m_display, currentTime.Minute % 10, 140, 75, GxEPD_BLACK);
m_display.fillRect(87, 110, 5, 5, GxEPD_BLACK);
m_display.display(partialRefresh); m_display.fillRect(87, 90, 5, 5, GxEPD_BLACK);
m_display.fillRect(87, 110, 5, 5, GxEPD_BLACK);
m_display.display(partialRefresh);
} }
void WatchFace::DrawBatteryIcon() void WatchFace::DrawBatteryIcon()
{ {
m_display.fillRect(200 - 48, 5, 43, 23, GxEPD_BLACK); m_display.fillRect(200 - 48, 5, 43, 23, GxEPD_BLACK);
m_display.fillRect(200 - 46, 7, 39, 19, GxEPD_WHITE); m_display.fillRect(200 - 46, 7, 39, 19, GxEPD_WHITE);
float VBAT = GetBatteryVoltage(); float VBAT = GetBatteryVoltage();
float level = (VBAT - 3.6f) / 0.6f; float level = (VBAT - 3.6f) / 0.6f;
if (level > 1.0f) { if (level > 1.0f) {
level = 1.0f; level = 1.0f;
} else if (level < 0.0f) { } else if (level < 0.0f) {
level = 0.0f; level = 0.0f;
} }
level = 0.5f - sin(asin(1.0f - 2.0f * level) / 3.0f); level = 0.5f - sin(asin(1.0f - 2.0f * level) / 3.0f);
m_display.fillRect(200 - 44, 9, (int)std::round(35.0f * level), 15, GxEPD_BLACK); m_display.fillRect(200 - 44, 9, (int)std::round(35.0f * level), 15, GxEPD_BLACK);
} }
void WatchFace::SetupVolatileMenuStuff() void WatchFace::SetupVolatileMenuStuff()
{ {
static const std::vector<MenuPage> menuPages = { static const std::vector<MenuPage> menuPages = {
{ {
0, // backPageNum 0, // backPageNum
"WATCHY", // title "WATCHY", // title
ALIGNMENT_CENTER, // titleAlignment ALIGNMENT_CENTER, // titleAlignment
"", // body "", // body
{ // Menu items { // Menu items
{ {
"Sync NTP", // title "Sync NTP", // title
nullptr, // callback nullptr, // callback
1 // pageNum 1 // pageNum
}, },
{ {
"Back", // title "Back", // title
nullptr, // callback nullptr, // callback
0 // pageNum 0 // pageNum
} }
}, },
}, },
{ {
0, // backPageNum 0, // backPageNum
"SYNC NTP", // title "SYNC NTP", // title
ALIGNMENT_CENTER, // titleAlignment ALIGNMENT_CENTER, // titleAlignment
"Sync with:\n" NTP_SERVER, // body "Sync with:\n" NTP_SERVER, // body
{ // Menu items { // Menu items
{ {
"Sync", // title "Sync", // title
std::bind(&WatchFace::MenuNTPSyncSelected, this), // callback std::bind(&WatchFace::MenuNTPSyncSelected, this), // callback
1 // pageNum 1 // pageNum
}, },
{ {
"Back", // title "Back", // title
nullptr, // callback nullptr, // callback
0 // pageNum 0 // pageNum
} }
}, },
}, },
}; };
m_menu.SetPages(menuPages); m_menu.SetPages(menuPages);
m_menu.SetExitCallback(std::bind(&WatchFace::MenuExited, this)); m_menu.SetExitCallback(std::bind(&WatchFace::MenuExited, this));
} }
void WatchFace::MenuExited() void WatchFace::MenuExited()
{ {
if (m_inMenu) { if (m_inMenu) {
m_inMenu = false; m_inMenu = false;
DrawWatchFace(false); DrawWatchFace(false);
} }
} }
void WatchFace::MenuNTPSyncSelected() void WatchFace::MenuNTPSyncSelected()
{ {
ConnectWiFi(); ConnectWiFi();
SyncNTPTime(); SyncNTPTime();
DisconnectWiFi(); DisconnectWiFi();
if (m_inMenu) { if (m_inMenu) {
m_inMenu = false; m_inMenu = false;
m_menu.Reset(); m_menu.Reset();
DrawWatchFace(false); DrawWatchFace(false);
} }
} }