Eric Wu / Mbed 2 deprecated WifiRobot

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers irobotUART.cpp Source File

irobotUART.cpp

00001 #include "irobotUART.h"
00002 
00003 /*
00004 Implementation for iRobot Create UART connection using mbed, Freescale KL25Z
00005 
00006 (This implementation is architecture specific and relies on the Serial class
00007     in the mbed SDK and TimeoutMultipleSerial, which was written for this project)
00008 
00009 The interface was written by Jeff C. Jensen, 2013-12-09 and is 
00010     Copyright (C) 2013, Jeff C. Jensen, Edward A. Lee, and Sanjit A. Seshia.
00011     This software accompanies An Introductory Lab in Embedded and Cyber-Physical Systems                  and is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0
00012 
00013 The implementation was written by Eric Wu, 2014-11-10, and is based heavily off of the 
00014     original implementation by Jeff C. Jensen.
00015 */
00016 
00017 /// Convert a baud code into its actual rate
00018 /// \return error code
00019 static int32_t irobotUARTBaudCodeToRate(
00020     const irobotBaud_t  baud,       ///< [in]   baud code
00021     int* const  rate        ///< [out]  baud rate
00022 ){
00023     if(!rate){
00024         return ERROR_INVALID_PARAMETER;
00025     }
00026     else{
00027         switch(baud){
00028         case IROBOT_BAUD_300:   *rate = 300;    break;
00029         case IROBOT_BAUD_600:   *rate = 600;    break;
00030         case IROBOT_BAUD_1200:  *rate = 1200;   break;
00031         case IROBOT_BAUD_2400:  *rate = 2400;   break;
00032         case IROBOT_BAUD_4800:  *rate = 4800;   break;
00033         case IROBOT_BAUD_9600:  *rate = 9600;   break;
00034         case IROBOT_BAUD_14400: *rate = 14400;  break;
00035         case IROBOT_BAUD_19200: *rate = 19200;  break;
00036         case IROBOT_BAUD_28800: *rate = 28800;  break;
00037         case IROBOT_BAUD_38400: *rate = 38400;  break;
00038         case IROBOT_BAUD_57600: *rate = 57600;  break;
00039         case IROBOT_BAUD_115200:*rate = 115200; break;  // WARNING: UNSTABLE
00040         default:
00041             *rate = 0;
00042             return ERROR_INVALID_PARAMETER;
00043             break;
00044         }
00045 
00046         return ERROR_SUCCESS;
00047     }
00048 }
00049 
00050 int32_t irobotUARTOpen(
00051     const irobotUARTPort_t port,
00052     const irobotBaud_t baud) {
00053 
00054     int32_t status = ERROR_SUCCESS;
00055     int baudRate = 0;
00056 
00057     irobot_StatusMerge(&status, irobotUARTBaudCodeToRate(baud, &baudRate));
00058 
00059     if (!irobot_IsError(status)) {
00060         port->setBaud(baudRate);
00061         port->setFormat(8, Serial::None, 1);
00062     }
00063 
00064     return status;
00065 }
00066 
00067 int32_t irobotUARTClose(const irobotUARTPort_t port) {
00068     return ERROR_SUCCESS;
00069 }
00070 
00071 int32_t irobotUARTRead(
00072     const irobotUARTPort_t port,
00073     xqueue_t* const queue,
00074     size_t nData) {
00075 
00076     // catch NULL pointers
00077     int32_t status = ERROR_SUCCESS;
00078     if (!queue) {
00079         irobot_StatusMerge(&status, ERROR_INVALID_PARAMETER);
00080     }
00081 
00082     // read
00083     while (!irobot_IsError(status) && nData--) {
00084         uint8_t rxByte = 0;
00085         irobot_StatusMerge(&status, irobotUARTReadRaw(port, &rxByte, 1));
00086         if (!irobot_IsError(status)) {
00087             xqueue_push8(queue, rxByte);
00088         }
00089     }
00090 
00091     return status;
00092 }
00093 
00094 int32_t irobotUARTReadRaw(
00095     const irobotUARTPort_t port,
00096     uint8_t* const data,
00097     const size_t nData) {
00098 
00099     if (!data) {
00100         return ERROR_INVALID_PARAMETER;
00101     } else {
00102         int status = port->readMultChars(data, nData);
00103         if (status == -1) return ERROR_TIMEOUT;
00104         return ERROR_SUCCESS;
00105     }
00106 }
00107 
00108 int32_t irobotUARTWrite(
00109     const irobotUARTPort_t port,
00110     xqueue_t* const queue) {
00111 
00112     int32_t status = ERROR_SUCCESS;
00113 
00114     // catch NULL pointers
00115     if(!queue){
00116         irobot_StatusMerge(&status, ERROR_INVALID_PARAMETER);
00117     }
00118     
00119     // write
00120     while(!irobot_IsError(status) && !xqueue_empty(queue)){
00121         uint8_t txByte = xqueue_front(queue);
00122         irobot_StatusMerge(&status, irobotUARTWriteRaw(port, &txByte, 1));
00123         if(!irobot_IsError(status)){
00124             xqueue_drop(queue);
00125         }
00126     }
00127 
00128     return status;
00129 
00130 }
00131 
00132 int32_t irobotUARTWriteRaw(
00133     const irobotUARTPort_t port,
00134     const uint8_t* const data,
00135     const size_t nData) {
00136 
00137     if (!data) {
00138         return ERROR_INVALID_PARAMETER;
00139     } else {
00140         int status = port->writeMultChars(data, nData);
00141         if (status == -1) return ERROR_TIMEOUT;
00142         return ERROR_SUCCESS;
00143     }
00144 }
00145 
00146 int32_t irobotUARTClear(
00147     const irobotUARTPort_t port) {
00148     port->clearAll();
00149     return ERROR_SUCCESS;
00150 }