/*
 * GTCP.h
 */

#ifndef GTCP_H_
#define GTCP_H_

#ifdef __MSP430F5529__
#include <msp430f5529.h>
#elif defined __MSP430FR6989__
#include <msp430fr6989.h>
#endif
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include "GTCP_SPI.h"
#include "GTCP_I2C.h"
#include "GTCP_UART.h"

#define MAX_MATRIX_SWITCHES_X 0x10
#define MAX_MATRIX_SWITCHES_Y 0x10

#define TOUCH_HEADER 0x10
#define TOUCH_COORDINATE_RELEASED 0x10
#define TOUCH_COORDINATE_TOUCHED 0x11
#define TOUCH_MATRIXSWITCH_RELEASED 0x20
#define TOUCH_MATRIXSWITCH_TOUCHED 0x21
#define TOUCH_CUSTOMSWITCH_RELEASED 0x30
#define TOUCH_CUSTOMSWITCH_TOUCHED 0x31

#define LINE_DRAW 0
#define BOX_DRAW 1
#define BOXFILL_DRAW 2

#define PEN_BACKGROUND_COLOR 0
#define PEN_CHARACTER_COLOR 1

#define IMAGE_FORMAT_MONOCHROME 0x81
#define IMAGE_FORMAT_6BIT 0x86
#define IMAGE_FORMAT_12BIT 0x8C
#define IMAGE_FORMAT_16BIT 0x90
#define IMAGE_FORMAT_16BIT_HS 0x91
#define IMAGE_FORMAT_24BIT 0x98
#define IMAGE_FORMAT_BMP 0xF0

#define GTCP_PORT0 0 // GPIO 0-7
#define GTCP_PORT1 1 // GPIO 8-15
#define GTCP_PORT2 2 // GPIO 16-23
#define GTCP_PORT3 3 // GPIO 24, 25
#define GPIO0 0b00000001
#define GPIO1 0b00000010
#define GPIO2 0b00000100
#define GPIO3 0b00001000
#define GPIO4 0b00010000
#define GPIO5 0b00100000
#define GPIO6 0b01000000
#define GPIO7 0b10000000
#define GPIO8 0b00000001
#define GPIO9 0b00000010
#define GPIO10 0b00000100
#define GPIO11 0b00001000
#define GPIO12 0b00010000
#define GPIO13 0b00100000
#define GPIO14 0b01000000
#define GPIO15 0b10000000
#define GPIO16 0b00000001
#define GPIO17 0b00000010
#define GPIO18 0b00000100
#define GPIO19 0b00001000
#define GPIO20 0b00010000
#define GPIO21 0b00100000
#define GPIO22 0b01000000
#define GPIO23 0b10000000
#define GPIO24 0b00000001
#define GPIO25 0b00000010

// Memory Switch Number Defines
#define INTERNATIONAL_FONT_SET_MSW 0
#define CHARACTER_TABLE_TYPE_MSW 1
#define HORIZONTAL_SCROLL_SPEED_MSW 2
#define REVERSE_DISPLAY_MSW 3
#define WRITE_MIXTURE_DISPLAY_MODE_MSW 4
#define BRIGHTNESS_LEVEL_MSW 5
#define DISPLAY_ORIENTATION_MSW 6
#define WRITE_SCREEN_MODE_MSW 7
#define FONT_SIZE_MSW 8
#define TWO_BYTE_CHARACTER_MSW 9
#define FONT_MAGNIFICATION_X_MSW 10
#define FONT_MAGNIFICATION_Y_MSW 11
#define CHARACTER_STYLE_MSW 12
#define TWO_BYTE_CHARACTER_TYPE_MSW 13
#define DOWNLOAD_6X8_CHARACTER_RESTORE_MSW 16
#define DOWNLOAD_8X16_CHARACTER_RESTORE_MSW 17
#define DOWNLOAD_16X16_CHARACTER_RESTORE_MSW 18
#define FROM_MACRO_EXECUTE_AT_POWER_ON_MSW 19
#define UART_BAUDRATE_MSW 48
#define UART_PARITY_MSW 49
#define MACRO_END_CODE_ENABLE_DISABLE_MSW 52
#define MACRO_END_CODE_MSW 53
#define MACRO_END_CLEAR_SCREEN_MSW 54
#define TOUCH_GAIN_MSW 58
#define TOUCH_THRESHOLD_MSW 59
#define TOUCH_TRANSMIT_MODE_MSW 60
#define TOUCH_SCAN_PERIOD_MSW 61
#define TOUCH_SENSITIVITY_SELECTION_MSW 62
#define TOUCH_PACKAGE_SELECTION_MSW 63

#define CHARACTER_TABLE_6X8 1
#define CHARACTER_TABLE_8X16 2
#define CHARACTER_TABLE_12X24 3
#define CHARACTER_TABLE_16X32 4
#define CHARACTER_TABLE_6X8_SIZE 768
#define CHARACTER_TABLE_8X16_SIZE 2048
#define CHARACTER_TABLE_12X24_SIZE 4608
#define CHARACTER_TABLE_16X32_SIZE 8192

//TFT structure to hold module information.
typedef struct {
	unsigned int WIDTH;
	unsigned int HEIGHT;
	unsigned int LINES;
	unsigned int tspID;
	uint8_t gain;
	uint8_t threshold;
	float bootVer;
	float firmwareVer;
	char productType[16];
	char tspName[16];
} TFT;

TFT display;

enum interfaceType{
    UART,
    SPI,
    I2C
};

enum interfaceType interface;

// Display Memory Configuration values
uint16_t Xdots;
uint16_t Ydots;
uint16_t Max_Xdot;
uint16_t Max_Ydot;
uint16_t Max_Xdot_CurtWin;
uint16_t Max_Ydot_CurtWin;
uint32_t DispMemSize;
uint32_t Max_DispMemAddr;

// Function Prototypes
void GTCP_print(unsigned char data);
uint8_t GTCP_read();
void GTCP_readMulti(uint8_t length);
void GTCP_start(enum interfaceType selectedInterface);
void GTCP_readDisplayInfo();
void GTCP_printString(unsigned char* string);
void GTCP_sendCommand(uint8_t* data, int size);
void GTCP_initialize();
void GTCP_back();
void GTCP_forward();
void GTCP_lineFeed();
void GTCP_home();
void GTCP_carriageReturn();
void GTCP_crlf();
void GTCP_setCursor(uint16_t x, uint16_t y);
void GTCP_clearScreen();
void GTCP_lineClear();
void GTCP_lineEndClear();
void GTCP_cursorOn();
void GTCP_cursorOff();
void GTCP_reset();
void GTCP_setDisplayScreenMode();
void GTCP_setAllScreenMode();
void GTCP_setNormalWriteDisplayMode();
void GTCP_setThruWriteDisplayMode();
void GTCP_enableMultiByteChars();
void GTCP_disableMultiByteChars();
void GTCP_setMultiByteCharSet(uint8_t code);
void GTCP_enableCustomChars();
void GTCP_disableCustomChars();
void GTCP_defineCustomChar(uint8_t codeStart, uint8_t codeEnd, uint8_t format, uint8_t xDir, uint8_t *data);
void GTCP_deleteCustomChar(uint8_t type, uint8_t code);
void GTCP_setAsciiVariant(uint8_t code);
void GTCP_setCharSet(uint8_t code);
void GTCP_setOverwriteMode();
void GTCP_setVerticalScrollMode();
void GTCP_setHorizontalScrollMode();
void GTCP_setHorizontalScrollONMode();
void GTCP_setHorizScrollSpeed(uint8_t speed);
void GTCP_invertOff();
void GTCP_invertOn();
void GTCP_wait(uint8_t wait);
void GTCP_shortWait(uint8_t time);
void GTCP_blinkScreenOff();
void GTCP_blinkScreenOn(uint8_t pattern, uint8_t onTime, uint8_t offTime, uint8_t cycles);
void GTCP_scrollScreen(uint16_t x, uint16_t y, uint16_t times, uint8_t speed);
void GTCP_curtainAction(uint8_t direction, uint8_t speed, uint8_t red, uint8_t green, uint8_t blue);
void GTCP_springAction(uint8_t direction, uint8_t speed, uint16_t x, uint16_t y);
void GTCP_randomAction(uint8_t actionType, uint8_t speed, uint16_t x, uint16_t y);
void GTCP_fadeInAction(uint8_t speed, uint16_t x, uint16_t y);
void GTCP_fadeOutAction(uint8_t speed);
void GTCP_displayOff();
void GTCP_displayOn();
void GTCP_displayOffWaitTouch();
void GTCP_setScreenBrightness(uint8_t level);
void GTCP_setFontWidth(uint8_t widthSetting);
void GTCP_setFontSize(uint8_t size);
void GTCP_setOutlineFontSize(uint16_t charLineHeight, uint16_t nominalCharY, uint16_t nominalCharX, int16_t baseOffsetY);
void GTCP_setUserFontAddressSize(uint32_t address, uint32_t size);
void GTCP_setOutlineFontType(uint8_t fontType);
void GTCP_setFontMagnification(uint8_t x, uint8_t y);
void GTCP_setCharacterStyle(uint8_t style);
void GTCP_setCharacterColor(uint8_t red, uint8_t green, uint8_t blue);
void GTCP_setBackgroundColor(uint8_t red, uint8_t green, uint8_t blue);
void GTCP_setShadowBorderingColor(uint8_t red, uint8_t green, uint8_t blue);
void GTCP_turnBackgroundOff();
void GTCP_turnBackgroundOn();
void GTCP_selectWindow(uint8_t window);
void GTCP_defineWindow(uint8_t window, uint16_t x, uint16_t y, uint16_t width, uint16_t height);
void GTCP_deleteWindow(uint8_t window);
void GTCP_drawImage(uint16_t width, uint16_t height, uint8_t format, unsigned char *data);
void GTCP_storedBitImageDraw(uint8_t memory, uint32_t address, uint16_t xDefine, uint16_t xSize, uint16_t ySize, uint8_t format);
void GTCP_pixelDrawing(uint8_t pen, uint16_t x, uint16_t y);
void GTCP_drawLineBox(uint8_t mode, uint8_t pen, uint16_t xStart, uint16_t yStart, uint16_t xEnd, uint16_t yEnd);

void GTCP_enableTouchTransmission();
void GTCP_disableTouchTransmission();
void GTCP_setTouchChannel(uint8_t channel);
void GTCP_setSingleTouchMode();
void GTCP_setMultiTouchMode(uint8_t maxTouches);
void GTCP_coordinatesMode(uint8_t channel);
void GTCP_switchMatrixMode(uint8_t channel, uint8_t switchesX, uint8_t switchesY, uint8_t clearanceX, uint8_t clearanceY);
void GTCP_customSwitchModeSingle(uint8_t channel, uint16_t switchX, uint16_t switchY, uint16_t sizeX, uint16_t sizeY);
void GTCP_customSwitchModeMultiple(uint8_t channel, uint8_t switchNum);
void GTCP_customSwitchParam(uint16_t switchX, uint16_t switchY, uint16_t sizeX, uint16_t sizeY);

void GTCP_IOPortSetting(uint8_t portNumber, uint8_t portSetting);
void GTCP_IOPortOutput(uint8_t portNumber, uint8_t portValue);
void GTCP_IOPortInput(uint8_t portNumber);
void GTCP_setSingleMemorySW(uint8_t memorySW, uint8_t data);
void GTCP_setMultipleMemorySwitchHeader(uint8_t numSettings);
void GTCP_setMemorySwitch(uint8_t switchNumber, uint8_t data);
void GTCP_getSingleMemorySWData(uint8_t switchNum);
void GTCP_getMultipleMemorySWData(uint8_t numReads, uint8_t *data);
void GTCP_16x16CharacterDefinition(uint8_t codeUpper, uint8_t codeLower, uint8_t *data);
void GTCP_16x16CharacterDelete(uint8_t codeUpper, uint8_t codeLower);
void GTCP_32x32CharacterDefinition(uint8_t codeUpper, uint8_t codeLower, uint8_t *data);
void GTCP_32x32CharacterDelete(uint8_t codeUpper, uint8_t codeLower);
void GTCP_downloadCharacterSave(uint8_t fontSize);
void GTCP_downloadCharacterRestore(uint8_t fontSize);
void GTCP_enterUserSetupMode();
void GTCP_endUserSetupMode();
void GTCP_FROMUserFontDefinition(uint8_t table, uint8_t *data);
void GTCP_RAMMacroDefineDelete(unsigned int length, uint8_t *data);
void GTCP_FROMMacroDefineDelete(uint8_t registrationNum, unsigned int length, uint8_t interval, uint8_t idleTime, uint8_t *data);
void GTCP_macroExecution(uint8_t definitionNum, uint8_t interval, uint8_t idleTime);
void GTCP_macroEndCondition(uint8_t endCodeEnable, uint8_t endCode, uint8_t endScreenSetting);
void GTCP_memoryStore(uint32_t size, uint8_t memorySelect, uint32_t address, uint8_t *data);
void GTCP_memoryTransfer(uint32_t size, uint8_t destMemory, uint32_t destAddress, uint8_t srcMemory, uint32_t srcAddress);
void GTCP_readMemory(uint8_t size, uint8_t memorySelect, uint32_t address);
float GTCP_getBootVersion();
float GTCP_getFirmwareVersion();
void GTCP_getCharacterCodeInfo();
void GTCP_getLanguageType();
void GTCP_getMemoryChecksum(uint8_t address, uint8_t length);
uint32_t GTCP_getFROM2MemoryChecksum(uint32_t address, uint32_t size);
void GTCP_getProductType();
void GTCP_getHorizontalResolution();
void GTCP_getVerticalResolution();
void GTCP_getTSPName();
void GTCP_getTSPID();
void GTCP_getTouchGain();
void GTCP_getTouchThreshold();
void GTCP_touchSettingPackageDataStore(uint8_t packageNum, uint8_t *data);
void GTCP_touchSettingPackageSelection(uint8_t packageNum);
void setPowerSavingMode(uint8_t wakeup);
void setTouchScanPeriod(uint8_t period);
void GTCP_setDisplayOrientation(int degrees);

#endif /* GTCP_H_ */
