a program i made a while back to log gps/accelerometer data

Dependencies:   FatFileSystem mbed

SerialBuffered.h

Committer:
Xach
Date:
2012-09-08
Revision:
0:82a02991476c

File content as of revision 0:82a02991476c:

#pragma once

// This is a buffered serial reading class, using the serial interrupt introduced in mbed library version 18 on 17/11/09

// In the simplest case, construct it with a buffer size at least equal to the largest message you
// expect your program to receive in one go.
#define STRING_BUFFERS 24

class SerialBuffered : public Serial {
public:
    SerialBuffered( size_t bufferSize, PinName tx, PinName rx );
    virtual ~SerialBuffered();

    int getc();     // will block till the next character turns up, or return -1 if there is a timeout

    int readable(); // returns 1 if there is a character available to read, 0 otherwise

    int strReady(); // returns the number of strings ready




    void setTimeout( float seconds );    // maximum time in seconds that getc() should block
    // while waiting for a character
    // Pass -1 to disable the timeout.

    size_t readBytes( uint8_t *bytes, size_t requested );    // read requested bytes into a buffer,
    // return number actually read,
    // which may be less than requested if there has been a timeout
    
    void startReadStrings( char term);
    void stopReadStrings();
    char* getString();
    int stringsAvailable();
private:

    void handleInterrupt();


    uint8_t *m_buff;            // points at a circular buffer, containing data from m_contentStart, for m_contentSize bytes, wrapping when you get to the end
    uint16_t  m_contentStart;   // index of first bytes of content
    uint16_t  m_contentEnd;     // index of bytes after last byte of content
    uint16_t m_buffSize;
    float m_timeout;
    Timer m_timer;
    int unreadStrings;
    char term;
    bool ReadStrings;
    char *str_buff[STRING_BUFFERS];
    int str_buff_pos;
    int str_wr_buff_pos;
};