#ifndef REV_LOG_TO_LIN_H
#define REV_LOG_TO_LIN_H

#include <stdint.h>

typedef struct {
    uint16_t in;
    uint16_t out;
} MapPoint;

uint16_t map_log_to_linear(uint16_t input) {
    static const MapPoint table[] = {
        {   0,   0},
        {  90,  85},
        { 317, 171},
        { 430, 256},
        { 553, 341},
        { 760, 426},
        { 844, 512},
        { 871, 597},
        { 900, 682},
        { 927, 767},
        { 963, 853},
        {1010, 938},
        {1023, 1023},
    };
    const int size = sizeof(table) / sizeof(table[0]);

    if (input <= table[0].in) return table[0].out;
    if (input >= table[size - 1].in) return table[size - 1].out;

    for (int i = 0; i < size - 1; i++) {
        if (input >= table[i].in && input <= table[i + 1].in) {
            uint16_t in0 = table[i].in;
            uint16_t out0 = table[i].out;
            uint16_t in1 = table[i + 1].in;
            uint16_t out1 = table[i + 1].out;

            // Linear interpolation
            return out0 + (uint32_t)(input - in0) * (out1 - out0) / (in1 - in0);
        }
    }

    return 0; // Should not reach here
}

#endif // REV_LOG_TO_LIN_H