Skip to content

Commit

Permalink
Update functions.c
Browse files Browse the repository at this point in the history
Added functions:
Min
Max
Clamp
RotateMinMax

Functions used to reduce overall code space, also help to clarify logic intent
  • Loading branch information
KennethSimpson authored Nov 17, 2023
1 parent a1eb13a commit 59a7f18
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions Src/functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,34 @@ int getAbsDif(int number1, int number2)
return result;
}


int32_t Min(const int32_t var, const int32_t var1) {
return var > var1 ? var1 : var;
}

int32_t Max(const int32_t var, const int32_t var1) {
return var < var1 ? var1 : var;
}

int32_t Clamp(const int32_t var, const int32_t min, const int32_t max) {
if (var < min) {
return min;
} else if (var > max) {
return max;
}
return var;
}

int32_t RotateMinMax(const int32_t startValue, const int32_t amount, const int32_t min, const int32_t max) {
if (startValue + amount < min) {
return (max + 1) - (min - (startValue + amount));
}
else if (startValue + amount > max) {
return (min - 1) + ((startValue + amount) - max);
}
return startValue + amount;
}

#ifdef STMICRO
void delayMicros(uint32_t micros)
{
Expand Down

0 comments on commit 59a7f18

Please sign in to comment.