diff --git a/README.md b/README.md index b4b4649..f700709 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Python APDS-9960 Library +# Python (and MicroPython) APDS-9960 Library Python library for the APDS-9960 gesture sensor developed while I was looking to get the APDS-9960 to work with a _Raspberry Pi_ to build a user interface feeling like in _Minority Report_. @@ -17,7 +17,6 @@ Features: Documentation: - [RPi](RPi.md) - connect and configure the APDS-9960 on Raspberry Pi - Example scripts: - - [test-ambient.py](test-ambient.py) - simple ambient light level demo - - [test-gesture.py](test-gesture.py) - simple gesture detection demo - - [test-prox.py](test-prox.py) - simple proximity level demo - \ No newline at end of file + - simple ambient light level demo: [rpi](rpi/test_ambient.py), [micropython](micropython/test_ambient.py) + - simple gesture detection demo: [rpi](rpi/test_gesture.py), [micropython](micropython/test_gesture.py) + - simple proximity level demo: [rpi](rpi/test_prox.py), [micropython](micropython/test_prox.py) diff --git a/apds9960/__init__.py b/apds9960/__init__.py index 10dc3b5..e8c7549 100644 --- a/apds9960/__init__.py +++ b/apds9960/__init__.py @@ -1,3 +1,3 @@ -from apds9960.device import APDS9960 +from apds9960.device import APDS9960, uAPDS9960 -__all__ = [ 'APDS9960' ] +__all__ = [ 'APDS9960', 'uAPDS9960', ] diff --git a/apds9960/device.py b/apds9960/device.py index 592a311..6f6fdaa 100644 --- a/apds9960/device.py +++ b/apds9960/device.py @@ -1083,3 +1083,21 @@ def _read_i2c_block_data(self, cmd, num): # ******************************************************************************* # resets all the parameters in the gesture data member + + + +class uAPDS9960(APDS9960): + """ + APDS9960 for MicroPython + + sensor = uAPDS9960(bus=I2C_instance, + address=APDS9960_I2C_ADDR, valid_id=APDS9960_DEV_ID) + """ + def _read_byte_data(self, cmd): + return self.bus.readfrom_mem(self.address, cmd, 1)[0] + + def _write_byte_data(self, cmd, val): + self.bus.writeto_mem(self.address, cmd, bytes([val])) + + def _read_i2c_block_data(self, cmd, num): + return self.bus.readfrom_mem(self.address, cmd, num) diff --git a/micropython/test_ambient.py b/micropython/test_ambient.py new file mode 100644 index 0000000..6ffe001 --- /dev/null +++ b/micropython/test_ambient.py @@ -0,0 +1,23 @@ +from time import sleep + +from machine import Pin, I2C + +from apds9960.const import * +from apds9960 import uAPDS9960 as APDS9960 + +bus = I2C(sda=Pin(13), scl=Pin(14)) + +apds = APDS9960(bus) + +print("Light Sensor Test") +print("=================") +apds.enableLightSensor() + +oval = -1 +while True: + sleep(0.25) + val = apds.readAmbientLight() + if val != oval: + print("AmbientLight={}".format(val)) + oval = val + diff --git a/micropython/test_gesture.py b/micropython/test_gesture.py new file mode 100644 index 0000000..ac895b3 --- /dev/null +++ b/micropython/test_gesture.py @@ -0,0 +1,33 @@ +from time import sleep + +from machine import Pin, I2C + +from apds9960.const import * +from apds9960 import uAPDS9960 as APDS9960 + +bus = I2C(sda=Pin(13), scl=Pin(14)) + +apds = APDS9960(bus) + +dirs = { + APDS9960_DIR_NONE: "none", + APDS9960_DIR_LEFT: "left", + APDS9960_DIR_RIGHT: "right", + APDS9960_DIR_UP: "up", + APDS9960_DIR_DOWN: "down", + APDS9960_DIR_NEAR: "near", + APDS9960_DIR_FAR: "far", +} + +apds.setProximityIntLowThreshold(50) + +print("Gesture Test") +print("============") +apds.enableGestureSensor() + +while True: + sleep(0.5) + if apds.isGestureAvailable(): + motion = apds.readGesture() + print("Gesture={}".format(dirs.get(motion, "unknown"))) + diff --git a/micropython/test_prox.py b/micropython/test_prox.py new file mode 100644 index 0000000..8a4e582 --- /dev/null +++ b/micropython/test_prox.py @@ -0,0 +1,25 @@ +from time import sleep + +from machine import Pin, I2C + +from apds9960.const import * +from apds9960 import uAPDS9960 as APDS9960 + +bus = I2C(sda=Pin(13), scl=Pin(14)) + +apds = APDS9960(bus) + +apds.setProximityIntLowThreshold(50) + +print("Proximity Sensor Test") +print("=====================") +apds.enableProximitySensor() + +oval = -1 +while True: + sleep(0.25) + val = apds.readProximity() + if val != oval: + print("proximity={}".format(val)) + oval = val + diff --git a/test-ambient.py b/rpi/test_ambient.py similarity index 100% rename from test-ambient.py rename to rpi/test_ambient.py diff --git a/test-gesture.py b/rpi/test_gesture.py similarity index 100% rename from test-gesture.py rename to rpi/test_gesture.py diff --git a/test-prox.py b/rpi/test_prox.py similarity index 100% rename from test-prox.py rename to rpi/test_prox.py