aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGergő J. Miklós2019-05-12 17:57:05 +0200
committerGergő J. Miklós2019-05-12 17:57:05 +0200
commitd8918243785c16bea16a5382e80f55cabfec1f51 (patch)
treece904f35b1bd888c0dc926aeead9201d14d0e6bc
parent7893fa123087328d5721fa27e9ee45130ba5da96 (diff)
downloadi2sensors-d8918243785c16bea16a5382e80f55cabfec1f51.tar.gz
i2sensors-d8918243785c16bea16a5382e80f55cabfec1f51.zip
refactoring
-rw-r--r--Makefile2
-rw-r--r--run.sh2
-rw-r--r--src/deftypes.h21
-rw-r--r--src/hih61xx.c31
-rw-r--r--src/hih61xx.h2
-rw-r--r--src/hmc5883.c3
-rw-r--r--src/lm75.c29
-rw-r--r--src/lm75.h21
-rw-r--r--src/main.c50
9 files changed, 98 insertions, 63 deletions
diff --git a/Makefile b/Makefile
index 78634ae..ca19f51 100644
--- a/Makefile
+++ b/Makefile
@@ -6,7 +6,7 @@ all: main copy
main:
- $(CC) $(CFLAGS) src/hmc5883.c src/hih61xx.c src/lm75.c src/main.c -o build/i2sensors
+# $(CC) $(CFLAGS) src/hmc5883.c src/hih61xx.c src/lm75.c src/main.c -o build/i2sensors
$(ACC) $(CFLAGS) src/hmc5883.c src/hih61xx.c src/lm75.c src/main.c -o build/i2sensors-armhf
diff --git a/run.sh b/run.sh
index 013205c..a27b1fb 100644
--- a/run.sh
+++ b/run.sh
@@ -8,7 +8,7 @@ if [ "$?" == "0" ]; then
#rsync -aHv -q -e "ssh" build/i2sensors-arm root@cb2:/tmp/i2sensors
scp build/i2sensors-armhf root@cb2:/tmp/i2sensors
- ssh root@cb2 '/tmp/i2sensors read_one 1 lm75 0x49 0,111.1'
+ ssh root@cb2 '/tmp/i2sensors read_one 1 lm75 0x49 0,-0.5'
echo
ssh root@cb2 '/tmp/i2sensors read_all 1 lm75 0x49 '
#ssh root@cb2 '/tmp/i2sensors list_all 1 lm75 0x49'
diff --git a/src/deftypes.h b/src/deftypes.h
new file mode 100644
index 0000000..ec91499
--- /dev/null
+++ b/src/deftypes.h
@@ -0,0 +1,21 @@
+
+#ifndef _DEFTYPESH_INCLUDED
+#define _DEFTYPESH_INCLUDED
+
+#define uint8 __uint8_t
+#define uint16 __uint16_t
+#define uint32 __uint32_t
+#define uint64 __uint64_t
+#define uint128 __uint128_t
+
+#define int8 __int8_t
+#define int16 __int16_t
+#define int32 __int32_t
+#define int64 __int64_t
+#define int128 __int128_t
+
+#define uchar __u_char
+
+#define ulong __u_long
+
+#endif \ No newline at end of file
diff --git a/src/hih61xx.c b/src/hih61xx.c
index 2a61d3b..732b697 100644
--- a/src/hih61xx.c
+++ b/src/hih61xx.c
@@ -7,15 +7,16 @@
#include "hih61xx.h"
+#include "deftypes.h"
-extern int file;
+extern uint16 devicef;
extern void bus_err(int ern);
extern void print_help(void);
unsigned char buf[5];
-
+
-void hih61xx_print_all(void)
+void hih61xx_print_all(void)
{
printf(
"00: Relative Humidity [%%] (reg: 0x00)\n"
@@ -27,34 +28,30 @@ void hih61xx_print_all(void)
void hih61xx_conf_set(const char *opts) {}
-void hih61xx_get_data(void)
+void hih61xx_get_data(void) //# Get measurement data from device
{
buf[0] = 0x00;
- if(write(file, buf, 1) != 1) //Start measure
- {
+ if(write(devicef, buf, 1) != 1){ //# Start the measurement
bus_err(errno);
}
- usleep(60*1000); // Wait 60ms for measurement
+ usleep(60*1000); //# Wait 60ms for measurement
- if(read(file, buf, 4) != 4) //Read the data
- {
+ if(read(devicef, buf, 4) != 4){ //# Read the data
bus_err(errno);
}
}
-unsigned char hih61xx_read_status(void)
-{
- return ((buf[0] & 0xc0) >> 6);
+unsigned char hih61xx_read_status(void){ //# Get device status
+ return ((buf[0] & 0xc0) >> 6); //# He ???
}
-float hih61xx_read_humidity(float offset)
-{
+float hih61xx_calc_humidity(float offset){ //# Measure
unsigned int raw = 0;
float humidity;
@@ -100,7 +97,7 @@ void hih61xx_read_all(const char *opts)
if(opts != NULL)
{
char *ptr;
- printf("00:%f\n", hih61xx_read_humidity(strtof(opts, &ptr)));
+ printf("00:%f\n", hih61xx_calc_humidity(strtof(opts, &ptr)));
ptr++; // One (,) character allowed for delimiter
@@ -108,7 +105,7 @@ void hih61xx_read_all(const char *opts)
}
else
{
- printf("00:%f\n", hih61xx_read_humidity(0.0));
+ printf("00:%f\n", hih61xx_calc_humidity(0.0));
printf("01:%f\n", hih61xx_read_temp(0.0));
}
@@ -130,7 +127,7 @@ void hih61xx_read_one(const char *opts)
switch (id)
{
case 0x00:
- printf("00:%f\n", hih61xx_read_humidity(strtof(ptr, NULL)));
+ printf("00:%f\n", hih61xx_calc_humidity(strtof(ptr, NULL)));
break;
case 0x01:
printf("01:%f\n", hih61xx_read_temp(strtof(ptr, NULL)));
diff --git a/src/hih61xx.h b/src/hih61xx.h
index 23546d2..f0def27 100644
--- a/src/hih61xx.h
+++ b/src/hih61xx.h
@@ -12,7 +12,7 @@ void hih61xx_print_all(void);
void hih61xx_get_data(void);
void hih61xx_conf_set(const char *opts);
unsigned char hih61xx_read_status(void);
-float hih61xx_read_humidity(float offset);
+float hih61xx_calc_humidity(float offset);
float hih61xx_read_temp(float offset);
void hih61xx_read_all(const char *opts);
void hih61xx_read_one(const char *opts);
diff --git a/src/hmc5883.c b/src/hmc5883.c
index 6167f73..c02d1cf 100644
--- a/src/hmc5883.c
+++ b/src/hmc5883.c
@@ -7,8 +7,9 @@
#include "hmc5883.h"
+#include "deftypes.h"
-extern int file;
+extern uint16 devicef;
extern void bus_err(int ern);
extern void print_help(void);
unsigned char buf[5];
diff --git a/src/lm75.c b/src/lm75.c
index fd6f374..784defd 100644
--- a/src/lm75.c
+++ b/src/lm75.c
@@ -7,11 +7,15 @@
#include "lm75.h"
+#include "deftypes.h"
-extern int file;
+
+
+extern uint16 devicef;
extern void bus_err(int ern);
extern void print_help(void);
-unsigned char buf[5];
+uchar buf[32];
+
void lm75_print_all(void)
@@ -25,16 +29,18 @@ void lm75_print_all(void)
}
+uchar *dev_xchg_data( uchar *buf, uint16 blen){ //# Exchange data with the device
-void lm75_get_data(void){
-
- if(write(file, buf, 1) != 1){
+ if(write(devicef, buf, 1) != 1){ //# write
bus_err(errno);
}
- if(read(file, buf, 2) != 2) {
+ if(read(devicef, buf, 2) != 2) { //# read
bus_err(errno);
- }
+ }
+
+ // *buf = 0x0022;
+ return buf;
}
void lm75_conf_set(const char *opts){
@@ -46,10 +52,11 @@ float lm75_read_temp(float offset)
{
signed int rawtemp = 0;
float temp;
+ // char *buff;
buf[0] = 0x00;
- lm75_get_data();
+ dev_xchg_data(buf,32);
rawtemp = (buf[0]*256 + buf[1]) >> 5; //(buf[0] << 8), and (>> 5), 11bit device is also supported
@@ -70,7 +77,7 @@ float lm75_read_tos(void)
buf[0] = 0x02;
- lm75_get_data();
+ dev_xchg_data(NULL,32);
rawtemp = buf[0]*256 + buf[1];
rawtemp = (rawtemp) >> 7; //9bit data
@@ -91,7 +98,7 @@ float lm75_read_thys(void)
buf[0] = 0x03;
- lm75_get_data();
+ dev_xchg_data(NULL,32);
rawtemp = buf[0]*256 + buf[1];
rawtemp = (rawtemp) >> 7; //9bit data
@@ -110,7 +117,7 @@ char lm75_read_conf(void)
buf[0] = 0x01;
- lm75_get_data();
+ dev_xchg_data(NULL,32);
return buf[0];
}
diff --git a/src/lm75.h b/src/lm75.h
index 30edee4..ec4dd70 100644
--- a/src/lm75.h
+++ b/src/lm75.h
@@ -7,15 +7,28 @@
// void lm75_list_all(void);
// void lm75_read_all(const char *opts);
//
-void lm75_print_all(void);
-void lm75_get_data(void);
+void lm75_print_all(void); //# Global funcions, required by main()
+void lm75_read_all(const char *opts);
+void lm75_read_one(const char *opts);
void lm75_conf_set(const char *opts);
+
+//unsigned char *dev_xchg_data(unsigned char *buf);
float lm75_read_temp(float offset);
float lm75_read_tos(void);
float lm75_read_thys(void);
char lm75_read_conf(void);
-void lm75_read_all(const char *opts);
-void lm75_read_one(const char *opts);
+//__uint8_t
+//__uint16_t
+//__uint32_t
+//__uint64_t
+//__uint128_t
+//__int8_t
+// __int16_t
+// __int32_t
+// __int64_t
+// __int128_t
+// __u_char
+//__u_long
#endif \ No newline at end of file
diff --git a/src/main.c b/src/main.c
index 7d39441..18f762a 100644
--- a/src/main.c
+++ b/src/main.c
@@ -13,6 +13,7 @@
#include <linux/i2c-dev.h>
#include <sys/ioctl.h>
+#include "deftypes.h"
#include "lm75.h"
#include "hih61xx.h"
#include "hmc5883.h"
@@ -27,8 +28,8 @@
// extern void hih61xx_read_one(const char *opts);
-int file;
-char temp[256];
+uint16 devicef; //# Bus-Device file
+uchar temp[256];
void (*print_all)(void) = NULL;
void (*read_all)(const char *opts) = NULL;
void (*read_one)(const char *opts) = NULL;
@@ -40,10 +41,11 @@ void print_help(void) {
" i2sensors <op> <bus> <dev_type> <dev_addr> (<options>)\n"
"\n"
"Aviable options: <op>\n"
- " print_all -- list all available measurement parameters of the device\n"
- " read_all -- read all measured parameters from device\n"
- " read_one -- read the selected parameters from device\n"
- " conf_set -- set the configuration register\n"
+ " print_all -- list all available measurement parameters of the device\n"
+ " read_all -- read all measured parameters from device\n"
+ " read_one -- read the selected parameters from device\n"
+ " read_repeat -- read measurements continously\n"
+ " conf_set -- set the configuration register\n"
"\n"
"Usage:\n"
" i2sensors read_all <bus> <device-type> <address> \n"
@@ -101,44 +103,40 @@ int main(int argc, char *argv[])
// ...set_reg... <dev_addr>, <reg_addr,value_hex>
- if(4 < argc && argc < 10) //Minimal 5 argument passed
+ if(4 < argc && argc < 10) //Minimum 5 arguments should be passed.
{
char filename[32];
int i;
preinit(argv[3]);
- for(i=0;i<strlen(argv[2]);i++) //Some test on bus address, for safety.
- {
+ for( i = 0; i < strlen(argv[2]); i++){ //# Test on bus address, for error detection.
if(!isdigit(argv[2][i])){
- printf("\n\tThe BUS Address must be an integer\n\n");
+ printf("\n\tThe BUS Address must be an integer!\n\n");
print_help();
exit (EXIT_FAILURE);
}
}
- snprintf(filename, 31, "/dev/i2c-%s", argv[2]); //Open i2c bus
- file = open(filename, O_RDWR);
+ snprintf(filename, 31, "/dev/i2c-%s", argv[2]);
+ devicef = open(filename, O_RDWR); //# Open the i2c bus
- if (file < 0){
+ if (devicef < 0){
printf("\n\tCan't open i2c BUS. Have you any permission?\n\n");
print_help();
exit (EXIT_FAILURE);
}
- if(argv[4][0]=='0' && argv[4][1]=='x'){ // Hexadecimal device address
+ if(argv[4][0]=='0' && argv[4][1]=='x'){ //# Hexadecimal representetion of device address
- long addr = (int)strtol(argv[4], NULL, 0); //# strtol() is working with hex string
+ long addr = (int)strtol(argv[4], NULL, 0); //# strtol() is working with hex-string correctly
- if (ioctl(file, I2C_SLAVE, addr) < 0)
- {
+ if (ioctl(devicef, I2C_SLAVE, addr) < 0){ //# Seeking/Opening the device on the Bus
printf("\n\tCan't open i2c DEVICE, errno: %d\n\n",errno);
print_help();
exit (EXIT_FAILURE);
}
- }
- else
- {
+ } else {
printf("\n\tThe DEVICE address must be a hexadecimal number\n\n");
print_help();
exit (EXIT_FAILURE);
@@ -146,25 +144,23 @@ int main(int argc, char *argv[])
- if(!strcmp("print_all",argv[1]) && print_all != NULL) // If list_all is selected.
- {
+ if(!strcmp("print_all",argv[1]) && print_all != NULL){ //# Gets the device's help message.
print_all();
}
- else if(!strcmp("read_all",argv[1]) && read_all != NULL)
- {
+ else if(!strcmp("read_all",argv[1]) && read_all != NULL){ //# Pulls all readable registers data from device
// read_all(argv[5]);
read_all(NULL);
}
- else if(!strcmp("read_one",argv[1]) && read_one != NULL && argc > 5)
+ else if(!strcmp("read_one",argv[1]) && read_one != NULL && argc > 5) //# Prints out the selected register
{
read_one(argv[5]);
}
- else
+ else //# No other option
{
print_help();
}
- close(file);
+ close(devicef);
}
else