2017-08-23 23:59:45 -07:00
/ *
Copyright ( c ) 2017 , Lars Brubaker , John Lewin
All rights reserved .
Redistribution and use in source and binary forms , with or without
modification , are permitted provided that the following conditions are met :
1. Redistributions of source code must retain the above copyright notice , this
list of conditions and the following disclaimer .
2. Redistributions in binary form must reproduce the above copyright notice ,
this list of conditions and the following disclaimer in the documentation
and / or other materials provided with the distribution .
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES , INCLUDING , BUT NOT LIMITED TO , THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED . IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT , INDIRECT , INCIDENTAL , SPECIAL , EXEMPLARY , OR CONSEQUENTIAL DAMAGES
( INCLUDING , BUT NOT LIMITED TO , PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES ;
LOSS OF USE , DATA , OR PROFITS ; OR BUSINESS INTERRUPTION ) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY , WHETHER IN CONTRACT , STRICT LIABILITY , OR TORT
( INCLUDING NEGLIGENCE OR OTHERWISE ) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE , EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE .
The views and conclusions contained in the software and documentation are those
of the authors and should not b e interpreted as representing official policies ,
either expressed or implied , of the FreeBSD Project .
* /
using System ;
using System.Collections.Generic ;
using MatterHackers.Agg ;
2014-01-29 19:09:30 -08:00
using MatterHackers.Agg.UI ;
using MatterHackers.Localizations ;
2014-06-11 14:52:58 -07:00
using MatterHackers.MatterControl.PrinterCommunication ;
2016-04-18 11:31:31 -07:00
using MatterHackers.MatterControl.SlicerConfiguration ;
2016-06-02 16:21:32 -07:00
using MatterHackers.SerialPortCommunication.FrostedSerial ;
2014-01-29 19:09:30 -08:00
namespace MatterHackers.MatterControl.PrinterControls.PrinterConnections
{
2017-08-23 23:54:31 -07:00
public class SetupStepComPortManual : WizardPage
2015-04-08 15:20:10 -07:00
{
private Button nextButton ;
private Button connectButton ;
private Button refreshButton ;
private Button printerComPortHelpLink ;
2016-04-18 11:31:31 -07:00
2016-06-02 16:21:32 -07:00
private bool printerComPortIsAvailable = false ;
2015-04-08 15:20:10 -07:00
private TextWidget printerComPortHelpMessage ;
2016-04-18 11:31:31 -07:00
private TextWidget printerComPortError ;
2015-04-08 15:20:10 -07:00
2016-12-29 06:55:12 -08:00
private EventHandler unregisterEvents ;
2016-06-02 16:21:32 -07:00
protected List < SerialPortIndexRadioButton > SerialPortButtonsList = new List < SerialPortIndexRadioButton > ( ) ;
2017-09-03 11:48:08 -07:00
PrinterConnection printerConnection = PrinterConnection . Instance ;
2016-04-18 11:31:31 -07:00
2016-06-07 15:45:50 -07:00
public SetupStepComPortManual ( )
2015-04-08 15:20:10 -07:00
{
FlowLayoutWidget printerComPortContainer = createComPortContainer ( ) ;
contentRow . AddChild ( printerComPortContainer ) ;
2014-01-29 19:09:30 -08:00
2017-09-03 11:48:08 -07:00
cancelButton . Click + = ( s , e ) = > printerConnection . HaltConnectionThread ( ) ;
2017-08-23 23:54:31 -07:00
2016-04-18 11:31:31 -07:00
//Construct buttons
nextButton = textImageButtonFactory . Generate ( "Done" . Localize ( ) ) ;
nextButton . Click + = ( s , e ) = > UiThread . RunOnIdle ( Parent . Close ) ;
nextButton . Visible = false ;
2014-01-29 19:09:30 -08:00
2016-04-18 11:31:31 -07:00
connectButton = textImageButtonFactory . Generate ( "Connect" . Localize ( ) ) ;
2017-08-23 23:54:31 -07:00
connectButton . Click + = ( s , e ) = >
{
try
{
printerComPortHelpLink . Visible = false ;
printerComPortError . TextColor = ActiveTheme . Instance . PrimaryTextColor ;
printerComPortError . Text = "Attempting to connect" . Localize ( ) + "..." ;
printerComPortError . TextColor = ActiveTheme . Instance . PrimaryTextColor ;
ActiveSliceSettings . Instance . Helpers . SetComPort ( GetSelectedSerialPort ( ) ) ;
2017-09-03 11:48:08 -07:00
printerConnection . ConnectToActivePrinter ( ) ;
2017-08-23 23:54:31 -07:00
connectButton . Visible = false ;
refreshButton . Visible = false ;
}
catch
{
printerComPortHelpLink . Visible = false ;
printerComPortError . TextColor = RGBA_Bytes . Red ;
printerComPortError . Text = "Oops! Please select a serial port." . Localize ( ) ;
}
} ;
2014-01-29 19:09:30 -08:00
2016-04-18 11:31:31 -07:00
refreshButton = textImageButtonFactory . Generate ( "Refresh" . Localize ( ) ) ;
2017-08-23 15:51:29 -07:00
refreshButton . Click + = ( s , e ) = > UiThread . RunOnIdle ( ( ) = >
{
WizardWindow . ChangeToPage < SetupStepComPortManual > ( ) ;
} ) ;
2015-04-08 15:20:10 -07:00
2017-08-23 17:27:30 -07:00
this . AddPageAction ( nextButton ) ;
this . AddPageAction ( connectButton ) ;
this . AddPageAction ( refreshButton ) ;
2015-04-08 15:20:10 -07:00
2017-09-03 11:48:08 -07:00
printerConnection . CommunicationStateChanged . RegisterEvent ( onPrinterStatusChanged , ref unregisterEvents ) ;
2016-04-18 11:31:31 -07:00
}
2015-04-08 15:20:10 -07:00
2017-02-03 13:06:08 -08:00
public override void OnClosed ( ClosedEventArgs e )
2015-04-08 15:20:10 -07:00
{
2016-04-18 11:31:31 -07:00
unregisterEvents ? . Invoke ( this , null ) ;
2015-04-08 15:20:10 -07:00
base . OnClosed ( e ) ;
}
private FlowLayoutWidget createComPortContainer ( )
{
FlowLayoutWidget container = new FlowLayoutWidget ( FlowDirection . TopToBottom ) ;
container . Margin = new BorderDouble ( 0 ) ;
2017-08-07 15:47:27 -07:00
container . VAnchor = VAnchor . Stretch ;
2015-04-08 15:20:10 -07:00
BorderDouble elementMargin = new BorderDouble ( top : 3 ) ;
2014-01-29 19:09:30 -08:00
2017-01-04 07:23:30 -08:00
string serialPortLabel = "Serial Port" . Localize ( ) ;
2014-01-29 19:09:30 -08:00
string serialPortLabelFull = string . Format ( "{0}:" , serialPortLabel ) ;
TextWidget comPortLabel = new TextWidget ( serialPortLabelFull , 0 , 0 , 12 ) ;
2016-04-18 11:31:31 -07:00
comPortLabel . TextColor = ActiveTheme . Instance . PrimaryTextColor ;
2015-04-08 15:20:10 -07:00
comPortLabel . Margin = new BorderDouble ( 0 , 0 , 0 , 10 ) ;
2017-08-07 15:47:27 -07:00
comPortLabel . HAnchor = HAnchor . Stretch ;
2014-01-29 19:09:30 -08:00
2015-05-01 13:28:31 -07:00
FlowLayoutWidget serialPortContainer = new FlowLayoutWidget ( FlowDirection . TopToBottom ) ;
CreateSerialPortControls ( serialPortContainer , null ) ;
2014-01-29 19:09:30 -08:00
2015-04-08 15:20:10 -07:00
FlowLayoutWidget comPortMessageContainer = new FlowLayoutWidget ( ) ;
comPortMessageContainer . Margin = elementMargin ;
2017-08-07 15:47:27 -07:00
comPortMessageContainer . HAnchor = HAnchor . Stretch ;
2014-01-29 19:09:30 -08:00
2016-04-18 11:31:31 -07:00
printerComPortError = new TextWidget ( "Currently available serial ports." . Localize ( ) , 0 , 0 , 10 ) ;
2014-03-24 14:44:43 -07:00
printerComPortError . TextColor = ActiveTheme . Instance . PrimaryTextColor ;
2015-04-08 15:20:10 -07:00
printerComPortError . AutoExpandBoundsToText = true ;
2014-01-29 19:09:30 -08:00
2016-04-18 11:31:31 -07:00
printerComPortHelpLink = linkButtonFactory . Generate ( "What's this?" . Localize ( ) ) ;
2015-04-08 15:20:10 -07:00
printerComPortHelpLink . Margin = new BorderDouble ( left : 5 ) ;
2017-08-07 15:47:27 -07:00
printerComPortHelpLink . VAnchor = VAnchor . Bottom ;
2016-04-18 11:31:31 -07:00
printerComPortHelpLink . Click + = ( s , e ) = > printerComPortHelpMessage . Visible = ! printerComPortHelpMessage . Visible ;
2014-01-29 19:09:30 -08:00
2017-06-15 17:10:36 -07:00
printerComPortHelpMessage = new TextWidget ( "The 'Serial Port' section lists all available serial\nports on your device. Changing which USB port the printer\nis connected to may change the associated serial port.\n\nTip: If you are uncertain, unplug/plug in your printer\nand hit refresh. The new port that appears should be\nyour printer." . Localize ( ) , 0 , 0 , 10 ) ;
2014-03-24 14:44:43 -07:00
printerComPortHelpMessage . TextColor = ActiveTheme . Instance . PrimaryTextColor ;
2015-04-08 15:20:10 -07:00
printerComPortHelpMessage . Margin = new BorderDouble ( top : 10 ) ;
printerComPortHelpMessage . Visible = false ;
2014-01-29 19:09:30 -08:00
2015-04-08 15:20:10 -07:00
comPortMessageContainer . AddChild ( printerComPortError ) ;
comPortMessageContainer . AddChild ( printerComPortHelpLink ) ;
2014-01-29 19:09:30 -08:00
2015-04-08 15:20:10 -07:00
container . AddChild ( comPortLabel ) ;
2015-05-01 13:28:31 -07:00
container . AddChild ( serialPortContainer ) ;
2015-04-08 15:20:10 -07:00
container . AddChild ( comPortMessageContainer ) ;
container . AddChild ( printerComPortHelpMessage ) ;
2014-01-29 19:09:30 -08:00
2017-08-07 15:47:27 -07:00
container . HAnchor = HAnchor . Stretch ;
2015-04-08 15:20:10 -07:00
return container ;
}
2014-01-29 19:09:30 -08:00
2015-04-08 15:20:10 -07:00
private void onPrinterStatusChanged ( object sender , EventArgs e )
{
2017-09-03 11:48:08 -07:00
if ( printerConnection . PrinterIsConnected )
2015-04-08 15:20:10 -07:00
{
2016-04-18 11:31:31 -07:00
printerComPortHelpLink . Visible = false ;
printerComPortError . TextColor = ActiveTheme . Instance . PrimaryTextColor ;
printerComPortError . Text = "Connection succeeded" . Localize ( ) + "!" ;
nextButton . Visible = true ;
connectButton . Visible = false ;
2016-09-23 16:24:43 -07:00
UiThread . RunOnIdle ( ( ) = > this ? . Parent ? . Close ( ) ) ;
2015-04-08 15:20:10 -07:00
}
2017-09-03 11:48:08 -07:00
else if ( printerConnection . CommunicationState ! = CommunicationStates . AttemptingToConnect )
2015-04-08 15:20:10 -07:00
{
2016-04-18 11:31:31 -07:00
printerComPortHelpLink . Visible = false ;
printerComPortError . TextColor = RGBA_Bytes . Red ;
printerComPortError . Text = "Uh-oh! Could not connect to printer." . Localize ( ) ;
connectButton . Visible = true ;
nextButton . Visible = false ;
2015-04-08 15:20:10 -07:00
}
}
private void MoveToNextWidget ( object state )
{
2016-06-07 15:45:50 -07:00
WizardWindow . ChangeToInstallDriverOrComPortOne ( ) ;
2015-04-08 15:20:10 -07:00
}
2016-06-02 16:21:32 -07:00
protected void CreateSerialPortControls ( FlowLayoutWidget comPortContainer , string activePrinterSerialPort )
{
int portIndex = 0 ;
2016-11-03 09:57:48 -07:00
string [ ] portsToCreate = FrostedSerialPort . GetPortNames ( ) ;
2016-06-02 16:21:32 -07:00
// Add a radio button for each filtered port
foreach ( string portName in portsToCreate )
{
SerialPortIndexRadioButton comPortOption = createComPortOption ( portName , activePrinterSerialPort = = portName ) ;
if ( comPortOption . Checked )
{
printerComPortIsAvailable = true ;
}
SerialPortButtonsList . Add ( comPortOption ) ;
comPortContainer . AddChild ( comPortOption ) ;
portIndex + + ;
}
// Add a virtual entry for serial ports that were previously configured but are not currently connected
if ( ! printerComPortIsAvailable & & activePrinterSerialPort ! = null )
{
SerialPortIndexRadioButton comPortOption = createComPortOption ( activePrinterSerialPort , true ) ;
comPortOption . Enabled = false ;
comPortContainer . AddChild ( comPortOption ) ;
SerialPortButtonsList . Add ( comPortOption ) ;
portIndex + + ;
}
//If there are still no com ports show a message to that effect
if ( portIndex = = 0 )
{
2017-01-04 07:23:30 -08:00
TextWidget comPortOption = new TextWidget ( "No COM ports available" . Localize ( ) ) ;
2016-06-02 16:21:32 -07:00
comPortOption . Margin = new BorderDouble ( 3 , 6 , 5 , 6 ) ;
comPortOption . TextColor = ActiveTheme . Instance . PrimaryTextColor ;
comPortContainer . AddChild ( comPortOption ) ;
}
}
private SerialPortIndexRadioButton createComPortOption ( string portName , bool isActivePrinterPort )
{
SerialPortIndexRadioButton comPortOption = new SerialPortIndexRadioButton ( portName , portName )
{
2017-08-07 15:47:27 -07:00
HAnchor = HAnchor . Left ,
2016-06-02 16:21:32 -07:00
Margin = new BorderDouble ( 3 , 3 , 5 , 3 ) ,
TextColor = ActiveTheme . Instance . PrimaryTextColor ,
Checked = isActivePrinterPort
} ;
return comPortOption ;
}
private string GetSelectedSerialPort ( )
{
foreach ( SerialPortIndexRadioButton button in SerialPortButtonsList )
{
if ( button . Checked )
{
return button . PortValue ;
}
}
2017-01-04 07:23:30 -08:00
throw new Exception ( "Could not find a selected button." . Localize ( ) ) ;
2016-06-02 16:21:32 -07:00
}
2015-04-08 15:20:10 -07:00
}
}