Skip to content

Commit

Permalink
debugged & added example for ctx manager
Browse files Browse the repository at this point in the history
  • Loading branch information
2bndy5 committed Mar 18, 2020
1 parent 565fe9e commit b7dd4f3
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
3 changes: 2 additions & 1 deletion circuitpython_cirque_pinnacle.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,14 @@ def __enter__(self):
# write to all internal attributes to registers
self._rap_write_bytes(PINNACLE_SYS_CONFIG, [
self._sys_config, self._feed_config1, self._feed_config2])
self.set_adc_gain(self._sensitivity)
self._era_write(0x0187, self._sensitivity)
self.detect_finger_stylus(
self._finger_stylus & 4, self._finger_stylus & 1, self._sample_rate)
self._rap_write(PINNACLE_CALIBRATE_CONFIG, self._cal_config)
self._rap_write(PINNACLE_Z_IDLE, self._z_idle_count)
self.feed_enable = True
self.clear_flags() # just to be sure
return self

def __exit__(self, *exc):
self.shutdown = True
Expand Down
9 changes: 9 additions & 0 deletions docs/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,12 @@ This example uses the Pinnacle touch controller's AnyMeas mode to fetch raw ADC
.. literalinclude:: ../examples/cirque_pinnacle_anymeas_test.py
:caption: examples/cirque_pinnacle_anymeas_test.py
:linenos:

Context Manager example
-----------------------

This example displays the difference in the Pinnacle ASIC's configuration registers.

.. literalinclude:: ../examples/cirque_pinnacle_context_test.py
:caption: examples/cirque_pinnacle_context_test.py
:linenos:
46 changes: 46 additions & 0 deletions examples/cirque_pinnacle_context_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""This example shows the difference in register values between AnyMeas mode
and Absolute mode using the library's context manager."""
import board
from digitalio import DigitalInOut
from circuitpython_cirque_pinnacle import PinnacleTouchSPI, DataModes

spi = board.SPI()
ss_pin = DigitalInOut(board.D7)
dr_pin = DigitalInOut(board.D2)
dr_pin.switch_to_input()
csn_pin = DigitalInOut(board.D5)

tpad_abs = PinnacleTouchSPI(spi, ss_pin) # NOTE we did not pass the dr_pin
tpad_abs.data_mode = DataModes.ABSOLUTE

# NOTE The dr_pin is a required arg to use AnyMeas mode
tpad_any = PinnacleTouchSPI(spi, ss_pin, dr_pin=dr_pin)
# if dr_pin was not specified upon instantiation.
# this command will raise an AttributeError exception
tpad_any.data_mode = DataModes.ANYMEAS

# This example accesses the hidden bus protocol functions to read and
# print the data from the Pinnacle ASIC's configuration registers.
# pylint: disable=protected-access

print("register configured for Absolute data mode:")
with tpad_abs as tpad:
for index, val in enumerate(tpad._rap_read_bytes(0x04, 11)):
print("\taddress {} = {}".format(hex(index + 4), hex(val)))

print("\nregister configured for AnyMeas data mode:")
with tpad_any as tpad:
for index, val in enumerate(tpad._rap_read_bytes(0x04, 11)):
print("\taddress {} = {}".format(hex(index + 4), hex(val)))

# NOTE how different the register values are.
# Registers remain unaffected when exiting the `with` block, so technically
# tpad_abs (configured for Absolute mode) will not function properly outside
# a new `with` block after tpad_any (configured for AnyMeas mode) was the last
# object used in a `with` block.

# NOTE the Pinnacle ASIC is powered down when exiting a `with` block
print("Powered down?", tpad_any.shutdown)
# ok to read the shutdown attribute here because it does not write to the
# Pinnacle ASIC's register, but notice it is the last object used in a `with`
# block (otherwise the data may be innaccurate).

0 comments on commit b7dd4f3

Please sign in to comment.