#include "lib/utils.h" #include "lib/mystd.h" #include "lib/uart.h" // this just burns cycles. // the volatile is important, it means that the cpu can't optimize any // read/writes for the value static inline void spin_loop(volatile uint16_t counts) { while (counts) counts--; } void delay_ms(uint16_t ms) { while (ms) { // this value was taken using a delay of 500ms, and just recording the led // blinking. it seems to be high enough such that each loop of delay_loop // takes 1ms :D spin_loop((F_CPU) / 5000); ms--; } } void ft_bzero(void* data, uint16_t size) { char* d = data; while (size) { *d = 0; d++; size--; } } uint8_t ft_stridx(const char* str, char chr) { if (!str) return -1; for (uint8_t i = 0; str[i]; i++) { if (str[i] == chr) return i; } return -1; } static float ft_pow(float base, float count) { float out = 1; while (count--) out *= base; return out; } // Reverses a string 'str' of length 'len' static void reverse(char* str, uint16_t len) { uint16_t i = 0, j = len - 1, temp; while (i < j) { temp = str[i]; str[i] = str[j]; str[j] = temp; i++; j--; } } static uint16_t ft_itoa(uint32_t value, char* out, uint16_t width) { uint16_t i = 0; if (value == 0) { while (i < width) out[i++] = '0'; return i; } while (value) { out[i++] = (value % 10) + '0'; value = value / 10; } // If number of digits required is more, then // add 0s at the beginning while (i < width) out[i++] = '0'; // we need to reverse the data reverse(out, i); out[i] = 0; return i; } // 0.3250351 // Converts a floating-point/double number to a string. uint8_t ft_ftoa(float val, char* out, uint16_t precision) { uint32_t ipart = (uint32_t)val; float fpart = val - (float)ipart; // convert integer part to string uint8_t i = ft_itoa(ipart, out, 1); if (precision != 0) { out[i] = '.'; fpart = fpart * ft_pow(10.f, precision); i += ft_itoa((uint32_t)fpart, &out[i + 1], precision); } return i; } void* ft_memcpy(void* dest, void* src, uint16_t len) { for (uint16_t i = 0; i < len; i++) ((uint8_t*)dest)[i] = ((uint8_t*)src)[i]; return dest; } bool ft_memcmp(void* s1, void* s2, uint16_t len) { for (uint16_t i = 0; i < len; i++) { if (((uint8_t*)s1)[i] != ((uint8_t*)s2)[i]) return false; } return true; } uint8_t is_hex_digit(char chr) { uint8_t i; if ((i = ft_stridx("0123456789", chr)) != 255) return i; if ((i = ft_stridx("abcdef", chr)) != 255) return 10 + i; if ((i = ft_stridx("ABCDEF", chr)) != 255) return 10 + i; return 255; }