add tsl2561 scipt

This commit is contained in:
quentin.perret 2023-05-03 09:14:11 +02:00
parent 14107f0301
commit a037d80c5d
3 changed files with 79 additions and 0 deletions

1
.gitignore vendored
View file

@ -9,6 +9,7 @@ Code-C/main
Code-C/exect
Code-C/ctest
Code-C/DartConfiguration.tcl
Code-C/tsl
Makefile

66
Code-C/TSL2561.c Normal file
View file

@ -0,0 +1,66 @@
// Code from the following github repo: https://github.com/ControlEverythingCommunity/TSL2561/blob/master/C/TSL2561.c
#include "include/TSL2561.h"
void getLux(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};
if(read(file, data, 4) != 4)
{
printf("Erorr : Input/output Erorr \n");
}
else
{
// Convert the data
float ch0 = (data[1] * 256 + data[0]);
float ch1 = (data[3] * 256 + data[2]);
// Output data to screen
printf("Full Spectrum(IR + Visible) : %.2f lux \n", ch0);
printf("Infrared Value : %.2f lux \n", ch1);
printf("Visible Value : %.2f lux \n", (ch0 - ch1));
}
}
int init_TSL2561(char *bus)
{
int file;
if((file = open(bus, O_RDWR)) < 0)
{
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;
}
void TSL2561()
{
int fe = 1;
int file;
file = init_TSL2561(bus);
while(1)
{
sleep(1/fe);
getLux(file);
}
}
void main(){
TSL2561();
}

12
Code-C/include/TSL2561.h Normal file
View file

@ -0,0 +1,12 @@
#include <stdio.h>
#include <stdlib.h>
#include <linux/i2c-dev.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
char *bus = "/dev/i2c-1";
void getLux(int file);
int init_TSL2561(char *bus);