test_spi_dummy.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. # spidev.SpiDev
  8. class SpiDevMock:
  9. def open(self, bus, cs):
  10. assert bus == 42 and cs == 2
  11. def close(self):
  12. pass
  13. def xfer2(self, data):
  14. length = len(data)
  15. assert length >= 1
  16. reg = data[0]
  17. if reg == 0xD0 and length == 1 + 1: # id
  18. return b'\0' + bytes([ 0x60, ])
  19. if reg == 0x88 and length == 26 + 1: # cal burst 1
  20. return b'\0' + binascii.unhexlify("04719f673200198a4dd6d00bc419fafff9ff0c3020d18813004b")
  21. if reg == 0xE1 and length == 7 + 1: # cal burst 2
  22. return b'\0' + binascii.unhexlify("5a01001626031e")
  23. if reg == 0xF7 and length == 8 + 1: # value burst
  24. return b'\0' + binascii.unhexlify("5e962085efc07bd2")
  25. return bytes([ 0, ] * length)
  26. # machine.SPI
  27. class SPIMock(SpiDevMock):
  28. MSB = object()
  29. def __init__(self, index=None, baudrate=None, *,
  30. polarity=None, phase=None, bits=None, firstbit=None,
  31. sck=None, mosi=None, miso=None):
  32. if index is not None:
  33. assert index == 42
  34. if polarity is not None:
  35. assert polarity == 0
  36. if phase is not None:
  37. assert phase == 0
  38. if bits is not None:
  39. assert bits == 8
  40. if firstbit is not None:
  41. assert firstbit == self.MSB
  42. if isinstance(sck, PinMock):
  43. assert sck._pin == 21
  44. assert sck._mode == PinMock.OUT
  45. assert sck._value == 0
  46. if isinstance(sck, int):
  47. assert sck == 21
  48. if isinstance(mosi, PinMock):
  49. assert mosi._pin == 22
  50. assert mosi._mode == PinMock.OUT
  51. assert mosi._value == 0
  52. if isinstance(mosi, int):
  53. assert mosi == 22
  54. if isinstance(miso, PinMock):
  55. assert miso._pin == 23
  56. assert miso._mode == PinMock.IN
  57. assert miso._value is None
  58. if isinstance(miso, int):
  59. assert miso == 23
  60. def deinit(self):
  61. pass
  62. def write(self, data):
  63. self.xfer2(data)
  64. def read(self, nbytes, write=0):
  65. return self.xfer2(bytes([write, ] * nbytes))
  66. # machine.SoftSPI
  67. class SoftSPIMock(SPIMock):
  68. def __init__(self, **kwargs):
  69. assert "index" not in kwargs
  70. SPIMock.__init__(self, **kwargs)
  71. # machine.Pin
  72. class PinMock:
  73. OUT = object()
  74. IN = object()
  75. def __init__(self, pin, mode, value=None):
  76. self._value = value
  77. self._mode = mode
  78. self._pin = pin
  79. def __call__(self, value=None):
  80. assert value is not None
  81. self._value = value
  82. class Test_SPIDummy(TestCase):
  83. @patch("bme280.bme280.isMicropython", False)
  84. @patch("spidev.SpiDev", SpiDevMock)
  85. def test_linux(self):
  86. with bme280.BME280(spiBus=42, spiCS=2) as bme:
  87. t, h, p = bme.readForced(filter=bme280.FILTER_4,
  88. tempOversampling=bme280.OVSMPL_4,
  89. humidityOversampling=bme280.OVSMPL_16,
  90. pressureOversampling=bme280.OVSMPL_4)
  91. self.assertTrue(t > 0 and h > 0 and p > 0)
  92. with bme280.BME280(spiBus=42, spiCS=2, calc=bme280.CALC_INT32) as bme:
  93. t, h, p = bme.readForced()
  94. self.assertAlmostEqual(t, 27.099998, places=4)
  95. self.assertAlmostEqual(h, 0.451729, places=4)
  96. self.assertAlmostEqual(p, 98484.001160, places=1)
  97. with bme280.BME280(spiBus=42, spiCS=2, calc=bme280.CALC_INT64) as bme:
  98. t, h, p = bme.readForced()
  99. self.assertAlmostEqual(t, 27.099998, places=4)
  100. self.assertAlmostEqual(h, 0.451729, places=4)
  101. self.assertAlmostEqual(p / 100, 984.84001160, places=1)
  102. with bme280.BME280(spiBus=42, spiCS=2, calc=bme280.CALC_FLOAT) as bme:
  103. t, h, p = bme.readForced()
  104. self.assertAlmostEqual(t, 27.099998, places=1)
  105. self.assertAlmostEqual(h, 0.451729, places=2)
  106. self.assertAlmostEqual(p / 100, 984.84001160, places=1)
  107. @patch("bme280.bme280.isMicropython", True)
  108. @patch("machine.SPI", SPIMock, create=True)
  109. @patch("machine.SoftSPI", SoftSPIMock, create=True)
  110. @patch("machine.Pin", PinMock, create=True)
  111. def test_micropython(self):
  112. with bme280.BME280(spiBus=42, spiCS=2) as bme:
  113. t, h, p = bme.readForced()
  114. self.assertTrue(t > 0 and h > 0 and p > 0)
  115. with bme280.BME280(spiBus={ "index": 42, "sck": 21, "mosi": 22, "miso": 23 }, spiCS=2) as bme:
  116. t, h, p = bme.readForced()
  117. self.assertTrue(t > 0 and h > 0 and p > 0)
  118. with bme280.BME280(spiBus={ "sck": 21, "mosi": 22, "miso": 23 }, spiCS=2) as bme:
  119. t, h, p = bme.readForced()
  120. self.assertTrue(t > 0 and h > 0 and p > 0)
  121. with bme280.BME280(spiBus=machine.SPI(42, sck=21, mosi=22, miso=23), spiCS=2) as bme:
  122. t, h, p = bme.readForced()
  123. self.assertTrue(t > 0 and h > 0 and p > 0)
  124. with bme280.BME280(spiBus=machine.SPI(42,
  125. sck=machine.Pin(21, mode=machine.Pin.OUT, value=0),
  126. mosi=machine.Pin(22, mode=machine.Pin.OUT, value=0),
  127. miso=machine.Pin(23, mode=machine.Pin.IN)),
  128. spiCS=machine.Pin(2, mode=machine.Pin.OUT, value=1)) as bme:
  129. t, h, p = bme.readForced()
  130. self.assertTrue(t > 0 and h > 0 and p > 0)
  131. # Test async
  132. async def coroutine_():
  133. async with bme280.BME280(spiBus=42, spiCS=2) as bme:
  134. t, h, p = await bme.readForcedAsync()
  135. self.assertTrue(t > 0 and h > 0 and p > 0)
  136. uasyncio.run(coroutine_())
  137. # Normal mode
  138. with bme280.BME280(spiBus=42, spiCS=2) as bme:
  139. bme.start(mode=bme280.MODE_NORMAL,
  140. standbyTime=bme280.T_SB_10ms)
  141. # Other methods
  142. with bme280.BME280(spiBus=42, spiCS=2) as bme:
  143. bme.readForced()
  144. self.assertFalse(bme.isMeasuring())
  145. bme.reset()
  146. # vim: ts=4 sw=4 expandtab