# Adafruit CAP1188 Capacitive Touch Sensor demo for Raspberry Pi Pico

# To run, this file should be copied to the CIRCUITPY drive as code.py

# Note: this sketch requires the optional adafruit_cap1188 library
# which should be copied as a folder to CIRCUITPY/lib on the Pico.
# The adafruit_cap1188 folder can be found within the library bundle
# available from https://circuitpython.org/libraries

# related docs:
#  https://www.adafruit.com/product/1602
#  https://learn.adafruit.com/adafruit-cap1188-breakout
#  https://learn.adafruit.com/adafruit-cap1188-breakout/python-circuitpython
#  https://docs.circuitpython.org/projects/cap1188/en/latest/

# wiring for I2C on a Raspberry Pi Pico:
#  pin 36, 3V3(OUT)      ->      sensor VIN
#  pin 38, GND           ->      sensor GND
#  pin 1, I2C0 SDA, GP0  ->      sensor SDA
#  pin 2, I2C0 SCL, GP1  ->      sensor SCK

#------------------------------------------------------------------
import time
import board
import busio

from adafruit_cap1188.i2c import CAP1188_I2C

#------------------------------------------------------------------
# initialize the hardware interface objects

# this assumes the wiring noted above
i2c = busio.I2C(scl=board.GP1, sda=board.GP0)
cap = CAP1188_I2C(i2c)

#------------------------------------------------------------------
print("initial thresholds:", cap.thresholds)
print("sensitivity:", cap.sensitivity)
print("averaging:", cap.averaging)
print("touch state:", cap.touched_pins)

# Notes:
#    cap.recalibrate()    will recalibrate all channels,
#    cap[n].recalibrate() will recalibrate a single channel.
#------------------------------------------------------------------

# Constantly read the touch sensors and print messages.
while True:
    for i in range(1, 9):
        if cap[i].value:
            print("Pin", i, "touched, raw value is", cap[i].raw_value, "threshold is", cap[i].threshold)


