view main/packages/common/chrobotics/um7.h @ 249:c36e268a624d main

Checkpoint for the UM7 parser, data structure definitions.
author Bob Cook <bob@bobcookdev.com>
date Mon, 24 Aug 2015 20:57:04 -0700
parents 44333100915a
children
line wrap: on
line source

// ----------------------------------------------------------------------------------------
//
//  common/chrobotics/um7.h
//    
//  Bob Cook Development, Robotics Library
//  http://www.bobcookdev.com/rl/
//
//  Implements a state machine for parsing packets from the CHRobotics UM7.
//
//  Copyright (c) 2015 Bob Cook
//  
//  Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
//  
//  The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
//  
//  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.
//  
//  Note: this package requires 73 bytes of RAM for state information.
//
// ----------------------------------------------------------------------------------------

#if !defined( BCDRL_COMMON_CHROBOTICS_UM7_H )
#define BCDRL_COMMON_CHROBOTICS_UM7_H

#include <stdint.h>

// ----------------------------------------------------------------------------------------
//  um7_parse
//
//      Accept another byte for the UM7 packet parsing state machine.
//
//      Returns true when a full packet is correctly decoded.
//

bool um7_parse( uint8_t byte );

// ----------------------------------------------------------------------------------------
//  um7_error_count
//
//      Returns the accumulated parse error count.
//

uint8_t um7_error_count();

// ----------------------------------------------------------------------------------------
//  um7_packet_address
//
//      Return the address byte from the completed packet.
//
//      This value will only be meaningful if um7_parse() returns true.
//

uint8_t um7_packet_address();

// ----------------------------------------------------------------------------------------
//  um7_packet_length
//
//      Return the number of bytes received in the packet date.
//
//      This value will only be meaningful if um7_parse() returns true.
//

uint8_t um7_packet_length();

// ----------------------------------------------------------------------------------------
//  um7_packet_value_*
//
//      Retrieve a value of a specifed size from the completed packet. The offset is the
//      the number of bytes from the start of the packet data.
//
//      This value will only be meaningful if um7_parse() returns true.
//

int8_t   um7_packet_value_s08( uint8_t offset );
uint8_t  um7_packet_value_u08( uint8_t offset );

int16_t  um7_packet_value_s16( uint8_t offset );
uint16_t um7_packet_value_u16( uint8_t offset );

int32_t  um7_packet_value_s32( uint8_t offset );
uint32_t um7_packet_value_u32( uint8_t offset );

// ----------------------------------------------------------------------------------------
//  um7_packet_data
//
//      Retrieve the entire data from the completed packet. The caller must provide a
//      buffer large enough to contain the full packet data. Returns the number of bytes
//      copied into the caller's buffer.
//
//      This value will only be meaningful if um7_parse() returns true.
//

uint8_t um7_packet_data( uint8_t* buffer, uint8_t buffer_len );

// ----------------------------------------------------------------------------------------
//  um7_create_packet
//
//      Construct a full packet ready to be sent to the UM7. The supplied buffer must
//      be at least seven bytes larger than the input data. Its ok to provide NULL for
//      the data ptr if the data length is zero. The maximum data length is 64 bytes.
//
//      Returns the number of bytes constructed in the ouput buffer.
//

static const uint8_t um7_min_packet_size =  7; // zero data
static const uint8_t um7_max_packet_size = 71; // maximum data

uint8_t um7_create_packet( uint8_t*       buffer,
                           uint8_t        address,
                           const uint8_t* data,
                           uint8_t        data_len );


#endif  // #if defined( BCDRL_COMMON_CHROBOTICS_UM7_H )
// ----------------------------------------------------------------------------------------