From d8d87351acd4a8fef65db6a59993cbac99dc3bab Mon Sep 17 00:00:00 2001 From: Gergő J. Miklós Date: Tue, 6 Aug 2019 08:51:57 +0200 Subject: qmc5883 initial --- Makefile | 8 +++----- run.sh | 6 +++--- src/hih61xx.c | 8 ++++---- src/hih61xx.h | 19 ++----------------- src/hmc5883.c | 30 ------------------------------ src/hmc5883.h | 23 ----------------------- src/lm75.c | 34 +++++++++++++++++----------------- src/lm75.h | 6 ++++-- src/main.c | 43 +++++++++++++++++++++++-------------------- 9 files changed, 56 insertions(+), 121 deletions(-) delete mode 100644 src/hmc5883.c delete mode 100644 src/hmc5883.h diff --git a/Makefile b/Makefile index 0c9b0c1..c84246a 100644 --- a/Makefile +++ b/Makefile @@ -2,15 +2,13 @@ CC = gcc ACC = arm-linux-gnueabihf-gcc CFLAGS = -Wall -all: armhf copy -#copy - +all: x86 copy armhf: - $(ACC) $(CFLAGS) src/hmc5883.c src/hih61xx.c src/lm75.c src/main.c -o build/i2sensors-armhf + $(ACC) $(CFLAGS) src/qmc5883.c src/hih61xx.c src/lm75.c src/main.c -o build/i2sensors-armhf x86: - $(CC) $(CFLAGS) src/hmc5883.c src/hih61xx.c src/lm75.c src/main.c -o build/i2sensors + $(CC) $(CFLAGS) src/qmc5883.c src/hih61xx.c src/lm75.c src/main.c -o build/i2sensors copy: scp -r build/i2sensors-armhf root@cb2:/tmp/i2sensors diff --git a/run.sh b/run.sh index 3baaa4c..6894d27 100644 --- a/run.sh +++ b/run.sh @@ -11,11 +11,11 @@ if [ "$?" == "0" ]; then #rsync -aHv -q -e "ssh" build/i2sensors-arm root@cb2:/tmp/i2sensors # scp -r build/i2sensors-armhf root@cb2:/tmp/i2sensors - ssh root@cb2 '/tmp/i2sensors read_one 1 lm75 0x49 0,-0.5' + ssh root@cb2 '/tmp/i2sensors read_one 1 qmc5883 0x0d 0,x' echo - ssh root@cb2 '/tmp/i2sensors conf_set 1 lm75 0x49 thys,-129.5' + #ssh root@cb2 '/tmp/i2sensors conf_set 1 lm75 0x49 thys,-129.5' #ssh root@cb2 '/tmp/i2sensors ' - ssh root@cb2 '/tmp/i2sensors read_all 1 lm75 0x49' + ssh root@cb2 '/tmp/i2sensors read_one 1 lm75 0x49 0,-0.5 ' # ssh root@cb2 'rm /tmp/i2sensors' diff --git a/src/hih61xx.c b/src/hih61xx.c index cf00c04..a985fda 100644 --- a/src/hih61xx.c +++ b/src/hih61xx.c @@ -11,7 +11,7 @@ #include "hih61xx.h" #include "deftypes.h" -extern uint16 devicef; +//extern uint16 devicef; extern void bus_err(int ern); extern void print_help(void); extern uchar *xchg_data (uchar *buf, uint8 wrlen, uint8 waitlen, uint8 rdlen); @@ -19,7 +19,7 @@ unsigned char buf[32]; -void hih61xx_print_all(void) +void hih61xx_print_all(const uchar *opts) { printf( "============ HiH61xx interface ===========\n" @@ -154,7 +154,7 @@ void hih61xx_read_one(const uchar *opts) if( !isdigit(*(opts+i)) ){ //# Check the register string printf("The Register address must be an integer!\n"); print_help(); - hih61xx_print_all(); + hih61xx_print_all(NULL); exit (EXIT_FAILURE); } temp[i] = *(opts+i); @@ -181,7 +181,7 @@ void hih61xx_read_one(const uchar *opts) break; default: print_help(); - hih61xx_print_all(); + hih61xx_print_all(NULL); exit(EXIT_FAILURE); } } diff --git a/src/hih61xx.h b/src/hih61xx.h index 44effd6..656e0e4 100644 --- a/src/hih61xx.h +++ b/src/hih61xx.h @@ -3,20 +3,15 @@ #define _HIH61xx_INCLUDED #include "deftypes.h" -/* -extern int file; -void hih61xx_list_all(void); -void hih61xx_read_all(const char *opts); -*/ -void hih61xx_print_all(void); +void hih61xx_print_all(const uchar *opts); void hih61xx_read_all(const uchar *opts); void hih61xx_read_one(const uchar *opts); void hih61xx_conf_set(const uchar *opts); -// Local functions +// Local functions: // uchar *xchg_data (uchar *buf, uint8 wrlen, uint8 waitlen, uint8 rdlen); // static float calc_temp(uchar *buf, float offset); @@ -24,15 +19,5 @@ void hih61xx_conf_set(const uchar *opts); // static float calc_humidity(uchar *buf, float offset); - - - -// void hih61xx_get_data(void); -// unsigned char hih61xx_read_status(void); -// float hih61xx_calc_humidity(float offset); -// float hih61xx_read_temp(float offset); - - - #endif diff --git a/src/hmc5883.c b/src/hmc5883.c deleted file mode 100644 index d45e972..0000000 --- a/src/hmc5883.c +++ /dev/null @@ -1,30 +0,0 @@ - -#include -#include -#include -#include -#include - - -#include "hmc5883.h" -#include "deftypes.h" - -extern uint16 devicef; -extern void bus_err(int ern); -extern void print_help(void); -unsigned char buf[5]; - - -void hmc5883_print_all(void) -{ - printf( - "00: Temperature data [°C] (reg: 0x00)\n" - "01: Configuration [hex] (reg: 0x01)\n" - "02: Tos (Overtemp) [°C] (reg: 0x03)\n" - "03: Thys (Hysteresis) [°C] (reg: 0x04)\n" - "\n"); -} - -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 deleted file mode 100644 index ec27d5b..0000000 --- a/src/hmc5883.h +++ /dev/null @@ -1,23 +0,0 @@ - -#ifndef _HMC5883_INCLUDED -#define _HMC5883_INCLUDED - -// extern int file; -// -// 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 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 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 2e14738..3754a56 100644 --- a/src/lm75.c +++ b/src/lm75.c @@ -12,7 +12,7 @@ -extern uint16 devicef; +//extern uint16 devicef; extern void bus_err(int ern); extern void print_help(void); extern uchar *xchg_data (uchar *buf, uint8 wrlen, uint8 waitlen, uint8 rdlen); @@ -20,7 +20,7 @@ uchar buf[32]; -void lm75_print_all(void) +void lm75_print_all(const uchar *opts) { printf( "============ LM75xx interface ===========\n" @@ -121,11 +121,11 @@ void lm75_read_all(const uchar *opts){ // Print out whole device's data // { // printf("00:%f\n", calc_temp(strtof(opts, NULL))); // } else { - printf("00:%f\n", calc_temp(0.0)); + fprintf(stdout, "00:%f\n", calc_temp(0.0)); // } - printf("01:0x%x\n", read_conf()); - printf("02:%f\n", read_thys()); - printf("03:%f\n", read_tos()); + fprintf(stdout, "01:0x%x\n", read_conf()); + fprintf(stdout, "02:%f\n", read_thys()); + fprintf(stdout, "03:%f\n", read_tos()); } @@ -142,9 +142,9 @@ void lm75_read_one(const uchar *opts){ // Prints the selected r } else { if( !isdigit(*(opts+i)) ){ //# Check the register string - printf("The Register address must be an integer!\n"); + fprintf(stderr, "The Register address must be an integer!\n"); print_help(); - lm75_print_all(); + lm75_print_all(NULL); exit (EXIT_FAILURE); } temp[i] = *(opts+i); //# copy register string @@ -158,16 +158,16 @@ void lm75_read_one(const uchar *opts){ // Prints the selected r switch (id) //# Which register is selected? { case 0x00: - printf("%f\n", calc_temp(atof((char*)temp))); //# with the offset + fprintf(stdout, "%f\n", calc_temp(atof((char*)temp))); //# with the offset break; case 0x01: - printf("0x%x\n", read_conf()); + fprintf(stdout, "0x%x\n", read_conf()); break; case 0x02: - printf("%f\n", read_thys()); + fprintf(stdout, "%f\n", read_thys()); break; case 0x03: - printf("%f\n", read_tos()); + fprintf(stdout, "%f\n", read_tos()); break; default: print_help(); @@ -253,7 +253,7 @@ void lm75_conf_set(const uchar *opts){ // Prints the selected r } else { - lm75_print_all(); + lm75_print_all(NULL); exit(EXIT_FAILURE); } buf[0] = 0x01; @@ -274,7 +274,7 @@ void lm75_conf_set(const uchar *opts){ // Prints the selected r } else { - lm75_print_all(); + lm75_print_all(NULL); exit(EXIT_FAILURE); } buf[0] = 0x01; @@ -295,7 +295,7 @@ void lm75_conf_set(const uchar *opts){ // Prints the selected r } else { - lm75_print_all(); + lm75_print_all(NULL); exit(EXIT_FAILURE); } buf[0] = 0x01; @@ -325,7 +325,7 @@ void lm75_conf_set(const uchar *opts){ // Prints the selected r } else { - lm75_print_all(); + lm75_print_all(NULL); exit(EXIT_FAILURE); } buf[0] = 0x01; @@ -337,7 +337,7 @@ void lm75_conf_set(const uchar *opts){ // Prints the selected r // if( !isxdigit(*(opts+i)) || *(opts+i) !='x' || opts[i] != ','){ //# Check the register string // printf("The Register address, and value must be a hex, or an integer!\n"); print_help(); - lm75_print_all(); + lm75_print_all(NULL); exit (EXIT_FAILURE); // } // } diff --git a/src/lm75.h b/src/lm75.h index 60cba38..d64696b 100644 --- a/src/lm75.h +++ b/src/lm75.h @@ -6,12 +6,14 @@ // extern int file; -void lm75_print_all(void); //# Global funcions, required by main() +//# Global funcions: +void lm75_print_all(const uchar *opts); void lm75_read_all(const uchar *opts); void lm75_read_one(const uchar *opts); void lm75_conf_set(const uchar *opts); -// //Local functions + +//Local functions: // uchar *xchg_data (uchar *buf, uint8 wrlen, uint8 waitlen, uint8 rdlen); // static float calc_temp(float offset); // static float read_tos(void); diff --git a/src/main.c b/src/main.c index 4623919..3558996 100644 --- a/src/main.c +++ b/src/main.c @@ -7,7 +7,7 @@ #include #include #include -#include +#include //usleep(); #include @@ -16,7 +16,7 @@ #include "deftypes.h" #include "lm75.h" #include "hih61xx.h" -#include "hmc5883.h" +#include "qmc5883.h" #define VERSION 1.22 @@ -30,14 +30,14 @@ uint16 devicef; //# Bus-Device file uchar temp[256]; -void (*print_all)(void) = NULL; +void (*print_all)(const uchar *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) { - printf ("\n" + fprintf (stdout, "\n" " i2sensors ()\n" "\n" "Aviable options: \n" @@ -56,11 +56,12 @@ void print_help(void) { " i2sensors read_all 1 lm75 0x4f \n" " i2sensors read_one 1 lm75 0x4f 00,-1.35\n" "\n" ); - printf ("Version: v%1.2f, build: %s %s\n\n",VERSION,__DATE__,__TIME__); + fprintf (stdout, "Version: v%1.2f, build: %s %s\n\n",VERSION,__DATE__,__TIME__); } void bus_err(int ern){ - printf("I2C communication(rd) error. errno: %d\n",errno); + fprintf(stderr, "I2C communication(rd) error. errno: %d\n",errno); + exit (EXIT_FAILURE); } @@ -69,8 +70,9 @@ uchar *xchg_data (uchar *buf, uint16 wrlen, uint16 waitlen, uint16 rdlen){ if(write(devicef, buf, wrlen) != wrlen){ //# write one byte to device bus_err(errno); } - usleep(waitlen*1000); //# Wait 10ms for reading - + if(waitlen > 0){ + usleep(waitlen*1000); //# Wait n*1ms for reading + } if(read(devicef, buf, rdlen) != rdlen) { //# read the result bus_err(errno); } @@ -78,6 +80,7 @@ uchar *xchg_data (uchar *buf, uint16 wrlen, uint16 waitlen, uint16 rdlen){ } + void preinit(const uchar *dev_type){ if(!strcmp("lm75", (char *)dev_type)){ @@ -94,12 +97,12 @@ void preinit(const uchar *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("qmc5883", (char *)dev_type)) { + print_all = qmc5883_print_all; + read_all = qmc5883_read_all; + read_one = qmc5883_read_one; + conf_set = qmc5883_conf_set; + } } @@ -121,13 +124,13 @@ int main(int argc, char *argv[]) if(4 < argc && argc < 10) //Minimum 5 arguments should be passed. { - char filename[32]; + char filename[256]; int i; preinit((uchar *)argv[3]); 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"); + fprintf(stderr, "\n\tThe BUS Address must be an integer!\n\n"); print_help(); exit (EXIT_FAILURE); } @@ -136,7 +139,7 @@ int main(int argc, char *argv[]) devicef = open(filename, O_RDWR); //# Open the i2c bus if (devicef < 0){ - printf("\n\tCan't open i2c BUS. Have you any permission?\n\n"); + fprintf(stderr, "\n\tCan't open i2c BUS. Have you any permission?\n\n"); print_help(); exit (EXIT_FAILURE); } @@ -144,10 +147,10 @@ int main(int argc, char *argv[]) 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 correctly + uint32 addr = (uint32)strtol(argv[4], NULL, 0); //# strtol() is working with hex-string correctly 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); + fprintf(stderr, "\n\tCan'tt open i2c DEVICE, errno: %d\n\n",errno); print_help(); exit (EXIT_FAILURE); } @@ -160,7 +163,7 @@ int main(int argc, char *argv[]) if(!strcmp("print_all",argv[1]) && print_all != NULL){ //# Gets the device's help message. - print_all(); + print_all(NULL); } else if(!strcmp("read_all",argv[1]) && read_all != NULL){ //# Pulls all readable registers data from device // read_all(argv[5]); -- cgit v1.2.3