rtc refactoring

This commit is contained in:
cawtds
2024-08-22 15:29:05 +02:00
parent c81fcffb4a
commit a5990e88b1
3 changed files with 79 additions and 99 deletions

View File

@@ -45,17 +45,17 @@
#define NIGHT_HOUR_END 4
//Gen 5 seasons change the times of day
#elif OW_TIMES_OF_DAY == GEN_5
#define MORNING_HOUR_BEGIN GetGen5MorningBegin()
#define MORNING_HOUR_END GetGen5DayBegin()
#define MORNING_HOUR_BEGIN GetGen5TimeOfDayStart(TIME_MORNING)
#define MORNING_HOUR_END GetGen5TimeOfDayStart(TIME_DAY)
#define DAY_HOUR_BEGIN GetGen5DayBegin()
#define DAY_HOUR_END GetGen5EveningBegin()
#define DAY_HOUR_BEGIN GetGen5TimeOfDayStart(TIME_DAY)
#define DAY_HOUR_END GetGen5TimeOfDayStart(TIME_EVENING)
#define EVENING_HOUR_BEGIN GetGen5EveningBegin()
#define EVENING_HOUR_END GetGen5NightBegin()
#define EVENING_HOUR_BEGIN GetGen5TimeOfDayStart(TIME_EVENING)
#define EVENING_HOUR_END GetGen5TimeOfDayStart(TIME_NIGHT)
#define NIGHT_HOUR_BEGIN GetGen5NightBegin()
#define NIGHT_HOUR_END GetGen5MorningBegin()
#define NIGHT_HOUR_BEGIN GetGen5TimeOfDayStart(TIME_NIGHT)
#define NIGHT_HOUR_END GetGen5TimeOfDayStart(TIME_MORNING)
#elif OW_TIMES_OF_DAY == GEN_6
#define MORNING_HOUR_BEGIN 4
#define MORNING_HOUR_END 11
@@ -95,15 +95,19 @@
#define NIGHT_HOUR_END 6
#endif
#define TIME_MORNING 0
#define TIME_DAY 1
#define TIME_EVENING 2
#define TIME_NIGHT 3
enum TimeOfDay {
TIME_MORNING,
TIME_DAY,
TIME_EVENING,
TIME_NIGHT,
};
#define SEASON_SPRING 0
#define SEASON_SUMMER 1
#define SEASON_AUTUMN 2
#define SEASON_WINTER 3
enum Season {
SEASON_SPRING,
SEASON_SUMMER,
SEASON_AUTUMN,
SEASON_WINTER,
};
extern struct Time gLocalTime;
@@ -129,7 +133,7 @@ void FormatHexDate(u8 *dest, s32 year, s32 month, s32 day);
void RtcCalcTimeDifference(struct SiiRtcInfo *rtc, struct Time *result, struct Time *t);
void RtcCalcLocalTime(void);
bool8 IsBetweenHours(s32 hours, s32 begin, s32 end);
u8 GetTimeOfDay(void);
enum TimeOfDay GetTimeOfDay(void);
void RtcInitLocalTimeOffset(s32 hour, s32 minute);
void RtcCalcLocalTimeOffset(s32 days, s32 hours, s32 minutes, s32 seconds);
void CalcTimeDifference(struct Time *result, struct Time *t1, struct Time *t2);
@@ -138,71 +142,9 @@ u32 RtcGetLocalDayCount(void);
void FormatDecimalTimeWithoutSeconds(u8 *dest, s8 hour, s8 minute, bool32 is24Hour);
u8 GetCurrentHour(void);
u8 GetCurrentMinute(void);
u8 GetSeason(void);
enum Season GetSeason(void);
const u8* GetSeasonName(enum Season);
u8 GetSeasonDay(void);
static inline s32 GetGen5MorningBegin()
{
switch (GetSeason())
{
case SEASON_SPRING:
default:
return 5;
case SEASON_SUMMER:
return 4;
case SEASON_AUTUMN:
return 6;
case SEASON_WINTER:
return 7;
}
}
static inline s32 GetGen5DayBegin()
{
switch (GetSeason())
{
case SEASON_SPRING:
default:
return 10;
case SEASON_SUMMER:
return 9;
case SEASON_AUTUMN:
return 10;
case SEASON_WINTER:
return 11;
}
}
static inline s32 GetGen5EveningBegin()
{
switch (GetSeason())
{
case SEASON_SPRING:
default:
return 17;
case SEASON_SUMMER:
return 19;
case SEASON_AUTUMN:
return 18;
case SEASON_WINTER:
return 17;
}
}
static inline s32 GetGen5NightBegin()
{
switch (GetSeason())
{
case SEASON_SPRING:
default:
return 20;
case SEASON_SUMMER:
return 21;
case SEASON_AUTUMN:
return 20;
case SEASON_WINTER:
return 19;
}
}
u32 GetGen5TimeOfDayStart(enum TimeOfDay);
#endif // GUARD_RTC_UTIL_H

View File

@@ -15,6 +15,10 @@ static u16 sSavedIme;
struct Time gLocalTime;
// const rom
static const u8 sText_SpringName[] = _("Spring");
static const u8 sText_SummerName[] = _("Summer");
static const u8 sText_AutumnName[] = _("Autumn");
static const u8 sText_WinterName[] = _("Winter");
static const struct SiiRtcInfo sRtcDummy = {0, MONTH_JAN, 1}; // 2000 Jan 1
@@ -34,6 +38,33 @@ static const s32 sNumDaysInMonths[12] =
31,
};
static const u32 sTimeOfDayStarts[SEASON_WINTER + 1][TIME_NIGHT + 1] = {
[SEASON_SPRING] = {
[TIME_MORNING] = 5,
[TIME_DAY] = 10,
[TIME_EVENING] = 17,
[TIME_NIGHT] = 20,
},
[SEASON_SUMMER] = {
[TIME_MORNING] = 4,
[TIME_DAY] = 9,
[TIME_EVENING] = 19,
[TIME_NIGHT] = 21,
},
[SEASON_AUTUMN] = {
[TIME_MORNING] = 6,
[TIME_DAY] = 10,
[TIME_EVENING] = 18,
[TIME_NIGHT] = 20,
},
[SEASON_WINTER] = {
[TIME_MORNING] = 7,
[TIME_DAY] = 11,
[TIME_EVENING] = 17,
[TIME_NIGHT] = 19,
},
};
void RtcDisableInterrupts(void)
{
sSavedIme = REG_IME;
@@ -319,7 +350,7 @@ bool8 IsBetweenHours(s32 hours, s32 begin, s32 end)
return hours >= begin && hours < end;
}
u8 GetTimeOfDay(void)
enum TimeOfDay GetTimeOfDay(void)
{
RtcCalcLocalTime();
if (IsBetweenHours(gLocalTime.hours, MORNING_HOUR_BEGIN, MORNING_HOUR_END))
@@ -343,7 +374,7 @@ u8 GetCurrentMinute(void)
return gLocalTime.minutes;
}
u8 GetSeason(void)
enum Season GetSeason(void)
{
RtcGetInfo(&sRtc);
switch(ConvertBcdToBinary(sRtc.month))
@@ -368,6 +399,22 @@ u8 GetSeason(void)
}
}
const u8* GetSeasonName(enum Season season)
{
switch (season)
{
case SEASON_SPRING:
default:
return sText_SpringName;
case SEASON_SUMMER:
return sText_SummerName;
case SEASON_AUTUMN:
return sText_AutumnName;
case SEASON_WINTER:
return sText_WinterName;
}
}
u8 GetSeasonDay(void)
{
RtcGetInfo(&sRtc);
@@ -462,3 +509,8 @@ void FormatDecimalTimeWithoutSeconds(u8 *txtPtr, s8 hour, s8 minute, bool32 is24
*txtPtr++ = EOS;
*txtPtr = EOS;
}
u32 GetGen5TimeOfDayStart(enum TimeOfDay timeOfDay)
{
return sTimeOfDayStarts[GetSeason()][timeOfDay];
}

View File

@@ -331,21 +331,7 @@ static void DrawTimeWindow(void)
StringExpandPlaceholders(gStringVar4, gText_MenuTime);
AddTextPrinterParameterized(sTimeWindowId, FONT_NORMAL, gStringVar4, 4, 1, 0xFF, NULL);
switch (GetSeason())
{
case SEASON_SPRING:
StringCopy(gStringVar1, gText_SeasonSpring);
break;
case SEASON_SUMMER:
StringCopy(gStringVar1, gText_SeasonSummer);
break;
case SEASON_AUTUMN:
StringCopy(gStringVar1, gText_SeasonAutumn);
break;
case SEASON_WINTER:
StringCopy(gStringVar1, gText_SeasonWinter);
break;
}
StringCopy(gStringVar1, GetSeasonName(GetSeason()));
ConvertIntToDecimalStringN(gStringVar2, GetSeasonDay(), STR_CONV_MODE_RIGHT_ALIGN, 2);
StringExpandPlaceholders(gStringVar4, gText_MenuDay);
AddTextPrinterParameterized(sTimeWindowId, FONT_NORMAL, gStringVar4, 4, 18, 0xFF, NULL);