const PULLUP_RESISTOR_KOHM = 10 const RT_TABLE = [ [-40, 345.275], [-39, 322.791], [-38, 301.925], [-37, 282.549], [-36, 264.549], [-35, 247.816], [-34, 232.254], [-33, 217.774], [-32, 204.292], [-31, 191.735], [-30, 180.032], [-29, 169.12], [-28, 158.941], [-27, 149.441], [-26, 140.571], [-25, 132.284], [-24, 124.522], [-23, 117.266], [-22, 110.48], [-21, 104.13], [-20, 98.185], [-19, 92.618], [-18, 87.402], [-17, 82.513], [-16, 77.927], [-15, 73.626], [-14, 69.588], [-13, 65.797], [-12, 62.237], [-11, 58.89], [-10, 55.744], [-9, 52.786], [-8, 50.002], [-7, 47.382], [-6, 44.916], [-5, 42.592], [-4, 40.4], [-3, 38.333], [-2, 36.385], [-1, 34.548], [0, 32.814], [1, 31.179], [2, 29.636], [3, 28.178], [4, 26.8], [5, 25.497], [6, 24.263], [7, 23.096], [8, 21.992], [9, 20.947], [10, 19.958], [11, 19.022], [12, 18.135], [13, 17.294], [14, 16.498], [15, 15.742], [16, 15.025], [17, 14.345], [18, 13.699], [19, 13.086], [20, 12.504], [21, 11.951], [22, 11.426], [23, 10.926], [24, 10.452], [25, 10], [26, 9.57], [27, 9.162], [28, 8.773], [29, 8.402], [30, 8.049], [31, 7.713], [32, 7.393], [33, 7.088], [34, 6.797], [35, 6.52], [36, 6.255], [37, 6.003], [38, 5.762], [39, 5.532], [40, 5.313], [41, 5.103], [42, 4.903], [43, 4.711], [44, 4.529], [45, 4.354], [46, 4.187], [47, 4.027], [48, 3.874], [49, 3.728], [50, 3.588], [51, 3.454], [52, 3.326], [53, 3.203], [54, 3.086], [55, 2.973], [56, 2.865], [57, 2.761], [58, 2.662], [59, 2.567], [60, 2.476], [61, 2.388], [62, 2.304], [63, 2.224], [64, 2.146], [65, 2.072], [66, 2.001], [67, 1.932], [68, 1.866], [69, 1.803], [70, 1.742], [71, 1.684], [72, 1.628], [73, 1.574], [74, 1.522], [75, 1.472], [76, 1.424], [77, 1.378], [78, 1.333], [79, 1.29], [80, 1.249], [81, 1.209], [82, 1.171], [83, 1.134], [84, 1.099], [85, 1.065], [86, 1.032], [87, 1], [88, 0.969], [89, 0.94], [90, 0.911], [91, 0.884], [92, 0.857], [93, 0.831], [94, 0.807], [95, 0.783], [96, 0.76], [97, 0.738], [98, 0.716], [99, 0.695], [100, 0.675], [101, 0.656], [102, 0.637], [103, 0.619], [104, 0.602], [105, 0.585], [106, 0.569], [107, 0.553], [108, 0.538], [109, 0.523], [110, 0.508], [111, 0.495], [112, 0.481], [113, 0.468], [114, 0.456], [115, 0.443], [116, 0.432], [117, 0.42], [118, 0.409], [119, 0.399], [120, 0.388], [121, 0.378], [122, 0.368], [123, 0.359], [124, 0.35], [125, 0.341] ] function interpolate(x, x0, y0, x1, y1) { if (x1 === x0) return y0 return y0 + (x - x0) * (y1 - y0) / (x1 - x0) } function resistanceToTemperature(resistanceKohm) { const resistance = Number(resistanceKohm) if (!Number.isFinite(resistance) || resistance <= 0) return null if (resistance >= RT_TABLE[0][1]) return RT_TABLE[0][0] const last = RT_TABLE[RT_TABLE.length - 1] if (resistance <= last[1]) return last[0] for (let index = 0; index < RT_TABLE.length - 1; index += 1) { const [temp0, resistance0] = RT_TABLE[index] const [temp1, resistance1] = RT_TABLE[index + 1] if (resistance <= resistance0 && resistance >= resistance1) { return interpolate(resistance, resistance0, temp0, resistance1, temp1) } } return null } function temperatureToResistance(temperatureCelsius) { const temperature = Number(temperatureCelsius) if (!Number.isFinite(temperature)) return null if (temperature <= RT_TABLE[0][0]) return RT_TABLE[0][1] const last = RT_TABLE[RT_TABLE.length - 1] if (temperature >= last[0]) return last[1] for (let index = 0; index < RT_TABLE.length - 1; index += 1) { const [temp0, resistance0] = RT_TABLE[index] const [temp1, resistance1] = RT_TABLE[index + 1] if (temperature >= temp0 && temperature <= temp1) { return interpolate(temperature, temp0, resistance0, temp1, resistance1) } } return null } function voltageRatioToResistance(voltageRatio) { const ratio = Number(voltageRatio) if (!Number.isFinite(ratio) || ratio <= 0 || ratio >= 1) return null return PULLUP_RESISTOR_KOHM * ratio / (1 - ratio) } function resistanceToVoltageRatio(resistanceKohm) { const resistance = Number(resistanceKohm) if (!Number.isFinite(resistance) || resistance <= 0) return null return resistance / (PULLUP_RESISTOR_KOHM + resistance) } function rawToTemperature(rawValue, scaleMax) { const rawNumber = Number(rawValue) const maxValue = Number(scaleMax) if (!Number.isFinite(rawNumber) || !Number.isFinite(maxValue) || maxValue <= 0) return null if (rawNumber <= 0) return RT_TABLE[RT_TABLE.length - 1][0] if (rawNumber >= maxValue) return RT_TABLE[0][0] return resistanceToTemperature(voltageRatioToResistance(rawNumber / maxValue)) } function temperatureToRaw(temperatureCelsius, scaleMax) { const resistance = temperatureToResistance(temperatureCelsius) const ratio = resistanceToVoltageRatio(resistance) const maxValue = Number(scaleMax) if (!Number.isFinite(ratio) || !Number.isFinite(maxValue) || maxValue <= 0) return null return ratio * maxValue } module.exports = { rawToTemperature, temperatureToRaw }