|
| 1 | +/** |
| 2 | +* ST LPS22HB/HD/HH pressure Sensor I2C extension for makecode. |
| 3 | +* From microbit/micropython Chinese community. |
| 4 | +* https://github.com/makecode-extensions |
| 5 | +*/ |
| 6 | + |
| 7 | +/** |
| 8 | + * ST LPS22HB/HD/HH pressure Sensor I2C extension |
| 9 | + */ |
| 10 | +//% weight=100 color=#60a0e0 icon="\uf2c8" block="LPS22" |
| 11 | +namespace LPS22 { |
| 12 | + export enum LPS22_I2C_ADDRESS { |
| 13 | + //% block="92" |
| 14 | + ADDR_92 = 92, |
| 15 | + //% block="93" |
| 16 | + ADDR_93 = 93 |
| 17 | + } |
| 18 | + |
| 19 | + export enum LPS22_P_UNIT { |
| 20 | + //% block="Pa" |
| 21 | + Pa = 0, |
| 22 | + //% block="hPa" |
| 23 | + hPa = 1 |
| 24 | + } |
| 25 | + |
| 26 | + export enum LPS22_T_UNIT { |
| 27 | + //% block="C" |
| 28 | + C = 0, |
| 29 | + //% block="F" |
| 30 | + F = 1 |
| 31 | + } |
| 32 | + |
| 33 | + const LPS22_CTRL_REG1 = (0x10) |
| 34 | + const LPS22_CTRL_REG2 = (0x11) |
| 35 | + const LPS22_STATUS = (0x27) |
| 36 | + const LPS22_TEMP_OUT_L = (0x2B) |
| 37 | + const LPS22_PRESS_OUT_XL = (0x28) |
| 38 | + const LPS22_PRESS_OUT_L = (0x29) |
| 39 | + |
| 40 | + let LPS22_I2C_ADDR = LPS22.LPS22_I2C_ADDRESS.ADDR_93 // defaut address |
| 41 | + let _oneshot = false |
| 42 | + |
| 43 | + init() // init sensor at startup |
| 44 | + |
| 45 | + // set dat to reg |
| 46 | + function setreg(reg: number, dat: number): void { |
| 47 | + let tb = pins.createBuffer(2) |
| 48 | + tb[0] = reg |
| 49 | + tb[1] = dat |
| 50 | + pins.i2cWriteBuffer(LPS22_I2C_ADDR, tb) |
| 51 | + } |
| 52 | + |
| 53 | + // read a Int8LE from reg |
| 54 | + function getInt8LE(reg: number): number { |
| 55 | + pins.i2cWriteNumber(LPS22_I2C_ADDR, reg, NumberFormat.UInt8BE); |
| 56 | + return pins.i2cReadNumber(LPS22_I2C_ADDR, NumberFormat.Int8LE); |
| 57 | + } |
| 58 | + |
| 59 | + // read a UInt8LE from reg |
| 60 | + function getUInt8LE(reg: number): number { |
| 61 | + pins.i2cWriteNumber(LPS22_I2C_ADDR, reg, NumberFormat.UInt8BE); |
| 62 | + return pins.i2cReadNumber(LPS22_I2C_ADDR, NumberFormat.UInt8LE); |
| 63 | + } |
| 64 | + |
| 65 | + // read a Int16LE from reg |
| 66 | + function getInt16LE(reg: number): number { |
| 67 | + pins.i2cWriteNumber(LPS22_I2C_ADDR, reg | 0x80, NumberFormat.UInt8BE); |
| 68 | + return pins.i2cReadNumber(LPS22_I2C_ADDR, NumberFormat.Int16LE); |
| 69 | + } |
| 70 | + |
| 71 | + // read a UInt16LE from reg |
| 72 | + function getUInt16LE(reg: number): number { |
| 73 | + pins.i2cWriteNumber(LPS22_I2C_ADDR, reg | 0x80, NumberFormat.UInt8BE); |
| 74 | + return pins.i2cReadNumber(LPS22_I2C_ADDR, NumberFormat.UInt16LE); |
| 75 | + } |
| 76 | + |
| 77 | + // set a mask dat to reg |
| 78 | + function setreg_mask(reg: number, dat: number, mask: number): void { |
| 79 | + setreg(reg, (getUInt8LE(reg) & mask) | dat) |
| 80 | + } |
| 81 | + |
| 82 | + // limit decimal digits |
| 83 | + function bitround(x: number, b: number = 1): number { |
| 84 | + let n = 10 ** b |
| 85 | + return Math.round(x * n) / n |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Init sensor |
| 90 | + */ |
| 91 | + //% block="Initialize, address %addr" |
| 92 | + //% addr.defl=LPS22.LPS22_I2C_ADDRESS.ADDR_93 |
| 93 | + export function init(addr: LPS22.LPS22_I2C_ADDRESS = LPS22.LPS22_I2C_ADDRESS.ADDR_93) { |
| 94 | + LPS22_I2C_ADDR = (addr == LPS22.LPS22_I2C_ADDRESS.ADDR_93) ? LPS22.LPS22_I2C_ADDRESS.ADDR_93 : LPS22.LPS22_I2C_ADDRESS.ADDR_92 |
| 95 | + |
| 96 | + // ODR = 1 EN_LPFP= 1 BDU= 1 |
| 97 | + setreg(LPS22_CTRL_REG1, 0x1A) |
| 98 | + oneshot_mode(false) |
| 99 | + } |
| 100 | + |
| 101 | + // oneshot mode handle |
| 102 | + function ONE_SHOT(b: number): void { |
| 103 | + if (_oneshot) { |
| 104 | + setreg(LPS22_CTRL_REG2, getUInt8LE(LPS22_CTRL_REG2) | 0x01) |
| 105 | + getUInt8LE(0x28 + b * 2) |
| 106 | + while (true) { |
| 107 | + if (getUInt8LE(LPS22_STATUS) & b) |
| 108 | + return |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * set oneshot mode to reduce power consumption |
| 115 | + */ |
| 116 | + //% block="oneshot mode %oneshot" |
| 117 | + export function oneshot_mode(oneshot: boolean = false) { |
| 118 | + _oneshot = oneshot |
| 119 | + let t = (oneshot) ? 0 : 0x10 |
| 120 | + setreg_mask(LPS22_CTRL_REG1, t, 0x0F) |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * get temperature |
| 125 | + */ |
| 126 | + //% block="temperature %u" |
| 127 | + export function temperature(u: LPS22.LPS22_T_UNIT = LPS22.LPS22_T_UNIT.C): number { |
| 128 | + ONE_SHOT(2) |
| 129 | + let T = getInt16LE(LPS22_TEMP_OUT_L) / 100 |
| 130 | + if (u == LPS22.LPS22_T_UNIT.F) T = 32 + T * 9 / 5 |
| 131 | + return bitround(T, 1) |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * get pressure from sensor |
| 136 | + */ |
| 137 | + //% block="pressure %u" |
| 138 | + //% u.defl=LPS22.LPS22_P_UNIT.hPa |
| 139 | + export function pressure(u: LPS22.LPS22_P_UNIT = LPS22.LPS22_P_UNIT.hPa): number { |
| 140 | + ONE_SHOT(1) |
| 141 | + let P = (getUInt16LE(LPS22_PRESS_OUT_L) * 256 + getUInt8LE(LPS22_PRESS_OUT_XL)) / 4096 |
| 142 | + if (u == LPS22.LPS22_P_UNIT.Pa) P = P * 100 |
| 143 | + return Math.round(P) |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * calaulate altitude use pressure and temperature |
| 148 | + */ |
| 149 | + //% block="altitude" |
| 150 | + export function altitude(): number { |
| 151 | + return (((1013.25 / pressure()) ** (1 / 5.257)) - 1.0) * (temperature() + 273.15) / 0.0065 |
| 152 | + } |
| 153 | +} |
0 commit comments