<commit_before>#include <xc.h>
#include <stdint.h>

#include "../Event.h"
#include "../NearScheduler.h"

#include "Motor.h"

#define CCP1CON_MODE_MASK (0b1111 << _CCP1CON_MODE_POSITION)
#define CCP1CON_COMPARE_AND_SET_MODE (0b1000 << _CCP1CON_MODE_POSITION)

static void incrementPwmDutyCycle(void *state);

static struct MotorStarted motorStartedEventArgs;

static const struct NearSchedule pwmDutyCycleIncrementingSchedule =
{
	.ticks = 1,
	.handler = &incrementPwmDutyCycle
};

void motorOn(int16_t count)
{
	if (count == 0 || (CWG1STR & 0x0f))
		return;

	motorStartedEventArgs.count = count;
	eventPublish(MOTOR_STARTED, &motorStartedEventArgs);

	uint8_t steeringMask = _CWG1STR_STRB_MASK;
	if (count < 0)
	{
		steeringMask = _CWG1STR_STRA_MASK;
		count = -count;
		if (count == -32768)
			count = 0x7fff;
	}

	CCP1CON &= ~CCP1CON_MODE_MASK;
	PWM4DCH = 0;
	PWM4DCL = 0;
	CCPR1H = (uint8_t) ((count >> 8) & 0xff);
	CCPR1L = (uint8_t) ((count >> 0) & 0xff);
	TMR1H = 0;
	TMR1L = 0;
	PIR4bits.TMR1IF = 0;
	CWG1AS0bits.SHUTDOWN = 0;
	CWG1STR |= steeringMask;
	CCP1CON |= CCP1CON_COMPARE_AND_SET_MODE;

	nearSchedulerAdd(&pwmDutyCycleIncrementingSchedule);
}

static void incrementPwmDutyCycle(void *state)
{
	if (!(CWG1STR & CWG1STR_STEERING_MASK))
		return;

	if (PWM4DCH != (256 - 8) >> 2)
		nearSchedulerAdd(&pwmDutyCycleIncrementingSchedule);

	PWM4DCH += 8 >> 2;
}

void motorOff(void)
{
	if (CWG1STR & CWG1STR_STEERING_MASK)
	{
		uint8_t antiClockwise = CWG1STRbits.STRA;
		CWG1STR &= ~CWG1STR_STEERING_MASK;<commit_msg>Hacks from bench testing to fix motor direction and over-current issues.<commit_after>#include <xc.h>
#include <stdint.h>

#include "../Event.h"
#include "../NearScheduler.h"

#include "Motor.h"

#define CCP1CON_MODE_MASK (0b1111 << _CCP1CON_MODE_POSITION)
#define CCP1CON_COMPARE_AND_SET_MODE (0b1000 << _CCP1CON_MODE_POSITION)

static void incrementPwmDutyCycle(void *state);

static struct MotorStarted motorStartedEventArgs;

static const struct NearSchedule pwmDutyCycleIncrementingSchedule =
{
	.ticks = 10, // TODO: TESTS WILL NEED UPDATING FOR THIS...(WAS 1, NOW 10)
	.handler = &incrementPwmDutyCycle
};

void motorOn(int16_t count)
{
	if (count == 0 || (CWG1STR & 0x0f))
		return;

	motorStartedEventArgs.count = count;
	eventPublish(MOTOR_STARTED, &motorStartedEventArgs);

	uint8_t steeringMask = _CWG1STR_STRA_MASK; // TODO: A AND B HAVE BEEN SWITCHED AROUND AS THEY WERE WRONG...FIX THE TESTS...
	if (count < 0)
	{
		steeringMask = _CWG1STR_STRB_MASK;
		count = -count;
		if (count == -32768)
			count = 0x7fff;
	}

	CCP1CON &= ~CCP1CON_MODE_MASK;
	PWM4DCH = 0;
	PWM4DCL = 0;
	CCPR1H = (uint8_t) ((count >> 8) & 0xff);
	CCPR1L = (uint8_t) ((count >> 0) & 0xff);
	TMR1H = 0;
	TMR1L = 0;
	PIR4bits.TMR1IF = 0;
PIR2bits.C1IF = 0; // TODO: SHOULD REALLY CLEAR THIS HERE - COMPARATOR CAN BE TWITCHY...
	CWG1AS0bits.SHUTDOWN = 0;
	CWG1STR |= steeringMask;
	CCP1CON |= CCP1CON_COMPARE_AND_SET_MODE;

	nearSchedulerAdd(&pwmDutyCycleIncrementingSchedule);
}

static void incrementPwmDutyCycle(void *state)
{
	if (!(CWG1STR & CWG1STR_STEERING_MASK))
		return;

	if (PWM4DCH != (256 - 8) >> 2)
		nearSchedulerAdd(&pwmDutyCycleIncrementingSchedule);

	PWM4DCH += 8 >> 2;
}

void motorOff(void)
{
	if (CWG1STR & CWG1STR_STEERING_MASK)
	{
		uint8_t antiClockwise = CWG1STRbits.STRB; // TODO: A AND B HAVE BEEN SWITCHED AROUND AS THEY WERE WRONG...FIX THE TESTS...
		CWG1STR &= ~CWG1STR_STEERING_MASK;
{'fraction_non_alphanumeric': 0.12164527421236873, 'fraction_numerical': 0.04638273045507585, 'mean_word_length': 4.802030456852792, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 23, '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': '7477518', 'n_tokens_mistral': 1624, 'n_tokens_neox': 1456, 'n_words': 297}