2021-02-11 13:11:35 +03:00
|
|
|
namespace modules {
|
2021-02-11 14:05:12 +03:00
|
|
|
/**
|
|
|
|
* Detects the amount of light falling onto a given surface area.
|
2022-04-08 18:39:19 +03:00
|
|
|
*
|
2021-12-09 02:55:03 +03:00
|
|
|
* Note that this is different from _luminance_, the amount of light that passes through, emits from, or reflects off an object.
|
2021-02-11 14:05:12 +03:00
|
|
|
**/
|
|
|
|
//% fixedInstances blockGap=8
|
2021-03-11 17:07:38 +03:00
|
|
|
export class IlluminanceClient extends jacdac.SimpleSensorClient {
|
2022-04-08 18:39:19 +03:00
|
|
|
private readonly _illuminanceError: jacdac.RegisterClient<[number]>
|
2021-02-11 18:13:59 +03:00
|
|
|
|
2021-02-23 14:23:30 +03:00
|
|
|
constructor(role: string) {
|
2022-04-11 19:26:43 +03:00
|
|
|
super(
|
|
|
|
jacdac.SRV_ILLUMINANCE,
|
|
|
|
role,
|
|
|
|
jacdac.IlluminanceRegPack.Illuminance
|
|
|
|
)
|
2021-02-23 13:36:16 +03:00
|
|
|
|
2022-04-08 18:39:19 +03:00
|
|
|
this._illuminanceError = this.addRegister<[number]>(
|
|
|
|
jacdac.IlluminanceReg.IlluminanceError,
|
2022-04-19 22:08:59 +03:00
|
|
|
jacdac.IlluminanceRegPack.IlluminanceError,
|
|
|
|
jacdac.RegisterClientFlags.Optional
|
2022-04-08 18:39:19 +03:00
|
|
|
)
|
2021-02-11 13:11:35 +03:00
|
|
|
}
|
2021-02-11 18:13:59 +03:00
|
|
|
|
2021-02-11 13:11:35 +03:00
|
|
|
/**
|
2022-04-08 18:39:19 +03:00
|
|
|
* The amount of illuminance, as lumens per square metre.
|
|
|
|
*/
|
2021-02-23 13:36:16 +03:00
|
|
|
//% callInDebugger
|
2021-12-09 02:55:03 +03:00
|
|
|
//% group="Environment"
|
2022-05-16 22:11:00 +03:00
|
|
|
//% block="%illuminance illuminance (lux)"
|
2021-12-09 02:55:03 +03:00
|
|
|
//% blockId=jacdac_illuminance_illuminance___get
|
2021-02-23 14:23:30 +03:00
|
|
|
//% weight=100
|
2021-12-09 02:55:03 +03:00
|
|
|
illuminance(): number {
|
2022-04-08 18:39:19 +03:00
|
|
|
return this.reading()
|
2021-02-23 13:36:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-04-08 18:39:19 +03:00
|
|
|
* Error on the reported sensor value.
|
|
|
|
*/
|
2021-02-23 13:36:16 +03:00
|
|
|
//% callInDebugger
|
2021-12-09 02:55:03 +03:00
|
|
|
//% group="Environment"
|
2021-02-23 14:23:30 +03:00
|
|
|
//% weight=99
|
2021-12-09 02:55:03 +03:00
|
|
|
illuminanceError(): number {
|
2022-04-08 18:39:19 +03:00
|
|
|
this.start()
|
|
|
|
const values = this._illuminanceError.pauseUntilValues() as any[]
|
|
|
|
return values[0]
|
2021-02-23 13:36:16 +03:00
|
|
|
}
|
2021-03-11 17:07:38 +03:00
|
|
|
|
|
|
|
/**
|
2021-12-09 02:55:03 +03:00
|
|
|
* Run code when the illuminance changes by the given threshold value.
|
2022-04-08 18:39:19 +03:00
|
|
|
*/
|
2021-12-09 02:55:03 +03:00
|
|
|
//% group="Environment"
|
|
|
|
//% blockId=jacdac_illuminance_on_illuminance_change
|
2022-05-18 22:29:10 +03:00
|
|
|
//% block="on %illuminance illuminance changed by %threshold (lux)"
|
2021-03-11 17:13:58 +03:00
|
|
|
//% weight=98
|
2021-09-20 18:49:38 +03:00
|
|
|
//% threshold.min=0
|
|
|
|
//% threshold.max=100000
|
2021-03-11 17:07:38 +03:00
|
|
|
//% threshold.defl=1
|
2021-12-09 02:55:03 +03:00
|
|
|
onIlluminanceChangedBy(threshold: number, handler: () => void): void {
|
2022-04-08 18:39:19 +03:00
|
|
|
this.onReadingChangedBy(threshold, handler)
|
2021-03-11 17:07:38 +03:00
|
|
|
}
|
2021-02-11 13:11:35 +03:00
|
|
|
}
|
2022-06-16 00:56:29 +03:00
|
|
|
|
2022-04-08 18:39:19 +03:00
|
|
|
//% fixedInstance whenUsed weight=1 block="illuminance1"
|
|
|
|
export const illuminance1 = new IlluminanceClient("illuminance1")
|
|
|
|
}
|