Subversion Repositories Projects

Rev

Rev 211 | Rev 216 | 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.cpp
18
*
19
*   @brief  This file implements the BioloidBus class, which is an
20
*           abstract base class for the hardware which actually talks
21
*   to the bioloid bus. This hardware usually takes the form of a UART.
22
*
23
****************************************************************************/
24
 
25
// ---- Include Files -------------------------------------------------------
26
 
27
#include "Log.h"
28
#include "Bioloid.h"
29
#include "BioloidBus.h"
213 dhylands 30
#include "BioloidDevice.h"
211 dhylands 31
 
32
// ---- Public Variables ----------------------------------------------------
33
 
34
bool    BioloidBus::m_log   = false;
35
 
36
// ---- Private Constants and Types -----------------------------------------
37
// ---- Private Variables ---------------------------------------------------
38
// ---- Private Function Prototypes -----------------------------------------
39
// ---- Functions -----------------------------------------------------------
40
 
41
/**
42
 * @addtogroup bioloid
43
 * @{
44
 */
45
 
46
//***************************************************************************
47
/**
48
*   Constructor
49
*/
50
 
51
BioloidBus::BioloidBus()
52
{
53
}
54
 
55
//***************************************************************************
56
/**
57
*   Destructor
58
*
59
*   virtual
60
*/
61
 
62
BioloidBus::~BioloidBus()
63
{
64
}
65
 
66
//***************************************************************************
67
/**
213 dhylands 68
*   Reads a packet. Returns true if a packet was read successfully,
69
*   false if a timeout or error occurred.
70
*
71
*   virtual
72
*/
73
 
74
bool BioloidBus::ReadStatusPacket( BioloidPacket *pkt )
75
{
76
    Bioloid::Error err;
77
 
78
    do
79
    {
80
        uint8_t         ch;
81
 
82
        if ( !ReadByte( &ch ))
83
        {
84
            return false;
85
        }
86
 
87
        err = pkt->ProcessChar( ch );
88
 
89
    } while ( err == Bioloid::ERROR_NOT_DONE );
90
 
91
    return err == Bioloid::ERROR_NONE;
92
}
93
 
94
//***************************************************************************
95
/**
96
*   Scans the bus, calling the passed callback for each device
97
*   ID which responds.
98
*/
99
 
100
bool BioloidBus::Scan( bool (*devFound)( BioloidBus *bus, BioloidDevice *dev ))
101
{
102
    Bioloid::ID_t   id;
103
    BioloidDevice   scanDev;
104
    bool            someDevFound = false;
105
 
106
    for ( id = 0; id < Bioloid::BROADCAST_ID; id++ )
107
    {
108
        scanDev.SetBusAndID( this, id );
109
 
110
        if ( scanDev.Ping() == Bioloid::ERROR_NONE )
111
        {
112
            someDevFound = true;
113
 
114
            if ( !devFound( this, &scanDev ))
115
            {
116
                break;
117
            }
118
        }
119
    }
120
 
121
    return someDevFound;
122
}
123
 
124
//***************************************************************************
125
/**
211 dhylands 126
*   Broadcasts an action packet to all of the devices on the bus.
127
*   This causes all of the devices to perform their deferred writes
128
*   at the same time.
129
*/
130
 
131
void BioloidBus::SendAction()
132
{
133
    BLD_BUS_LOG( "Sending ACTION\n" );
134
 
135
    SendCmdHeader( Bioloid::BROADCAST_ID, 0, Bioloid::CMD_ACTION );
136
    SendCheckSum();
137
}
138
 
139
//***************************************************************************
140
/**
213 dhylands 141
*   Send the checksum. Since the checksum byte is the last byte of the
142
*   packet, this function is made virtual to allow bus drivers to
143
*   buffer the packet bytes until the entire packet is ready to send.
144
*
145
*   virtual
146
*/
147
 
148
void BioloidBus::SendCheckSum()
149
{
150
    SendByte( ~m_checksum );
151
}
152
 
153
//***************************************************************************
154
/**
211 dhylands 155
*   Sends 'len' bytes, returning the sum of the bytes for easy accumulation
156
*   into the checksum)
157
*/
158
 
213 dhylands 159
void BioloidBus::SendData( uint8_t len, const void *voidData )
211 dhylands 160
{
213 dhylands 161
    const uint8_t *data = static_cast< const uint8_t *>( voidData );
162
 
211 dhylands 163
    while ( len-- > 0 )
164
    {
165
        SendByte( *data++ );
166
    }
167
}
168
 
169
//***************************************************************************
170
/**
171
*   Sends the command header, which is common to all of the commands.
172
*   2 is added to paramLen (to cover the length and cmd bytes). This
173
*   way the caller is only responsible for figuring out how many extra
174
*   parameter bytes are being sent.
213 dhylands 175
*
176
*   virtual
211 dhylands 177
*/
178
 
179
void BioloidBus::SendCmdHeader
180
(
181
    Bioloid::ID_t       id,
182
    uint8_t             paramLen,
183
    Bioloid::Command    cmd
184
)
185
{
186
    SendByte( 0xff );
187
    SendByte( 0xff );
188
 
189
    m_checksum = 0;
190
 
191
    SendByte( id );
192
    SendByte( paramLen + 2 );
193
    SendByte( cmd );
194
}
195
 
196
/** @} */
197