#ifndef _IMU_SERIAL_HPP_
#define _IMU_SERIAL_HPP_

#include <string>
#include <glm/glm.hpp>
#include <glm/gtc/quaternion.hpp>
#include <mutex>
#include <thread>

class ImuSerial
{
	public:
		ImuSerial(const std::string &serial_device, unsigned int baud);
		~ImuSerial();
		void start();
		void stop();

		void set_device(const std::string &device);
		void set_baudrate(const unsigned int baud);
		unsigned int get_baudrate();
		const std::string &get_device();
		bool runs();
		glm::quat get_quaternion();
		glm::vec3 get_accel();
		glm::vec3 get_gyro();
		const glm::mat4 get_rot_matrix();

	private:
		std::string serial_device;
		unsigned int baud;
		std::mutex quat_mutex;
		std::mutex acc_mutex;
		std::mutex gyr_mutex;
		glm::quat quaternion;
		glm::vec3 acc;
		glm::vec3 gyr;
		std::thread worker;
		int stop_flag;
		int fd;
		friend void working_thread(ImuSerial *self);
		friend void update_quaternion(ImuSerial *self, const glm::quat &quat);
		friend void update_acc_value(ImuSerial *self, const glm::vec3 &acc);
		friend void update_gyr_value(ImuSerial *self, const glm::vec3 &gyr);
};


#endif /* _IMU_SERIAL_HPP_ */
