Skip to content
OE_controller.cpp 13.5 KiB
Newer Older
/*
 * Copyright (c) 2015 Tricoire Sebastien 3dsman@free.fr
 *
 * This software is provided 'as-is', without any express or implied
 * warranty.  In no event will the authors be held liable for any damages
 * arising from the use of this software.
 *
 * Permission is granted to anyone to use this software for any purpose,
 * including commercial applications, and to alter it and redistribute it
 * freely, subject to the following restrictions:
 *
 * 1. The origin of this software must not be misrepresented; you must not
 * claim that you wrote the original software. If you use this software
 * in a product, an acknowledgment in the product documentation would be
 * appreciated but is not required.
 * 2. Altered source versions must be plainly marked as such, and must not be
 * misrepresented as being the original software.
 * 3. This notice may not be removed or altered from any source distribution.
 *
 */

#include "OE_controller.h"
#include <iostream>
#include <typeinfo>


3dsman's avatar
3dsman committed
OE_controller::OE_controller(OE_display * display, OE_document* document ) : comm("/dev/ttyUSB0", 80, 10)
{
	this->display = display;
	this->document = document;
	OE_linestitch::initMotifs();
}

OE_controller::~OE_controller()
{
}
        
bool OE_controller::test()
{
	// test motifs
	if (document)
	{
		document->setZeroPoint(vector_2d(0,1050));
		//test joincurve
		selectCurve(getCurve(0), false);
		OE_joincurve* curve = new OE_joincurve(getCurve(8),getCurve(13));
		addCurve(curve);
		selectCurve(curve, true);
		addCurve(new OE_subcurve(getCurve(2),0.1, 0.8, false));
		selectCurve(getCurve(document->curves.size()-1), true);
		addLineStitch(getCurve(document->curves.size()-1),1,2,4,-0.2);
		curStitch = document->stitchs.size()-1;
		setStitchThread(curStitch, getThread(0));
		// interrieur premier rectange
		addCurve(new OE_subcurve(getCurve(2),0.1, 0.8, true));
		addCurve(new OE_subcurve(getCurve(2),2, 3, false));
		addBirailStitch(getCurve(document->curves.size()-1), getCurve(document->curves.size()-2), false, false, -0.2, -0.2, 1.0);
		curStitch = document->stitchs.size()-1;
		
		
		addThread(120,220,20,2);
		addLineStitch();
		curStitch = document->stitchs.size()-1;
		setLineStitchCurve(curStitch, getCurve(5));
		setLineStitchSize(curStitch,1,0.5);
		setStitchThread(curStitch, getThread(1));
		addCurve(new OE_subcurve(getCurve(1),0, 2, true));
		addCurve(new OE_subcurve(getCurve(1),2, 4, true));
		addCurve(new OE_joincurve(getCurve(document->curves.size()-2),getCurve(document->curves.size()-1)));
		selectCurve(getCurve(document->curves.size()-1), true);
		
		addBirailStitch(getCurve(0), getCurve(document->curves.size()-1),false,false, 0, 0, 1);
		curStitch = document->stitchs.size()-1;
		//addLineStitch(document->curves.at(15),1.5,3.5,1,0,0,0.1);
		/*
		setLineStitchCurve(0, getCurve(0));
		setLineStitchMotif(0, 1);
		setLineStitchSize(0,2,2);
		setStitchThread(0, getCurve(0));*/
		addLineStitch(getCurve(13),1,0.5,1,0.1);
		curStitch = document->stitchs.size()-1;
		setStitchThread(curStitch, getThread(0));
	}
	// fin motifs
	return false;
}

bool OE_controller::setDefault()
{
	OE_birailstitch::defaultLen = 1;
	OE_birailstitch::defaultMaxlen = 0;
OE_curve* OE_controller::getCurve (unsigned index)
{
	if ((document)&&(index<document->curves.size()))
	{
		std::list<OE_curve*>::iterator curve = document->curves.begin();
		for(unsigned i=0; i<index;i++)
			curve++;
		return (*curve);
	}

	return nullptr;
}

OE_pointcurve* OE_controller::getPointcurve (unsigned index)
{
	//if ((document)&&(index<document->curves.size()))
		return (dynamic_cast<OE_pointcurve*> (getCurve (index)));
}

OE_subcurve* OE_controller::getSubcurve (unsigned index)
{
	//if ((document)&&(index<document->curves.size()))
		return (dynamic_cast<OE_subcurve*> (getCurve (index)));
}

OE_joincurve* OE_controller::getJoincurve (unsigned index)
{
	//if ((document)&&(index<document->curves.size()))
		return (dynamic_cast<OE_joincurve*> (getCurve (index)));
OE_stitchs* OE_controller::getStitch (unsigned index)
{
	if ((document)&&(index<document->stitchs.size()))
	{
		std::list<OE_stitchs*>::iterator stitch = document->stitchs.begin();
		for(unsigned i=0; i<index;i++)
			stitch++;
		return (*stitch);
	}
		//return document->stitchs.at(index);
OE_linestitch* OE_controller::getLineStitch (unsigned index)
	//if ((document)&&(index<document->stitchs.size()))
		return (dynamic_cast<OE_linestitch*> (getStitch(index)));
}

OE_birailstitch* OE_controller::getBirailStitch (unsigned index)
{
	//if ((document)&&(index<document->stitchs.size()))
		return (dynamic_cast<OE_birailstitch*> (getStitch(index)));
OE_linkstitch* OE_controller::getLinkStitch (unsigned index)
{
	//if ((document)&&(index<document->stitchs.size()))
		return (dynamic_cast<OE_linkstitch*> (getStitch(index)));
OE_thread* OE_controller::getThread (unsigned index)
{
	if ((document)&&(index<document->threads.size()))
	{
		std::list<OE_thread*>::iterator thread = document->threads.begin();
		for(unsigned i=0; i<index;i++)
			thread++;
		return (*thread);
	}
		//return document->threads.at(index);

bool OE_controller::addStitch(OE_stitchs * stitch)
{
	if (document)
	{
		if (document->stitchs.size() > 0)
		{
			OE_stitchs* tmpstitch = document->stitchs.back();
			document->addStitch( new OE_linkstitch(tmpstitch, stitch));
			
		}
		
bool OE_controller::addCurve( std::vector<vector_2d> points, bool closed)
{
	if (document)
	{
		return document->addCurve( new OE_pointcurve(points, closed));
	}
	return false;
}

bool OE_controller::addCurve( OE_curve* curve)
{
	if (document)
	{
		return document->addCurve( curve);
	}
	return false;
}

bool OE_controller::addLineStitch()
{
	if (document)
bool OE_controller::addLineStitch( OE_curve* curve, float len, float width, unsigned motif, float offset)
{
	if (document)
		return addStitch(new OE_linestitch(curve, len, width, motif, offset));
bool OE_controller::addBirailStitch(OE_curve* curve1, OE_curve* curve2, bool reverse1, bool reverse2, float offset1, float offset2, float len)
		return addStitch(new OE_birailstitch(curve1, curve2, reverse1, reverse2, offset1, offset2, len));
bool OE_controller::addThread()
{
	if (document)
		return document->addThread(new OE_thread());

	return false;
}

bool OE_controller::addThread(unsigned char r, unsigned char g, unsigned char b, float width )
{
	if (document)
		return document->addThread(new OE_thread(r, g, b, width));

	return false;
}

bool OE_controller::selectCurve( OE_curve* curve, bool add)
{
	if (document)
	{
		if (!add) document->selectedCurves.clear();
		document->selectedCurves.push_back(curve);
		return true;
	}
	return false;
}

bool OE_controller::selectStitch( OE_stitchs* stitch, bool add)
{
	if (document)
	{
		if (!add) document->selectedStitchs.clear();
		document->selectedStitchs.push_back(stitch);
		return true;
	}
	return false;
}

bool OE_controller::refreshStitchs()
{
	if (document)
	{
		std::list<OE_stitchs*>::iterator stitch = document->stitchs.begin();
		
		while (stitch != document->stitchs.end())
		{
		//for(unsigned i = 0; i < document->stitchs.size();i++)
			(*stitch)->refresh();
			stitch++;
		}
bool OE_controller::setStitchMaxLen( unsigned index, float maxLen)
{
	if ((document)&&(index<document->stitchs.size()))
	{
		getStitch(index)->setMaxLen(maxLen);
bool OE_controller::setLineStitchMotif( unsigned index, unsigned motif)
{
	OE_linestitch * tmpstitch = getLineStitch(index);
	if (!tmpstitch)return false;
	
	tmpstitch->setMotif(motif);
	return true;
}

bool OE_controller::setLineStitchCurve( unsigned index, OE_curve* curve)
{
	OE_linestitch * tmpstitch = getLineStitch(index);
	if (!tmpstitch)return false;
	
	tmpstitch->setCurve(curve);
	return true;
}

bool OE_controller::setLineStitchSize( unsigned index, float len, float width)
{
	OE_linestitch * tmpstitch = getLineStitch(index);
	if (!tmpstitch)return false;
	
	tmpstitch->setLen(len);
	tmpstitch->setWidth(width);
	return true;
}
bool OE_controller::setLineStitchSizeReverse(unsigned index, bool reverse)
{
	OE_linestitch * tmpstitch = getLineStitch(index);
	if (!tmpstitch)return false;
	
	tmpstitch->setReverse(reverse);
	return true;
}

bool OE_controller::setBirailStitchCurves( unsigned index, OE_curve* curve1, OE_curve* curve2)
{
	OE_birailstitch * tmpstitch = getBirailStitch(index);
	if (!tmpstitch)return false;
	
	tmpstitch->setCurve1(curve1);
	tmpstitch->setCurve2(curve2);
	return true;
}
bool OE_controller::setBirailStitchSub( unsigned index, float start1, float end1, bool reverse1, float start2, float end2, bool reverse2)
{
	OE_birailstitch * tmpstitch = getBirailStitch(index);
	if (!tmpstitch)return false;
	
	tmpstitch->setReverse1(reverse1);
	tmpstitch->setReverse1(reverse2);
	return true;
}
bool OE_controller::setBirailStitchSize( unsigned index, float len, float offset1, float offset2)
{
	OE_birailstitch * tmpstitch = getBirailStitch(index);
	if (!tmpstitch)return false;
	
	tmpstitch->setLen(len);
	tmpstitch->setOffset1(offset1);
	tmpstitch->setOffset2(offset2);
	return true;
}
bool OE_controller::setLinkStitchStitch( unsigned index, OE_stitchs* stitchStart, OE_stitchs* stitchEnd)
{
	OE_linkstitch * tmpstitch = getLinkStitch(index);
	if (!tmpstitch)return false;
	
	tmpstitch->setStitchStart(stitchStart);
	tmpstitch->setStitchStart(stitchEnd);
	return true;
}

bool OE_controller::setStitchThread(unsigned index, OE_thread * thread)
{
	OE_stitchs * tmpstitch = getStitch(index);
	if (!tmpstitch)return false;
	
	tmpstitch->setThread(thread);

	return true;
}

bool OE_controller::setThreadColor(unsigned index, unsigned char r, unsigned char g, unsigned char b )
{
	OE_thread * tmpthread = getThread(index);
	if (!tmpthread)return false;
	
	tmpthread->setColor(r, g, b);

	return true;
}

bool OE_controller::setThreadWidth(unsigned index, float width)
{
	OE_thread * tmpthread = getThread(index);
	if (!tmpthread)return false;
	
	tmpthread->setWidth(width);

	return true;
}

bool OE_controller::setsubcurvePos( unsigned index, float start, float end)
{
	OE_subcurve * tmpcurve = getSubcurve(index);
	if (!tmpcurve)return false;
	
	tmpcurve->setStart(start);
	tmpcurve->setEnd(end);
	return true;
}

bool OE_controller::generateInstructions()
{
		
	if (document)
	{
		document->instPoints.clear();
		for (unsigned i = 0; i < document->instCommand.size(); i++) {
			delete (document->instCommand.at(i));
		}
		document->instCommand.clear();
		
		std::list<OE_stitchs*>::iterator stitch = document->stitchs.begin();
		
		OE_thread * curThread = (*stitch)->getThread();
		
		while (stitch != document->stitchs.end())
		{
			(*stitch)->refresh();
			if((*stitch)->check()&&(*stitch)->getNpts()>0)
			{
				OE_thread * newThread = (*stitch)->getThread();
				if (!newThread)
				{
					newThread = document->threads.front();
				}
				
				if ((newThread!=curThread))
				{
					curThread = newThread;
					document->instCommand.push_back(new OE_waitcolor(document->instPoints.size(), curThread->getColor()));
				}								
				std::vector<vector_2d> points = (*stitch)->getPoints();
			
				document->instPoints.insert( document->instPoints.end(), points.begin(), points.end() );

			}
			stitch++;
		}
		
		vector_2d zero = document->getZeroPoint();
		float size = document->getPulseByMm();
		for ( unsigned i = 0; i < document->instPoints.size(); i++)
		{
			document->instPoints.at(i) = (document->instPoints.at(i) - zero)*size;
		}
	}
	return true;
}

3dsman's avatar
3dsman committed
uint8_t int7(int i) // transform int32 to int7
{
	if (i>=0)
		return i;
	int8_t i8 = 2*i;
	return (*(uint8_t*)&i8)>>1;
}

void OE_controller::sendInstPoint()
{
3dsman's avatar
3dsman committed
		std::cout << "sendInstPoint x " << document->instPoints.size() <<std::endl;
		comm.stopMove();
		if (!comm.ack()) std::cerr << "Can't set pos" << std::endl;
		comm.setPos(0, 0);
		if (!comm.ack()) std::cerr << "Can't set pos" << std::endl;
		comm.setNPoint(0);
		if (!comm.ack()) std::cerr << "Can't set point" << std::endl;
		comm.deletePoints();
		if (!comm.ack()) std::cerr << "Can't delete points" << std::endl;
		uint8_t dxys[32];
		uint8_t cmdPoints = 0;
		int x, y, dx, dy, bigdx, bigdy, oldX, oldY;
		oldX = oldY = 0;
		for ( unsigned i = 0; i < document->instPoints.size(); i++)
		{
			x = document->instPoints.at(i).x+0.5;
			y = document->instPoints.at(i).y+0.5;
			dx = x-oldX;
			dy = y-oldY;
			if ((dx<-64)||(63<dx)||(dy<-64)||(63<dy))
			{
				bigdx = (x+32)/64;
				bigdy = (y+32)/64;
				dx -= 64*bigdx;
				dy -= 64*bigdy;
				if (cmdPoints==15)
				{
					comm.addPoints(dxys, 15);
					std::cout << "Jump" << std::endl;
					if (!comm.ack()) std::cerr << "Can't add points" << std::endl;
					cmdPoints = 0;
				}
				dxys[2*cmdPoints] = 0x80|int7(bigdx);
				dxys[2*cmdPoints+1] = int7(bigdy);
				cmdPoints++;
			}
			dxys[2*cmdPoints] = int7(dx);
			dxys[2*cmdPoints+1] = int7(dy);
			cmdPoints++;
			oldX = x;
			oldY = y;
			if (cmdPoints==16)
			{
				comm.addPoints(dxys, 16);
				if (!comm.ack()) std::cerr << "Can't add points" << std::endl;
				cmdPoints = 0;
			}
		}
		if (cmdPoints)
		{
			comm.addPoints(dxys, cmdPoints);
			if (!comm.ack()) std::cerr << "Can't add points" << std::endl;
		}
		comm.startMove();