<commit_before>/* Standard includes. */
#include <stdio.h>

/* Kernel includes. */
#include "FreeRTOS.h"
#include "task.h"
#include "timers.h"

/* Atmel library includes. */
#include "asf.h"

#include "SEGGER_RTT.h"
#include "eep.h"
#include "sm.h"
#include "cam.h"
#include "ble.h"
#include "mode_sms.h"
#include "utils.h"

#define mode_smsTEST_NONE               0
#define mode_smsTEST_CAMERA             1
#define mode_smsTEST_EXPOSE_NOW	        2

#define mode_smsCONTROL_TIMER_RATE      (10 / portTICK_RATE_MS)

#define mode_smsCOUNT_SUM_TOO_HIGH()    ((eep_params.mode_sms_leadin_count[motor] + eep_params.mode_sms_accel_count[motor] + eep_params.mode_sms_decel_count[motor] + eep_params.mode_sms_leadout_count[motor]) > (eep_params.mode_sms_count - 1))

static uint8_t state = MODE_SMS_STATE_STOP;
static bool finished = false;
static uint8_t test_mode = mode_smsTEST_NONE;
static uint32_t current_step;
static uint32_t current_loop;
static int32_t accel_steps_x1000[SM_MOTORS_USED];
static int32_t decel_steps_x1000[SM_MOTORS_USED];
static int32_t run_steps[SM_MOTORS_USED];
static uint32_t step_timer;
static uint32_t interval_timer;
static uint32_t remaining_step_time;
static uint32_t remaining_time;
static uint32_t overall_time;
static uint32_t elapsed_time;
static xTimerHandle xModeSmsControlTimer;

static void prvModeSmsControlCallback(void *pvParameters);

void vModeSmsInit(void)
{
#if !defined(DEBUG)
	for (uint8_t f = 0; f < MODE_SMS_MAX_KEY_FRAMES; f++)
	{
		for (uint8_t motor = 0; motor < SM_MOTORS_USED; motor++)
		{
			eep_params.mode_sms_positions[f].pos[motor] = 0;
		}
	}
#endif

	xModeSmsControlTimer = xTimerCreate(
		(const char * const) "ModeSmsCtrlTmr",
		mode_smsCONTROL_TIMER_RATE,
		pdTRUE,
		NULL,
		prvModeSmsControlCallback);
	configASSERT(xModeSmsControlTimer);
}

void vModeSmsUpdateIntervalToMinimum(void)
{
    eep_params.mode_sms_interval = utilsMAX(eep_params.mode_sms_interval, ulModeSmsGetMinimumInterval(eep_params.mode_sms_pre_time, eep_params.mode_sms_focus_time, eep_params.mode_sms_exposure_time, eep_params.mode_sms_post_time));
    vEepSave();
}

uint32_t ulModeSmsGetMinimumInterval(uint32_t pre_time, uint32_t focus_time, uint32_t exposure_time, uint32_t post_time)
{
    return pre_time + focus_time + exposure_time + post_time + 500;
}

void vModeSmsPause(void)
{
	if (state == MODE_SMS_STATE_STOP) return;
	if (bModeSmsIsPaused()) return;
	
	xTimerStop(xModeSmsControlTimer, 0);
}

void vModeSmsResume(void)
{
	if (state == MODE_SMS_STATE_STOP) return;
	if (!bModeSmsIsPaused()) return;
	
	xTimerStart(xModeSmsControlTimer, 0);
}

void vModeSmsStartExposeNow(void)
{
	if (state != MODE_SMS_STATE_STOP) return;

	taskENTER_CRITICAL();
	{
		test_mode = mode_smsTEST_EXPOSE_NOW;
		
		current_step = 0;
		current_loop = 0;
		step_timer = 0;
		interval_timer = 0;
		
		state = MODE_SMS_STATE_WAIT_PRE_TIME;
		xTimerStart(xModeSmsControlTimer, 0);
	}
	taskEXIT_CRITICAL();
}

void vModeSmsStartCameraTest(void)
{
	if (state != MODE_SMS_STATE_STOP) return;
	
	taskENTER_CRITICAL();
	{
		test_mode = mode_smsTEST_CAMERA;
		
		current_step = 0;
		current_loop = 0;
		step_timer = 0;
		interval_timer = 0;
		
		state = MODE_SMS_STATE_WAIT_PRE_TIME;
		xTimerStart(xModeSmsControlTimer, 0);
	}
	taskEXIT_CRITICAL();
}

void vModeSmsStart(void)
{
	if (state != MODE_SMS_STATE_STOP) return;
	
	taskENTER_CRITICAL();
	{
		for (uint8_t motor = 0; motor < SM_MOTORS_USED; motor++)
		{
			if (eep_params.mode_sms_accel_count[motor] == 0)
			{
				eep_params.mode_sms_accel_count[motor] = 1;
			}
			if (eep_params.mode_sms_decel_count[motor] == 0)
			{
				eep_params.mode_sms_decel_count[motor] = 1;
			}
			
			while (true)
			{
				if (mode_smsCOUNT_SUM_TOO_HIGH() && eep_params.mode_sms_leadin_count[motor] > 0)
				{
					eep_params.mode_sms_leadin_count[motor]--;
				}
				if (mode_smsCOUNT_SUM_TOO_HIGH() && eep_params.mode_sms_leadout_count[motor] > 0)
				{
    				eep_params.mode_sms_leadout_count[motor]--;
				}
				if (mode_smsCOUNT_SUM_TOO_HIGH() && eep_params.mode_sms_accel_count[motor] > 1)
				{
    				eep_params.mode_sms_accel_count[motor]--;
				}
				if (mode_smsCOUNT_SUM_TOO_HIGH() && eep_params.mode_sms_decel_count[motor] > 1)
				{
    				eep_params.mode_sms_decel_count[motor]--;
				}
				if (!mode_smsCOUNT_SUM_TOO_HIGH())
				{
    				break;
				}
			}

			int32_t steps = eep_params.mode_sms_positions[1].pos[motor] - eep_params.mode_sms_positions[0].pos[motor];

			int32_t img_count = (int32_t)eep_params.mode_sms_count;
			int32_t step_count = (int32_t)eep_params.mode_sms_count - 1;
			int32_t ramp_count = (int32_t)eep_params.mode_sms_accel_count[motor] + (int32_t)eep_params.mode_sms_decel_count[motor];
			int32_t lead_count = (int32_t)eep_params.mode_sms_leadin_count[motor] + (int32_t)eep_params.mode_sms_leadout_count[motor];
			int32_t move_count = step_count - lead_count;
			int32_t run_count = step_count - lead_count - ramp_count;

			if (move_count > 0 && steps != 0)
			{
                run_steps[motor] = 2 * steps / (2 * run_count + ramp_count);
                accel_steps_x1000[motor] = run_steps[motor] * 1000 / (int32_t)eep_params.mode_sms_accel_count[motor];
                decel_steps_x1000[motor] = run_steps[motor] * 1000 / (int32_t)eep_params.mode_sms_decel_count[motor];
			}
			else
			{
				run_steps[motor] = 0;
				accel_steps_x1000[motor] = 0;
				decel_steps_x1000[motor] = 0;
			}<commit_msg>Fixed a bad define (missing variable)<commit_after>/* Standard includes. */
#include <stdio.h>

/* Kernel includes. */
#include "FreeRTOS.h"
#include "task.h"
#include "timers.h"

/* Atmel library includes. */
#include "asf.h"

#include "SEGGER_RTT.h"
#include "eep.h"
#include "sm.h"
#include "cam.h"
#include "ble.h"
#include "mode_sms.h"
#include "utils.h"

#define mode_smsTEST_NONE               0
#define mode_smsTEST_CAMERA             1
#define mode_smsTEST_EXPOSE_NOW	        2

#define mode_smsCONTROL_TIMER_RATE      (10 / portTICK_RATE_MS)

#define mode_smsCOUNT_SUM_TOO_HIGH(motor)   ((eep_params.mode_sms_leadin_count[motor] + eep_params.mode_sms_accel_count[motor] + eep_params.mode_sms_decel_count[motor] + eep_params.mode_sms_leadout_count[motor]) > (eep_params.mode_sms_count - 1))

static uint8_t state = MODE_SMS_STATE_STOP;
static bool finished = false;
static uint8_t test_mode = mode_smsTEST_NONE;
static uint32_t current_step;
static uint32_t current_loop;
static int32_t accel_steps_x1000[SM_MOTORS_USED];
static int32_t decel_steps_x1000[SM_MOTORS_USED];
static int32_t run_steps[SM_MOTORS_USED];
static uint32_t step_timer;
static uint32_t interval_timer;
static uint32_t remaining_step_time;
static uint32_t remaining_time;
static uint32_t overall_time;
static uint32_t elapsed_time;
static xTimerHandle xModeSmsControlTimer;

static void prvModeSmsControlCallback(void *pvParameters);

void vModeSmsInit(void)
{
#if !defined(DEBUG)
	for (uint8_t f = 0; f < MODE_SMS_MAX_KEY_FRAMES; f++)
	{
		for (uint8_t motor = 0; motor < SM_MOTORS_USED; motor++)
		{
			eep_params.mode_sms_positions[f].pos[motor] = 0;
		}
	}
#endif

	xModeSmsControlTimer = xTimerCreate(
		(const char * const) "ModeSmsCtrlTmr",
		mode_smsCONTROL_TIMER_RATE,
		pdTRUE,
		NULL,
		prvModeSmsControlCallback);
	configASSERT(xModeSmsControlTimer);
}

void vModeSmsUpdateIntervalToMinimum(void)
{
    eep_params.mode_sms_interval = utilsMAX(eep_params.mode_sms_interval, ulModeSmsGetMinimumInterval(eep_params.mode_sms_pre_time, eep_params.mode_sms_focus_time, eep_params.mode_sms_exposure_time, eep_params.mode_sms_post_time));
    vEepSave();
}

uint32_t ulModeSmsGetMinimumInterval(uint32_t pre_time, uint32_t focus_time, uint32_t exposure_time, uint32_t post_time)
{
    return pre_time + focus_time + exposure_time + post_time + 500;
}

void vModeSmsPause(void)
{
	if (state == MODE_SMS_STATE_STOP) return;
	if (bModeSmsIsPaused()) return;
	
	xTimerStop(xModeSmsControlTimer, 0);
}

void vModeSmsResume(void)
{
	if (state == MODE_SMS_STATE_STOP) return;
	if (!bModeSmsIsPaused()) return;
	
	xTimerStart(xModeSmsControlTimer, 0);
}

void vModeSmsStartExposeNow(void)
{
	if (state != MODE_SMS_STATE_STOP) return;

	taskENTER_CRITICAL();
	{
		test_mode = mode_smsTEST_EXPOSE_NOW;
		
		current_step = 0;
		current_loop = 0;
		step_timer = 0;
		interval_timer = 0;
		
		state = MODE_SMS_STATE_WAIT_PRE_TIME;
		xTimerStart(xModeSmsControlTimer, 0);
	}
	taskEXIT_CRITICAL();
}

void vModeSmsStartCameraTest(void)
{
	if (state != MODE_SMS_STATE_STOP) return;
	
	taskENTER_CRITICAL();
	{
		test_mode = mode_smsTEST_CAMERA;
		
		current_step = 0;
		current_loop = 0;
		step_timer = 0;
		interval_timer = 0;
		
		state = MODE_SMS_STATE_WAIT_PRE_TIME;
		xTimerStart(xModeSmsControlTimer, 0);
	}
	taskEXIT_CRITICAL();
}

void vModeSmsStart(void)
{
	if (state != MODE_SMS_STATE_STOP) return;
	
	taskENTER_CRITICAL();
	{
		for (uint8_t motor = 0; motor < SM_MOTORS_USED; motor++)
		{
			if (eep_params.mode_sms_accel_count[motor] == 0)
			{
				eep_params.mode_sms_accel_count[motor] = 1;
			}
			if (eep_params.mode_sms_decel_count[motor] == 0)
			{
				eep_params.mode_sms_decel_count[motor] = 1;
			}
			
			while (true)
			{
				if (mode_smsCOUNT_SUM_TOO_HIGH(motor) && eep_params.mode_sms_leadin_count[motor] > 0)
				{
					eep_params.mode_sms_leadin_count[motor]--;
				}
				if (mode_smsCOUNT_SUM_TOO_HIGH(motor) && eep_params.mode_sms_leadout_count[motor] > 0)
				{
    				eep_params.mode_sms_leadout_count[motor]--;
				}
				if (mode_smsCOUNT_SUM_TOO_HIGH(motor) && eep_params.mode_sms_accel_count[motor] > 1)
				{
    				eep_params.mode_sms_accel_count[motor]--;
				}
				if (mode_smsCOUNT_SUM_TOO_HIGH(motor) && eep_params.mode_sms_decel_count[motor] > 1)
				{
    				eep_params.mode_sms_decel_count[motor]--;
				}
				if (!mode_smsCOUNT_SUM_TOO_HIGH(motor))
				{
    				break;
				}
			}

			int32_t steps = eep_params.mode_sms_positions[1].pos[motor] - eep_params.mode_sms_positions[0].pos[motor];

			int32_t img_count = (int32_t)eep_params.mode_sms_count;
			int32_t step_count = (int32_t)eep_params.mode_sms_count - 1;
			int32_t ramp_count = (int32_t)eep_params.mode_sms_accel_count[motor] + (int32_t)eep_params.mode_sms_decel_count[motor];
			int32_t lead_count = (int32_t)eep_params.mode_sms_leadin_count[motor] + (int32_t)eep_params.mode_sms_leadout_count[motor];
			int32_t move_count = step_count - lead_count;
			int32_t run_count = step_count - lead_count - ramp_count;

			if (move_count > 0 && steps != 0)
			{
                run_steps[motor] = 2 * steps / (2 * run_count + ramp_count);
                accel_steps_x1000[motor] = run_steps[motor] * 1000 / (int32_t)eep_params.mode_sms_accel_count[motor];
                decel_steps_x1000[motor] = run_steps[motor] * 1000 / (int32_t)eep_params.mode_sms_decel_count[motor];
			}
			else
			{
				run_steps[motor] = 0;
				accel_steps_x1000[motor] = 0;
				decel_steps_x1000[motor] = 0;
			}
{'fraction_non_alphanumeric': 0.10505013338239352, 'fraction_numerical': 0.026124551559194187, 'mean_word_length': 4.7493389740877845, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 17, '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': '2106000', 'n_tokens_mistral': 5150, 'n_tokens_neox': 4503, 'n_words': 665}