PiicoDev Requests/Suggestions

Yeah and if you want a different address you order a different part number. Strange way of doing it, leads to selling more parts I guess. Some other I2C devices allow for reprogramming of the address, much more practical. (see extract from Honeywell data sheet)

Some Python code ( has not been tested !!! ) using the PiicoDev Unified Library and along the same lines of the driver code for other PiicoDev sensors. Based on the Arduino driver code and Honeywell data sheet. Ignored the EOC and Reset pins, these need extra GPIO connections.

Maybe I am completely wrong, but this is a new area for me, so fun, will test code when I get part.
Cheers
Jim

"""
Honeywell Micro Pressure Sensor - MPR
Device in Standby Mode normally, send command, device enters Operating Mode, sensors pressure, when done flags EOC pin and Status byte.

Send 0xAA 0x00 0x00 to start reading.
Wait for status or EOC pin or at least 5ms.
Read 4 bytes, status, data 24:16, data 15:8, data 7:0

"""
from PiicoDev_Unified import *

compat_str = '\nUnified PiicoDev library out of date.  Get the latest module: https://piico.dev/unified \n'

_MPR = 0x18
MAXIMUM_PSI = 25
MINIMUM_PSI = 0

BUSY_FLAG = 0x20
INTEGRITY_FLAG = 0x04
MATH_SAT_FLAG = 0x01

OUTPUT_MAX 0xE66666
OUTPUT_MIN 0x19999A

class PiicoDev_MPR(object):     
    def __init__(self, bus=None, freq=None, sda=None, scl=None, addr=_MPR):
        try:
            if compat_ind >= 1:
                pass
            else:
                print(compat_str)
        except:
            print(compat_str)
        self.i2c = create_unified_i2c(bus=bus, freq=freq, sda=sda, scl=scl)
        self.addr = addr

    def read(self):
        self.i2c.writeto_mem(self.addr, 0, bytes([0xAA,0x00,0x00]))
        sleep_ms(10)
       
        status = self.i2c.readfrom_mem(self.addr, 0, 1)
        while((status & BUSY_FLAG) && (status != 0xFF))
            sleep_ms(1)
            status = self.i2c.readfrom_mem(self.addr, 0, 1)

        data = self.i2c.readfrom_mem(self.addr, 0, 4)

        status = data[0]
        if((status & INTEGRITY_FLAG) || (status & MATH_SAT_FLAG))
            return NAN;
        reading = data[1]<<8 & data[2]<<8 & data[3]
        pressure = (reading - OUTPUT_MIN) * (MAXIMUM_PSI - MINIMUM_PSI)
        pressure = (pressure / (OUTPUT_MAX - OUTPUT_MIN)) + MINIMUM_PSI

        return pressure
6 Likes