From a037d80c5da0c19985fe684ac7e38a3f42471394 Mon Sep 17 00:00:00 2001 From: "quentin.perret" Date: Wed, 3 May 2023 09:14:11 +0200 Subject: [PATCH] add tsl2561 scipt --- .gitignore | 1 + Code-C/TSL2561.c | 66 ++++++++++++++++++++++++++++++++++++++++ Code-C/include/TSL2561.h | 12 ++++++++ 3 files changed, 79 insertions(+) create mode 100644 Code-C/TSL2561.c create mode 100644 Code-C/include/TSL2561.h diff --git a/.gitignore b/.gitignore index 3680055..e1e226f 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,7 @@ Code-C/main Code-C/exect Code-C/ctest Code-C/DartConfiguration.tcl +Code-C/tsl Makefile diff --git a/Code-C/TSL2561.c b/Code-C/TSL2561.c new file mode 100644 index 0000000..180358b --- /dev/null +++ b/Code-C/TSL2561.c @@ -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(); +} \ No newline at end of file diff --git a/Code-C/include/TSL2561.h b/Code-C/include/TSL2561.h new file mode 100644 index 0000000..8781491 --- /dev/null +++ b/Code-C/include/TSL2561.h @@ -0,0 +1,12 @@ +#include +#include +#include +#include +#include +#include + + +char *bus = "/dev/i2c-1"; + +void getLux(int file); +int init_TSL2561(char *bus);