From 2048514fb6f1df73cdd4d5d0a05040e77e0876ce Mon Sep 17 00:00:00 2001 From: Nick Van Doorn Date: Fri, 28 Sep 2018 21:19:59 -0700 Subject: Import serial routines from util --- Component.cdef | 4 +- camera.c | 118 ++------------------------------------------------------- camera.h | 9 ++--- 3 files changed, 10 insertions(+), 121 deletions(-) diff --git a/Component.cdef b/Component.cdef index 539e98e..25975ff 100644 --- a/Component.cdef +++ b/Component.cdef @@ -1,9 +1,11 @@ cflags: { - -std=c99 + -std=c99 + -I$BRNKL_ROOT/apps/util } sources: { camera.c + $BRNKL_ROOT/apps/util/util.c } diff --git a/camera.c b/camera.c index 98e1798..7070d1a 100644 --- a/camera.c +++ b/camera.c @@ -1,113 +1,10 @@ #include "camera.h" #include "legato.h" #include +#include "util.h" COMPONENT_INIT {} -/** - * Convert an integer baud rate to a speed_t - */ -speed_t fd_convertBaud(int baud) { - speed_t b; - switch (baud) { - case 50: - b = B50; - break; - case 75: - b = B75; - break; - case 110: - b = B110; - break; - case 134: - b = B134; - break; - case 150: - b = B150; - break; - case 200: - b = B200; - break; - case 300: - b = B300; - break; - case 600: - b = B600; - break; - case 1200: - b = B1200; - break; - case 1800: - b = B1800; - break; - case 2400: - b = B2400; - break; - case 9600: - b = B9600; - break; - case 19200: - b = B19200; - break; - case 38400: - b = B38400; - break; - case 57600: - b = B57600; - break; - case 115200: - b = B115200; - break; - case 230400: - b = B230400; - break; - default: - b = -2; - } - return b; -} - -/** - * Open a serial connection on device - * - * Lifted from here - * https://github.com/WiringPi/WiringPi/blob/master/wiringPi/wiringSerial.c - */ -int fd_openSerial(const char* device, int baud) { - struct termios options; - speed_t binaryBaud = fd_convertBaud(baud); - int status, fd; - if ((fd = open(device, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK)) == -1) - return -1; - fcntl(fd, F_SETFL, O_RDWR); - tcgetattr(fd, &options); - cfmakeraw(&options); - cfsetispeed(&options, binaryBaud); - cfsetospeed(&options, binaryBaud); - - options.c_cflag |= (CLOCAL | CREAD); - options.c_cflag &= ~PARENB; - options.c_cflag &= ~CSTOPB; - options.c_cflag &= ~CSIZE; - options.c_cflag |= CS8; - options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); - options.c_oflag &= ~OPOST; - - options.c_cc[VMIN] = 0; - options.c_cc[VTIME] = 100; // Ten seconds (100 deciseconds) - - tcsetattr(fd, TCSANOW | TCSAFLUSH, &options); - - ioctl(fd, TIOCMGET, &status); - - status |= TIOCM_DTR; - status |= TIOCM_RTS; - - ioctl(fd, TIOCMSET, &status); - usleep(10000); // 10mS - return fd; -} - /** * Open a serial connection to the camera */ @@ -132,14 +29,6 @@ ssize_t fd_getByte(int fd, uint8_t* data) { return read(fd, data, 1); } -/** - * Check how many bytes are available - * on the serial connection described by fd - */ -int fd_dataAvail(int fd, int* data) { - return ioctl(fd, FIONREAD, data); -} - /** * Send a command sequence to cam * Should not be called directly (use cam_runCommand) @@ -545,10 +434,11 @@ bool cam_readImageToFile(Camera* cam, const char* path, char* imgPath) { if (filePtr != NULL) { LE_INFO("File pointer valid"); success = cam_readImageBlocks(cam, filePtr); - if (success) + if (success) { LE_INFO("Successfully wrote image to %s", imgPath); - else + } else { LE_INFO("Failed to write photo data to %s", imgPath); + } fclose(filePtr); } else { LE_ERROR("Invalid file pointer for %s", imgPath); diff --git a/camera.h b/camera.h index a3291a4..fc84c15 100644 --- a/camera.h +++ b/camera.h @@ -46,9 +46,10 @@ #define CAM_SERIAL 0 #define CAM_BAUD_RATE 38400 #define TTY_TIMEOUT 5000 +#define MAX_PATH_SIZE 256 typedef struct { - char devPath[256]; + char devPath[MAX_PATH_SIZE]; int fd; // file descriptor for the serial port uint8_t serialNum; // camera serial number uint8_t buff[CAM_BUFF_SIZE]; // uint8_t array to store camera data @@ -120,11 +121,7 @@ bool cam_runCommand(Camera* cam, void cam_sendCommand(Camera* cam, uint8_t cmd, uint8_t args[], uint8_t nArgs); // Serial/file descriptor helpers -int fd_dataAvail(int fd, int* data); -ssize_t fd_getByte(int fd, uint8_t* data); LE_SHARED int fd_closeCam(int fd); LE_SHARED int fd_openCam(char* devPath); -int fd_openSerial(const char* device, int baud); -speed_t fd_convertBaud(int baud); - +ssize_t fd_getByte(int fd, uint8_t* data); #endif -- cgit v1.2.3