irom static app_action_t application_function_i2c_sensor_calibrate(const string_t *src, string_t *dst)
{
	int intin;
	i2c_sensor_t sensor;
	double factor;
	double offset;

	if(parse_int(1, src, &intin, 0) != parse_ok)
	{
		string_cat(dst, "> invalid i2c sensor\n");
		return(app_action_error);
	}

	sensor = (i2c_sensor_t)intin;

	if(parse_float(2, src, &factor) != parse_ok)
	{
		string_cat(dst, "> invalid factor\n");
		return(app_action_error);
	}

	if(parse_float(3, src, &offset) != parse_ok)
	{
		string_cat(dst, "> invalid offset\n");
		return(app_action_error);
	}

	if(!i2c_sensor_setcal(sensor, factor, offset))
	{
		string_format(dst, "> invalid i2c sensor: %d\n", (int)sensor);
		return(app_action_error);
	}

	string_format(dst, "> i2c sensor %d calibration set to factor ", (int)sensor);
	string_double(dst, config.i2c_sensors.sensor[sensor].calibration.factor, 4, 1e10);
	string_cat(dst, ", offset: ");
	string_double(dst, config.i2c_sensors.sensor[sensor].calibration.offset, 4, 1e10);
	string_cat(dst, "\n");

	return(app_action_normal);
}
irom static app_action_t application_function_i2c_sensor_calibrate(application_parameters_t ap)
{
	i2c_sensor_t sensor;
	float factor;
	float offset;
	uint16_t length;

	sensor = atoi((*ap.args)[1]);
	factor = string_to_double((*ap.args)[2]);
	offset = string_to_double((*ap.args)[3]);

	if(!i2c_sensor_setcal(sensor, factor, offset))
	{
		snprintf(ap.dst, ap.size, "> invalid i2c sensor: %d\n", (int)sensor);
		return(app_action_error);
	}

	length = snprintf(ap.dst, ap.size, "> i2c sensor %d calibration set to factor ", (int)sensor);
	ap.dst += length;
	ap.size -= length;

	length = double_to_string(config->i2c_sensors.sensor[sensor].calibration.factor, 4, 1e10, ap.size, ap.dst);
	ap.dst += length;
	ap.size -= length;

	length = snprintf(ap.dst, ap.size, ", offset: ");
	ap.dst += length;
	ap.size -= length;

	length = double_to_string(config->i2c_sensors.sensor[sensor].calibration.offset, 4, 1e10, ap.size, ap.dst);
	ap.dst += length;
	ap.size -= length;

	length = snprintf(ap.dst, ap.size, "\n");
	ap.dst += length;
	ap.size -= length;

	return(app_action_normal);
}