Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring code chunk #400

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from
2 changes: 1 addition & 1 deletion include/apps/watchfaces/OswAppWatchface.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class OswAppWatchface : public OswAppV2 {
void onButton(Button id, bool up, ButtonStateNames state) override;

#ifdef OSW_FEATURE_STATS_STEPS
static void drawStepHistory(OswUI* ui, uint8_t x, uint8_t y, uint8_t w, uint8_t h, uint32_t max);
static void drawStepHistory(OswUI* ui, uint8_t posX, uint8_t posY, uint8_t width, uint8_t height, uint32_t max);
#endif
static void addButtonDefaults(std::array<ButtonStateNames, BTN_NUMBER>& knownButtonStates);
static bool onButtonDefaults(OswAppV2& app, Button id, bool up, ButtonStateNames state);
Expand Down
10 changes: 5 additions & 5 deletions src/apps/tools/OswAppDistStats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ void OswAppDistStats::drawChart() {
uint32_t dayOfMonth = 0;
hal->getLocalDate(&dayOfMonth, &weekDay);

for (uint8_t index = 0; index < 7; index++) {
uint32_t weekDayDist = OswAppWatchfaceFitness::calculateDistance(hal->environment()->getStepsOnDay(index));
for (uint8_t indexOfWeek = 0; indexOfWeek < 7; indexOfWeek++) {
uint32_t weekDayDist = OswAppWatchfaceFitness::calculateDistance(hal->environment()->getStepsOnDay(indexOfWeek));
uint16_t chartStickValue = ((float)(weekDayDist > goalValue ? goalValue : weekDayDist) / goalValue) * chartStickHeight;

uint16_t barColor = (unsigned int) OswConfigAllKeys::distPerDay.get() <= weekDayDist ? ui->getSuccessColor() : changeColor(ui->getSuccessColor(),2.85);

chartStickValue = chartStickValue < 2 ? 0 : chartStickValue;

if (index == cursorPos) {
hal->gfx()->drawThickTick(60 + index * interval, 147, 0, chartStickHeight, 0, 5, ui->getForegroundColor());
if (indexOfWeek == cursorPos) {
hal->gfx()->drawThickTick(60 + indexOfWeek * interval, 147, 0, chartStickHeight, 0, 5, ui->getForegroundColor());
}
hal->gfx()->drawThickTick(60 + index * interval, 147, 0, chartStickValue, 0, 3, barColor, true);
hal->gfx()->drawThickTick(60 + indexOfWeek * interval, 147, 0, chartStickValue, 0, 3, barColor, true);
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/apps/tools/OswAppStepStats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@ void OswAppStepStats::drawChart() {
uint32_t dayOfMonth = 0;
hal->getLocalDate(&dayOfMonth, &weekDay);

for (uint8_t index = 0; index < 7; index++) {
unsigned int weekDayStep = hal->environment()->getStepsOnDay(index);
for (uint8_t indexOfWeek = 0; indexOfWeek < 7; indexOfWeek++) {
unsigned int weekDayStep = hal->environment()->getStepsOnDay(indexOfWeek);
unsigned short chartStickValue = ((float)(weekDayStep > goalValue ? goalValue : weekDayStep) / goalValue) * chartStickHeight;

uint16_t barColor = (unsigned int) OswConfigAllKeys::stepsPerDay.get() <= weekDayStep ? ui->getSuccessColor() : changeColor(ui->getSuccessColor(), 2.85);

chartStickValue = chartStickValue < 2 ? 0 : chartStickValue;

if (index == cursorPos) {
hal->gfx()->drawThickTick(60 + index * interval, 147, 0, chartStickHeight, 0, 5, ui->getForegroundColor());
if (indexOfWeek == cursorPos) {
hal->gfx()->drawThickTick(60 + indexOfWeek * interval, 147, 0, chartStickHeight, 0, 5, ui->getForegroundColor());
}
hal->gfx()->drawThickTick(60 + index * interval, 147, 0, chartStickValue, 0, 3, barColor, true);
hal->gfx()->drawThickTick(60 + indexOfWeek * interval, 147, 0, chartStickValue, 0, 3, barColor, true);
}
}

Expand Down
24 changes: 12 additions & 12 deletions src/apps/watchfaces/OswAppWatchface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,33 +25,33 @@ const char* OswAppWatchface::getAppName() {
}

#ifdef OSW_FEATURE_STATS_STEPS
void OswAppWatchface::drawStepHistory(OswUI* ui, uint8_t x, uint8_t y, uint8_t w, uint8_t h, uint32_t max) {
void OswAppWatchface::drawStepHistory(OswUI* ui, uint8_t posX, uint8_t posY, uint8_t width, uint8_t height, uint32_t max) {
OswHal* hal = OswHal::getInstance();
OswUI::getInstance()->resetTextColors();
uint32_t weekDay = 0;
uint32_t dayOfMonth = 0;
hal->getLocalDate(&dayOfMonth, &weekDay);
for (uint8_t i = 0; i < 7; i++) {
uint32_t s = hal->environment()->getStepsOnDay(i);
uint16_t boxHeight = ((float)(s > max ? max : s) / max) * h;
boxHeight = boxHeight < 2 ? 0 : boxHeight;
for (uint8_t indexOfWeek = 0; indexOfWeek < 7; indexOfWeek++) {
uint32_t stepsCountOnDay = hal->environment()->getStepsOnDay(indexOfWeek);
uint16_t boxHeight = ((float)(stepsCountOnDay > max ? max : stepsCountOnDay) / max) * height;
boxHeight = boxHeight < 2 ? 0 : boxHeight; // minumum value is 2

// step bars
uint16_t c = (unsigned int) OswConfigAllKeys::stepsPerDay.get() <= s ? ui->getSuccessColor() : ui->getPrimaryColor();
hal->gfx()->fillFrame(x + i * w, y + (h - boxHeight), w, boxHeight, c);
uint16_t stepsCountBoxColor = (unsigned int) OswConfigAllKeys::stepsPerDay.get() <= stepsCountOnDay ? ui->getSuccessColor() : ui->getPrimaryColor();
hal->gfx()->fillFrame(posX + indexOfWeek * width, posY + (height - boxHeight), width, boxHeight, stepsCountBoxColor);
// bar frames
uint16_t f = weekDay == i ? ui->getForegroundColor() : ui->getForegroundDimmedColor();
hal->gfx()->drawRFrame(x + i * w, y, w, h, 2, f);
uint16_t frameLineColor = weekDay == indexOfWeek ? ui->getForegroundColor() : ui->getForegroundDimmedColor();
hal->gfx()->drawRFrame(posX + indexOfWeek * width, posY, width, height, 2, frameLineColor); // 2 is cornerRoundness

// labels
hal->gfx()->setTextCenterAligned(); // horiz.
hal->gfx()->setTextBottomAligned();
hal->gfx()->setTextSize(1);
hal->gfx()->setTextCursor(CENTER_X, y - 1);
hal->gfx()->setTextCursor(CENTER_X, posY - 1);

hal->gfx()->print(hal->environment()->getStepsToday() + (OswConfigAllKeys::settingDisplayStepsGoal.get() ? String("/") + max:""));

hal->gfx()->setTextCursor(CENTER_X, y + 1 + 8 + w * 4);
hal->gfx()->setTextCursor(CENTER_X, posY + 9 + width * 4);
hal->gfx()->setTextColor(ui->getForegroundColor()); // Let's make the background transparent.
// See : https://github.com/Open-Smartwatch/open-smartwatch-os/issues/194
// font : WHITE / bg : None
Expand All @@ -70,7 +70,7 @@ void OswAppWatchface::drawWatch() {
#if OSW_PLATFORM_ENVIRONMENT_ACCELEROMETER == 1
uint32_t steps = hal->environment()->getStepsToday();
uint32_t stepsTarget = OswConfigAllKeys::stepsPerDay.get();
hal->gfx()->drawArc(CENTER_X, CENTER_Y, 0, 360.0f * (float)(steps % stepsTarget) / (float)stepsTarget, 90, 93, 6,
hal->gfx()->drawArc(CENTER_X, CENTER_Y, 0, (float) (360.0f / stepsTarget * (steps % stepsTarget)), 90, 93, 6,
steps > stepsTarget ? ui->getSuccessColor() : ui->getInfoColor(), true);
#endif

Expand Down
7 changes: 3 additions & 4 deletions src/apps/watchfaces/OswAppWatchfaceDual.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@ void OswAppWatchfaceDual::drawProgressBar(OswUI* ui,uint8_t cx, uint8_t cy, uint
void OswAppWatchfaceDual::drawAnimSec() {
OswHal* hal = OswHal::getInstance();
uint8_t barWidth = 140;
uint32_t Hs, Ms, Ss = 0;
hal->getLocalTime(&Hs,&Ms,&Ss);
uint32_t onlySecond = Ss;
uint16_t barValue = ((float)onlySecond / 60) * barWidth;
uint32_t seconds = 0;
hal->getLocalTime(nullptr, nullptr, &seconds);
uint16_t barValue = ((float)seconds / 60) * barWidth;
barValue = barValue < 2 ? 0 : barValue;
uint8_t coordX = (DISP_W - barWidth) / 2;
uint8_t levelY = DISP_H / 2;
Expand Down
4 changes: 2 additions & 2 deletions src/apps/watchfaces/OswAppWatchfaceMix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ const char* OswAppWatchfaceMix::getAppName() {
void OswAppWatchfaceMix::analogWatchDisplay() {
OswHal* hal = OswHal::getInstance();
uint32_t second = 0;
uint32_t minute = 0; // Unused, but required by function signature
uint32_t hour = 0; // Unused, but required by function signature
uint32_t minute = 0;
uint32_t hour = 0;

hal->getLocalTime(&hour, &minute, &second);
hal->gfx()->drawCircle((int)(DISP_W*0.5)-OFF_SET_ANALOG_WATCH_X_COORD, 100, 50, ui->getForegroundColor());
Expand Down
30 changes: 14 additions & 16 deletions src/hal/environment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ void OswHal::Environment::setupStepStatistics() {
assert(res);
if(prefs.getBytes(PREFS_STEPS_STATS, &this->_stepsCache, sizeof(this->_stepsCache)) != sizeof(this->_stepsCache)) {
// Uoh, the steps history is not initialized -> fill it with zero and do it now!
for(size_t i = 0; i < 7; i++)
this->_stepsCache[i] = 0;
for(size_t indexOfWeek = 0; indexOfWeek < 7; indexOfWeek++)
this->_stepsCache[indexOfWeek] = 0;
res = prefs.putBytes(PREFS_STEPS_STATS, &this->_stepsCache, sizeof(this->_stepsCache)) == sizeof(this->_stepsCache);
assert(res);
} else {
Expand All @@ -132,9 +132,8 @@ void OswHal::Environment::setupStepStatistics() {
* @param alwaysPrintStepStatistics Set to true to print the step history to the console
*/
void OswHal::Environment::commitStepStatistics(const bool& alwaysPrintStepStatistics) {
uint32_t currDoM = 0; // Unused, but required by function signature
uint32_t currDoW = 0;
OswHal::getInstance()->getLocalDate(&currDoM, &currDoW);
OswHal::getInstance()->getLocalDate(nullptr, &currDoW);
bool changedDoW = currDoW != this->_stepsLastDoW;
if(changedDoW) {
Preferences prefs;
Expand All @@ -153,8 +152,8 @@ void OswHal::Environment::commitStepStatistics(const bool& alwaysPrintStepStatis
if(currDoW > 0)
for(uint32_t i = currDoW; 0 < i; i--)
this->_stepsCache[i - 1] = 0;
for(uint32_t i = this->_stepsLastDoW + 1; i < 7; i++)
this->_stepsCache[i] = 0;
for(uint32_t indexOfWeek = this->_stepsLastDoW + 1; indexOfWeek < 7; indexOfWeek++)
this->_stepsCache[indexOfWeek] = 0;
}
}

Expand All @@ -179,13 +178,13 @@ void OswHal::Environment::commitStepStatistics(const bool& alwaysPrintStepStatis
#ifndef NDEBUG
if(changedDoW or alwaysPrintStepStatistics) {
String stepHistoryDbgMsg = "Current step history (day " + String(currDoW) + ", today " + String(OswHal::getInstance()->environment()->getStepsToday()) + ", sum " + String(this->_stepsSum) + ") is: {";
for(size_t i = 0; i < 7; i++) {
if(i > 0)
for(size_t indexOfWeek = 0; indexOfWeek < 7; indexOfWeek++) {
if(indexOfWeek > 0)
stepHistoryDbgMsg += ", ";
if(i == currDoW)
if(indexOfWeek == currDoW)
stepHistoryDbgMsg += "[";
stepHistoryDbgMsg += this->_stepsCache[i];
if(i == currDoW)
stepHistoryDbgMsg += this->_stepsCache[indexOfWeek];
if(indexOfWeek == currDoW)
stepHistoryDbgMsg += "]";
}
stepHistoryDbgMsg += "}";
Expand Down Expand Up @@ -225,14 +224,13 @@ uint32_t OswHal::Environment::getStepsTotalWeek() {
#ifdef OSW_FEATURE_STATS_STEPS
this->commitStepStatistics();
uint32_t sum = 0;
uint32_t currDoM = 0; // Unused, but required by function signature
uint32_t currDoW = 0;
OswHal::getInstance()->getLocalDate(&currDoM, &currDoW);
for (uint8_t i = 0; i < 7; i++) {
if (i == currDoW) {
OswHal::getInstance()->getLocalDate(nullptr, &currDoW);
for (uint8_t indexOfWeek = 0; indexOfWeek < 7; indexOfWeek++) {
if (indexOfWeek == currDoW) {
sum = sum + this->getStepsToday();
}
sum = sum + this->_stepsCache[i];
sum = sum + this->_stepsCache[indexOfWeek];
}
return sum;
#else
Expand Down
63 changes: 46 additions & 17 deletions src/hal/time.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,34 +29,54 @@ void OswHal::updateTimeProvider() {
void OswHal::getUTCTime(uint32_t* hour, uint32_t* minute, uint32_t* second) {
RtcDateTime d = RtcDateTime();
d.InitWithUnix32Time(this->getUTCTime());
*hour = d.Hour();
*minute = d.Minute();
*second = d.Second();
if (hour != nullptr){
*hour = d.Hour();
}
if (minute != nullptr){
*minute = d.Minute();
}
if (second != nullptr){
*second = d.Second();
}
}

void OswHal::getTime(time_t& offset, uint32_t* hour, uint32_t* minute, uint32_t* second, bool* afterNoon) {
RtcDateTime d = RtcDateTime();
d.InitWithUnix32Time(this->getTime(offset));
if (!OswConfigAllKeys::timeFormat.get()) {
if (d.Hour() > 12) {
*hour = d.Hour() - 12;
if(hour != nullptr){
*hour = d.Hour() - 12;
}
if (afterNoon != nullptr) *afterNoon = true;
} else if (d.Hour() == 0) {
*hour = 12;
if(hour != nullptr){
*hour = 12;
}
if (afterNoon != nullptr) *afterNoon = false;
} else if (d.Hour() == 12) {
*hour = d.Hour();
if(hour != nullptr){
*hour = d.Hour();
}
if (afterNoon != nullptr) *afterNoon = true;
} else {
*hour = d.Hour();
if(hour != nullptr){
*hour = d.Hour();
}
if (afterNoon != nullptr) *afterNoon = false;
}
} else {
*hour = d.Hour();
if(hour != nullptr){
*hour = d.Hour();
}
if (afterNoon != nullptr) *afterNoon = false;
}
*minute = d.Minute();
*second = d.Second();
if (minute != nullptr){
*minute = d.Minute();
}
if (second != nullptr){
*second = d.Second();
}
}

/**
Expand Down Expand Up @@ -125,24 +145,33 @@ time_t OswHal::getTime(time_t& offset) {
void OswHal::getDate(time_t& offset, uint32_t* day, uint32_t* weekDay) {
RtcDateTime d = RtcDateTime();
d.InitWithUnix32Time(this->getTime(offset));
*weekDay = d.DayOfWeek();
*day = d.Day();
if (weekDay != nullptr){
*weekDay = d.DayOfWeek();
}
if (day != nullptr){
*day = d.Day();
}
}

void OswHal::getDate(time_t& offset, uint32_t* day, uint32_t* month, uint32_t* year) {
RtcDateTime d = RtcDateTime();
d.InitWithUnix32Time(this->getTime(offset));
*day = d.Day();
*month = d.Month();
*year = d.Year();
if (day != nullptr){
*day = d.Day();
}
if (month != nullptr){
*month = d.Month();
}
if (year != nullptr){
*year = d.Year();
}
}

const char* OswHal::getWeekday(time_t& offset, uint32_t* setWDay) {
uint32_t day = 0;
uint32_t wDay = 0;
this->getDate(offset, &day, &wDay);

const char* dayMap[7] = {LANG_SUNDAY, LANG_MONDAY, LANG_TUESDAY, LANG_WEDNESDAY, LANG_THURSDAY, LANG_FRIDAY, LANG_SATURDAY};
this->getDate(offset, &day, &wDay);
if (setWDay != nullptr) wDay = *setWDay;
return dayMap[wDay];
}
Loading