Traitement-signal-plantes/Code-C/TSL2561.c

108 lines
2.3 KiB
C
Raw Normal View History

2023-05-03 09:14:11 +02:00
// Code from the following github repo: https://github.com/ControlEverythingCommunity/TSL2561/blob/master/C/TSL2561.c
2023-06-01 17:22:38 +02:00
#include "./Include/TSL2561.h"
2023-05-03 09:14:11 +02:00
2023-05-03 10:14:31 +02:00
void printSpectrum(int file)
2023-05-03 09:14:11 +02:00
{
// Read 4 bytes of data from register(0x0C | 0x80)
// ch0 lsb, ch0 msb, ch1 lsb, ch1 msb
char reg[1] = {0x0C | 0x80};
write(file, reg, 1);
char data[4] = {0};
2023-06-01 17:22:38 +02:00
if (read(file, data, 4) != 4)
2023-05-03 09:14:11 +02:00
{
printf("Erorr : Input/output Erorr \n");
}
else
{
// Convert the data
2023-06-01 17:22:38 +02:00
float ch0 = (data[1] * 256 + data[0]); // Full Spectrum(IR + Visible)
float ch1 = (data[3] * 256 + data[2]); // Infrared Value
2023-05-03 09:14:11 +02:00
// Output data to screen
2023-05-03 10:14:31 +02:00
printf("Full Spectrum(IR + Visible) : %.1f lux \n", ch0);
printf("Infrared Value : %.1f lux \n", ch1);
printf("Visible Value : %.1f lux \n", (ch0 - ch1));
}
}
float getFullSpectrum(int file)
{
// Read 4 bytes of data from register(0x0C | 0x80)
// ch0 lsb, ch0 msb, ch1 lsb, ch1 msb
char reg[1] = {0x0C | 0x80};
write(file, reg, 1);
char data[4] = {0};
2023-06-01 17:22:38 +02:00
if (read(file, data, 4) != 4)
2023-05-03 10:14:31 +02:00
{
printf("Erorr : Input/output Erorr \n");
}
else
{
// Convert the data
2023-06-01 17:22:38 +02:00
float ch0 = (data[1] * 256 + data[0]); // Full Spectrum(IR + Visible)
2023-05-03 10:14:31 +02:00
return ch0;
}
}
float getInfraredLight(int file)
{
// Read 4 bytes of data from register(0x0C | 0x80)
// ch0 lsb, ch0 msb, ch1 lsb, ch1 msb
char reg[1] = {0x0C | 0x80};
write(file, reg, 1);
char data[4] = {0};
2023-06-01 17:22:38 +02:00
if (read(file, data, 4) != 4)
2023-05-03 10:14:31 +02:00
{
printf("Erorr : Input/output Erorr \n");
}
else
{
// Convert the data
2023-06-01 17:22:38 +02:00
float ch1 = (data[3] * 256 + data[2]); // Infrared Value
2023-05-03 10:14:31 +02:00
return ch1;
}
2023-05-03 09:14:11 +02:00
}
int init_TSL2561(char *bus)
{
int file;
2023-06-01 17:22:38 +02:00
if ((file = open(bus, O_RDWR)) < 0)
2023-05-03 09:14:11 +02:00
{
printf("Failed to open the bus. \n");
exit(1);
}
// Get I2C device, TSL2561 I2C address is 0x39(57)
ioctl(file, I2C_SLAVE, 0x39);
// Select control register(0x00 | 0x80)
// Power ON mode(0x03)
char config[2] = {0};
config[0] = 0x00 | 0x80;
config[1] = 0x03;
write(file, config, 2);
// Select timing register(0x01 | 0x80)
// Nominal integration time = 402ms(0x02)
config[0] = 0x01 | 0x80;
config[1] = 0x02;
write(file, config, 2);
return file;
}
2023-06-01 17:22:38 +02:00
void TSL2561()
2023-05-03 09:14:11 +02:00
{
int fe = 1;
int file;
file = init_TSL2561(bus);
2023-06-01 17:22:38 +02:00
while (1)
2023-05-03 09:14:11 +02:00
{
2023-06-01 17:22:38 +02:00
sleep(1 / fe);
2023-05-03 10:14:31 +02:00
float a = getFullSpectrum(file);
float b = getInfraredLight(file);
2023-06-01 17:22:38 +02:00
printf("%.1f\n", a);
printf("%.1f\n", b);
2023-05-03 10:14:31 +02:00
printSpectrum(file);
2023-05-03 09:14:11 +02:00
}
}
2023-06-01 17:22:38 +02:00
void main()
{
TSL2561();
2023-05-03 09:14:11 +02:00
}