Myserial Library extends RawSerial

MySerial.h

Committer:
naao
Date:
2014-06-24
Revision:
9:981384455445
Parent:
7:d5fe75a0a885
Child:
10:930d325f3d31

File content as of revision 9:981384455445:

/** mbed Serial Library extend RawSerial
 * Copyright (c) 2014 Naoki Okino
 *
 * 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.
 */
#ifndef MBED_MYSERIAL_H
#define MBED_MYSERIAL_H

#include "mbed.h"

/** MySerial control class, based on a RawSerial
 *
 * Example:
 * @code
 * #include "mbed.h"
 * #include "MySerial.h"
 * 
 * MySerial pc(USBTX,USBRX);   //instance of MySerial Class
 * char cWord[16]; //receive chars  or cWord[256], cWord[1024] etc..
 * 
 * void readbuf()
 * {
 *   // int iRtn =  pc.GetString(6,&cWord);    //Serial received chars by pointer cWord
 *    int iRtn =  pc.GetString(6,cWord); //Serial received chars byref of cWord
 * }
 * int main() {
 *    pc.baud(9600);                  //set baud rate
 *    pc.format(8, MySerial::None, 1);//set bits for a byte, parity bit, stop bit
 *    pc.SetRxWait(0.01, 0.001);       //set wait getting chars after interrupted, each char
 *    pc.attach( readbuf, MySerial::RxIrq );    //Set Interrupt by Serial receive
 * }
 * @endcode
 */
class MySerial : public RawSerial{
    
public:
    /** constructor to get chars received by serial
     * 
     * @param PinName tx
     * @param PinName rx
     */
    MySerial(PinName tx, PinName rx);
   
    /** set wait getting chars after interrupted
     * 
     * @param float _fRxStartWait wait getting a 1st char after interrupted
     * @param float _fRxEachWait wait getting each char
    */
    void SetRxWait(float _fRxStartWait, float _fRxEachWait);

    /** function to get chars after received shars by serial
     * 
     * @param int size for get chars
     * @param *cWord returns got chars by pointer
     * @param returns success by 0
     */
    virtual int GetString(int size, char *cWord);
    
    /** override function to get chars after received shars by serial
     * 
     * @param int size for get chars
     * @param (&cWord)[16] returns got chars by ref
     * @param returns success by 0
     */
    template <class X> int GetString(int size, X cWord)
    {
        return GetString(size, &cWord);
    }

protected:
    float fRxStartWait;
    float fRxEachWait;
};

#endif