You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
1.2 KiB
C
50 lines
1.2 KiB
C
#include <stdlib.h>
|
|
#include <stdio.h> //to print error
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h> //header file for standard I2c send and receive commands
|
|
#include <sys/ioctl.h> //setup I2c devices
|
|
#include <linux/i2c-dev.h> //definitions and structures
|
|
#include <i2c/smbus.h> //smbus commands
|
|
|
|
#include "BH1750_H.h"
|
|
|
|
//Declarations of functions
|
|
// Brightness:
|
|
int get_lux(int fd, double *lux){
|
|
|
|
// Establish the direction of the slave
|
|
if (ioctl(fd, I2C_SLAVE, BH1750_ADDR) < 0){
|
|
perror("Error to stablish the direction of I2C");
|
|
//close(fd);
|
|
return 1;
|
|
}
|
|
|
|
// Send the command for the mesaurment
|
|
if(i2c_smbus_write_byte(fd, BH1750_OTHR) < 0){
|
|
perror("Failed to send the command for mesaurment ");
|
|
//close(fd);
|
|
return 1;
|
|
}
|
|
|
|
usleep(180000);
|
|
|
|
char buffer[2];
|
|
__s32 res = i2c_smbus_read_i2c_block_data(fd, 0x00, sizeof(buffer), buffer);
|
|
if (res != 2){
|
|
perror("Error -1: Failed to read from the device");
|
|
return 1;
|
|
}
|
|
*lux = ((buffer[0] << 8) | buffer[1]) / 1.2;
|
|
return 0;
|
|
}
|
|
|
|
// Reset Sensor
|
|
/*int reset_BH1750(int fd){
|
|
if(0 > ioctl(fd, I2C_SLAVE, BH1750_ADDR)){
|
|
perror("Error -2: Failed to reset");
|
|
return -2;
|
|
}
|
|
i2c_smbus_write_byte(fd, BH1750_RESET);
|
|
return 0;
|
|
}*/ |