Subversion Repositories Projects

Rev

Rev 229 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
213 dhylands 1
/****************************************************************************
2
*
3
*   Copyright (c) 2009 Dave Hylands     <dhylands@gmail.com>
4
*
5
*   This program is free software; you can redistribute it and/or modify
6
*   it under the terms of the GNU General Public License version 2 as
7
*   published by the Free Software Foundation.
8
*
9
*   Alternatively, this software may be distributed under the terms of BSD
10
*   license.
11
*
12
*   See README and COPYING for more details.
13
*
14
****************************************************************************/
15
/**
16
*
17
*   @file   SerialBus.h
18
*
19
*   @brief  Implements a bioloid bus using posix serial. This typically
20
*           assumes that an FTDI USB-to-serial adapter is being used
21
*   to do the RS-485 management.
22
*
23
****************************************************************************/
24
 
25
#if !defined( SERIALBUS_H )
26
#define SERIALBUS_H       /**< Include Guard                             */
27
 
28
// ---- Include Files -------------------------------------------------------
29
 
30
#include "BioloidBus.h"
31
#include "SerialPort.h"
32
 
33
/**
34
 * @addtogroup bioloid
35
 * @{
36
 */
37
 
38
class SerialBus : public BioloidBus
39
{
40
public:
41
    //------------------------------------------------------------------------
42
    // Default constructor
43
 
44
    SerialBus();
45
 
46
    //------------------------------------------------------------------------
47
    // Destructor
48
 
49
    virtual ~SerialBus();
50
 
51
    //------------------------------------------------------------------------
52
    // Sets the serial port which will be used for communications
53
 
54
    void SetSerialPort( SerialPort *serPort );
55
 
56
    //------------------------------------------------------------------------
57
    // Reads a byte.
58
 
59
    virtual bool ReadByte( uint8_t *ch );
60
 
61
    //------------------------------------------------------------------------
62
    // Reads a packet. Returns true if a packet was read successfully,
63
    // false if a timeout or error occurred.
64
 
65
    virtual bool ReadStatusPacket( BioloidPacket *pkt );
66
 
67
    //------------------------------------------------------------------------
68
    // Sends a byte. This will automatically accumulate the byte into 
69
    // the checksum
70
 
71
    virtual void SendByte( uint8_t data );
72
 
73
    //------------------------------------------------------------------------
74
    //  Send the checksum. Since the checksum byte is the last byte of the
75
    //  packet, this function is made virtual to allow bus drivers to
76
    //  buffer the packet bytes until the entire packet is ready to send.
77
 
78
    virtual void SendCheckSum();
79
 
80
    //------------------------------------------------------------------------
81
    // Sends the command header, which is common to all of the commands.
82
    // 2 is added to paramLen (to cover the length and cmd bytes). This
83
    // way the caller is only responsible for figuring out how many extra
84
    // parameter bytes are being sent.
85
 
86
    virtual void SendCmdHeader( Bioloid::ID_t id, uint8_t paramLen, Bioloid::Command cmd );
87
 
88
    //------------------------------------------------------------------------
89
    // Sets the debug mode
90
 
91
    void SetDebug( bool debug ) { m_debug = debug; }
92
 
93
private:
94
 
95
    //------------------------------------------------------------------------
96
    // Adds a byte to the buffer of data to send.
97
 
98
    void BufferByte( uint8_t data );
99
 
100
    //------------------------------------------------------------------------
101
    // Writes all of the buffered bytes to the serial port.
102
 
103
    void WriteBuffer();
104
 
105
    //------------------------------------------------------------------------
106
 
107
    SerialPort *m_serialPort;
108
 
109
    bool        m_debug;
110
 
111
    int         m_fd;
112
    int         m_dataBytes;
113
    uint8_t     m_data[ 128 ];
114
 
115
};
116
 
117
/** @} */
118
 
119
#endif /* SERIALBUS_H */
120