From 8798202171f3ab2765e0e42a9988dc1f348cad0c Mon Sep 17 00:00:00 2001 From: "quentin.perret" Date: Wed, 3 May 2023 10:14:31 +0200 Subject: [PATCH] change tsl code to easily get values --- Code-C/TSL2561.c | 59 ++++++++++++++++++++++++++++++++++------ Code-C/getArray.h | 11 -------- Code-C/include/TSL2561.h | 4 ++- 3 files changed, 53 insertions(+), 21 deletions(-) delete mode 100644 Code-C/getArray.h diff --git a/Code-C/TSL2561.c b/Code-C/TSL2561.c index 180358b..152fa5d 100644 --- a/Code-C/TSL2561.c +++ b/Code-C/TSL2561.c @@ -1,7 +1,7 @@ // Code from the following github repo: https://github.com/ControlEverythingCommunity/TSL2561/blob/master/C/TSL2561.c #include "include/TSL2561.h" -void getLux(int file) +void printSpectrum(int file) { // Read 4 bytes of data from register(0x0C | 0x80) // ch0 lsb, ch0 msb, ch1 lsb, ch1 msb @@ -15,14 +15,51 @@ void getLux(int file) else { // Convert the data - float ch0 = (data[1] * 256 + data[0]); - float ch1 = (data[3] * 256 + data[2]); - + float ch0 = (data[1] * 256 + data[0]); //Full Spectrum(IR + Visible) + float ch1 = (data[3] * 256 + data[2]); //Infrared Value // 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)); - } + 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}; + if(read(file, data, 4) != 4) + { + printf("Erorr : Input/output Erorr \n"); + } + else + { + // Convert the data + float ch0 = (data[1] * 256 + data[0]); //Full Spectrum(IR + Visible) + 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}; + if(read(file, data, 4) != 4) + { + printf("Erorr : Input/output Erorr \n"); + } + else + { + // Convert the data + float ch1 = (data[3] * 256 + data[2]); //Infrared Value + return ch1; + } } int init_TSL2561(char *bus) @@ -58,7 +95,11 @@ void TSL2561() while(1) { sleep(1/fe); - getLux(file); + float a = getFullSpectrum(file); + float b = getInfraredLight(file); + printf("%.1f\n",a); + printf("%.1f\n",b); + printSpectrum(file); } } void main(){ diff --git a/Code-C/getArray.h b/Code-C/getArray.h deleted file mode 100644 index 6d75887..0000000 --- a/Code-C/getArray.h +++ /dev/null @@ -1,11 +0,0 @@ -#include -#include -#include -#include -#include -#include - -long **getRawDataArray(char *rawDataFileName, int N , int M); -void printArrayData(long** p, int N, int M); -void freeArray(long **p, int N); -bool checkArrayFullyFill(long **p, int N ); \ No newline at end of file diff --git a/Code-C/include/TSL2561.h b/Code-C/include/TSL2561.h index 8781491..9cccbd3 100644 --- a/Code-C/include/TSL2561.h +++ b/Code-C/include/TSL2561.h @@ -8,5 +8,7 @@ char *bus = "/dev/i2c-1"; -void getLux(int file); +void printSpectrum(int file); +float getFullSpectrum(int file); +float getInfraredLight(int file); int init_TSL2561(char *bus);