aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGergő J. Miklós2019-05-13 00:49:29 +0200
committerGergő J. Miklós2019-05-13 00:49:29 +0200
commite0eeeb389302c3050310ac8e618119384ce0fef8 (patch)
tree1e8ead8fffde3ad3341cfc3741773d359455c8ac
parentd8918243785c16bea16a5382e80f55cabfec1f51 (diff)
downloadi2sensors-e0eeeb389302c3050310ac8e618119384ce0fef8.tar.gz
i2sensors-e0eeeb389302c3050310ac8e618119384ce0fef8.zip
LM75 refactored
-rw-r--r--src/deftypes.h5
-rw-r--r--src/hih61xx.c10
-rw-r--r--src/hih61xx.h8
-rw-r--r--src/hmc5883.c6
-rw-r--r--src/hmc5883.h16
-rw-r--r--src/lm75.c129
-rw-r--r--src/lm75.h18
-rw-r--r--src/main.c20
8 files changed, 100 insertions, 112 deletions
diff --git a/src/deftypes.h b/src/deftypes.h
index ec91499..c53cd57 100644
--- a/src/deftypes.h
+++ b/src/deftypes.h
@@ -2,20 +2,25 @@
#ifndef _DEFTYPESH_INCLUDED
#define _DEFTYPESH_INCLUDED
+// Unsigned types
#define uint8 __uint8_t
#define uint16 __uint16_t
#define uint32 __uint32_t
#define uint64 __uint64_t
#define uint128 __uint128_t
+// Signed types (default)
#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 732b697..508fd3e 100644
--- a/src/hih61xx.c
+++ b/src/hih61xx.c
@@ -25,7 +25,7 @@ void hih61xx_print_all(void)
"\n");
}
-void hih61xx_conf_set(const char *opts) {}
+void hih61xx_conf_set(const uchar *opts) {}
void hih61xx_get_data(void) //# Get measurement data from device
@@ -90,14 +90,14 @@ float hih61xx_read_temp(float offset)
-void hih61xx_read_all(const char *opts)
+void hih61xx_read_all(const uchar *opts)
{
hih61xx_get_data();
if(opts != NULL)
{
char *ptr;
- printf("00:%f\n", hih61xx_calc_humidity(strtof(opts, &ptr)));
+ printf("00:%f\n", hih61xx_calc_humidity(strtof((char*)opts, &ptr)));
ptr++; // One (,) character allowed for delimiter
@@ -115,13 +115,13 @@ void hih61xx_read_all(const char *opts)
-void hih61xx_read_one(const char *opts)
+void hih61xx_read_one(const uchar *opts)
{
int id;
char *ptr;
hih61xx_get_data();
- id = strtol (opts,&ptr,0); //all format allowed
+ id = strtol ((char*)opts,&ptr,0); //all format allowed
ptr++; //one separator allowed
switch (id)
diff --git a/src/hih61xx.h b/src/hih61xx.h
index f0def27..8675562 100644
--- a/src/hih61xx.h
+++ b/src/hih61xx.h
@@ -1,6 +1,8 @@
#ifndef _HIH61XX_INCLUDED
#define _HIH61xx_INCLUDED
+
+#include "deftypes.h"
/*
extern int file;
@@ -10,11 +12,11 @@ void hih61xx_read_all(const char *opts);
*/
void hih61xx_print_all(void);
void hih61xx_get_data(void);
-void hih61xx_conf_set(const char *opts);
+void hih61xx_conf_set(const uchar *opts);
unsigned char hih61xx_read_status(void);
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);
+void hih61xx_read_all(const uchar *opts);
+void hih61xx_read_one(const uchar *opts);
#endif \ No newline at end of file
diff --git a/src/hmc5883.c b/src/hmc5883.c
index c02d1cf..d45e972 100644
--- a/src/hmc5883.c
+++ b/src/hmc5883.c
@@ -25,6 +25,6 @@ void hmc5883_print_all(void)
"\n");
}
-void hmc5883_read_all(const char *opts) {}
-void hmc5883_read_one(const char *opts) {}
-void hmc5883_conf_set(const char *opts){} \ No newline at end of file
+void hmc5883_read_all(const uchar *opts) {}
+void hmc5883_read_one(const uchar *opts) {}
+void hmc5883_conf_set(const uchar *opts){} \ No newline at end of file
diff --git a/src/hmc5883.h b/src/hmc5883.h
index b4c6a58..ec27d5b 100644
--- a/src/hmc5883.h
+++ b/src/hmc5883.h
@@ -6,16 +6,18 @@
//
// void lm75_list_all(void);
// void lm75_read_all(const char *opts);
-//
-void hmc5883_print_all(void);
-void hmc5883_get_data(void);
-void hmc5883_conf_set(const char *opts);
+// //
+// void hmc5883_print_all(void);
+// void hmc5883_get_data(void);
+// void hmc5883_conf_set(const uchar *opts);
+
// float lm75_read_temp(float offset);
// float lm75_read_tos(void);
// float lm75_read_thys(void);
-// char lm75_read_conf(void);
-void hmc5883_read_all(const char *opts);
-void hmc5883_read_one(const char *opts);
+// char lm75_read_conf(void);
+
+// void hmc5883_read_all(const uchar *opts);
+// void hmc5883_read_one(const uchar *opts);
#endif \ No newline at end of file
diff --git a/src/lm75.c b/src/lm75.c
index 784defd..bd1f764 100644
--- a/src/lm75.c
+++ b/src/lm75.c
@@ -29,127 +29,100 @@ void lm75_print_all(void)
}
-uchar *dev_xchg_data( uchar *buf, uint16 blen){ //# Exchange data with the device
+uchar *read_data( uchar *buf ){ //# [buf] = 32 byte
- if(write(devicef, buf, 1) != 1){ //# write
+ if(write(devicef, buf, 1) != 1){ //# write one byte to device
bus_err(errno);
}
+ usleep(10*1000); //# Wait 10ms for reading
- if(read(devicef, buf, 2) != 2) { //# read
+ if(read(devicef, buf, 2) != 2) { //# read the result
bus_err(errno);
}
-
- // *buf = 0x0022;
return buf;
}
-void lm75_conf_set(const char *opts){
- // printf("");
-}
-float lm75_read_temp(float offset)
-{
- signed int rawtemp = 0;
+float calculate_temp(float offset){ // Calculate temperature
+ int16 rawtemp = 0; //# Signed by default
float temp;
- // char *buff;
- buf[0] = 0x00;
-
- dev_xchg_data(buf,32);
+ buf[0] = 0x00; //# Measurement in the REG[0]
+ read_data(buf); //# [buf] = 32 byte
- rawtemp = (buf[0]*256 + buf[1]) >> 5; //(buf[0] << 8), and (>> 5), 11bit device is also supported
+ rawtemp = (buf[0]*256 + buf[1]) >> 5; // (buf[0] << 8), and (>> 5), Device with 11bit data, is also supported
- if((rawtemp & 0x400) == 0x400) //check if the msb(bit11) is 1 (1024 = 0x400), 2'complement negative number
- {
- rawtemp = rawtemp - 2048; //2^11 = 2048
+ if((rawtemp & 0x400) == 0x400){ //# check if the msb(bit11) is 1 (1024 = 0x400), 2'complement negative number
+ rawtemp = rawtemp - 2048; //# 2^11 = 2048
}
- temp = rawtemp * 0.125 ; //11bit->0.125°C or 9bit->0.5°C
+ temp = rawtemp * 0.125 ; //# 11bit -> 0.125°C or 9bit -> 0.5°C
return (temp + offset);
}
-float lm75_read_tos(void)
-{
- int rawtemp = 0;
-
- buf[0] = 0x02;
-
- dev_xchg_data(NULL,32);
-
+float read_tos(void){ // Over-Temperature Shutdown register
+ int16 rawtemp = 0; //# Int16 default signed
+ buf[0] = 0x02; //# Tos = REG[2]
+ read_data(buf);
+
rawtemp = buf[0]*256 + buf[1];
- rawtemp = (rawtemp) >> 7; //9bit data
-
- if((rawtemp & 256) == 256) // check if the msb 2'complement negative number
- {
- rawtemp = rawtemp - 512 ; // 2^9 = 512;
+ rawtemp = (rawtemp) >> 7; //# 9bit data
+
+ if((rawtemp & 256) == 256){ //# check MSB if it's a 2'complement number
+ rawtemp = rawtemp - 512 ; //# 2^9 = 512;
}
-
- return (rawtemp * 0.5); //9bit -> 0.5 celsius
+ return (rawtemp * 0.5); //# 9bit -> 0.5 celsius
}
-float lm75_read_thys(void)
-{
- int rawtemp = 0;
-
- buf[0] = 0x03;
-
- dev_xchg_data(NULL,32);
+float read_thys(void){ // Over-Temp Hysteresis Register
+ int16 rawtemp = 0; //# signed
+ buf[0] = 0x03;
+ read_data(buf); //# read from REG[3]
rawtemp = buf[0]*256 + buf[1];
- rawtemp = (rawtemp) >> 7; //9bit data
-
- if((rawtemp & 256) == 256) // check if the msb 2'complement negative number
- {
- rawtemp = rawtemp - 512 ; // 2^9 = 512;
+ rawtemp = (rawtemp) >> 7; //# 9bit data
+ if((rawtemp & 256) == 256){
+ rawtemp = rawtemp - 512 ; //# 2^9 = 512;
}
-
- return (rawtemp * 0.5); //9bit -> 0.5 celsius
+ return (rawtemp * 0.5); //# 9bit -> 0.5 celsius
}
-char lm75_read_conf(void)
-{
-
- buf[0] = 0x01;
-
- dev_xchg_data(NULL,32);
-
+uchar read_conf(void){ //Configuration register
+ buf[0] = 0x01;
+ read_data(buf);
return buf[0];
}
-void lm75_read_all(const char *opts)
-{
+void lm75_read_all(const uchar *opts){ // Print out whole device's data
// if(opts != NULL)
// {
- // printf("00:%f\n", lm75_read_temp(strtof(opts, NULL)));
+ // printf("00:%f\n", calculate_temp(strtof(opts, NULL)));
// }
// else
// {
- printf("00:%f\n", lm75_read_temp(0.0));
+ printf("00:%f\n", calculate_temp(0.0));
// }
-
- printf("01:0x%x\n", lm75_read_conf());
- printf("02:%f\n", lm75_read_tos());
- printf("03:%f\n", lm75_read_thys());
+ printf("01:0x%x\n", read_conf());
+ printf("02:%f\n", read_tos());
+ printf("03:%f\n", read_thys());
}
-void lm75_read_one(const char *opts)
-{
- int id,i;
- //char *ptr;
- char temp[256];
+void lm75_read_one(const uchar *opts){ // Prints the selected register's data
+ uint16 id,i;
+ uchar temp[256];
if(opts != NULL){
- for(i = 0; i < strlen(opts); i++){
+ for(i = 0; i < strlen((char*)opts); i++){
if (*(opts+i) == ','){
break;
}
@@ -157,24 +130,24 @@ void lm75_read_one(const char *opts)
temp[i+1] = '\0';
}
- id = atoi(temp);
- strncpy(temp, opts+i+1, 255);
+ id = atoi((char*)temp);
+ strncpy((char*)temp, (char*)opts+i+1, 255);
// id = strtol (opts,&ptr,0); //all format allowed
// ptr++; //one separator allowed
switch (id)
{
case 0x00:
- printf("00:%f\n", lm75_read_temp(atof(temp)));
+ printf("%f\n", calculate_temp(atof((char*)temp)));
break;
case 0x01:
- printf("01:0x%x\n", lm75_read_conf());
+ printf("0x%x\n", read_conf());
break;
case 0x02:
- printf("02:%f\n", lm75_read_tos());
+ printf("%f\n", read_tos());
break;
case 0x03:
- printf("03:%f\n", lm75_read_thys());
+ printf("%f\n", read_thys());
break;
default:
print_help();
@@ -182,3 +155,7 @@ void lm75_read_one(const char *opts)
}
}
+
+void lm75_conf_set(const uchar *opts){
+ // printf("");
+} \ No newline at end of file
diff --git a/src/lm75.h b/src/lm75.h
index ec4dd70..600934c 100644
--- a/src/lm75.h
+++ b/src/lm75.h
@@ -2,21 +2,23 @@
#ifndef _LM75_INCLUDED
#define _LM75_INCLUDED
+#include "deftypes.h"
+
// extern int file;
//
// void lm75_list_all(void);
// void lm75_read_all(const char *opts);
//
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);
+void lm75_read_all(const uchar *opts);
+void lm75_read_one(const uchar *opts);
+void lm75_conf_set(const uchar *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);
+//unsigned char *read_data(unsigned char *buf);
+float calculate_temp(float offset);
+float read_tos(void);
+float read_thys(void);
+uchar read_conf(void);
//__uint8_t
//__uint16_t
diff --git a/src/main.c b/src/main.c
index 18f762a..7afe9c0 100644
--- a/src/main.c
+++ b/src/main.c
@@ -31,9 +31,9 @@
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;
-void (*conf_set)(const char *opts) = NULL;
+void (*read_all)(const uchar *opts) = NULL;
+void (*read_one)(const uchar *opts) = NULL;
+void (*conf_set)(const uchar *opts) = NULL;
void print_help(void) {
@@ -79,12 +79,12 @@ void preinit(const char *dev_type){
conf_set = hih61xx_conf_set;
}
- else if(!strcmp("hmc5883", dev_type)) {
- print_all = hmc5883_print_all;
- read_all = hmc5883_read_all;
- read_one = hmc5883_read_one;
- conf_set = hmc5883_conf_set;
- }
+ // else if(!strcmp("hmc5883", dev_type)) {
+ // print_all = hmc5883_print_all;
+ // read_all = hmc5883_read_all;
+ // read_one = hmc5883_read_one;
+ // conf_set = hmc5883_conf_set;
+ // }
}
@@ -153,7 +153,7 @@ int main(int argc, char *argv[])
}
else if(!strcmp("read_one",argv[1]) && read_one != NULL && argc > 5) //# Prints out the selected register
{
- read_one(argv[5]);
+ read_one((uchar*)argv[5]);
}
else //# No other option
{