#ifndef WEBCAM_H_
#define WEBCAM_H_

#include "MCF52233_PE.h"
#include "PE_Types.h"
#include "PE_Error.h"
#include "PE_Const.h"
#include "FreeRTOS.h"
#include "queue.h"
#include "semphr.h"
#include "task.h"


/*--------------Camera constants------------------*/
// camera commands
#define CMD_PREFIX 0xAA
#define CMD_INITIAL 0x01
#define CMD_GETPICTURE 0x04
#define CMD_SNAPSHOT 0x05
#define CMD_PACKAGESIZE 0x06
#define CMD_BAUDRATE 0x07
#define CMD_RESET 0x08
#define CMD_POWEROFF 0x09
#define CMD_DATA 0x0A
#define CMD_SYNC 0x0D
#define CMD_ACK 0x0E
#define CMD_NAK 0x0F
#define CMD_LIGHTFREQ 0x13

// camera constants
#define COLOR_GRAY_2 1
#define COLOR_GRAY_4 2
#define COLOR_GRAY_8 3
#define COLOR_RGB_12 5
#define COLOR_RGB_16 6
#define COLOR_JPEG 7

#define PREVIEW_R80x60 1
#define PREVIEW_R160x120 3

#define JPEG_R80x64 1
#define JPEG_R160x128 3
#define JPEG_R320x240 5
#define JPEG_R640x480 7

#define TYPE_SNAPSHOT 1
#define TYPE_PREVIEW 2
#define TYPE_JPEG 5

#define SNAPSHOT_COMPRESSED 0
#define SNAPSHOT_RAW 1

#define FREQ_50HZ 0
#define FREQ_60HZ 1

#define RESET_SYSTEM 0
#define RESET_STATES 1


#define cam_reset_packages() start_package_counter=0; last_length=4

#define cam_next_packages() start_package_counter=akt_package_counter-1; last_length=next_length

#define cam_packages_total(data_length) akt_total_packages = (((data_length / (PACKAGE_SIZE - 6)) * (PACKAGE_SIZE - 6))< data_length) ? (data_length / (PACKAGE_SIZE - 6))+1:(data_length / (PACKAGE_SIZE - 6))

extern int PACKAGE_SIZE; // the packet size to use for jpeg
extern int akt_package_counter; //Globlana premena pre aktualny paket, pouziva sa pri http nacitavanie s kamery
extern int akt_total_packages;
extern unsigned long cam_data_Length;
extern int start_package_counter;
extern int last_length;
extern int next_length;
xSemaphoreHandle CamPictureMutex;


int get_jpeg_data(int data_length);
uint32_t take_picture(uint8_t picture_type);
int set_snapshot(uint8_t snapshot_type);
int reset_camera(uint8_t reset_type);
int poweroff_camera();
int init_camera(uint8_t color_type, uint8_t preview_resolution,
		uint8_t jpeg_resolution);
int sync_camera();
int get_ack(uint8_t expected_ack);
int get_jpeg_data_packet(char* buffer, int length);
void picture_taker(void *pvParameters);

//Podmienka ak je nacitany obrazok tak mozes ho posielat
char cam_podmienka;

/*
** ===================================================================
**     Method      :  kameraBit_GetVal (component BitIO)
**
**     Description :
**         This method returns an input value.
**           a) direction = Input  : reads the input value from the
**                                   pin and returns it
**           b) direction = Output : returns the last written value
**         Note: This component is set to work in Output direction only.
**     Parameters  : None
**     Returns     :
**         ---             - Input value. Possible values:
**                           FALSE - logical "0" (Low level)
**                           TRUE - logical "1" (High level)

** ===================================================================
*/
#define kameraBit_GetVal() ( \
    (bool)((getReg8(PORTTA) & 0x02))   /* Return port output data */ \
  )

/*
** ===================================================================
**     Method      :  kameraBit_PutVal (component BitIO)
**
**     Description :
**         This method writes the new output value.
**     Parameters  :
**         NAME       - DESCRIPTION
**         Val             - Output value. Possible values:
**                           FALSE - logical "0" (Low level)
**                           TRUE - logical "1" (High level)
**     Returns     : Nothing
** ===================================================================
*/
void kameraBit_PutVal(bool Val);

/*
** ===================================================================
**     Method      :  kameraBit_ClrVal (component BitIO)
**
**     Description :
**         This method clears (sets to zero) the output value.
**     Parameters  : None
**     Returns     : Nothing
** ===================================================================
*/
#define kameraBit_ClrVal() ( \
    (void)setReg8(CLRTA, 0xFD)         /* CLRTA1=0x00 */ \
  )

/*
** ===================================================================
**     Method      :  kameraBit_SetVal (component BitIO)
**
**     Description :
**         This method sets (sets to one) the output value.
**     Parameters  : None
**     Returns     : Nothing
** ===================================================================
*/
#define kameraBit_SetVal() ( \
    (void)setReg8(SETTA, 0x02)         /* SETTA1=0x01 */ \
  )



/* END kameraBit. */
#endif

