////////////////////////////////////////////////////////////////////////////////
//
// Code::Blocks new project wizard script
//
// Project: Atmel AVR project
// Author:  Brian Sidebotham
//
////////////////////////////////////////////////////////////////////////////////

// Global Vars
Processor <- _T("");     // The chosen processor
AvrHex <- false;         // produce Hex file from ELF output ?
AvrSrec <- false;              // produce Motorola S-Record files?
AvrBin <- false;               // produce Binary image files?
AvrLss <- false;         // produce extended list file from ELF output?
AvrMap <- false;         // produce Symbol Map file from ELF output?
AvrExtMem <- false;      // Locate .data in on-board SRAM, or external SRAM
AvrExtMemAddr <- ""      // The external memory address
AvrSize <- false;        // Run avr-size after the build 
AvrF_CPU <- false;       // Define F_CPU?
AvrF_CPUValue <- _T(""); // F_CPU textual value
 
function BeginWizard()
{
    local wiz_type = Wizard.GetWizardType();

    if (wiz_type == wizProject)
    {
        local intro_msg = _T("Welcome to the new Atmel AVR project wizard!\n" +
                             "This wizard will guide you to create a new Atmel AVR project.\n\n" +
                             "When you 're ready to proceed, please click \"Next\"...");

        Wizard.AddInfoPage(_T("AtmelAVRIntro"), intro_msg);
        Wizard.AddProjectPathPage();
        Wizard.AddCompilerPage(_T("GNU AVR GCC Compiler"), _T("avr*"), false, true);
        Wizard.AddPage(_T("processorChoice"));        
    }
    else
        print(wiz_type);
}

function GetFilesDir()
{
    local result = _T("avr/files");
    return result;
}

////////////////////////////////////////////////////////////////////////////////
// Processor choice page
////////////////////////////////////////////////////////////////////////////////


function OnLeave_processorChoice(fwd)
{
    if (fwd)
    {
        Processor = Wizard.GetComboboxStringSelection(_T("comboboxProc"));
        AvrHex = Wizard.IsCheckboxChecked(_T("checkboxHex"));
        AvrSrec = Wizard.IsCheckboxChecked(_T("checkboxSrec"));
        AvrBin = Wizard.IsCheckboxChecked(_T("checkboxBin"));
        AvrMap = Wizard.IsCheckboxChecked(_T("checkboxMap"));
        AvrLss = Wizard.IsCheckboxChecked(_T("checkboxLss"));
        AvrExtMem = Wizard.IsCheckboxChecked(_T("checkboxExtMem"));
        AvrExtMemAddr = Wizard.GetTextControlValue(_T("textctrlExtMem"));        
        AvrSize = Wizard.IsCheckboxChecked(_T("checkboxAvrSize"));
        AvrF_CPU = Wizard.IsCheckboxChecked(_T("checkboxF_CPU"));
        AvrF_CPUValue = Wizard.GetTextControlValue(_T("textctrlF_CPU"));
    }
    return true;
}

function SetupProject(project)
{   
    // Linker options 
        local lo_map = ::wxString();
        local lo_extmem = ::wxString();
    
    // Post Build steps
    local pb_avrsize = ::wxString();
    local pb_eephex = ::wxString();
    local pb_hex = ::wxString();
    local pb_eepbin = ::wxString();
    local pb_bin = ::wxString();
    local pb_eepsrec = ::wxString();
    local pb_srec = ::wxString();    
    local pb_lss = ::wxString();

        // Post build commands
        pb_eephex = _T("avr-objcopy --no-change-warnings -j .eeprom --change-section-lma .eeprom=0 -O ihex $(TARGET_OUTPUT_FILE) $(TARGET_OUTPUT_FILE).eep.hex");
        pb_hex = _T("avr-objcopy -O ihex -R .eeprom -R .eesafe $(TARGET_OUTPUT_FILE) $(TARGET_OUTPUT_FILE).hex");
        pb_eepbin = _T("avr-objcopy --no-change-warnings -j .eeprom --change-section-lma .eeprom=0 -O binary $(TARGET_OUTPUT_FILE) $(TARGET_OUTPUT_FILE).eep.bin");
        pb_bin = _T("avr-objcopy -O binary -R .eeprom -R .eesafe $(TARGET_OUTPUT_FILE) $(TARGET_OUTPUT_FILE).bin");
        pb_eepsrec = _T("avr-objcopy --no-change-warnings -j .eeprom --change-section-lma .eeprom=0 -O srec $(TARGET_OUTPUT_FILE) $(TARGET_OUTPUT_FILE).eep.srec");
        pb_srec = _T("avr-objcopy -O srec -R .eeprom -R .eesafe $(TARGET_OUTPUT_FILE) $(TARGET_OUTPUT_FILE).srec");

        if ( PLATFORM == PLATFORM_MSW )
            pb_lss = _T("cmd /c \"avr-objdump -h -S $(TARGET_OUTPUT_FILE) > $(TARGET_OUTPUT_FILE).lss\"");
        else
            pb_lss = _T("avr-objdump -h -S $(TARGET_OUTPUT_FILE) > $(TARGET_OUTPUT_FILE).lss");

        // avr-size is compiled with patches under winavr to produce a fancy output
        // which displays the percentage of memory used by the application for the 
        // target mcu. However, this option is not available under standard binutils
        // avr-size.
        if (PLATFORM_MSW == PLATFORM)
            pb_avrsize = _T("avr-size --mcu=") + Processor + _T(" --format=avr $(TARGET_OUTPUT_FILE)");
        else
            pb_avrsize = _T("avr-size $(TARGET_OUTPUT_FILE)");

        // Setup the linker options
        lo_map = _T("-Wl,-Map=$(TARGET_OUTPUT_FILE).map,--cref");
        
        // Get external memory start address
        lo_extmem = _T("-Wl,--section-start=.data=") + AvrExtMemAddr;
                
    // Project compiler options
    WarningsOn(project, Wizard.GetCompilerID());
        project.AddCompilerOption(_T("-mmcu=") + Processor);
        
        if (AvrF_CPU)
            project.AddCompilerOption(_T("-DF_CPU=") + AvrF_CPUValue);
            
        // Project linker options
        project.AddLinkerOption(_T("-mmcu=") + Processor);
        
        if (AvrMap)
            project.AddLinkerOption(lo_map);
        
        if (AvrExtMem)
            project.AddLinkerOption(lo_extmem);
        
        // Project post-build steps 
        if (AvrSize)
            project.AddCommandsAfterBuild(pb_avrsize);
                    
        if (AvrHex)
        {
            project.AddCommandsAfterBuild(pb_hex);
            project.AddCommandsAfterBuild(pb_eephex);
        }

        if (AvrSrec)
        {
            project.AddCommandsAfterBuild(pb_srec);
            project.AddCommandsAfterBuild(pb_eepsrec);
        }

        if (AvrBin)
        {
            project.AddCommandsAfterBuild(pb_bin);
            project.AddCommandsAfterBuild(pb_eepbin);
        }

        if ( AvrLss )
            project.AddCommandsAfterBuild(pb_lss);


    // Debug build target
    local target = project.GetBuildTarget(Wizard.GetDebugName());
    if (!IsNull(target))
    {
        target.SetTargetType(ttConsoleOnly);
        target.SetTargetFilenameGenerationPolicy(tgfpPlatformDefault, tgfpNone);
        target.SetOutputFilename(Wizard.GetDebugOutputDir() + Wizard.GetProjectName() + _T(".elf"));
        DebugSymbolsOn(target, Wizard.GetCompilerID());
    }

    // Release build target
    target = project.GetBuildTarget(Wizard.GetReleaseName());
    if (!IsNull(target))
        {
            target.SetTargetType(ttConsoleOnly);
            target.SetTargetFilenameGenerationPolicy(tgfpPlatformDefault, tgfpNone);
            target.SetOutputFilename(Wizard.GetReleaseOutputDir() + Wizard.GetProjectName() + _T(".elf"));
            OptimizationsOn(target, Wizard.GetCompilerID());
        }
        
    return true;
}
