/***************************************************************************
                          BuiltinProgrammer.cpp  -  description
                             -------------------
    begin                : mar jan 31 2006
    copyright            : (C) 2006 by Gibaud Alain
    email                : alain.gibaud@free.fr
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include <klocale.h>
#include <qmessagebox.h>
#include <qstring.h>

#include "BuiltinProgrammer.h"
#include "PrgConfigWidget.h"
#include "PicFactory.hh"
#include "Config.h"

BuiltinProgrammer::BuiltinProgrammer( )
{
}

BuiltinProgrammer::~BuiltinProgrammer()
{
}


ConfigWidget *BuiltinProgrammer::configWidget()
{
    ConfigWidget *c = new PrgConfigWidget(&hard)  ;
    return c ;
}


QString BuiltinProgrammer::shortName()
{
    return "Builtin" ;
}

QString BuiltinProgrammer::longName()
{
    return i18n("Builtin PiKdev programmer") ;
}

QString BuiltinProgrammer::exit()
{
    if(thepic) thepic->setPort(0) ;
    hard.releasePort() ;
    return "" ;
}

QString BuiltinProgrammer::enter()
{
    QString r ;
    // read current config
    QString type, port ;
    int vpp, vdd, clock, dataout, datain, datarw, clkdelay ;
    Config::getCurrentPortConfig(type,port, vpp,vdd,clock,dataout,datain,datarw,clkdelay );


    // allocate port
    hard.setpins(type.latin1(), port.latin1() ,vpp,vdd,clock,dataout,datain,datarw,clkdelay );

    if(!hard.isok())
    {
        r = i18n("Cannot allocate %1 port %2.<br><br>"
               "Please&nbsp;use&nbsp;<b>Configure->Programmer</b>&nbsp;(or&nbsp;<b>CTRL+SHIFT+P</b>)<br>"
               "to setup your port configuration.").arg(type).arg(port)  ;
    }
    else
    {
      // choose a default pic device: it will be overwritten later
      // just before calling programming widget
      if( selectDevice("p16f628"))
            thepic->setPort(&hard) ;
      else
        r = i18n("Internal error in BuiltinProgrammer::enter()") ;
    }
  
  return r ;
}



bool  BuiltinProgrammer::selectDevice(const QString& devname)
{
    thepic = PicFactory::factory()->getPic(devname.latin1()) ;
    if( thepic ) thepic->setPort(&hard) ;
    return thepic != 0 ;
}

QStringList BuiltinProgrammer::devices()
{
    QString validprocs(PicFactory::factory()->getPicNames().c_str() ) ;
    return QStringList::split(QString(" "), validprocs) ;
}

bool BuiltinProgrammer::connectionOK()
{
  return hard.isok();
}


