Manages the 1-wire bus

Dependents:   oldheating heating

Committer:
andrewboyson
Date:
Wed Jun 10 17:02:21 2020 +0000
Revision:
10:b4e0b4c4e045
Parent:
9:6f663ad53c7e
Modified the web module to allow up to 8 devices rather than 4.

Who changed what in which revision?

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