Source file: /~heha/Mikrocontroller/Displays/utft/Kalender.zip/ILI9341/TouchScreen0.cpp

#include "TouchScreen.h"
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <util/delay.h>

// increase or decrease the touchscreen oversampling. This is a little different than you make think:
// 1 is no oversampling, whatever data we get is immediately returned
// 2 is double-sampling and we only return valid data if both points are the same
// 3+ uses insert sort to get the median value.
// We found 2 is precise yet not too slow so we suggest sticking with it!

#define NUMSAMPLES 2

EMPTY_INTERRUPT(PCINT0_vect); 

static void bitdelay() {_delay_us(0.2);}

static uint8_t spi(uint8_t b) {
 for (uint8_t i=0; i<8; i++) {
  if (b&0x80) PORTC|=0x01; else PORTC&=~0x01;
  bitdelay();
  PORTC|=0x02;
  bitdelay();
  b<<=1;
  PORTC&=~0x02;
  bitdelay();
  if (PINB&0x01) ++b;
 }
 return b;
}

void TouchScreen::Init() {
 PCMSK0|=0x02;	// PCINT0 heißmachen
 PORTC&=~0x03;	// Low
 DDRC |=0x03;	// Ausgänge
 PORTB|=0x03;	// Pull-Ups
 spi(0); spi(0); spi(0);
}

bool TouchScreen::isTouching() {
 return !(PINB&0x02);
}

uint16_t TouchScreen::readTouch(uint8_t b) {
 spi(b);
 _delay_us(1.5);
 uint16_t r=spi(0);
 return uint16_t(r<<8|spi(0))>>6;
}

// Hier: Nur für Argument -2: Sleep
bool TouchScreen::wait(int ms) {
 if (PINB&0x02) {	// Finger nicht drauf?
  PCIFR=0x01;	// Evtl. vorhandenen anhängigen Interrupt löschen
  uint8_t o=PCICR;
  PCICR|=0x01;	// Pegelwechselinterrupt für Touchscreen aktivieren
  sleep_cpu();
  PCICR=o;
 }
 return false;
}
/*
long TouchScreen::getPoint() {
  int x, y, z;
  int samples[NUMSAMPLES];
  uint8_t i, valid;
  

  valid = 1;

  pinMode(_yp, INPUT);
  pinMode(_ym, INPUT);
  
  *portOutputRegister(yp_port) &= ~yp_pin;
  *portOutputRegister(ym_port) &= ~ym_pin;
  //digitalWrite(_yp, LOW);
  //digitalWrite(_ym, LOW);
   
  pinMode(_xp, OUTPUT);
  pinMode(_xm, OUTPUT);
  //digitalWrite(_xp, HIGH);
  //digitalWrite(_xm, LOW);
  *portOutputRegister(xp_port) |= xp_pin;
  *portOutputRegister(xm_port) &= ~xm_pin;
   
   for (i=0; i<NUMSAMPLES; i++) {
     samples[i] = analogRead(_yp);
   }
#if NUMSAMPLES > 2
   insert_sort(samples, NUMSAMPLES);
#endif
#if NUMSAMPLES == 2
   if (samples[0] != samples[1]) { valid = 0; }
#endif
   x = (1023-samples[NUMSAMPLES/2]);

   pinMode(_xp, INPUT);
   pinMode(_xm, INPUT);
   *portOutputRegister(xp_port) &= ~xp_pin;
   //digitalWrite(_xp, LOW);
   
   pinMode(_yp, OUTPUT);
   *portOutputRegister(yp_port) |= yp_pin;
   //digitalWrite(_yp, HIGH);
   pinMode(_ym, OUTPUT);
  
   for (i=0; i<NUMSAMPLES; i++) {
     samples[i] = analogRead(_xm);
   }

#if NUMSAMPLES > 2
   insert_sort(samples, NUMSAMPLES);
#endif
#if NUMSAMPLES == 2
   if (samples[0] != samples[1]) { valid = 0; }
#endif

   y = (1023-samples[NUMSAMPLES/2]);

   // Set X+ to ground
   pinMode(_xp, OUTPUT);
   *portOutputRegister(xp_port) &= ~xp_pin;
   //digitalWrite(_xp, LOW);
  
   // Set Y- to VCC
   *portOutputRegister(ym_port) |= ym_pin;
   //digitalWrite(_ym, HIGH); 
  
   // Hi-Z X- and Y+
   *portOutputRegister(yp_port) &= ~yp_pin;
   //digitalWrite(_yp, LOW);
   pinMode(_yp, INPUT);
  
   int z1 = analogRead(_xm); 
   int z2 = analogRead(_yp);

   if (_rxplate != 0) {
     // now read the x 
     float rtouch;
     rtouch = z2;
     rtouch /= z1;
     rtouch -= 1;
     rtouch *= x;
     rtouch *= _rxplate;
     rtouch /= 1024;
     
     z = rtouch;
   } else {
     z = (1023-(z2-z1));
   }

   if (! valid) {
     z = 0;
   }

   return TSPoint(x, y, z);
}

int TouchScreen::readTouchY(void) {
   pinMode(_xp, INPUT);
   pinMode(_xm, INPUT);
   digitalWrite(_xp, LOW);
   digitalWrite(_xm, LOW);
   
   pinMode(_yp, OUTPUT);
   digitalWrite(_yp, HIGH);
   pinMode(_ym, OUTPUT);
   digitalWrite(_ym, LOW);
   
   return (1023-analogRead(_xm));
}


uint16_t TouchScreen::pressure(void) {
  // Set X+ to ground
  pinMode(_xp, OUTPUT);
  digitalWrite(_xp, LOW);
  
  // Set Y- to VCC
  pinMode(_ym, OUTPUT);
  digitalWrite(_ym, HIGH); 
  
  // Hi-Z X- and Y+
  digitalWrite(_xm, LOW);
  pinMode(_xm, INPUT);
  digitalWrite(_yp, LOW);
  pinMode(_yp, INPUT);
  
  int z1 = analogRead(_xm); 
  int z2 = analogRead(_yp);

  if (_rxplate != 0) {
    // now read the x 
    float rtouch;
    rtouch = z2;
    rtouch /= z1;
    rtouch -= 1;
    rtouch *= readTouchX();
    rtouch *= _rxplate;
    rtouch /= 1024;
    
    return rtouch;
  } else {
    return (1023-(z2-z1));
  }
}
*/
Detected encoding: UTF-80