Click here to Skip to main content
15,897,226 members
Articles / Desktop Programming / MFC

A PIC C Code Wizard

Rate me:
Please Sign up or sign in to vote.
4.94/5 (27 votes)
5 May 2003CPOL5 min read 156.3K   7.9K   46  
Creates C code templates for PIC microcontrollers. The default templates are for use with the Hi-Tech (tm) PICC compiler.
// PICWiz.cpp: implementation of the CPICWiz class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "PICWiz.h"

#include "TreePropSheet\\TreePropSheet.h"
#include "wizdlgs\\WizDlg1.h"
#include "wizdlgs\\adc.h"
#include "wizdlgs\\ccp1.h"
#include "wizdlgs\\ccp2.h"
#include "wizdlgs\\commi2c.h"
#include "wizdlgs\\commrs232.h"
#include "wizdlgs\\ioporta.h"
#include "wizdlgs\\ioportb.h"
#include "wizdlgs\\ioportc.h"
#include "wizdlgs\\ioportd.h"
#include "wizdlgs\\ioporte.h"
#include "wizdlgs\\timer1.h"
#include "wizdlgs\\timer2.h"
#include "wizdlgs\\timers.h"
#include "wizdlgs\\wizinfodlg.h"
#include "wizdlgs\\iogpio.h"
//#include "wizdlgs\\documentation.h"
//#include "wizdlgs\\checklist.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CPICWiz::CPICWiz()
{
}

CPICWiz::~CPICWiz()
{
}

void CPICWiz::Init() {
	TCHAR d[MAX_PATH];
	GetCurrentDirectory( MAX_PATH, d );
	m_sWizardFolder = d;
	_tcscpy( wizInfo.I2CSCLPort, _T("B") );
	_tcscpy( wizInfo.I2CSCLPin, _T("2") );
	_tcscpy( wizInfo.I2CSDAPort, _T("B") );
	_tcscpy( wizInfo.I2CSDAPin, _T("1") );
	wizInfo.pPICWiz = this;
	wizInfo.dwOSCFrequency = 4000000;
	wizInfo.bUseDelay = FALSE;
	wizInfo.bRS232BufferRound = FALSE;
	wizInfo.bDOCUseHTML = TRUE;
	wizInfo.bDOCUseHTMLHelp = FALSE;
	wizInfo.bDOCUseLaTeX = FALSE;
	wizInfo.bDOCUseRTF = FALSE;
	wizInfo.bHasGPIO = FALSE;
	wizInfo.bHasPortA = FALSE;
	wizInfo.bHasPortB = FALSE;
	wizInfo.bHasPortC = FALSE;
	wizInfo.bHasPortD = FALSE;
	wizInfo.bHasPortE = FALSE;
	wizInfo.sfr.ADCON0 = 0;
	wizInfo.sfr.ADCON1 = 0;
	wizInfo.sfr.CCP1CON = 0;
	wizInfo.sfr.CCP2CON = 0;
	wizInfo.sfr.EECON1 = 0;
	wizInfo.sfr.INTCON = 0;
	wizInfo.sfr.OPTION = 0xFF;
	wizInfo.sfr.PIE1 = 0;
	wizInfo.sfr.PIE2 = 0;
	wizInfo.sfr.PIR1 = 0;
	wizInfo.sfr.PIR2 = 0;
	wizInfo.sfr.PORTA = 0;
	wizInfo.sfr.PR2 = 0xFF;
	wizInfo.sfr.RCREG = 0;
	wizInfo.sfr.RCSTA = 0;
	wizInfo.sfr.SPBRG = 0;
	wizInfo.sfr.SSPADD = 0;
	wizInfo.sfr.SSPCON = 0;
	wizInfo.sfr.SSPCON2 = 0;
	wizInfo.sfr.SSPSTAT = 0;
	wizInfo.sfr.STATUS = 0x18;
	wizInfo.sfr.T1CON = 0;
	wizInfo.sfr.T2CON = 0;
	wizInfo.sfr.TMR2 = 0;
	wizInfo.sfr.TRIS = 0xFF;
	wizInfo.sfr.TRISA = 0xFF;
	wizInfo.sfr.TRISB = 0xFF;
	wizInfo.sfr.TRISC = 0xFF;
	wizInfo.sfr.TRISD = 0xFF;
	wizInfo.sfr.TRISE = 0x07;
	wizInfo.sfr.TXREG = 0;
	wizInfo.sfr.TXSTA = 0x02;
	wizInfo.sfr.CONFIG = 0x3FFF;
}

static BOOL CALLBACK MyEnumResNameProc( HANDLE hModule,
								  LPCTSTR lpszType,
								  LPTSTR lpszName,
								  LONG lParam )
{
	CFile f;
	if ( f.Open(lpszName,CFile::modeWrite|CFile::modeCreate) ) {
		HRSRC hResInfo;
		if ( (hResInfo=FindResource((HINSTANCE)hModule,lpszName,lpszType)) ) {
			HGLOBAL h;
			if ( (h=LoadResource((HINSTANCE)hModule,hResInfo)) ) {
				f.Write( h, SizeofResource((HINSTANCE)hModule,hResInfo) );
			}
		}
	}
	f.Close();
	return TRUE;
}

BOOL CPICWiz::CreateDefaultTemplatesFromResources() {
	return EnumResourceNames( m_hModule,
					_T("TEMPLATE"),
					(ENUMRESNAMEPROC)MyEnumResNameProc,
					(long)this );
}

BOOL CPICWiz::AskCreateDefaultTemplates() {
	CString s, s1;
	if ( m_bUserFolderCreated )
		return TRUE;
	s1.LoadString( IDS_CONFIGNOTFOUND );
	s.Format( s1, WIZ_FILE_PROJECT, WIZ_FILE_PICCONFIG, m_sTemplatesFolder );
	int iRet = AfxMessageBox( s, MB_YESNO|MB_ICONQUESTION );
	if ( IDYES == iRet )
		return TRUE;
	else
		return FALSE;
}

void CPICWiz::InitTemplatesFolder() {
	DWORD dwSize = 80;
#ifdef _DEBUG
	m_sTemplatesFolder.Format( _T("%s\\template_debug"), m_sWizardFolder );
#else
	m_sTemplatesFolder.Format( _T("%s\\template"), m_sWizardFolder );
#endif
	m_bUserFolderCreated = CreateDirectory( m_sTemplatesFolder, NULL );
	SetCurrentDirectory( m_sTemplatesFolder );
}

void CPICWiz::GetConfirmText( CString& sText ) {
	sText = m_sConfirmBuf;
}

PTCHAR CPICWiz::LoadTemplate( LPCTSTR lpszTemplateName ) {
	DWORD dwSize;
	CFile fo;
	PTCHAR Buf;
	if ( fo.Open(lpszTemplateName,CFile::modeRead) ) {
		dwSize = (DWORD)fo.GetLength();
		Buf = new TCHAR[dwSize+1];
		fo.Read( Buf, dwSize );
		fo.Close();
		Buf[dwSize] = NULL;
		return Buf;
	}
	else {
		return NULL;
	}
}

void CPICWiz::FileStatusInProject( CString& sFName ) {
	TCHAR c = sFName[0];
	switch( c ) {
	case '!': //exclude from build
		sFName.Remove( c );
		break;
	case '-': //do not add to the project
		sFName.Remove( c );
		break;
	case '*': //this is the DSP file
		sFName.Remove( c );
	case '^': //put under pic files
		sFName.Remove( c );
		break;
	}
}

BOOL CPICWiz::LoadProjectInfo() {
	CStdioFile f;
	PTCHAR Buf;
	CString sTemplateName;
	CString s;
	TCHAR seps[] = _T(" ,\t\n");
	if ( f.Open(WIZ_FILE_PROJECT,CFile::modeRead) ) {
		while ( f.ReadString(s) ) {
			if ( s.Find(_T("$$//"))==-1 && !s.IsEmpty() ) {
				sTemplateName = _tcstok( (LPTSTR)(LPCTSTR)s, seps );
				Buf = LoadTemplate( sTemplateName );
				if ( Buf ) {
					FileStatusInProject( sTemplateName );
					m_saTemplates.Add( _tcstok( NULL, seps) );
					m_saTemplatesBuf.Add( Buf );
					delete Buf;
				}
				else {
					return FALSE;
				}
			}
		}
		f.Close();
		Buf = LoadTemplate( WIZ_FILE_CONFIRM );
		if ( Buf ) {
			m_sConfirmBuf = Buf;
			delete Buf;
		}
		else {
			return FALSE;
		}
	}
	else {
		CString s;
		s.LoadString( IDS_PROJFILENOTFOUND );
		AfxMessageBox( s, MB_ICONERROR );
		return FALSE;
	}
	return TRUE;
}

void CPICWiz::GetWizardFolder( CString& sWF ) {
	sWF = m_sWizardFolder;
}

void CPICWiz::SetWizardFolder( LPCTSTR lpszPF ) {
	m_sWizardFolder = lpszPF;
}

void CPICWiz::GetWizardFolder( LPCTSTR& lpszWF ) {
	lpszWF = m_sWizardFolder;
}

void CPICWiz::GetProjectPath( CString& sPF ) {
	sPF = m_sProjectPath;
}

void CPICWiz::SetProjectPath( LPCTSTR lpszPF ) {
	m_sProjectPath = lpszPF;
	m_sProjectFolder = wizInfo.sProjectName;
	if ( m_sProjectFolder.IsEmpty() )
		return;
	CString s = _T("\\") + m_sProjectFolder + _T(".dsp");
	m_sProjectFolder = m_sProjectPath;
	m_sProjectFolder.Replace( s, _T("") );
}

BOOL CPICWiz::CreateProjectFiles() {
	CWaitCursor();
	if ( m_sProjectFolder.IsEmpty() )
		return FALSE;
	if ( !CreateDirectory(m_sProjectFolder,NULL) )
		return FALSE;
	if ( !SetCurrentDirectory(m_sProjectFolder) )
		return FALSE;
	CFile f;
	for ( int i=0;i<m_saTemplates.GetSize();i++ ) {
		if ( f.Open(m_saTemplates[i],CFile::modeCreate|CFile::modeWrite) ) {
			m_pList->AddTail( m_saTemplates[i] );
			f.Write( m_saTemplatesBuf[i], m_saTemplatesBuf[i].GetLength() );
			f.Close();
		}
		else {
			return FALSE;
		}
	}
	return TRUE;
}

BOOL CPICWiz::LoadWizardConfigFile() {
	CFileFind ff;

	if ( !ff.FindFile(WIZ_FILE_PROJECT) || !ff.FindFile(WIZ_FILE_PICCONFIG) ) {
		if ( !AskCreateDefaultTemplates() )
			return FALSE;
		else {
			if ( !CreateDefaultTemplatesFromResources() )
				return FALSE;
			else
				return LoadWizardConfigFile();
		}
	}
	return TRUE;
}

void CPICWiz::AddMacroFromDouble( LPCTSTR lpszMacro, double value ) {
	CString sMacro;

	sMacro.Format( _T("%f"), value );
	m_Dictionary[lpszMacro] = sMacro;
}

void CPICWiz::AddMacroFromInt( LPCTSTR lpszMacro, int value, LPCTSTR lpszFormat ) {
	CString sValue;

	sValue.Format( lpszFormat, value );
	m_Dictionary[lpszMacro] = sValue;
}

void CPICWiz::CreateAndFillMacros() {
	// TODO: create and assign values to your
	// macros.
	CString s;
	CTime t = CTime::GetCurrentTime();
	s = t.Format( _T("%A, %B %d, %Y") );
	m_Dictionary[_T("DATE")] = s;
	s = t.Format( _T("%Y") );
	m_Dictionary[_T("DATE_YEAR")] = s;
	m_Dictionary[_T("PROJNAME")] = wizInfo.sProjectName;
	m_Dictionary[_T("MICRO")] = wizInfo.sMicro;
	m_Dictionary[_T("CONFIG_FUSES")] = wizInfo.sConfig;
	AddMacroFromInt( _T("OSC_FREQ"), wizInfo.dwOSCFrequency, _T("%d") );
	// PIC's SFRs
	AddMacroFromInt( _T("GPIO"), wizInfo.sfr.GPIO, _T("0x%02X") );
	AddMacroFromInt( _T("TRIS"), wizInfo.sfr.GPIO, _T("0x%02X") );
	AddMacroFromInt( _T("PORTA"), wizInfo.sfr.PORTA, _T("0x%02X") );
	AddMacroFromInt( _T("PORTB"), wizInfo.sfr.PORTB, _T("0x%02X") );
	AddMacroFromInt( _T("PORTC"), wizInfo.sfr.PORTC, _T("0x%02X") );
	AddMacroFromInt( _T("PORTD"), wizInfo.sfr.PORTD, _T("0x%02X") );
	AddMacroFromInt( _T("PORTE"), wizInfo.sfr.PORTE, _T("0x%02X") );
	AddMacroFromInt( _T("TRISA"), wizInfo.sfr.TRISA, _T("0x%02X") );
	AddMacroFromInt( _T("TRISB"), wizInfo.sfr.TRISB, _T("0x%02X") );
	AddMacroFromInt( _T("TRISC"), wizInfo.sfr.TRISC, _T("0x%02X") );
	AddMacroFromInt( _T("TRISD"), wizInfo.sfr.TRISD, _T("0x%02X") );
	AddMacroFromInt( _T("TRISE"), wizInfo.sfr.TRISE, _T("0x%02X") );
	AddMacroFromInt( _T("ADCON0"), wizInfo.sfr.ADCON0, _T("0x%02X") );
	AddMacroFromInt( _T("ADCON1"), wizInfo.sfr.ADCON1, _T("0x%02X") );
	AddMacroFromInt( _T("ADRESH"), wizInfo.sfr.ADRESH, _T("0x%02X") );
	AddMacroFromInt( _T("ADRESL"), wizInfo.sfr.ADRESL, _T("0x%02X") );
	AddMacroFromInt( _T("ADRES"), wizInfo.sfr.ADRES, _T("0x%02X") );
	AddMacroFromInt( _T("CCP1CON"), wizInfo.sfr.CCP1CON, _T("0x%02X") );
	AddMacroFromInt( _T("CCP2CON"), wizInfo.sfr.CCP2CON, _T("0x%02X") );
	AddMacroFromInt( _T("CCPR1H"), wizInfo.sfr.CCPR1H, _T("0x%02X") );
	AddMacroFromInt( _T("CCPR1L"), wizInfo.sfr.CCPR1L, _T("0x%02X") );
	AddMacroFromInt( _T("CCPR2H"), wizInfo.sfr.CCPR2H, _T("0x%02X") );
	AddMacroFromInt( _T("CCPR2L"), wizInfo.sfr.CCPR2L, _T("0x%02X") );
	AddMacroFromInt( _T("CONFIG"), wizInfo.sfr.CONFIG, _T("%02X") );
	AddMacroFromInt( _T("EEADR"), wizInfo.sfr.EEADR, _T("0x%02X") );
	AddMacroFromInt( _T("EEADRH"), wizInfo.sfr.EEADRH, _T("0x%02X") );
	AddMacroFromInt( _T("EECON1"), wizInfo.sfr.EECON1, _T("0x%02X") );
	AddMacroFromInt( _T("EECON2"), wizInfo.sfr.EECON2, _T("0x%02X") );
	AddMacroFromInt( _T("EEDATA"), wizInfo.sfr.EEDATA, _T("0x%02X") );
	AddMacroFromInt( _T("EEDATH"), wizInfo.sfr.EEDATH, _T("0x%02X") );
	AddMacroFromInt( _T("INTCON"), wizInfo.sfr.INTCON, _T("0x%02X") );
	AddMacroFromInt( _T("OPTION"), wizInfo.sfr.OPTION, _T("0x%02X") );
	AddMacroFromInt( _T("OPTION_REG"), wizInfo.sfr.OPTION, _T("0x%02X") );
	AddMacroFromInt( _T("PCON"), wizInfo.sfr.PCON, _T("0x%02X") );
	AddMacroFromInt( _T("PIE1"), wizInfo.sfr.PIE1, _T("0x%02X") );
	AddMacroFromInt( _T("PIE2"), wizInfo.sfr.PIE2, _T("0x%02X") );
	AddMacroFromInt( _T("PIR1"), wizInfo.sfr.PIR1, _T("0x%02X") );
	AddMacroFromInt( _T("PIR2"), wizInfo.sfr.PIR2, _T("0x%02X") );
	AddMacroFromInt( _T("PR2"), wizInfo.sfr.PR2, _T("0x%02X") );
	AddMacroFromInt( _T("RCREG"), wizInfo.sfr.RCREG, _T("0x%02X") );
	AddMacroFromInt( _T("RCSTA"), wizInfo.sfr.RCSTA, _T("0x%02X") );
	AddMacroFromInt( _T("SPBRG"), wizInfo.sfr.SPBRG, _T("0x%02X") );
	AddMacroFromInt( _T("SSPADD"), wizInfo.sfr.SSPADD, _T("0x%02X") );
	AddMacroFromInt( _T("SSPCON"), wizInfo.sfr.SSPCON, _T("0x%02X") );
	AddMacroFromInt( _T("SSPCON2"), wizInfo.sfr.SSPCON2, _T("0x%02X") );
	AddMacroFromInt( _T("SSPSTAT"), wizInfo.sfr.SSPSTAT, _T("0x%02X") );
	AddMacroFromInt( _T("STATUS"), wizInfo.sfr.STATUS, _T("0x%02X") );
	AddMacroFromInt( _T("T1CON"), wizInfo.sfr.T1CON, _T("0x%02X") );
	AddMacroFromInt( _T("T2CON"), wizInfo.sfr.T2CON, _T("0x%02X") );
	AddMacroFromInt( _T("TMR0"), wizInfo.sfr.TMR0, _T("0x%02X") );
	AddMacroFromInt( _T("TMR1H"), wizInfo.sfr.TMR1H, _T("0x%02X") );
	AddMacroFromInt( _T("TMR1L"), wizInfo.sfr.TMR1L, _T("0x%02X") );
	AddMacroFromInt( _T("TMR2"), wizInfo.sfr.TMR2, _T("0x%02X") );
	AddMacroFromInt( _T("TXREG"), wizInfo.sfr.TXREG, _T("0x%02X") );
	AddMacroFromInt( _T("TXSTA"), wizInfo.sfr.TXSTA, _T("0x%02X") );
	// other macros
	int dc = wizInfo.sfr.CCPR1L<<2;
	dc |= (wizInfo.sfr.CCP1CON&0x30)>>4;
	AddMacroFromInt( _T("PWM1_DC"), dc, _T("%d") );
	dc = wizInfo.sfr.CCPR2L<<2;
	dc |= (wizInfo.sfr.CCP2CON&0x30)>>4;
	AddMacroFromInt( _T("PWM2_DC"), dc, _T("%d") );
	AddMacroFromInt( _T("TMR0_CNT_LIMIT"), wizInfo.dwT0Counter, _T("%d") );
	AddMacroFromInt( _T("TMR0_OFFSET"), wizInfo.dwT0Offset, _T("%d") );
	AddMacroFromInt( _T("TMR1_CNT_LIMIT"), wizInfo.dwT1Counter, _T("%d") );
	AddMacroFromInt( _T("TMR1_OFFSET"), wizInfo.dwT1Offset, _T("%d") );
	AddMacroFromInt( _T("TMR2_CNT_LIMIT"), wizInfo.dwT2Counter, _T("%d") );
	AddMacroFromInt( _T("TMR2_OFFSET"), wizInfo.dwT2Offset, _T("%d") );
	AddMacroFromInt( _T("RS232_BAUDRATE"), wizInfo.dwRS232Baudrate, _T("%u") );
	AddMacroFromInt( _T("RS232_BUFFER_SIZE")
		,wizInfo.dwRS232BufferSize
		,_T("%u") );
	AddMacroFromInt( _T("I2C_BUFFER_SIZE")
		,wizInfo.dwI2CBufferSize
		,_T("%u") );
	// documentation macros
	if ( wizInfo.bDOCUseHTML )
		m_Dictionary[_T("USE_HTML")] = _T("YES");
	else
		m_Dictionary[_T("USE_HTML")] = _T("NO");
	if ( wizInfo.bDOCUseHTMLHelp ) {
		CString s;
		s = "\"";
		s += wizInfo.sHHCPath;
		s += "\"";
		m_Dictionary[_T("HHC_PATH")] = s;
		m_Dictionary[_T("USE_HTML_HELP")] = _T("YES");
	}
	else
		m_Dictionary[_T("USE_HTML_HELP")] = _T("NO");
	if ( wizInfo.bDOCUseLaTeX )
		m_Dictionary[_T("USE_LATEX")] = _T("YES");
	else
		m_Dictionary[_T("USE_LATEX")] = _T("NO");
	if ( wizInfo.bDOCUseRTF )
		m_Dictionary[_T("USE_RTF")] = _T("YES");
	else
		m_Dictionary[_T("USE_RTF")] = _T("NO");
	// ADC signals macros
	if ( wizInfo.sfr.ADCON0&0x01 ) {
		m_Dictionary[_T("USE_ADC")] = _T("YES");
		int n = 0;
		for ( int i=0;i<16;i++ ) {
			if ( wizInfo.sADCLabels[i][0] && wizInfo.sADCChannels[i][0] ) {
				CString s, s1;
				s.Format( _T("ADC_SIGNALS_DEF_%d"), n );
				s1.Format( _T("ADC_%s_%s"),
					wizInfo.sADCChannels[n],
					wizInfo.sADCLabels[n] );
				m_Dictionary[s] = s1;
				s.Format( _T("ADC_CHANNELS_%d"), n );
				s1.Format( _T("ADC_%s"),
					wizInfo.sADCChannels[n] );
				m_Dictionary[s] = s1;
				s.Format( _T("ADC_I_%d"), n );
				s1.Format( _T("%d"), n );
				m_Dictionary[s] = s1;
				n++;
			}
		}
		AddMacroFromInt( _T("ADC_SIGNALS"), n, _T("%d") );
		AddMacroFromInt( _T("ADC_CHANNELS"), n, _T("%d") );
		AddMacroFromInt( _T("ADC_SIGNALS_DEF"), n, _T("%d") );
		AddMacroFromInt( _T("ADC_I"), n, _T("%d") );
	}
	if ( wizInfo.sfr.OPTION&0x40 )
		m_Dictionary[_T("INT_EXT_EDGE")] = _T("INT_EXT_EDGE_ON_RISING");
	else
		m_Dictionary[_T("INT_EXT_EDGE")] = _T("INT_EXT_EDGE_ON_FALLING");
	// port macros
	if ( wizInfo.bHasGPIO ) {
		m_Dictionary[_T("HAS_GPIO")] = _T("YES");
	}
	if ( wizInfo.bHasPortA ) {
		m_Dictionary[_T("HAS_PORTA")] = _T("YES");
	}
	if ( wizInfo.bHasPortB ) {
		m_Dictionary[_T("HAS_PORTB")] = _T("YES");
	}
	if ( wizInfo.bHasPortC ) {
		m_Dictionary[_T("HAS_PORTC")] = _T("YES");
	}
	if ( wizInfo.bHasPortD ) {
		m_Dictionary[_T("HAS_PORTD")] = _T("YES");
	}
	if ( wizInfo.bHasPortE ) {
		m_Dictionary[_T("HAS_PORTE")] = _T("YES");
	}
	// boolean macros
	if ( wizInfo.bUseI2CSoftware ) {
		m_Dictionary[_T("USE_I2C_SOFTWARE")] = _T("YES");
		CString s1, s2, s3, s4;
		s1 = wizInfo.I2CSCLPort;
		s2 = wizInfo.I2CSDAPort;
#ifdef PICANT
		s1.MakeLower();
		s2.MakeLower();
#elif PICCWIZ
		s1 += wizInfo.I2CSCLPin;
		s2 += wizInfo.I2CSDAPin;
		s3 = _T("TRIS");
		s4 = _T("R");
#endif
		m_Dictionary[_T("I2C_SCL_IO")] = s3 + s1;
		m_Dictionary[_T("I2C_SCL_PIN")] = s4 + s1;
		m_Dictionary[_T("I2C_SDA_IO")] = s3 + s2;
		m_Dictionary[_T("I2C_SDA_PIN")] = s4 + s2;
	}
	if ( wizInfo.bUseI2CFirmware )
		m_Dictionary[_T("USE_I2C_FIRMWARE")] = _T("YES");
	if ( wizInfo.bUseI2CHardware )
		m_Dictionary[_T("USE_I2C_HARDWARE")] = _T("YES");
	if ( wizInfo.bUseCommentOnSource )
		m_Dictionary[_T("USE_COMMENTS")] = _T("YES");
	if ( wizInfo.bUseSleepFunctions )
		m_Dictionary[_T("USE_SLEEP")] = _T("YES");
	if ( wizInfo.bUseEEPROMIFunctions )
		m_Dictionary[_T("USE_EEPROMI")] = _T("YES");
	if ( wizInfo.bIsFlash )
		m_Dictionary[_T("FLASH")] = _T("YES");
	// interrupts
	BOOL bUseInterrupts = FALSE;
	if ( wizInfo.sfr.INTCON&0x08 ) {
		m_Dictionary[_T("USE_INT_PORTB")] = _T("YES");
		bUseInterrupts = TRUE;
	}
	if ( wizInfo.sfr.INTCON&0x10 ) {
		m_Dictionary[_T("USE_INT_EXT")] = _T("YES");
		bUseInterrupts = TRUE;
	}
	if ( wizInfo.sfr.PIE1&0x20 ) {
		m_Dictionary[_T("USE_RS232_INT")] = _T("YES");
		bUseInterrupts = TRUE;
	}
	if ( wizInfo.sfr.CCP1CON&0x0F )
		m_Dictionary[_T("USE_CCP1")] = _T("YES");
	if ( wizInfo.sfr.CCP2CON&0x0F )
		m_Dictionary[_T("USE_CCP2")] = _T("YES");
	if ( wizInfo.sfr.PIE1&0x04 ) {
		m_Dictionary[_T("USE_INT_CCP1")] = _T("YES");
		bUseInterrupts = TRUE;
	}
	if ( wizInfo.sfr.PIE2&0x01 ) {
		m_Dictionary[_T("USE_INT_CCP2")] = _T("YES");
		bUseInterrupts = TRUE;
	}
	if ( wizInfo.sfr.RCSTA&0x80 )
		m_Dictionary[_T("USE_RS232")] = _T("YES");
	if ( wizInfo.sfr.INTCON&0x20 ) {
		m_Dictionary[_T("USE_TMR0")] = _T("YES");
		bUseInterrupts = TRUE;
	}
	if ( wizInfo.sfr.PIE1&0x40 ) {
		m_Dictionary[_T("USE_ADC_INT")] = _T("YES");
		wizInfo.bUseDelay = TRUE;
		bUseInterrupts = TRUE;
	}
	if ( wizInfo.bUseDelay ) {
		m_Dictionary[_T("USE_DELAY")] = _T("YES");
		int v = wizInfo.dwDelay1 & MAXWORD;
		AddMacroFromInt( _T("DELAY_MS"), v, _T("%u") );
	}

	if ( wizInfo.sfr.PIE1&0x01 ) {
		m_Dictionary[_T("USE_TMR1")] = _T("YES");
		bUseInterrupts = TRUE;
	}
	if ( wizInfo.bUseT2Interrupt ) {
		m_Dictionary[_T("USE_TMR2")] = _T("YES");
		bUseInterrupts = TRUE;
	}
	if ( (wizInfo.sfr.CCP1CON&0x0C) == 0x0C ) {
		m_Dictionary[_T("USE_PWM")] = _T("YES");
		m_Dictionary[_T("USE_PWM1")] = _T("YES");
	}
	if ( (wizInfo.sfr.CCP2CON&0x0C) == 0x0C ) {
		m_Dictionary[_T("USE_PWM")] = _T("YES");
		m_Dictionary[_T("USE_PWM2")] = _T("YES");
	}
	if ( wizInfo.sfr.PIE1&0x10 ) {
		m_Dictionary[_T("USE_INT_TX")] = _T("YES");
		bUseInterrupts = TRUE;
	}
	if ( bUseInterrupts )
		m_Dictionary[_T("USE_INTERRUPTS")] = _T("YES");
	if ( wizInfo.bRS232BufferRound )
		m_Dictionary[_T("RS232_BUFFER_ROUND")] = _T("YES");
	else
		m_Dictionary[_T("RS232_BUFFER_TXT")] = _T("YES");
}


void CPICWiz::ProcessTemplates() {
	CString sMacro;
	CString sValue;
	CString sTmp;
	// Create the macros from the data collected through
	// the wizard's GUI
	CreateAndFillMacros();
	for ( POSITION pos=m_Dictionary.GetStartPosition();pos!=NULL; ) {
		m_Dictionary.GetNextAssoc( pos, sMacro, sValue );
		sTmp.Format( _T("$$%s$$"), sMacro );
		sMacro = sTmp;
		for ( int i=0;i<m_saTemplatesBuf.GetSize();i++ ) {
			TRACE(_T("%s\n"), m_saTemplates[i]);
			int pos_if;
			while ( (pos_if=m_saTemplatesBuf[i].Find(_T("$$IF")))!= -1 ) {
				int pos_endif = pos_if;
				int if_counter = 0;
				int t_endif = 0;
				int t_if = pos_if;
				do {
					t_endif = m_saTemplatesBuf[i].Find( _T("$$ENDIF"), pos_endif+3 );
					t_if = m_saTemplatesBuf[i].Find( _T("$$IF"), t_if+3 );
					if_counter++;
					if ( t_endif != -1 )
						pos_endif = t_endif;
				}while( t_endif>t_if && t_endif!=-1 && t_if!=-1 );
				CString s;
				int p1 = m_saTemplatesBuf[i].Find( _T("("), pos_if );
				int p2 = m_saTemplatesBuf[i].Find( _T(")"), pos_if );
				if ( p1!=-1 && p2!=-1 ) {
					s = m_saTemplatesBuf[i].Mid( p1+1, p2-p1-1 );
					if ( pos_endif != -1 ) {
						if ( m_Dictionary.Lookup(s,sTmp) ) {
							m_saTemplatesBuf[i].Delete( pos_endif, 9 );
							m_saTemplatesBuf[i].Delete( pos_if, p2-pos_if+3 );
						}
						else {
							m_saTemplatesBuf[i].Delete( pos_if, pos_endif-pos_if+9 );
						}
					}
				}
				else { // error: open parenthesis
				}
			}
			int pos_beginloop;
			while ( (pos_beginloop=m_saTemplatesBuf[i].Find(_T("$$BEGINLOOP")))!= -1 ) {
				int pos_endloop = m_saTemplatesBuf[i].Find( _T("$$ENDLOOP"), pos_beginloop+9 );
				int p1 = m_saTemplatesBuf[i].Find( _T("("), pos_beginloop );
				int p2 = m_saTemplatesBuf[i].Find( _T(")"), pos_beginloop );
				if ( p1!=-1 && p2!=-1 ) {
					CString s1 = m_saTemplatesBuf[i].Mid( p1+1, p2-p1-1 );
					if ( pos_endloop != -1 ) {
						if ( m_Dictionary.Lookup(s1,sTmp) ) {
							CString sBeginblk = m_saTemplatesBuf[i].Mid( p2+3, pos_endloop-p2-3 );
							m_saTemplatesBuf[i].Delete( pos_beginloop, pos_endloop-pos_beginloop+11 );
							// look for macro within beginloop
							CStringArray saNMacros;
							int nmacros = 0;
							p1 = 0;
							do {
								p1 = sBeginblk.Find( _T("$$"), p1 );
								p2 = sBeginblk.Find( _T("$$"), p1+1 );
								if ( p1!=-1 && p2!=-1 ) {
									nmacros++;
									saNMacros.Add( sBeginblk.Mid(p1+2,p2-p1-2) );
									p1 = p2 + 1;
								}
							}while ( p1!=-1 && p2!=-1 );
							CString sNew;
							int n = _ttoi( (LPCSTR)(LPCTSTR)sTmp );
							for ( int k=0;k<n;k++ ) {
								sTmp = sBeginblk;
								for ( p1=0;p1<nmacros;p1++ ) {
									CString ss;
									ss.Format( _T("%s_%d"), saNMacros[p1], k );
									TRACE1( "%s\n", saNMacros[p1] );
									sTmp.Replace( saNMacros[p1], ss );
								}
								sNew += sTmp;
							}
							m_saTemplatesBuf[i].Insert( pos_beginloop, sNew );
						}
						else {
							m_saTemplatesBuf[i].Delete( pos_beginloop, pos_endloop-pos_beginloop+11 );
						}
					}
				}
				else { // error: open parenthesis
				}
			}
			/*while ( (pos_beginloop=m_saTemplatesBuf[i].Find(_T("$$BEGINLOOP")))!= -1 ) {
				CString s1;
				CString s2;
				int pos_endloop = m_saTemplatesBuf[i].Find( _T("$$ENDLOOP"), pos_beginloop+9 );
				int p1 = m_saTemplatesBuf[i].Find( _T("("), pos_beginloop );
				int p2 = m_saTemplatesBuf[i].Find( _T(")"), pos_beginloop );
				if ( p1!=-1 && p2!=-1 ) {
					sTmp = m_saTemplatesBuf[i].Mid( p1+1, p2-p1-1 );
					int pc = sTmp.Find( _T(",") );
					if ( pc != -1 ) {
						s1 = sTmp.Mid( 0, pc );
						s2 = sTmp.Mid( pc+1, sTmp.GetLength()-pc );
					}
					else {
						s1 = sTmp;
					}
					if ( pos_endloop != -1 ) {
						if ( m_Dictionary.Lookup(s1,sTmp) ) {
							CString snew;
							CString sbetween = m_saTemplatesBuf[i].Mid( p2+3, pos_endloop-p2-3 );
							m_saTemplatesBuf[i].Delete( pos_beginloop, pos_endloop-pos_beginloop+11 );
							int n = _ttoi( (LPCSTR)(LPCTSTR)sTmp );
							for ( int k=0;k<n;k++ ) {
								CString ss;
								ss.Format( _T("%s_%d"), s1, k );
								sTmp = sbetween;
								sTmp.Replace( s1, ss );
								if ( !s2.IsEmpty() ) {
									ss.Format( _T("%s_%d"), s2, k );
									sTmp.Replace( s2, ss );
								}
								snew += sTmp;
							}
							m_saTemplatesBuf[i].Insert( pos_beginloop, snew );
						}
						else {
							m_saTemplatesBuf[i].Delete( pos_beginloop, pos_endloop-pos_beginloop+11 );
						}
					}
				}
				else { // error: open parenthesis
				}
			}*/
			// replace macros in file names
			m_saTemplates[i].Replace( sMacro, sValue );
			// replace macros in file buffers
			m_saTemplatesBuf[i].Replace( sMacro, sValue );
		}
		// confirm text
		m_sConfirmBuf.Replace( sMacro, sValue );
	}
}

void CPICWiz::EnableWindow( CWnd* pWnd, BOOL bEnable ) {
	while ( pWnd ) {
		pWnd->EnableWindow( bEnable );
		pWnd = pWnd->GetWindow( GW_HWNDNEXT );
	}
}

BOOL CPICWiz::Run( HINSTANCE hModule
				  ,CList<CString,CString>* list
				  ,CWnd* p /*=NULL*/ ) {
	m_pList = list;
	m_hModule = hModule;
	if ( NULL == m_hModule )
		m_hModule = GetModuleHandle( _T("wizard") );
	// set images
	CImageList	DefaultImages, Images;
	DefaultImages.Create(IDB_DEFAULT, 16, 0, RGB(0x00, 0x80, 0x80));
	Images.Create(IDB_IMAGES, 16, 0, RGB(0x00, 0x80, 0x80));

	// declare and create the only property (wizard) page
	CString s;
	s.LoadString( IDS_PICWIZARD );
	TreePropSheet::CTreePropSheet wizardWnd( IDS_PICWIZARD );
	// declare and add the pages (wizard dialogs) to the wizard page
	// TODO: add as many are necessary to implement your wizard
	CWizDlg1 wndPage1;
	CADC wndADC;
	CCCP1 wndCCP1;
	CCCP2 wndCCP2;
	CCommI2C wndCommI2C;
	CCommRS232 wndCommRS232;
	CIOGPIO wndIOGPIO;
	CIOPortA wndIOPortA;
	CIOPortB wndIOPortB;
	CIOPortC wndIOPortC;
	CIOPortD wndIOPortD;
	CIOPortE wndIOPortE;
	CTimer1	wndTimer1;
	CTimer2	wndTimer2;
	CTimers	wndTimer0;
	//CChecklist wndChecklist;
	//CDocumentation wndDocumentation;
	wizardWnd.AddPage( &wndPage1 );
	wizardWnd.AddPage( &wndADC );
	wizardWnd.AddPage( &wndCCP1 );
	wizardWnd.AddPage( &wndCCP2 );
	wizardWnd.AddPage( &wndCommI2C );
	wizardWnd.AddPage( &wndCommRS232 );
	wizardWnd.AddPage( &wndIOGPIO );
	wizardWnd.AddPage( &wndIOPortA );
	wizardWnd.AddPage( &wndIOPortB );
	wizardWnd.AddPage( &wndIOPortC );
	wizardWnd.AddPage( &wndIOPortD );
	wizardWnd.AddPage( &wndIOPortE );
	wizardWnd.AddPage( &wndTimer0 );
	wizardWnd.AddPage( &wndTimer1 );
	wizardWnd.AddPage( &wndTimer2 );
	//wizardWnd.AddPage( &wndDocumentation );
	//wizardWnd.AddPage( &wndChecklist );

	s.LoadString( IDS_EMPTYPAGETEXT );
	wizardWnd.SetEmptyPageText( s );
	wizardWnd.SetTreeViewMode( TRUE, TRUE, TRUE );
	wizardWnd.SetTreeDefaultImages( &DefaultImages );
	wizardWnd.SetActivePage( &wndPage1 );

	Init();
	// make sure the user's folder exists
	InitTemplatesFolder();
	// TODO: determine if the configuration files exist under
	// the user's folder.
	if (LoadWizardConfigFile()) {
		if (IDOK == wizardWnd.DoModal())
			return TRUE;
	}

	return FALSE;
}

WIZARDINFO wizInfo;

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Delphi
United States United States
Carlos Buelna works in the automotive industry.

Comments and Discussions