Ejemplo n.º 1
0
/** Read a single bit from an 8-bit device register.
 * @param devAddr I2C slave device address
 * @param regAddr Register regAddr to read from
 * @param bitNum Bit position to read (0-7)
 * @param data Container for single bit value
 * @return Status of read operation (true = success)
 */
int8_t I2Cdev::readBit(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint8_t *data) {
  bcm2835_i2c_setSlaveAddress(devAddr);
  sendBuf[0] = regAddr;
  uint8_t response = bcm2835_i2c_write_read_rs(sendBuf, 1, recvBuf, 1);
  *data = recvBuf[1] & (1 << bitNum);
  return response == BCM2835_I2C_REASON_OK ;
}
Ejemplo n.º 2
0
boolean ssd1306_init()
{
	ssd1306_lcdwidth  = 128;
	ssd1306_lcdheight = 64;
	_i2c_addr = 0x3C;
	cursor_y = cursor_x = 0;
	//textsize = 1;

	// De-Allocate memory for OLED buffer if any
	if (poledbuff)
		free(poledbuff);

	// Allocate memory for OLED buffer
	poledbuff = (uint8_t *) malloc ( (ssd1306_lcdwidth * ssd1306_lcdheight / 8 ));
	if (!poledbuff)
    		return false;

	// Init Raspberry PI GPIO
  	if (!bcm2835_init())
    		return false;

	// default OLED are using internal boost VCC converter
	vcc_type = SSD_Internal_Vcc;
	// Init & Configure Raspberry PI I2C
	if (bcm2835_i2c_begin()==0)
		return false;

	bcm2835_i2c_setSlaveAddress(_i2c_addr) ;

  	// Setup reset pin direction as output
  	bcm2835_gpio_fsel(OLED_I2C_RESET, BCM2835_GPIO_FSEL_OUTP);

	return ( true);
}
Ejemplo n.º 3
0
// initializer for I2C - we only indicate the reset pin and OLED type !
boolean ArduiPi_OLED::init(int8_t RST, uint8_t OLED_TYPE) 
{
  dc = cs = -1; // DC and chip Select do not exist in I2C
  rst = RST==OLED_PIN_DEFAULT ? DEF_I2C_RESET : RST;  // Reset Pin

  // Select OLED parameters
  if (!select_oled(OLED_TYPE))
    return false;

  // Init & Configure Raspberry PI I2C
  if (bcm2835_i2c_begin()==0)
    return false;
    
  bcm2835_i2c_setSlaveAddress(_i2c_addr) ;
    
  // Set clock to 400 KHz
  // does not seem to work, will check this later
  // bcm2835_i2c_set_baudrate(400000);
  bcm2835_i2c_set_baudrate(400000);

  // Setup reset pin direction as output
  if (rst >= 0)
    bcm2835_gpio_fsel(rst, BCM2835_GPIO_FSEL_OUTP);
  
  return ( true);
}
Ejemplo n.º 4
0
 /**
 *@brief Initializes the I2C peripheral
 *@param reg	Address of sensor register, address autoincrements
 *@param *buffer	Pointer to byte data buffer array
 *@param length	length of buffer array
 *@return none
 */
void I2C_ReadByteArray(unsigned char address,char reg,char *buffer,unsigned int  length)
{
	
	bcm2835_i2c_setSlaveAddress(address);
	
	bcm2835_i2c_read_register_rs(&reg,buffer,length);
}
Ejemplo n.º 5
0
/*!
 \param freq desired frequency. 40Hz to 1000Hz using internal 25MHz oscillator.
 */
void PCA9685::setPWMFreq(int freq) {

    uint8_t prescale_val = (CLOCK_FREQ / 4096 / freq)  - 1;
    bcm2835_i2c_set_baudrate(400000);
    bcm2835_i2c_setSlaveAddress(addr);

    pca_sendBuf[0] = MODE1;
    pca_sendBuf[1] = 0x10;
    bcm2835_i2c_write (pca_sendBuf, 2);

    pca_sendBuf[0] = PRE_SCALE;
    pca_sendBuf[1] = prescale_val;
    bcm2835_i2c_write (pca_sendBuf, 2);

    pca_sendBuf[0] = MODE1;
    pca_sendBuf[1] = 0x80;
    bcm2835_i2c_write (pca_sendBuf, 2);

    pca_sendBuf[0] = MODE2;
    pca_sendBuf[1] = 0x04;
    bcm2835_i2c_write (pca_sendBuf, 2);
    //i2c->write_byte(MODE1, 0x10); //sleep
    //i2c->write_byte(PRE_SCALE, prescale_val); // multiplyer for PWM frequency
    //i2c->write_byte(MODE1, 0x80); //restart
    //i2c->write_byte(MODE2, 0x04); //totem pole (default)
}
Ejemplo n.º 6
0
/*!
 \param led channel to set PWM value for
 \param on_value 0-4095 value to turn on the pulse
 \param off_value 0-4095 value to turn off the pulse
 */
void PCA9685::setPWM(uint8_t led, int on_value, int off_value) {
    bcm2835_i2c_set_baudrate(400000);
    bcm2835_i2c_setSlaveAddress(addr);

    pca_sendBuf[0] = LED0_ON_L + LED_MULTIPLYER * (led - 1);
    pca_sendBuf[1] = on_value & 0xFF;
    bcm2835_i2c_write (pca_sendBuf, 2);

    pca_sendBuf[0] = LED0_ON_H + LED_MULTIPLYER * (led - 1);
    pca_sendBuf[1] = on_value >> 8;
    bcm2835_i2c_write (pca_sendBuf, 2);

    pca_sendBuf[0] = LED0_OFF_L + LED_MULTIPLYER * (led - 1);
    pca_sendBuf[1] = off_value & 0xFF;
    //std::cout << (int)pca_sendBuf[1] << std::endl;
    bcm2835_i2c_write (pca_sendBuf, 2);

    pca_sendBuf[0] = LED0_OFF_H + LED_MULTIPLYER * (led - 1);
    pca_sendBuf[1] = off_value >> 8;
    //std::cout << (int)pca_sendBuf[1] << std::endl;
    bcm2835_i2c_write (pca_sendBuf, 2);

    //i2c->write_byte(LED0_ON_L + LED_MULTIPLYER * (led - 1), on_value & 0xFF);
    //i2c->write_byte(LED0_ON_H + LED_MULTIPLYER * (led - 1), on_value >> 8);
    //i2c->write_byte(LED0_OFF_L + LED_MULTIPLYER * (led - 1), off_value & 0xFF);
    //i2c->write_byte(LED0_OFF_H + LED_MULTIPLYER * (led - 1), off_value >> 8);
}
Ejemplo n.º 7
0
/*
* Read a single bit from an 8-bit device register.
* @param devAddr I2C slave device address
* @param regAddr Register regAddr to read from
* @param bitNum Bit position to read (0-7)
* @param data Container for single bit value
* @return Status of read operation (true = success)
*/
uint8_t i2c_read_bit(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint8_t *data) {
    bcm2835_i2c_setSlaveAddress(devAddr);
    i2c_send_buf[0] = regAddr;
    uint8_t response = bcm2835_i2c_write_read_rs(i2c_send_buf, 1, i2c_recv_buf, 1);
    *data = i2c_recv_buf[1] & (1 << bitNum);
    return response == BCM2835_I2C_REASON_OK ;
}
Ejemplo n.º 8
0
	void I2C_Bus::blockBus(const uint8_t slaveAddr)
	{
		bus_lock.lock();
		if (bus_slaveAddr != slaveAddr) {
			bus_slaveAddr = slaveAddr;
			bcm2835_i2c_setSlaveAddress(slaveAddr);
		}
	}
Ejemplo n.º 9
0
uint8_t MAG3110_READ_REGISTER(char reg)
{
	char *buf = malloc(sizeof(char));

    bcm2835_i2c_setSlaveAddress(MAG3110_I2C_ADDRESS);
    bcm2835_i2c_read_register_rs(&reg, buf, 1);
    return *buf;	
}
Ejemplo n.º 10
0
uint16_t SlushBoard::getTempRaw(void) {
	uint8_t buf[2] = { 0, 0 };

	bcm2835_i2c_setSlaveAddress(MAX1164_I2C_ADDRESS);
	bcm2835_i2c_setClockDivider(BCM2835_I2C_CLOCK_DIVIDER_626);
	(void) bcm2835_i2c_read((char *) buf, (uint32_t) 2);

	return (uint16_t) ((uint16_t) buf[0] << 8 | (uint16_t) buf[1]);
}
Ejemplo n.º 11
0
int main(int argc, char **argv) {

    printf("Running ... \n");
    
    // parse the command line
    if (comparse(argc, argv) == EXIT_FAILURE) return showusage (EXIT_FAILURE);

    if (!bcm2835_init())
    {
      printf("bcm2835_init failed. Are you running as root??\n");
      return 1;
    }
      
    // I2C begin if specified    
    if (init == I2C_BEGIN)
    {
      if (!bcm2835_i2c_begin())
      {
        printf("bcm2835_i2c_begin failed. Are you running as root??\n");
	return 1;
      }
    }
	  

    // If len is 0, no need to continue, but do I2C end if specified
    if (len == 0) {
         if (init == I2C_END) bcm2835_i2c_end();
	 printf("... done!\n");
         return EXIT_SUCCESS;
    }

    bcm2835_i2c_setSlaveAddress(slave_address);
    bcm2835_i2c_setClockDivider(clk_div);
    fprintf(stderr, "Clock divider set to: %d\n", clk_div);
    fprintf(stderr, "len set to: %d\n", len);
    fprintf(stderr, "Slave address set to: %d\n", slave_address);   
    
    if (mode == MODE_READ) {
    	for (i=0; i<MAX_LEN; i++) buf[i] = 'n';
    	data = bcm2835_i2c_read(buf, len);
    	printf("Read Result = %d\n", data);   
    	for (i=0; i<MAX_LEN; i++) {
    		if(buf[i] != 'n') printf("Read Buf[%d] = %x\n", i, buf[i]);
	}    
    }
    if (mode == MODE_WRITE) {
    	data = bcm2835_i2c_write(wbuf, len);
    	printf("Write Result = %d\n", data);
    }   

    // This I2C end is done after a transfer if specified
    if (init == I2C_END) bcm2835_i2c_end();   
    bcm2835_close();
    printf("... done!\n");
    return 0;
}
Ejemplo n.º 12
0
/**
 *@brief Reads a byte from a register
 *@param reg Address of sensor register.
 *@return val Byte value of register.
 */
unsigned char I2C_ReadByteRegister(unsigned char address, char reg)
{
	
	bcm2835_i2c_setSlaveAddress(address);
	
	char val = 0;
 
	bcm2835_i2c_read_register_rs(&reg,&val,1);
	
	return val;
}
Ejemplo n.º 13
0
 /**
 *@brief Readm result from a word length register
 *@param reg register to read from
 *@return val Word value of register
 */
unsigned int I2C_ReadWordRegisterRS(unsigned char address,char reg)
{
	
	bcm2835_i2c_setSlaveAddress(address);
	
	char cmd[1] = {reg}; 
	char receive[2] = {0};
	bcm2835_i2c_write_read_rs(cmd,1,receive,2);
	
	return (receive[0]<<8)|receive[1];
}
Ejemplo n.º 14
0
/**
 *@brief Read the value of a register that has already been select via the address pointer
 *@return Data Value of preset register
 */
unsigned int I2C_ReadWordPresetPointer(unsigned char address)
{
	
	bcm2835_i2c_setSlaveAddress(address);
	
	char val[2] = {0}; 
	bcm2835_i2c_read(val,2);
	unsigned int data = (val[0] << 8)|val[1];
	
	return data;
}
Ejemplo n.º 15
0
uint8_t MAG3110_WRITE_REGISTER(char reg, char value)
{
	/* care the end of string '\n' here */
	char data[2];
	data[0] = reg;
	data[1] = value;

    bcm2835_i2c_setSlaveAddress(MAG3110_I2C_ADDRESS);
    bcm2835_i2c_write(data, 2); 
    
}
Ejemplo n.º 16
0
static PyObject *
PyBCM2835_i2c_setSlaveAddress(PyObject *self, PyObject *args)
{
	int addr;

	if (!PyArg_ParseTuple(args,"i",&addr)) {
		return NULL;
	}

	bcm2835_i2c_setSlaveAddress(addr);
	Py_RETURN_NONE;
}
int readPage(int aI2CDevice, char aPage, unsigned char* aTagBuffer, int* aTagBufferLen)
{
  int ret = BCM2835_I2C_REASON_ERROR_NACK; // Just needs to be not BCM2835_I2C_REASON_OK
  int i;
  struct s_cmdResponse* resp;

  /* Set up command to select a tag */
  cmdBuffer[0] = 2;
  cmdBuffer[1] = SL030_READ_PAGE;
  cmdBuffer[2] = aPage;
  int tries = 0;
  while ((tries++ < 12) && (ret != BCM2835_I2C_REASON_OK))
  {
    bcm2835_i2c_begin();
  
    bcm2835_i2c_setSlaveAddress(SL030_ID);
    ret = bcm2835_i2c_write(cmdBuffer, 3);
#if DEBUG
    printf("write returned %d\n", ret);
#endif
    usleep(12000);
    memset(cmdBuffer, 0, CMD_BUF_LEN);
    ret = bcm2835_i2c_read(cmdBuffer, cmdBufferLen);
    bcm2835_i2c_end();
  }
#if DEBUG
  printf("read returned %d\n", ret);
  for (i = 0; i < cmdBufferLen; i++)
  {
    printf("%02x ", cmdBuffer[i]);
  }
  printf("\n");
#endif
  resp = (struct s_cmdResponse*)cmdBuffer;
#if DEBUG
  printf("Length: %d\n", resp->iLength);
  printf("Status: %d\n", resp->iStatus);
  printf("Status: %u\n", (char)resp->iStatus);
#endif
  if ((resp->iStatus == 0) && (resp->iLength > 2))
  {
    /* We found a tag! */
    /* Copy the ID across */
    /* drop 2 bytes from iLength: one each for command and status */
    *aTagBufferLen = (resp->iLength - 2 > MAX_TAG_ID_LENGTH ? MAX_TAG_ID_LENGTH : resp->iLength-2);
    memcpy(aTagBuffer, resp->iTag, *aTagBufferLen);
    return 1;
  }
  else
  {
    return 0;
  }
}
Ejemplo n.º 18
0
/**
 *@brief Writes a byte value to a register address
 *@param reg Address of sensor register.
 *@param data Data byte to be written on register.
 *@return none
 */
void I2C_WriteByteRegister(unsigned char address,unsigned char reg,unsigned char data)
{
	
	bcm2835_i2c_setSlaveAddress(address);
	
	unsigned char wr_buf[2];

	wr_buf[0] = reg;
	wr_buf[1] = data;

	bcm2835_i2c_write((const char *)wr_buf, 2);
}
Ejemplo n.º 19
0
int main(int argc, char **argv) {
	int first = 0x03, last = 0x77;
    int flags = 0;
    int version = 0;

	while (1 + flags < argc && argv[1 + flags][0] == '-') {
		switch (argv[1 + flags][1]) {
		case 'V': version = 1; break;
		case 'a':
			first = 0x00;
			last = 0x7F;
			break;
		default:
			fprintf(stderr, "Warning: Unsupported flag \"-%c\"!\n", argv[1 + flags][1]);
			//help(); // TODO
			exit(1);
		}
		flags++;
	}

    if (version) {
		fprintf(stderr, "i2cdetect version %s\n", VERSION);
		exit(0);
	}

	if (bcm2835_init() != 1) {
		fprintf(stderr, "bcm2835_init() failed\n");
		exit(1);
	}

	bcm2835_i2c_begin();

	bcm2835_i2c_setClockDivider(BCM2835_I2C_CLOCK_DIVIDER_2500); // 100kHz

	int address;

	for (address = 0x00; address <= 0x7F; address++) {
		if (address < first || address > last) {
			continue;
		}
		bcm2835_i2c_setSlaveAddress(address);
		if (bcm2835_i2c_write(NULL, 0) == 0) {
			printf("0x%.2X : 0x%.2X : %s\n", address, address << 1, lookup_device(address));
		}
	}

	bcm2835_i2c_end();

	bcm2835_close();

	return 0;
}
Ejemplo n.º 20
0
/**
 *@brief Initializes the I2C peripheral
 *@param address Address the I2C peripheral is communicating with.
 *@return none
 */
void I2C_Initialize(unsigned char address)
{
    if (!bcm2835_init())						//Configure I2C pins
    {
        printf("BCM libray error.\n");
    }
    bcm2835_i2c_end();		//Close I2C peripheral to reconfigure it

    bcm2835_i2c_begin();						//Set pins as I2C
    bcm2835_i2c_set_baudrate(baudrate);			//Set I2C baudrate
    bcm2835_i2c_setClockDivider(BCM2835_I2C_CLOCK_DIVIDER_2500);		//100 Khz
    bcm2835_i2c_setSlaveAddress(address);	//Set device address
}
Ejemplo n.º 21
0
/**
 *@brief Writes a word value (16 bit) to a register address.
 *@param reg Address of sensor register.
 *@param data Data word to be written on word size register.
 *@return none
 */
void I2C_WriteWordRegister(unsigned char address,unsigned char reg, unsigned char* data)
{
	
	bcm2835_i2c_setSlaveAddress(address);
	
	unsigned char wr_buf[3];
	
	wr_buf[0] = reg;
	wr_buf[1] = data[0];
	wr_buf[2] = data[1];

	bcm2835_i2c_write((const char *)wr_buf, 3);
}
Ejemplo n.º 22
0
int main(int argc, char **argv)
{
  unsigned char buf[6];
  unsigned char i,reg;
  double temp=0,calc=0, skytemp,atemp;
  FILE *flog;
  flog=fopen("mlxlog.csv", "a");
    bcm2835_init();
    bcm2835_i2c_begin();
    bcm2835_i2c_set_baudrate(25000);
    // set address...........................................................................................
    bcm2835_i2c_setSlaveAddress(0x5a);
    printf("\nOk, your device is working!!\n");
    while(1) {
      time_t t = time(NULL);
      struct tm tm = *localtime(&t);
      calc=0;
      reg=7;
      for(i=0;i<AVG;i++){
	bcm2835_i2c_begin();
	bcm2835_i2c_write (&reg, 1);
	bcm2835_i2c_read_register_rs(&reg,&buf[0],3);
	temp = (double) (((buf[1]) << 8) + buf[0]);
	temp = (temp * 0.02)-0.01;
	temp = temp - 273.15;
	calc+=temp;
	sleep(1);
      }
      skytemp=calc/AVG;
      calc=0;
      reg=6;
      for(i=0;i<AVG;i++){
	bcm2835_i2c_begin();
	bcm2835_i2c_write (&reg, 1);
	bcm2835_i2c_read_register_rs(&reg,&buf[0],3);
	temp = (double) (((buf[1]) << 8) + buf[0]);
	temp = (temp * 0.02)-0.01;
	temp = temp - 273.15;
	calc+=temp;
	sleep(1);
      }
      atemp=calc/AVG;
      printf("%02d-%02d %02d:%02d:%02d\n    Tambi=%04.2f C, Tobj=%04.2f C\n", tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,atemp,skytemp);
      fprintf(flog,"%04d-%02d-%02d %02d:%02d:%02d,%04.2f,%04.02f\n",tm.tm_year+1900, tm.tm_mon +1, tm.tm_mday,tm.tm_hour, tm.tm_min, tm.tm_sec,atemp,skytemp);
      fflush(flog);
      sleep(LOGTIME-(2*AVG));
    }
    printf("[done]\n");
}
int checkForTag(int aI2CDevice, unsigned char* aTagBuffer, int* aTagBufferLen)
{
  int ret = 0;
  int i;
  struct s_cmdResponse* resp;

  /* Set up command to select a tag */
  cmdBuffer[0] = 1;
  cmdBuffer[1] = SL030_CMD_SELECT;
  bcm2835_i2c_begin();

  bcm2835_i2c_setSlaveAddress(SL030_ID);
  ret = bcm2835_i2c_write(cmdBuffer, 2);
#if DEBUG
  printf("write returned %d\n", ret);
#endif
  usleep(30000);
  memset(cmdBuffer, 0, CMD_BUF_LEN);
  ret = bcm2835_i2c_read(cmdBuffer, cmdBufferLen);
  bcm2835_i2c_end();
#if DEBUG
  printf("read returned %d\n", ret);
  for (i = 0; i < cmdBufferLen; i++)
  {
    printf("%02x ", cmdBuffer[i]);
  }
  printf("\n");
#endif
  resp = (struct s_cmdResponse*)cmdBuffer;
#if DEBUG
  printf("Length: %d\n", resp->iLength);
  printf("Status: %d\n", resp->iStatus);
  printf("Status: %u\n", (char)resp->iStatus);
#endif
  /* We'll get a status of 128 for success on a Pi 1, and 0 for any later Pi models */
  if ( ((resp->iStatus == 128) || (resp->iStatus == 0)) && (resp->iLength > 2) )
  {
    /* We found a tag! */
    /* Copy the ID across */
    /* drop 3 bytes from iLength: one each for command, status and tag type */
    *aTagBufferLen = (resp->iLength - 3 > MAX_TAG_ID_LENGTH ? MAX_TAG_ID_LENGTH : resp->iLength-3);
    memcpy(aTagBuffer, resp->iTag, *aTagBufferLen);
    return 1;
  }
  else
  {
    return 0;
  }
}
Ejemplo n.º 24
0
/*
* Read multiple bits from an 8-bit device register.
* @param devAddr I2C slave device address
* @param regAddr Register regAddr to read from
* @param bitStart First bit position to read (0-7)
* @param length Number of bits to read (not more than 8)
* @param data Container for right-aligned value (i.e. '101' read from any bitStart position will equal 0x05)
* @return Status of read operation (true = success)
*/
uint8_t i2c_read_bits(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint8_t *data) {
    // 01101001 read byte
    // 76543210 bit numbers
    //    xxx   args: bitStart=4, length=3
    //    010   masked
    //   -> 010 shifted
    bcm2835_i2c_setSlaveAddress(devAddr);
    i2c_send_buf[0] = regAddr;
    uint8_t response = bcm2835_i2c_write_read_rs(i2c_send_buf, 1, i2c_recv_buf, 1);
    uint8_t b = (uint8_t) i2c_recv_buf[0];
    if (response == BCM2835_I2C_REASON_OK) {
        uint8_t mask = ((1 << length) - 1) << (bitStart - length + 1);
        b &= mask;
        b >>= (bitStart - length + 1);
        *data = b;
    }
Ejemplo n.º 25
0
//! Sets PCA9685 mode to 00
void PCA9685::reset() {
    bcm2835_init();
    bcm2835_i2c_set_baudrate(400000);
    bcm2835_i2c_setSlaveAddress(addr);

    pca_sendBuf[0] = MODE1;
    pca_sendBuf[1] = 0x00;
    bcm2835_i2c_write (pca_sendBuf, 2);

    pca_sendBuf[0] = MODE2;
    pca_sendBuf[1] = 0x04;
    bcm2835_i2c_write (pca_sendBuf, 2);

    //i2c->write_byte(MODE1, 0x00); //Normal mode
    //i2c->write_byte(MODE2, 0x04); //totem pole
}
Ejemplo n.º 26
0
IMU::IMU(void)
{	 

	char buf[1];
	bcm2835_init();
	bcm2835_i2c_begin();
	bcm2835_i2c_setSlaveAddress(MPU6050_ADDRESS);
	WriteRegister(SMPRT_DIV,0x07);	// Set the sample rate to 1000Hz - 8kHz/(7+1) = 1000Hz
	WriteRegister(CONFIG,0x00); // Disable FSYNC and set 260 Hz Acc filtering, 256 Hz Gyro filtering, 8 KHz sampling
	WriteRegister(GYRO_CONFIG,0x00); //250dpi
	WriteRegister(ACCEL_CONFIG,0x00); //2g resolution
	WriteRegister(PWR_MGMT_1,0x00); //sleep mode disabled

	
	regaddr[0]=WHO_AM_I;
	bcm2835_i2c_write(regaddr, 1);
	bcm2835_i2c_read(buf, 1);
	if(buf[0]==0x88)
	{
		printf("sensor config was successful WHO_AM_I: %x\n",buf[0]);
	}
	else
	{
		printf("sensor config was unsuccessful, %x\n",buf[0]);
	}
	
	bcm2835_i2c_end();
	///////////SETUP VARIABLES

	ReadGyr();
	ReadAccel();
	#ifdef RESTRICT_PITCH // Eq. 25 and 26
	  KFData.roll  = atan2(AData.y, AData.z) * RAD_TO_DEG;
	  KFData.pitch = atan(-AData.x / sqrt(AData.y * AData.y + AData.z * AData.z)) * RAD_TO_DEG;
	#else // Eq. 28 and 29
	  KFData.roll  = atan(AData.y / sqrt(AData.x * AData.x + AData.z * AData.z)) * RAD_TO_DEG;
	  KFData.pitch = atan2(-AData.x, AData.z) * RAD_TO_DEG;
	#endif

	kalmanX.setAngle(KFData.roll); // Set starting angle
  	kalmanY.setAngle(KFData.pitch);


}
Ejemplo n.º 27
0
/**
 *@brief Writes a buffer array to the registers
 *@param reg	Address of sensor register, address autoincrements
 *@param data	Pointer to byte data buffer array
 *@param length	length of buffer array
 *@return none
 */
void I2C_WriteByteArray(unsigned char address, char reg, char* data, unsigned int length)
{
	
	bcm2835_i2c_setSlaveAddress(address);
	
	char* wr_buf = (char*) malloc(sizeof(char) * length);
	if (wr_buf==NULL) 
	{
		printf("Error allocating memory!\n"); //print an error message
	}
	
	wr_buf[0] = reg;
	for(unsigned int i = 1;i<length;i++)
	{
		wr_buf[i] = data[i];
	}

	bcm2835_i2c_write((const char *)wr_buf, length);
}
Ejemplo n.º 28
0
int PCA9685::getPWM(uint8_t led) {
    int ledval = 0;

    bcm2835_i2c_set_baudrate(400000);
    bcm2835_i2c_setSlaveAddress(addr);

    char regH = LED0_OFF_H + LED_MULTIPLYER * (led-1);
    char regL = LED0_OFF_L + LED_MULTIPLYER * (led-1);

    bcm2835_i2c_read_register_rs(&regH,&pca_recvBuf[0],1);
    //std::cout << (int)pca_recvBuf[0] << std::endl;
    //ledval = i2c->read_byte(LED0_OFF_H + LED_MULTIPLYER * (led-1));
    ledval = pca_recvBuf[0] & 0xf;
    ledval <<= 8;
    bcm2835_i2c_read_register_rs(&regL,&pca_recvBuf[0],1);
    //std::cout << (int)pca_recvBuf[0] << std::endl;
    //ledval += i2c->read_byte(LED0_OFF_L + LED_MULTIPLYER * (led-1));
    ledval += pca_recvBuf[0];
    return ledval;
}
Ejemplo n.º 29
0
/*
*	Function: i2c_test
* Description: be used to test I2C related functions by using the PCF8574 module
*/
void i2c_test(void)
{
	uint8_t i2cWBuf[10] = {0x00};
	uint8_t i2cRBuf[10] = {0X00};
	printf("--------------->Test I2C With Pcf8574<--------------\n");
       i2cWBuf[0] = 0x40;
	bcm2835_i2c_begin();
	bcm2835_i2c_setSlaveAddress(PCF8574_ADDR); //set the slave address
       bcm2835_i2c_set_baudrate(400000);  //set the speed of the SDA 400kb/s
	bcm2835_i2c_write(i2cWBuf,1); 
	
	bcm2835_i2c_read(i2cRBuf, 1);
	if(i2cWBuf[0] == i2cRBuf[0])
		printf("I2C interface work well !...\n");
	else
		printf("I2C interface work bad!...\n");
	
	bcm2835_i2c_end();
	printf("==============Test Over Of I2C=================\n");		
}
Ejemplo n.º 30
0
int main(int argc, char **argv)
{


      if (!bcm2835_init())
	return 1;
	char temp[1];				//temporary values
	int ret;
	int ad[2];

	bcm2835_i2c_begin();
	bcm2835_i2c_setSlaveAddress(0x29);      // addr pin attached to ground
	bcm2835_i2c_set_baudrate(1000);         // Default

	temp[0] = 0xa0;				//select the control register
	bcm2835_i2c_write(temp,1);
	temp[0] = 0x03;				//Power up the device
   	bcm2835_i2c_write(temp,1);
	bcm2835_delay(500);

	bcm2835_i2c_read(temp,1);
	printf("%x - if 33 the device is turned on\n",temp[0]);

	temp[0] = 0xac;				//Channel 0 lower byte
    	bcm2835_i2c_write(temp,1);		
	bcm2835_i2c_read(temp,1);

	ad[1]= (int)temp[0];

        temp[0] = 0xad;				//channel 0 upper byte
        bcm2835_i2c_write(temp,1);
        bcm2835_i2c_read(temp,1); 

	ad[0] = (int)temp[0];
	printf("ad value:%d\n",ad[0]*256+ad[1]);

	bcm2835_i2c_end();
	bcm2835_close();
	
	return 0;
}