Manages the 1-wire bus

Dependents:   oldheating heating

Revision:
2:79cad6a51fd0
Parent:
1:c272b1fcc834
Child:
8:ceafef18cbf7
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/web/web-1wire-class.js	Sat Apr 27 09:26:15 2019 +0000
@@ -0,0 +1,32 @@
+//OneWire script
+'use strict';
+
+class OneWire
+{
+    static DS18B20ToString(text)
+    {
+        switch (text)
+        {
+            case '7FFF': return 'CRC error'                     ;
+            case '7FFE': return 'ROM not found'                 ;
+            case '7FFD': return 'Timed out'                     ;
+            case '7FFC': return 'No device detected after reset';
+            case '7FFB': return 'Device removed during search'  ;
+        }
+        let isNegative = false;
+        switch(text.charAt(0))
+        {
+            case '8': text = '7' + text.substr(1, 3); isNegative = true; break;
+            case '9': text = '6' + text.substr(1, 3); isNegative = true; break;
+            case 'A': text = '5' + text.substr(1, 3); isNegative = true; break;
+            case 'B': text = '4' + text.substr(1, 3); isNegative = true; break;
+            case 'C': text = '3' + text.substr(1, 3); isNegative = true; break;
+            case 'D': text = '2' + text.substr(1, 3); isNegative = true; break;
+            case 'E': text = '1' + text.substr(1, 3); isNegative = true; break;
+            case 'F': text = '0' + text.substr(1, 3); isNegative = true; break;
+        }
+        let value = parseInt(text, 16) / 16.0;
+        if (isNegative) value = -value;
+        return value.toFixed(1);
+    }
+}
\ No newline at end of file