diff --git a/api/mraa/common.h b/api/mraa/common.h index 6e5109d..61063f4 100644 --- a/api/mraa/common.h +++ b/api/mraa/common.h @@ -265,6 +265,14 @@ int mraa_spi_lookup(const char* spi_name); */ int mraa_pwm_lookup(const char* pwm_name); +/** + * Get UART index by name, board must be initialised. + * + * @param uart_name: Name of UART. Eg:UART1 + * @return int of MRAA index for UART, or -1 if not found. + */ +int mraa_uart_lookup(const char* uart_name); + /** * Get default i2c bus, board must be initialised. * diff --git a/api/mraa/common.hpp b/api/mraa/common.hpp index c9b1888..0619a36 100644 --- a/api/mraa/common.hpp +++ b/api/mraa/common.hpp @@ -300,6 +300,27 @@ getPwmLookup(std::string pwm_name) return index; } +/** + * Get UART index by UART name, board must be initialised. + * + * @param pwm_name: Name of the UART. Eg: UART2 + * @throws std::invalid_argument if name is not found + * @return MRAA index for the UART + */ +inline int +getUartLookup(std::string uart_name) +{ + int index = mraa_uart_lookup(uart_name.c_str()); + + if (index < 0) { + std::ostringstream oss; + oss << "UART name " << uart_name << " is not valid"; + throw std::invalid_argument(oss.str()); + } + + return index; +} + /** * Sets the log level to use from 0-7 where 7 is very verbose. These are the * syslog log levels, see syslog(3) for more information on the levels. diff --git a/src/mraa.c b/src/mraa.c index 3d0c4e1..20153c2 100644 --- a/src/mraa.c +++ b/src/mraa.c @@ -903,6 +903,27 @@ mraa_pwm_lookup(const char* pwm_name) return -1; } +int +mraa_uart_lookup(const char* uart_name) +{ + int i; + + if (plat == NULL) { + return -1; + } + + if (uart_name == NULL || strlen(uart_name) == 0) { + return -1; + } + + for (i = 0; i < plat->uart_dev_count; i++) { + if (plat->uart_dev[i].name != NULL && strcmp(uart_name, plat->uart_dev[i].name) == 0) { + return plat->uart_dev[i].index; + } + } + return -1; +} + int mraa_get_default_i2c_bus(uint8_t platform_offset) {