cap1xxx

package
v3.3.0+incompatible Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 7, 2018 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package cap1xxx controls a Microchip cap1105/cap1106/cap1114/cap1133/cap1126/cap1128/cap1166/cap1188 device over I²C.

The cap1xxx devices are a 3/5/6/8/14 channel capacitive touch sensor with 2/3/6/8/11 LED drivers.

Datasheet

3 sensors, 3 LEDs: http://ww1.microchip.com/downloads/en/DeviceDoc/CAP1133.pdf

5-6 sensors, no LED: http://ww1.microchip.com/downloads/en/DeviceDoc/CAP1105_CAP1106.pdf

6 sensors, 2 LEDs: http://ww1.microchip.com/downloads/en/DeviceDoc/CAP1126.pdf

6 sensors, 6 LEDs: http://ww1.microchip.com/downloads/en/DeviceDoc/CAP1166.pdf

8 sensors, 2 LEDs: http://ww1.microchip.com/downloads/en/DeviceDoc/CAP1128.pdf

8 sensors, 8 LEDs: http://ww1.microchip.com/downloads/en/DeviceDoc/CAP1188.pdf

14 sensors, 11 LEDs: http://ww1.microchip.com/downloads/en/DeviceDoc/CAP1114.pdf

Example
package main

import (
	"fmt"
	"log"

	"periph.io/x/periph/conn/gpio"
	"periph.io/x/periph/conn/gpio/gpioreg"
	"periph.io/x/periph/conn/i2c/i2creg"
	"periph.io/x/periph/devices/cap1xxx"
)

func main() {
	// Open the I²C bus to which the cap1188 is connected.
	i2cBus, err := i2creg.Open("")
	if err != nil {
		log.Fatal(err)
	}
	defer i2cBus.Close()

	// We need to set an alert ping that will let us know when a touch event
	// occurs. The alert pin is the pin connected to the IRQ/interrupt pin.
	alertPin := gpioreg.ByName("GPIO25")
	if alertPin == nil {
		log.Fatal("invalid alert GPIO pin number")
	}
	// We set the alert pin to monitor for interrupts.
	if err := alertPin.In(gpio.PullUp, gpio.BothEdges); err != nil {
		log.Fatalf("Can't monitor the alert pin")
	}

	// Optionally but highly recommended, we can also set a reset pin to
	// start/leave things in a clean state.
	resetPin := gpioreg.ByName("GPIO21")
	if resetPin == nil {
		log.Fatal("invalid reset GPIO pin number")
	}

	// We will configure the cap1188 by setting some options, we can start by the
	// defaults.
	opts := cap1xxx.DefaultOpts
	opts.AlertPin = alertPin
	opts.ResetPin = resetPin

	// Open the device so we can detect touch events.
	dev, err := cap1xxx.NewI2C(i2cBus, &opts)
	if err != nil {
		log.Fatalf("couldn't open cap1xxx: %v", err)
	}

	fmt.Println("Monitoring for touch events")
	maxTouches := 42 // Stop the program after 42 touches.
	for maxTouches > 0 {
		if alertPin.WaitForEdge(-1) {
			maxTouches--
			var statuses [8]cap1xxx.TouchStatus
			if err := dev.InputStatus(statuses[:]); err != nil {
				fmt.Printf("Error reading inputs: %v\n", err)
				continue
			}
			// print the status of each sensor
			for i, st := range statuses {
				fmt.Printf("#%d: %s\t", i, st)
			}
			fmt.Println()
			// we need to clear the interrupt so it can be triggered again
			if err := dev.ClearInterrupt(); err != nil {
				fmt.Println(err, "while clearing the interrupt")
			}
		}
	}
	fmt.Print("\n")
}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var DefaultOpts = Opts{
	LinkedLEDs:            true,
	MaxTouchDuration:      MaxDur5600ms,
	RetriggerOnHold:       false,
	EnableRecalibration:   false,
	InterruptOnRelease:    false,
	SamplesPerMeasurement: Avg1,
	SamplingTime:          S1_28ms,
	CycleTime:             C35ms,
}

DefaultOpts contains default options to use.

Functions

This section is empty.

Types

type AvgSampling

type AvgSampling uint8

AvgSampling set the number of samples per measurement that get averaged.

const (
	// Avg1 means that 1 sample is taken per measurement
	Avg1   AvgSampling = iota // 0
	Avg2                      // 1
	Avg4                      // 2
	Avg8                      // 3 default
	Avg16                     // 4
	Avg32                     // 5
	Avg64                     // 6
	Avg128                    // 7
)

Valid AvgSampling values.

type CycleTime

type CycleTime uint8

CycleTime determines the overall cycle time for all measured channels during normal operation.

const (
	C35ms CycleTime = iota // 0
	C70ms                  // default
	C105ms
	C140ms
)

Valid CycleTime values.

type Dev

type Dev struct {
	// contains filtered or unexported fields
}

Dev is a handle to a device of the cap1xxx family.

func NewI2C

func NewI2C(b i2c.Bus, opts *Opts) (*Dev, error)

NewI2C returns a new device that communicates over I²C to one of the supported cap1xxx device.

Use default options if nil is used.

func (*Dev) AllLEDs

func (d *Dev) AllLEDs(on bool) error

AllLEDs turns all the LEDs on or off.

This is quite more efficient than looping through each LED and turn them on/off.

func (*Dev) ClearInterrupt

func (d *Dev) ClearInterrupt() error

ClearInterrupt resets the interrupt flag.

func (*Dev) Halt

func (d *Dev) Halt() error

Halt is a noop for the cap1xxx.

func (*Dev) InputStatus

func (d *Dev) InputStatus(t []TouchStatus) error

InputStatus reads and returns the status of the inputs.

The slice t will have the sensed inputs updated upon successful read. If the slice is too long, extraneous elements are ignored. If the slice is too short, only the provided subset is updated without error.

func (*Dev) LinkLEDs

func (d *Dev) LinkLEDs(on bool) error

LinkLEDs links the behavior of the LEDs to the touch sensors. Doing so, disabled the option for the host to set specific LEDs on/off.

func (*Dev) Reset

func (d *Dev) Reset() error

Reset issues a soft reset to the device using the reset pin if available.

func (*Dev) SetLED

func (d *Dev) SetLED(idx int, state bool) error

SetLED sets the state of a LED as on or off.

Only works if the LEDs are not linked to the sensors

func (*Dev) String

func (d *Dev) String() string

type MaxDur

type MaxDur uint8

MaxDur is the maximum duration of a touch event before it triggers a recalibration.

const (
	MaxDur560ms MaxDur = iota
	MaxDur840ms
	MaxDur1120ms
	MaxDur1400ms
	MaxDur1680ms
	MaxDur2240ms
	MaxDur2800ms
	MaxDur3360ms
	MaxDur3920ms
	MaxDur44800ms
	MaxDur5600ms // default
	MaxDur6720ms
	MaxDur7840ms
	MaxDur8906ms
	MaxDur10080ms
	MaxDur11200ms
)

Valid MaxDur values.

type Opts

type Opts struct {
	// Debug turns on extra logging capabilities.
	Debug bool
	// I2CAddr is the I²C slave address to use. It can only used on creation of
	// an I²C-device. Its default value is 0x28. It can be set to other values
	// (0x29, 0x2a, 0x2b, 0x2c) depending on the HW configuration of the
	// ADDR_COMM pin. Must not be set when used over SPI.
	I2CAddr uint16
	// LinkedLEDs indicates if the LEDs should be activated automatically
	// when their sensors detect a touch event.
	LinkedLEDs bool
	// MaxTouchDuration sets the touch duration threshold. It is possible that a
	// “stuck button” occurs when something is placed on a button which causes a
	// touch to be detected for a long period. By setting this value, a
	// recalibration can be forced when a touch is held on a button for longer
	// than the duration specified.
	MaxTouchDuration MaxDur
	// EnableRecalibration is used to force the recalibration if a touch event
	// lasts longer than MaxTouchDuration.
	EnableRecalibration bool

	// AlertPin is the pin receiving the interrupt when a touch event is detected
	// and optionally if a release event is detected.
	AlertPin gpio.PinIn
	// ResetPin is the pin used to reset the device.
	ResetPin gpio.PinOut

	// InterruptOnRelease indicates if the device should also trigger an
	// interrupt on the AlertPin when a release event is detected.
	InterruptOnRelease bool

	// RetriggerOnHold forces a retrigger of the interrupt when a sensor is
	// pressed for longer than MaxTouchDuration
	RetriggerOnHold bool

	// SamplesPerMeasurement is the number of samples taken per measurement. All
	// samples are taken consecutively on the same channel before the next
	// channel is sampled and the result is averaged over the number of samples
	// measured before updating the measured results.
	// Available options: 1, 2, 4, 8 (default), 16, 32, 64, 128
	SamplesPerMeasurement AvgSampling

	// SamplingTime Determines the time to take a single sample as shown.
	SamplingTime SamplingTime

	// CycleTime determines the overall cycle time for all measured channels
	// during normal operation. All measured channels are sampled at the
	// beginning of the cycle time. If additional time is remaining, then the
	// device is placed into a lower power state for the remaining duration of
	// the cycle.
	CycleTime CycleTime
}

Opts is options to pass to the constructor.

type SamplingTime

type SamplingTime uint8

SamplingTime determines the time to make a single sample.

const (
	S320us SamplingTime = 0
	S640us SamplingTime = 1
	// S1_28ms represents 1.28ms sampling time, which is the default.
	S1_28ms SamplingTime = 2
	S2_56ms SamplingTime = 3
)

Valid SamplingTime values.

type TouchStatus

type TouchStatus int8

TouchStatus is the status of an input sensor.

const (
	// OffStatus indicates that the input sensor isn't being activated.
	OffStatus TouchStatus = iota
	// PressedStatus indicates that the input sensor is currently pressed.
	PressedStatus
	// HeldStatus indicates that the input sensor was pressed and is still held
	// pressed.
	HeldStatus
	// ReleasedStatus indicates that the input sensor was pressed and is being
	// released.
	ReleasedStatus
)

func (TouchStatus) String

func (i TouchStatus) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL