From 789659599cc711d9b45adf73dbd0cd66731a045c Mon Sep 17 00:00:00 2001
From: Geoff Shannon <geoffpshannon@gmail.com>
Date: Thu, 10 Oct 2019 17:11:34 -0700
Subject: [PATCH] adc_temperature: add support for AD849x thermocouple
 amplifiers (#1994)

Signed-off-by: Geoff Shannon <geoffpshannon@gmail.com>
---
 config/example-extras.cfg        | 18 +++++++
 klippy/extras/adc_temperature.py | 88 +++++++++++++++++++++++++++++++-
 2 files changed, 104 insertions(+), 2 deletions(-)

diff --git a/config/example-extras.cfg b/config/example-extras.cfg
index c595bafd3..feba2a380 100644
--- a/config/example-extras.cfg
+++ b/config/example-extras.cfg
@@ -835,6 +835,24 @@
 #   pullup_resistor parameter (see example.cfg for details). At least
 #   two measurements must be provided.
 
+# AD849x series thermocouple amplifier temperature sensors. The
+# following parameters are available in heater sections that use one
+# of these sensor types.
+#[extruder]
+# See the "extruder" section in example.cfg for a description of
+# heater parameters. The parameters below describe sensor parameters.
+#sensor_type:
+#   One of "AD8494", "AD8495", "AD8496", "AD8497"
+#adc_voltage:
+#   The reference voltage for the ADC pin specified for
+#   sensor_pin. This parameter is required.
+#sensor_pin:
+#   The ADC pin the AD849x device is connected to. This parameter is
+#   required.
+#voltage_offset:
+#   The voltage applied to the REF pin on the AD849x device being
+#   used. The default is 0.
+
 # MAXxxxxx serial peripheral interface (SPI) temperature based
 # sensors. The following parameters are available in heater sections
 # that use one of these sensor types.
diff --git a/klippy/extras/adc_temperature.py b/klippy/extras/adc_temperature.py
index 7f8b0f467..2e3337927 100644
--- a/klippy/extras/adc_temperature.py
+++ b/klippy/extras/adc_temperature.py
@@ -88,9 +88,10 @@ class LinearInterpolate:
 class LinearVoltage:
     def __init__(self, config, params):
         adc_voltage = config.getfloat('adc_voltage', 5., above=0.)
+        voltage_offset = config.getfloat('voltage_offset', 0.0)
         samples = []
         for temp, volt in params:
-            adc = volt / adc_voltage
+            adc = (volt - voltage_offset) / adc_voltage
             if adc < 0. or adc > 1.:
                 logging.warn("Ignoring adc sample %.3f/%.3f in heater %s",
                              temp, volt, config.get_name())
@@ -172,6 +173,84 @@ AD595 = [
     (420., 4.266), (440., 4.476), (460., 4.686), (480., 4.896)
 ]
 
+
+AD8494 = [
+    (-180, -0.714), (-160, -0.658), (-140, -0.594), (-120, -0.523),
+    (-100, -0.446), (-80, -0.365), (-60, -0.278), (-40, -0.188),
+    (-20, -0.095), (0, 0.002), (20, 0.1), (25, 0.125), (40, 0.201),
+    (60, 0.303), (80, 0.406), (100, 0.511), (120, 0.617), (140, 0.723),
+    (160, 0.829), (180, 0.937), (200, 1.044), (220, 1.151), (240, 1.259),
+    (260, 1.366), (280, 1.473), (300, 1.58), (320, 1.687), (340, 1.794),
+    (360, 1.901), (380, 2.008), (400, 2.114), (420, 2.221), (440, 2.328),
+    (460, 2.435), (480, 2.542), (500, 2.65), (520, 2.759), (540, 2.868),
+    (560, 2.979), (580, 3.09), (600, 3.203), (620, 3.316), (640, 3.431),
+    (660, 3.548), (680, 3.666), (700, 3.786), (720, 3.906), (740, 4.029),
+    (760, 4.152), (780, 4.276), (800, 4.401), (820, 4.526), (840, 4.65),
+    (860, 4.774), (880, 4.897), (900, 5.018), (920, 5.138), (940, 5.257),
+    (960, 5.374), (980, 5.49), (1000, 5.606), (1020, 5.72), (1040, 5.833),
+    (1060, 5.946), (1080, 6.058), (1100, 6.17), (1120, 6.282), (1140, 6.394),
+    (1160, 6.505), (1180, 6.616), (1200, 6.727)
+]
+
+AD8495 = [
+    (-260, -0.786), (-240, -0.774), (-220, -0.751), (-200, -0.719),
+    (-180, -0.677), (-160, -0.627), (-140, -0.569), (-120, -0.504),
+    (-100, -0.432), (-80, -0.355), (-60, -0.272), (-40, -0.184), (-20, -0.093),
+    (0, 0.003), (20, 0.1), (25, 0.125), (40, 0.2), (60, 0.301), (80, 0.402),
+    (100, 0.504), (120, 0.605), (140, 0.705), (160, 0.803), (180, 0.901),
+    (200, 0.999), (220, 1.097), (240, 1.196), (260, 1.295), (280, 1.396),
+    (300, 1.497), (320, 1.599), (340, 1.701), (360, 1.803), (380, 1.906),
+    (400, 2.01), (420, 2.113), (440, 2.217), (460, 2.321), (480, 2.425),
+    (500, 2.529), (520, 2.634), (540, 2.738), (560, 2.843), (580, 2.947),
+    (600, 3.051), (620, 3.155), (640, 3.259), (660, 3.362), (680, 3.465),
+    (700, 3.568), (720, 3.67), (740, 3.772), (760, 3.874), (780, 3.975),
+    (800, 4.076), (820, 4.176), (840, 4.275), (860, 4.374), (880, 4.473),
+    (900, 4.571), (920, 4.669), (940, 4.766), (960, 4.863), (980, 4.959),
+    (1000, 5.055), (1020, 5.15), (1040, 5.245), (1060, 5.339), (1080, 5.432),
+    (1100, 5.525), (1120, 5.617), (1140, 5.709), (1160, 5.8), (1180, 5.891),
+    (1200, 5.98), (1220, 6.069), (1240, 6.158), (1260, 6.245), (1280, 6.332),
+    (1300, 6.418), (1320, 6.503), (1340, 6.587), (1360, 6.671), (1380, 6.754)
+]
+
+AD8496 = [
+    (-180, -0.642), (-160, -0.59), (-140, -0.53), (-120, -0.464),
+    (-100, -0.392), (-80, -0.315), (-60, -0.235), (-40, -0.15), (-20, -0.063),
+    (0, 0.027), (20, 0.119), (25, 0.142), (40, 0.213), (60, 0.308),
+    (80, 0.405), (100, 0.503), (120, 0.601), (140, 0.701), (160, 0.8),
+    (180, 0.9), (200, 1.001), (220, 1.101), (240, 1.201), (260, 1.302),
+    (280, 1.402), (300, 1.502), (320, 1.602), (340, 1.702), (360, 1.801),
+    (380, 1.901), (400, 2.001), (420, 2.1), (440, 2.2), (460, 2.3),
+    (480, 2.401), (500, 2.502), (520, 2.603), (540, 2.705), (560, 2.808),
+    (580, 2.912), (600, 3.017), (620, 3.124), (640, 3.231), (660, 3.34),
+    (680, 3.451), (700, 3.562), (720, 3.675), (740, 3.789), (760, 3.904),
+    (780, 4.02), (800, 4.137), (820, 4.254), (840, 4.37), (860, 4.486),
+    (880, 4.6), (900, 4.714), (920, 4.826), (940, 4.937), (960, 5.047),
+    (980, 5.155), (1000, 5.263), (1020, 5.369), (1040, 5.475), (1060, 5.581),
+    (1080, 5.686), (1100, 5.79), (1120, 5.895), (1140, 5.999), (1160, 6.103),
+    (1180, 6.207), (1200, 6.311)
+]
+
+AD8497 = [
+    (-260, -0.785), (-240, -0.773), (-220, -0.751), (-200, -0.718),
+    (-180, -0.676), (-160, -0.626), (-140, -0.568), (-120, -0.503),
+    (-100, -0.432), (-80, -0.354), (-60, -0.271), (-40, -0.184),
+    (-20, -0.092), (0, 0.003), (20, 0.101), (25, 0.126), (40, 0.2),
+    (60, 0.301), (80, 0.403), (100, 0.505), (120, 0.605), (140, 0.705),
+    (160, 0.804), (180, 0.902), (200, 0.999), (220, 1.097), (240, 1.196),
+    (260, 1.296), (280, 1.396), (300, 1.498), (320, 1.599), (340, 1.701),
+    (360, 1.804), (380, 1.907), (400, 2.01), (420, 2.114), (440, 2.218),
+    (460, 2.322), (480, 2.426), (500, 2.53), (520, 2.634), (540, 2.739),
+    (560, 2.843), (580, 2.948), (600, 3.052), (620, 3.156), (640, 3.259),
+    (660, 3.363), (680, 3.466), (700, 3.569), (720, 3.671), (740, 3.773),
+    (760, 3.874), (780, 3.976), (800, 4.076), (820, 4.176), (840, 4.276),
+    (860, 4.375), (880, 4.474), (900, 4.572), (920, 4.67), (940, 4.767),
+    (960, 4.863), (980, 4.96), (1000, 5.055), (1020, 5.151), (1040, 5.245),
+    (1060, 5.339), (1080, 5.433), (1100, 5.526), (1120, 5.618), (1140, 5.71),
+    (1160, 5.801), (1180, 5.891), (1200, 5.981), (1220, 6.07), (1240, 6.158),
+    (1260, 6.246), (1280, 6.332), (1300, 6.418), (1320, 6.503), (1340, 6.588),
+    (1360, 6.671), (1380, 6.754)
+]
+
 PT100 = [
     (0, 0.00), (1, 1.11), (10, 1.15), (20, 1.20), (30, 1.24), (40, 1.28),
     (50, 1.32), (60, 1.36), (70, 1.40), (80, 1.44), (90, 1.48), (100, 1.52),
@@ -188,7 +267,12 @@ PT100 = [
 def load_config(config):
     # Register default sensors
     pheater = config.get_printer().lookup_object("heater")
-    for sensor_type, params in [("AD595", AD595), ("PT100 INA826", PT100)]:
+    for sensor_type, params in [("AD595", AD595),
+                                ("AD8494", AD8494),
+                                ("AD8495", AD8495),
+                                ("AD8496", AD8496),
+                                ("AD8497", AD8497),
+                                ("PT100 INA826", PT100)]:
         func = (lambda config, params=params:
                 PrinterADCtoTemperature(config, LinearVoltage(config, params)))
         pheater.add_sensor_factory(sensor_type, func)