#include <stdbool.h>
#include <stdlib.h>
#include <math.h>

#include "data_manager.h"
#include "barometer.h"
#include "timer.h"


/* utility functions */
static float pressure_to_metres_asl(float real, float setting)
{
	return 44330.f*(1.f-powf(real/setting, 1/5.255));
}

void data_manager_init(struct data_ctx *ctx, struct timer *timer, struct barometer *barometer)
{
	ctx->setting = 1013.25; /* FIXME don't hardcode, at least not here */
	ctx->timer = timer;
	ctx->barometer = barometer;
	data_manager_tick(ctx);
}

void data_manager_tick(struct data_ctx *ctx)
{
	/* FIXME altitude rate */
	ctx->timestamp = ctx->timer->get_time();
	ctx->pressure = ctx->barometer->read_pressure();
	ctx->altitude = pressure_to_metres_asl(ctx->pressure, ctx->setting);
}