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 --- src/main.c | 43 +++++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 20 deletions(-) (limited to 'src/main.c') 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