/*********************************************************************
 *           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

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

[Port]
DesiredPort=1
PortMandatory=1
SamePortAsID=-1

[Pins]
Count=0
DesiredPin=0
BitMandatory=1

[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)

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

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 = SegmentA Port Letter
c = SegmentB Port Letter
d = SegmentC Port Letter
e = SegmentD Port Letter
f = SegmentE Port Letter
g = SegmentF Port Letter
h = SegmentG Port Letter
i = SegmentDP Port letter
j = Common Port letter
k = SegmentA Pin 0
l = SegmentB Pin 1
m = SegmentC Pin 2
n = SegmentD Pin 3
o = SegmentE Pin 4
p = SegmentF Pin 5
q = SegmentG Pin 6
r = SegmentDP Pin 7
s = Common Pin
t = Display Type 0=Common Cathode, 1=Common Anode
******************************/
#define %a_MX_7SEG_SEG_PORT0   		port%b
#define %a_MX_7SEG_SEG_PORT1   		port%c
#define %a_MX_7SEG_SEG_PORT2 		port%d
#define %a_MX_7SEG_SEG_PORT3 		port%e
#define %a_MX_7SEG_SEG_PORT4 		port%f
#define %a_MX_7SEG_SEG_PORT5   		port%g
#define %a_MX_7SEG_SEG_PORT6   		port%h
#define %a_MX_7SEG_SEG_PORT7   		port%i
#define %a_MX_7SEG_COM_PORT		port%j
#define %a_MX_7SEG_SEG_TRIS0  		tris%b
#define %a_MX_7SEG_SEG_TRIS1    	tris%c
#define %a_MX_7SEG_SEG_TRIS2    	tris%d
#define %a_MX_7SEG_SEG_TRIS3    	tris%e
#define %a_MX_7SEG_SEG_TRIS4		tris%f
#define %a_MX_7SEG_SEG_TRIS5 		tris%g
#define %a_MX_7SEG_SEG_TRIS6 		tris%h
#define %a_MX_7SEG_SEG_TRIS7 		tris%i
#define %a_MX_7SEG_COM_TRIS		tris%j
#define %a_MX_7SEG_SEG_PIN0		%k
#define %a_MX_7SEG_SEG_PIN1		%l
#define %a_MX_7SEG_SEG_PIN2		%m
#define %a_MX_7SEG_SEG_PIN3		%n
#define %a_MX_7SEG_SEG_PIN4		%o
#define %a_MX_7SEG_SEG_PIN5		%p
#define %a_MX_7SEG_SEG_PIN6		%q
#define %a_MX_7SEG_SEG_PIN7		%r
#define %a_MX_7SEG_COM_PIN		%s
#define %a_MX_7SEG_COM_TYPE		%t

#ifndef MX_7SEG_ARRAY
 #define MX_7SEG_ARRAY
 ROMARRAY_S SegmentArray ROMARRAY_E = {192, 249, 164, 176, 153, 146, 130, 248, 128, 152};		//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 % 10];

	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_PORT0, %a_MX_7SEG_SEG_TRIS0, %a_MX_7SEG_SEG_PIN0);
		else FC_CAL_Bit_Low_DDR(%a_MX_7SEG_SEG_PORT0, %a_MX_7SEG_SEG_TRIS0, %a_MX_7SEG_SEG_PIN0);

		if (cSegmentValue & 0x02)
			FC_CAL_Bit_High_DDR(%a_MX_7SEG_SEG_PORT1, %a_MX_7SEG_SEG_TRIS1, %a_MX_7SEG_SEG_PIN1);
		else FC_CAL_Bit_Low_DDR(%a_MX_7SEG_SEG_PORT1, %a_MX_7SEG_SEG_TRIS1, %a_MX_7SEG_SEG_PIN1);

		if (cSegmentValue & 0x04)
			FC_CAL_Bit_High_DDR(%a_MX_7SEG_SEG_PORT2, %a_MX_7SEG_SEG_TRIS2, %a_MX_7SEG_SEG_PIN2);
		else FC_CAL_Bit_Low_DDR(%a_MX_7SEG_SEG_PORT2, %a_MX_7SEG_SEG_TRIS2, %a_MX_7SEG_SEG_PIN2);

		if (cSegmentValue & 0x08)
			FC_CAL_Bit_High_DDR(%a_MX_7SEG_SEG_PORT3, %a_MX_7SEG_SEG_TRIS3, %a_MX_7SEG_SEG_PIN3);
		else FC_CAL_Bit_Low_DDR(%a_MX_7SEG_SEG_PORT3, %a_MX_7SEG_SEG_TRIS3, %a_MX_7SEG_SEG_PIN3);

		if (cSegmentValue & 0x10)
			FC_CAL_Bit_High_DDR(%a_MX_7SEG_SEG_PORT4, %a_MX_7SEG_SEG_TRIS4, %a_MX_7SEG_SEG_PIN4);
		else FC_CAL_Bit_Low_DDR(%a_MX_7SEG_SEG_PORT4, %a_MX_7SEG_SEG_TRIS4, %a_MX_7SEG_SEG_PIN4);

		if (cSegmentValue & 0x20)
			FC_CAL_Bit_High_DDR(%a_MX_7SEG_SEG_PORT5, %a_MX_7SEG_SEG_TRIS5, %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_PORT6, %a_MX_7SEG_SEG_TRIS6, %a_MX_7SEG_SEG_PIN6);
		else FC_CAL_Bit_Low_DDR(%a_MX_7SEG_SEG_PORT6, %a_MX_7SEG_SEG_TRIS6, %a_MX_7SEG_SEG_PIN6);

		if (cSegmentValue & 0x80)
			FC_CAL_Bit_High_DDR(%a_MX_7SEG_SEG_PORT7, %a_MX_7SEG_SEG_TRIS7, %a_MX_7SEG_SEG_PIN7);
		else FC_CAL_Bit_Low_DDR(%a_MX_7SEG_SEG_PORT7, %a_MX_7SEG_SEG_TRIS7, %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_PORT0, %a_MX_7SEG_SEG_TRIS0, %a_MX_7SEG_SEG_PIN0);
		else FC_CAL_Bit_High_DDR(%a_MX_7SEG_SEG_PORT0, %a_MX_7SEG_SEG_TRIS0, %a_MX_7SEG_SEG_PIN0);

		if (cSegmentValue & 0x02)
			FC_CAL_Bit_Low_DDR(%a_MX_7SEG_SEG_PORT1, %a_MX_7SEG_SEG_TRIS1, %a_MX_7SEG_SEG_PIN1);
		else FC_CAL_Bit_High_DDR(%a_MX_7SEG_SEG_PORT1, %a_MX_7SEG_SEG_TRIS1, %a_MX_7SEG_SEG_PIN1);

		if (cSegmentValue & 0x04)
			FC_CAL_Bit_Low_DDR(%a_MX_7SEG_SEG_PORT2, %a_MX_7SEG_SEG_TRIS2, %a_MX_7SEG_SEG_PIN2);
		else FC_CAL_Bit_High_DDR(%a_MX_7SEG_SEG_PORT2, %a_MX_7SEG_SEG_TRIS2, %a_MX_7SEG_SEG_PIN2);

		if (cSegmentValue & 0x08)
			FC_CAL_Bit_Low_DDR(%a_MX_7SEG_SEG_PORT3, %a_MX_7SEG_SEG_TRIS3, %a_MX_7SEG_SEG_PIN3);
		else FC_CAL_Bit_High_DDR(%a_MX_7SEG_SEG_PORT3, %a_MX_7SEG_SEG_TRIS3, %a_MX_7SEG_SEG_PIN3);

		if (cSegmentValue & 0x10)
			FC_CAL_Bit_Low_DDR(%a_MX_7SEG_SEG_PORT4, %a_MX_7SEG_SEG_TRIS4, %a_MX_7SEG_SEG_PIN4);
		else FC_CAL_Bit_High_DDR(%a_MX_7SEG_SEG_PORT4, %a_MX_7SEG_SEG_TRIS4, %a_MX_7SEG_SEG_PIN4);

		if (cSegmentValue & 0x20)
			FC_CAL_Bit_Low_DDR(%a_MX_7SEG_SEG_PORT5, %a_MX_7SEG_SEG_TRIS5, %a_MX_7SEG_SEG_PIN5);
		else FC_CAL_Bit_High_DDR(%a_MX_7SEG_SEG_PORT5, %a_MX_7SEG_SEG_TRIS5, %a_MX_7SEG_SEG_PIN5);

		if (cSegmentValue & 0x40)
			FC_CAL_Bit_Low_DDR(%a_MX_7SEG_SEG_PORT6, %a_MX_7SEG_SEG_TRIS6, %a_MX_7SEG_SEG_PIN6);
		else FC_CAL_Bit_High_DDR(%a_MX_7SEG_SEG_PORT6, %a_MX_7SEG_SEG_TRIS6, %a_MX_7SEG_SEG_PIN6);

		if (cSegmentValue & 0x80)
			FC_CAL_Bit_Low_DDR(%a_MX_7SEG_SEG_PORT7, %a_MX_7SEG_SEG_TRIS7, %a_MX_7SEG_SEG_PIN7);
		else FC_CAL_Bit_High_DDR(%a_MX_7SEG_SEG_PORT7, %a_MX_7SEG_SEG_TRIS7, %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*/
}