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

Simplifies API usage #405

Merged
merged 9 commits into from
Oct 22, 2024
58 changes: 34 additions & 24 deletions include/osw_hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@
#define ERR_SD_MISSING 1
#define ERR_SD_MOUNT_FAILED 2

typedef struct {
uint32_t hour;
uint32_t minute;
uint32_t second;
bool afterNoon;
} OswTime;

typedef struct {
uint32_t year;
uint32_t month;
uint32_t day;
uint32_t weekDay;
} OswDate;

class OswHal {
public:
static OswHal* getInstance();
Expand Down Expand Up @@ -181,51 +195,47 @@ class OswHal {
// UTC Time
void setUTCTime(const time_t& epoch);
time_t getUTCTime();
void getUTCTime(uint32_t* hour, uint32_t* minute, uint32_t* second);
void getUTCTime(OswTime& oswTime);

// Offset getters for primary / secondary time (cached!)
time_t getTimezoneOffsetPrimary();
time_t getTimezoneOffsetSecondary();

// New time functions with offset
time_t getTime(time_t& offset);
void getTime(time_t& offset, uint32_t* hour, uint32_t* minute, uint32_t* second, bool* afterNoon = nullptr);
void getDate(time_t& offset, uint32_t* day, uint32_t* weekDay);
void getDate(time_t& offset, uint32_t* day, uint32_t* month, uint32_t* year);
const char* getWeekday(time_t& offset, uint32_t* setWDay = nullptr);
void getTime(time_t& offset, OswTime& oswTime);
void getDate(time_t& offset, OswDate& oswDate);

// For backward compatibility: Local time functions (= primary timezone)
inline void getLocalTime(uint32_t* hour, uint32_t* minute, uint32_t* second, bool* afterNoon = nullptr) {
this->getTime(this->timezoneOffsetPrimary, hour, minute, second, afterNoon);
inline void getLocalTime(OswTime& oswTime) {
this->getTime(this->timezoneOffsetPrimary, oswTime);
}
inline uint32_t getLocalTime() {
return this->getTime(this->timezoneOffsetPrimary);
}
inline void getLocalDate(uint32_t* day, uint32_t* weekDay) {
this->getDate(this->timezoneOffsetPrimary, day, weekDay);
};
inline void getLocalDate(uint32_t* day, uint32_t* month, uint32_t* year) {
this->getDate(this->timezoneOffsetPrimary, day, month, year);
};
inline const char* getLocalWeekday(uint32_t* sWDay = nullptr) {
return this->getWeekday(this->timezoneOffsetPrimary, sWDay);
inline void getLocalDate(OswDate& oswDate) {
this->getDate(this->timezoneOffsetPrimary, oswDate);
};

// For backward compatibility: Dual time functions (= secondary timezone)
inline void getDualTime(uint32_t* hour, uint32_t* minute, uint32_t* second, bool* afterNoon = nullptr) {
this->getTime(this->timezoneOffsetSecondary, hour, minute, second, afterNoon);
inline void getDualTime(OswTime& oswTime) {
this->getTime(this->timezoneOffsetSecondary, oswTime);
}
inline uint32_t getDualTime() {
return this->getTime(this->timezoneOffsetSecondary);
}
inline void getDualDate(uint32_t* day, uint32_t* weekDay) {
this->getDate(this->timezoneOffsetSecondary, day, weekDay);
inline void getDualDate(OswDate& oswDate) {
this->getDate(this->timezoneOffsetPrimary, oswDate);
};
inline void getDualDate(uint32_t* day, uint32_t* month, uint32_t* year) {
this->getDate(this->timezoneOffsetSecondary, day, month, year);
};
inline const char* getDualWeekday(uint32_t* sWDay = nullptr) {
return this->getWeekday(this->timezoneOffsetSecondary, sWDay);

const std::array<const char*, 7> getWeekDay = {
LANG_SUNDAY,
LANG_MONDAY,
LANG_TUESDAY,
LANG_WEDNESDAY,
LANG_THURSDAY,
LANG_FRIDAY,
LANG_SATURDAY
};

bool _requestDisableBuffer = false;
Expand Down
13 changes: 4 additions & 9 deletions src/apps/tools/OswAppDistStats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ void OswAppDistStats::drawChart() {
uint8_t interval = 20;
uint16_t goalValue = OswConfigAllKeys::distPerDay.get();

uint32_t weekDay = 0;
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));
uint16_t chartStickValue = ((float)(weekDayDist > goalValue ? goalValue : weekDayDist) / goalValue) * chartStickHeight;
Expand Down Expand Up @@ -46,15 +42,14 @@ void OswAppDistStats::showStickChart() {
hal->gfx()->print(LANG_DISTSTATS_TITLE);

OswAppDistStats::drawChart();
OswAppStepStats::drawInfoPanel(ui, (uint32_t)cursorPos, OswAppWatchfaceFitness::calculateDistance(hal->environment()->getStepsOnDay((uint32_t)cursorPos, true)), OswAppWatchfaceFitness::calculateDistance(hal->environment()->getStepsOnDay((uint32_t)cursorPos)), OswAppWatchfaceFitness::calculateDistance(hal->environment()->getStepsAverage()), OswAppWatchfaceFitness::calculateDistance(hal->environment()->getStepsTotalWeek()), " m");
OswAppStepStats::drawInfoPanel(ui, cursorPos, OswAppWatchfaceFitness::calculateDistance(hal->environment()->getStepsOnDay((uint32_t)cursorPos, true)), OswAppWatchfaceFitness::calculateDistance(hal->environment()->getStepsOnDay((uint32_t)cursorPos)), OswAppWatchfaceFitness::calculateDistance(hal->environment()->getStepsAverage()), OswAppWatchfaceFitness::calculateDistance(hal->environment()->getStepsTotalWeek()), " m");
}

void OswAppDistStats::setup() {
OswHal* hal = OswHal::getInstance();
uint32_t weekDay = 0;
uint32_t dayOfMonth = 0;
hal->getLocalDate(&dayOfMonth, &weekDay);
cursorPos = weekDay;
OswDate oswDate = { };
hal->getLocalDate(oswDate);
cursorPos = oswDate.weekDay;
}
void OswAppDistStats::loop() {
OswHal* hal = OswHal::getInstance();
Expand Down
6 changes: 3 additions & 3 deletions src/apps/tools/OswAppKcalStats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@

uint32_t findCursorWeekDay(uint8_t Index) { // Show the day of the week that cursor (Dynamic weekDay--info)
OswHal* hal = OswHal::getInstance();
uint32_t d, wD = 0;
hal->getLocalDate(&d, &wD);
int cursorWeekDay = wD - (6 - Index);
OswDate oswDate = { };
hal->getLocalDate(oswDate);
int cursorWeekDay = oswDate.weekDay - (6 - Index);
int findWeekDay = (cursorWeekDay >= 0) ? cursorWeekDay : (cursorWeekDay + 7);
uint32_t fWD = findWeekDay;
return fWD;
Expand Down
16 changes: 7 additions & 9 deletions src/apps/tools/OswAppStepStats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ void OswAppStepStats::drawChart() {
uint8_t interval = 20;
uint16_t goalValue = OswConfigAllKeys::stepsPerDay.get();

uint32_t weekDay = 0;
uint32_t dayOfMonth = 0;
hal->getLocalDate(&dayOfMonth, &weekDay);

for (uint8_t index = 0; index < 7; index++) {
unsigned int weekDayStep = hal->environment()->getStepsOnDay(index);
unsigned short chartStickValue = ((float)(weekDayStep > goalValue ? goalValue : weekDayStep) / goalValue) * chartStickHeight;
Expand All @@ -43,7 +39,10 @@ void OswAppStepStats::drawInfoPanel(OswUI* ui, uint32_t pos, uint32_t lastWeekDa
hal->gfx()->setTextCenterAligned();
hal->gfx()->setTextBottomAligned();
hal->gfx()->setTextCursor(DISP_W / 2, 170);
hal->gfx()->print(hal->getLocalWeekday(&pos));
OswDate oswDate = { };
hal->getLocalDate(oswDate);
const char* weekday = hal->getWeekDay[oswDate.weekDay];
RuffaloLavoisier marked this conversation as resolved.
Show resolved Hide resolved
hal->gfx()->print(weekday);
hal->gfx()->setTextCursor(DISP_W / 2, 190);
hal->gfx()->print(String(lastWeekData)); // lastweek(before 7 day)
hal->gfx()->setTextCursor(DISP_W / 2, 215);
Expand All @@ -69,10 +68,9 @@ void OswAppStepStats::showStickChart() {

void OswAppStepStats::setup() {
OswHal* hal = OswHal::getInstance();
uint32_t weekDay = 0;
uint32_t dayOfMonth = 0;
hal->getLocalDate(&dayOfMonth, &weekDay);
cursorPos = weekDay;
OswDate oswDate = { };
hal->getLocalDate(oswDate);
cursorPos = oswDate.weekDay;
}
void OswAppStepStats::loop() {
OswHal* hal = OswHal::getInstance();
Expand Down
47 changes: 20 additions & 27 deletions src/apps/tools/OswAppTimeConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,21 @@
void OswAppTimeConfig::setup() {}

void OswAppTimeConfig::enterManualMode() {
uint32_t second = 0;
uint32_t minute = 0;
uint32_t hour = 0;
uint32_t day = 0;
uint32_t month = 0;
uint32_t year = 0;
OswHal::getInstance()->getLocalTime(&hour, &minute, &second);
OswHal::getInstance()->getLocalDate(&day, &month, &year);
manualSettingTimestamp[0] = year % 10;
manualSettingTimestamp[1] = month / 10;
manualSettingTimestamp[2] = month % 10;
manualSettingTimestamp[3] = day / 10;
manualSettingTimestamp[4] = day % 10;
manualSettingTimestamp[5] = hour / 10;
manualSettingTimestamp[6] = hour % 10;
manualSettingTimestamp[7] = minute / 10;
manualSettingTimestamp[8] = minute % 10;
manualSettingTimestamp[9] = second / 10;
manualSettingTimestamp[10] = second % 10;
OswTime oswTime = { };
OswDate oswDate = { };
OswHal::getInstance()->getLocalTime(oswTime);
OswHal::getInstance()->getLocalDate(oswDate);
manualSettingTimestamp[0] = oswDate.year % 10;
manualSettingTimestamp[1] = oswDate.month / 10;
manualSettingTimestamp[2] = oswDate.month % 10;
manualSettingTimestamp[3] = oswDate.day / 10;
manualSettingTimestamp[4] = oswDate.day % 10;
manualSettingTimestamp[5] = oswTime.hour / 10;
manualSettingTimestamp[6] = oswTime.hour % 10;
manualSettingTimestamp[7] = oswTime.minute / 10;
manualSettingTimestamp[8] = oswTime.minute % 10;
manualSettingTimestamp[9] = oswTime.second / 10;
manualSettingTimestamp[10] = oswTime.second % 10;
manualSettingScreen = true;
}

Expand Down Expand Up @@ -196,16 +192,13 @@ void OswAppTimeConfig::loop() {
hal->gfx()->setTextMiddleAligned();
hal->gfx()->setTextLeftAligned();
hal->gfx()->setTextCursor(120 - hal->gfx()->getTextOfsetColumns(4), 120);

uint32_t second = 0;
uint32_t minute = 0;
uint32_t hour = 0;
hal->getLocalTime(&hour, &minute, &second);
hal->gfx()->printDecimal(hour, 2);
OswTime oswTime = { };
hal->getLocalTime(oswTime);
hal->gfx()->printDecimal(oswTime.hour, 2);
hal->gfx()->print(":");
hal->gfx()->printDecimal(minute, 2);
hal->gfx()->printDecimal(oswTime.minute, 2);
hal->gfx()->print(":");
hal->gfx()->printDecimal(second, 2);
hal->gfx()->printDecimal(oswTime.second, 2);

} else {
handleIncrementButton();
Expand Down
36 changes: 17 additions & 19 deletions src/apps/watchfaces/OswAppWatchface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ const char* OswAppWatchface::getAppName() {
void OswAppWatchface::drawStepHistory(OswUI* ui, uint8_t x, uint8_t y, uint8_t w, uint8_t h, uint32_t max) {
OswHal* hal = OswHal::getInstance();
OswUI::getInstance()->resetTextColors();
uint32_t weekDay = 0;
uint32_t dayOfMonth = 0;
hal->getLocalDate(&dayOfMonth, &weekDay);

OswDate oswDate = { };
hal->getLocalDate(oswDate);

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;
Expand All @@ -40,7 +41,7 @@ void OswAppWatchface::drawStepHistory(OswUI* ui, uint8_t x, uint8_t y, uint8_t w
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);
// bar frames
uint16_t f = weekDay == i ? ui->getForegroundColor() : ui->getForegroundDimmedColor();
uint16_t f = oswDate.weekDay == i ? ui->getForegroundColor() : ui->getForegroundDimmedColor();
hal->gfx()->drawRFrame(x + i * w, y, w, h, 2, f);

// labels
Expand All @@ -64,6 +65,7 @@ void OswAppWatchface::drawStepHistory(OswUI* ui, uint8_t x, uint8_t y, uint8_t w
void OswAppWatchface::drawWatch() {
OswHal* hal = OswHal::getInstance();


hal->gfx()->drawMinuteTicks(CENTER_Y, CENTER_Y, 116, 112, ui->getForegroundDimmedColor(), true);
hal->gfx()->drawHourTicks(CENTER_X, CENTER_Y, 117, 107, ui->getForegroundColor(), true);

Expand Down Expand Up @@ -91,33 +93,29 @@ void OswAppWatchface::drawWatch() {
// hal->gfx()->drawArc(120, 120, 0, bat, 180, 57, 7, dimColor(COLOR_BLUE, 25));
// hal->gfx()->drawArc(120, 120, 0, bat, 180, 57, 6, COLOR_BLUE);

uint32_t second = 0;
uint32_t minute = 0;
uint32_t hour = 0;
hal->getLocalTime(&hour, &minute, &second);
OswTime oswTime = { };
hal->getLocalTime(oswTime);

if(OswConfigAllKeys::settingDisplayDualHourTick.get()) {
uint32_t dualSecond = 0;
uint32_t dualMinute = 0;
uint32_t dualHour = 0;
hal->getDualTime(&dualHour, &dualMinute, &dualSecond);
OswTime oswDualTime = { };
hal->getDualTime(oswDualTime);

// dual-hours
hal->gfx()->drawThickTick(CENTER_X, CENTER_Y, 0, 16, (int)(360.0f / 12.0f * (dualHour + dualMinute / 60.0f)), 2, ui->getBackgroundDimmedColor(), true, STRAIGHT_END);
hal->gfx()->drawThickTick(CENTER_X, CENTER_Y, 16, 60, (int)(360.0f / 12.0f * (dualHour + dualMinute / 60.0f)), 5, ui->getBackgroundDimmedColor(), true);
hal->gfx()->drawThickTick(CENTER_X, CENTER_Y, 0, 16, (int)(360.0f / 12.0f * (oswDualTime.hour + oswDualTime.minute / 60.0f)), 2, ui->getBackgroundDimmedColor(), true, STRAIGHT_END);
hal->gfx()->drawThickTick(CENTER_X, CENTER_Y, 16, 60, (int)(360.0f / 12.0f * (oswDualTime.hour + oswDualTime.minute / 60.0f)), 5, ui->getBackgroundDimmedColor(), true);
}
// hours
hal->gfx()->drawThickTick(CENTER_X, CENTER_Y, 0, (int)16, (int)(360.0f / 12.0f * (hour + minute / 60.0f)), 1, ui->getForegroundColor(), true, STRAIGHT_END);
hal->gfx()->drawThickTick(CENTER_X, CENTER_Y, 16, 60, (int)(360.0f / 12.0f * (hour + minute / 60.0f)), 4, ui->getForegroundColor(), true);
hal->gfx()->drawThickTick(CENTER_X, CENTER_Y, 0, (int)16, (int)(360.0f / 12.0f * (oswTime.hour + oswTime.minute / 60.0f)), 1, ui->getForegroundColor(), true, STRAIGHT_END);
hal->gfx()->drawThickTick(CENTER_X, CENTER_Y, 16, 60, (int)(360.0f / 12.0f * (oswTime.hour + oswTime.minute / 60.0f)), 4, ui->getForegroundColor(), true);

// minutes
hal->gfx()->drawThickTick(CENTER_X, CENTER_Y, 0, 16, (int)(360.0f / 60.0f * (minute + second / 60.0f)), 1, ui->getForegroundColor(), true, STRAIGHT_END);
hal->gfx()->drawThickTick(CENTER_X, CENTER_Y, 16, 105, (int)(360.0f / 60.0f * (minute + second / 60.0f)), 4, ui->getForegroundColor(), true);
hal->gfx()->drawThickTick(CENTER_X, CENTER_Y, 0, 16, (int)(360.0f / 60.0f * (oswTime.minute + oswTime.second / 60.0f)), 1, ui->getForegroundColor(), true, STRAIGHT_END);
hal->gfx()->drawThickTick(CENTER_X, CENTER_Y, 16, 105, (int)(360.0f / 60.0f * (oswTime.minute + oswTime.second / 60.0f)), 4, ui->getForegroundColor(), true);

#ifndef GIF_BG
// seconds
hal->gfx()->fillCircleAA(CENTER_X, CENTER_Y, 3, ui->getDangerColor());
hal->gfx()->drawThickTick(CENTER_X, CENTER_Y, -16, 110, 180 + 360 / 60 * second, 1, ui->getDangerColor(), true);
hal->gfx()->drawThickTick(CENTER_X, CENTER_Y, -16, 110, 180 + 360 / 60 * oswTime.second, 1, ui->getDangerColor(), true);
#endif
}

Expand Down
14 changes: 6 additions & 8 deletions src/apps/watchfaces/OswAppWatchfaceBinary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,18 @@
#define COLOR_WHxITE rgb565(255, 255, 255)

void OswAppWatchfaceBinary::drawWatch() {
uint32_t second = 0;
uint32_t minute = 0;
uint32_t hour = 0;
bool afterNoon = false;
OswHal* hal = OswHal::getInstance();
hal->getLocalTime(&hour, &minute, &second, &afterNoon);

OswTime oswTime = { };
hal->getLocalTime(oswTime);

uint16_t width = hal->gfx()->getWidth();
uint16_t height = hal->gfx()->getHeight();

//hours
for(uint8_t i = 0; i < 5 ; i++ ) {
uint32_t b = pow(2, i);
if((hour & b) == 0) {
if((oswTime.hour & b) == 0) {
hal->gfx()->drawFrame(width - (((width - 32) / 8) * i + 64) - 32, height / 2 - 16, 8, 8, ui->getWarningColor());
} else {
hal->gfx()->fillFrame(width - (((width - 32) / 8) * i + 64) - 32, height / 2 - 16, 8, 8, ui->getWarningColor());
Expand All @@ -36,7 +34,7 @@ void OswAppWatchfaceBinary::drawWatch() {
//minutes
for(uint8_t i = 0; i < 6 ; i++ ) {
uint32_t b = pow(2, i);
if((minute & b) == 0) {
if((oswTime.minute & b) == 0) {
hal->gfx()->drawFrame(width - (((width - 32) / 8) * i + 64) - 32, height / 2, 8, 8, ui->getDangerColor());
} else {
hal->gfx()->fillFrame(width - (((width - 32) / 8) * i + 64) - 32, height / 2, 8, 8, ui->getDangerColor());
Expand All @@ -45,7 +43,7 @@ void OswAppWatchfaceBinary::drawWatch() {
//seconds
for(uint8_t i = 0; i < 6 ; i++ ) {
uint32_t b = pow(2, i);
if((second & b) == 0) {
if((oswTime.second & b) == 0) {
hal->gfx()->drawFrame(width - (((width - 32) / 8) * i + 64) - 32, height / 2 + 16, 8, 8, ui->getInfoColor());
} else {
hal->gfx()->fillFrame(width - (((width - 32) / 8) * i + 64) - 32, height / 2 + 16, 8, 8, ui->getInfoColor());
Expand Down
Loading
Loading