From caad98d8d118e016516a18cec7b37448977f42ba Mon Sep 17 00:00:00 2001 From: Brendan <2bndy5@gmail.com> Date: Wed, 8 Apr 2020 03:49:03 -0700 Subject: [PATCH] moved clamping from report() to simpletest.py --- circuitpython_cirque_pinnacle/glidepoint.py | 2 - .../glidepoint_lite.py | 2 - examples/cirque_pinnacle_anymeas_test.py | 26 +++++++---- examples/cirque_pinnacle_simpletest.py | 45 +++++++++++++++++++ examples/cirque_pinnacle_spi_simpletest.py | 29 ------------ examples/cirque_pinnacle_usb_mouse.py | 31 ++++++++----- 6 files changed, 82 insertions(+), 53 deletions(-) create mode 100644 examples/cirque_pinnacle_simpletest.py delete mode 100644 examples/cirque_pinnacle_spi_simpletest.py diff --git a/circuitpython_cirque_pinnacle/glidepoint.py b/circuitpython_cirque_pinnacle/glidepoint.py index 6ab31d8..902b1ba 100644 --- a/circuitpython_cirque_pinnacle/glidepoint.py +++ b/circuitpython_cirque_pinnacle/glidepoint.py @@ -146,8 +146,6 @@ def report(self, only_new=True): ((temp[4] & 0x0F) << 8) | temp[2], # x ((temp[4] & 0xF0) << 4) | temp[3], # y temp[5] & 0x3F] # z - return_vals[1] = max(128, min(1920, return_vals[1])) - return_vals[2] = max(64, min(1472, return_vals[2])) elif self.data_mode == RELATIVE: # if in relative mode temp = self._rap_read_bytes(0x12, 4) return_vals = bytearray([temp[0] & 7, temp[1], temp[2]]) diff --git a/circuitpython_cirque_pinnacle/glidepoint_lite.py b/circuitpython_cirque_pinnacle/glidepoint_lite.py index d023044..1f89c66 100644 --- a/circuitpython_cirque_pinnacle/glidepoint_lite.py +++ b/circuitpython_cirque_pinnacle/glidepoint_lite.py @@ -79,8 +79,6 @@ def report(self, only_new=True): ((temp[4] & 0x0F) << 8) | temp[2], ((temp[4] & 0xF0) << 4) | temp[3], temp[5] & 0x3F] - return_vals[1] = max(128, min(1920, return_vals[1])) - return_vals[2] = max(64, min(1472, return_vals[2])) elif self.data_mode == RELATIVE: temp = self._rap_read_bytes(0x12, 4) return_vals = bytearray([temp[0] & 7, temp[1], temp[2]]) diff --git a/examples/cirque_pinnacle_anymeas_test.py b/examples/cirque_pinnacle_anymeas_test.py index 2c56a33..2904c31 100644 --- a/examples/cirque_pinnacle_anymeas_test.py +++ b/examples/cirque_pinnacle_anymeas_test.py @@ -1,18 +1,28 @@ """ a test example using SPI to read ADC measurements from the Pinnacle touch -controller in "AnyMeas" mode""" +controller in "AnyMeas" mode. This example does NOT work with +glidepoint_lite.py""" import time -from struct import unpack +import struct import board from digitalio import DigitalInOut -# this example does NOT work with glideoint_lite.py -from circuitpython_cirque_pinnacle.glidepoint import PinnacleTouchSPI, ANYMEAS +# if using a trackpad configured for SPI +from circuitpython_cirque_pinnacle.glidepoint import PinnacleTouchSPI, ANYMEAS +# if using a trackpad configured for I2C +# from circuitpython_cirque_pinnacle.glidepoint import PinnacleTouchI2C, ANYMEAS +# i2c = board.I2C() +# if using a trackpad configured for SPI spi = board.SPI() ss_pin = DigitalInOut(board.D7) + dr_pin = DigitalInOut(board.D2) # NOTE The dr_pin is a required arg to use AnyMeas mode +# if using a trackpad configured for SPI tpad = PinnacleTouchSPI(spi, ss_pin, dr_pin=dr_pin) +# if using a trackpad configured for I2C +# tpad = PinnacleTouchI2C(i2c, dr_pin=dr_pin) + # if dr_pin was not specified upon instantiation. # this command will raise an AttributeError exception tpad.data_mode = ANYMEAS @@ -36,15 +46,13 @@ def __init__(self, toggle, polarity): # This toggles Y0-Y7 negative and X0-X7 positive vectors.append(MeasVector(0x00FF00FF, 0x000000FF)) -# tell pylint this example script is able to print results without compensation -# pylint: disable=redefined-outer-name idle_vectors = [0] * len(vectors) def compensate(count=5): """take ``count`` measurements, then average them together """ - idle_vectors = [0] * len(vectors) for i, v in enumerate(vectors): + idle_vectors[i] = 0 for _ in range(count): # - idle_vectors[i] += unpack('h', tpad.measure_adc(v.toggle, v.polarity))[0] + idle_vectors[i] += struct.unpack('h', tpad.measure_adc(v.toggle, v.polarity))[0] idle_vectors[i] /= count print("compensation {}: {}".format(i, idle_vectors[i])) @@ -54,6 +62,6 @@ def take_measurements(timeout=10): start = time.monotonic() while time.monotonic() - start < timeout: for i, v in enumerate(vectors): - result = unpack('h', tpad.measure_adc(v.toggle, v.polarity))[0] + result = struct.unpack('h', tpad.measure_adc(v.toggle, v.polarity))[0] print("vector{}: {}".format(i, result - idle_vectors[i]), end='\t') print() diff --git a/examples/cirque_pinnacle_simpletest.py b/examples/cirque_pinnacle_simpletest.py new file mode 100644 index 0000000..4a34d0d --- /dev/null +++ b/examples/cirque_pinnacle_simpletest.py @@ -0,0 +1,45 @@ +"""A simple test example. this example also works with glidepoint_lite.py""" +import time +import struct +import board +from digitalio import DigitalInOut +# if using a trackpad configured for SPI +from circuitpython_cirque_pinnacle.glidepoint import PinnacleTouchSPI, ABSOLUTE, RELATIVE +# if using a trackpad configured for I2C +# from circuitpython_cirque_pinnacle.glidepoint import PinnacleTouchI2C, ABSOLUTE, RELATIVE +# i2c = board.I2C() + +spi = board.SPI() # if using a trackpad configured for SPI +ss_pin = DigitalInOut(board.D7) +dr_pin = DigitalInOut(board.D2) + +# if using a trackpad configured for SPI +tpad = PinnacleTouchSPI(spi, ss_pin) # NOTE we did not pass the dr_pin +# if using a trackpad configured for I2C +# tpad = PinnacleTouchI2C(i2c) # NOTE we did not pass the dr_pin + +tpad.data_mode = ABSOLUTE # ensure Absolute mode is enabled +tpad.absolute_mode_config(z_idle_count=1) # limit idle packet count to 1 + +def print_data(timeout=6): + """Print available data reports from the Pinnacle touch controller + until there's no touch for a period of ``timeout`` seconds.""" + print("using {} mode".format("Relative" if tpad.data_mode < ABSOLUTE else "Absolute")) + start = time.monotonic() + while time.monotonic() - start < timeout: + if dr_pin.value: # is there new data? + data = tpad.report(only_new=False) + # Because we did not specify the dr_pin when instantiating the tpad variable, + # only_new=False skips the extra SPI transaction to check the SW_DR flag in + # the STATUS register which is reflected on the dr_pin + + if tpad.data_mode == ABSOLUTE and data[3]: # only when Z-axis is > 0 + # spec sheet recommends clamping absolute position data of X & Y axis for + # reliability + data[1] = max(128, min(1920, data[1])) # X-axis + data[2] = max(64, min(1472, data[2])) # Y-axis + elif tpad.data_mode == RELATIVE: + # convert 2's compliment form into natural numbers + data = struct.unpack('Bbbb', data) + print(data) + start = time.monotonic() diff --git a/examples/cirque_pinnacle_spi_simpletest.py b/examples/cirque_pinnacle_spi_simpletest.py deleted file mode 100644 index 983180f..0000000 --- a/examples/cirque_pinnacle_spi_simpletest.py +++ /dev/null @@ -1,29 +0,0 @@ -""" a simple test example using SPI """ -import time -from struct import unpack -import board -from digitalio import DigitalInOut -# this example also works with glideoint_lite.py -from circuitpython_cirque_pinnacle.glidepoint import PinnacleTouchSPI, ABSOLUTE - -spi = board.SPI() -ss_pin = DigitalInOut(board.D7) -dr_pin = DigitalInOut(board.D2) -dr_pin.switch_to_input() - -tpad = PinnacleTouchSPI(spi, ss_pin) # NOTE we did not pass the dr_pin -tpad.data_mode = ABSOLUTE # ensure Absolute mode is enabled -tpad.absolute_mode_config(z_idle_count=1) # limit idle packet count to 1 - -def print_data(timeout=10): - """Print available data reports from the Pinnacle touch controller - for a period of ``timeout`` seconds.""" - print("using {} mode".format("Relative" if tpad.data_mode < 2 else "Absolute")) - start = time.monotonic() - while time.monotonic() - start < timeout: - if dr_pin.value: # is there new data? - data = tpad.report(only_new=False) - # Because we did not specify the dr_pin when instantiating the tpad variable, - # only_new=False skips the extra SPI transaction to check the SW_DR flag in - # the STATUS register which is reflected on the dr_pin - print(unpack('Bbbb', data) if tpad.data_mode < 2 else data) diff --git a/examples/cirque_pinnacle_usb_mouse.py b/examples/cirque_pinnacle_usb_mouse.py index 86ef54f..b31d0f5 100644 --- a/examples/cirque_pinnacle_usb_mouse.py +++ b/examples/cirque_pinnacle_usb_mouse.py @@ -1,11 +1,28 @@ """ This example uses CircuitPython's built-in `usb_hid` API -to emulate a mouse with the Cirque circle trackpad.""" +to emulate a mouse with the Cirque circle trackpad. This example +also works with glidepoint_lite.py""" import time import board from digitalio import DigitalInOut import usb_hid -# this example also works with glideoint_lite.py -from circuitpython_cirque_pinnacle.glidepoint import PinnacleTouchSPI, RELATIVE +# if using a trackpad configured for SPI +from circuitpython_cirque_pinnacle.glidepoint import PinnacleTouchSPI, ABSOLUTE, RELATIVE +# if using a trackpad configured for I2C +# from circuitpython_cirque_pinnacle.glidepoint import PinnacleTouchI2C, ABSOLUTE, RELATIVE +# i2c = board.I2C() + +# if using a trackpad configured for SPI +spi = board.SPI() +ss_pin = DigitalInOut(board.D7) + +dr_pin = DigitalInOut(board.D2) + +# if using a trackpad configured for SPI +tpad = PinnacleTouchSPI(spi, ss_pin) # NOTE we did not pass the dr_pin +# if using a trackpad configured for I2C +# tpad = PinnacleTouchI2C(i2c) # NOTE we did not pass the dr_pin + +tpad.data_mode = RELATIVE # ensure mouse mode is enabled mouse = None for dev in usb_hid.devices: @@ -19,14 +36,6 @@ # byte2 = delta y-axis # byte3 = delta scroll wheel -spi = board.SPI() -ss_pin = DigitalInOut(board.D7) -dr_pin = DigitalInOut(board.D2) - -tpad = PinnacleTouchSPI(spi, ss_pin, dr_pin=dr_pin) -# NOTE we passed the dr_pin for slightly faster data reporting -tpad.data_mode = RELATIVE # ensure mouse mode is enabled - def move(timeout=10): """Send mouse X & Y reported data from the Pinnacle touch controller for a period of ``timeout`` seconds."""