From f202fb491553477c3a490f6a5c3647c7bad93472 Mon Sep 17 00:00:00 2001 From: Thomas Waldmann Date: Sun, 4 Mar 2018 01:42:36 +0100 Subject: [PATCH 1/3] implement uAPDS9960 with port for MicroPython --- README.md | 4 ++-- apds9960/__init__.py | 4 ++-- apds9960/device.py | 18 ++++++++++++++++++ 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index b4b4649..82ddfcc 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_. @@ -20,4 +20,4 @@ Documentation: - [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 + 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) From dae27c758b3d40930e9da4c710e988308a7e1f42 Mon Sep 17 00:00:00 2001 From: Thomas Waldmann Date: Sun, 4 Mar 2018 01:49:30 +0100 Subject: [PATCH 2/3] add examples for micropython port --- README.md | 2 +- micropython/test_ambient.py | 23 +++++++++++++++++++++++ micropython/test_gesture.py | 33 +++++++++++++++++++++++++++++++++ micropython/test_prox.py | 25 +++++++++++++++++++++++++ 4 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 micropython/test_ambient.py create mode 100644 micropython/test_gesture.py create mode 100644 micropython/test_prox.py diff --git a/README.md b/README.md index 82ddfcc..137c366 100644 --- a/README.md +++ b/README.md @@ -20,4 +20,4 @@ Documentation: - [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 - + - micropython/* - same as above, but for MicroPython, using uAPDS9960, Pin, I2C 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 + From e78fcdf12715e753767c00bff270a062a600dc41 Mon Sep 17 00:00:00 2001 From: Thomas Waldmann Date: Sun, 4 Mar 2018 01:56:42 +0100 Subject: [PATCH 3/3] move rpi examples to rpi/*, adapt README --- README.md | 7 +++---- test-ambient.py => rpi/test_ambient.py | 0 test-gesture.py => rpi/test_gesture.py | 0 test-prox.py => rpi/test_prox.py | 0 4 files changed, 3 insertions(+), 4 deletions(-) rename test-ambient.py => rpi/test_ambient.py (100%) rename test-gesture.py => rpi/test_gesture.py (100%) rename test-prox.py => rpi/test_prox.py (100%) diff --git a/README.md b/README.md index 137c366..f700709 100644 --- a/README.md +++ b/README.md @@ -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 - - micropython/* - same as above, but for MicroPython, using uAPDS9960, Pin, I2C + - 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/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