2011-09-08 22:31:32 -03:00
using System ;
using System.Collections.Generic ;
2012-02-26 19:13:23 -04:00
using System.Reflection ;
2011-09-08 22:31:32 -03:00
using System.Windows.Forms ;
using System.IO.Ports ;
using System.IO ;
using System.Xml ;
using System.Net ;
2012-02-26 19:13:23 -04:00
using log4net ;
2012-04-24 10:49:27 -03:00
using ArdupilotMega.Arduino ;
2012-04-25 07:10:11 -03:00
using ArdupilotMega.Utilities ;
2011-09-08 22:31:32 -03:00
namespace ArdupilotMega.GCSViews
{
2012-01-27 04:01:28 -04:00
partial class Firmware : MyUserControl
2011-09-08 22:31:32 -03:00
{
2012-02-26 19:13:23 -04:00
private static readonly ILog log = LogManager . GetLogger ( MethodBase . GetCurrentMethod ( ) . DeclaringType ) ;
2011-09-08 22:31:32 -03:00
protected override bool ProcessCmdKey ( ref Message msg , Keys keyData )
{
2011-12-06 08:59:52 -04:00
if ( keyData = = ( Keys . Control | Keys . C ) )
{
2012-02-26 19:13:23 -04:00
var fd = new OpenFileDialog { Filter = "Firmware (*.hex)|*.hex" } ;
2011-12-06 08:59:52 -04:00
fd . ShowDialog ( ) ;
if ( File . Exists ( fd . FileName ) )
{
2012-03-18 20:26:20 -03:00
UploadFlash ( fd . FileName , ArduinoDetect . DetectBoard ( MainV2 . comPortName ) ) ;
2011-12-06 08:59:52 -04:00
}
return true ;
}
2011-09-08 22:31:32 -03:00
return base . ProcessCmdKey ( ref msg , keyData ) ;
}
List < software > softwares = new List < software > ( ) ;
bool flashing = false ;
public struct software
{
public string url ;
public string url2560 ;
2011-11-26 08:49:13 -04:00
public string url2560_2 ;
2011-09-08 22:31:32 -03:00
public string name ;
public string desc ;
public int k_format_version ;
}
public Firmware ( )
{
InitializeComponent ( ) ;
2012-01-27 04:01:28 -04:00
2011-09-08 22:31:32 -03:00
WebRequest . DefaultWebProxy . Credentials = System . Net . CredentialCache . DefaultCredentials ;
2012-01-27 04:01:28 -04:00
this . pictureBoxAPM . Image = ArdupilotMega . Properties . Resources . APM_airframes_001 ;
this . pictureBoxQuad . Image = ArdupilotMega . Properties . Resources . quad ;
this . pictureBoxHexa . Image = ArdupilotMega . Properties . Resources . hexa ;
this . pictureBoxTri . Image = ArdupilotMega . Properties . Resources . tri ;
this . pictureBoxY6 . Image = ArdupilotMega . Properties . Resources . y6 ;
this . pictureBoxHeli . Image = ArdupilotMega . Properties . Resources . APM_airframes_08 ;
this . pictureBoxHilimage . Image = ArdupilotMega . Properties . Resources . hil ;
this . pictureBoxAPHil . Image = ArdupilotMega . Properties . Resources . hilplane ;
this . pictureBoxACHil . Image = ArdupilotMega . Properties . Resources . hilquad ;
this . pictureBoxACHHil . Image = ArdupilotMega . Properties . Resources . hilheli ;
this . pictureBoxOcta . Image = ArdupilotMega . Properties . Resources . octo ;
this . pictureBoxOctav . Image = ArdupilotMega . Properties . Resources . octov ;
2011-09-08 22:31:32 -03:00
}
2012-01-27 04:01:28 -04:00
internal void Firmware_Load ( object sender , EventArgs e )
2011-09-08 22:31:32 -03:00
{
2012-02-26 19:13:23 -04:00
log . Info ( "FW load" ) ;
2012-01-27 04:01:28 -04:00
2011-09-08 22:31:32 -03:00
string url = "" ;
string url2560 = "" ;
2011-11-26 08:49:13 -04:00
string url2560_2 = "" ;
2011-09-08 22:31:32 -03:00
string name = "" ;
string desc = "" ;
int k_format_version = 0 ;
2012-01-27 04:01:28 -04:00
softwares . Clear ( ) ;
2011-09-08 22:31:32 -03:00
software temp = new software ( ) ;
try
{
2011-09-10 03:15:14 -03:00
using ( XmlTextReader xmlreader = new XmlTextReader ( "http://ardupilot-mega.googlecode.com/git/Tools/ArdupilotMegaPlanner/Firmware/firmware2.xml" ) )
2011-09-08 22:31:32 -03:00
{
while ( xmlreader . Read ( ) )
{
xmlreader . MoveToElement ( ) ;
switch ( xmlreader . Name )
{
case "url" :
url = xmlreader . ReadString ( ) ;
break ;
case "url2560" :
url2560 = xmlreader . ReadString ( ) ;
break ;
2011-11-26 08:49:13 -04:00
case "url2560-2" :
url2560_2 = xmlreader . ReadString ( ) ;
break ;
2011-09-08 22:31:32 -03:00
case "name" :
name = xmlreader . ReadString ( ) ;
break ;
case "format_version" :
k_format_version = int . Parse ( xmlreader . ReadString ( ) ) ;
break ;
case "desc" :
desc = xmlreader . ReadString ( ) ;
break ;
case "Firmware" :
if ( ! url . Equals ( "" ) & & ! name . Equals ( "" ) & & ! desc . Equals ( "Please Update" ) )
{
temp . desc = desc ;
temp . name = name ;
temp . url = url ;
temp . url2560 = url2560 ;
2011-11-26 08:49:13 -04:00
temp . url2560_2 = url2560_2 ;
2011-09-08 22:31:32 -03:00
temp . k_format_version = k_format_version ;
2011-09-20 21:23:43 -03:00
try
{
updateDisplayName ( temp ) ;
}
catch { } // just in case
2011-09-08 22:31:32 -03:00
softwares . Add ( temp ) ;
}
url = "" ;
url2560 = "" ;
name = "" ;
desc = "" ;
k_format_version = 0 ;
temp = new software ( ) ;
break ;
default :
break ;
}
}
}
}
2012-02-26 19:13:23 -04:00
catch ( Exception ex )
{
2012-03-09 11:18:12 -04:00
CustomMessageBox . Show ( "Failed to get Firmware List : " + ex . Message ) ;
2012-02-26 19:13:23 -04:00
}
log . Info ( "FW load done" ) ;
2011-09-08 22:31:32 -03:00
}
2011-09-20 21:23:43 -03:00
void updateDisplayName ( software temp )
{
if ( temp . url . ToLower ( ) . Contains ( "firmware/AP-1" . ToLower ( ) ) )
{
2011-12-17 05:22:40 -04:00
pictureBoxAPM . Text = temp . name ;
2011-09-20 21:23:43 -03:00
}
else if ( temp . url . ToLower ( ) . Contains ( "firmware/APHIL-" . ToLower ( ) ) )
{
2011-12-17 05:22:40 -04:00
pictureBoxAPHil . Text = temp . name ;
2011-09-20 21:23:43 -03:00
}
else if ( temp . url . ToLower ( ) . Contains ( "firmware/ac2-quad-" . ToLower ( ) ) )
{
2011-12-17 05:22:40 -04:00
pictureBoxQuad . Text = temp . name ;
2011-09-20 21:23:43 -03:00
}
else if ( temp . url . ToLower ( ) . Contains ( "firmware/ac2-tri" . ToLower ( ) ) )
{
2011-12-17 05:22:40 -04:00
pictureBoxTri . Text = temp . name ;
2011-09-20 21:23:43 -03:00
}
else if ( temp . url . ToLower ( ) . Contains ( "firmware/ac2-hexa" . ToLower ( ) ) )
{
2011-12-17 05:22:40 -04:00
pictureBoxHexa . Text = temp . name ;
2011-09-20 21:23:43 -03:00
}
else if ( temp . url . ToLower ( ) . Contains ( "firmware/ac2-y6" . ToLower ( ) ) )
{
2011-12-17 05:22:40 -04:00
pictureBoxY6 . Text = temp . name ;
2011-09-20 21:23:43 -03:00
}
2011-10-16 10:21:36 -03:00
else if ( temp . url . ToLower ( ) . Contains ( "firmware/ac2-heli-1" . ToLower ( ) ) )
2011-09-20 21:23:43 -03:00
{
2011-12-17 05:22:40 -04:00
pictureBoxHeli . Text = temp . name ;
2011-09-20 21:23:43 -03:00
}
else if ( temp . url . ToLower ( ) . Contains ( "firmware/ac2-quadhil" . ToLower ( ) ) )
{
2011-12-17 05:22:40 -04:00
pictureBoxACHil . Text = temp . name ;
}
else if ( temp . url . ToLower ( ) . Contains ( "firmware/ac2-octav-" . ToLower ( ) ) )
{
pictureBoxOctav . Text = temp . name ;
}
else if ( temp . url . ToLower ( ) . Contains ( "firmware/ac2-octa-" . ToLower ( ) ) )
{
pictureBoxOcta . Text = temp . name ;
2011-09-20 21:23:43 -03:00
}
2011-10-16 10:21:36 -03:00
else
{
2012-02-26 19:13:23 -04:00
log . Info ( "No Home " + temp . name + " " + temp . url ) ;
2011-10-16 10:21:36 -03:00
}
2011-09-20 21:23:43 -03:00
}
2011-09-08 22:31:32 -03:00
void findfirmware ( string findwhat )
{
2012-01-19 10:01:53 -04:00
List < software > items = new List < software > ( ) ;
2012-01-20 00:07:23 -04:00
// build list
2011-09-08 22:31:32 -03:00
foreach ( software temp in softwares )
{
if ( temp . url . ToLower ( ) . Contains ( findwhat . ToLower ( ) ) )
{
2012-01-19 10:01:53 -04:00
items . Add ( temp ) ;
}
}
2012-01-20 00:07:23 -04:00
// none found
2012-01-19 10:01:53 -04:00
if ( items . Count = = 0 )
{
2012-03-09 11:18:12 -04:00
CustomMessageBox . Show ( "The requested firmware was not found." ) ;
2012-01-19 10:01:53 -04:00
return ;
}
2012-01-20 00:07:23 -04:00
else if ( items . Count = = 1 ) // 1 found so accept it
2012-01-19 10:01:53 -04:00
{
2012-03-09 11:18:12 -04:00
DialogResult dr = CustomMessageBox . Show ( "Are you sure you want to upload " + items [ 0 ] . name + "?" , "Continue" , MessageBoxButtons . YesNo ) ;
2012-01-19 10:01:53 -04:00
if ( dr = = System . Windows . Forms . DialogResult . Yes )
{
update ( items [ 0 ] ) ;
}
return ;
}
2012-01-20 00:07:23 -04:00
else
{
2012-03-09 11:18:12 -04:00
CustomMessageBox . Show ( "Something has gone wrong, to many firmware choices" ) ;
2012-01-20 00:07:23 -04:00
return ;
}
2011-09-08 22:31:32 -03:00
}
private void pictureBoxAPM_Click ( object sender , EventArgs e )
{
2011-09-20 21:23:43 -03:00
findfirmware ( "firmware/AP-1" ) ;
2011-09-08 22:31:32 -03:00
}
private void pictureBoxAPMHIL_Click ( object sender , EventArgs e )
{
2011-09-10 03:15:14 -03:00
findfirmware ( "firmware/APHIL-" ) ;
2011-09-08 22:31:32 -03:00
}
private void pictureBoxQuad_Click ( object sender , EventArgs e )
{
findfirmware ( "AC2-Quad-" ) ;
}
private void pictureBoxHexa_Click ( object sender , EventArgs e )
{
findfirmware ( "AC2-Hexa-" ) ;
}
private void pictureBoxTri_Click ( object sender , EventArgs e )
{
findfirmware ( "AC2-Tri-" ) ;
}
private void pictureBoxY6_Click ( object sender , EventArgs e )
{
findfirmware ( "AC2-Y6-" ) ;
}
private void pictureBoxHeli_Click ( object sender , EventArgs e )
{
findfirmware ( "AC2-Heli-" ) ;
}
private void pictureBoxQuadHil_Click ( object sender , EventArgs e )
{
findfirmware ( "AC2-QUADHIL" ) ;
}
private void update ( software temp )
{
string board = "" ;
MainV2 . comPort . BaseStream . DtrEnable = false ;
MainV2 . comPort . Close ( ) ;
System . Threading . Thread . Sleep ( 100 ) ;
2012-03-18 20:26:20 -03:00
MainV2 . giveComport = true ;
2011-09-08 22:31:32 -03:00
try
{
if ( softwares . Count = = 0 )
{
2012-03-09 11:18:12 -04:00
CustomMessageBox . Show ( "No valid options" ) ;
2011-09-08 22:31:32 -03:00
return ;
}
lbl_status . Text = "Detecting APM Version" ;
this . Refresh ( ) ;
2012-03-18 20:26:20 -03:00
board = ArduinoDetect . DetectBoard ( MainV2 . comPortName ) ;
2011-09-08 22:31:32 -03:00
if ( board = = "" )
{
2012-03-09 11:18:12 -04:00
CustomMessageBox . Show ( "Cant detect your APM version. Please check your cabling" ) ;
2011-09-08 22:31:32 -03:00
return ;
}
2011-10-29 00:34:38 -03:00
int apmformat_version = - 1 ; // fail continue
try
{
2012-03-18 20:26:20 -03:00
apmformat_version = ArduinoDetect . decodeApVar ( MainV2 . comPortName , board ) ;
2011-10-29 00:34:38 -03:00
}
catch { }
2011-09-08 22:31:32 -03:00
if ( apmformat_version ! = - 1 & & apmformat_version ! = temp . k_format_version )
{
2012-03-09 11:18:12 -04:00
if ( DialogResult . No = = CustomMessageBox . Show ( "Epprom changed, all your setting will be lost during the update,\nDo you wish to continue?" , "Epprom format changed (" + apmformat_version + " vs " + temp . k_format_version + ")" , MessageBoxButtons . YesNo ) )
2011-09-08 22:31:32 -03:00
{
2012-03-09 11:18:12 -04:00
CustomMessageBox . Show ( "Please connect and backup your config in the configuration tab." ) ;
2011-09-08 22:31:32 -03:00
return ;
}
}
2012-02-26 19:13:23 -04:00
log . Info ( "Detected a " + board ) ;
2011-09-08 22:31:32 -03:00
string baseurl = "" ;
2011-11-21 20:32:11 -04:00
if ( board = = "2560" )
2011-09-08 22:31:32 -03:00
{
baseurl = temp . url2560 . ToString ( ) ;
}
2011-11-12 09:17:26 -04:00
else if ( board = = "1280" )
2011-09-08 22:31:32 -03:00
{
baseurl = temp . url . ToString ( ) ;
}
2011-11-26 08:49:13 -04:00
else if ( board = = "2560-2" )
{
baseurl = temp . url2560_2 . ToString ( ) ;
}
2011-11-12 09:17:26 -04:00
else
{
2012-03-09 11:18:12 -04:00
CustomMessageBox . Show ( "Invalid Board Type" ) ;
2011-11-12 09:17:26 -04:00
return ;
}
2011-09-08 22:31:32 -03:00
2012-02-26 19:13:23 -04:00
log . Info ( "Using " + baseurl ) ;
2011-12-06 08:59:52 -04:00
2011-09-08 22:31:32 -03:00
// Create a request using a URL that can receive a post.
WebRequest request = WebRequest . Create ( baseurl ) ;
request . Timeout = 10000 ;
// Set the Method property of the request to POST.
request . Method = "GET" ;
// Get the request stream.
Stream dataStream ; //= request.GetRequestStream();
// Get the response.
WebResponse response = request . GetResponse ( ) ;
// Display the status.
2012-02-26 19:13:23 -04:00
log . Info ( ( ( HttpWebResponse ) response ) . StatusDescription ) ;
2011-09-08 22:31:32 -03:00
// Get the stream containing content returned by the server.
dataStream = response . GetResponseStream ( ) ;
long bytes = response . ContentLength ;
long contlen = bytes ;
byte [ ] buf1 = new byte [ 1024 ] ;
FileStream fs = new FileStream ( Path . GetDirectoryName ( Application . ExecutablePath ) + Path . DirectorySeparatorChar + @"firmware.hex" , FileMode . Create ) ;
lbl_status . Text = "Downloading from Internet" ;
this . Refresh ( ) ;
2011-09-10 03:15:14 -03:00
dataStream . ReadTimeout = 30000 ;
while ( dataStream . CanRead )
2011-09-08 22:31:32 -03:00
{
try
{
2011-09-10 03:15:14 -03:00
progress . Value = 50 ; // (int)(((float)(response.ContentLength - bytes) / (float)response.ContentLength) * 100);
2011-09-08 22:31:32 -03:00
this . progress . Refresh ( ) ;
}
catch { }
int len = dataStream . Read ( buf1 , 0 , 1024 ) ;
2011-09-10 03:15:14 -03:00
if ( len = = 0 )
break ;
2011-09-08 22:31:32 -03:00
bytes - = len ;
fs . Write ( buf1 , 0 , len ) ;
}
fs . Close ( ) ;
dataStream . Close ( ) ;
response . Close ( ) ;
progress . Value = 100 ;
this . Refresh ( ) ;
2012-02-26 19:13:23 -04:00
log . Info ( "Downloaded" ) ;
2011-09-08 22:31:32 -03:00
}
2012-03-09 11:18:12 -04:00
catch ( Exception ex ) { lbl_status . Text = "Failed download" ; CustomMessageBox . Show ( "Failed to download new firmware : " + ex . ToString ( ) ) ; return ; }
2011-09-08 22:31:32 -03:00
2011-09-14 10:31:00 -03:00
UploadFlash ( Path . GetDirectoryName ( Application . ExecutablePath ) + Path . DirectorySeparatorChar + @"firmware.hex" , board ) ;
}
2012-01-15 05:00:50 -04:00
public void UploadFlash ( string filename , string board )
2011-09-14 10:31:00 -03:00
{
2011-09-08 22:31:32 -03:00
byte [ ] FLASH = new byte [ 1 ] ;
StreamReader sr = null ;
try
{
lbl_status . Text = "Reading Hex File" ;
this . Refresh ( ) ;
2011-09-14 10:31:00 -03:00
sr = new StreamReader ( filename ) ;
2011-09-08 22:31:32 -03:00
FLASH = readIntelHEXv2 ( sr ) ;
sr . Close ( ) ;
2012-02-26 19:13:23 -04:00
log . InfoFormat ( "\n\nSize: {0}\n\n" , FLASH . Length ) ;
}
catch ( Exception ex )
{
if ( sr ! = null )
{
sr . Dispose ( ) ;
}
lbl_status . Text = "Failed read HEX" ;
2012-03-09 11:18:12 -04:00
CustomMessageBox . Show ( "Failed to read firmware.hex : " + ex . Message ) ;
2012-02-26 19:13:23 -04:00
return ;
2011-09-08 22:31:32 -03:00
}
ArduinoComms port = new ArduinoSTK ( ) ;
if ( board = = "1280" )
{
2011-11-18 10:33:44 -04:00
if ( FLASH . Length > 126976 )
{
2012-03-09 11:18:12 -04:00
CustomMessageBox . Show ( "Firmware is to big for a 1280, Please upgrade!!" ) ;
2011-11-18 10:33:44 -04:00
return ;
}
2011-09-08 22:31:32 -03:00
//port = new ArduinoSTK();
port . BaudRate = 57600 ;
}
2011-12-04 18:43:29 -04:00
else if ( board = = "2560" | | board = = "2560-2" )
2011-09-08 22:31:32 -03:00
{
2012-02-26 19:13:23 -04:00
port = new ArduinoSTKv2
{
BaudRate = 115200
} ;
2011-09-08 22:31:32 -03:00
}
port . DataBits = 8 ;
port . StopBits = StopBits . One ;
port . Parity = Parity . None ;
port . DtrEnable = true ;
try
{
2012-03-18 20:26:20 -03:00
port . PortName = MainV2 . comPortName ;
2011-09-08 22:31:32 -03:00
port . Open ( ) ;
flashing = true ;
if ( port . connectAP ( ) )
{
2012-02-26 19:13:23 -04:00
log . Info ( "starting" ) ;
2011-09-08 22:31:32 -03:00
lbl_status . Text = "Uploading " + FLASH . Length + " bytes to APM" ;
progress . Value = 0 ;
this . Refresh ( ) ;
// this is enough to make ap_var reset
//port.upload(new byte[256], 0, 2, 0);
2012-02-26 19:13:23 -04:00
port . Progress + = port_Progress ;
2011-09-08 22:31:32 -03:00
if ( ! port . uploadflash ( FLASH , 0 , FLASH . Length , 0 ) )
{
flashing = false ;
if ( port . IsOpen )
port . Close ( ) ;
throw new Exception ( "Upload failed. Lost sync. Try Arduino!!" ) ;
}
port . Progress - = new ProgressEventHandler ( port_Progress ) ;
progress . Value = 100 ;
2012-02-26 19:13:23 -04:00
log . Info ( "Uploaded" ) ;
2011-09-08 22:31:32 -03:00
this . Refresh ( ) ;
int start = 0 ;
short length = 0x100 ;
byte [ ] flashverify = new byte [ FLASH . Length + 256 ] ;
lbl_status . Text = "Verify APM" ;
progress . Value = 0 ;
this . Refresh ( ) ;
while ( start < FLASH . Length )
{
progress . Value = ( int ) ( ( start / ( float ) FLASH . Length ) * 100 ) ;
progress . Refresh ( ) ;
port . setaddress ( start ) ;
2012-02-26 19:13:23 -04:00
log . Info ( "Downloading " + length + " at " + start ) ;
2011-09-08 22:31:32 -03:00
port . downloadflash ( length ) . CopyTo ( flashverify , start ) ;
start + = length ;
}
progress . Value = 100 ;
for ( int s = 0 ; s < FLASH . Length ; s + + )
{
if ( FLASH [ s ] ! = flashverify [ s ] )
{
2012-03-09 11:18:12 -04:00
CustomMessageBox . Show ( "Upload succeeded, but verify failed: exp " + FLASH [ s ] . ToString ( "X" ) + " got " + flashverify [ s ] . ToString ( "X" ) + " at " + s ) ;
2011-09-08 22:31:32 -03:00
break ;
}
}
2011-12-29 06:31:42 -04:00
lbl_status . Text = "Write Done... Waiting (17 sec)" ;
2011-09-08 22:31:32 -03:00
}
else
{
lbl_status . Text = "Failed upload" ;
2012-03-09 11:18:12 -04:00
CustomMessageBox . Show ( "Communication Error - no connection" ) ;
2011-09-08 22:31:32 -03:00
}
port . Close ( ) ;
flashing = false ;
Application . DoEvents ( ) ;
2011-12-17 18:50:40 -04:00
try
{
( ( SerialPort ) port ) . Open ( ) ;
}
catch { }
DateTime startwait = DateTime . Now ;
2011-09-08 22:31:32 -03:00
2011-12-29 06:31:42 -04:00
while ( ( DateTime . Now - startwait ) . TotalSeconds < 17 )
2011-12-17 18:50:40 -04:00
{
try
{
Console . Write ( ( ( SerialPort ) port ) . ReadExisting ( ) . Replace ( "\0" , " " ) ) ;
}
catch { }
System . Threading . Thread . Sleep ( 1000 ) ;
2012-01-02 18:53:14 -04:00
progress . Value = ( int ) Math . Min ( ( ( DateTime . Now - startwait ) . TotalSeconds / 17 * 100 ) , 100 ) ;
2011-12-17 18:50:40 -04:00
progress . Refresh ( ) ;
}
try
{
( ( SerialPort ) port ) . Close ( ) ;
}
catch { }
progress . Value = 100 ;
2011-11-19 20:17:17 -04:00
lbl_status . Text = "Done" ;
2011-09-08 22:31:32 -03:00
}
2012-02-26 19:13:23 -04:00
catch ( Exception ex )
{
lbl_status . Text = "Failed upload" ;
2012-03-09 11:18:12 -04:00
CustomMessageBox . Show ( "Check port settings or Port in use? " + ex ) ;
2012-02-26 19:13:23 -04:00
port . Close ( ) ;
}
2011-09-08 22:31:32 -03:00
flashing = false ;
2012-03-18 20:26:20 -03:00
MainV2 . giveComport = false ;
2011-09-08 22:31:32 -03:00
}
2012-02-20 07:30:47 -04:00
void port_Progress ( int progress , string status )
2011-09-08 22:31:32 -03:00
{
2012-02-26 19:13:23 -04:00
log . InfoFormat ( "Progress {0} " , progress ) ;
2011-09-08 22:31:32 -03:00
this . progress . Value = progress ;
this . progress . Refresh ( ) ;
}
byte [ ] readIntelHEXv2 ( StreamReader sr )
{
2011-12-17 05:22:40 -04:00
byte [ ] FLASH = new byte [ 1024 * 1024 ] ;
2011-09-08 22:31:32 -03:00
int optionoffset = 0 ;
int total = 0 ;
2011-10-04 08:19:25 -03:00
bool hitend = false ;
2011-09-08 22:31:32 -03:00
while ( ! sr . EndOfStream )
{
progress . Value = ( int ) ( ( ( float ) sr . BaseStream . Position / ( float ) sr . BaseStream . Length ) * 100 ) ;
progress . Refresh ( ) ;
string line = sr . ReadLine ( ) ;
if ( line . StartsWith ( ":" ) )
{
int length = Convert . ToInt32 ( line . Substring ( 1 , 2 ) , 16 ) ;
int address = Convert . ToInt32 ( line . Substring ( 3 , 4 ) , 16 ) ;
int option = Convert . ToInt32 ( line . Substring ( 7 , 2 ) , 16 ) ;
2012-02-26 19:13:23 -04:00
log . InfoFormat ( "len {0} add {1} opt {2}" , length , address , option ) ;
2011-09-08 22:31:32 -03:00
if ( option = = 0 )
{
string data = line . Substring ( 9 , length * 2 ) ;
for ( int i = 0 ; i < length ; i + + )
{
byte byte1 = Convert . ToByte ( data . Substring ( i * 2 , 2 ) , 16 ) ;
FLASH [ optionoffset + address ] = byte1 ;
address + + ;
if ( ( optionoffset + address ) > total )
total = optionoffset + address ;
}
}
else if ( option = = 2 )
{
2011-12-17 05:22:40 -04:00
optionoffset = ( int ) Convert . ToUInt16 ( line . Substring ( 9 , 4 ) , 16 ) < < 4 ;
2011-09-08 22:31:32 -03:00
}
2011-10-04 08:19:25 -03:00
else if ( option = = 1 )
{
hitend = true ;
}
2011-09-08 22:31:32 -03:00
int checksum = Convert . ToInt32 ( line . Substring ( line . Length - 2 , 2 ) , 16 ) ;
2011-10-04 08:19:25 -03:00
byte checksumact = 0 ;
2011-12-17 05:22:40 -04:00
for ( int z = 0 ; z < ( ( line . Length - 1 - 2 ) / 2 ) ; z + + ) // minus 1 for : then mins 2 for checksum itself
{
2011-10-04 08:19:25 -03:00
checksumact + = Convert . ToByte ( line . Substring ( z * 2 + 1 , 2 ) , 16 ) ;
}
checksumact = ( byte ) ( 0x100 - checksumact ) ;
if ( checksumact ! = checksum )
{
2012-03-09 11:18:12 -04:00
CustomMessageBox . Show ( "The hex file loaded is invalid, please try again." ) ;
2011-10-04 08:19:25 -03:00
throw new Exception ( "Checksum Failed - Invalid Hex" ) ;
}
2011-09-08 22:31:32 -03:00
}
//Regex regex = new Regex(@"^:(..)(....)(..)(.*)(..)$"); // length - address - option - data - checksum
}
2011-10-04 08:19:25 -03:00
if ( ! hitend )
{
2012-03-09 11:18:12 -04:00
CustomMessageBox . Show ( "The hex file did no contain an end flag. aborting" ) ;
2011-10-04 08:19:25 -03:00
throw new Exception ( "No end flag in file" ) ;
}
2011-09-08 22:31:32 -03:00
Array . Resize < byte > ( ref FLASH , total ) ;
return FLASH ;
}
private void FirmwareVisual_FormClosing ( object sender , FormClosingEventArgs e )
{
if ( flashing = = true )
{
e . Cancel = true ;
2012-03-09 11:18:12 -04:00
CustomMessageBox . Show ( "Cant exit while updating" ) ;
2011-09-08 22:31:32 -03:00
}
}
private void BUT_setup_Click ( object sender , EventArgs e )
{
2012-04-25 07:10:11 -03:00
Form temp = new Form ( ) ;
MyUserControl configview = new GCSViews . ConfigurationView . Setup ( ) ;
temp . Controls . Add ( configview ) ;
2012-03-03 20:42:42 -04:00
ThemeManager . ApplyThemeTo ( temp ) ;
2012-04-25 07:10:11 -03:00
// fix title
temp . Text = configview . Name ;
// fix size
temp . Size = configview . Size ;
configview . Dock = DockStyle . Fill ;
temp . FormClosing + = configview . Close ;
2011-09-08 22:31:32 -03:00
temp . ShowDialog ( ) ;
}
2011-12-17 05:22:40 -04:00
private void pictureBoxOctav_Click ( object sender , EventArgs e )
{
findfirmware ( "AC2-Octav-" ) ;
}
private void pictureBoxOcta_Click ( object sender , EventArgs e )
{
findfirmware ( "AC2-Octa-" ) ;
}
private void pictureBoxAPHil_Click ( object sender , EventArgs e )
{
findfirmware ( "Firmware/APHIL-" ) ;
}
private void pictureBoxACHil_Click ( object sender , EventArgs e )
{
findfirmware ( "AC2-QUADHIL-" ) ;
}
private void pictureBoxACHHil_Click ( object sender , EventArgs e )
{
findfirmware ( "AC2-HELHIL-" ) ;
}
2011-09-08 22:31:32 -03:00
}
}