Skip to content

Commit

Permalink
Refactor chronometer functions
Browse files Browse the repository at this point in the history
  • Loading branch information
AuroraWright committed Jun 4, 2017
1 parent cab54d1 commit 4429cb2
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions source/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,19 @@

static void startChrono(void)
{
bool isChronoStarted = false;

if(isChronoStarted) return;

REG_TIMER_CNT(0) = 0; //67MHz
for(u32 i = 1; i < 4; i++) REG_TIMER_CNT(i) = 4; //Count-up

for(u32 i = 0; i < 4; i++) REG_TIMER_VAL(i) = 0;

REG_TIMER_CNT(0) = 0x80; //67MHz; enabled
for(u32 i = 1; i < 4; i++) REG_TIMER_CNT(i) = 0x84; //Count-up; enabled

isChronoStarted = true;
}

static u64 chrono(void)
Expand All @@ -58,13 +64,15 @@ static u64 chrono(void)
u32 waitInput(bool isMenu)
{
static u64 dPadDelay = 0ULL;
u64 initialValue = 0ULL;
u32 key,
oldKey = HID_PAD;

if(isMenu)
{
dPadDelay = dPadDelay > 0ULL ? 87ULL : 143ULL;
startChrono();
initialValue = chrono();
}

while(true)
Expand All @@ -79,7 +87,7 @@ u32 waitInput(bool isMenu)
continue;
}

if(key == oldKey && (!isMenu || (!(key & DPAD_BUTTONS) || chrono() < dPadDelay))) continue;
if(key == oldKey && (!isMenu || (!(key & DPAD_BUTTONS) || chrono() - initialValue < dPadDelay))) continue;

//Make sure the key is pressed
u32 i;
Expand All @@ -104,7 +112,10 @@ void mcuPowerOff(void)
void wait(u64 amount)
{
startChrono();
while(chrono() < amount);

u64 initialValue = chrono();

while(chrono() - initialValue < amount);
}

void error(const char *fmt, ...)
Expand Down

0 comments on commit 4429cb2

Please sign in to comment.