2015-06-02 08:43:14 +01:00
|
|
|
/*
|
|
|
|
|
* Author: Brendan Le Foll <brendan.le.foll@intel.com>
|
2018-01-24 15:09:29 +05:30
|
|
|
* Contributors: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
|
2015-06-02 08:43:14 +01:00
|
|
|
* 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.
|
2018-01-24 15:09:29 +05:30
|
|
|
*
|
|
|
|
|
* Example usage: Prints "Hello Mraa!" recursively. Press Ctrl+C to exit
|
|
|
|
|
*
|
2015-06-02 08:43:14 +01:00
|
|
|
*/
|
|
|
|
|
|
2018-01-24 15:09:29 +05:30
|
|
|
/* standard headers */
|
2017-11-17 17:22:34 +05:30
|
|
|
#include <iostream>
|
2018-01-24 15:09:29 +05:30
|
|
|
#include <signal.h>
|
|
|
|
|
#include <stdlib.h>
|
2017-11-17 17:22:34 +05:30
|
|
|
#include <unistd.h>
|
2015-06-02 08:43:14 +01:00
|
|
|
|
2018-01-24 15:09:29 +05:30
|
|
|
/* 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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-06-02 08:43:14 +01:00
|
|
|
|
|
|
|
|
int
|
2018-01-24 15:09:29 +05:30
|
|
|
main(void)
|
2015-06-02 08:43:14 +01:00
|
|
|
{
|
2018-01-24 15:09:29 +05:30
|
|
|
signal(SIGINT, sig_handler);
|
|
|
|
|
|
2015-06-02 08:43:14 +01:00
|
|
|
//! [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
|
2018-01-24 15:09:29 +05:30
|
|
|
mraa::Uart* uart;
|
2015-06-02 08:43:14 +01:00
|
|
|
try {
|
2018-01-24 15:09:29 +05:30
|
|
|
uart = new mraa::Uart(UART_PORT);
|
2015-06-02 08:43:14 +01:00
|
|
|
} catch (std::exception& e) {
|
2018-01-24 15:09:29 +05:30
|
|
|
std::cerr << e.what() << ", likely invalid platform config" << std::endl;
|
2015-06-02 08:43:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
2018-01-24 15:09:29 +05:30
|
|
|
uart = new mraa::Uart(dev_path);
|
2015-06-02 08:43:14 +01:00
|
|
|
} catch (std::exception& e) {
|
2018-01-24 15:09:29 +05:30
|
|
|
std::cerr << "Error while setting up raw UART, do you have a uart?" << std::endl;
|
2015-06-02 08:43:14 +01:00
|
|
|
std::terminate();
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-24 15:09:29 +05:30
|
|
|
if (uart->setBaudRate(115200) != mraa::SUCCESS) {
|
|
|
|
|
std::cerr << "Error setting parity on UART" << std::endl;
|
2015-06-02 08:43:14 +01:00
|
|
|
}
|
|
|
|
|
|
2018-01-24 15:09:29 +05:30
|
|
|
if (uart->setMode(8, mraa::UART_PARITY_NONE, 1) != mraa::SUCCESS) {
|
|
|
|
|
std::cerr << "Error setting parity on UART" << std::endl;
|
2015-06-02 08:43:14 +01:00
|
|
|
}
|
|
|
|
|
|
2018-01-24 15:09:29 +05:30
|
|
|
if (uart->setFlowcontrol(false, false) != mraa::SUCCESS) {
|
|
|
|
|
std::cerr << "Error setting flow control UART" << std::endl;
|
2015-06-02 08:43:14 +01:00
|
|
|
}
|
|
|
|
|
|
2018-01-24 15:09:29 +05:30
|
|
|
while (flag) {
|
|
|
|
|
/* send data through uart */
|
|
|
|
|
uart->writeStr("Hello Mraa!");
|
|
|
|
|
|
|
|
|
|
sleep(1);
|
|
|
|
|
}
|
2015-06-02 08:43:14 +01:00
|
|
|
//! [Interesting]
|
|
|
|
|
|
2018-01-24 15:09:29 +05:30
|
|
|
delete uart;
|
2015-06-02 08:43:14 +01:00
|
|
|
|
2018-01-24 15:09:29 +05:30
|
|
|
return EXIT_SUCCESS;
|
2015-06-02 08:43:14 +01:00
|
|
|
}
|