Home | History | Annotate | Download | only in mraa
      1 /*
      2  * Author: Brendan Le Foll <brendan.le.foll (at) intel.com>
      3  * Contributions: Jon Trulson <jtrulson (at) ics.com>
      4  * Contributions: Thomas Ingleby <thomas.c.ingleby (at) intel.com>
      5  * Copyright (c) 2014 - 2015 Intel Corporation.
      6  *
      7  * Permission is hereby granted, free of charge, to any person obtaining
      8  * a copy of this software and associated documentation files (the
      9  * "Software"), to deal in the Software without restriction, including
     10  * without limitation the rights to use, copy, modify, merge, publish,
     11  * distribute, sublicense, and/or sell copies of the Software, and to
     12  * permit persons to whom the Software is furnished to do so, subject to
     13  * the following conditions:
     14  *
     15  * The above copyright notice and this permission notice shall be
     16  * included in all copies or substantial portions of the Software.
     17  *
     18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
     19  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
     20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
     21  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
     22  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
     23  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
     24  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     25  */
     26 
     27 #pragma once
     28 
     29 #include "uart.h"
     30 #include "types.hpp"
     31 #include <stdlib.h>
     32 #include <stdexcept>
     33 #include <cstring>
     34 
     35 namespace mraa
     36 {
     37 
     38 /**
     39  * @brief API to UART (enabling only)
     40  *
     41  * This file defines the UART interface for libmraa
     42  *
     43  * @snippet Uart-example.cpp Interesting
     44  */
     45 class Uart
     46 {
     47   public:
     48     /**
     49      * Uart Constructor, takes a pin number which will map directly to the
     50      * linux uart number, this 'enables' the uart, nothing more
     51      *
     52      * @param uart the index of the uart set to use
     53      */
     54     Uart(int uart)
     55     {
     56         m_uart = mraa_uart_init(uart);
     57 
     58         if (m_uart == NULL) {
     59             throw std::invalid_argument("Error initialising UART");
     60         }
     61     }
     62 
     63     /**
     64      * Uart Constructor, takes a string to the path of the serial
     65      * interface that is needed.
     66      *
     67      * @param uart the index of the uart set to use
     68      */
     69     Uart(std::string path)
     70     {
     71         m_uart = mraa_uart_init_raw(path.c_str());
     72 
     73         if (m_uart == NULL) {
     74             throw std::invalid_argument("Error initialising UART");
     75         }
     76     }
     77 
     78     /**
     79      * Uart destructor
     80      */
     81     ~Uart()
     82     {
     83         mraa_uart_stop(m_uart);
     84     }
     85 
     86     /**
     87      * Get string with tty device path within Linux
     88      * For example. Could point to "/dev/ttyS0"
     89      *
     90      * @return char pointer of device path
     91      */
     92     std::string
     93     getDevicePath()
     94     {
     95         std::string ret_val(mraa_uart_get_dev_path(m_uart));
     96         return ret_val;
     97     }
     98 
     99     /**
    100      * Read bytes from the device into char* buffer
    101      *
    102      * @param data buffer pointer
    103      * @param length maximum size of buffer
    104      * @return numbers of bytes read
    105      */
    106     int
    107     read(char* data, int length)
    108     {
    109         return mraa_uart_read(m_uart, data, (size_t) length);
    110     }
    111 
    112     /**
    113      * Write bytes in String object to a device
    114      *
    115      * @param data buffer pointer
    116      * @param length maximum size of buffer
    117      * @return the number of bytes written, or -1 if an error occurred
    118      */
    119     int
    120     write(const char* data, int length)
    121     {
    122         return mraa_uart_write(m_uart, data, (size_t) length);
    123     }
    124 
    125     /**
    126      * Read bytes from the device into a String object
    127      *
    128      * @param length to read
    129      * @return string of data
    130      */
    131     std::string
    132     readStr(int length)
    133     {
    134         char* data = (char*) malloc(sizeof(char) * length);
    135         int v = mraa_uart_read(m_uart, data, (size_t) length);
    136         std::string ret(data, v);
    137         free(data);
    138         return ret;
    139     }
    140 
    141     /**
    142      * Write bytes in String object to a device
    143      *
    144      * @param string to write
    145      * @return the number of bytes written, or -1 if an error occurred
    146      */
    147     int
    148     writeStr(std::string data)
    149     {
    150         // this is data.length() not +1 because we want to avoid the '\0' char
    151         return mraa_uart_write(m_uart, data.c_str(), (data.length()));
    152     }
    153 
    154     /**
    155      * Check to see if data is available on the device for reading
    156      *
    157      * @param millis number of milliseconds to wait, or 0 to return immediately
    158      * @return true if there is data available to read, false otherwise
    159      */
    160     bool
    161     dataAvailable(unsigned int millis = 0)
    162     {
    163         if (mraa_uart_data_available(m_uart, millis))
    164             return true;
    165         else
    166             return false;
    167     }
    168 
    169     /**
    170      * Flush the outbound data.
    171      * Blocks until complete.
    172      *
    173      * @return Result of operation
    174      */
    175     Result
    176     flush()
    177     {
    178         return (Result) mraa_uart_flush(m_uart);
    179     }
    180 
    181     /**
    182      * Set the baudrate.
    183      * Takes an int and will attempt to decide what baudrate  is
    184      * to be used on the UART hardware.
    185      *
    186      * @param baud unsigned int of baudrate i.e. 9600
    187      * @return Result of operation
    188      */
    189     Result
    190     setBaudRate(unsigned int baud)
    191     {
    192         return (Result) mraa_uart_set_baudrate(m_uart, baud);
    193     }
    194 
    195     /**
    196      * Set the transfer mode
    197      * For example setting the mode to 8N1 would be
    198      * "dev.setMode(8,UART_PARITY_NONE , 1)"
    199      *
    200      * @param bytesize data bits
    201      * @param parity Parity bit setting
    202      * @param stopbits stop bits
    203      * @return Result of operation
    204      */
    205     Result
    206     setMode(int bytesize, UartParity parity, int stopbits)
    207     {
    208         return (Result) mraa_uart_set_mode(m_uart, bytesize, (mraa_uart_parity_t) parity, stopbits);
    209     }
    210 
    211     /**
    212      * Set the flowcontrol
    213      *
    214      * @param xonxoff XON/XOFF Software flow control.
    215      * @param rtscts RTS/CTS out of band hardware flow control
    216      * @return Result of operation
    217      */
    218     Result
    219     setFlowcontrol(bool xonxoff, bool rtscts)
    220     {
    221         return (Result) mraa_uart_set_flowcontrol(m_uart, xonxoff, rtscts);
    222     }
    223 
    224     /**
    225      * Set the timeout for read and write operations
    226      * <= 0 will disable that timeout
    227      *
    228      * @param read read timeout
    229      * @param write write timeout
    230      * @param interchar inbetween char timeout
    231      * @return Result of operation
    232      */
    233     Result
    234     setTimeout(int read, int write, int interchar)
    235     {
    236         return (Result) mraa_uart_set_timeout(m_uart, read, write, interchar);
    237     }
    238 
    239   private:
    240     mraa_uart_context m_uart;
    241 };
    242 }
    243