//---------------------------------------------------------------------------

#ifndef BLDRH
#define BLDRH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ComCtrls.hpp>
#include <ExtCtrls.hpp>
#include <Dialogs.hpp>
//---------------------------------------------------------------------------
class TBootloader : public TForm
{
__published:	// IDE-managed Components
        TMemo *Memo;
        TProgressBar *ProgressBar;
        TButton *BVer;
        TButton *BHex;
        TButton *BConnect;
        TButton *BAll;
        TButton *BRun;
        TOpenDialog *OpenDialog;
        TCheckBox *CBUnlock;
        void __fastcall BConnectClick(TObject *Sender);
        void __fastcall FormCreate(TObject *Sender);
        void __fastcall FormDestroy(TObject *Sender);
        void __fastcall BVerClick(TObject *Sender);
        void __fastcall BHexClick(TObject *Sender);
        void __fastcall BRunClick(TObject *Sender);
        void __fastcall FormClose(TObject *Sender, TCloseAction &Action);
        void __fastcall BAllClick(TObject *Sender);
private:	// User declarations
        CUSBDevice *USBPORT;
        bool Connected;

        bool HexLoad;

        AnsiString IniFile;

        unsigned char usbBuf[64];
        sQuery devQuery;
        ExtendedQueryInfo extQueryInfo;

        size_t hexFileSize;
        char *hexFileData;             /* Memory-mapped hex file data */
        char *hexPlusOne;              /* Saves a lot of "+1" math    */
        unsigned char  hexBuf[56];     /* Data read/written to USB    */
        unsigned char bytesPerAddress; /* Bytes in flash per address */
        char *hexFile;

        ErrorCode __fastcall usbWrite(const char len, const char read, int timeout = 100);
        void __fastcall hexSetBytesPerAddress(unsigned char bytes);
        ErrorCode __fastcall hexOpen(char * const filename);
        int __fastcall verifyBlockProgrammable(unsigned int *addr, char *len);
        ErrorCode __fastcall issueBlock(unsigned int addr, char len, char verify);
        ErrorCode __fastcall hexWrite(const char verify);
        void __fastcall hexClose(void);

        ErrorCode __fastcall Query(void);
        ErrorCode __fastcall ExtQuery(void);
        ErrorCode __fastcall Reset(void);

        ErrorCode __fastcall Program(void);
        void __fastcall Error(ErrorCode status);



        void __fastcall Buttons(void);

        void __fastcall Delay(DWORD ms, int SLEEP);
        AnsiString __fastcall ConnectString(void);

protected:
        void __fastcall WMChangeDev(TMessage &Message);
BEGIN_MESSAGE_MAP
  MESSAGE_HANDLER(WM_DEVICECHANGE, TMessage, WMChangeDev)
END_MESSAGE_MAP(TControl)


public:		// User declarations
        __fastcall TBootloader(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TBootloader *Bootloader;
//---------------------------------------------------------------------------
#endif
