Commit 87641a14 authored by aorlati's avatar aorlati Committed by Giuseppe Carboni
Browse files

Fix issue 242 (#250)

* fix #242: proposed a recoding that should solve the issue. Need test before merging into branch

* fix #242: done the test qith the device. Also fixed the zero reference of position sensor
parent 0071f92b
Loading
Loading
Loading
Loading
+73 −0
Original line number Diff line number Diff line
#ifndef BASE_CONVERTER_H
#define BASE_CONVERTER_H


/* **************************************************************************************************** */
/* IRA Istituto di Radioastronomia                                                                      */
/*                                                                                                      */
/* This code is under GNU General Public Licence (GPL).                                                 */
/*                                                                                                      */
/* Who                                  when            What                                              */
/* Andrea Orlati(andrea.orlati@inaf.it) 24/07/2018      Creation                                          */


#include "String.h"

#include <vector>
#include <string>
#include <iostream>

namespace IRA{
	
	/**
	* This class contains some tools method to convert the base representing integer numbers.
 	* Initially some tools will be missing, they will be implemented as soon as they are required.
	*/
	class CBaseConverter {
	public:

		/**
		 * This static method converts a decimal number to its binary representation
		 * @param number decimal number to be converted
		 */	
		template<typename T> static IRA::CString decToBin(T number) {
			std::vector<char> result;
			decToBitSequence(number,result);
    		return CBaseConverter::binary(result);
		}		

		/**
		 * This static method converts a decimal number to its Hex representation
		 * @param number decimal number to be converted
		*/	
		template<typename T> static IRA::CString decToHex(T number) {
			std::vector<char> result;
			decToBitSequence(number,result);
    		return CBaseConverter::hex(result);
		}		

	private:
	
		template<typename T> static void decToBitSequence(T number,std::vector<char>& result) {
			unsigned bytes=sizeof(T);
			unsigned digits=bytes*8;
			result.empty();
			result.resize(digits,'0');
			unsigned pos=0;
    		do {
    			if ((number & 1)==0)
    				result[pos]='0';
    			else
    				result[pos]='1';
    			pos++;
    		} while ((number>>=1) && (pos<digits));
		}
		static IRA::CString binary(const std::vector<char>& v);
		static IRA::CString hex(const std::vector<char>& v);
	};
	
};



#endif
 No newline at end of file
+42 −0
Original line number Diff line number Diff line
#include "BaseConverter.h"


using namespace IRA;

IRA::CString CBaseConverter::binary(const std::vector<char> & v)
{
	std::string out="";
	for (unsigned iter=v.size();iter>0;iter--) { 
		out+=v[iter-1];
	}
	return IRA::CString(out.c_str());
}

IRA::CString CBaseConverter::hex(const std::vector<char> & v)
{
	std::string binToHex="",tmp="0000";
	for (int j=v.size()-1;j>=0;j-=4){
		for(unsigned i=0;i<4;i++) {
        	tmp[i]=v[j-i];  
        }
        if (!tmp.compare("0000")) binToHex+="0";
        else if (!tmp.compare("0001")) binToHex+="1";
        else if (!tmp.compare("0010")) binToHex+="2";
        else if (!tmp.compare("0011")) binToHex+="3";
        else if (!tmp.compare("0100")) binToHex+="4";
        else if (!tmp.compare("0101")) binToHex+="5";
        else if (!tmp.compare("0110")) binToHex+="6";
        else if (!tmp.compare("0111")) binToHex+="7";
        else if (!tmp.compare("1000")) binToHex+="8";
        else if (!tmp.compare("1001")) binToHex+="9";
        else if (!tmp.compare("1010")) binToHex+="A";
        else if (!tmp.compare("1011")) binToHex+="B";
        else if (!tmp.compare("1100")) binToHex+="C";
        else if (!tmp.compare("1101")) binToHex+="D";
        else if (!tmp.compare("1110")) binToHex+="E";
        else if (!tmp.compare("1111")) binToHex+="F";
        else continue;
    }
    return IRA::CString(binToHex.c_str());
}
+2 −2
Original line number Diff line number Diff line
@@ -36,7 +36,7 @@ USER_CFLAGS =
INCLUDES = Definitions.h String.h IRATools.h Error.h SecureArea.h SerialPort.h Lecom.h FastQueue.h\
 Socket.h DataField.h DBTable.h LogFilter.h Site.h DateTime.h Timer.h SkySource.h TimeTaggedCircularArray.h \
 ScheduleTimer.h TimeoutSync.h MicroControllerBoard.h ReceiverControl.h MicroControllerBoardDef.h \
 SourceFlux.h CustomLoggerUtils.h LogDike.h FrequencyTracking.h IRA
 SourceFlux.h CustomLoggerUtils.h LogDike.h FrequencyTracking.h BaseConverter.h IRA

#
# Libraries (public and local)
@@ -45,7 +45,7 @@ LIBRARIES = IRALibrary
LIBRARIES_L     =
IRALibrary_OBJECTS = String IRATools Error SerialPort Lecom Socket DataField DBTable LogFilter \
 Site DateTime Timer SkySource TimeTaggedCircularArray ScheduleTimer TimeoutSync MicroControllerBoard \
 ReceiverControl SourceFlux CustomLoggerUtils LogDike FrequencyTracking
 ReceiverControl SourceFlux CustomLoggerUtils LogDike FrequencyTracking BaseConverter
IRALibrary_LIBS = acstime SlaLibrary maciClient

PY_PACKAGES  = IRAPy
+3 −5
Original line number Diff line number Diff line
@@ -161,8 +161,7 @@ CString & CString::Concat(const char *add)

CString & CString::Concat(char add)
{
	Concat(&add);
    return *this;
    return Concat(&add);
}

CString & CString::Concat(int nValue)
@@ -680,8 +679,7 @@ const CString & CString::operator +=(const CString & src)

const CString & CString::operator +=(char ch)
{
	Concat(ch);
	return *this;
	return Concat(ch);
}

const CString & CString::operator +=(const char *str)
+52 −0
Original line number Diff line number Diff line
#include "BaseConverter.h"

#include <stdlib.h>

namespace IRALibraryTest {

class IRALibrary_BaseConverter : public ::testing::Test {

public:

::testing::AssertionResult decToBin_checkConversion() {
	::testing::Test::RecordProperty("description","check if straight decimal to binary convertion works correctly");
	char onebyte=1;
	if (IRA::CBaseConverter::decToBin(onebyte)!="00000001") return ::testing::AssertionFailure() << "Convertion of 1 (byte) is not correct";
	if (IRA::CBaseConverter::decToBin(1234)!="00000000000000000000010011010010") return ::testing::AssertionFailure() 
	  << "Convertion of 1234 (int) is not correct";
	if (IRA::CBaseConverter::decToBin(-1234)!="11111111111111111111101100101110") return ::testing::AssertionFailure() 
	  << "Convertion of -1234 (int) is not correct";	
	if (IRA::CBaseConverter::decToBin(-1234l)!="1111111111111111111111111111111111111111111111111111101100101110") 
	  return ::testing::AssertionFailure() << "Convertion of -1234 (long) is not correct";
	return ::testing::AssertionSuccess();
	

}

::testing::AssertionResult decToHex_checkConversion() {
	::testing::Test::RecordProperty("description","check if straight decimal to hex convertion works correctly");
	//std::cout << IRA::CBaseConverter::decToHex(-2147483648) << endl;
	if (IRA::CBaseConverter::decToHex(0)!="00000000") return ::testing::AssertionFailure() << "Convertion of 0 is not correct";
	if (IRA::CBaseConverter::decToHex(2147483647)!="7FFFFFFF") return ::testing::AssertionFailure() << "Convertion of 2147483647 is not correct";
	if (IRA::CBaseConverter::decToHex(-1)!="FFFFFFFF") return ::testing::AssertionFailure() << "Convertion of -1 is not correct";
	if (IRA::CBaseConverter::decToHex(-2147483648l)!="FFFFFFFF80000000") return ::testing::AssertionFailure() << "Convertion of -2147483648 is not correct";	
	return ::testing::AssertionSuccess();
}

static void TearDownTestCase()
{
}

static void SetUpTestCase()
{
}

virtual void SetUp() {
}

virtual void TearDown() {
}

};

};
 No newline at end of file
Loading