Example #1
0
int main(int argc, char **argv)
{
    HANDLE hDevice;
    u6CalibrationInfo caliInfo;

    //Opening first found U6 over USB
    if( (hDevice = openUSBConnection(-1)) == NULL )
        goto done;

    //Getting calibration information from U6
    if( getCalibrationInfo(hDevice, &caliInfo) < 0 )
        goto close;

    if( ConfigIO_example(hDevice) != 0 )
        goto close;

    //Stopping any previous streams
    StreamStop(hDevice);

    if( StreamConfig_example(hDevice) != 0 )
        goto close;

    if( StreamStart(hDevice) != 0 )
        goto close;

    StreamData_example(hDevice, &caliInfo);
    StreamStop(hDevice);

close:
    closeUSBConnection(hDevice);
done:
    return 0;
}
int main(int argc, char **argv)
{
  int socketFDA, socketFDB;
  ue9CalibrationInfo caliInfo;
  socketFDA = -1;
  socketFDB = -1;

  if(argc < 2)
  {
    printf("Please enter an ip address to connect to.\n");
    exit(0);
  }
  else if(argc > 2)
  {
    printf("Too many arguments.\nPlease enter only an ip address.\n");
    exit(0);
  }

  ipAddress = argv[1];

  if( (socketFDA = openTCPConnection(ipAddress, ue9_portA)) < 0)
    goto exit;

  doFlush(socketFDA);

  if( (socketFDB = openTCPConnection(ipAddress, ue9_portB)) < 0)
    goto close;

  if(getCalibrationInfo(socketFDA, &caliInfo) < 0)
    goto close;

  if(StreamConfig_example(socketFDA) != 0)
    goto close;

  if(StreamStart(socketFDA) != 0)
    goto close;

  StreamData_example(socketFDA, socketFDB, &caliInfo);
  StreamStop(socketFDA, 1);

close:
  if(closeTCPConnection(socketFDA) < 0)
    printf("Error: failed to close socket (portA)\n");
  if(closeTCPConnection(socketFDB) < 0)
    printf("Error: failed to close socket (portB)\n");
exit:
  return 0;
}
Example #3
0
void CEXIMic::TransferByte(u8 &byte)
{
	if (m_position == 0)
	{
		command = byte;	// first byte is command
		byte = 0xFF;	// would be tristate, but we don't care.
		m_position++;
		return;
	}

	int pos = m_position - 1;

	switch (command)
	{
	case cmdID:
		byte = exi_id[pos];
		break;

	case cmdGetStatus:
		if (pos == 0)
			status.button = Pad::GetMicButton(slot);

		byte = status.U8[pos ^ 1];
		
		if (pos == 1)
			status.buff_ovrflw = 0;
		break;

	case cmdSetStatus:
		{
		bool wasactive = status.is_active;
		status.U8[pos ^ 1] = byte;

		// safe to do since these can only be entered if both bytes of status have been written
		if (!wasactive && status.is_active)
		{
			sample_rate = rate_base << status.sample_rate;
			buff_size = ring_base << status.buff_size;
			buff_size_samples = buff_size / sample_size;

			UpdateNextInterruptTicks();
			
			StreamStart();
		}
		else if (wasactive && !status.is_active)
		{
			StreamStop();
		}
		}
		break;

	case cmdGetBuffer:
		{
		if (ring_pos == 0)
			StreamReadOne();
		
		byte = ring_buffer[ring_pos ^ 1];
		ring_pos = (ring_pos + 1) % buff_size;
		}
		break;

	default:
		ERROR_LOG(EXPANSIONINTERFACE,  "EXI MIC: unknown command byte %02x", command);
		break;
	}

	m_position++;
}
Example #4
0
//Reads the StreamData low-level function response in a loop.  All voltages from
//the stream are stored in the voltages 2D array.
int StreamData_example(HANDLE hDevice, ue9CalibrationInfo *caliInfo)
{
    uint16 voltageBytes, checksumTotal;
    int recChars, backLog, overflow;
    int i, j, k, m, packetCounter, currChannel, scanNumber;
    int totalPackets;        //The total number of StreamData responses read
    int numDisplay;          //Number of times to display streaming information
    int numReadsPerDisplay;  //Number of packets to read before displaying streaming information
    int readSizeMultiplier;  //Multiplier for the StreamData receive buffer size
    long startTime, endTime;

    packetCounter = 0;
    currChannel = 0;
    scanNumber = 0;
    totalPackets = 0;
    recChars = 0;
    numDisplay = 6;
    numReadsPerDisplay = 3;
    readSizeMultiplier = 10;

    /* Each StreamData response contains (16/NumChannels) * readSizeMultiplier
     * samples for each channel.
     * Total number of scans = (16 / NumChannels) * 4 * readSizeMultiplier * numReadsPerDisplay * numDisplay
     */
    double voltages[(16/NumChannels)*4*readSizeMultiplier*numReadsPerDisplay*numDisplay][NumChannels];
    uint8 recBuff[192*readSizeMultiplier];

    printf("Reading Samples...\n");

    startTime = getTickCount();

    for( i = 0; i < numDisplay; i++ )
    {
        for( j = 0; j < numReadsPerDisplay; j++ )
        {
            /* For USB StreamData, use Endpoint 2 for reads and 192 byte packets
             * instead of the 46.  The 192 byte response is 4 StreamData packet
             * responses of 48 bytes.
             * You can read the multiple StreamData responses of 192 bytes to help
             * improve streaming performance.  In this example this multiple is
             * adjusted by the readSizeMultiplier variable.
             */

            //Reading response from UE9
            recChars = LJUSB_Stream(hDevice, recBuff, 192*readSizeMultiplier);
            if( recChars < 192*readSizeMultiplier )
            {
                if( recChars == 0 )
                    printf("Error : read failed (StreamData).\n");
                else
                    printf("Error : did not read all of the buffer %d (StreamData).\n", recChars);
                return -1;
            }

            overflow = 0;

            //Checking for errors and getting data out of each StreamData response
            for( m = 0; m < 4*readSizeMultiplier; m++ )
            {
                totalPackets++;
                checksumTotal = extendedChecksum16(recBuff + m*48, 46);
                if( (uint8)((checksumTotal >> 8) & 0xff) != recBuff[m*48 + 5] )
                {
                    printf("Error : read buffer has bad checksum16(MSB) (StreamData).\n");
                    return -1;
                }

                if( (uint8)(checksumTotal & 0xff) != recBuff[m*48 + 4] )
                {
                    printf("Error : read buffer has bad checksum16(LSB) (StreamData).\n");
                    return -1;
                }

                checksumTotal = extendedChecksum8(recBuff + m*48);
                if( checksumTotal != recBuff[m*48] )
                {
                    printf("Error : read buffer has bad checksum8 (StreamData).\n");
                    return -1;
                }

                if( recBuff[m*48 + 1] != (uint8)(0xF9) || recBuff[m*48 + 2] != (uint8)(0x14) || recBuff[m*48 + 3] != (uint8)(0xC0) )
                {
                    printf("Error : read buffer has wrong command bytes (StreamData).\n");
                    return -1;
                }

                if( recBuff[m*48 + 11] != 0 )
                {
                    printf("Errorcode # %d from StreamData read.\n", (unsigned int)recBuff[11]);
                    return -1;
                }

                if( packetCounter != (int) recBuff[m*48 + 10] )
                {
                    printf("PacketCounter does not match with with current packet count (StreamData).\n");
                    return -1;
                }

                backLog = recBuff[m*48 + 45]&0x7F;

                //Checking MSB for Comm buffer overflow
                if( (recBuff[m*48 + 45] & 128) == 128 )
                {
                    printf("\nComm buffer overflow detected in packet %d\n", totalPackets);
                    printf("Current Comm backlog: %d\n", recBuff[m*48 + 45]&0x7F);
                    overflow = 1;
                }

                for( k = 12; k < 43; k += 2 )
                {
                    voltageBytes = (uint16)recBuff[m*48 + k] + (uint16)recBuff[m*48 + k+1]*256;
                    getAinVoltCalibrated(caliInfo, (uint8)(0x00), ainResolution, voltageBytes, &(voltages[scanNumber][currChannel]));
                    currChannel++;
                    if( currChannel > 3 )
                    {
                        currChannel = 0;
                        scanNumber++;
                    }
                }

                if( packetCounter >= 255 )
                    packetCounter = 0;
                else
                    packetCounter++;

                //Handle Comm buffer overflow by stopping, flushing and restarting stream
                if( overflow == 1 )
                {
                    printf("\nRestarting stream...\n");
                    doFlush(hDevice);
                    if( StreamConfig_example(hDevice) != 0 )
                    {
                        printf("Error restarting StreamConfig.\n");
                        return -1;
                    }

                    if( StreamStart(hDevice) != 0 )
                    {
                        printf("Error restarting StreamStart.\n");
                        return -1;
                    }
                    packetCounter = 0;
                    break;
                }
            }
        }

        printf("\nNumber of scans: %d\n", scanNumber);
        printf("Total packets read: %d\n", totalPackets);
        printf("Current PacketCounter: %d\n", ((packetCounter == 0) ? 255 : packetCounter-1));
        printf("Current Comm backlog: %d\n", backLog);

        for( k = 0; k < 4; k++ )
            printf("  AI%d: %.4f V\n", k, voltages[scanNumber - 1][k]);
    }

    endTime = getTickCount();
    printf("\nRate of samples: %.0lf samples per second\n", (scanNumber*NumChannels)/((endTime - startTime)/1000.0));
    printf("Rate of scans: %.0lf scans per second\n\n", scanNumber/((endTime - startTime)/1000.0));

    return 0;
}
//Reads the StreamData low-level function response in a loop.  All voltages from
//the stream are stored in the voltages 2D array.
int StreamData_example(int socketFDA, int socketFDB, ue9CalibrationInfo *caliInfo)
{
  uint8 *recBuff;
  double **voltages;
  int recChars, backLog, overflow, totalScans, ret;
  int i, k, m, packetCounter, currChannel, scanNumber;
  int totalPackets;        //The total number of StreamData responses read
  uint16 voltageBytes, checksumTotal;

  
  int numDisplay;          //Number of times to display streaming information
  int readSizeMultiplier;  //Multiplier for the StreamData receive buffer size
  long startTime, endTime;

  packetCounter = 0;
  currChannel = 0;
  scanNumber = 0;
  totalPackets = 0;
  recChars = 0;
  numDisplay = 6;
  readSizeMultiplier = 120;
  ret = 0;
  
  /* Each StreamData response contains (16/NumChannels) * readSizeMultiplier
   * samples for each channel.
   * Total number of scans = (16 / NumChannels) * readSizeMultiplier * numDisplay
   */
  totalScans = (16/NumChannels)*readSizeMultiplier*numDisplay;
  voltages = malloc(sizeof(double)*totalScans);
  for(i = 0; i < totalScans; i++)
    voltages[i] = malloc(sizeof(double)*NumChannels);

  recBuff = malloc(sizeof(uint8)*46*readSizeMultiplier);

  printf("Reading Samples...\n");

  startTime = getTickCount();

  for (i = 0; i < numDisplay; i++)
  {
    /* You can read the multiple StreamData responses of 46 bytes to help
     * improve throughput.  In this example this multiple is adjusted by the 
     * readSizeMultiplier variable.  We may not read 46 * readSizeMultiplier 
     * bytes per each recv call, but we will continue reading until we read
     * 46 * readSizeMultiplier bytes total.
     */
    recChars = 0;
    for(k = 0; k < 46*readSizeMultiplier; k += recChars)
    {
      //Reading response from UE9
      recChars = recv(socketFDB, recBuff + k, 46*readSizeMultiplier - k, 0);
      if(recChars == 0)
      {
        printf("Error : read failed (StreamData).\n");
        ret = -1;
        goto cleanmem;
      }
    }
      
    overflow = 0;

    //Checking for errors and getting data out of each StreamData response
    for (m = 0; m < readSizeMultiplier; m++)
    {
      totalPackets++;

      checksumTotal = extendedChecksum16(recBuff + m*46, 46);
      if( (uint8)((checksumTotal / 256) & 0xff) != recBuff[m*46 + 5])
      {
        printf("Error : read buffer has bad checksum16(MSB) (StreamData).\n");
        ret = -1;
        goto cleanmem;
      }

      if( (uint8)(checksumTotal & 0xff) != recBuff[m*46 + 4])
      {
        printf("Error : read buffer has bad checksum16(LBS) (StreamData).\n");
        ret = -1;
        goto cleanmem;
      }

      checksumTotal = extendedChecksum8(recBuff + m*46);
      if( checksumTotal != recBuff[m*46])
      {
        printf("Error : read buffer has bad checksum8 (StreamData).\n");
        ret = -1;
        goto cleanmem;
      }

      if( recBuff[m*46 + 1] != (uint8)(0xF9) || recBuff[m*46 + 2] != (uint8)(0x14) || recBuff[m*46 + 3] != (uint8)(0xC0) )
      {
        printf("Error : read buffer has wrong command bytes (StreamData).\n");
        ret = -1;
        goto cleanmem;
      }

      if(recBuff[m*46 + 11] != 0)
      {
        printf("Errorcode # %d from StreamData read.\n", (unsigned int)recBuff[11]);
        ret = -1;
        goto cleanmem;
      }

      if(packetCounter != (int)recBuff[m*46 + 10])
      {
        printf("PacketCounter (%d) does not match with with current packet count (%d) (StreamData).\n", packetCounter, (int)recBuff[m*46 + 10]);
        ret = -1;
        goto cleanmem;
      }

      backLog = recBuff[m*46 + 45] & 0x7F;

      //Checking MSB for Comm buffer overflow
      if( (recBuff[m*46 + 45] & 128) == 128)
      {
        printf("\nComm buffer overflow detected in packet %d\n", totalPackets);
        printf("Current Comm backlog: %d\n", recBuff[m*46 + 45] & 0x7F);
        overflow = 1;
      }

      for(k = 12; k < 43; k += 2)
      {
        voltageBytes = (uint16)recBuff[m*46 + k] + (uint16)recBuff[m*46 + k+1] * 256;
        binaryToCalibratedAnalogVoltage(caliInfo, (uint8)(0x00), ainResolution, voltageBytes, &(voltages[scanNumber][currChannel])); 
        currChannel++;
        if(currChannel > 3)
        {
          currChannel = 0;
          scanNumber++;
        }
      }

      if(packetCounter >= 255)
        packetCounter = 0;
      else
        packetCounter++;

      //Handle Comm buffer overflow by stopping, flushing and restarting stream
      if(overflow == 1)
      {
        printf("\nRestarting stream...\n");

        doFlush(socketFDA);
        closeTCPConnection(socketFDB);

        if( (socketFDB = openTCPConnection(ipAddress, ue9_portB)) < 0)
          goto cleanmem;

        if(StreamConfig_example(socketFDA) != 0)
        {
          printf("Error restarting StreamConfig.\n");
          ret = -1;
          goto cleanmem;
        }

        if(StreamStart(socketFDA) != 0)
        {
          printf("Error restarting StreamStart.\n");
          ret = -1;
          goto cleanmem;
        }
        packetCounter = 0;
        break;
      }
    }

    printf("\nNumber of scans: %d\n", scanNumber);
    printf("Total packets read: %d\n", totalPackets);
    printf("Current PacketCounter: %d\n", ((packetCounter == 0) ? 255 : packetCounter-1));
    printf("Current Comm backlog: %d\n", backLog);

    for(k = 0; k < 4; k++)
      printf("  AI%d: %.4f V\n", k, voltages[scanNumber - 1][k]);
  }

  endTime = getTickCount();
  printf("\nRate of samples: %.0lf samples per second\n", (scanNumber*NumChannels)/((endTime - startTime)/1000.0));
  printf("Rate of scans: %.0lf scans per second\n\n", scanNumber/((endTime - startTime)/1000.0));

cleanmem:
  free(recBuff);
  recBuff = NULL;
  for(i = 0; i < totalScans; i++)
  {
    free(voltages[i]);
    voltages[i] = NULL;
  }
  free(voltages);
  voltages = NULL;

  return ret;
}