-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.ts
64 lines (56 loc) · 1.5 KB
/
main.ts
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
64
/**
* 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 数字光强传感器软件包
*/
//% weight=100 color=#000011 icon="\uf185"
namespace BH1750 {
/**
* 设置 BH1750 传感器的地址,默认是 35
* @param is I2C address, eg: 35
*/
//% blockId="BH1750_SET_ADDRESS" block="设置地址 %addr"
//% weight=100 blockGap=8
export function SetAddress(addr: BH1750_ADDRESS): void {
if (addr == BH1750_ADDRESS.A35)
Address = 35
else
Address = 92
}
/**
* 打开 BH1750,开始转换光强。
*/
//% blockId="BH1750_ON" block="打开传感器"
//% weight=90 blockGap=8
export function on(): void {
pins.i2cWriteNumber(Address, 0x10, NumberFormat.UInt8BE)
}
/**
* 关闭 BH1750,省电模式。
*/
//% blockId="BH1750_OFF" block="关闭传感器"
//% weight=90 blockGap=8
export function off(): void {
pins.i2cWriteNumber(Address, 0, NumberFormat.UInt8BE)
}
/**
* 获取环境光强度 (lx)
*/
//% blockId="BH1750_GET_INTENSITY" block="环境光强度 (lx)"
//% weight=80 blockGap=8
export function getIntensity(): number {
return Math.idiv(pins.i2cReadNumber(Address, NumberFormat.UInt16BE) * 5, 6)
}
on();
}