Change tty initialization.
authorWalter Fetter Lages <w.fetter@ieee.org>
Sun, 3 Feb 2019 05:49:03 +0000 (03:49 -0200)
committerWalter Fetter Lages <w.fetter@ieee.org>
Sun, 3 Feb 2019 05:56:26 +0000 (03:56 -0200)
include/bno055_tty.h
lib/bno055_tty.c
test/bno055_test.c

index 61e8688..5a6e342 100644 (file)
@@ -49,18 +49,18 @@ extern s8 BNO055_tty_write(u8 dev_addr, u8 reg_addr, u8 *reg_data, u8 cnt);
 
 /*
  *     \Brief: TTY initilization routine
- *     \Return: status of the initialization
- *     \param fd: tty device file descriptor   
+ *     \Return: 0 on success, -1 on failure and set errno to indicate the error
+ *     \param dev_name: tty device name        
  *     \param bno055: pointer to a bno055 structure
 */
-extern s8 BNO055_tty_init(int fd,struct bno055_t *bno055);
+extern int BNO055_tty_init(const char *dev_name,struct bno055_t *bno055);
 
 /*
  *     \Brief: TTY close routine
- *     \Return: status of the close
+ *     \Return: 0 on success, -1 on failure and set errno to indicate the error
  *     \param bno055: pointer to a bno055 structure
 */
-extern s8 BNO055_tty_close(struct bno055_t *bno055);
+extern int BNO055_tty_close(struct bno055_t *bno055);
 
 
 #ifdef __cplusplus
index 4476599..8e08f71 100644 (file)
@@ -20,6 +20,7 @@
 *******************************************************************************/
 
 #include <errno.h>
+#include <fcntl.h>
 #include <termios.h>
 #include <unistd.h>
 #include <sys/ioctl.h>
 but the Bosch BNO055_driver is already non-reentrant */
 static struct termios old_tty;
 
-s8 BNO055_tty_init(int fd,struct bno055_t *bno055)
+int BNO055_tty_init(const char *dev_name,struct bno055_t *bno055)
 {
        struct termios tty;
        int status;
+       int fd;
        
-       if(tcgetattr(fd,&tty)) return BNO055_ERROR;
+       if((fd=open(dev_name,O_RDWR | O_NOCTTY))==-1) return -1;
+       if(tcgetattr(fd,&tty)) return -1;
        old_tty=tty;
-        if(cfsetspeed(&tty,B115200)) return BNO055_OUT_OF_RANGE;
+        if(cfsetspeed(&tty,B115200)) return -1;
        cfmakeraw(&tty);
        tty.c_cflag&=~CRTSCTS;
        tty.c_cflag|=CLOCAL;
        tty.c_cc[VMIN]=0;
        tty.c_cc[VTIME]=10; /* 1 s timeout */
-        if(tcsetattr(fd,TCSANOW,&tty)) return BNO055_ERROR;
+        if(tcsetattr(fd,TCSANOW,&tty)) return -1;
 
         /* nRESET low */
        status=TIOCM_DTR | TIOCM_RTS;
-       ioctl(fd,TIOCMBIS,&status);
+       if(ioctl(fd,TIOCMBIS,&status)) return -1;
        usleep(1);
        /* nRESET high */
        status=TIOCM_DTR | TIOCM_RTS;
-       ioctl(fd,TIOCMBIC,&status);
+       if(ioctl(fd,TIOCMBIC,&status)) return -1;
        /* Wait BNO055 Power on Reset time */
        usleep(650000);
 
@@ -71,19 +74,23 @@ s8 BNO055_tty_init(int fd,struct bno055_t *bno055)
        bno055->delay_msec=BNO055_delay;
        bno055->dev_addr=fd;
        
-       return BNO055_SUCCESS;
+       return 0;
 }
 
-s8 BNO055_tty_close(struct bno055_t *bno055)
+int BNO055_tty_close(struct bno055_t *bno055)
 {
-        if(tcsetattr(bno055->dev_addr,TCSANOW,&old_tty)) return BNO055_ERROR;
+       int fd=bno055->dev_addr;
+       
+        if(tcsetattr(fd,TCSANOW,&old_tty)) return -1;
 
        bno055->bus_write=NULL;
        bno055->bus_read=NULL;
        bno055->delay_msec=NULL;
        bno055->dev_addr=0;
+       
+       if(close(fd)) return -1;
 
-       return BNO055_SUCCESS;
+       return 0;
 }
 
 s8 BNO055_tty_write(u8 dev_addr,u8 reg_addr,u8 *reg_data,u8 cnt)
index f353a0f..d4d7577 100644 (file)
@@ -19,7 +19,6 @@
 *******************************************************************************/
 
 #include <errno.h>
-#include <fcntl.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
@@ -36,7 +35,6 @@ void usage_help(const char *name)
 int main(int argc,char *argv[])
 {
        int opt;
-       int fd;
        struct bno055_t bno055;
 
        struct bno055_accel_t accel;
@@ -74,13 +72,11 @@ int main(int argc,char *argv[])
                
                        case 't':       
                                /* TTY interface */
-                               if((fd=open(optarg,O_RDWR | O_NOCTTY))==-1)
+                               if(BNO055_tty_init(optarg,&bno055))
                                {
-                                       perror(argv[0]);
+                                       perror("Error while initializing tty interface");
                                        return -errno;
                                }
-                               if(BNO055_tty_init(fd,&bno055) != BNO055_SUCCESS)
-                                       fprintf(stderr,"Error while initializing tty interface.\n");
                                break;
                        
                        default:
@@ -181,6 +177,6 @@ int main(int argc,char *argv[])
        else fprintf(stderr,"Error while reading temperature in degrees Celsius.\n");
 
        BNO055_tty_close(&bno055);
-       close(fd);
+
        return 0;
 }