common.c 4.26 KB
/*
*********************************************************************************************************
*	                                  
*	模块名称 : 
*	文件名称 : 
*	版    本 : V1.0
*	说    明 : 
*	修改记录 :
*		版本号  日期       作者    说明
*
*	Copyright (C), 2016-2017, 湖南驰众机器人 www.cizon.com.cn
*
*********************************************************************************************************
*/
#include "common.h"

void BitToByte(unsigned char byte_data,unsigned char bit_len,unsigned char byte_buf[],unsigned short *byte_offset,unsigned short *bit_offset)
{
    unsigned int byteOffset,bitOffset;
    unsigned char mask[8]={0x01,0x03,0x07,0x0F,0x1F,0x3F,0x7F,0xFF};

    byteOffset = *byte_offset;
    bitOffset  = *bit_offset;

    if((bit_len == 0) || (bit_len > 8) || (bitOffset == 0) || (bitOffset > 8))
        return;

    byte_data &= mask[bit_len-1];
    
    if(bit_len <= bitOffset)
    {
        byte_buf[byteOffset] |= (byte_data << (bitOffset - bit_len));
        bitOffset = bitOffset - bit_len;
    }
    else
    {
        byte_buf[byteOffset] |= (byte_data >> (bit_len - bitOffset));
        byteOffset++;
        byte_buf[byteOffset]  = (byte_data << (8 - (bit_len - bitOffset)));
        bitOffset = (8 - (bit_len - bitOffset));
    }

    if(bitOffset == 0)
    {
        byteOffset++;
        bitOffset = 8;
    }

    *byte_offset = byteOffset;
    *bit_offset  = bitOffset;
}

void SetBitToByte(unsigned long data,unsigned char bit_len,unsigned char byte_buf[],unsigned short *byte_offset,unsigned short *bit_offset)
{
    unsigned short byteOffset,bitOffset;
    unsigned byte_data;
    
    byteOffset = *byte_offset;
    bitOffset  = *bit_offset;

    if((bit_len == 0) || (bit_len > 32) || (bitOffset == 0) || (bitOffset > 8))
        return;

    while(bit_len > 0)
    {
        if(bit_len > bitOffset)
        {
            byte_data = (data >> (bit_len - bitOffset));
            BitToByte(byte_data,(unsigned char)bitOffset,byte_buf,byte_offset,bit_offset);
            bit_len -= bitOffset;

            byteOffset = *byte_offset;
            bitOffset  = *bit_offset;
        }
        else
        {
            byte_data = data;
            BitToByte(byte_data,bit_len,byte_buf,byte_offset,bit_offset);
            bit_len = 0;
            
            byteOffset = *byte_offset;
            bitOffset  = *bit_offset;
        }
    }
}

unsigned long GetBitFromByte(unsigned char bit_len,unsigned char byte_buf[],unsigned short *byte_offset,unsigned short *bit_offset)
{
    unsigned short byteOffset,bitOffset;
    unsigned long  data,tempdata;

    byteOffset = *byte_offset;
    bitOffset  = *bit_offset;
    
    data = 0;

    if((bit_len > 32) || (bitOffset == 0) || (bitOffset > 8))
        return 0;
    
    while(bit_len > 0)
    {
        if(bit_len <= bitOffset)
        {
            tempdata  = (byte_buf[byteOffset] >> (bitOffset - bit_len));
            tempdata &= ((0x01 << bit_len) - 1);
            bitOffset -= bit_len;
            data += tempdata;
            bit_len = 0;
        }
        else
        {
            tempdata = byte_buf[byteOffset] & ((0x01 << bitOffset) - 1);
            tempdata <<= (bit_len - bitOffset);
            bit_len -= bitOffset;
            byteOffset++;
            bitOffset = 8;
            data += tempdata;
        }
    }

    if(bitOffset == 0)
    {
        byteOffset++;
        bitOffset = 8;
    }
    *byte_offset = byteOffset;
    *bit_offset  = bitOffset;
    return data;
}

unsigned char HexAscii2Decimal(unsigned char asciiH,unsigned char asciiL)
{
	unsigned char decimal = 0;
    unsigned char hexH,hexL;
    
	if((asciiH >= '0') && (asciiH <= '9'))
    {
		hexH = asciiH - '0';
    }        
	else if((asciiH >= 'a') && (asciiH <= 'f'))
    {
	    hexH = asciiH - 'a' + 10; 
	}
	else if((asciiH >= 'A') && (asciiH <= 'F'))
    {
	    hexH = asciiH - 'A' + 10; 
	}
    else
    {
        hexH = 0;
    }
  	
	if((asciiL >= '0') && (asciiL <= '9'))
    {
		hexL = asciiL - '0';
    }        
	else if((asciiL >= 'a') && (asciiL <= 'f'))
    {
	    hexL = asciiL - 'a' + 10; 
	}
	else if((asciiL >= 'A') && (asciiL <= 'F'))
    {
	    hexL = asciiL - 'A' + 10; 
	}
    else
    {
        hexL = 0;
    }
	
	decimal = hexH * 16 + hexL;
	return decimal;
}

////////////////////////////////////////////////////////////////////