You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_prox.py 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. from time import sleep
  2. from machine import Pin, I2C
  3. class test:
  4. def __init__(self,bus):
  5. self.bus = bus
  6. self.address = 57
  7. self.dev_id = self._read_byte_data(146)
  8. self.setMode(7, False)
  9. self.setMode(0,True)
  10. self.setMode(2, True)
  11. def getMode(self):
  12. return self._read_byte_data(128)
  13. def setMode(self, mode, enable=True):
  14. # read ENABLE register
  15. reg_val = self.getMode()
  16. # change bit(s) in ENABLE register */
  17. if mode == 7:
  18. if enable:
  19. reg_val = 0x7f
  20. else:
  21. reg_val = 0x00
  22. else:
  23. if enable:
  24. reg_val |= (1 << mode)
  25. else:
  26. reg_val &= ~(1 << mode)
  27. # write value to ENABLE register
  28. self._write_byte_data(128, reg_val)
  29. def setLEDDrive(self, drive):
  30. """Sets LED drive strength for proximity and ALS.
  31. Value LED Current
  32. 0 100 mA
  33. 1 50 mA
  34. 2 25 mA
  35. 3 12.5 mA
  36. Args:
  37. drive (int): value for the LED drive strength
  38. """
  39. val = self._read_byte_data(0x8f)
  40. # set bits in register to given value
  41. drive &= 0b00000011
  42. drive = drive << 6
  43. val &= 0b00111111
  44. val |= drive
  45. self._write_byte_data(0x8f, val)
  46. def setProximityGain(self, drive):
  47. """Returns receiver gain for proximity detection.
  48. Value Gain
  49. 0 1x
  50. 1 2x
  51. 2 4x
  52. 3 8x
  53. Args:
  54. drive (int): value for the proximity gain
  55. """
  56. val = self._read_byte_data(0x8f)
  57. # set bits in register to given value
  58. drive &= 0b00000011
  59. drive = drive << 2
  60. val &= 0b11110011
  61. val |= drive
  62. self._write_byte_data(0x8f, val)
  63. def readProximity(self):
  64. return self._read_byte_data(156)
  65. def _read_byte_data(self, cmd):
  66. return self.bus.readfrom_mem(self.address, cmd, 1)[0]
  67. def _write_byte_data(self, cmd, val):
  68. self.bus.writeto_mem(self.address, cmd, bytes([val]))
  69. def _read_i2c_block_data(self, cmd, num):
  70. return self.bus.readfrom_mem(self.address, cmd, num)
  71. bus = I2C(sda=Pin(4), scl=Pin(5),freq=400000)
  72. #apds = APDS9960(bus)
  73. apds = test(bus)
  74. #apds.setProximityIntLowThreshold(50)
  75. print("Proximity Sensor Test")
  76. print("=====================")
  77. #apds.enableProximitySensor()
  78. apds.setLEDDrive(0)
  79. apds.setProximityGain(1)
  80. oval = -1
  81. while True:
  82. sleep(0.25)
  83. val = apds.readProximity()
  84. if val != oval:
  85. print("proximity={}".format(val))
  86. oval = val