void Touchpanel_FT5X06::readTouchPoint(uint8_t addr, TouchPoint *tp)
{
  uint8_t b;

  b = i2cReadByte(addr++);
  tp->event = b >> 6;
  b &= 0x0F;
  tp->x = ((uint16_t)b) << 8;
  tp->x |= i2cReadByte(addr++);

  b = i2cReadByte(addr++);
  tp->id = b >> 4;
  b &= 0x0F;
  tp->y = ((uint16_t)b) << 8;
  tp->y |= i2cReadByte(addr);
}
Beispiel #2
0
int i2cMasterRead(uint8_t i2c_addr, uint8_t intaddr_size, uint32_t int_addr, uint8_t *data) {
    MUTEX_TAKE(i2c_mutex, -1); 
    pm_lock();
    i2cMasterConf(i2c_addr, intaddr_size, int_addr, I2CMASTER_READ);
    int rc = i2cReadByte(data);
    pm_unlock();
    MUTEX_GIVE(i2c_mutex); 
    return rc;
}
Status I2CGpioHardwareBus::Receive(byte& value)
{
    auto result = i2cReadByte(_i2cHandle);
    checkStatus(result, Status::ReceiveFail);

    value = byte(result & 0x000000FF);

    return Status::Ok;
}
// Reads x, y and z gyroscope registers
void Read_Gyro()
{
  unsigned char buff[6];
  unsigned short raw[3];
  unsigned char reg = GYRO_DATAREG;
  int i = 0;
  
  // Select device
  i2cSetAddress(GYRO_ADDRESS);
  
  // Read 6 bytes
  while(i<6&&reg<GYRO_DATAREG+6){
    
    buff[i] = i2cReadByte(reg);
    //printf("gyro[%x]:%x ",reg,buff[i]);
    //usleep(5000);
    reg++;
    i++; 
  }
  
  if (i == 6)  // All bytes received?
  {
    
    //ints are stored as 2-byte 2s-complements on an arduino so casting to int is sufficent to
    //retrieve 16bit 2s-complement values from sensor registers on an arduino. 
    //gyro1[0] = -1 * ((((int) buff[2]) << 8) | buff[3]);    // X axis (internal sensor -y axis)
    //gyro1[1] = -1 * ((((int) buff[0]) << 8) | buff[1]);    // Y axis (internal sensor -x axis)
    //gyro1[2] = -1 * ((((int) buff[4]) << 8) | buff[5]);    // Z axis (internal sensor -z axis)
    
    // MSB first, X Y reversed
    raw[0] = ((buff[2]) << 8) | buff[3];    // X axis (internal sensor -y axis)
    raw[1] = ((buff[0]) << 8) | buff[1];    // Y axis (internal sensor -x axis)
    raw[2] = ((buff[4]) << 8) | buff[5];    // Z axis (internal sensor -z axis)

    //Manual 2s-complement conversion on ARM v8
    //nasty bit of code can be optimized.
    //TODO: make it branch-less
    int k;
    for(k=0;k<3;k++){
        //Convert from twos complement
        if((raw[k] >> 15) == 1){
	  raw[k] = ~raw[k] + 1;
	  gyro[k] = raw[k];
	  gyro[k] *= -1;
	  //printf("negative");
        }else{
	  gyro[k] = raw[k];
	}
        //XXXX is the maximum value of an X-bit signed register
        //TODO: Scale based on Max value being read.
        //gyro2[k] = (float)16 * (gyro2[k]/(0x1FF));
	//printf("\n%f\n",gyro[k]);
    }
void Touchpanel_FT5X06::setup()
{
  uint8_t b, i;

  power = 0;
  mouseX = mouseY = 0;
  mouseButtonState = 0;
  mouseZoom = 0;

  // set analog pins to input
  pinMode(AXM, INPUT);
  pinMode(AXP, INPUT);
  pinMode(AYM, INPUT);
  pinMode(AYP, INPUT);
  // set interrupt pin to input
  pinMode(INT_PIN, INPUT);
  digitalWrite(INT_PIN, HIGH); // pull-up on

  // wait for startup (check chip vendor id)
  #if DEBUG > 0
    Serial.println(F("TP: init chip..."));
  #endif
  for(i=0; i < 5; i++)
  {
    b = i2cReadByte(REG_CIPHER);
    if(b == CHIP_VENDOR_ID)
    {
      break;
    }
    else
    {
      #if DEBUG > 0
        Serial.print(F("TP: Chip Vendor wrong 0x"));
        Serial.println(b, HEX);
      #endif
      #if USE_WATCHDOG > 0
        wdt_reset();
      #endif
      digitalWrite(LED_RED, HIGH);
      delay(250);
      digitalWrite(LED_RED, LOW);
      delay(250);
    }
  }

  // read settings
  #if DEBUG > 0
    b = i2cReadByte(REG_CIPHER);
    Serial.print(F("TP: Chip Vendor 0x"));
    Serial.println(b, HEX);
    b = i2cReadByte(REG_FWID);
    Serial.print(F("TP: FW 0x"));
    Serial.println(b, HEX);
    b = i2cReadByte(REG_FTCHIPID);
    Serial.print(F("TP: Chip ID 0x"));
    Serial.println(b, HEX);
    b = i2cReadByte(REG_DEVICE_MODE);
    Serial.print(F("TP: Device Mode 0x"));
    Serial.println(b, HEX);
    Serial.println(F("TP: Reg 0x80...0xE0"));
    for(i = 0x80; i <= 0xE0; i++)
    {
      Serial.print(i, HEX);
      Serial.print("  ");
      Serial.println(i2cReadByte(i));
    }
  #endif

  // write settings
  i2cWriteByte(REG_DEVICE_MODE, 0);          // Device Mode, 0x00 = normal operating, 0x40 = test
  i2cWriteByte(REG_MODE, 0);                 // Interrupt status to host, 0 = polling, 1 = trigger
  i2cWriteByte(REG_THGROUP, 35);             // Valid touching detect threshold
  i2cWriteByte(REG_ENTERMONITOR, 120);       // Delay to enter 'Monitor' status (s)

  /*
  i2cWriteByte(REG_THGROUP, 35);             // Valid touching detect threshold
  i2cWriteByte(REG_THPEAK, 60);              // Valid touching peak detect threshold
  i2cWriteByte(REG_THCAL, 140);              // Touch focus threshold
  i2cWriteByte(REG_THWATER, 211);            // Threshold when there is surface water
  i2cWriteByte(REG_THTEMP, 235);             // Threshold of temperature compensation
  i2cWriteByte(REG_THDIFF, 160);             // Touch difference threshold
  i2cWriteByte(REG_CTRL, 1);                 // Power Control Mode
  i2cWriteByte(REG_ENTERMONITOR, 200);       // Delay to enter 'Monitor' status (s)
  i2cWriteByte(REG_PERIODACTIVE, 6);         // Period of 'Active' status (ms) 3-14
  i2cWriteByte(REG_PERIODMONITOR, 40);       // Timer to enter ‘idle’ when in 'Monitor' (ms) 3-14
  */

  // check error register
  for(i=0; i < 10; i++)
  {
    b = i2cReadByte(REG_ERR);
    if(b == 0) // okay
    {
      on(); // touchpanel on
      break;
    }
    else // error
    {
      #if DEBUG > 0
        Serial.print(F("TP: error 0x"));
        Serial.println(b, HEX);
      #endif
    }
  }
}
void Touchpanel_FT5X06::loop()
{
  uint8_t b;
  static uint8_t slowdown = 0;

  if(!power)
    return;

  // check INT pin of touch controller, when no touch press is active/detected
  if(mouseButtonState == 0)
  {
    slowdown++;
    if((slowdown > 8) || (digitalRead(INT_PIN) == 0)) // INT low -> new data
    {
      slowdown = 0;
    }
    else // INT high -> no new data
    {
      return;
    }
  }

  b = i2cReadByte(REG_STATE);
  //#if DEBUG > 2
  //  Serial.print(F("TP: state 0x"));
  //  Serial.println(b, HEX);
  //#endif
  if(b == STATE_WORK)
  {
    b = i2cReadByte(REG_TD_STATUS);
    if((b != 0) && (b != 255)) // no touch points on 0 and 255
    {
      nrPoints = b & 0x0F; //Bit3:0
      readTouchPoint(REG_TOUCH_1, &touch[0]);
      // no multi touch support at the moment
      //readTouchPoint(REG_TOUCH_2, &touch[1]);
      //readTouchPoint(REG_TOUCH_3, &touch[2]);
      //readTouchPoint(REG_TOUCH_4, &touch[3]);
      //readTouchPoint(REG_TOUCH_5, &touch[4]);

      #if DEBUG > 1
        Serial.print(F("TP: Points 0x"));
        Serial.print(nrPoints, HEX);
        Serial.print(F(" ID 0x"));
        Serial.print(touch[0].id, HEX);
        Serial.print(F(" Event 0x"));
        Serial.println(touch[0].event, HEX);
      #endif

      if(nrPoints >= 1 && touch[0].id == 0 && (touch[0].event == 0 || touch[0].event == 2)) // put down or contact
      {
        mouseX = touch[0].x * TOUCHMAX / (settings.data.screenWidth - 1);
        mouseY = touch[0].y * TOUCHMAX / (settings.data.screenHeight - 1);

        if(settings.data.x0 != 0) // add x correction
        {
          int16_t x = settings.data.x0 + mouseX;
          mouseX = x;
        }
        if(settings.data.y0 != 0) // add y correction
        {
          int16_t y = settings.data.y0 + mouseY;
          mouseY = y;
        }

        b = i2cReadByte(REG_GESTURE); // GESTURE_MOVE_UP GESTURE_MOVE_LEFT GESTURE_MOVE_DOWN GESTURE_MOVE_RIGHT GESTURE_ZOOM_IN GESTURE_ZOOM_OUT
        #if DEBUG > 1
          Serial.print(F("TP: Gesture 0x"));
          Serial.println(b, HEX);
        #endif
        if(b == GESTURE_ZOOM_IN)
          mouseZoom = 1;
        else if(b == GESTURE_ZOOM_OUT)
          mouseZoom = -1;
        else
          mouseZoom = 0;

        mouseButtonDown();
      }
      else
      {
        mouseButtonUp();
      }
    }
    else
    {
      mouseButtonUp();
    }
  }
}
Beispiel #7
0
int main(int argc, char *argv[])
{
   int i;
   int r;
   int handle;
   char aout;
   unsigned char command[2];
   unsigned char value[4];
   unsigned char str[8];

   int j;
   int key;

   if (gpioInitialise() < 0) return 1;

   initscr();
   noecho();
   cbreak();
   nodelay(stdscr, true);
   curs_set(0);

   printw("PCF8591 + or - to change aout, any other key to quit.");

   mvaddstr(10, 0, "Brightness");
   mvaddstr(12, 0, "Temperature");
   mvaddstr(14, 0, "?");
   mvaddstr(16, 0, "Resistor");

   refresh();

   handle = i2cOpen(1, PCF8591_I2C_ADDR, 0);

   command[1] = 0;
   aout = 128;

   while (1)
   {
      for (i=0; i<4; i++)
      {
         command[1] = aout;
         command[0] = 0x40 | ((i + 1) & 0x03); // output enable | read input i

         i2cWriteDevice(handle, &command, 2);

         usleep(20000);

         // the read is always one step behind the selected input
         value[i] = i2cReadByte(handle);

         sprintf(str, "%3d", value[i]);
         mvaddstr(10+i+i, 12, str);
         value[i] = value[i] / 4;
         move(10 + i + i, 16);

         for(j = 0; j < 64; j++)
            if(j < value[i]) addch('*'); else addch(' ');
      }

      refresh();

      key = getch();

      if      ((key == '+') || (key == '=')) aout++;
      else if ((key == '-') || (key == '_')) aout--;
      else if  (key != -1)                   break;
   }

   endwin();
 
   i2cClose(handle);

   gpioTerminate();

   return (0);
}
void DataAquisition::run()
{
     //Variablendeklaration
     int handle = 0;
     int dataready = 0;

     int winkel_old= 999;

    while(1)
    {

         int dataX_MSB = 0;
         int dataX_LSB = 0;
         int dataY_MSB = 0;
         int dataY_LSB = 0;
         int dataZ_MSB = 0;
         int dataZ_LSB = 0;

         signed short int dataX = 0;
         signed short int dataY = 0;
         signed short int dataZ = 0;

         int winkel = 0;
         double tangens = 0;
         double dataX_ABS = 0;
         double dataZ_ABS = 0;

          //i2c

         handle = i2cOpen(1,0x55,0);

             i2cSwitchCombined(1);               // WICHTIG! Enable "Repeated Start"!!

             gpioWrite(13,1);                    // MMA8491_EN = 1
             gpioDelay(2000);                   // Ton (minimales Delay)

             i2cWriteByte(handle,0x00);
             dataready = i2cReadByte(handle);

             if((dataready & 0x08) == 0x08)      // Falls neue Daten Verfügbar
             {
                 dataX_MSB = i2cReadByteData(handle,0x01);
                 dataX_LSB = i2cReadByteData(handle,0x02);
                 dataY_MSB = i2cReadByteData(handle,0x03);
                 dataY_LSB = i2cReadByteData(handle,0x04);
                 dataZ_MSB = i2cReadByteData(handle,0x05);
                 dataZ_LSB = i2cReadByteData(handle,0x06);

                 dataX = dataX_LSB + (dataX_MSB << 8);
                 dataY = dataY_LSB + (dataY_MSB << 8);
                 dataZ = dataZ_LSB + (dataZ_MSB << 8);

                 //Absolutwert und Wandlung in Double
                 dataX_ABS = abs(dataX);
                 dataZ_ABS = abs(dataZ);

                 //Berechnung Tangens
                 tangens = (dataX_ABS)/(dataZ_ABS);

                 //Berechnung Winkel
                 winkel = (atan(tangens))*180 / PI;

                 //Unterscheidung Quadrant
                 if((dataZ >= 0) & (dataX >= 0))         // Rechts unten
                     winkel = winkel;
                 else if((dataZ <= 0) & (dataX >= 0))    // Rechts oben
                     winkel = 180 - winkel;
                 else if((dataZ <= 0) & (dataX <= 0))    // Links oben
                     winkel = 180 + winkel;
                 else if((dataZ >= 0) & (dataX <= 0))    // Links unten
                     winkel = 360 - winkel ;
             }

        i2cClose(handle);
        gpioWrite(13,0);                    // MMA8491_EN = 0

        if (winkel < 3 || winkel > 357)
        {
            winkel = 0;
        }


        //Filter gegen Stösse und Schläge
        if(winkel_old != 999)
        {
                if(winkel > (winkel_old+10))
                {
                    winkel = (winkel_old+10);
                }
                else if(winkel < (winkel_old-10))
                {
                    winkel = (winkel_old-10);
                }
        }

        if(fps == "2")
        {
            if(i%5 == 0)
            {
                angleArray[(i/5)] = winkel;
            }
        }
        else if(fps == "30")
        {
            angleArray[i] = winkel;
            angleArray[++i] = winkel;
            angleArray[++i] = winkel;
        }
        else
        {
            angleArray[i] = winkel;
        }
        winkel_old = winkel;
        i++;
        msleep(92);
    }
}