|
|
/*------------------------------------------------------------------------------
|
|
|
-- This file is a part of the libuc, microcontroler library
|
|
|
-- Copyright (C) 2013, Alexis Jeandet
|
|
|
--
|
|
|
-- This program is free software; you can redistribute it and/or modify
|
|
|
-- it under the terms of the GNU General Public License as published by
|
|
|
-- the Free Software Foundation; either version 3 of the License, or
|
|
|
-- (at your option) any later version.
|
|
|
--
|
|
|
-- This program is distributed in the hope that it will be useful,
|
|
|
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
-- GNU General Public License for more details.
|
|
|
--
|
|
|
-- You should have received a copy of the GNU General Public License
|
|
|
-- along with this program; if not, write to the Free Software
|
|
|
-- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
-------------------------------------------------------------------------------
|
|
|
-- Author : Alexis Jeandet
|
|
|
-- Mail : alexis.jeandet@member.fsf.org
|
|
|
-------------------------------------------------------------------------------*/
|
|
|
|
|
|
#include <ADS7843.h>
|
|
|
#include <core.h>
|
|
|
|
|
|
int ads7843init(ADS7843_t *dev, spi_t spidev, char config, void (*setnCS)(char), int (*busy)())
|
|
|
{
|
|
|
dev->spidev = spidev;
|
|
|
dev->setnCS = setnCS;
|
|
|
dev->busy = busy;
|
|
|
dev->setnCS(1);
|
|
|
dev->config = config & ADS7843_CONFIG_MASK;
|
|
|
dev->xMax = 1000;
|
|
|
dev->xMin = 0;
|
|
|
dev->yMax = 1000;
|
|
|
dev->yMin = 0;
|
|
|
dev->zeroRtouch = 0;
|
|
|
dev->zeroX = 500;
|
|
|
dev->zeroY = 500;
|
|
|
dev->delta = 200;
|
|
|
return 1;
|
|
|
}
|
|
|
|
|
|
unsigned int __ads7843read(ADS7843_t *dev,char cfg)
|
|
|
{
|
|
|
unsigned int data;
|
|
|
dev->setnCS(0);
|
|
|
while(dev->busy());
|
|
|
spiputw(dev->spidev,ADS7843_START|dev->config|cfg);
|
|
|
data= (0x0FF & spigetw2(dev->spidev,0))<<8;
|
|
|
data+= 0x0FF & spigetw2(dev->spidev,0);
|
|
|
dev->setnCS(1);
|
|
|
return (data>>4);
|
|
|
}
|
|
|
|
|
|
int ads7843read(ADS7843_t *dev,int* x,int* y)
|
|
|
{
|
|
|
*x=0;
|
|
|
*y=0;
|
|
|
for(int i=0;i<32;i++)
|
|
|
{
|
|
|
*x += __ads7843read(dev,ADS7843_MEAS_X_POS_DIFF|ADS7843_POWER_REF_ON|ADS7843_POWER_ADC_ON);
|
|
|
*y += __ads7843read(dev,ADS7843_MEAS_Y_POS_DIFF|ADS7843_POWER_REF_ON|ADS7843_POWER_ADC_ON);
|
|
|
}
|
|
|
*x>>=5;
|
|
|
*y>>=5;
|
|
|
*x = ((*x-dev->xMin)*1000)/(dev->xMax-dev->xMin);
|
|
|
*y = ((*y-dev->yMin)*1000)/(dev->yMax-dev->yMin);
|
|
|
return 1;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int ads7843readRtouch(ADS7843_t *dev)
|
|
|
{
|
|
|
#define avg 32
|
|
|
int z1=0,z2=0,x=0;
|
|
|
for(int i=0;i<avg;i++)
|
|
|
{
|
|
|
x += __ads7843read(dev,ADS7843_MEAS_X_POS_DIFF|ADS7843_POWER_REF_ON|ADS7843_POWER_ADC_ON);
|
|
|
z1 += __ads7843read(dev,ADS7843_MEAS_Z1_POS_DIFF|ADS7843_POWER_REF_ON|ADS7843_POWER_ADC_ON);
|
|
|
z2 += __ads7843read(dev,ADS7843_MEAS_Z2_POS_DIFF|ADS7843_POWER_REF_ON|ADS7843_POWER_ADC_ON);
|
|
|
}
|
|
|
return (x/(4096/avg))*((z2/z1)-1);
|
|
|
}
|
|
|
|
|
|
|
|
|
int ads7843calibrate(ADS7843_t *dev, uint16_t zeroX, uint16_t zeroY, uint16_t zeroRtouch, uint16_t xMin, uint16_t xMax, uint16_t yMin, uint16_t yMax, char delta)
|
|
|
{
|
|
|
dev->xMax = xMax;
|
|
|
dev->xMin = xMin;
|
|
|
dev->yMax = yMax;
|
|
|
dev->yMin = yMin;
|
|
|
dev->zeroRtouch = zeroRtouch;
|
|
|
dev->zeroX = zeroX;
|
|
|
dev->zeroY = zeroY;
|
|
|
dev->delta = delta;
|
|
|
}
|
|
|
|
|
|
|
|
|
int ads7843isPenTouching(ADS7843_t *dev)
|
|
|
{
|
|
|
int rtc= ads7843readRtouch(dev);
|
|
|
return ((rtc+dev->delta) < dev->zeroRtouch);
|
|
|
|
|
|
}
|
|
|
|