/*
* CapScroll.cpp rev01 - Capacitive Scroll Wheel Library for 'duino / Wiring
* Paul Bagder 2009-2011
* 9-27-2011 <[email protected]> http://opensource.org/licenses/mit-license.php
*/
#include "CapScroll.h"
// include core Wiring API
#include "pins_arduino.h"
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#include "pins_arduino.h"
#include "WConstants.h"
#endif
// Constructor /////////////////////////////////////////////////////////////////
// Function that handles the creation and setup of instances
CapScroll::CapScroll(uint8_t pinA, uint8_t pinB, uint8_t pinC){
// get pin mapping and port for pinA
aBit = digitalPinToBitMask(pinA); // get pinA's ports and bitmask
aPort = digitalPinToPort(pinA);
aOut = portOutputRegister(aPort); // get pointer to output register
aIn = portInputRegister(aPort); // get pointer to input register
aReg = portModeRegister(aPort); // get pointer to pin direction register
// get pin mapping and port for pinB
bBit = digitalPinToBitMask(pinB); // get pinB's ports and bitmask
bPort = digitalPinToPort(pinB);
bOut = portOutputRegister(bPort); // get pointer to output register
bIn = portInputRegister(bPort); // get pointer to input register
bReg = portModeRegister(bPort); // get pointer to pin direction register
// get pin mapping and port for pinC
cBit = digitalPinToBitMask(pinC); // get pinC's ports and bitmask
cPort = digitalPinToPort(pinC);
cOut = portOutputRegister(cPort); // get pointer to output register
cIn = portInputRegister(cPort); // get pointer to input register
cReg = portModeRegister(cPort); // get pointer to pin direction register
// this Arduino core function appears not to work
if (aPort == NOT_A_PORT || bPort == NOT_A_PORT || cPort == NOT_A_PORT) {
Serial.println("bad pin number");
return;
}
calibrateFlag = 0;
}
/*********************************************************************************/
/* Here's the general overview of the method
Read the scroll wheel as three sliders - swapping send and receive pins.
The first time through save values in a baseline value. This should be
with sensor untouched. There is a one second delay to allow users to get
away from the sensor on powerup.
This slider-read technique leads to six values. The raw values have the baseline
subtracted from them. They are then merged into 3 vectors associated with each wire.
Vector arithmetic is then used to sum vectors and resolve angle and touch values.
*/
ScrollWheelData CapScroll::readScroll(unsigned int samples){
uint8_t sBit; // declare variables for send pin's ports and bitmask
uint8_t sPort;
volatile uint8_t *sReg;
volatile uint8_t *sOut;
volatile uint8_t *sIn;
uint8_t rBit; // declare variables for receive pin's ports and bitmask
uint8_t rPort;
volatile uint8_t *rReg;
volatile uint8_t *rOut;
volatile uint8_t *rIn;
unsigned long totalS;
unsigned long totalR;
unsigned int* sBaseline;
unsigned int* rBaseline;
unsigned int Avect;
unsigned int Bvect;
unsigned int Cvect;
unsigned int topVect, middleVect;
float x, y;
int angle;
unsigned int Tvect;
// calibrateFlag = 0; // calibrate first time through the loop
for (byte j=0; j<3; j++){
totalR = 0;
totalS = 0;
// point accumulate register pointers to pins being sampled
if (j==0){
// pins A & B
sBit = aBit; // asssign send pointers to pin A
sOut = aOut;
sIn = aIn;
sReg = aReg;
rBit = bBit; // asssign receive pointers to pin B
rOut = bOut;
rIn = bIn;
rReg = bReg;
if (calibrateFlag == 0){
delay(1000); // delay 1 sec to give user time to get hands away from the sensor
sBaseline = &Baseline[0];
rBaseline = &Baseline[1];
}
else {
sBaseline = &RawResult[0];
rBaseline = &RawResult[1];
}
}
else if (j==1){
// pins B & C
sBit = bBit; // asssign send pointers to pin B
sOut = bOut;
sIn = bIn;
sReg = bReg;
rBit = cBit; // asssign receive pointers to pin C
rOut = cOut;
rIn = cIn;
rReg = cReg;
if (calibrateFlag == 0){
sBaseline = &Baseline[2];
rBaseline = &Baseline[3];
}
else {
sBaseline = &RawResult[2];
rBaseline = &RawResult[3];
}
}
else if (j==2){
// pins C & A
sBit = cBit; // asssign send pointers to pin C
sOut = cOut;
sIn = cIn;
sReg = cReg;
rBit = aBit; // asssign receive pointers to pin A
rOut = aOut;
rIn = aIn;
rReg = aReg;
if (calibrateFlag == 0){
sBaseline = &Baseline[4];
rBaseline = &Baseline[5];
calibrateFlag = 1;
}
else {
sBaseline = &RawResult[4];
rBaseline = &RawResult[5];
}
}
for(int i =0; i < samples; i++){ // 1 extra to make up for calibration cycle
// we will set up a send-receive cycle for each pin
// in the second cycle, the role of the send and receive pins will be reversed
// while the pin actions stay the same as the first pair
*sReg |= sBit; // set send pin to OUTPUT
*sOut &= ~sBit; // set Send Pin Register LOW to discharge send pin
*rOut &= ~rBit; // receive pin LOW - because the bottom loop will exit when pin is ~ 2.5V
*rReg |= rBit; // receive pin is LOW AND OUTPUT to discharge pin
delayMicroseconds(10); // short pause to discharge pin
*rReg &= ~rBit; // set receive pin to INPUT
*sOut |= sBit; // set send pin High
while( !(*rIn & rBit) && (totalR < timeoutCount )){ // while receive pin is LOW && total < timeout value
totalR++;
}
// set receive pin HIGH briefly to charge up fully - because the while loop above will exit when pin is ~ 2.5V
*rOut |= rBit; // set receive pin HIGH - turns on pullup
*rReg |= rBit; // pin is now HIGH AND OUTPUT
delayMicroseconds(10); // short pause to discharge pin
*rReg &= ~rBit; // set pin to INPUT
*rOut &= ~rBit; // turn off pullup
*sOut &= ~sBit; // set send pin LOW
while( (*rIn & rBit) && (totalR < timeoutCount)){ // while receive pin is HIGH AND total is less than timeout
totalR++;
}
// OK reverse the roles now so sIn is doing the receiving
// receive pin is now doing the sending
// actual pins in [brackets] in comments - maybe not worth the confusion
*rReg |= rBit; // set send [receive] pin to OUTPUT
*rOut &= ~rBit; // set Send Pin Register LOW to discharge send pin
*sOut &= ~sBit; // receive [send] pin LOW - because the bottom loop will exit when pin is ~ 2.5V
*sReg |= sBit; // receive [send] pin is LOW AND OUTPUT to discharge pin
delayMicroseconds(10); // short pause to discharge pin
*sReg &= ~sBit; // set receive [send] pin to INPUT
*rOut |= rBit; // set send [receive] pin High
while( !(*sIn & sBit) && (totalS < timeoutCount )){ // while receive pin is LOW && total < timeout value
totalS++;
}
// set receive pin HIGH briefly to charge up fully - because the while loop above will exit when pin is ~ 2.5V
*sOut |= sBit; // set receive [send] pin HIGH - turns on pullup
*sReg |= sBit; // pin is now HIGH AND OUTPUT
delayMicroseconds(10); // short pause to discharge pin
*sReg &= ~sBit; // set receive [send] pin to INPUT
*sOut &= ~sBit; // turn off pullup
*rOut &= ~rBit; // set send [receive] pin LOW
while( (*sIn & sBit) && (totalS < timeoutCount)){ // while receive pin is HIGH AND total is less than timeout
totalS++;
}
}
// set pins back to inputs
*sReg &= ~sBit; // set pin to INPUT
*sOut &= ~sBit; // turn off pullup
*rReg &= ~rBit; // set pin to INPUT
*rOut &= ~rBit;
没有合适的资源?快使用搜索试试~ 我知道了~
适用于 Arduino 和 AVR微控制器的简单电容感应_C++_代码_相关文件_下载
共16个文件
pde:5个
cpp:4个
h:4个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 198 浏览量
2022-07-07
17:27:20
上传
评论
收藏 22KB ZIP 举报
温馨提示
使用 Arduino / 接线的电容感应库
资源推荐
资源详情
资源评论
收起资源包目录
CapSense-master.zip (16个子文件)
CapSense-master
CapTouch.h 2KB
CapPin.cpp 4KB
CapPin.h 2KB
CapSlider.cpp 6KB
CapScroll.h 2KB
CapTouch.cpp 6KB
CapScroll.cpp 9KB
README 159B
Examples
CapPin
CapPin3.pde 2KB
CapSenseSlider
CapSenseSlider.pde 2KB
CapPinSketch
CapPinSketch.pde 2KB
CapTouch
CapTouch.ino 2KB
CapSlider
CapSlider.ino 2KB
CapSlider.pde 1KB
CapScroll
CapScroll.pde 3KB
CapSlider.h 1KB
共 16 条
- 1
资源评论
快撑死的鱼
- 粉丝: 1w+
- 资源: 9154
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- vscode-1.64.1.tar源码文件
- vscode-1.64.0.tar源码文件
- vscode-1.52.0.tar源码文件
- Music-Player +PlayerActivity+ rockplayer+ SeeJoPlayer 播放器JAVA源码
- vscode-1.46.0.tar源码文件
- 最近很火植物大战僵尸杂交版2.08苹果+安卓+PC+防闪退工具V2+修改工具+高清工具+通关存档整合包更新
- 超级好用的截图工具PixPin,可录制Gif图
- Screenshot_2024-05-21-17-06-42-64_2332cb9b27b851b548ba47a91682926c.jpg
- 毕业设计参考 - 基于树莓派、OpenCV及Python的人脸识别
- node-v18.20.2-linux-arm64
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功