Manages the 1-wire bus

Dependents:   oldheating heating

Committer:
andrewboyson
Date:
Thu Feb 18 16:47:12 2021 +0000
Revision:
11:3859fee99d5d
Parent:
10:b4e0b4c4e045
Added 'value not set' to the list of possible values.

Who changed what in which revision?

UserRevisionLine numberNew 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 9:6f663ad53c7e 76 " elem.textContent += OneWire.DS18B20ToString(Ajax.hexToSignedInt16(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 10:b4e0b4c4e045 90 " elem = Ajax.getElementOrNull('ajax-name-0'); if (elem && assignedNames.length > 0) elem.textContent = assignedNames[0];\n"
andrewboyson 10:b4e0b4c4e045 91 " elem = Ajax.getElementOrNull('ajax-name-1'); if (elem && assignedNames.length > 1) elem.textContent = assignedNames[1];\n"
andrewboyson 10:b4e0b4c4e045 92 " elem = Ajax.getElementOrNull('ajax-name-2'); if (elem && assignedNames.length > 2) elem.textContent = assignedNames[2];\n"
andrewboyson 10:b4e0b4c4e045 93 " elem = Ajax.getElementOrNull('ajax-name-3'); if (elem && assignedNames.length > 3) elem.textContent = assignedNames[3];\n"
andrewboyson 10:b4e0b4c4e045 94 " elem = Ajax.getElementOrNull('ajax-name-4'); if (elem && assignedNames.length > 4) elem.textContent = assignedNames[4];\n"
andrewboyson 10:b4e0b4c4e045 95 " elem = Ajax.getElementOrNull('ajax-name-5'); if (elem && assignedNames.length > 5) elem.textContent = assignedNames[5];\n"
andrewboyson 10:b4e0b4c4e045 96 " elem = Ajax.getElementOrNull('ajax-name-6'); if (elem && assignedNames.length > 6) elem.textContent = assignedNames[6];\n"
andrewboyson 10:b4e0b4c4e045 97 " elem = Ajax.getElementOrNull('ajax-name-7'); if (elem && assignedNames.length > 7) elem.textContent = assignedNames[7];\n"
andrewboyson 10:b4e0b4c4e045 98 " elem = Ajax.getElementOrNull('ajax-rom-0' ); if (elem && assignedRoms.length > 0) elem.value = assignedRoms[0];\n"
andrewboyson 10:b4e0b4c4e045 99 " elem = Ajax.getElementOrNull('ajax-rom-1' ); if (elem && assignedRoms.length > 1) elem.value = assignedRoms[1];\n"
andrewboyson 10:b4e0b4c4e045 100 " elem = Ajax.getElementOrNull('ajax-rom-2' ); if (elem && assignedRoms.length > 2) elem.value = assignedRoms[2];\n"
andrewboyson 10:b4e0b4c4e045 101 " elem = Ajax.getElementOrNull('ajax-rom-3' ); if (elem && assignedRoms.length > 3) elem.value = assignedRoms[3];\n"
andrewboyson 10:b4e0b4c4e045 102 " elem = Ajax.getElementOrNull('ajax-rom-4' ); if (elem && assignedRoms.length > 4) elem.value = assignedRoms[4];\n"
andrewboyson 10:b4e0b4c4e045 103 " elem = Ajax.getElementOrNull('ajax-rom-5' ); if (elem && assignedRoms.length > 5) elem.value = assignedRoms[5];\n"
andrewboyson 10:b4e0b4c4e045 104 " elem = Ajax.getElementOrNull('ajax-rom-6' ); if (elem && assignedRoms.length > 6) elem.value = assignedRoms[6];\n"
andrewboyson 10:b4e0b4c4e045 105 " elem = Ajax.getElementOrNull('ajax-rom-7' ); if (elem && assignedRoms.length > 7) elem.value = assignedRoms[7];\n"
andrewboyson 1:c272b1fcc834 106 "}\n"
andrewboyson 1:c272b1fcc834 107 "\n"
andrewboyson 1:c272b1fcc834 108 "Ajax.server = '/1wire-ajax';\n"
andrewboyson 1:c272b1fcc834 109 "Ajax.onResponse = function() { parse(); display(); };\n"
andrewboyson 1:c272b1fcc834 110 "Ajax.init();\n"
andrewboyson 1:c272b1fcc834 111 ""