Пример #1
0
int getLux(int fd)
{
	wiringPiI2CWrite(fd,0x10);
	delay(LUXDELAY);
	int word = wiringPiI2CReadReg16(fd, 0x00);
	int lux=((word & 0xff00)>>8) | ((word & 0x00ff)<<8);
	return lux;
}
Пример #2
0
json_t * rpi_i2c_register16_n_get(duda_request_t *dr, int parameter)
{
    int fd = get_fd(dr);
    if (fd == -1) {
        return json->create_null();
    }
    
    return json->create_number((double)wiringPiI2CReadReg16(fd, parameter));
}
void readSensor(void) {
    //Must read 0x8C
    //gotta read i2c.readU16
    //reverse it useing revereseByteOrder
    int out = wiringPiI2CReadReg16(i2c, 0x8C);
    fprintf(stdout, "Value: %d\r", out);
    fflush(stdout);
    
   
}
Пример #4
0
std::string TemperatureSensor::getData(int i2c_fd)
{
	//int i2c_fd = wiringPiI2CSetup(I2C_TEMP_ADDRESS);
	int celcius = wiringPiI2CReadReg16(i2c_fd, I2C_TEMP_REGISTER_ADDRESS);
	
	std::stringstream ss;
	std::string dataString;
	ss << celcius;
	ss >> dataString;
	
	return dataString;
}
Пример #5
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);

}
Пример #6
0
int I2C_command(const int fd, const char command, const char data) {
  // check parameter range
  if ((command < 0) || (command > 0x07))
    return I2C_ERR_INVALIDARGUMENT;
  if ((data < 0) || (data > 0x0f))
    return I2C_ERR_INVALIDARGUMENT;
  
  // TODO check fd
  
    
  // build the I2C data byte
  // arguments have been checked, 
  // this cannot be negative or more than 8 bits
  unsigned char send = (command << 4) + data; 
  
  // calculate the parity
  char v = send;
  char c;
  for (c = 0; v; c++) 
    v &= v-1;
  c &= 1;

  // set parity bit  
  send += (c << 7);
  
  union I2C_result result;
  result.r = 0;

  // maximal number of tries
  int hops=20;

  // try for hops times until the result is not zero
  while (!result.c[0] && --hops) {
    // send command
    result.r = wiringPiI2CReadReg16(fd, send);

    // check for transmission errors: 2nd byte is inverted 1st byte
    const unsigned char c = ~result.c[0];
    if (result.c[1] != c) 
      // if no match, reset the result
      result.r = 0;
  }
  
  if (!hops)
    syslog(LOG_DEBUG, "Giving up transmission!\n");
  
  return result.c[0];
}
Пример #7
0
int main(int argc , char **argv) {

	// Initialize the ROS system.
	ros::init(argc, argv, "dist");
	// Establish this program as a ROS node.
	ros::NodeHandle nh("~");

	int sensorNumber = -1;

	nh.getParam("sensor", sensorNumber);
	int dID = getSensorAddress(sensorNumber);
	if (dID < 0) {
		printf("error getting sensor address\n");
                return -1;
	}
	if((fd=wiringPiI2CSetup(dID))<0) {
                printf("error opening i2c channel\n");
                return -1;
        }

	sensor_msgs::Range msg;

	msg.radiation_type = sensor_msgs::Range::ULTRASOUND;
  	msg.header.frame_id =  "ultrasound_ranger";
  	msg.field_of_view = 1;
  	msg.min_range = 0.20;
  	msg.max_range = MAX_READING/100;
	ros::Publisher pub = nh.advertise<sensor_msgs::Range> ("dist", 1000);
	ros::Rate rate(10);
	while(ros::ok()) {
		int e = wiringPiI2CWrite(fd, 0x51);
		rate.sleep();
		int r = wiringPiI2CReadReg16(fd, 0xe1);
		int val = (r >> 8) & 0xff | (r << 8) & 0x1;
		if (val <= MAX_READING) {
            msg.range = val/100.0;
	     	msg.header.stamp = ros::Time();
            pub.publish(msg);
            ROS_INFO_STREAM("Sending "<< msg.range);
		} else {
            ROS_INFO_STREAM("Not sending "<< val/100.0);
		}
		// Send a message to rosout with the details.
	}
}
static void read16(byte reg, uint16_t *value) {
//    Wire.beginTransmission((uint8_t)BMP085_ADDRESS);
//#if ARDUINO >= 100
//    Wire.write((uint8_t)reg);
//#else
//    Wire.send(reg);
//#endif
//    Wire.endTransmission();
//    Wire.requestFrom((uint8_t)BMP085_ADDRESS, (byte) 2);
//#if ARDUINO >= 100
//    *value = (Wire.read() << 8) | Wire.read();
//#else
//    *value = (Wire.receive() << 8) | Wire.receive();
//#endif
//    Wire.endTransmission();
    int fd = wiringPiI2CSetup(BMP085_ADDRESS);
    *value = wiringPiI2CReadReg16(fd, reg);
}
Пример #9
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);
	}
}
Пример #10
0
int main(int argc, char *argv[])
{
   int dev;
   if (wiringPiSetup() == -1) {
      printf("wiringPi init failed\n");
      return -1;
   }

   pullUpDnControl(8, PUD_UP);
   pullUpDnControl(9, PUD_UP);
   if ( (dev = wiringPiI2CSetup(0x1e)) == -1) {
      printf("error init i2c at 0x1e\n");
      return -1;
   }
   else
      printf("i2c init at 0x1e OK, dev = %i\n",dev);
   while (1)
   {
      int val = (int)wiringPiI2CReadReg16(dev,0);
      printf("val = %i\n", val);
   }
   return 0;
}
double getAccel(int fd, Axis axis, AScale scale)
{
    int reg = 0;
	switch(axis)
	{
		case X:
		reg = ACCEL_XOUT_H;
		break;

		case Y:
		reg = ACCEL_YOUT_H;
		break;

		case Z:
		reg = ACCEL_ZOUT_H;
		break;

		default:
		return -1;
		break;
	}

	return ((int16_t)wiringPiI2CReadReg16(fd, reg))*getARes(scale);
}
double getMag(int fd, Axis axis, MScale scale)
{
    int reg = 0;
	switch(axis)
	{
        case X:
        reg = AK8963_XOUT_H;
        break;

        case Y:
        reg = AK8963_YOUT_H;
        break;

        case Z:
        reg = AK8963_ZOUT_H;
        break;

        default:
        return -1;
        break;
	}

	return ((int16_t)wiringPiI2CReadReg16(fd, reg))*getMRes(scale);
}
double getRot(int fd, Axis axis, GScale scale)
{
    int reg = 0;
	switch(axis)
	{
        case X:
        reg = GYRO_XOUT_H;
        break;

        case Y:
        reg = GYRO_YOUT_H;
        break;

        case Z:
        reg = GYRO_ZOUT_H;
        break;

        default:
        return -1;
        break;
	}

	return ((int16_t)wiringPiI2CReadReg16(fd, reg))*getGRes(scale);
}
Пример #14
0
static int myAnalogRead (struct wiringPiNodeStruct *node, int pin)
{
  int chan = pin - node->pinBase ;
  int16_t  result ;
  uint16_t config = CONFIG_DEFAULT ;

  chan &= 7 ;

// Setup the configuration register

//	Set PGA/voltage range

  config &= ~CONFIG_PGA_MASK ;
  config |= node->data0 ;

//	Set sample speed

  config &= ~CONFIG_DR_MASK ;
  config |= node->data1 ;

//	Set single-ended channel or differential mode

  config &= ~CONFIG_MUX_MASK ;

  switch (chan)
  {
    case 0: config |= CONFIG_MUX_SINGLE_0 ; break ;
    case 1: config |= CONFIG_MUX_SINGLE_1 ; break ;
    case 2: config |= CONFIG_MUX_SINGLE_2 ; break ;
    case 3: config |= CONFIG_MUX_SINGLE_3 ; break ;

    case 4: config |= CONFIG_MUX_DIFF_0_1 ; break ;
    case 5: config |= CONFIG_MUX_DIFF_2_3 ; break ;
    case 6: config |= CONFIG_MUX_DIFF_0_3 ; break ;
    case 7: config |= CONFIG_MUX_DIFF_1_3 ; break ;
  }

//	Start a single conversion

  config |= CONFIG_OS_SINGLE ;
  config = __bswap_16 (config) ;
  wiringPiI2CWriteReg16 (node->fd, 1, config) ;

// Wait for the conversion to complete

  for (;;)
  {
    result =  wiringPiI2CReadReg16 (node->fd, 1) ;
    result = __bswap_16 (result) ;
    if ((result & CONFIG_OS_MASK) != 0)
      break ;
    delayMicroseconds (100) ;
  }

  result =  wiringPiI2CReadReg16 (node->fd, 0) ;
  result = __bswap_16 (result) ;

// Sometimes with a 0v input on a single-ended channel the internal 0v reference
//	can be higher than the input, so you get a negative result...

  if ( (chan < 4) && (result < 0) ) 
    return 0 ;
  else
    return (int)result ;
}
Пример #15
0
void *lm76Parse(void *param) {
	struct JsonNode *json = (struct JsonNode *)param;
	struct JsonNode *jsettings = NULL;
	struct JsonNode *jid = NULL;
	struct JsonNode *jchild = NULL;
	struct timeval tp;
	struct timespec ts;	
	struct lm76data_t *lm76data = malloc(sizeof(struct lm76data_t));	
	int y = 0, interval = 10, rc = 0;
	int temp_corr = 0;
	char *stmp = NULL;
	int firstrun = 1;

	lm76data->nrid = 0;
	lm76data->id = NULL;
	lm76data->fd = 0;
	
#ifndef __FreeBSD__
	pthread_mutex_t mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;        
    pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
#else
	pthread_mutex_t mutex;
	pthread_cond_t cond;
	pthread_mutexattr_t attr;

	pthread_mutexattr_init(&attr);
	pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
	pthread_mutex_init(&mutex, &attr);
#endif

	if((jid = json_find_member(json, "id"))) {
		jchild = json_first_child(jid);
		while(jchild) {
			if(json_find_string(jchild, "id", &stmp) == 0) {
				lm76data->id = realloc(lm76data->id, (sizeof(char *)*(size_t)(lm76data->nrid+1)));
				lm76data->id[lm76data->nrid] = malloc(strlen(stmp)+1);
				strcpy(lm76data->id[lm76data->nrid], stmp);
				lm76data->nrid++;
			}
			jchild = jchild->next;
		}
	}
	if((jsettings = json_find_member(json, "settings"))) {
		json_find_number(jsettings, "interval", &interval);
		json_find_number(jsettings, "temp-corr", &temp_corr);
	}
	json_delete(json);
#ifndef __FreeBSD__	
	lm76data->fd = realloc(lm76data->fd, (sizeof(int)*(size_t)(lm76data->nrid+1)));
	for(y=0;y<lm76data->nrid;y++) {
		lm76data->fd[y] = wiringPiI2CSetup((int)strtol(lm76data->id[y], NULL, 16));
	}
#endif
	pthread_cleanup_push(lm76ParseCleanUp, (void *)lm76data);
	
	while(lm76_loop) {	
		rc = gettimeofday(&tp, NULL);
		ts.tv_sec = tp.tv_sec;
		ts.tv_nsec = tp.tv_usec * 1000;
		if(firstrun) {
			ts.tv_sec += 1;
			firstrun = 0;
		} else {
			ts.tv_sec += interval;
		}

		pthread_mutex_lock(&mutex);
		rc = pthread_cond_timedwait(&cond, &mutex, &ts);
		if(rc == ETIMEDOUT) {
#ifndef __FreeBSD__	
			for(y=0;y<lm76data->nrid;y++) {
				if(lm76data->fd[y] > 0) {		
					int raw = wiringPiI2CReadReg16(lm76data->fd[y], 0x00);            
					float temp = ((float)((raw&0x00ff)+((raw>>12)*0.0625))*1000);

					lm76->message = json_mkobject();
					JsonNode *code = json_mkobject();
					json_append_member(code, "id", json_mkstring(lm76data->id[y]));
					json_append_member(code, "temperature", json_mknumber((int)temp+temp_corr));

					json_append_member(lm76->message, "code", code);
					json_append_member(lm76->message, "origin", json_mkstring("receiver"));
					json_append_member(lm76->message, "protocol", json_mkstring(lm76->id));

					pilight.broadcast(lm76->id, lm76->message);
					json_delete(lm76->message);
					lm76->message = NULL;
				} else {
					logprintf(LOG_DEBUG, "error connecting to lm76");
					logprintf(LOG_DEBUG, "(probably i2c bus error from wiringPiI2CSetup)");
					logprintf(LOG_DEBUG, "(maybe wrong id? use i2cdetect to find out)");
					sleep(1);
				}
			}
#endif
		}