And with that configuration, here's the DS18B20 module's example rewritten in modern JS:
import { connect } from './modules/DS18B20.js' let ow = new OneWire(A1) let sensor = connect(ow) setInterval(() => { sensor.getTemp(temp => console.log('Temp is ' + temp + '°C')) }, 1000)
The same code bundled with the module, uglified and sent to my Espruino after running rollup -c:
rollup -c
function DS18B20(t,e){if(this.bus=t,void 0===e?this.sCode=this.bus.search()[0]:parseInt(e).toString()==e&&e>=0&&e<=126?this.sCode=this.bus.search()[e]:this.sCode=e,!this.sCode)throw new Error("No DS18B20 found");this.type=parseInt(this.sCode[0]+this.sCode[1])}var C={CONVERT_T:68,COPY:72,READ:190,WRITE:78};DS18B20.prototype._r=function(){var t=this.bus;return t.select(this.sCode),t.write(C.READ),t.read(9)},DS18B20.prototype._w=function(t,e,s){var r=this.bus;r.select(this.sCode),r.write([C.WRITE,t,e,s]),r.select(this.sCode),r.write(C.COPY),r.reset()},DS18B20.prototype.setRes=function(t){var e=this._r();t=[31,63,95,127][E.clip(t,9,12)-9],this._w(e[2],e[3],t)},DS18B20.prototype.getRes=function(){return[31,63,95,127].indexOf(this._r()[4])+9},DS18B20.prototype.isPresent=function(){return this.bus.search().indexOf(this.sCode)!==-1},DS18B20.prototype.getTemp=function(t){function e(e){for(var s=e._r(),r=0,i=0;i<8;i++){r^=s[i];for(var o=0;o<8;o++)r=r>>1^140*(1&r)}var n=null;return r==s[8]&&(n=s[0]+(s[1]<<8),32768&n&&(n-=65536),n/=10==e.type?2:16),t&&t(n),n}if(this.bus.select(this.sCode),this.bus.write(C.CONVERT_T,!0),!t)return e(this);var s={9:94,10:188,11:375,12:750};setTimeout(e,s[this.getRes()],this)},DS18B20.prototype.searchAlarm=function(){return this.bus.search(236)},DS18B20.prototype.setAlarm=function(t,e){t--,t<0&&(t+=256),e<0&&(e+=256);var s=this._r();this._w(e,t,s[4])};var connect=function(t,e){return new DS18B20(t,e)};let t=new OneWire(A1),e=connect(t);setInterval(()=>{e.getTemp(t=>console.log("Temp is "+t+"°C"))},1e3);
Not so pretty anymore..
@Joakim started
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
And with that configuration, here's the DS18B20 module's example rewritten in modern JS:
The same code bundled with the module, uglified and sent to my Espruino after running
rollup -c
:Not so pretty anymore..