/*********************************************************************
 *           Flowcode Seven Segment Display Component Code
 *
 * File: 7seg.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
 * -------+----+-----------------------------------------------------
 * 120607 | BR | Created
 * 160707 | ST | Modified to work with CTL file
 * 140309 | BR | Added Common Cathode / Common Anode Operation.
 * 210409 | BR | Added Clear Digit Function
 * 130509 | BR | Added % definitions to DefinesCode section
 * 030909 | BR | Added BoostC and HiTECH definitions
 * 110111 | SK | ClearDigit now only affects the common pin. Now matches component simulation.
 * 071011 | BR | Converted component C file to universal component code
 * 011211 | BR | Updated FC_CAL_Bit functions
 ********************************************************************/

/*********************************************************************

 Return & parameter types:
   void
   MX_UINT8
   MX_UINT16
   MX_STRING

 Pin directions:
   0 = OUTPUT
   2 = INPUT
   3 = BIDIRECTIONAL

**********************************************************************

[Settings]
CLSID={CBC38511-A335-4CDD-BBAC-AE067B64AAE2}
IsAnalogue=0
MultipleAllowed=1
Description=Seven Segment Display Flowcode Component

**********************************************************************

NOTE: For simulation to work, these macros _MUST_ remain at the indicated index:
	1=ShowDigit
	2=ClearDigit

[MacroNames]
Count=2
1=ShowDigit
2=ClearDigit

[MacroReturns]
1=void
2=void

[MacroIsPrivate]
1=0
2=0

[MacroParameters_ShowDigit]
Count=2
1=Value
2=DecimalPoint

[MacroParamTypes_ShowDigit]
1=MX_UINT8
2=MX_UINT8

[MacroParameters_ClearDigit]
Count=0

[MacroParamTypes_ClearDigit]

 ********************************************************************/

/********************************************************************
 * ADDITIONAL CODE
 ********************************************************************/

/*DefinesCode_Start*/

/**** Macro Substitutions ****
a = Unique Reference
b = Segment Port letter
c = Common Port letter
d = Segment Pin 0
e = Segment Pin 1
f = Segment Pin 2
g = Segment Pin 3
h = Segment Pin 4
i = Segment Pin 5
j = Segment Pin 6
k = Segment Pin 7
l = Common Pin
m = Display Type 0=Common Cathode, 1=Common Anode
******************************/

#define %a_MX_7SEG_SEG_PORT		port%b
#define %a_MX_7SEG_SEG_TRIS		tris%b
#define %a_MX_7SEG_COM_PORT		port%c
#define %a_MX_7SEG_COM_TRIS		tris%c
#define %a_MX_7SEG_SEG_PIN0		%d
#define %a_MX_7SEG_SEG_PIN1		%e
#define %a_MX_7SEG_SEG_PIN2		%f
#define %a_MX_7SEG_SEG_PIN3		%g
#define %a_MX_7SEG_SEG_PIN4		%h
#define %a_MX_7SEG_SEG_PIN5		%i
#define %a_MX_7SEG_SEG_PIN6		%j
#define %a_MX_7SEG_SEG_PIN7		%k
#define %a_MX_7SEG_COM_PIN		%l
#define %a_MX_7SEG_COM_TYPE		%m

#ifndef MX_7SEG_ARRAY
 #define MX_7SEG_ARRAY
 ROMARRAY_S SegmentArray ROMARRAY_E = {192, 249, 164, 176, 153, 146, 130, 248, 128, 152, 191, 136, 131, 198, 161, 134, 142, 137, 207, 241, 199, 200, 163, 140, 156, 193, 206, 255};		//Create ROM pattern array for the display
#endif

/*DefinesCode_End*/

/*InitialisationCode_Start*/
/*InitialisationCode_End*/

/*InterruptCode_Start*/
/*InterruptCode_End*/

/********************************************************************
 * FUNCTIONS
 ********************************************************************/

void ShowDigit(MX_UINT8 Value, MX_UINT8 DecimalPoint)
{
/*Macro_ShowDigit_Start*/

	MX_UINT8 cSegmentValue = SegmentArray[Value % 28];

	if (DecimalPoint)
		cSegmentValue = cSegmentValue & 0x7F;			//Clear decimal point bit

	//display the digit
    #if (%a_MX_7SEG_COM_TYPE == 1)						//Common Anode - Low = on, High = off

		if (cSegmentValue & 0x01)
			FC_CAL_Bit_High_DDR(%a_MX_7SEG_SEG_PORT, %a_MX_7SEG_SEG_TRIS, %a_MX_7SEG_SEG_PIN0);
		else FC_CAL_Bit_Low_DDR(%a_MX_7SEG_SEG_PORT, %a_MX_7SEG_SEG_TRIS, %a_MX_7SEG_SEG_PIN0);

		if (cSegmentValue & 0x02)
			FC_CAL_Bit_High_DDR(%a_MX_7SEG_SEG_PORT, %a_MX_7SEG_SEG_TRIS, %a_MX_7SEG_SEG_PIN1);
		else FC_CAL_Bit_Low_DDR(%a_MX_7SEG_SEG_PORT, %a_MX_7SEG_SEG_TRIS, %a_MX_7SEG_SEG_PIN1);

		if (cSegmentValue & 0x04)
			FC_CAL_Bit_High_DDR(%a_MX_7SEG_SEG_PORT, %a_MX_7SEG_SEG_TRIS, %a_MX_7SEG_SEG_PIN2);
		else FC_CAL_Bit_Low_DDR(%a_MX_7SEG_SEG_PORT, %a_MX_7SEG_SEG_TRIS, %a_MX_7SEG_SEG_PIN2);

		if (cSegmentValue & 0x08)
			FC_CAL_Bit_High_DDR(%a_MX_7SEG_SEG_PORT, %a_MX_7SEG_SEG_TRIS, %a_MX_7SEG_SEG_PIN3);
		else FC_CAL_Bit_Low_DDR(%a_MX_7SEG_SEG_PORT, %a_MX_7SEG_SEG_TRIS, %a_MX_7SEG_SEG_PIN3);

		if (cSegmentValue & 0x10)
			FC_CAL_Bit_High_DDR(%a_MX_7SEG_SEG_PORT, %a_MX_7SEG_SEG_TRIS, %a_MX_7SEG_SEG_PIN4);
		else FC_CAL_Bit_Low_DDR(%a_MX_7SEG_SEG_PORT, %a_MX_7SEG_SEG_TRIS, %a_MX_7SEG_SEG_PIN4);

		if (cSegmentValue & 0x20)
			FC_CAL_Bit_High_DDR(%a_MX_7SEG_SEG_PORT, %a_MX_7SEG_SEG_TRIS, %a_MX_7SEG_SEG_PIN5);
		else FC_CAL_Bit_Low_DDR(%a_MX_7SEG_SEG_PORT, %a_MX_7SEG_SEG_TRIS, %a_MX_7SEG_SEG_PIN5);

		if (cSegmentValue & 0x40)
			FC_CAL_Bit_High_DDR(%a_MX_7SEG_SEG_PORT, %a_MX_7SEG_SEG_TRIS, %a_MX_7SEG_SEG_PIN6);
		else FC_CAL_Bit_Low_DDR(%a_MX_7SEG_SEG_PORT, %a_MX_7SEG_SEG_TRIS, %a_MX_7SEG_SEG_PIN6);

		if (cSegmentValue & 0x80)
			FC_CAL_Bit_High_DDR(%a_MX_7SEG_SEG_PORT, %a_MX_7SEG_SEG_TRIS, %a_MX_7SEG_SEG_PIN7);
		else FC_CAL_Bit_Low_DDR(%a_MX_7SEG_SEG_PORT, %a_MX_7SEG_SEG_TRIS, %a_MX_7SEG_SEG_PIN7);

		FC_CAL_Bit_High_DDR(%a_MX_7SEG_COM_PORT, %a_MX_7SEG_COM_TRIS, %a_MX_7SEG_COM_PIN);

	#else												//Common Cathode - Low = off, High = on

		if (cSegmentValue & 0x01)
			FC_CAL_Bit_Low_DDR(%a_MX_7SEG_SEG_PORT, %a_MX_7SEG_SEG_TRIS, %a_MX_7SEG_SEG_PIN0);
		else FC_CAL_Bit_High_DDR(%a_MX_7SEG_SEG_PORT, %a_MX_7SEG_SEG_TRIS, %a_MX_7SEG_SEG_PIN0);

		if (cSegmentValue & 0x02)
			FC_CAL_Bit_Low_DDR(%a_MX_7SEG_SEG_PORT, %a_MX_7SEG_SEG_TRIS, %a_MX_7SEG_SEG_PIN1);
		else FC_CAL_Bit_High_DDR(%a_MX_7SEG_SEG_PORT, %a_MX_7SEG_SEG_TRIS, %a_MX_7SEG_SEG_PIN1);

		if (cSegmentValue & 0x04)
			FC_CAL_Bit_Low_DDR(%a_MX_7SEG_SEG_PORT, %a_MX_7SEG_SEG_TRIS, %a_MX_7SEG_SEG_PIN2);
		else FC_CAL_Bit_High_DDR(%a_MX_7SEG_SEG_PORT, %a_MX_7SEG_SEG_TRIS, %a_MX_7SEG_SEG_PIN2);

		if (cSegmentValue & 0x08)
			FC_CAL_Bit_Low_DDR(%a_MX_7SEG_SEG_PORT, %a_MX_7SEG_SEG_TRIS, %a_MX_7SEG_SEG_PIN3);
		else FC_CAL_Bit_High_DDR(%a_MX_7SEG_SEG_PORT, %a_MX_7SEG_SEG_TRIS, %a_MX_7SEG_SEG_PIN3);

		if (cSegmentValue & 0x10)
			FC_CAL_Bit_Low_DDR(%a_MX_7SEG_SEG_PORT, %a_MX_7SEG_SEG_TRIS, %a_MX_7SEG_SEG_PIN4);
		else FC_CAL_Bit_High_DDR(%a_MX_7SEG_SEG_PORT, %a_MX_7SEG_SEG_TRIS, %a_MX_7SEG_SEG_PIN4);

		if (cSegmentValue & 0x20)
			FC_CAL_Bit_Low_DDR(%a_MX_7SEG_SEG_PORT, %a_MX_7SEG_SEG_TRIS, %a_MX_7SEG_SEG_PIN5);
		else FC_CAL_Bit_High_DDR(%a_MX_7SEG_SEG_PORT, %a_MX_7SEG_SEG_TRIS, %a_MX_7SEG_SEG_PIN5);

		if (cSegmentValue & 0x40)
			FC_CAL_Bit_Low_DDR(%a_MX_7SEG_SEG_PORT, %a_MX_7SEG_SEG_TRIS, %a_MX_7SEG_SEG_PIN6);
		else FC_CAL_Bit_High_DDR(%a_MX_7SEG_SEG_PORT, %a_MX_7SEG_SEG_TRIS, %a_MX_7SEG_SEG_PIN6);

		if (cSegmentValue & 0x80)
			FC_CAL_Bit_Low_DDR(%a_MX_7SEG_SEG_PORT, %a_MX_7SEG_SEG_TRIS, %a_MX_7SEG_SEG_PIN7);
		else FC_CAL_Bit_High_DDR(%a_MX_7SEG_SEG_PORT, %a_MX_7SEG_SEG_TRIS, %a_MX_7SEG_SEG_PIN7);

		FC_CAL_Bit_Low_DDR(%a_MX_7SEG_COM_PORT, %a_MX_7SEG_COM_TRIS, %a_MX_7SEG_COM_PIN);

	#endif

/*Macro_ShowDigit_End*/
}


void ClearDigit (void)
{
/*Macro_ClearDigit_Start*/

	#if (%a_MX_7SEG_COM_TYPE == 1)
		FC_CAL_Bit_Low_DDR(%a_MX_7SEG_COM_PORT, %a_MX_7SEG_COM_TRIS, %a_MX_7SEG_COM_PIN);		//common anode
	#else
		FC_CAL_Bit_High_DDR(%a_MX_7SEG_COM_PORT, %a_MX_7SEG_COM_TRIS, %a_MX_7SEG_COM_PIN);		//common cathode
	#endif

/*Macro_ClearDigit_End*/
}