Skip to content

Instantly share code, notes, and snippets.

@alexshalamov
Created May 18, 2017 10:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexshalamov/e6a250cad4fe7486453b27d0e0ca8d66 to your computer and use it in GitHub Desktop.
Save alexshalamov/e6a250cad4fe7486453b27d0e0ca8d66 to your computer and use it in GitHub Desktop.
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