Added a math library, with clamp and POW. added printf support for printing doubles added a few helper functions in PIT for calcaulting the time in seconds of a clock cycle based on divisor
310 lines
5.3 KiB
C
310 lines
5.3 KiB
C
#include <stdbool.h>
|
|
#include <stdint.h>
|
|
#include <string.h>
|
|
#include <math.h>
|
|
|
|
static void reverse(char* buf, int len)
|
|
{
|
|
int start = 0;
|
|
int end = len - 1;
|
|
while (start < end) {
|
|
char tmp = buf[start];
|
|
buf[start] = buf[end];
|
|
buf[end] = tmp;
|
|
end--;
|
|
start++;
|
|
}
|
|
}
|
|
|
|
char* s64toa(int64_t num, char* buf, int base)
|
|
{
|
|
int i = 0;
|
|
bool neg = false;
|
|
|
|
if (num == 0) { // Handle zero explicitly
|
|
buf[i++] = '0';
|
|
buf[i] = '\0';
|
|
return buf;
|
|
}
|
|
|
|
if (num < 0 && base == 10) { // only handle negative on base10
|
|
neg = true;
|
|
num = -num;
|
|
}
|
|
|
|
while (num != 0) {
|
|
int rem = num % base;
|
|
buf[i++] = (rem > 9) ? (rem - 10) + 'a' : rem + '0';
|
|
num = num / base;
|
|
}
|
|
|
|
if (neg) // lets reapply the negative sign
|
|
buf[i++] = '-';
|
|
|
|
buf[i] = '\0';
|
|
|
|
reverse(buf, i); // reverse, since we did it backwards!
|
|
return buf;
|
|
}
|
|
|
|
char* u64toa(uint64_t num, char* buf, int base)
|
|
{
|
|
int i = 0;
|
|
if (num == 0) { // Handle zero explicitly
|
|
buf[i++] = '0';
|
|
buf[i] = '\0';
|
|
return buf;
|
|
}
|
|
|
|
while (num != 0) {
|
|
int rem = num % base;
|
|
buf[i++] = (rem > 9) ? (rem - 10) + 'a' : rem + '0';
|
|
num = num / base;
|
|
}
|
|
|
|
buf[i] = '\0';
|
|
|
|
reverse(buf, i); // reverse, since we did it backwards!
|
|
return buf;
|
|
}
|
|
|
|
char* s32toa(int32_t num, char* buf, int base)
|
|
{
|
|
int i = 0;
|
|
bool neg = false;
|
|
|
|
if (num == 0) { // Handle zero explicitly
|
|
buf[i++] = '0';
|
|
buf[i] = '\0';
|
|
return buf;
|
|
}
|
|
|
|
if (num < 0 && base == 10) { // only handle negative on base10
|
|
neg = true;
|
|
num = -num;
|
|
}
|
|
|
|
while (num != 0) {
|
|
int rem = num % base;
|
|
buf[i++] = (rem > 9) ? (rem - 10) + 'a' : rem + '0';
|
|
num = num / base;
|
|
}
|
|
|
|
if (neg) // lets reapply the negative sign
|
|
buf[i++] = '-';
|
|
|
|
buf[i] = '\0';
|
|
|
|
reverse(buf, i); // reverse, since we did it backwards!
|
|
return buf;
|
|
}
|
|
|
|
char* u32toa(uint32_t num, char* buf, int base)
|
|
{
|
|
int i = 0;
|
|
if (num == 0) { // Handle zero explicitly
|
|
buf[i++] = '0';
|
|
buf[i] = '\0';
|
|
return buf;
|
|
}
|
|
|
|
while (num != 0) {
|
|
int rem = num % base;
|
|
buf[i++] = (rem > 9) ? (rem - 10) + 'a' : rem + '0';
|
|
num = num / base;
|
|
}
|
|
|
|
buf[i] = '\0';
|
|
|
|
reverse(buf, i); // reverse, since we did it backwards!
|
|
return buf;
|
|
}
|
|
|
|
char* s16toa(int16_t num, char* buf, int base)
|
|
{
|
|
int i = 0;
|
|
bool neg = false;
|
|
|
|
if (num == 0) { // Handle zero explicitly
|
|
buf[i++] = '0';
|
|
buf[i] = '\0';
|
|
return buf;
|
|
}
|
|
|
|
if (num < 0 && base == 10) { // only handle negative on base10
|
|
neg = true;
|
|
num = -num;
|
|
}
|
|
|
|
while (num != 0) {
|
|
int rem = num % base;
|
|
buf[i++] = (rem > 9) ? (rem - 10) + 'a' : rem + '0';
|
|
num = num / base;
|
|
}
|
|
|
|
if (neg) // lets reapply the negative sign
|
|
buf[i++] = '-';
|
|
|
|
buf[i] = '\0';
|
|
|
|
reverse(buf, i); // reverse, since we did it backwards!
|
|
return buf;
|
|
}
|
|
|
|
char* u16toa(uint16_t num, char* buf, int base)
|
|
{
|
|
int i = 0;
|
|
if (num == 0) { // Handle zero explicitly
|
|
buf[i++] = '0';
|
|
buf[i] = '\0';
|
|
return buf;
|
|
}
|
|
|
|
while (num != 0) {
|
|
int rem = num % base;
|
|
buf[i++] = (rem > 9) ? (rem - 10) + 'a' : rem + '0';
|
|
num = num / base;
|
|
}
|
|
|
|
buf[i] = '\0';
|
|
|
|
reverse(buf, i); // reverse, since we did it backwards!
|
|
return buf;
|
|
}
|
|
|
|
char* s8toa(int8_t num, char* buf, int base)
|
|
{
|
|
int i = 0;
|
|
bool neg = false;
|
|
|
|
if (num == 0) { // Handle zero explicitly
|
|
buf[i++] = '0';
|
|
buf[i] = '\0';
|
|
return buf;
|
|
}
|
|
|
|
if (num < 0 && base == 10) { // only handle negative on base10
|
|
neg = true;
|
|
num = -num;
|
|
}
|
|
|
|
while (num != 0) {
|
|
int rem = num % base;
|
|
buf[i++] = (rem > 9) ? (rem - 10) + 'a' : rem + '0';
|
|
num = num / base;
|
|
}
|
|
|
|
if (neg) // lets reapply the negative sign
|
|
buf[i++] = '-';
|
|
|
|
buf[i] = '\0';
|
|
|
|
reverse(buf, i); // reverse, since we did it backwards!
|
|
return buf;
|
|
}
|
|
|
|
char* u8toa(uint8_t num, char* buf, int base)
|
|
{
|
|
int i = 0;
|
|
if (num == 0) { // Handle zero explicitly
|
|
buf[i++] = '0';
|
|
buf[i] = '\0';
|
|
return buf;
|
|
}
|
|
|
|
while (num != 0) {
|
|
int rem = num % base;
|
|
buf[i++] = (rem > 9) ? (rem - 10) + 'a' : rem + '0';
|
|
num = num / base;
|
|
}
|
|
|
|
buf[i] = '\0';
|
|
|
|
reverse(buf, i); // reverse, since we did it backwards!
|
|
return buf;
|
|
}
|
|
|
|
char* itoa(int num, char* buf, int base)
|
|
{
|
|
int i = 0;
|
|
bool neg = false;
|
|
|
|
if (num == 0) { // Handle zero explicitly
|
|
buf[i++] = '0';
|
|
buf[i] = '\0';
|
|
return buf;
|
|
}
|
|
|
|
if (num < 0 && base == 10) { // only handle negative on base10
|
|
neg = true;
|
|
num = -num;
|
|
}
|
|
|
|
while (num != 0) {
|
|
int rem = num % base;
|
|
buf[i++] = (rem > 9) ? (rem - 10) + 'a' : rem + '0';
|
|
num = num / base;
|
|
}
|
|
|
|
if (neg) // lets reapply the negative sign
|
|
buf[i++] = '-';
|
|
|
|
buf[i] = '\0';
|
|
|
|
reverse(buf, i); // reverse, since we did it backwards!
|
|
return buf;
|
|
}
|
|
|
|
char* utoa(unsigned int num, char* buf, int base)
|
|
{
|
|
int i = 0;
|
|
if (num == 0) { // Handle zero explicitly
|
|
buf[i++] = '0';
|
|
buf[i] = '\0';
|
|
return buf;
|
|
}
|
|
|
|
while (num != 0) {
|
|
int rem = num % base;
|
|
buf[i++] = (rem > 9) ? (rem - 10) + 'a' : rem + '0';
|
|
num = num / base;
|
|
}
|
|
|
|
buf[i] = '\0';
|
|
|
|
reverse(buf, i); // reverse, since we did it backwards!
|
|
return buf;
|
|
}
|
|
|
|
static int dbl_to_str(int x, char* buf, int d)
|
|
{
|
|
int i = 0;
|
|
while (x) {
|
|
buf[i++] = (x % 10) + '0';
|
|
x = x / 10;
|
|
}
|
|
|
|
while (i < d)
|
|
buf[i++] = '0';
|
|
|
|
reverse(buf, i);
|
|
buf[i] = '\0';
|
|
return i;
|
|
}
|
|
|
|
char* dtoa(double n, char* buf, int afterpoint)
|
|
{
|
|
int whole = (int) n;
|
|
|
|
double decimals = n - (double) whole;
|
|
|
|
int i = dbl_to_str(whole, buf, 0);
|
|
if (afterpoint != 0) {
|
|
buf[i] = '.'; // add the decimal
|
|
|
|
decimals = decimals * pow(10, afterpoint);
|
|
dbl_to_str((int) decimals, buf + i + 1, afterpoint + 1);
|
|
}
|
|
return buf;
|
|
}
|