/*********************************************************************
 *                    Flowcode Custom Component Code
 *
 * File: PIC_Custom.c
 *
 * (c) 2009 Matrix Multimedia Ltd.
 * http://www.matrixmultimedia.com
 *
 * Software License Agreement
 *
 * The software supplied herewith by Matrix Multimedia Ltd (the
 * “Company”) for its Flowcode graphical programming language is
 * intended and supplied to you, the Company’s customer, for use
 * solely and exclusively on the Company's products. The software
 * is owned by the Company, and is protected under applicable
 * copyright laws. All rights are reserved. Any use in violation
 * of the foregoing restrictions may subject the user to criminal
 * sanctions under applicable laws, as well as to civil liability
 * for the breach of the terms and conditions of this licence.
 *
 * THIS SOFTWARE IS PROVIDED IN AN “AS IS” CONDITION. NO WARRANTIES,
 * WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
 * TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
 * IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
 * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
 *
 * Changelog:
 *
 *  date  | by | description
 * -------+----+-----------------------------------------------------
 * 240407 | ST | Created
 * 200612 | JK | Implement VFD code
 *
 ********************************************************************/

/*********************************************************************

 Return & parameter types:
   void
   char
   short
   char*

 Pin directions:
   0 = OUTPUT
   2 = INPUT
   3 = BIDIRECTIONAL

**********************************************************************

[Settings]
CLSID={DA77448F-7831-4F3E-969E-6AE4EE551B6F}
IsAnalogue=0
MultipleAllowed=0
Description=Custom Flowcode Component

**********************************************************************

[Port]
DesiredPort=0
PortMandatory=0
SamePortAsID=0

[Pins]
Count=0

[PinPort]
# PORTA = 0
# PORTB = 1
# PORTC = 2
# PORTD = 3
# PORTE = 4

[PinDesiredBit]

[PinDirection]
# DIR_INPUT = 2
# DIR_OUTPUT = 0
# DIR_BIDIRECTIONAL = 3

[PinMustUsePort]
# boolean

[SamePortAsPinX]
# -1 = any pin
# otherwise, the pin number

[PinMustUsePin]
# boolean

[PinValue]
# boolean (0 = off, 1 = on)

**********************************************************************

[MacroNames]
Count=14
1=writechar
2=InitDisplay
3=SetPower
4=SetBrightness
5=GotoXY
6=WriteChar
7=WriteString
8=Pixel
9=Line
10=Box
11=Bitmap
12=Clear
13=FontSize
14=FontMagnify

[MacroReturns]
1=void
2=void
3=void
4=void
5=void
6=void
6=void
8=void
9=void
10=void
11=void
12=void
13=void
14=void

[MacroIsPrivate]
1=1
2=0
3=0
4=0
5=0
6=0
7=0
8=0
9=0
10=0
11=0
12=0
13=0
14=0

[MacroParameters_writechar]
Count=1
1=ch

[MacroParamTypes_writechar]
1=char

[MacroParameters_InitDisplay]
Count=0

[MacroParamTypes_InitDisplay]


[MacroParameters_SetPower]
Count=1
1=on

[MacroParamTypes_SetPower]
1=char

[MacroParameters_SetBrightness]
Count=1
1=brightness

[MacroParamTypes_SetBrightness]
1=short

[MacroParameters_GotoXY]
Count=2
1=x
2=y

[MacroParamTypes_GotoXY]
1=short
2=short

[MacroParameters_WriteChar]
Count=1
1=ch

[MacroParamTypes_WriteChar]
1=char

[MacroParameters_WriteString]
Count=1
1=text

[MacroParamTypes_WriteString]
1=MX_STRING

[MacroParameters_Pixel]
Count=3
1=x
2=y
3=on

[MacroParamTypes_Pixel]
1=short
2=short
3=char

[MacroParameters_Line]
Count=5
1=StartX
2=StartY
3=EndX
4=EndY
5=on

[MacroParamTypes_Line]
1=short
2=short
3=short
4=short
5=char

[MacroParameters_Box]
Count=6
1=StartX
2=StartY
3=EndX
4=EndY
5=on
6=fill

[MacroParamTypes_Box]
1=short
2=short
3=short
4=short
5=char
6=char

[MacroParameters_Bitmap]
Count=4
1=width
2=height
3=length
4=bitmap

[MacroParamTypes_Bitmap]
1=short
2=short
3=char
4=char*

[MacroParameters_Clear]
Count=0

[MacroParamTypes_Clear]


[MacroParameters_FontSize]
Count=1
1=size

[MacroParamTypes_FontSize]
1=char

[MacroParameters_FontMagnify]
Count=2
1=ScaleX
2=ScaleY

[MacroParamTypes_FontMagnify]
1=char
2=char

 ********************************************************************/

/********************************************************************
 * ADDITIONAL CODE
 ********************************************************************/

/*DefinesCode_Start*/

/**** Macro Substitutions ****
%a = User Define 1
%b = User Define 2
%c = User Define 3
%d = User Define 4
%e = User Define 5
%f = User Define 6
%g = User Define 7
%h = User Define 8
%i = User Define 9
%j = User Define 10
******************************/

//common defines
#define MX_CUSTOM_1  %a
#define MX_CUSTOM_2  %b
#define MX_CUSTOM_3  %c
#define MX_CUSTOM_4  %d
#define MX_CUSTOM_5  %e
#define MX_CUSTOM_6  %f
#define MX_CUSTOM_7  %g
#define MX_CUSTOM_8  %h
#define MX_CUSTOM_9  %i
#define MX_CUSTOM_10 %j


//global variables
// None used atm
//char CUSTOM_A;
//char CUSTOM_B;


/*DefinesCode_End*/

/*InitialisationCode_Start*/

// Set ports to correct mode for use with display
// Input A0 is RDY
trisa = trisa | 0b00000001;
// Output A1 = WR
trisa = trisa & 0b11111101;
// Output D0-D7 is data
trisd = 0x00;
// Initialize display port to defined state
portd = 0x00;
porta = porta | 0x02;

/*InitialisationCode_End*/

/*InterruptCode_Start*/
/*InterruptCode_End*/

/********************************************************************
 * FUNCTIONS
 ********************************************************************/

void writechar(char ch)
{
/*Macro_writechar_Start*/
	char ready;

	// Wait for display to become ready to accept input
	do {
		ready = (porta & 0x01) == 0x01;
	} while ( ! ready );

	// WR = 0
	porta = porta & 0b11111101;

	// Write data to port
	portd = ch;

	// Slight delay
	delay_us(1);

	// WR = 1 to signal display to read data
	porta = porta | 0b00000010;

	// wait for display to read data
	delay_us(1);
/*Macro_writechar_End*/
}

void InitDisplay()
{
/*Macro_InitDisplay_Start*/
	writechar(0x1b);
	writechar(0x40);
/*Macro_InitDisplay_End*/
}


void SetPower(char on)
{
/*Macro_SetPower_Start*/
	writechar(0x1f);
	writechar(0x28);
	writechar(0x61);
	writechar(0x40);
	writechar( on ? 0x01 : 0x00 );
/*Macro_SetPower_End*/
}


void SetBrightness(char brightness)
{
/*Macro_SetBrightness_Start*/
	writechar(0x1f);
	writechar(0x58);
	writechar(brightness);
/*Macro_SetBrightness_End*/
}


void GotoXY(short x, short y)
{
/*Macro_GotoXY_Start*/
	writechar(0x1f);
	writechar(0x24);
	writechar(x & 0xff);
	writechar((x >> 8) & 0xff);
	writechar(y & 0xff);
	writechar((y >> 8) & 0xff);
/*Macro_GotoXY_End*/
}


void WriteChar(char ch)
{
/*Macro_WriteChar_Start*/
	writechar(ch);
/*Macro_WriteChar_End*/
}


void WriteString(MX_UINT8 * text, MX_UINT8 MSZ_text)
{
/*Macro_WriteString_Start*/
	short i;

	for (i = 0; i < MSZ_text; i++) {
		if (text[i] == 0) {
			break;
		}
		writechar(text[i]);
	}
/*Macro_WriteString_End*/
}


void Pixel(short x, short y, char on)
{
/*Macro_Pixel_Start*/
	writechar(0x1f);
	writechar(0x28);
	writechar(0x64);
	writechar(0x10);
	writechar(on ? 0x01 : 0x00);
	writechar(x & 0xff);
	writechar((x >> 8) & 0xff);
	writechar(y & 0xff);
	writechar((y >> 8) & 0xff);
/*Macro_Pixel_End*/
}


void Line(short StartX, short StartY, short EndX, short EndY, char on)
{
/*Macro_Line_Start*/
	writechar(0x1f);
	writechar(0x28);
	writechar(0x64);
	writechar(0x11);
	writechar(0x0);
	writechar(on ? 0x01 : 0x00);
	writechar(StartX & 0xff);
	writechar((StartX >> 8) & 0xff);
	writechar(StartY & 0xff);
	writechar((StartY >> 8) & 0xff);
	writechar(EndX & 0xff);
	writechar((EndX >> 8) & 0xff);
	writechar(EndY & 0xff);
	writechar((EndY >> 8) & 0xff);
/*Macro_Line_End*/
}


void Box(short StartX, short StartY, short EndX, short EndY, char on, char fill)
{
/*Macro_Box_Start*/
	writechar(0x1f);
	writechar(0x28);
	writechar(0x64);
	writechar(0x11);
	writechar(fill ? 0x02 : 0x01);
	writechar(on ? 0x01 : 0x00);
	writechar(StartX & 0xff);
	writechar((StartX >> 8) & 0xff);
	writechar(StartY & 0xff);
	writechar((StartY >> 8) & 0xff);
	writechar(EndX & 0xff);
	writechar((EndX >> 8) & 0xff);
	writechar(EndY & 0xff);
	writechar((EndY >> 8) & 0xff);
/*Macro_Box_End*/
}


void Bitmap(short width, short height, char length, char *bitmap)
{
/*Macro_Bitmap_Start*/
	writechar(0x1f);
	writechar(0x28);
	writechar(0x66);
	writechar(0x11);
	writechar(width & 0xff);
	writechar((width >> 8) & 0xff);
	writechar(height & 0xff);
	writechar((height >> 8) & 0xff);
	writechar(0x1);
	for (int i = 0; i < length; i++) {
		writechar(bitmap[i]);
	}
/*Macro_Bitmap_End*/
}


void Clear()
{
/*Macro_Clear_Start*/
	writechar(0x0c);
/*Macro_Clear_End*/
}


void FontSize(char size)
{
/*Macro_FontSize_Start*/
	writechar(0x1f);
	writechar(0x28);
	writechar(0x67);
	writechar(0x01);
	writechar(size);
/*Macro_FontSize_End*/
}


void FontMagnify(char ScaleX, char ScaleY)
{
/*Macro_FontMagnify_Start*/
	if (ScaleX < 1 || ScaleX > 4) {
		ScaleX = 4;
	}
	if (ScaleY < 1 || ScaleY > 2) {
		ScaleY = 2;
	}
	writechar(0x1f);
	writechar(0x28);
	writechar(0x67);
	writechar(0x40);
	writechar(ScaleX);
	writechar(ScaleY);
/*Macro_FontMagnify_End*/
}


