Skip to content

Commit

Permalink
moved clamping from report() to simpletest.py
Browse files Browse the repository at this point in the history
  • Loading branch information
2bndy5 committed Apr 8, 2020
1 parent 3ed3aa2 commit caad98d
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 53 deletions.
2 changes: 0 additions & 2 deletions circuitpython_cirque_pinnacle/glidepoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]])
Expand Down
2 changes: 0 additions & 2 deletions circuitpython_cirque_pinnacle/glidepoint_lite.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]])
Expand Down
26 changes: 17 additions & 9 deletions examples/cirque_pinnacle_anymeas_test.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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]))

Expand All @@ -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()
45 changes: 45 additions & 0 deletions examples/cirque_pinnacle_simpletest.py
Original file line number Diff line number Diff line change
@@ -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()
29 changes: 0 additions & 29 deletions examples/cirque_pinnacle_spi_simpletest.py

This file was deleted.

31 changes: 20 additions & 11 deletions examples/cirque_pinnacle_usb_mouse.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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."""
Expand Down

0 comments on commit caad98d

Please sign in to comment.