forked from CapnBry/Powermon433
-
Notifications
You must be signed in to change notification settings - Fork 6
/
temp_lerp.cpp
128 lines (114 loc) · 2.24 KB
/
temp_lerp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/* temp_lerp.cpp - scruss, 2014-10-06
int temp_lerp(uint8_t x);
for input x (0..255) returns equivalent °F value
int fudged_f_to_c(int f);
special conversion factor from °F to °C;
*** NOT FOR GENERAL USE ***
*/
#include <Arduino.h>
#include "temp_lerp.h"
#define TEMP_TABSIZE 22
static coord_t temp_tab[TEMP_TABSIZE] = {
{
0, -49 }
,
{
5, -45 }
,
{
10, -42 }
,
{
20, -22 }
,
{
30, -7 }
,
{
40, 5 }
,
{
50, 16 }
,
{
70, 34 }
,
{
80, 42 }
,
{
90, 49 }
,
{
100, 57 }
,
{
130, 78 }
,
{
150, 94 }
,
{
152, 96 }
,
{
154, 97 }
,
{
158, 101 }
,
{
160, 102 }
,
{
176, 118 }
,
{
180, 121 }
,
{
184, 126 }
,
{
185, 127 }
,
{
255, 127 }
};
int temp_lerp(uint8_t x) {
int i;
for (i = 0; i < TEMP_TABSIZE - 1; i++) {
if (temp_tab[i].x <= x && temp_tab[i + 1].x >= x)
return temp_tab[i].y + (temp_tab[i + 1].y - temp_tab[i].y) * (x - temp_tab[i].x) / (temp_tab[i + 1].x -
temp_tab[i].x);
}
return -125; /* should never get here, so return stupid */
}
int fudged_f_to_c(int f) {
/****************************************************
* ### #### ###########
* ### ## #### #### THIS IS ** NOT **
* #### ## ### #### GENERAL-PURPOSE °F
* #### # ### #### TO °C CONVERSION
* ##### # ### ### CODE !!!
* # #### # ### ###
* # #### ## ####### It is fudged to
* # ### # #### #### reduce specific
* ## ### # ### #### quantization error
* # ##### ### #### in the Powermon433
* # #### ### #### code.
* # ### ### ####
* ## ## #### #### ** DO NOT REUSE **
* #### # ##########
****************************************************/
return (10 * f - 308) / 18;
}
/*
int main(int argc, char **argv) {
int x, y;
for (x = 0; x <= 255; x++) {
y = temp_lerp(temp_tab, x, TEMP_TABSIZE);
printf("%5d : %5d : %5d\n", x, y, (10 * y - 308) / 18);
}
}
*/