Browse Source

Faster (bisect) search in thermistor tables (#10883)

Scott Lahteine 6 years ago
parent
commit
1ce97f1f6d
No account linked to committer's email address
1 changed files with 19 additions and 11 deletions
  1. 19
    11
      Marlin/src/module/temperature.cpp

+ 19
- 11
Marlin/src/module/temperature.cpp View File

@@ -937,17 +937,25 @@ void Temperature::manage_heater() {
937 937
 #define TEMP_AD595(RAW)  ((RAW) * 5.0 * 100.0 / 1024.0 / (OVERSAMPLENR) * (TEMP_SENSOR_AD595_GAIN) + TEMP_SENSOR_AD595_OFFSET)
938 938
 #define TEMP_AD8495(RAW) ((RAW) * 6.6 * 100.0 / 1024.0 / (OVERSAMPLENR) * (TEMP_SENSOR_AD8495_GAIN) + TEMP_SENSOR_AD8495_OFFSET)
939 939
 
940
-#define SCAN_THERMISTOR_TABLE(TBL,LEN) do{                          \
941
-  for (uint8_t i = 1; i < LEN; i++) {                               \
942
-    const short entry10 = (short)pgm_read_word(&TBL[i][0]);         \
943
-    if (entry10 > raw) {                                            \
944
-      const short entry00 = (short)pgm_read_word(&TBL[i-1][0]),     \
945
-                  entry01 = (short)pgm_read_word(&TBL[i-1][1]),     \
946
-                  entry11 = (short)pgm_read_word(&TBL[i][1]);       \
947
-      return entry01 + (raw - entry00) * float(entry11 - entry01) / float(entry10 - entry00); \
948
-    }                                                               \
949
-  }                                                                 \
950
-  return (short)pgm_read_word(&TBL[LEN-1][1]);                      \
940
+/**
941
+ * Bisect search for the range of the 'raw' value, then interpolate
942
+ * proportionally between the under and over values.
943
+ */
944
+#define SCAN_THERMISTOR_TABLE(TBL,LEN) do{                             \
945
+  uint8_t l = 0, r = LEN, m;                                           \
946
+  for (;;) {                                                           \
947
+    m = (l + r) >> 1;                                                  \
948
+    if (m == l || m == r) return (short)pgm_read_word(&TBL[LEN-1][1]); \
949
+    short v00 = pgm_read_word(&TBL[m-1][0]),                           \
950
+          v10 = pgm_read_word(&TBL[m-0][0]);                           \
951
+         if (raw < v00) r = m;                                         \
952
+    else if (raw > v10) l = m;                                         \
953
+    else {                                                             \
954
+      const short v01 = (short)pgm_read_word(&TBL[m-1][1]),            \
955
+                  v11 = (short)pgm_read_word(&TBL[m-0][1]);            \
956
+      return v01 + (raw - v00) * float(v11 - v01) / float(v10 - v00);  \
957
+    }                                                                  \
958
+  }                                                                    \
951 959
 }while(0)
952 960
 
953 961
 // Derived from RepRap FiveD extruder::getTemperature()

Loading…
Cancel
Save