This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RoundedAmbientLightSensor { | |
constructor(frequency, threshold) { | |
this.onchange_ = null; | |
this.illuminance_ = null; | |
this.sensor_ = new AmbientLightSensor({ frequency }); | |
this.sensor_.onchange = () => { | |
let old_illuminance = this.illuminance_; | |
if (this.sensor_.illuminance != null) | |
this.illuminance_ = Math.ceil(this.sensor_.illuminance / threshold) * threshold; | |
if (this.onchange_ != null | |
&& this.illuminance_ != null | |
&& old_illuminance != this.illuminance_) { | |
this.onchange_(); | |
} | |
}; | |
} | |
set onchange(func) { this.onchange_ = func; } | |
get onchange() { return this.onchange_; } | |
get illuminance() { return this.illuminance_; } | |
get timestamp() { return this.sensor_.timestamp; } | |
start() { this.sensor_.start(); } | |
stop() { this.sensor_.stop(); } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment