#include "ctp.h"
#include "lcd.h"
#include "LCD_Draw.h"
#define PIXCIR_ID 0x70
#define I2C_STARTTCH_READ 0x00
CTP_STATUS ctp,pre_ctp;
uint8_t RdBuff[34];
void IIC_SDA(char x)
{
if (x)
CTP_SDA_SET;
else
CTP_SDA_CLR;
}
void IIC_SCL(char x)
{
if (x)
LCD_SCL_SET;
else
LCD_SCL_CLR;
}
void CTP_RST(char x)
{
if (x)
LCD_SDA_SET;
else
LCD_SDA_CLR;
}
char GET_SDA()
{
if (gpio->rGPBDAT & (1 << 10))
return 1;
else
return 0;
}
char GET_INIT()
{
if (gpio->rGPBDAT &(1 << 9)){
return 1;
}else{
return 0;
}
}
/*
II2C管脚定义
SDA ===>GPB10 output/input
SCL====>GPB3 output
INT====>GPB9 设置为输入
RST====>GPB2 output
*/
void CTP_II2C_Init()
{
//gpio->rGPBCON = (gpio->rGPBCON & (~0x003c0cf0))| 0x00040450;
//gpio->rGPBUDP = (gpio->rGPBUDP & (~0x003c0cf0))| 0x002808a0;
//gpio->rGPBCON = 0x00100050;
//gpio->rGPBUDP = 0x00500050;
gpio->rGPBCON = (gpio->rGPBCON & (~0x003c0cf0))|0x00140055;
gpio->rGPBUDP = (gpio->rGPBUDP & (~0x003c0cf0))|0x001000a5;
}
void Delayms(int time)
{
u8_t i;
while(time--)
for(i=0;i<100;i++);
}
void ctp_init(void)
{
CTP_II2C_Init();
LCD_SDA_OUT;
CTP_INIT_SET;
CTP_INIT_SET;
IIC_SDA(1);
IIC_SCL(1);
// CTP_RST(1);
// Delayms(10);
// CTP_RST(0);
// Delayms(200);
// CTP_RST(1);
CTP_INIT_CLR;
Delayms(20);
CTP_INIT_SET;
Delayms(50);
CTP_INIT_CLR;
CTP_INIT_INT;
Delayms(100);
ctp.pointnum = 0;
}
void Delayus(int t)
{
int i = 0;
while(t--)
for(i = 100; i > 0; i--);
}
void IIC_START(void)
{
LCD_SDA_OUT;
// SDA_OUTPUT; //sda线输出
IIC_SDA(1);
IIC_SCL(1);
Delayus(4);
IIC_SDA(0);//START:when CLK is high,DATA change form high to low
Delayus(4);
IIC_SCL(0);//钳住I2C总线,准备发送或接收数据
}
void IIC_STOP(void)
{
LCD_SDA_OUT;//sda线输出
IIC_SCL(0);
IIC_SDA(0);//STOP:when CLK is high DATA change form low to high
Delayus(4);
IIC_SCL(1);
IIC_SDA(1);//发送I2C总线结束信号
Delayus(4);
}
void send_nack()
{
IIC_SCL(0);
LCD_SDA_OUT;
IIC_SDA(1);
Delayus(4);
IIC_SCL(1);
Delayus(4);
IIC_SCL(0);
}
void send_ack()
{
IIC_SCL(0);
LCD_SDA_OUT;
IIC_SDA(0);
Delayus(4);
IIC_SCL(1);
Delayus(4);
IIC_SCL(0);
}
uint8_t SendByte(uint8_t b)
{
uint16_t i,k=128;
uint8_t t,ret;
t=b;
LCD_SDA_OUT;
for(i=0;i<8;i++){
if(t&0x80)IIC_SDA(1);
else IIC_SDA(0);
t<<=1;
Delayus(4); //对TEA5767这三个延时都是必须的
IIC_SCL(1);
Delayus(4);
IIC_SCL(0);
Delayus(4);
}
ret=0;
//LCD_SDA_INT;
LCD_SDA_INT;
IIC_SDA(1);
IIC_SCL(1);
Delayus(4);
while(--k){
if(GET_SDA()==0){
ret=1;
break;
}
}
IIC_SCL(0);
LCD_SDA_OUT;
return ret;
}
uint8_t ReadByte(uint8_t n)
{
uint8_t t,i;
t=0;
LCD_SDA_INT;
for(i=0;i<8;i++){
IIC_SCL(0);
Delayus(4);
IIC_SCL(1);
Delayus(4);
t<<=1;
if(GET_SDA())t|=1;
Delayus(4);
}
IIC_SCL(0);
Delayus(4);
LCD_SDA_OUT;
if(n)IIC_SDA(1);
else IIC_SDA(0);
IIC_SCL(1);
Delayus(4);
IIC_SCL(0);
return t;
}
uint8_t IIC_WriteByte(uint8_t RA,uint8_t data)
{
// uint8_t i;
IIC_START();
if(SendByte(PIXCIR_ID)==0){
IIC_STOP();
return 0;
}
if(SendByte(RA)==0){
IIC_STOP();
return 0;
}
if(SendByte(data)==0){
IIC_STOP();
return 0;
}
IIC_STOP();
return 1;
}
uint8_t IIC_Write(uint8_t RA,uint8_t* data,uint32_t len)
{
uint8_t i;
IIC_START();
if(SendByte(PIXCIR_ID)==0){
IIC_STOP();
return 0;
}
if(SendByte(RA)==0){
IIC_STOP();
return 0;
}
for(i=0;i<len;i++){
SendByte(data[i]);
}
IIC_STOP();
return 1;
}
uint8_t IIC_Read(uint32_t RA,uint8_t* data,uint32_t len)
{
uint8_t i;
IIC_START();
if(SendByte(PIXCIR_ID)==0){
IIC_STOP();
return 0;
}
if(SendByte(RA)==0){
IIC_STOP();
return 0;
}
IIC_START();
if(SendByte(PIXCIR_ID|1)==0){
IIC_STOP();
return 0;
}
for(i=0;i<len-1;i++){
data[i]=ReadByte(0);
Delayus(2);//8
}
data[len-1]=ReadByte(1);
IIC_STOP();
return len;
}
uint8_t ReadCtp(void)
{
uint8_t i;
if(IIC_Read(I2C_STARTTCH_READ, RdBuff, 32) != 32){
return 1;
}
// if(RdBuff[1]!= 0xaa || RdBuff[0] != 0xaa){
// return 1;
// }
ctp.pointnum = RdBuff[2]&0x0f;
if((ctp.pointnum==0) || (ctp.pointnum > 5))return 1;
ctp.gesture = RdBuff[4];
#if 0
for(i=0;i<ctp.pointnum;i++){
ctp.x[i] =1024 - (((RdBuff[3+6*i]&0x0f)<<8)|RdBuff[3+6*i+1]);
ctp.y[i] =600-((((RdBuff[3+6*i+2]&0x0f)<<8)|RdBuff[3+6*i+3])&0x7ff);
}
#else
for(i=0;i<ctp.pointnum;i++){
ctp.x[i] = (((RdBuff[3+6*i]&0x0f)<<8)|RdBuff[3+6*i+1]);
ctp.y[i] =((((RdBuff[3+6*i+2]&0x0f)<<8)|RdBuff[3+6*i+3])&0x7ff);
}
#endif
return 0;
}
const UINT32 c[5]={LCD_COLORREF(0xff,0,0), LCD_COLORREF(0,0xff,0), LCD_COLORREF(0,0,0xff), LCD_COLORREF(0xff,0xff,0), LCD_COLORREF(0,0xff,0xff)};
void ctptest_Dealkey(UINT16 input)
{
UINT16 i;
switch(input&0x00ff){
case 1:
for(i=0;i<ctp.pointnum;i++){
if(pre_ctp.x[i]!=0xFFF)
Fill_Circle(pre_ctp.x[i], pre_ctp.y[i], 9, c[i]);
Fill_Circle(ctp.x[i], ctp.y[i], 9, c[i]);
pre_ctp.x[i]=ctp.x[i];
pre_ctp.y[i]=ctp.y[i];
//ctp.pointnum = 1;
}
for(i=ctp.pointnum;i<5;i++){
if(pre_ctp.x[i]!=0xFFF){
Fill_Circle(pre_ctp.x[i], pre_ctp.y[i], 9, c[i]);
pre_ctp.x[i]=0xfff;
pre_ctp.y[i]=0xfff;
}
}
//ctp.pointnum = 0;
break;
}
}
/*============================================================================*/
/*============================================================================*/