/***********************************************************************//**
 * @file		Common.h
 * @brief		Contains functions and variables for global usage
 * @version		1.0
 * @date		21. April. 2014
 * @author		Michal Semrad
 **************************************************************************/

#ifndef COMMON_H
#define COMMON_H

#include "LPC17xx.h"
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
#include "queue.h"
#include "timers.h"

#include "SerialCOM.h"

#define MODESTART 0
#define MODEXYZ 1
#define MODEANGLES 2
#define MODEALPHAGRIP 3
#define MODEAUT 4
#define MODEPAUSE 5
#define MODESTARTPOS 6

#define EXCYCLE		0
#define EXONCE		1

#define	PWMNORMAL 0
#define PWMFIRSTMULTI 1
#define PWMFIRSTSINGLE 2

#define LEDOFF 0
#define LEDON 1
#define LEDBLINKING 2

typedef struct {
	int16_t xyzabg[6];
	int16_t speed;
	int16_t delay;
} StructInstruction;

xSemaphoreHandle xPWMMutex;
xSemaphoreHandle xAngXyzMutex;
xSemaphoreHandle xMutexSpeed;
xSemaphoreHandle xMutexPwmMode;
xSemaphoreHandle xMutexKinMode;
xSemaphoreHandle xMutexDelay;
xSemaphoreHandle xMutexInstCount;
xSemaphoreHandle xMutexExecutType;
xSemaphoreHandle xMutexLedStatus;
xSemaphoreHandle xSemaphoreCOM;
xSemaphoreHandle xSemaphoreNewData;
xSemaphoreHandle xSemaphoreTime;
xSemaphoreHandle xSemaphoreStartAut;

xQueueHandle xSerialReceive;

xTaskHandle  xHandleAutomat;
xTimerHandle Timer20ms;

StructInstruction instructions[100];
int16_t xyzabg[6];
uint16_t anglesfrac[6];
int PWMvalue[6];
int speed;
int PWMmode;
int kinematicMode;
int delayTime;
int instcount;
int executingType;
int LedStatus;

void CreateFreeRTOSElements(void);
void CopyToAddress(char* where, char* what, int count);

void SetSpeed(int value);
int GetSpeed();
void SetPWMmode(int value);
int GetPWMmode();
void SetKinMode(int value);
int GetKinMode();
void SetDelay(int value);
int GetDelay();
void SetInstCount(int value);
int GetInstCount();
void SetExecutType(int value);
int GetExecutType();
void SetLedStatus(int value);
int GetLedStatus();

#endif /* COMMON_H */
