test_i2c_dummy.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. from unittest import TestCase
  2. from unittest.mock import patch
  3. import binascii
  4. import bme280
  5. import machine
  6. import asyncio as uasyncio
  7. # smbus.SMBus
  8. class SMBusMock:
  9. def __init__(self, bus):
  10. assert bus == 42
  11. def close(self):
  12. pass
  13. def write_i2c_block_data(self, addr, reg, data):
  14. pass
  15. def read_i2c_block_data(self, addr, reg, length):
  16. if reg == 0xD0 and length == 1: # id
  17. return [ 0x60, ]
  18. if reg == 0x88 and length == 26: # cal burst 1
  19. return list(binascii.unhexlify("04719f673200198a4dd6d00bc419fafff9ff0c3020d18813004b"))
  20. if reg == 0xE1 and length == 7: # cal burst 2
  21. return list(binascii.unhexlify("5a01001626031e"))
  22. if reg == 0xF7 and length == 8: # value burst
  23. return list(binascii.unhexlify("5e962085efc07bd2"))
  24. return [ 0, ] * length
  25. # machine.I2C
  26. class I2CMock(SMBusMock):
  27. def __init__(self, index=None, scl=None, sda=None, freq=None):
  28. if index is not None:
  29. assert index == 42
  30. assert freq is not None
  31. if isinstance(scl, PinMock):
  32. assert scl._pin == 11
  33. if isinstance(scl, int):
  34. assert scl == 11
  35. if isinstance(sda, PinMock):
  36. assert sda._pin == 12
  37. if isinstance(sda, int):
  38. assert sda == 12
  39. def deinit(self):
  40. pass
  41. def writeto_mem(self, addr, reg, data):
  42. self.write_i2c_block_data(addr, reg, data)
  43. def readfrom_mem(self, addr, reg, length):
  44. return bytes(self.read_i2c_block_data(addr, reg, length))
  45. # machine.SoftI2C
  46. class SoftI2CMock(I2CMock):
  47. def __init__(self, **kwargs):
  48. assert "index" not in kwargs
  49. I2CMock.__init__(self, **kwargs)
  50. # machine.Pin
  51. class PinMock:
  52. OPEN_DRAIN = object()
  53. def __init__(self, pin, mode, value):
  54. assert mode == self.OPEN_DRAIN
  55. assert value == 1
  56. self._pin = pin
  57. class Test_I2CDummy(TestCase):
  58. @patch("bme280.bme280.isMicropython", False)
  59. @patch("smbus.SMBus", SMBusMock)
  60. def test_linux(self):
  61. with bme280.BME280(i2cBus=42) as bme:
  62. t, h, p = bme.readForced(filter=bme280.FILTER_4,
  63. tempOversampling=bme280.OVSMPL_4,
  64. humidityOversampling=bme280.OVSMPL_16,
  65. pressureOversampling=bme280.OVSMPL_4)
  66. self.assertTrue(t > 0 and h > 0 and p > 0)
  67. with bme280.BME280(i2cBus=42, calc=bme280.CALC_INT32) as bme:
  68. t, h, p = bme.readForced()
  69. self.assertAlmostEqual(t, 27.099998, places=4)
  70. self.assertAlmostEqual(h, 0.451729, places=4)
  71. self.assertAlmostEqual(p, 98484.001160, places=1)
  72. with bme280.BME280(i2cBus=42, calc=bme280.CALC_INT64) as bme:
  73. t, h, p = bme.readForced()
  74. self.assertAlmostEqual(t, 27.099998, places=4)
  75. self.assertAlmostEqual(h, 0.451729, places=4)
  76. self.assertAlmostEqual(p / 100, 984.84001160, places=1)
  77. with bme280.BME280(i2cBus=42, calc=bme280.CALC_FLOAT) as bme:
  78. t, h, p = bme.readForced()
  79. self.assertAlmostEqual(t, 27.099998, places=1)
  80. self.assertAlmostEqual(h, 0.451729, places=2)
  81. self.assertAlmostEqual(p / 100, 984.84001160, places=1)
  82. @patch("bme280.bme280.isMicropython", True)
  83. @patch("machine.I2C", I2CMock, create=True)
  84. @patch("machine.SoftI2C", SoftI2CMock, create=True)
  85. @patch("machine.Pin", PinMock, create=True)
  86. def test_micropython(self):
  87. with bme280.BME280(i2cBus=42) as bme:
  88. t, h, p = bme.readForced()
  89. self.assertTrue(t > 0 and h > 0 and p > 0)
  90. with bme280.BME280(i2cBus={ "index": 42, "scl": 11, "sda": 12 }) as bme:
  91. t, h, p = bme.readForced()
  92. self.assertTrue(t > 0 and h > 0 and p > 0)
  93. with bme280.BME280(i2cBus={ "scl": 11, "sda": 12 }) as bme:
  94. t, h, p = bme.readForced()
  95. self.assertTrue(t > 0 and h > 0 and p > 0)
  96. with bme280.BME280(i2cBus=machine.I2C(42, scl=11, sda=12, freq=100000)) as bme:
  97. t, h, p = bme.readForced()
  98. self.assertTrue(t > 0 and h > 0 and p > 0)
  99. with bme280.BME280(i2cBus=machine.I2C(42,
  100. scl=machine.Pin(11, mode=machine.Pin.OPEN_DRAIN, value=1),
  101. sda=machine.Pin(12, mode=machine.Pin.OPEN_DRAIN, value=1),
  102. freq=100000)) as bme:
  103. t, h, p = bme.readForced()
  104. self.assertTrue(t > 0 and h > 0 and p > 0)
  105. # Test async
  106. async def coroutine_():
  107. async with bme280.BME280(i2cBus=42) as bme:
  108. t, h, p = await bme.readForcedAsync()
  109. self.assertTrue(t > 0 and h > 0 and p > 0)
  110. uasyncio.run(coroutine_())
  111. # Normal mode
  112. with bme280.BME280(i2cBus=42) as bme:
  113. bme.start(mode=bme280.MODE_NORMAL,
  114. standbyTime=bme280.T_SB_10ms)
  115. # Other methods
  116. with bme280.BME280(i2cBus=42) as bme:
  117. bme.readForced()
  118. self.assertFalse(bme.isMeasuring())
  119. bme.reset()
  120. # vim: ts=4 sw=4 expandtab