<commit_before>// coding: utf-8

#ifndef TASK_MECHANICS_HPP
#	error	"Don't include this file directly, use 'task_mechanics.hpp' instead!"
#endif

task::Mechanics::Mechanics(Leds &leds, Buttons &buttons)
:	leds(leds), buttons(buttons),
	isCalibratedX(false), isCalibratedZ(false),
	correctionX(-40), correctionZ(15),
	xMotor(&OCR1AL, &TCCR1B, t1_steps), zMotor(&OCR0A, &TCCR0B, t0_steps),
	motorTimeout(30000)
{
}

void
task::Mechanics::initialize()
{
	// setup CTC mode for Z axis
	TCCR0A = (1 << COM0A0) | (1 << WGM01);
	TCCR0B = 0;
	OCR0A = 0;
	TIMSK0 = (1 << OCIE0A);

	// setup CTC mode for X axis
	TCCR1A = (1 << COM1A0);
	TCCR1B = (1 << WGM12);
	OCR1A = 0;
	TIMSK1 = (1 << OCIE1A);

	XZ_Enable::setOutput(xpcc::Gpio::Low);

	releaseMotors();
	xMotor.initialize();
	zMotor.initialize();
}

xpcc::co::Result<bool>
task::Mechanics::calibrateX(void *ctx)
{
	CO_BEGIN(ctx);
	startMotors();

	if (buttons.isX_AxisLimitPressed())
	{
		xMotor.rotateBy(100, 500);
		CO_WAIT_WHILE(xMotor.isRunning());
	}

	xMotor.setSpeed(-100);
	timeout.restart(18000);
	CO_WAIT_UNTIL(buttons.isX_AxisLimitPressed() || timeout.isExpired());
	xMotor.stop();

	if ( (isCalibratedX = !timeout.isExpired()) )
	{
		xMotor.rotateBy(correctionX, 200);
		CO_WAIT_WHILE(xMotor.isRunning());
		leds.resetMechanicalError();
		leds.resetBusy();
		CO_RETURN(true);
	}

	leds.setMechanicalError();
	leds.resetBusy();
	CO_RETURN(false);

	CO_END();
}

xpcc::co::Result<bool>
task::Mechanics::calibrateZ(void *ctx)
{
	CO_BEGIN(ctx);
	startMotors();

	zMotor.stop();
	if (buttons.isZ_AxisLimitPressed())
	{
		zMotor.rotateBy(100, 500);
		CO_WAIT_WHILE(zMotor.isRunning());
		zMotor.stop();
	}

	zMotor.setSpeed(-100);
	timeout.restart(18000);
	CO_WAIT_UNTIL(buttons.isZ_AxisLimitPressed() || timeout.isExpired());

	zMotor.stop();
	if ( (isCalibratedZ = !timeout.isExpired()) )
	{
		zMotor.rotateBy(correctionZ, 300);
		CO_WAIT_WHILE(zMotor.isRunning());
		leds.resetMechanicalError();
		leds.resetBusy();
		CO_RETURN(true);
	}

	leds.setMechanicalError();
	leds.resetBusy();
	CO_RETURN(false);
<commit_msg>Make mechanics a little faster.<commit_after>// coding: utf-8

#ifndef TASK_MECHANICS_HPP
#	error	"Don't include this file directly, use 'task_mechanics.hpp' instead!"
#endif

task::Mechanics::Mechanics(Leds &leds, Buttons &buttons)
:	leds(leds), buttons(buttons),
	isCalibratedX(false), isCalibratedZ(false),
	correctionX(-40), correctionZ(15),
	xMotor(&OCR1AL, &TCCR1B, t1_steps), zMotor(&OCR0A, &TCCR0B, t0_steps),
	motorTimeout(30000)
{
}

void
task::Mechanics::initialize()
{
	// setup CTC mode for Z axis
	TCCR0A = (1 << COM0A0) | (1 << WGM01);
	TCCR0B = 0;
	OCR0A = 0;
	TIMSK0 = (1 << OCIE0A);

	// setup CTC mode for X axis
	TCCR1A = (1 << COM1A0);
	TCCR1B = (1 << WGM12);
	OCR1A = 0;
	TIMSK1 = (1 << OCIE1A);

	XZ_Enable::setOutput(xpcc::Gpio::Low);

	releaseMotors();
	xMotor.initialize();
	zMotor.initialize();
}

xpcc::co::Result<bool>
task::Mechanics::calibrateX(void *ctx)
{
	CO_BEGIN(ctx);
	startMotors();

	if (buttons.isX_AxisLimitPressed())
	{
		xMotor.rotateBy(100, 500);
		CO_WAIT_WHILE(xMotor.isRunning());
	}

	xMotor.setSpeed(-200);
	timeout.restart(18000);
	CO_WAIT_UNTIL(buttons.isX_AxisLimitPressed() || timeout.isExpired());
	xMotor.stop();

	if ( (isCalibratedX = !timeout.isExpired()) )
	{
		xMotor.rotateBy(correctionX, 200);
		CO_WAIT_WHILE(xMotor.isRunning());
		leds.resetMechanicalError();
		leds.resetBusy();
		CO_RETURN(true);
	}

	leds.setMechanicalError();
	leds.resetBusy();
	CO_RETURN(false);

	CO_END();
}

xpcc::co::Result<bool>
task::Mechanics::calibrateZ(void *ctx)
{
	CO_BEGIN(ctx);
	startMotors();

	zMotor.stop();
	if (buttons.isZ_AxisLimitPressed())
	{
		zMotor.rotateBy(100, 500);
		CO_WAIT_WHILE(zMotor.isRunning());
		zMotor.stop();
	}

	zMotor.setSpeed(-200);
	timeout.restart(18000);
	CO_WAIT_UNTIL(buttons.isZ_AxisLimitPressed() || timeout.isExpired());

	zMotor.stop();
	if ( (isCalibratedZ = !timeout.isExpired()) )
	{
		zMotor.rotateBy(correctionZ, 300);
		CO_WAIT_WHILE(zMotor.isRunning());
		leds.resetMechanicalError();
		leds.resetBusy();
		CO_RETURN(true);
	}

	leds.setMechanicalError();
	leds.resetBusy();
	CO_RETURN(false);

{'fraction_non_alphanumeric': 0.17427884615384615, 'fraction_numerical': 0.03701923076923077, 'mean_word_length': 5.563091482649842, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 7, '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': '6098202', 'n_tokens_mistral': 2005, 'n_tokens_neox': 1798, 'n_words': 264}