Manages the 1-wire bus
Dependents: oldheating heating
web/web-1wire-script.inc@2:79cad6a51fd0, 2019-04-27 (annotated)
- Committer:
- andrewboyson
- Date:
- Sat Apr 27 09:26:15 2019 +0000
- Revision:
- 2:79cad6a51fd0
- Parent:
- http/http-1wire-script.inc@1:c272b1fcc834
- Child:
- 8:ceafef18cbf7
Updated web library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andrewboyson | 1:c272b1fcc834 | 1 | "//1wire script\n" |
andrewboyson | 1:c272b1fcc834 | 2 | "'use strict';\n" |
andrewboyson | 1:c272b1fcc834 | 3 | "\n" |
andrewboyson | 1:c272b1fcc834 | 4 | "let allRomValues = new Map();\n" |
andrewboyson | 1:c272b1fcc834 | 5 | "let oneWireScanTime = 0;\n" |
andrewboyson | 1:c272b1fcc834 | 6 | "let oneWireLowTweak = 0;\n" |
andrewboyson | 1:c272b1fcc834 | 7 | "let oneWireFloatTweak = 0;\n" |
andrewboyson | 1:c272b1fcc834 | 8 | "let oneWireReadTweak = 0;\n" |
andrewboyson | 1:c272b1fcc834 | 9 | "let oneWireHighTweak = 0;\n" |
andrewboyson | 1:c272b1fcc834 | 10 | "let oneWireReleaseTweak = 0;\n" |
andrewboyson | 1:c272b1fcc834 | 11 | "let oneWireTrace = false;\n" |
andrewboyson | 1:c272b1fcc834 | 12 | "let assignedRoms = new Array();\n" |
andrewboyson | 1:c272b1fcc834 | 13 | "let assignedNames = new Array();\n" |
andrewboyson | 1:c272b1fcc834 | 14 | "\n" |
andrewboyson | 1:c272b1fcc834 | 15 | "function getAssignedName(rom)\n" |
andrewboyson | 1:c272b1fcc834 | 16 | "{\n" |
andrewboyson | 1:c272b1fcc834 | 17 | " for (let i = 0; i < assignedRoms.length; i++)\n" |
andrewboyson | 1:c272b1fcc834 | 18 | " {\n" |
andrewboyson | 1:c272b1fcc834 | 19 | " if (assignedRoms[i] === rom) return assignedNames[i]; \n" |
andrewboyson | 1:c272b1fcc834 | 20 | " }\n" |
andrewboyson | 1:c272b1fcc834 | 21 | " return '';\n" |
andrewboyson | 1:c272b1fcc834 | 22 | "}\n" |
andrewboyson | 1:c272b1fcc834 | 23 | "\n" |
andrewboyson | 1:c272b1fcc834 | 24 | "function addRomValue(line)\n" |
andrewboyson | 1:c272b1fcc834 | 25 | "{\n" |
andrewboyson | 1:c272b1fcc834 | 26 | " if (line) allRomValues.set(line.substr(0, 16), line.substr(16));\n" |
andrewboyson | 1:c272b1fcc834 | 27 | "}\n" |
andrewboyson | 1:c272b1fcc834 | 28 | "function parseAllRomValues(topic)\n" |
andrewboyson | 1:c272b1fcc834 | 29 | "{\n" |
andrewboyson | 1:c272b1fcc834 | 30 | " allRomValues.clear();\n" |
andrewboyson | 1:c272b1fcc834 | 31 | " topic.split('\\n').forEach(addRomValue);\n" |
andrewboyson | 1:c272b1fcc834 | 32 | "}\n" |
andrewboyson | 1:c272b1fcc834 | 33 | "function parseTimings(topic)\n" |
andrewboyson | 1:c272b1fcc834 | 34 | "{\n" |
andrewboyson | 1:c272b1fcc834 | 35 | " let lines = topic.split('\\n');\n" |
andrewboyson | 1:c272b1fcc834 | 36 | " oneWireScanTime = Ajax.hexToSignedInt16(lines[0]);\n" |
andrewboyson | 1:c272b1fcc834 | 37 | " oneWireLowTweak = Ajax.hexToSignedInt16(lines[1]);\n" |
andrewboyson | 1:c272b1fcc834 | 38 | " oneWireFloatTweak = Ajax.hexToSignedInt16(lines[2]);\n" |
andrewboyson | 1:c272b1fcc834 | 39 | " oneWireReadTweak = Ajax.hexToSignedInt16(lines[3]);\n" |
andrewboyson | 1:c272b1fcc834 | 40 | " oneWireHighTweak = Ajax.hexToSignedInt16(lines[4]);\n" |
andrewboyson | 1:c272b1fcc834 | 41 | " oneWireReleaseTweak = Ajax.hexToSignedInt16(lines[5]);\n" |
andrewboyson | 1:c272b1fcc834 | 42 | " oneWireTrace = lines[6] != '0';\n" |
andrewboyson | 1:c272b1fcc834 | 43 | "}\n" |
andrewboyson | 1:c272b1fcc834 | 44 | "function addRom(line)\n" |
andrewboyson | 1:c272b1fcc834 | 45 | "{\n" |
andrewboyson | 1:c272b1fcc834 | 46 | " if (line)\n" |
andrewboyson | 1:c272b1fcc834 | 47 | " {\n" |
andrewboyson | 1:c272b1fcc834 | 48 | " assignedRoms.push(line.substr(0, 16))\n" |
andrewboyson | 1:c272b1fcc834 | 49 | " assignedNames.push(line.substr(16));\n" |
andrewboyson | 1:c272b1fcc834 | 50 | " }\n" |
andrewboyson | 1:c272b1fcc834 | 51 | "}\n" |
andrewboyson | 1:c272b1fcc834 | 52 | "function parseAssigned(topic)\n" |
andrewboyson | 1:c272b1fcc834 | 53 | "{\n" |
andrewboyson | 1:c272b1fcc834 | 54 | " assignedRoms = new Array();\n" |
andrewboyson | 1:c272b1fcc834 | 55 | " assignedNames = new Array();\n" |
andrewboyson | 1:c272b1fcc834 | 56 | " topic.split('\\n').forEach(addRom);\n" |
andrewboyson | 1:c272b1fcc834 | 57 | "}\n" |
andrewboyson | 1:c272b1fcc834 | 58 | "function parse()\n" |
andrewboyson | 1:c272b1fcc834 | 59 | "{\n" |
andrewboyson | 1:c272b1fcc834 | 60 | " let topics = Ajax.response.split('\\f');\n" |
andrewboyson | 1:c272b1fcc834 | 61 | " parseTimings (topics[0]);\n" |
andrewboyson | 1:c272b1fcc834 | 62 | " parseAllRomValues(topics[1]);\n" |
andrewboyson | 1:c272b1fcc834 | 63 | " parseAssigned (topics[2]);\n" |
andrewboyson | 1:c272b1fcc834 | 64 | "}\n" |
andrewboyson | 1:c272b1fcc834 | 65 | "function display()\n" |
andrewboyson | 1:c272b1fcc834 | 66 | "{\n" |
andrewboyson | 1:c272b1fcc834 | 67 | " let elem;\n" |
andrewboyson | 1:c272b1fcc834 | 68 | " elem = Ajax.getElementOrNull('ajax-device-values');\n" |
andrewboyson | 1:c272b1fcc834 | 69 | " if (elem) \n" |
andrewboyson | 1:c272b1fcc834 | 70 | " {\n" |
andrewboyson | 1:c272b1fcc834 | 71 | " elem.textContent = '';\n" |
andrewboyson | 1:c272b1fcc834 | 72 | " for (let [key, value] of allRomValues)\n" |
andrewboyson | 1:c272b1fcc834 | 73 | " {\n" |
andrewboyson | 1:c272b1fcc834 | 74 | " elem.textContent += key;\n" |
andrewboyson | 1:c272b1fcc834 | 75 | " elem.textContent += ' '; \n" |
andrewboyson | 1:c272b1fcc834 | 76 | " elem.textContent += OneWire.DS18B20ToString(value);\n" |
andrewboyson | 1:c272b1fcc834 | 77 | " elem.textContent += ' ';\n" |
andrewboyson | 1:c272b1fcc834 | 78 | " elem.textContent += getAssignedName(key);\n" |
andrewboyson | 1:c272b1fcc834 | 79 | " elem.textContent += '\\r\\n';\n" |
andrewboyson | 1:c272b1fcc834 | 80 | " }\n" |
andrewboyson | 1:c272b1fcc834 | 81 | " }\n" |
andrewboyson | 1:c272b1fcc834 | 82 | " elem = Ajax.getElementOrNull('ajax-1-wire-scan-time' ); if (elem) elem.textContent = oneWireScanTime;\n" |
andrewboyson | 1:c272b1fcc834 | 83 | " elem = Ajax.getElementOrNull('ajax-1-wire-tweak-low' ); if (elem) elem.textContent = oneWireLowTweak;\n" |
andrewboyson | 1:c272b1fcc834 | 84 | " elem = Ajax.getElementOrNull('ajax-1-wire-tweak-float' ); if (elem) elem.textContent = oneWireFloatTweak;\n" |
andrewboyson | 1:c272b1fcc834 | 85 | " elem = Ajax.getElementOrNull('ajax-1-wire-tweak-read' ); if (elem) elem.textContent = oneWireReadTweak;\n" |
andrewboyson | 1:c272b1fcc834 | 86 | " elem = Ajax.getElementOrNull('ajax-1-wire-tweak-high' ); if (elem) elem.textContent = oneWireHighTweak;\n" |
andrewboyson | 1:c272b1fcc834 | 87 | " elem = Ajax.getElementOrNull('ajax-1-wire-tweak-release'); if (elem) elem.textContent = oneWireReleaseTweak;\n" |
andrewboyson | 1:c272b1fcc834 | 88 | " elem = Ajax.getElementOrNull('ajax-1-wire-trace' ); if (elem) elem.setAttribute('dir', oneWireTrace ? 'rtl' : 'ltr');\n" |
andrewboyson | 1:c272b1fcc834 | 89 | "\n" |
andrewboyson | 1:c272b1fcc834 | 90 | " elem = Ajax.getElementOrNull('ajax-name-0'); if (elem) elem.textContent = assignedNames[0];\n" |
andrewboyson | 1:c272b1fcc834 | 91 | " elem = Ajax.getElementOrNull('ajax-name-1'); if (elem) elem.textContent = assignedNames[1];\n" |
andrewboyson | 1:c272b1fcc834 | 92 | " elem = Ajax.getElementOrNull('ajax-name-2'); if (elem) elem.textContent = assignedNames[2];\n" |
andrewboyson | 1:c272b1fcc834 | 93 | " elem = Ajax.getElementOrNull('ajax-name-3'); if (elem) elem.textContent = assignedNames[3];\n" |
andrewboyson | 1:c272b1fcc834 | 94 | " elem = Ajax.getElementOrNull('ajax-rom-0' ); if (elem) elem.value = assignedRoms[0];\n" |
andrewboyson | 1:c272b1fcc834 | 95 | " elem = Ajax.getElementOrNull('ajax-rom-1' ); if (elem) elem.value = assignedRoms[1];\n" |
andrewboyson | 1:c272b1fcc834 | 96 | " elem = Ajax.getElementOrNull('ajax-rom-2' ); if (elem) elem.value = assignedRoms[2];\n" |
andrewboyson | 1:c272b1fcc834 | 97 | " elem = Ajax.getElementOrNull('ajax-rom-3' ); if (elem) elem.value = assignedRoms[3];\n" |
andrewboyson | 1:c272b1fcc834 | 98 | "}\n" |
andrewboyson | 1:c272b1fcc834 | 99 | "\n" |
andrewboyson | 1:c272b1fcc834 | 100 | "Ajax.server = '/1wire-ajax';\n" |
andrewboyson | 1:c272b1fcc834 | 101 | "Ajax.onResponse = function() { parse(); display(); };\n" |
andrewboyson | 1:c272b1fcc834 | 102 | "Ajax.init();\n" |
andrewboyson | 1:c272b1fcc834 | 103 | "" |