//
// Created by lydxh on 2024/5/9.
//
#include <stdio.h>
#include <ctime>
#include <stdint.h>
#include <cstdlib>
#include "tool.h"

float Mapping(float val, float I_Min, float I_Max, float O_Min, float O_Max) {
    return (((val - I_Min) * ((O_Max - O_Min) / (I_Max - I_Min))) + O_Min);
}

void BufPrint(char *name, void *buf, Type_t type, unsigned int len, unsigned char frame) {
    printf("\n%s:\n", name);
    for (int i = 0; i < len; ++i) {
        if (i % frame == 0 && i != 0) printf("\n");
        switch (type) {
            case T_U8:
                printf("%02X ", *((unsigned char *) buf + i));
                break;
            case T_U16:
                printf("%04X ", *((unsigned short *) buf + i));
                break;
            case T_U32:
                printf("%08X ", *((unsigned int *) buf + i));
                break;
            case T_CHAR:
                printf("%c ", *((char *) buf + i));
                break;
            case T_SHORT:
                printf("%d ", *((short *) buf + i));
                break;
            case T_INT:
                printf("%d ", *((int *) buf + i));
                break;
            case T_FLOAT:
                printf("%0.2f ", *((float *) buf + i));
                break;
            case T_DOUBLE:
                printf("%0.2f ", *((double *) buf + i));
                break;
            default:
                printf("未指定类型大小:%p", buf);
        }
    }
    printf("\n");
}

float Str2Float(char *str) {
    int integer = 0;
    double decimal = 0;
    int sign = 1;
    int decimalFlag = 0;
    int decimalWeight = 1;
    if (*str == '-') {
        sign = -1;
        str++;
    }
    while (*str != '\0') {
        if (*str == '.') {
            decimalFlag = 1;
            str++;
            continue;
        }
        if (!decimalFlag) {
            integer = integer * 10 + (*str - '0');
        } else {
            decimal = decimal * 10 + (*str - '0');
            decimalWeight *= 10;
        }
        str++;
    }
    decimal /= decimalWeight;
    return float(sign * (integer + decimal));
}
