2021年3月11日星期四

Is this the correct way to write a MicroPython module that works across different builds?

I'm writing a module for an I2C temperature sensor (TMP117) with cross-compatability being a priority.

The MicroPython implementation is different on BBC Micro:bit and Raspberry Pi Pico - an important difference is how the I2C driver is implemented:

  • Pico uses the machine class to drive i2c: i2c.writeto
  • Micro:bit has already defined i2c.write (bundled with from microbit import *) docs

This simple difference in naming is really all that affects compatability!

So far I have shoehorned a working solution so my module tmp117.py is usable on both platforms. In tmp117.py, when the sensor class is initialised it checks the sysname and declares function pointers self.i2cWrite and self.i2cRead that are assigned the hardware-appropriate definition.

However, I'm quite new to Python and imagine I may have committed many atrocities in doing so by eg. mishandling namespaces or chewing up memory with inappropriate imports. Can anybody comment if this method is appropriate? In terms of scalability, needless imports, namespaces etc.

main.py

# TMP117 minimal example code  from tmp117 import *  from time import sleep    mySensor = tmp117()    while True:      # Read and print the temperature in various units      tempC = mySensor.readTempC() # Celsius            tempStringC = str(tempC) # convert temperature number to string          print("it's " + tempStringC)            sleep(1)  

tmp117.py

# A simple class to read temperature from the TMP117 i2c temperature sensor  # Currently only supports reading temperature in C, F, K. Does not support  # alarms.    # This module has been tested with the following development boards:  #    • BBC Micro:bit  #    • Raspberry Pi Pico (RP2040)    import os      if os.uname().sysname == 'microbit':      from microbit import *  else: # for Raspberry Pi Pico      from machine import I2C      i2c = I2C(0)        print("Running on " + os.uname().sysname)        # Register definitions  REG_TEMPC = b'\x00'    class tmp117(object):          def __init__(self, addr_=0x48, i2c_=i2c):          if os.uname().sysname == 'microbit':              self.i2c = i2c_              self.addr = addr_              self.i2cWrite = self.i2c.write              self.i2cRead = self.i2c.read          else:              self.i2c = i2c_              self.addr = addr_              self.i2cWrite = self.i2c.writeto              self.i2cRead = self.i2c.readfrom            def readTempC(self):          self.i2cWrite(self.addr, REG_TEMPC)          data = self.i2cRead(self.addr, 2) # returns a bytes object          tempDataRaw = int.from_bytes(data, 'big')          # handle negatives (MicroPython int.from_bytes does not support signed conversion (yet)          if tempDataRaw >= 0x8000:              return -256.0 + (tempDataRaw - 0x8000) * 7.8125e-3 # One LSB equals 7.812 mdegC          else:              return tempDataRaw * 7.8125e-3 # One LSB equals 7.812 mdegC  
https://stackoverflow.com/questions/66594433/is-this-the-correct-way-to-write-a-micropython-module-that-works-across-differen March 12, 2021 at 01:00PM

没有评论:

发表评论