int CI2C::InitI2C () {
	////Log_file->WriteTopic ("Init I2C bus", 1);
	m_bError = false;

	m_nCompassAdress = wiringPiI2CSetup (0x60); //initialisation of the cmps10
	if (m_nCompassAdress == -1) {
		g_pTracer->Trace (ERROR, "Failed to initialise the compass!");
		m_bError = true;
	}

	m_nLidarAdress = wiringPiI2CSetup (0x62); //initialisation of the LiDAR
	if (m_nLidarAdress == -1) {
		g_pTracer->Trace (ERROR, "Failed to initialise the LiDAR!");
		m_bError = true;
	}else{
		I2CWrite (m_nLidarAdress, 0, 0); //set LiDAR acquisition count (128 = default)
	}

	if (m_bError == false) {
		g_pTracer->Trace (NOTE, "I2C bus initialised.");
		return 1;
	}else{
		return -1;
	}
}
Beispiel #2
0
SHT3x::SHT3x(const uint8_t& i2cAddr) {
    _fd = wiringPiI2CSetup(i2cAddr);
    _header = i2cAddr << 1;
    if (_fd < 0) {
        throw std::runtime_error("Unable to connect");
    }
}
int main(void) {
   unsigned int nextTime;
   short flip;

   if (wiringPiSetup () == -1)
    {
      fprintf (stdout, "Unable to start wiringPi: %s\n", strerror (errno)) ;
      fflush(stdout);
      return 1;
    }
   i2c = wiringPiI2CSetup (0x39);
   if (i2c == -1)
    {
      fprintf (stdout, "Unable to start I2C wiringPi: %s\n", strerror (errno)) ;
      fflush(stdout);
      return 1;
    }

    wiringPiI2CWriteReg8(i2c, 0x80, 0x03);
    

    nextTime = millis() + TIME_DELAY+400;
    int x = 0;
    for (;x<500;x++){
      //startReading();
      flip = !flip;
      flipper(flip);
      while (millis() < nextTime) {
      }
      readSensor();
      nextTime+=TIME_DELAY;
    }
}
int main()
{
	wiringPiSetup();
	int fd = wiringPiI2CSetup(HTU21D_I2C_ADDR);
	if ( 0 > fd )
	{
		fprintf(stderr, "ERROR: Unable to access RabbitMax humidity sensor: %s\n", strerror (errno));
		exit (-1);
	}

	// Retrieve temperature and humidity
	double temperature = 0;
	double humidity = 0;
	if ( (0 > getHumidity(fd, &humidity)) || (0 > getTemperature(fd, &temperature)) )
	{
		fprintf(stderr, "ERROR: RabbitMax humidity sensor not found\n");
		exit(-1);
	}
	
	// Print temperature and humidity on the screen
	printf("RabbitMax Humidity and Temperature Sensor\n");
	printf("%5.2fC\n", temperature);
	printf("%5.2f%%rh\n", humidity);
	
	return 0;
}
Beispiel #5
0
static int get_fd(duda_request_t *dr)
{
    int devid;
    char *devid_str;
    devid_str = qs->get(dr, "address");
    if (devid_str == NULL) {
        return -1;
    }
    
    // convert hex or decimal
    if (strncmp(devid_str, "0x", 2) == 0) {
        devid = strtol(devid_str + 2, NULL, 16);
    } else {
        devid = atoi(devid_str);
    }
    
    // check range
    if (devid < 3 || devid > NI2C) {
        return -1;
    }
    
    // init if not yet
    if (i2cfd[devid] == -1) {
        i2cfd[devid] = wiringPiI2CSetup(devid);
    }

    return i2cfd[devid];
}
Beispiel #6
0
void main() {

	char *df = "/dev/input/js0";
	struct ps3ctls ps3dat;

	wiringPiSetup();
	softPwmCreate( 5,0,20); // start-0 10ms
	softPwmCreate( 6,0,20); // start-0 10ms
	softPwmCreate(26,0,20); // start-0 10ms
	softPwmCreate(27,0,20); // start-0 10ms
	softPwmCreate(28,0,20); // start-0 10ms
	softPwmCreate(29,0,20); // start-0 10ms
	softPwmCreate(14,0,20); // start-0 10ms
	softPwmCreate(23,0,20); // start-0 10ms
	softPwmCreate(24,0,20); // start-0 10ms
	softPwmCreate(25,0,20); // start-0 10ms
	
	pinMode(15,OUTPUT);
	digitalWrite(15,0);
	
	fds = wiringPiI2CSetup(0x40);	// PCA9685
	resetPCA9685(fds);
	setPCA9685Freq(fds,50);

	while(1) {
		if(!(ps3c_init(&ps3dat, df))) {

			do {
				if (ps3c_test(&ps3dat) < 0) break;
			} while (!(ps3c_input(&ps3dat)));
		
			ps3c_exit(&ps3dat);		
		};
	};
}
Beispiel #7
0
void initMagneto(void){
  int i;
  printf("Magnetometer Initializing..........\n");
  if(first_boot){
    pinMode(MAG_LED, OUTPUT);
    if( (mag.fd = wiringPiI2CSetup(HMC5883L_I2C)) < 0 )
      printf ("I2C connection failed: %s\n", strerror(errno));
    
    if( (wiringPiI2CWriteReg8(mag.fd, HMC5883L_REG_A, HIGH_FREQ8)) < 0 )
      printf("Write to HMC5883L_REG_A failed: %s/n", strerror(errno));

    if( (wiringPiI2CWriteReg8(mag.fd, HMC5883L_REG_B, SENSOR_GAIN)) < 0 )
      printf("Write to HMC5883L_REG_B failed: %s/n", strerror(errno));
   
    if( (wiringPiI2CWriteReg8(mag.fd, HMC5883L_MODE_REG, CONTINUOUS)) < 0)
      printf ("Write to HMC5883L_MODE_REG failed: %s/n", strerror(errno));
    first_boot=0;
  }
  mag_starting_up = 1;
  mag.reading = 0;
  mag.average = 0;
  mag.deviation = 0;
  mag.magnitude = 0;
  mag.oldest_sample = 0;
  for(i = 0 ; i < MAX_SAMPLES ; i++){
    readMagneto();
  }
  updateMagneto();
  printf("Magnetometer Initialized...........\n");
  mag_starting_up = 0;
}
Beispiel #8
0
int htu21dSetup (const int pinBase)
{
  int fd ;
  struct wiringPiNodeStruct *node ;
  uint8_t data ;
  int status ;

  if ((fd = wiringPiI2CSetup (I2C_ADDRESS)) < 0)
    return FALSE ;

  node = wiringPiNewNode (pinBase, 2) ;

  node->fd         = fd ;
  node->analogRead = myAnalogRead ;

// Send a reset code to it:

  data = 0xFE ;
  if (write (fd, &data, 1) != 1)
    return FALSE ;

  delay (15) ;

// Read the status register to check it's really there

  status = wiringPiI2CReadReg8 (fd, 0xE7) ;

  return (status == 0x02) ? TRUE : FALSE ;
}
int main(int argc, char **argv)
{
    int devieAddress = 0x55;
	int fd = wiringPiI2CSetup(devieAddress);
    
    if (fd == -1)
    {
        printf("I2C Bus file could not be opened\n");
    }
    else
    {
		printf("I2C Bus opened on FD: %d\n", fd);
        
        while (1)
        {   
            unsigned char byte[2];
            int res = read(fd, byte, 2);

            if (res == -1)
            {
                printf("I2C Device with address %d was not available\n", devieAddress);
            }
            else
            {
                int distanceInCM = (byte[0] << 8) | byte[1];
                printf("Distance: %dcm\n", distanceInCM);
            }
            
            delay(250);
        }    
    }

    return 0;
}
Beispiel #10
0
void main(int argc,char* argv[])
{
	int fd,i2cAddress,j,k;
	if(argc<2)
	{
		printf("No address entered\n");
		return;
	}
	i2cAddress=atoi(argv[1]);
	
	if((fd = wiringPiI2CSetup(i2cAddress))<0)
	{
		printf("could not connect to slave address\n");
		return;
	}
	printf("setup done\n");
	while(1)
	{
		for(j=0;j<16;j++)
		{
			printf("Sending %d\t",j);
			wiringPiI2CWriteReg8(fd,0,j);
			k=wiringPiI2CReadReg8(fd,2);
			printf("data read %d\n",k);
			delay(1000);
		}
	}
}
bool I2C_Interface::attach_fd(uint8_t i2c_address) {
	if ((0 <= i2c_address) & (i2c_address < 128)) {
		//Lock mutex for address acquire
		pthread_mutex_lock(&aMutex);
		
		//Grab fd from wiringPi
		fd[i2c_address] = wiringPiI2CSetup(i2c_address);
		
		//Unlock mutex for address acquire
		pthread_mutex_unlock(&aMutex);

		if (fd[i2c_address] == -1) {
			if (MLogger::is_error_enabled()) {
				printf("Couldn't get device %u fd.\n", i2c_address);
			}
			return true;
		} else {
			if (MLogger::is_info_enabled()) {
				printf("Got device %u fd.\n", i2c_address);
			}
			return false;
		}
	}
	else {
		if (MLogger::is_error_enabled()) {
			printf("Address %u is out of I2C range.\n", i2c_address);
		}
		return false;
	}
}
Beispiel #12
0
void HDC1000::Init()
{
// デバイスアドレス 
// i2cdetect -y 1 で調べられる
  DevAddr=0x40;
// 温度取得ポインタとConfigポインタのアドレス指定。
// 基本的にこのままでOK。今回は温度と湿度を同時取得するので
// 温度側だけでOK
  tempp=0x00;
  confp=0x02;
// デバイスの設定
// 2バイトの設定データを1バイトに分けて設定。
// 1=上位ビット側 2=下位ビット側
// 0x1234という設定データなら 1=0x12 2=0x34と設定
  devconf1=0x10;
  devconf2=0x00;

  unsigned char ConfData[3];
  ConfData[0]= confp;
  ConfData[1]= devconf1;
  ConfData[2]= devconf2;
  fd=wiringPiI2CSetup(DevAddr);
  /*	
  wiringPiSetupGpio();
  pinMode(RDPin,INPUT);
  */
  if(write(fd,ConfData,sizeof(ConfData))<0)
    {
      std::cerr <<"センサー設定エラー"<<std::endl;
    }
  //  return fd;
}
Beispiel #13
0
/**
  * Initialize an I2C channel to the specified address. Exits with an error
  * message if the initialization fails,
  *
  * @param addr The target address.
  * @return File Descriptor for the I2C channel
  */
int I2C_setup_fd(const int addr) {
  const int fd = wiringPiI2CSetup(addr);
  if (!fd) {
    syslog(LOG_EMERG, "Error %d on I2C initialization!", errno);
    exit(-1);
  }
  return fd;
}
Beispiel #14
0
int main(int argc, char** argv) {
  fd = wiringPiI2CSetup(I2C_ADDRESS);
  init();
  init();
  set_backlight(true);
  print("Please wait...",0);
  printf("Done\n");
}
void main(){
	fd = wiringPiI2CSetup(LCDAddr);
	init();
	write(0, 0, "Greetings!");
	write(1, 1, "From SunFounder");
	delay(2000);
	clear();
}
Beispiel #16
0
int main(){
   int fd = wiringPiI2CSetup(0x68);
   int secs  = wiringPiI2CReadReg8(fd, 0x00);
   int mins  = wiringPiI2CReadReg8(fd, 0x01);
   int hours = wiringPiI2CReadReg8(fd, 0x02);
   printf("The RTC time is %2d:%02d:%02d\n", hours, mins, secs);
   return 0;
}
int changeEncoderAddress(int fd, int newAddress)
{
	wiringPiI2CWriteReg8(fd, 0x4D, (2*newAddress));
	int newFd = wiringPiI2CSetup(newAddress);
	wiringPiI2CWrite(newFd, 0x4B);
	usleep(100000);
	wiringPiI2CWrite(newFd, 0x4A);
	usleep(100000);
	return newFd;
}
Beispiel #18
0
//Initialise la communication I2C avec l'appareil en AddrId
int I2C_Init(int AddrId)
{
   int fd;
   if( fd = wiringPiI2CSetup(0x12) )
      printf("I2C communication initialisé avec succés.\n");
   else
      fprintf(stderr,"I2C Init Error.  Errno is: ",strerror(errno));

   return fd; //Linux file handler, used by other function.
}
Beispiel #19
0
// Initialize wiring I2C interface to LidarLite
int lidarLite::lidar_init(bool dbg) {
    int fd;
    m_dbg=dbg;
    if (m_dbg) printf("LidarLite V0.1\n\n");
    fd = wiringPiI2CSetup(m_addr);
    if (fd != -1) {
        lidar_status(fd);  // Dummy request to wake up device
        usleep (100000);
    }
    return(fd);
}
Beispiel #20
0
void main()
{
	int GYRO_X, GYRO_Y, GYRO_Z, ACCEL_X, ACCEL_Y, ACCEL_Z;
	double G_RE_X, G_RE_Y, G_RE_Z, A_RE_X, A_RE_Y, A_RE_Z, ACCX, ACCY, ACCZ, GYROX, GYROY, GYROZ, XANGLE, YANGLE, ZANGLE;
	int sensor;

	sensor = wiringPiI2CSetup(ADDRESS);

	wiringPiI2CWriteReg8(sensor, 0x6B, 0);
	wiringPiI2CWriteReg8(sensor, 27, 0x00);

	while(1) {

		GYRO_X = wiringPiI2CReadReg8(sensor, 0x43);
		GYRO_X = GYRO_X << 8 | wiringPiI2CReadReg8(sensor, 0x44); // GYRO X
		G_RE_X = GYRO_Change(GYRO_X);

		GYRO_Y = wiringPiI2CReadReg8(sensor, 0x45);
		GYRO_Y = GYRO_Y << 8 | wiringPiI2CReadReg8(sensor, 0x46); // GYRO Y
		G_RE_Y = GYRO_Change(GYRO_Y);

		GYRO_Z = wiringPiI2CReadReg8(sensor, 0x47);
		GYRO_Z = GYRO_Z << 8 | wiringPiI2CReadReg8(sensor, 0x48); // GYRO Z
		G_RE_Z = GYRO_Change(GYRO_Z);

		ACCEL_X = wiringPiI2CReadReg8(sensor, 0x3B);
		ACCEL_X = ACCEL_X << 8 | wiringPiI2CReadReg8(sensor, 0x3C); // ACC X
		A_RE_X = ACCEL_Change(ACCEL_X);

		ACCEL_Y = wiringPiI2CReadReg8(sensor, 0x3D);
		ACCEL_Y = ACCEL_Y << 8 | wiringPiI2CReadReg8(sensor, 0x3E); // ACC Y
		A_RE_Y = ACCEL_Change(ACCEL_Y);

		ACCEL_Z = wiringPiI2CReadReg8(sensor, 0x3F);
		ACCEL_Z = ACCEL_Z << 8 | wiringPiI2CReadReg8(sensor, 0x40); // ACC Z
		A_RE_Z = ACCEL_Change(ACCEL_Z);


		ACCY = atan2(A_RE_Z, A_RE_X) * 180 / PI;
		ACCX = atan2(A_RE_Z, A_RE_Y) * 180 / PI;
		ACCZ = atan2(A_RE_Y, A_RE_X) * 180 / PI;

		GYROX = (double)G_RE_X / 131.0;
		GYROY = (double)G_RE_Y / 131.0;
		GYROZ = (double)G_RE_Z / 131.0;

		XANGLE = (0.93 * (XANGLE + (GYROX * DT)) + (0.07 * ACCX));
		YANGLE = (0.93 * (YANGLE + (GYROY * DT)) + (0.07 * ACCY));
		ZANGLE = (0.93 * (ZANGLE + (GYROZ * DT)) + (0.07 * ACCZ)); //NOT CORRECT

		printf("XANGLE : %10lf\t YANGLE : %10lf\tZANGLE : %10lf\n", XANGLE, YANGLE, ZANGLE);
		delay(5);
	}
}
Beispiel #21
0
int motorDrive(int motorNo, enum DriveMode driveSet,float vSetF){

  int fd,ret;
  unsigned char setValue[3];
  unsigned char vSet,drive,motorHex;

  if(motorNo==0){
    motorHex=DRV8830_0;
  }else if(motorNo==1){
    motorHex=DRV8830_1;
  }else{
    motorHex=DRV8830_0;
  }

  switch(driveSet){
  case Stanby:
    drive=STANBY;
    break;
  case NegativeDrive:
    drive=NEG_ROT;
    break;
  case PositiveDrive:
    drive=POS_ROT;
    break;
  case Break:
    drive=BREAK;
    break;
  default :
    printf("Operand Error!!\n");
    return 1;
  }
  
  if(vSetF < 0.48 || vSetF > 5.06){
    printf("vSet=%lf Error!!\n",vSetF);
    return 1;
  }
  //printf("vSetF= %lf\n",vSetF);
  vSet=((int)(vSetF * 100)) / 8;  
  //printf("vSet= %x\n",vSet);
  
  setValue[0]=CONTROL_REG;
  setValue[1] = ( vSet << 2 )  | drive;  
  fd = wiringPiI2CSetup(motorHex);  
  ret = write(fd, setValue, 2);
  
  if (ret < 0) {
    printf("error: set configuration value\n");
    return 1;
  }
  
  printf("OK!\n");
  return 0;

}
Beispiel #22
0
 // Initialize wiring I2C interface to LidarLite
 int lidar_init(bool dbg) {
         int fd;
         _dbg = dbg;
         if (_dbg) printf("LidarLite V0.1\n\n");
         fd = wiringPiI2CSetup(LIDAR_LITE_ADRS);
         if (fd != -1) {
             lidar_status(fd);  // Dummy request to wake up device
             delay (100);
             }
         return(fd);
         }        
bool ofApp::setupOrient3d() {
    pullUpDnControl(8, PUD_UP);
    pullUpDnControl(9, PUD_UP);
    if ( (myOrient3d = wiringPiI2CSetup(0x60)) == -1) {
        printf("error initializing i2c for orient3d!\n");
        return false;
    }
    else {
        printf("orient3d: i2c init successful\n");
        return true;
    }
}
static void writeCommand(byte reg, byte value) {
//    Wire.beginTransmission((uint8_t)BMP085_ADDRESS);
//#if ARDUINO >= 100
//    Wire.write((uint8_t)reg);
//    Wire.write((uint8_t)value);
//#else
//    Wire.send(reg);
//    Wire.send(value);
//#endif
//    Wire.endTransmission();
    int fd = wiringPiI2CSetup(BMP085_ADDRESS);
    wiringPiI2CWriteReg8(fd, reg, value);
}
Beispiel #25
0
int main(int argc , char **argv) {
	if((fd=wiringPiI2CSetup(dID))<0)
		printf("error opening i2c channel\n");

	int e = wiringPiI2CWrite(fd, 0x51);

	usleep(100000);

	int r = wiringPiI2CReadReg16(fd, 0xe1);

	int val = (r >> 8) & 0xff | (r << 8) & 0x1;
	printf("%d\n", val);

}
Beispiel #26
0
void ScanI2CBus() {
	int i;
	char buff[200];
	// najpierw szukamy expanderów i2c na pcf8574
	for (i = 0; i < 4; i++) {	// zakładamy 4 ekspandery max
		hardware.i2c_PCF8574[i].fd = wiringPiI2CSetup (PCF8574_BASE_ADDR+i);
		// nie ma innego (?) sposobu na potwierdzenie czy urządzenie istnieje niż próba zapisu
		hardware.i2c_PCF8574[i].state = wiringPiI2CWrite (hardware.i2c_PCF8574[i].fd, wiringPiI2CRead(hardware.i2c_PCF8574[i].fd)); 
		if (hardware.i2c_PCF8574[i].state != -1) {
			sprintf(buff,"Wykryty osprzęt: Expander pcf8574 o adresie %#x",PCF8574_BASE_ADDR+i);
			Log(buff,E_DEV);
			// dodatkowe porty trzeba zarejestrować
			pcf8574Setup (PCF8574_BASE_PIN+i*8,PCF8574_BASE_ADDR+i) ;
		}
		
	}
	//sprawdzamy czy jest obecne MinipH
	hardware.i2c_MinipH.fd = wiringPiI2CSetup(MINIPH_ADDR);
	hardware.i2c_MinipH.state = wiringPiI2CReadReg16(hardware.i2c_MinipH.fd, 0 );
	if (hardware.i2c_MinipH.state != -1) {
		Log("Wykryty osprzęt: Mostek pomiarowy MinipH",E_DEV);
	}
}
void Controller::i2cComm() {
	int fd = wiringPiI2CSetup(3);
	if(fd > -1) {
		int reg, data, op, resp;
		while(1) {
			cout << "read (0) / write(1): ";
			cin >> op;
			if(op == 1) {
				cout << "write register: ";
				cin >> reg;
				cout << "write data: ";
				cin >> data;
				resp = wiringPiI2CWriteReg8(fd, reg, data);
				dlog << "response status: " << resp;
			} else if (op==2) {
Beispiel #28
0
int main(void)
{
	int device = wiringPiI2CSetup(address);

	if (device < 0)
	{
		return 1;
	}

	int data = wiringPiI2CReadReg8(device, 0x01);

	printf("%d\n", data);

	return 0;
}
Beispiel #29
0
int main(int argc, char* argv[])
{
	if (argc != 2)
	{
		std::cerr << "Usage: TCP_echo_server <port>\n";
		return -1;
	}
	wiringPiSetup();
	wiringPiI2CSetup(I2C_TEMP_ADDRESS);
	boost::thread workerThread(listener_loop, std::atoi(argv[1])); // Creates a thread that loops to accept sockets

	workerThread.join();
	
	return 0;
}
Beispiel #30
0
int mychecksum(unsigned char start_byte, unsigned char end_byte)
{
	int sum = 0;
	int counter;
	int cc_base;
	for (counter = start_byte; counter <end_byte; counter++)
	sum = (A50[counter] + sum);
	sum = sum & 0x0ff;
	cc_base = A50[end_byte]; //sum stored in address 63 or 95.
	if (start_byte == 0x0) printf("\ncc_base = %x, sum = %x",cc_base,sum);
	else printf("\nextended cc_base = %x and sum = %x\n",cc_base,sum);
		if (cc_base != sum && write_checksum){
			printf("\nCheck Sum failed, Do you want to write the"
			       "	checksum value %x to address byte \"%x\" ?"
			       "	(Y/N)", sum, end_byte);
			int ch = 0;
			ch = getchar();
			getchar();
			if ((ch == 'Y') || (ch == 'y')) {
#ifndef BEAGLEBONE
				xio = wiringPiI2CSetup (0x50);
				if (xio < 0){
				fprintf (stderr, "xio: Unable to initialise I2C: %s\n",
                                strerror (errno));
				return 1;
				}
#else
                xio = open(filenm, O_RDWR);
                if (xio < 0) {
                  fprintf (stderr, "Unable to open device: %s\n", filenm);
                  return(1);
                }

                if (ioctl(xio, I2C_SLAVE, 0x50) < 0) {
                    fprintf (stderr, "xio: Unable to initialise I2C: %s\n", strerror (errno));
                    return(1);
                }
#endif
				printf("end_byte = %x and sum = %x - yes\n",end_byte, sum);
#ifndef BEAGLEBONE
				wiringPiI2CWriteReg8(xio, end_byte, sum);
#else
                i2c_smbus_write_byte_data(xio, end_byte, sum);
#endif
			} else printf("nothing written\n");
		}
		return 0;
}