-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathBH1750.ts
More file actions
63 lines (56 loc) · 1.5 KB
/
Copy pathBH1750.ts
File metadata and controls
63 lines (56 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
* makecode BH1750 Digital Ambient Light Sensor Package.
* From microbit/micropython Chinese community.
* http://www.micropython.org.cn
*/
// BH1750's I2C address
enum BH1750_ADDRESS {
//% block="35"
A35,
//% block="92"
A92
}
let Address = 35
/**
* BH1750 Digital Ambient Light Sensor Package
*/
//% weight=100 color=#000011 icon="\uf185"
namespace BH1750 {
/**
* set BH1750 Digital Ambient Light Sensor I2C address, default is 35
* @param is I2C address, eg: 35
*/
//% blockId="BH1750_SET_ADDRESS" block="set Address %addr"
//% weight=100 blockGap=8
export function SetAddress(addr: BH1750_ADDRESS): void {
if (addr == BH1750_ADDRESS.A35)
Address = 35
else
Address = 92
}
/**
* turn on BH1750.
*/
//% blockId="BH1750_ON" block="turn on"
//% weight=90 blockGap=8
export function on(): void {
pins.i2cWriteNumber(Address, 0x10, NumberFormat.UInt8BE)
}
/**
* turn off BH1750, to reduce power consumption.
*/
//% blockId="BH1750_OFF" block="turn off"
//% weight=90 blockGap=8
export function off(): void {
pins.i2cWriteNumber(Address, 0, NumberFormat.UInt8BE)
}
/**
* get ambient light data (lx)
*/
//% blockId="BH1750_GET_INTENSITY" block="get intensity (lx)"
//% weight=80 blockGap=8
export function getIntensity(): number {
return Math.idiv(pins.i2cReadNumber(Address, NumberFormat.UInt16BE) * 5, 6)
}
on();
}