Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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