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
51 changes: 26 additions & 25 deletions include/osw_hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@
#define ERR_SD_MISSING 1
#define ERR_SD_MOUNT_FAILED 2

typedef struct {
uint32_t hour;
uint32_t minute;
uint32_t second;
bool afterNoon;
} OSW_TIME;
RuffaloLavoisier marked this conversation as resolved.
Show resolved Hide resolved

typedef struct {
uint32_t year;
uint32_t month;
uint32_t day;
uint32_t weekDay;
const char* weekDayName;
} OSW_DATE;
RuffaloLavoisier marked this conversation as resolved.
Show resolved Hide resolved

class OswHal {
public:
static OswHal* getInstance();
Expand Down Expand Up @@ -181,51 +196,37 @@ 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(OSW_TIME* oswTime);
RuffaloLavoisier marked this conversation as resolved.
Show resolved Hide resolved

// 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, OSW_TIME* oswTime);
void getDate(time_t& offset, OSW_DATE* oswDate, uint32_t* setWDay = nullptr);

// 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(OSW_TIME* 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(OSW_DATE* oswDate, uint32_t* sWDay = nullptr) {
this->getDate(this->timezoneOffsetPrimary, oswDate, sWDay);
};

// 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(OSW_TIME* 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(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);
inline void getDualDate(OSW_DATE* oswDate, uint32_t* sWDay = nullptr) {
this->getDate(this->timezoneOffsetPrimary, oswDate, sWDay);
};

bool _requestDisableBuffer = false;
Expand Down
16 changes: 8 additions & 8 deletions src/apps/tools/OswAppDistStats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ 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);
OSW_DATE oswDate = { 0, };
RuffaloLavoisier marked this conversation as resolved.
Show resolved Hide resolved
hal->getLocalDate(&oswDate);
uint32_t weekDay = oswDate.day;
uint32_t dayOfMonth = oswDate.weekDay;
RuffaloLavoisier marked this conversation as resolved.
Show resolved Hide resolved

for (uint8_t index = 0; index < 7; index++) {
uint32_t weekDayDist = OswAppWatchfaceFitness::calculateDistance(hal->environment()->getStepsOnDay(index));
Expand Down Expand Up @@ -46,15 +47,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;
OSW_DATE oswDate = { 0, };
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);
OSW_DATE oswDate = { 0, };
hal->getLocalDate(&oswDate);
int cursorWeekDay = oswDate.weekDay - (6 - Index);
int findWeekDay = (cursorWeekDay >= 0) ? cursorWeekDay : (cursorWeekDay + 7);
uint32_t fWD = findWeekDay;
return fWD;
Expand Down
19 changes: 11 additions & 8 deletions src/apps/tools/OswAppStepStats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ 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);
OSW_DATE oswDate = { 0, };
hal->getLocalDate(&oswDate);
uint32_t weekDay = oswDate.weekDay;
uint32_t dayOfMonth = oswDate.day;

for (uint8_t index = 0; index < 7; index++) {
unsigned int weekDayStep = hal->environment()->getStepsOnDay(index);
Expand All @@ -43,7 +44,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));
OSW_DATE oswDate = { 0, };
hal->getLocalDate(&oswDate, &pos);
const char* weekday = oswDate.weekDayName;
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 +73,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;
OSW_DATE oswDate = { 0, };
hal->getLocalDate(&oswDate);
cursorPos = oswDate.weekDay;
}
void OswAppStepStats::loop() {
OswHal* hal = OswHal::getInstance();
Expand Down
31 changes: 15 additions & 16 deletions src/apps/tools/OswAppTimeConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@
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);
OSW_TIME oswTime = { 0, };
OSW_DATE oswDate = { 0, };
OswHal::getInstance()->getLocalTime(&oswTime);
OswHal::getInstance()->getLocalDate(&oswDate);
uint32_t second = oswTime.second;
uint32_t minute = oswTime.minute;
uint32_t hour = oswTime.hour;
uint32_t day = oswDate.day;
uint32_t month = oswDate.month;
uint32_t year = oswDate.year;
manualSettingTimestamp[0] = year % 10;
manualSettingTimestamp[1] = month / 10;
manualSettingTimestamp[2] = month % 10;
Expand Down Expand Up @@ -196,16 +198,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);
OSW_TIME oswTime = { 0, };
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
27 changes: 16 additions & 11 deletions src/apps/watchfaces/OswAppWatchface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ 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);

OSW_DATE oswDate = { 0, };
hal->getLocalDate(&oswDate);
uint32_t weekDay = oswDate.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;
Expand Down Expand Up @@ -64,6 +66,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,16 +94,18 @@ 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);
OSW_TIME oswTime = { 0, };
hal->getLocalTime(&oswTime);
uint32_t second = oswTime.second;
uint32_t minute = oswTime.minute;
uint32_t hour = oswTime.hour;

if(OswConfigAllKeys::settingDisplayDualHourTick.get()) {
uint32_t dualSecond = 0;
uint32_t dualMinute = 0;
uint32_t dualHour = 0;
hal->getDualTime(&dualHour, &dualMinute, &dualSecond);
OSW_TIME oswTime = { 0, };
hal->getDualTime(&oswTime);
uint32_t dualSecond = oswTime.second;
uint32_t dualMinute = oswTime.minute;
uint32_t dualHour = oswTime.hour;
RuffaloLavoisier marked this conversation as resolved.
Show resolved Hide resolved

// 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);
Expand Down
12 changes: 7 additions & 5 deletions src/apps/watchfaces/OswAppWatchfaceBinary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
#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);

OSW_TIME oswTime = { 0, };
hal->getLocalTime(&oswTime);
uint32_t second = oswTime.second;
uint32_t minute = oswTime.minute;
uint32_t hour = oswTime.hour;
bool afterNoon = oswTime.afterNoon;

uint16_t width = hal->gfx()->getWidth();
uint16_t height = hal->gfx()->getHeight();
Expand Down
20 changes: 11 additions & 9 deletions src/apps/watchfaces/OswAppWatchfaceDigital.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,16 @@ static void drawDate(time_t timeZone, uint8_t fontSize, uint8_t CoordY) {
uint32_t dayInt = 0;
uint32_t monthInt = 0;
uint32_t yearInt = 0;
const char* weekday = nullptr;
OSW_DATE oswDate = { 0, };
OswHal* hal = OswHal::getInstance();
const char* weekday = hal->getWeekday(timeZone);

hal->getDate(timeZone,&dayInt, &monthInt, &yearInt);
hal->getDate(timeZone, &oswDate);

dayInt = oswDate.day;
monthInt = oswDate.month;
yearInt = oswDate.year;
weekday = oswDate.weekDayName;
// we want to output a value like "Wed, 05/02/2021"

hal->gfx()->setTextSize(fontSize);
Expand Down Expand Up @@ -101,10 +106,7 @@ void OswAppWatchfaceDigital::timeOutput(uint32_t hour, uint32_t minute, uint32_t
}

static void drawTime(time_t timeZone,uint8_t CoordY) {
uint32_t second = 0;
uint32_t minute = 0;
uint32_t hour = 0;
bool afterNoon = false;
OSW_TIME oswTime = { 0, };
char am[] = "AM";
char pm[] = "PM";
OswHal* hal = OswHal::getInstance();
Expand All @@ -114,11 +116,11 @@ static void drawTime(time_t timeZone,uint8_t CoordY) {
hal->gfx()->setTextLeftAligned();
hal->gfx()->setTextCursor(120 - hal->gfx()->getTextOfsetColumns(OswConfigAllKeys::timeFormat.get() ? 4 : 5.5),CoordY );

hal->getTime(timeZone,&hour, &minute, &second, &afterNoon);
OswAppWatchfaceDigital::timeOutput(hour, minute, second);
hal->getTime(timeZone, &oswTime);
OswAppWatchfaceDigital::timeOutput(oswTime.hour, oswTime.minute, oswTime.second);
if (!OswConfigAllKeys::timeFormat.get()) {
hal->gfx()->print(" ");
if (afterNoon) {
if (oswTime.afterNoon) {
hal->gfx()->print(pm);
} else {
hal->gfx()->print(am);
Expand Down
6 changes: 3 additions & 3 deletions src/apps/watchfaces/OswAppWatchfaceDual.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +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;
OSW_TIME oswTime = { 0, };
hal->getLocalTime(&oswTime);
uint32_t onlySecond = oswTime.second;
uint16_t barValue = ((float)onlySecond / 60) * barWidth;
barValue = barValue < 2 ? 0 : barValue;
uint8_t coordX = (DISP_W - barWidth) / 2;
Expand Down
25 changes: 13 additions & 12 deletions src/apps/watchfaces/OswAppWatchfaceFitness.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ uint32_t OswAppWatchfaceFitness::calculateKcalorie(uint32_t steps) {
}

void dateDisplay() {
uint32_t dayInt = 0;
uint32_t monthInt = 0;
uint32_t yearInt = 0;
OswHal* hal = OswHal::getInstance();
const char* weekday = hal->getLocalWeekday();

hal->getLocalDate(&dayInt, &monthInt, &yearInt);

OSW_DATE oswDate = { 0, };
hal->getLocalDate(&oswDate);
uint32_t dayInt = oswDate.day;
uint32_t monthInt = oswDate.month;
uint32_t yearInt = oswDate.year;
const char* weekday = oswDate.weekDayName;

hal->gfx()->setTextSize(2);
hal->gfx()->setTextMiddleAligned();
hal->gfx()->setTextRightAligned();
Expand Down Expand Up @@ -62,10 +63,6 @@ void timeDisplay(uint32_t hour, uint32_t minute, uint32_t second) {
}

void digitalWatchDisplay() {
uint32_t second = 0;
uint32_t minute = 0;
uint32_t hour = 0;
bool afterNoon = false;
char am[] = "AM";
char pm[] = "PM";
OswHal* hal = OswHal::getInstance();
Expand All @@ -74,8 +71,12 @@ void digitalWatchDisplay() {
hal->gfx()->setTextMiddleAligned();
hal->gfx()->setTextLeftAligned();
hal->gfx()->setTextCursor(DISP_W / 2 - 30, DISP_W / 2);

hal->getLocalTime(&hour, &minute, &second, &afterNoon);
OSW_TIME oswTime = { 0, };
hal->getLocalTime(&oswTime);
uint32_t second = oswTime.second;
uint32_t minute = oswTime.minute;
uint32_t hour = oswTime.hour;
bool afterNoon = oswTime.afterNoon;

timeDisplay(hour, minute, second);
if (!OswConfigAllKeys::timeFormat.get()) {
Expand Down
Loading
Loading