-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
EV3-Basic can access the EV3 over a Serial Port connection. Setup of the connection must be done manually by the user, with the normal facilities of the operating system (an explaination is available in he user manual)
- Loading branch information
c0pperdragon
committed
Apr 6, 2015
1 parent
843352e
commit 306a002
Showing
14 changed files
with
420 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* EV3-Basic: A basic compiler to target the Lego EV3 brick | ||
Copyright (C) 2015 Reinhard Grafl | ||
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 3 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Windows; | ||
using System.Windows.Threading; | ||
using System.Threading; | ||
|
||
|
||
namespace EV3Communication | ||
{ | ||
public class ConnectionFinder | ||
{ | ||
static EV3Connection reuse = null; | ||
|
||
public static EV3Connection CreateConnection() | ||
{ | ||
// check if can re-use connection | ||
if (reuse!=null) | ||
{ | ||
EV3Connection c = reuse; | ||
reuse = null; | ||
return c; | ||
} | ||
|
||
// first try to get an USB connection | ||
try | ||
{ | ||
return new EV3ConnectionUSB(); | ||
} | ||
catch (Exception) | ||
{ } | ||
|
||
|
||
// when no USB connection available, check the serial ports | ||
String[] ports = System.IO.Ports.SerialPort.GetPortNames(); | ||
for (int i = 0; i < ports.Length; i++) | ||
{ | ||
// strange problem: Port names seem to end with an 'o'? | ||
if (ports[i].EndsWith("o")) | ||
{ | ||
ports[i] = ports[i].Substring(0, ports[i].Length - 1); | ||
} | ||
} | ||
// when no serial ports are available at all, stop try | ||
if (ports.Length<1) | ||
{ | ||
throw new Exception("Found no brick and no serial ports"); | ||
} | ||
|
||
|
||
// when there are serial ports, let user decide | ||
Array.Sort(ports, StringComparer.InvariantCulture); | ||
ConnectionTypeDialog dialog = null; | ||
// Create an extra thread for the dialog window | ||
Thread newWindowThread = new Thread(new ThreadStart(() => | ||
{ | ||
// Create and show the Window | ||
var window = new ConnectionTypeDialog(ports); | ||
// When the window closes, shut down the dispatcher | ||
window.Closed += (s, e) => | ||
Dispatcher.CurrentDispatcher.BeginInvokeShutdown(DispatcherPriority.Background); | ||
window.Show(); | ||
// let other thread get hold the window to check for finish | ||
dialog = window; | ||
// Start the Dispatcher Processing | ||
System.Windows.Threading.Dispatcher.Run(); | ||
})); | ||
// Set the apartment state | ||
newWindowThread.SetApartmentState(ApartmentState.STA); | ||
// Make the thread a background thread | ||
newWindowThread.IsBackground = true; | ||
// Start the thread | ||
newWindowThread.Start(); | ||
|
||
// wait here until the window actually was created and user has answered the prompt or closed the window... | ||
while (dialog==null || dialog.IsVisible) | ||
{ | ||
System.Threading.Thread.Sleep(100); | ||
} | ||
|
||
String port = dialog.GetSelectedPort(); | ||
if (port == null) | ||
{ | ||
throw new Exception("User did not select serial port"); | ||
} | ||
|
||
return new EV3ConnectionBluetooth(port); | ||
} | ||
|
||
public static void CloseConnection(EV3Connection c) | ||
{ | ||
if (reuse!=null) | ||
{ | ||
reuse.Close(); | ||
reuse = null; | ||
} | ||
// keep open bluetooth connections for further use | ||
if (c is EV3ConnectionBluetooth && c.IsOpen()) | ||
{ | ||
reuse = c; | ||
} | ||
else | ||
{ | ||
c.Close(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<Window | ||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="window" | ||
Title="No EV3 found" Height="220" Width="300" | ||
x:Class="EV3Communication.ConnectionTypeDialog" ResizeMode="NoResize" | ||
> | ||
<Grid Margin="15" RenderTransformOrigin="0.75,0.6"> | ||
<TextBlock Margin="0" TextWrapping="Wrap" Text="Could not find an EV3 brick connected via USB. Do you want to try one of the following serial ports instead?" VerticalAlignment="Top" RenderTransformOrigin="0.513,-0.627" Height="51"/> | ||
<ListBox x:Name="PortList" SelectionChanged="PortList_SelectionChanged" HorizontalAlignment="Left" Height="90" Margin="0" VerticalAlignment="Bottom" Width="140" VerticalContentAlignment="Bottom" Cursor="Arrow" BorderThickness="1" FontWeight="Bold"/> | ||
<Button Click="CloseButton_clicked" Content="Close" HorizontalAlignment="Right" Margin="0" VerticalAlignment="Bottom" Width="75" IsCancel="True"/> | ||
|
||
</Grid> | ||
</Window> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* EV3-Basic: A basic compiler to target the Lego EV3 brick | ||
Copyright (C) 2015 Reinhard Grafl | ||
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 3 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Windows; | ||
using System.Windows.Controls; | ||
using System.Windows.Data; | ||
using System.Windows.Documents; | ||
using System.Windows.Input; | ||
using System.Windows.Media; | ||
using System.Windows.Media.Imaging; | ||
using System.Windows.Navigation; | ||
using System.Windows.Shapes; | ||
|
||
namespace EV3Communication | ||
{ | ||
/// <summary> | ||
/// Interaction logic for ConnectionTypeDialog.xaml | ||
/// </summary> | ||
public partial class ConnectionTypeDialog : Window | ||
{ | ||
private String[] ports; | ||
private String selected; | ||
|
||
public ConnectionTypeDialog(String[] ports) | ||
{ | ||
this.ports = ports; | ||
this.selected = null; | ||
|
||
InitializeComponent(); | ||
|
||
foreach (String p in ports) | ||
{ | ||
PortList.Items.Add(p); | ||
} | ||
} | ||
|
||
public String GetSelectedPort() | ||
{ | ||
return selected; | ||
} | ||
|
||
private void CloseButton_clicked(object sender, System.Windows.RoutedEventArgs e) | ||
{ | ||
selected = null; | ||
Close(); | ||
} | ||
|
||
private void PortList_SelectionChanged(Object sender, EventArgs e) | ||
{ | ||
if (PortList.SelectedIndex>=0 && PortList.SelectedIndex<ports.Length) | ||
{ | ||
selected = ports[PortList.SelectedIndex]; | ||
Close(); | ||
} | ||
} | ||
|
||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.