Added lib

This commit is contained in:
lewishe 2023-06-30 09:41:36 +08:00
parent 0309b4cffd
commit 6e172271b6
8 changed files with 536 additions and 0 deletions

8
lib/Adafruit_MPR121/.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
# osx
.DS_Store
# doxygen
Doxyfile*
doxygen_sqlite3.db
html
*.tmp

View File

@ -0,0 +1,29 @@
language: c
sudo: false
cache:
directories:
- ~/arduino_ide
- ~/.arduino15/packages/
git:
depth: false
quiet: true
env:
global:
- ARDUINO_IDE_VERSION="1.8.5"
- PRETTYNAME="Adafruit MPR121 Arduino Library"
# Optional, will default to "$TRAVIS_BUILD_DIR/Doxyfile"
# - DOXYFILE: $TRAVIS_BUILD_DIR/Doxyfile
before_install:
- source <(curl -SLs https://raw.githubusercontent.com/adafruit/travis-ci-arduino/master/install.sh)
install:
- arduino --install-library "Adafruit ILI9341"
script:
- build_main_platforms
# Generate and deploy documentation
after_success:
- source <(curl -SLs https://raw.githubusercontent.com/adafruit/travis-ci-arduino/master/library_check.sh)
- source <(curl -SLs https://raw.githubusercontent.com/adafruit/travis-ci-arduino/master/doxy_gen_and_deploy.sh)

View File

@ -0,0 +1,244 @@
/*!
* @file Adafruit_MPR121.cpp
*
* @mainpage Adafruit MPR121 arduino driver
*
* @section intro_sec Introduction
*
* This is a library for the MPR121 I2C 12-chan Capacitive Sensor
*
* Designed specifically to work with the MPR121 sensor from Adafruit
* ----> https://www.adafruit.com/products/1982
*
* These sensors use I2C to communicate, 2+ pins are required to
* interface
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* @section author Author
*
* Written by Limor Fried/Ladyada for Adafruit Industries.
*
* @section license License
*
* BSD license, all text here must be included in any redistribution.
*/
#include "Adafruit_MPR121.h"
/*!
* @brief Default constructor
*/
Adafruit_MPR121::Adafruit_MPR121() {}
/*!
* @brief Begin an MPR121 object on a given I2C bus. This function resets
* the device and writes the default settings.
* @param i2caddr
* the i2c address the device can be found on. Defaults to 0x5A.
* @param *theWire
* Wire object
* @returns true on success, false otherwise
*/
boolean Adafruit_MPR121::begin(uint8_t i2caddr, TwoWire *theWire)
{
_i2caddr = i2caddr;
_wire = theWire;
// _wire->begin();
// soft reset
writeRegister(MPR121_SOFTRESET, 0x63);
delay(1);
for (uint8_t i = 0; i < 0x7F; i++) {
// Serial.print("$"); Serial.print(i, HEX);
// Serial.print(": 0x"); Serial.println(readRegister8(i));
}
writeRegister(MPR121_ECR, 0x0);
uint8_t c = readRegister8(MPR121_CONFIG2);
if (c != 0x24)
return false;
// setThreshholds(255, 255);
setThreshholds(12, 6);
writeRegister(MPR121_MHDR, 0x01);
writeRegister(MPR121_NHDR, 0x01);
writeRegister(MPR121_NCLR, 0x0E);
writeRegister(MPR121_FDLR, 0x00);
writeRegister(MPR121_MHDF, 0x01);
writeRegister(MPR121_NHDF, 0x05);
writeRegister(MPR121_NCLF, 0x01);
writeRegister(MPR121_FDLF, 0x00);
writeRegister(MPR121_NHDT, 0x00);
writeRegister(MPR121_NCLT, 0x00);
writeRegister(MPR121_FDLT, 0x00);
writeRegister(MPR121_DEBOUNCE, 0);
writeRegister(MPR121_CONFIG1, 0x10); // default, 16uA charge current
writeRegister(MPR121_CONFIG2, 0x20); // 0.5uS encoding, 1ms period
// writeRegister(MPR121_AUTOCONFIG0, 0x8F);
// writeRegister(MPR121_UPLIMIT, 150);
// writeRegister(MPR121_TARGETLIMIT, 100); // should be ~400 (100 shifted)
// writeRegister(MPR121_LOWLIMIT, 50);
// enable all electrodes
writeRegister(MPR121_ECR, 0x8F); // start with first 5 bits of baseline tracking
return true;
}
/*!
* @brief DEPRECATED. Use Adafruit_MPR121::setThresholds(uint8_t touch,
* uint8_t release) instead.
* @param touch
* see Adafruit_MPR121::setThresholds(uint8_t touch, uint8_t *release)
* @param release
* see Adafruit_MPR121::setThresholds(uint8_t touch, *uint8_t release)
*/
void Adafruit_MPR121::setThreshholds(uint8_t touch, uint8_t release)
{
setThresholds(touch, release);
}
/*!
* @brief Set the touch and release thresholds for all 13 channels on the
* device to the passed values. The threshold is defined as a
* deviation value from the baseline value, so it remains constant even baseline
* value changes. Typically the touch threshold is a little bigger than the
* release threshold to touch debounce and hysteresis. For typical touch
* application, the value can be in range 0x05~0x30 for example. The setting of
* the threshold is depended on the actual application. For the operation details
* and how to set the threshold refer to application note AN3892 and MPR121
* design guidelines.
* @param touch
* the touch threshold value from 0 to 255.
* @param release
* the release threshold from 0 to 255.
*/
void Adafruit_MPR121::setThresholds(uint8_t touch, uint8_t release)
{
// set all thresholds (the same)
for (uint8_t i = 0; i < 12; i++) {
writeRegister(MPR121_TOUCHTH_0 + 2 * i, touch);
writeRegister(MPR121_RELEASETH_0 + 2 * i, release);
}
}
/*!
* @brief Read the filtered data from channel t. The ADC raw data outputs
* run through 3 levels of digital filtering to filter out the high frequency and
* low frequency noise encountered. For detailed information on this filtering
* see page 6 of the device datasheet.
* @param t
* the channel to read
* @returns the filtered reading as a 10 bit unsigned value
*/
uint16_t Adafruit_MPR121::filteredData(uint8_t t)
{
if (t > 12)
return 0;
return readRegister16(MPR121_FILTDATA_0L + t * 2);
}
/*!
* @brief Read the baseline value for the channel. The 3rd level filtered
* result is internally 10bit but only high 8 bits are readable from registers
* 0x1E~0x2A as the baseline value output for each channel.
* @param t
* the channel to read.
* @returns the baseline data that was read
*/
uint16_t Adafruit_MPR121::baselineData(uint8_t t)
{
if (t > 12)
return 0;
uint16_t bl = readRegister8(MPR121_BASELINE_0 + t);
return (bl << 2);
}
/**
* @brief Read the touch status of all 13 channels as bit values in a 12 bit integer.
* @returns a 12 bit integer with each bit corresponding to the touch status
* of a sensor. For example, if bit 0 is set then channel 0 of the device is
* currently deemed to be touched.
*/
uint16_t Adafruit_MPR121::touched(void)
{
uint16_t t = readRegister16(MPR121_TOUCHSTATUS_L);
return t & 0x0FFF;
}
/*!
* @brief Read the contents of an 8 bit device register.
* @param reg the register address to read from
* @returns the 8 bit value that was read.
*/
uint8_t Adafruit_MPR121::readRegister8(uint8_t reg)
{
_wire->beginTransmission(_i2caddr);
_wire->write(reg);
_wire->endTransmission(false);
_wire->requestFrom(_i2caddr, 1);
if (_wire->available() < 1)
return 0;
return (_wire->read());
}
/*!
* @brief Read the contents of a 16 bit device register.
* @param reg the register address to read from
* @returns the 16 bit value that was read.
*/
uint16_t Adafruit_MPR121::readRegister16(uint8_t reg)
{
_wire->beginTransmission(_i2caddr);
_wire->write(reg);
_wire->endTransmission(false);
_wire->requestFrom(_i2caddr, 2);
if (_wire->available() < 2)
return 0;
uint16_t v = _wire->read();
v |= ((uint16_t)_wire->read()) << 8;
return v;
}
/*!
@brief Writes 8-bits to the specified destination register
@param reg the register address to write to
@param value the value to write
*/
void Adafruit_MPR121::writeRegister(uint8_t reg, uint8_t value)
{
//MPR121 must be put in Stop Mode to write to most registers
bool stop_required = true;
if (reg == MPR121_ECR || (0x73 <= reg && reg <= 0x7A)) {
stop_required = false;
}
if (stop_required) {
_wire->beginTransmission(_i2caddr);
_wire->write(MPR121_ECR);
_wire->write(0x00);
_wire->endTransmission();
}
_wire->beginTransmission(_i2caddr);
_wire->write((uint8_t)reg);
_wire->write((uint8_t)(value));
_wire->endTransmission();
if (stop_required) {
_wire->beginTransmission(_i2caddr);
_wire->write(MPR121_ECR);
_wire->write(0x8F);
_wire->endTransmission();
}
}

View File

@ -0,0 +1,105 @@
/*!
* @file Adafruit_MPR121.h
*
* This is a library for the MPR121 12-Channel Capacitive Sensor
*
* Designed specifically to work with the MPR121 board.
*
* Pick one up today in the adafruit shop!
* ------> https://www.adafruit.com/product/1982
*
* These sensors use I2C to communicate, 2+ pins are required to interface
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit andopen-source hardware by purchasing products
* from Adafruit!
*
* Limor Fried/Ladyada (Adafruit Industries).
*
* BSD license, all text above must be included in any redistribution
*/
#ifndef ADAFRUIT_MPR121_H
#define ADAFRUIT_MPR121_H
#include "Arduino.h"
#include <Wire.h>
// The default I2C address
#define MPR121_I2CADDR_DEFAULT 0x5A ///< default I2C address
/*!
* Device register map
*/
enum {
MPR121_TOUCHSTATUS_L = 0x00,
MPR121_TOUCHSTATUS_H = 0x01,
MPR121_FILTDATA_0L = 0x04,
MPR121_FILTDATA_0H = 0x05,
MPR121_BASELINE_0 = 0x1E,
MPR121_MHDR = 0x2B,
MPR121_NHDR = 0x2C,
MPR121_NCLR = 0x2D,
MPR121_FDLR = 0x2E,
MPR121_MHDF = 0x2F,
MPR121_NHDF = 0x30,
MPR121_NCLF = 0x31,
MPR121_FDLF = 0x32,
MPR121_NHDT = 0x33,
MPR121_NCLT = 0x34,
MPR121_FDLT = 0x35,
MPR121_TOUCHTH_0 = 0x41,
MPR121_RELEASETH_0 = 0x42,
MPR121_DEBOUNCE = 0x5B,
MPR121_CONFIG1 = 0x5C,
MPR121_CONFIG2 = 0x5D,
MPR121_CHARGECURR_0 = 0x5F,
MPR121_CHARGETIME_1 = 0x6C,
MPR121_ECR = 0x5E,
MPR121_AUTOCONFIG0 = 0x7B,
MPR121_AUTOCONFIG1 = 0x7C,
MPR121_UPLIMIT = 0x7D,
MPR121_LOWLIMIT = 0x7E,
MPR121_TARGETLIMIT = 0x7F,
MPR121_GPIODIR = 0x76,
MPR121_GPIOEN = 0x77,
MPR121_GPIOSET = 0x78,
MPR121_GPIOCLR = 0x79,
MPR121_GPIOTOGGLE = 0x7A,
MPR121_SOFTRESET = 0x80,
};
//.. thru to 0x1C/0x1D
/*!
* @brief Class that stores state and functions for interacting with MPR121
* proximity capacitive touch sensor controller.
*/
class Adafruit_MPR121 {
public:
// Hardware I2C
Adafruit_MPR121();
boolean begin(uint8_t i2caddr = MPR121_I2CADDR_DEFAULT, TwoWire *theWire = &Wire);
uint16_t filteredData(uint8_t t);
uint16_t baselineData(uint8_t t);
uint8_t readRegister8(uint8_t reg);
uint16_t readRegister16(uint8_t reg);
void writeRegister(uint8_t reg, uint8_t value);
uint16_t touched(void);
// Add deprecated attribute so that the compiler shows a warning
void setThreshholds(uint8_t touch, uint8_t release)
__attribute__((deprecated));
void setThresholds(uint8_t touch, uint8_t release);
private:
int8_t _i2caddr;
TwoWire *_wire;
};
#endif

View File

@ -0,0 +1,48 @@
# Adafruit MPR121 Library [![Build Status](https://travis-ci.org/adafruit/Adafruit_MPR121.svg?branch=master)](https://travis-ci.org/adafruit/Adafruit_MPR121)
<a href="https://www.adafruit.com/products/1982"><img src="https://cdn-shop.adafruit.com/970x728/1982-00.jpg" height="300"/></a>
Tested and works great with the Adafruit MPR121
* https://www.adafruit.com/products/1982
* https://www.adafruit.com/product/2024
* https://www.adafruit.com/product/2340
Check out the links above for our tutorials and wiring diagrams.
These sensors use I2C to communicate, 2+ pins are required to interface
Adafruit invests time and resources providing this open source code, please support Adafruit and open-source hardware by purchasing products from Adafruit!
Written by Limor Fried/Ladyada (Adafruit Industries).
MIT license, all text above must be included in any redistribution
<!-- START COMPATIBILITY TABLE -->
## Compatibility
MCU | Tested Works | Doesn't Work | Not Tested | Notes
------------------ | :----------: | :----------: | :---------: | -----
Atmega328 @ 16MHz | X | | |
Atmega328 @ 12MHz | X | | |
Atmega32u4 @ 16MHz | X | | |
Atmega32u4 @ 8MHz | X | | |
ESP8266 | X | | |
Atmega2560 @ 16MHz | X | | |
ATSAM3X8E | | X | |
ATSAM21D | X | | |
ATtiny85 @ 16MHz | | | X |
ATtiny85 @ 8MHz | | | X |
Intel Curie @ 32MHz | | | X |
STM32F2 | | | X |
* ATmega328 @ 16MHz : Arduino UNO, Adafruit Pro Trinket 5V, Adafruit Metro 328, Adafruit Metro Mini
* ATmega328 @ 12MHz : Adafruit Pro Trinket 3V
* ATmega32u4 @ 16MHz : Arduino Leonardo, Arduino Micro, Arduino Yun, Teensy 2.0
* ATmega32u4 @ 8MHz : Adafruit Flora, Bluefruit Micro
* ESP8266 : Adafruit Huzzah
* ATmega2560 @ 16MHz : Arduino Mega
* ATSAM3X8E : Arduino Due
* ATSAM21D : Arduino Zero, M0 Pro
* ATtiny85 @ 16MHz : Adafruit Trinket 5V
* ATtiny85 @ 8MHz : Adafruit Gemma, Arduino Gemma, Adafruit Trinket 3V
<!-- END COMPATIBILITY TABLE -->

View File

@ -0,0 +1,87 @@
/*********************************************************
This is a library for the MPR121 12-channel Capacitive touch sensor
Designed specifically to work with the MPR121 Breakout in the Adafruit shop
----> https://www.adafruit.com/products/
These sensors use I2C communicate, at least 2 pins are required
to interface
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, all text above must be included in any redistribution
**********************************************************/
#include <Wire.h>
#include "Adafruit_MPR121.h"
#ifndef _BV
#define _BV(bit) (1 << (bit))
#endif
// You can have up to 4 on one i2c bus but one is enough for testing!
Adafruit_MPR121 cap = Adafruit_MPR121();
// Keeps track of the last pins touched
// so we know when buttons are 'released'
uint16_t lasttouched = 0;
uint16_t currtouched = 0;
void setup() {
Serial.begin(9600);
while (!Serial) { // needed to keep leonardo/micro from starting too fast!
delay(10);
}
Serial.println("Adafruit MPR121 Capacitive Touch sensor test");
// Default address is 0x5A, if tied to 3.3V its 0x5B
// If tied to SDA its 0x5C and if SCL then 0x5D
if (!cap.begin(0x5A)) {
Serial.println("MPR121 not found, check wiring?");
while (1);
}
Serial.println("MPR121 found!");
}
void loop() {
// Get the currently touched pads
currtouched = cap.touched();
for (uint8_t i=0; i<12; i++) {
// it if *is* touched and *wasnt* touched before, alert!
if ((currtouched & _BV(i)) && !(lasttouched & _BV(i)) ) {
Serial.print(i); Serial.println(" touched");
}
// if it *was* touched and now *isnt*, alert!
if (!(currtouched & _BV(i)) && (lasttouched & _BV(i)) ) {
Serial.print(i); Serial.println(" released");
}
}
// reset our state
lasttouched = currtouched;
// comment out this line for detailed data from the sensor!
return;
// debugging info, what
Serial.print("\t\t\t\t\t\t\t\t\t\t\t\t\t 0x"); Serial.println(cap.touched(), HEX);
Serial.print("Filt: ");
for (uint8_t i=0; i<12; i++) {
Serial.print(cap.filteredData(i)); Serial.print("\t");
}
Serial.println();
Serial.print("Base: ");
for (uint8_t i=0; i<12; i++) {
Serial.print(cap.baselineData(i)); Serial.print("\t");
}
Serial.println();
// put a delay so it isn't overwhelming
delay(100);
}

View File

@ -0,0 +1,6 @@
Adafruit_MPR121 KEYWORD1
begin KEYWORD2
filteredData KEYWORD2
baselineData KEYWORD2
touched KEYWORD2
setThresholds KEYWORD2

View File

@ -0,0 +1,9 @@
name=Adafruit MPR121
version=1.0.2
author=Adafruit <info@adafruit.com>
maintainer=Adafruit <info@adafruit.com>
sentence=Arduino library for the MPR121-based capacitive sensors in the Adafruit shop.
paragraph=Designed specifically to work with the MPR121 Breakout in the Adafruit shop.
category=Sensors
url=https://github.com/adafruit/Adafruit_MPR121
architectures=*