Documentation ¶
Overview ¶
Package cap1188 controls a Microchip cap1188 device over I²C.
The cap1188 device is a 8 channel capacitive touch sensor with 8 LED drivers.
Datasheet ¶
The official data sheet can be found here:
http://ww1.microchip.com/downloads/en/DeviceDoc/CAP1188.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/experimental/devices/cap1188" ) 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 := cap1188.DefaultOpts opts.AlertPin = alertPin opts.ResetPin = resetPin // Open the device so we can detect touch events. dev, err := cap1188.NewI2C(i2cBus, &opts) if err != nil { log.Fatalf("couldn't open cap1188: %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]cap1188.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 ¶
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.
type Dev ¶
type Dev struct {
// contains filtered or unexported fields
}
Dev is a handle to a cap1188.
func NewI2C ¶
NewI2C returns a new device that communicates over I²C to cap1188.
Use default options if nil is used.
func (*Dev) AllLEDs ¶
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 ¶
ClearInterrupt resets the interrupt flag.
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 ¶
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.
type MaxDur ¶
type MaxDur uint8
MaxDur is the maximum duration of a touch event before it triggers a recalibration.
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