readtouchloc

ivanmason/gt9271/readtouchloc

No description
readtouchloc
@/readtouchloc
I2Cxod/i2c/i2c
TLOC@/touchlocation
NLOCnumber
UPDpulse
readtouchloc
NTOUCH
TOUCH
DONE
I2C
TLOC
NLOC
UPD
DONEpulse
TOUCHboolean
NTOUCHnumber
To use the node in your project you should have the ivanmason/gt9271 library installed. Use the “File → Add Library” menu item in XOD IDE if you don’t have it yet. See Using libraries for more info.

C++ implementation

struct State {
};

{{ GENERATED_CODE }}

void writeGT9271TouchRegister(Context ctx, uint16_t regAddr,uint8_t *val, uint16_t cnt)
{
    uint16_t i=0;
    auto wire = getValue<input_I2C>(ctx);
    wire->beginTransmission((uint8_t)0x14);
    wire->write( regAddr>>8 );  // register 0
    wire->write( regAddr);  // register 0 
	for(i=0;i<cnt;i++,val++)//data
	{
          wire->write( *val );  // value
	}
  uint8_t retVal = Wire.endTransmission(); 
}



uint8_t readGT9271TouchAddr(Context ctx, uint16_t regAddr, uint8_t * pBuf, uint8_t len )
{

    auto wire = getValue<input_I2C>(ctx);
    wire->beginTransmission((uint8_t)0x14);
    wire->write( regAddr>>8 );  // register 0
    wire->write( regAddr);  // register 0  
    uint8_t retVal = wire->endTransmission();

    uint8_t returned = wire->requestFrom((uint8_t)0x14, len);    // request 1 bytes from slave device #2

    uint8_t i;
    for (i = 0; (i < len) && wire->available(); i++)
    {
        pBuf[i] = wire->read();
    }

    return i;
}


void evaluate(Context ctx) {

    if (!isInputDirty<input_UPD>(ctx))
        return;

    auto pLoc = getValue<input_TLOC>(ctx);
    uint8_t num = getValue<input_NLOC>(ctx);
    uint8_t retVal;
    uint8_t i;
    uint8_t k;
    uint8_t  ss[1];
    do
    {

        //if (!pLoc) break; // must have a buffer
        if (!num)  break; // must be able to take at least one
         ss[0]=0;
         readGT9271TouchAddr(ctx, 0x814e, ss, 1);
        uint8_t status=ss[0];

        if ((status & 0x0f) == 0){
            retVal = 0;
            break;
        } // no points detected
        uint8_t hitPoints = status & 0x0f;

        //Serial.print("number of hit points = ");
        //Serial.println( hitPoints );

        uint8_t tbuf[32]; uint8_t tbuf1[32];uint8_t tbuf2[16];  
        readGT9271TouchAddr(ctx, 0x8150, tbuf, 32);
        readGT9271TouchAddr(ctx, 0x8150+32, tbuf1, 32);
        readGT9271TouchAddr(ctx, 0x8150+64, tbuf2,16);

        if(hitPoints<=4)
            {
              for (k=0,i = 0; (i <  4*8)&&(k < num); k++, i += 8)
              {
                pLoc.x[k] = tbuf[i+1] << 8 | tbuf[i+0];
                pLoc.y[k] = tbuf[i+3] << 8 | tbuf[i+2];
              }
            }
        if(hitPoints>4)
            {
               for (k=0,i = 0; (i <  4*8)&&(k < num); k++, i += 8)
              {
                pLoc.x[k] = tbuf[i+1] << 8 | tbuf[i+0];
                pLoc.y[k] = tbuf[i+3] << 8 | tbuf[i+2];
              }

              for (k=4,i = 0; (i <  4*8)&&(k < num); k++, i += 8)
              {
                pLoc.x[k] = tbuf1[i+1] << 8 | tbuf1[i+0];
                pLoc.y[k] = tbuf1[i+3] << 8 | tbuf1[i+2];
              }
            }

          if(hitPoints>8)
            {
                for (k=0,i = 0; (i <  4*8)&&(k < num); k++, i += 8)
              {
                pLoc.x[k] = tbuf[i+1] << 8 | tbuf[i+0];
                pLoc.y[k] = tbuf[i+3] << 8 | tbuf[i+2];
              }

              for (k=4,i = 0; (i <  4*8)&&(k < num); k++, i += 8)
              {
                pLoc.x[k] = tbuf1[i+1] << 8 | tbuf1[i+0];
                pLoc.y[k] = tbuf1[i+3] << 8 | tbuf1[i+2];
              }

              for (k=8,i = 0; (i <  2*8)&&(k < num); k++, i += 8)
              {
                pLoc.x[k] = tbuf2[i+1] << 8 | tbuf2[i+0];
                pLoc.y[k] = tbuf2[i+3] << 8 | tbuf2[i+2];
              }
            }


    retVal = hitPoints;

  } while (0);

    ss[0]=0;
    writeGT9271TouchRegister(ctx, 0x814e,ss,1);

    if(retVal == 0){
        emitValue<output_TOUCH>(ctx,0);
        emitValue<output_NTOUCH>(ctx,0);
    }
    else{
        emitValue<output_TOUCH>(ctx,1);
        emitValue<output_NTOUCH>(ctx,retVal);
    }
   emitValue<output_DONE>(ctx, 1);
}