Created
December 27, 2015 16:46
-
-
Save thekitchenscientist/733f327714bc283a6b97 to your computer and use it in GitHub Desktop.
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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
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; | |
using Lego.Ev3.Core; | |
using Lego.Ev3.Desktop; | |
using System.Diagnostics; | |
namespace LegoLogger | |
{ | |
/// <summary> | |
/// Interaction logic for MainWindow.xaml | |
/// </summary> | |
public partial class MainWindow : Window | |
{ | |
Brick _brick; | |
public MainWindow() | |
{ | |
InitializeComponent(); | |
} | |
// Create a list to hold sensor data | |
public List<string> LogList = new List<string>(); | |
public bool IsLogging = new bool(); | |
public int LetterCount = new int(); | |
// Stuff that happens when the program is loaded | |
private async void Grid_Loaded(object sender, RoutedEventArgs e) | |
{ | |
// Connect to brick via bluetooth | |
_brick = new Brick(new BluetoothCommunication("COM3")); | |
_brick.BrickChanged += _brick_BrickChanged; | |
await _brick.ConnectAsync(TimeSpan.FromMilliseconds(50)); | |
await _brick.DirectCommand.PlayToneAsync(50, 1000, 300); | |
// Add comma seperated titles to list | |
LogList.Add("IsWriting,NewLetter,LetterCount,xCoordinate,yCoordinate"); | |
// Initialise variables | |
IsLogging = false; | |
LetterCount = 0; | |
// Set Sensor to Color Mode | |
_brick.Ports[InputPort.Three].SetMode(ColorMode.Color); | |
} | |
//Stuff that happens every time the sensor values change | |
private void _brick_BrickChanged(object sender, BrickChangedEventArgs e) | |
{ | |
var IsWriting = _brick.Ports[InputPort.One].SIValue; | |
var NewLetter = _brick.Ports[InputPort.Three].SIValue; | |
var xCoordinate = _brick.Ports[InputPort.A].SIValue; | |
var yCoordinate = _brick.Ports[InputPort.B].SIValue; | |
yCoordinate = yCoordinate * -1; | |
// Logic to Control if to save sensor data | |
if (IsLogging) | |
{ | |
//create comma seperated list of data | |
string LogValues = IsWriting.ToString() + "," + NewLetter.ToString() + "," + LetterCount.ToString() + "," + xCoordinate.ToString() + "," + yCoordinate.ToString(); | |
//add to list | |
LogList.Add(LogValues); | |
} | |
// Logic to count colour sensor interactions | |
if (NewLetter>=1) | |
{ | |
LetterCount = LetterCount + 1; | |
} | |
// Update GUI | |
GUILetterCount.Content = LetterCount.ToString(); | |
GUIxCoordinate.Content = xCoordinate.ToString(); | |
GUIyCoordinate.Content = yCoordinate.ToString(); | |
GUIIsWriting.Content = IsWriting.ToString(); | |
// Cursor formatting | |
GUICursorV.X1 = 178 + xCoordinate * 3 - 10; | |
GUICursorV.X2 = 178 + xCoordinate * 3 + 10; | |
GUICursorV.Y1 = 118 - yCoordinate * 3; | |
GUICursorV.Y2 = 118 - yCoordinate * 3; | |
GUICursorH.X1 = 178 + xCoordinate * 3; | |
GUICursorH.X2 = 178 + xCoordinate * 3; | |
GUICursorH.Y1 = 118 - yCoordinate * 3 - 10; | |
GUICursorH.Y2 = 118 - yCoordinate * 3 + 10; | |
} | |
//action to take on UI button presses | |
private void StartLoggingButton_Click(object sender, RoutedEventArgs e) | |
{ | |
IsLogging = true; | |
} | |
private void StopLoggingButton_Click(object sender, RoutedEventArgs e) | |
{ | |
IsLogging = false; | |
} | |
private void WriteLogButton_Click(object sender, RoutedEventArgs e) | |
{ | |
try | |
{ | |
//method 1 to write log | |
System.IO.File.WriteAllLines(@"C:\EV3\Log.csv", LogList); | |
} | |
catch (Exception) | |
{ | |
System.Windows.MessageBox.Show("Unable to write to file. Check it is not in use"); | |
//throw; | |
} | |
//method 2 | |
//add a carriage return after each string | |
//var text = string.Empty; | |
//foreach (String s in LogList) | |
//{ | |
// text += s.ToString() + "\r\n"; | |
//} | |
//display the final list | |
//MessageBox.Show(text); | |
//System.IO.File.WriteAllText(@"C:\EV3\Log2.csv", text); | |
} | |
private void ClearLogButton_Click(object sender, RoutedEventArgs e) | |
{ | |
LogList.Clear(); | |
LogList.Add("IsWriting,NewLetter,LetterCount,xCoordinate,yCoordinate"); | |
} | |
private class GUIText | |
{ | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment