Manages the 1-wire bus

Dependents:   oldheating heating

Committer:
andrewboyson
Date:
Sat Apr 27 09:26:15 2019 +0000
Revision:
2:79cad6a51fd0
Parent:
http/http-1wire-script.js@1:c272b1fcc834
Child:
8:ceafef18cbf7
Updated web library

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 1:c272b1fcc834 76 elem.textContent += OneWire.DS18B20ToString(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 1:c272b1fcc834 90 elem = Ajax.getElementOrNull('ajax-name-0'); if (elem) elem.textContent = assignedNames[0];
andrewboyson 1:c272b1fcc834 91 elem = Ajax.getElementOrNull('ajax-name-1'); if (elem) elem.textContent = assignedNames[1];
andrewboyson 1:c272b1fcc834 92 elem = Ajax.getElementOrNull('ajax-name-2'); if (elem) elem.textContent = assignedNames[2];
andrewboyson 1:c272b1fcc834 93 elem = Ajax.getElementOrNull('ajax-name-3'); if (elem) elem.textContent = assignedNames[3];
andrewboyson 1:c272b1fcc834 94 elem = Ajax.getElementOrNull('ajax-rom-0' ); if (elem) elem.value = assignedRoms[0];
andrewboyson 1:c272b1fcc834 95 elem = Ajax.getElementOrNull('ajax-rom-1' ); if (elem) elem.value = assignedRoms[1];
andrewboyson 1:c272b1fcc834 96 elem = Ajax.getElementOrNull('ajax-rom-2' ); if (elem) elem.value = assignedRoms[2];
andrewboyson 1:c272b1fcc834 97 elem = Ajax.getElementOrNull('ajax-rom-3' ); if (elem) elem.value = assignedRoms[3];
andrewboyson 1:c272b1fcc834 98 }
andrewboyson 1:c272b1fcc834 99
andrewboyson 1:c272b1fcc834 100 Ajax.server = '/1wire-ajax';
andrewboyson 1:c272b1fcc834 101 Ajax.onResponse = function() { parse(); display(); };
andrewboyson 1:c272b1fcc834 102 Ajax.init();