<commit_before>
#include "DynamixelInterfaceArduinoImpl.h"

#if __AVR__
// define TXEN, RXEN and RXCIE
#if !defined(TXEN)
#if defined(TXEN0)
#define TXEN TXEN0
#define RXEN RXEN0
#define RXCIE RXCIE0
#elif defined(TXEN1) // Some devices have uart1 but no uart0 (leonardo)
#define TXEN TXEN1
#define RXEN RXEN1
#define RXCIE RXCIE1
#endif
#endif
#endif //__AVR__

template<class T>
void DynamixelInterfaceImpl<T>::readMode()
{
	if(mDirectionPin!=NO_DIR_PORT)
	{
		digitalWrite(mDirectionPin, LOW);
	}
	else
	{
		setReadMode(mStream);
	}
}
	
template<class T>
void DynamixelInterfaceImpl<T>::writeMode()
{
	if(mDirectionPin!=NO_DIR_PORT)
	{
		digitalWrite(mDirectionPin, HIGH);
	}
	else
	{
		setWriteMode(mStream);
	}
}
	
template<class T>
DynamixelInterfaceImpl<T>::DynamixelInterfaceImpl(T &aStream, uint8_t aDirectionPin=NO_DIR_PORT):
	mStream(aStream), mDirectionPin(aDirectionPin)
{
	if(mDirectionPin!=NO_DIR_PORT)
	{
		digitalWrite(mDirectionPin, LOW);
		pinMode(mDirectionPin, OUTPUT);
	}
}

template<class T>
DynamixelInterfaceImpl<T>::~DynamixelInterfaceImpl()
{
}

template<class T>
void DynamixelInterfaceImpl<T>::begin(unsigned long aBaud)
{
	mStream.begin(aBaud);
	mStream.setTimeout(50); //warning : response delay seems much higher than expected for some operation (eg writing eeprom)
	readMode();
}

template<class T>
void DynamixelInterfaceImpl<T>::sendPacket(const DynamixelPacket &aPacket)
{
	writeMode();

	mStream.write(0xFF);
	mStream.write(0xFF);
	mStream.write(aPacket.mID);
	mStream.write(aPacket.mLength);
	mStream.write(aPacket.mInstruction);
	uint8_t n=0;
	if(aPacket.mAddress!=255)
	{
		mStream.write(aPacket.mAddress);
		++n;
	}
	if(aPacket.mDataLength!=255)
	{
		mStream.write(aPacket.mDataLength);
		++n;
	}
	if(aPacket.mLength>(2+n))
	{
		if(aPacket.mIDListSize==0)
		{
			mStream.write(aPacket.mData, aPacket.mLength-2-n);
		}
		else
		{
			uint8_t *ptr=aPacket.mData;
			for(uint8_t i=0; i<aPacket.mIDListSize; ++i)
			{
				mStream.write(aPacket.mIDList[i]);
				mStream.write(ptr, aPacket.mDataLength);
				ptr+=aPacket.mDataLength;
			}
		}
	}
	mStream.write(aPacket.mCheckSum);
	mStream.flush();
	mStream.clear();
	readMode();
}

template<class T>
void DynamixelInterfaceImpl<T>::receivePacket(DynamixelPacket &aPacket)
{
	static uint8_t buffer[3];
	aPacket.mIDListSize = 0;
	aPacket.mAddress = 255;
	aPacket.mDataLength = 255;
	if (mStream.readBytes(buffer, 2)<2)
	{
		aPacket.mStatus = DYN_STATUS_COM_ERROR | DYN_STATUS_TIMEOUT;
		return;
	}
	if (buffer[0] != 255 || buffer[1] != 255)
	{
		aPacket.mStatus = DYN_STATUS_COM_ERROR;
		return;
	}
	if (mStream.readBytes(buffer, 3)<3)
	{
		aPacket.mStatus = DYN_STATUS_COM_ERROR | DYN_STATUS_TIMEOUT;<commit_msg>removing the 'clear' in sendPacket since it is not supported by the AVR library<commit_after>
#include "DynamixelInterfaceArduinoImpl.h"

#if __AVR__
// define TXEN, RXEN and RXCIE
#if !defined(TXEN)
#if defined(TXEN0)
#define TXEN TXEN0
#define RXEN RXEN0
#define RXCIE RXCIE0
#elif defined(TXEN1) // Some devices have uart1 but no uart0 (leonardo)
#define TXEN TXEN1
#define RXEN RXEN1
#define RXCIE RXCIE1
#endif
#endif
#endif //__AVR__

template<class T>
void DynamixelInterfaceImpl<T>::readMode()
{
	if(mDirectionPin!=NO_DIR_PORT)
	{
		digitalWrite(mDirectionPin, LOW);
	}
	else
	{
		setReadMode(mStream);
	}
}
	
template<class T>
void DynamixelInterfaceImpl<T>::writeMode()
{
	if(mDirectionPin!=NO_DIR_PORT)
	{
		digitalWrite(mDirectionPin, HIGH);
	}
	else
	{
		setWriteMode(mStream);
	}
}
	
template<class T>
DynamixelInterfaceImpl<T>::DynamixelInterfaceImpl(T &aStream, uint8_t aDirectionPin=NO_DIR_PORT):
	mStream(aStream), mDirectionPin(aDirectionPin)
{
	if(mDirectionPin!=NO_DIR_PORT)
	{
		digitalWrite(mDirectionPin, LOW);
		pinMode(mDirectionPin, OUTPUT);
	}
}

template<class T>
DynamixelInterfaceImpl<T>::~DynamixelInterfaceImpl()
{
}

template<class T>
void DynamixelInterfaceImpl<T>::begin(unsigned long aBaud)
{
	mStream.begin(aBaud);
	mStream.setTimeout(50); //warning : response delay seems much higher than expected for some operation (eg writing eeprom)
	readMode();
}

template<class T>
void DynamixelInterfaceImpl<T>::sendPacket(const DynamixelPacket &aPacket)
{
	writeMode();

	mStream.write(0xFF);
	mStream.write(0xFF);
	mStream.write(aPacket.mID);
	mStream.write(aPacket.mLength);
	mStream.write(aPacket.mInstruction);
	uint8_t n=0;
	if(aPacket.mAddress!=255)
	{
		mStream.write(aPacket.mAddress);
		++n;
	}
	if(aPacket.mDataLength!=255)
	{
		mStream.write(aPacket.mDataLength);
		++n;
	}
	if(aPacket.mLength>(2+n))
	{
		if(aPacket.mIDListSize==0)
		{
			mStream.write(aPacket.mData, aPacket.mLength-2-n);
		}
		else
		{
			uint8_t *ptr=aPacket.mData;
			for(uint8_t i=0; i<aPacket.mIDListSize; ++i)
			{
				mStream.write(aPacket.mIDList[i]);
				mStream.write(ptr, aPacket.mDataLength);
				ptr+=aPacket.mDataLength;
			}
		}
	}
	mStream.write(aPacket.mCheckSum);
	mStream.flush();
	readMode();
}

template<class T>
void DynamixelInterfaceImpl<T>::receivePacket(DynamixelPacket &aPacket)
{
	static uint8_t buffer[3];
	aPacket.mIDListSize = 0;
	aPacket.mAddress = 255;
	aPacket.mDataLength = 255;
	if (mStream.readBytes(buffer, 2)<2)
	{
		aPacket.mStatus = DYN_STATUS_COM_ERROR | DYN_STATUS_TIMEOUT;
		return;
	}
	if (buffer[0] != 255 || buffer[1] != 255)
	{
		aPacket.mStatus = DYN_STATUS_COM_ERROR;
		return;
	}
	if (mStream.readBytes(buffer, 3)<3)
	{
		aPacket.mStatus = DYN_STATUS_COM_ERROR | DYN_STATUS_TIMEOUT;
{'fraction_non_alphanumeric': 0.13419117647058823, 'fraction_numerical': 0.01838235294117647, 'mean_word_length': 5.516167664670658, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1441914', 'n_tokens_mistral': 2360, 'n_tokens_neox': 2192, 'n_words': 365}