forked from makecode-extensions/BME280
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
160 lines (145 loc) · 4.86 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/**
* makecode BME280 digital pressure and humidity sensor Package.
* From microbit/micropython Chinese community.
* http://www.micropython.org.cn
*/
enum BME280_I2C_ADDRESS {
//% block="0x76"
ADDR_0x76 = 0x76,
//% block="0x77"
ADDR_0x77 = 0x77
}
/**
* BME280 block
*/
//% weight=100 color=#70c0f0 icon="\uf042" block="BME280"
namespace BME280 {
let BME280_I2C_ADDR = BME280_I2C_ADDRESS.ADDR_0x76
function setreg(reg: number, dat: number): void {
let buf = pins.createBuffer(2);
buf[0] = reg;
buf[1] = dat;
pins.i2cWriteBuffer(BME280_I2C_ADDR, buf);
}
function getreg(reg: number): number {
pins.i2cWriteNumber(BME280_I2C_ADDR, reg, NumberFormat.UInt8BE);
return pins.i2cReadNumber(BME280_I2C_ADDR, NumberFormat.UInt8BE);
}
function getInt8LE(reg: number): number {
pins.i2cWriteNumber(BME280_I2C_ADDR, reg, NumberFormat.UInt8BE);
return pins.i2cReadNumber(BME280_I2C_ADDR, NumberFormat.Int8LE);
}
function getUInt16LE(reg: number): number {
pins.i2cWriteNumber(BME280_I2C_ADDR, reg, NumberFormat.UInt8BE);
return pins.i2cReadNumber(BME280_I2C_ADDR, NumberFormat.UInt16LE);
}
function getInt16LE(reg: number): number {
pins.i2cWriteNumber(BME280_I2C_ADDR, reg, NumberFormat.UInt8BE);
return pins.i2cReadNumber(BME280_I2C_ADDR, NumberFormat.Int16LE);
}
let dig_T1 = getUInt16LE(0x88)
let dig_T2 = getInt16LE(0x8A)
let dig_T3 = getInt16LE(0x8C)
let dig_P1 = getUInt16LE(0x8E)
let dig_P2 = getInt16LE(0x90)
let dig_P3 = getInt16LE(0x92)
let dig_P4 = getInt16LE(0x94)
let dig_P5 = getInt16LE(0x96)
let dig_P6 = getInt16LE(0x98)
let dig_P7 = getInt16LE(0x9A)
let dig_P8 = getInt16LE(0x9C)
let dig_P9 = getInt16LE(0x9E)
let dig_H1 = getreg(0xA1)
let dig_H2 = getInt16LE(0xE1)
let dig_H3 = getreg(0xE3)
let a = getreg(0xE5)
let dig_H4 = (getreg(0xE4) << 4) + (a % 16)
let dig_H5 = (getreg(0xE6) << 4) + (a >> 4)
let dig_H6 = getInt8LE(0xE7)
setreg(0xF2, 0x04)
setreg(0xF4, 0x2F)
setreg(0xF5, 0x0C)
let T = 0
let P = 0
let H = 0
function get(): void {
let adc_T = (getreg(0xFA) << 12) + (getreg(0xFB) << 4) + (getreg(0xFC) >> 4)
let var1 = (((adc_T >> 3) - (dig_T1 << 1)) * dig_T2) >> 11
let var2 = (((((adc_T >> 4) - dig_T1) * ((adc_T >> 4) - dig_T1)) >> 12) * dig_T3) >> 14
let t = var1 + var2
T = ((t * 5 + 128) >> 8) / 100
var1 = (t >> 1) - 64000
var2 = (((var1 >> 2) * (var1 >> 2)) >> 11) * dig_P6
var2 = var2 + ((var1 * dig_P5) << 1)
var2 = (var2 >> 2) + (dig_P4 << 16)
var1 = (((dig_P3 * ((var1 >> 2) * (var1 >> 2)) >> 13) >> 3) + (((dig_P2) * var1) >> 1)) >> 18
var1 = ((32768 + var1) * dig_P1) >> 15
if (var1 == 0)
return; // avoid exception caused by division by zero
let adc_P = (getreg(0xF7) << 12) + (getreg(0xF8) << 4) + (getreg(0xF9) >> 4)
let _p = ((1048576 - adc_P) - (var2 >> 12)) * 3125
_p = (_p / var1) * 2;
var1 = (dig_P9 * (((_p >> 3) * (_p >> 3)) >> 13)) >> 12
var2 = (((_p >> 2)) * dig_P8) >> 13
P = _p + ((var1 + var2 + dig_P7) >> 4)
let adc_H = (getreg(0xFD) << 8) + getreg(0xFE)
var1 = t - 76800
var2 = (((adc_H << 14) - (dig_H4 << 20) - (dig_H5 * var1)) + 16384) >> 15
var1 = var2 * (((((((var1 * dig_H6) >> 10) * (((var1 * dig_H3) >> 11) + 32768)) >> 10) + 2097152) * dig_H2 + 8192) >> 14)
var2 = var1 - (((((var1 >> 15) * (var1 >> 15)) >> 7) * dig_H1) >> 4)
if (var2 < 0) var2 = 0
if (var2 > 419430400) var2 = 419430400
H = (var2 >> 12) / 1024
}
/**
* get pressure
*/
//% blockId="BME280_GET_PRESSURE" block="get pressure"
//% weight=80 blockGap=8
export function pressure(): number {
get();
return P;
}
/**
* get temperature
*/
//% blockId="BME280_GET_TEMPERATURE" block="get temperature"
//% weight=80 blockGap=8
export function temperature(): number {
get();
return T;
}
/**
* get humidity
*/
//% blockId="BME280_GET_HUMIDITY" block="get humidity"
//% weight=80 blockGap=8
export function hunidity(): number {
get();
return H;
}
/**
* power on
*/
//% blockId="BME280_POWER_ON" block="Power On"
//% weight=61 blockGap=8
export function PowerOn() {
setreg(0xF4, 0x2F)
}
/**
* power off
*/
//% blockId="BME280_POWER_OFF" block="Power Off"
//% weight=60 blockGap=8
export function PowerOff() {
setreg(0xF4, 0)
}
/**
* set I2C address
*/
//% blockId="BME280_SET_ADDRESS" block="set address %addr"
//% weight=50 blockGap=8
export function Address(addr: BME280_I2C_ADDRESS) {
BME280_I2C_ADDR = addr
}
}