forked from deshipu/circuitpython-paj7620
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaj7620.py
More file actions
111 lines (81 loc) · 2.7 KB
/
paj7620.py
File metadata and controls
111 lines (81 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
# SPDX-FileCopyrightText: Copyright (c) 2022 Radomir Dopieralski
#
# SPDX-License-Identifier: MIT
"""
`paj7620`
================================================================================
Driver for the PAJ7620 gesture sensor.
* Author(s): Radomir Dopieralski
Implementation Notes
--------------------
**Hardware:**
* `Waveshare Breakout <https://www.waveshare.com/wiki/PAJ7620U2_Gesture_Sensor>`_"
**Software and Dependencies:**
* Adafruit CircuitPython firmware for the supported boards:
https://circuitpython.org/downloads
* Adafruit's Bus Device library:
https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
"""
# imports
__version__ = "0.0.0+auto.0"
__repo__ = "https://github.com/deshipu/CircuitPython_paj7620.git"
from adafruit_bus_device.i2c_device import I2CDevice
_ADDR = (
b"\xefAB789BFGHIJLQ^`\x80\x81\x82\x8b\x90\x95\x96\x97\x9a\x9c"
b"\xa5\xcc\xcd\xce\xcf\xd0\xef\x02\x03\x04%'()>^egijmnrstw\xef"
b"AB"
)
_DATA = (
b"\x00\x00\x00\x07\x17\x06\x01-\x0f<\x00\x1e\"\x10\x10'BD\x04"
b"\x01\x06\n\x0c\x05\x14?\x19\x19\x0b\x13d!\x01\x0f\x10\x02\x01"
b"9\x7f\x08\xff=\x96\x97\xcd\x01,\x01\x015\x00\x01\x00\xff\x01"
)
UP = 0x01
DOWN = 0x02
LEFT = 0x04
RIGHT = 0x08
NEAR = 0x10
FAR = 0x20
CW = 0x40
CCW = 0x80
WAVE = 0x100
NONE = 0x00
class PAJ7620:
"""Gesture sensor."""
UP = UP
DOWN = DOWN
LEFT = LEFT
RIGHT = RIGHT
NEAR = NEAR
FAR = FAR
CW = CW
CCW = CCW
WAVE = WAVE
NONE = NONE
buf = bytearray(2)
def __init__(self, i2c, addr=0x73):
self.device = I2CDevice(i2c, addr)
with self.device as device:
for address, data in zip(_ADDR, _DATA):
self.buf[0] = address
self.buf[1] = data
device.write(self.buf)
def read(self):
"""Read and clear the gestures from the sensor."""
with self.device as device:
device.write_then_readinto(b"\x43", self.buf)
return int.from_bytes(self.buf, "little")
def proximity_raw(self):
""" Read raw proximity value from register 0x6C (S_AvgY[8:1])."""
with self.device as device:
device.write_then_readinto(bytes([0x6C]), self.buf, in_end=1)
return self.buf[0]
def proximity(self):
""" Map the raw proximity from the reading 255-70 to 255-0."""
raw_value = self.proximity_raw()
# clamp values below 70 due to inconsistency
if raw_value < 70:
return 0
mapped_value = (255 * (raw_value - 70)) // 185
return min(mapped_value, 255) # Ensure output doesn't exceed 255