/**
 * \brief Test MCP980X
 *
 * This test reads temperature values returned by MCP980X every 100ms and check if the temperature is delightful.
 *
 * \param test Current test case.
 */
static void run_mcp980x_test(const struct test_case *test)
{
	/* Initialize MCP980X driver. */
	mcp980x_init();

	/* Set MCP980X configuration.
	 * Disable ONE-SHOT mode;
	 * 12-bit ADC resolution;
	 * 2 fault queue cycles;
	 * Active-high alert polarity;
	 * Alert output in interrupt mode;
	 * Disable shutdown mode.
	 */
	mcp980x_set_configuration(
			MCP980X_CONFIG_RESOLUTION_12_BIT          |
			MCP980X_CONFIG_FAULT_QUEUE_2              |
			MCP980X_CONFIG_ALERT_POLARITY_ACTIVE_HIGH |
			MCP980X_CONFIG_INTERRUPT_MODE);

	uint32_t i = 0;
	uint32_t ul_unused = 0;
	for (; i < MCP980X_MAX_SAMPLES; i++) {
		/* Enable One-Shot mode to perform a single temperature measurement. */
		if (TWI_SUCCESS != mcp980x_one_shot_mode()) {
			mcp980x_test_flag = 1;
			break;
		}

		/* Retrieve ambient temperature every 100ms. */
		mdelay(100);
		if (TWI_SUCCESS != mcp980x_get_temperature(&temp_buf[i], &ul_unused)) {
			mcp980x_test_flag = 1;
			break;
		}
	}

	/* Test1: if TWI bus operation is successful */
	test_assert_true(test, mcp980x_test_flag == 0, "Test1: TWI bus operation is aborted!");

	/* Test2: if the temperature values returned are correct */
	for (i = 0; i < MCP980X_MAX_SAMPLES; i++) {
		if ((temp_buf[i] > MCP980X_HIGHEST_TEMP) || (temp_buf[i] < MCP980X_LOWEST_TEMP)) {
			mcp980x_test_flag = 1;
			break;
		}
	}
	test_assert_true(test, mcp980x_test_flag == 0, "Test2: The temperature is undelightful!");

	/* Test3: if the temperature change is reasonable */
	volatile int8_t temp_delta = 0;
	for (i = 0; i < (MCP980X_MAX_SAMPLES - 1); i++) {
		temp_delta = (temp_buf[i] > temp_buf[i + 1]) ?
				(temp_buf[i] - temp_buf[i + 1]) : (temp_buf[i + 1] - temp_buf[i]);
		if (temp_delta > MCP980X_TEMP_DELTA) {
			mcp980x_test_flag = 1;
			break;
		}
	}
	test_assert_true(test, mcp980x_test_flag == 0, "Test3: The temperature change is unreasonable!");
}
/**
 * \brief Application entry point for TWI temperature sensor example.
 *
 * \return Unused (ANSI-C compatibility).
 */
int main(void)
{
	int8_t temperature_int = 0;
	uint32_t temperature_dec = 0;

	/* Initialize the SAM system */
	sysclk_init();
	board_init();

	/* Initialize the console UART */
	configure_console();

	/* Output example information */
	puts(STRING_HEADER);

	/* Configure systick for 1 ms */
	printf("Configure system tick to get 1ms tick period.\n\r");
	if (SysTick_Config(sysclk_get_cpu_hz() / 1000)) {
		printf("-E- Systick configuration error\n\r");
		while (1) {
			/* Capture error */
		}
	}

	/* Initialize MCP980X driver. */
	mcp980x_init();

	/* Set MCP980X configuration.
	 * Disable ONE-SHOT mode;
	 * 12-bit ADC resolution;
	 * 2 fault queue cycles;
	 * Active-high alert polarity;
	 * Alert output in interrupt mode;
	 * Disable shutdown mode.
	 */
	mcp980x_set_configuration(
			MCP980X_CONFIG_RESOLUTION_12_BIT          |
			MCP980X_CONFIG_FAULT_QUEUE_2              |
			MCP980X_CONFIG_ALERT_POLARITY_ACTIVE_HIGH |
			MCP980X_CONFIG_INTERRUPT_MODE);

	/* Set temperature limit and then get it. */
	mcp980x_set_temperature_limit(TEMP_LIMIT_MAX, TEMP_LIMIT_DEC);
	mcp980x_get_temperature_limit(&temperature_int, &temperature_dec);
	printf("Temperature Limit:      %d.%04lu\r\n", temperature_int,
			(unsigned long)temperature_dec);

	/* Set temperature hysteresis and then get it. */
	mcp980x_set_temperature_hysteresis(TEMP_LIMIT_MIN, TEMP_LIMIT_DEC);
	mcp980x_get_temperature_hysteresis(&temperature_int, &temperature_dec);
	printf("Temperature Hysteresis: %d.%04lu\r\n", temperature_int,
			(unsigned long)temperature_dec);

	while (1) {
		/* Enable One-Shot mode to perform a single temperature measurement. */
		if (TWI_SUCCESS != mcp980x_one_shot_mode()) {
			break;
		}

		/* Retrieve ambient temperature every second and print it. */
		mdelay(1000);
		if (TWI_SUCCESS !=
				mcp980x_get_temperature(&temperature_int,
				&temperature_dec)) {
			break;
		}
		printf("Ambient Temperature:    %d.%04lu\r\n", temperature_int,
				(unsigned long)temperature_dec);
	}

	printf("TWI bus operation error!\r\n");
	while (1) {
		/* Do nothing */
	}
}