Subversion Repositories Projects

Rev

Rev 211 | Rev 222 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
211 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   BioloidBus.h
18
*
19
*   @brief  Contains the definitions for a bioloid bus. Essentially
20
*           there is a bus for each UART with bioloid devices attached.
21
*
22
****************************************************************************/
23
 
24
#if !defined( BIOLOIDBUS_H )
25
#define BIOLOIDBUS_H       /**< Include Guard                             */
26
 
27
// ---- Include Files -------------------------------------------------------
28
 
29
#include <stdint.h>
30
#include "Bioloid.h"
213 dhylands 31
#include "BioloidPacket.h"
211 dhylands 32
 
213 dhylands 33
class BioloidDevice;
34
 
211 dhylands 35
/**
36
 * @addtogroup bioloid
37
 * @{
38
 */
39
 
40
#define BLD_BUS_LOG( fmt, args... ) do { if ( BioloidBus::m_log ) { Log( fmt, ##args ); }} while (0)
41
 
42
class BioloidBus
43
{
44
public:
45
 
46
    //------------------------------------------------------------------------
47
    // Default constructor
48
 
49
    BioloidBus();
50
 
51
    //------------------------------------------------------------------------
52
    // Destructor
53
 
54
    virtual ~BioloidBus();
55
 
56
    //------------------------------------------------------------------------
213 dhylands 57
    // Scans the bus, calling the passed callback for each device
58
    // ID which responds.
59
 
60
    bool Scan( bool (*devFound)( BioloidBus *bus, BioloidDevice *dev ));
61
 
62
    //------------------------------------------------------------------------
211 dhylands 63
    // Broadcasts an action packet to all of the devices on the bus.
64
    // This causes all of the devices to perform their deferred writes
65
    // at the same time.
66
 
67
    void SendAction();
68
 
69
    //------------------------------------------------------------------------
70
    // Sends a byte. This will automatically accumulate the byte into 
71
    // the checksum
72
 
73
    virtual void SendByte( uint8_t data ) = 0;
74
 
75
    //------------------------------------------------------------------------
213 dhylands 76
    // Reads a byte. This function returns true if a character was read, and
77
    // returns false if no character is received within the designated
78
    // timeout.
79
    // 
80
    // The max Return Delay time is 254 x 2 usec = 508 usec (the default
81
    // is 500 usec). This represents the minimum time between receiving a
82
    // packet and sending the response.
211 dhylands 83
 
213 dhylands 84
    virtual bool ReadByte( uint8_t *ch ) = 0;
211 dhylands 85
 
86
    //------------------------------------------------------------------------
213 dhylands 87
    // Send the checksum. Since the checksum byte is the last byte of the
88
    // packet, this function is made virtual to allow bus drivers to
89
    // buffer the packet bytes until the entire packet is ready to send.
90
 
91
    virtual void SendCheckSum();
92
 
93
    //------------------------------------------------------------------------
211 dhylands 94
    // Sends 'len' bytes
95
 
213 dhylands 96
    void SendData( uint8_t len, const void *data );
211 dhylands 97
 
98
    //------------------------------------------------------------------------
99
    // Sends the command header, which is common to all of the commands.
100
    // 2 is added to paramLen (to cover the length and cmd bytes). This
101
    // way the caller is only responsible for figuring out how many extra
102
    // parameter bytes are being sent.
103
 
213 dhylands 104
    virtual void SendCmdHeader( Bioloid::ID_t id, uint8_t paramLen, Bioloid::Command cmd );
211 dhylands 105
 
213 dhylands 106
    //------------------------------------------------------------------------
107
    // Reads a packet. Returns true if a packet was read successfully,
108
    // false if a timeout or error occurred.
109
 
110
    virtual bool ReadStatusPacket( BioloidPacket *pkt );
111
 
211 dhylands 112
    static  bool    m_log;
113
 
114
protected:
115
 
116
    uint8_t     m_checksum;
117
 
118
private:
119
 
120
    //------------------------------------------------------------------------
121
    // The copy constructor and assignment operator are not need for this
122
    // class so we declare them private and don't provide an implementation.
123
 
124
    BioloidBus( const BioloidBus & copy );
125
    BioloidBus &operator =( const BioloidBus &rhs );
126
};
127
 
128
/** @} */
129
 
130
#endif /* BIOLOIDBUS_H */
131