Private
Public Access
2
0
Files
mraa/examples/c++/uart.cpp

102 lines
2.9 KiB
C++
Raw Normal View History

/*
* Author: Brendan Le Foll <brendan.le.foll@intel.com>
* Contributors: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
* Copyright (c) 2015 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* Example usage: Prints "Hello Mraa!" recursively. Press Ctrl+C to exit
*
*/
/* standard headers */
#include <iostream>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
/* mraa headers */
#include "mraa/common.hpp"
#include "mraa/uart.hpp"
/* UART port */
#define UART_PORT 0
const char* dev_path = "/dev/ttyS0";
volatile sig_atomic_t flag = 1;
void
sig_handler(int signum)
{
if (signum == SIGINT) {
std::cout << "Exiting..." << std::endl;
flag = 0;
}
}
int
main(void)
{
signal(SIGINT, sig_handler);
//! [Interesting]
// If you have a valid platform configuration use numbers to represent uart
// device. If not use raw mode where std::string is taken as a constructor
// parameter
mraa::Uart* uart;
try {
uart = new mraa::Uart(UART_PORT);
} catch (std::exception& e) {
std::cerr << e.what() << ", likely invalid platform config" << std::endl;
}
try {
uart = new mraa::Uart(dev_path);
} catch (std::exception& e) {
std::cerr << "Error while setting up raw UART, do you have a uart?" << std::endl;
std::terminate();
}
if (uart->setBaudRate(115200) != mraa::SUCCESS) {
std::cerr << "Error setting parity on UART" << std::endl;
}
if (uart->setMode(8, mraa::UART_PARITY_NONE, 1) != mraa::SUCCESS) {
std::cerr << "Error setting parity on UART" << std::endl;
}
if (uart->setFlowcontrol(false, false) != mraa::SUCCESS) {
std::cerr << "Error setting flow control UART" << std::endl;
}
while (flag) {
/* send data through uart */
uart->writeStr("Hello Mraa!");
sleep(1);
}
//! [Interesting]
delete uart;
return EXIT_SUCCESS;
}