Skip to content

Commit

Permalink
Updated Example Program:
Browse files Browse the repository at this point in the history
- Added repeat write
- Fixed spelling errors
- Program indicticates when a disconnection was successful
  • Loading branch information
brianEngio committed Aug 26, 2016
1 parent 8cae349 commit 68bfa3b
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 20 deletions.
18 changes: 14 additions & 4 deletions Example/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,15 @@
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
<Button Name="writeURLButton" HorizontalAlignment="Right" Style="{StaticResource RoundButton}" Click="WriteURLButton_Click">Write</Button>
<Button HorizontalAlignment="Right" Style="{StaticResource RoundButton}" Click="WriteUrlWithTagMirror_Click">Write with Tag Mirror</Button>
<CheckBox Name="repeatUrlWrite" IsChecked="False" VerticalAlignment="Center"/>
<TextBlock Foreground="White" Margin="5,0,10,0" VerticalAlignment="Center">Repeat Write</TextBlock>
</StackPanel>
</StackPanel>
</TabItem>
<TabItem Style="{StaticResource WriteText}">
<StackPanel Background="{StaticResource DarkBackground}" Orientation="Vertical">
<StackPanel Margin="10">
<TextBlock Foreground="White" TextWrapping="Wrap" FontSize="20" Margin="0,0,0,10">
<TextBlock Foreground="White" TextWrapping="Wrap" FontSize="20">
Write Text
</TextBlock>
<TextBlock Foreground="White" TextWrapping="Wrap">
Expand All @@ -209,7 +211,11 @@
</StackPanel>
<Label Margin="10,10,0,0" Padding="0,0,0,10" Foreground="White" Target="{Binding ElementName=TextBox}" Content="Text"/>
<TextBox Name="TextBox" Margin="10,0,20,20" Foreground="White" Background="{StaticResource LightBackground}" BorderBrush="#595959" BorderThickness="0,0,0,1"/>
<Button Name="writeTextButton" Margin="10" HorizontalAlignment="Right" Style="{StaticResource RoundButton}" Click="WriteTextButton_Click">Write</Button>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
<Button Name="writeTextButton" Style="{StaticResource RoundButton}" Click="WriteTextButton_Click">Write</Button>
<CheckBox Name="repeatTextWrite" IsChecked="False" VerticalAlignment="Center"/>
<TextBlock Foreground="White" Margin="5,0,10,0" VerticalAlignment="Center">Repeat Write</TextBlock>
</StackPanel>
</StackPanel>
</TabItem>
<TabItem Style="{StaticResource WriteMultiTab}">
Expand All @@ -221,7 +227,11 @@
<Button Style="{StaticResource RoundButton}" Click="ClearButton_Click">Clear</Button>
</WrapPanel>
<WrapPanel HorizontalAlignment="Right" VerticalAlignment="Center">
<Button Style="{StaticResource RoundButton}" Click="WriteMultNdef">Write</Button>
<StackPanel Orientation="Horizontal">
<Button Style="{StaticResource RoundButton}" Click="WriteMultNdef">Write</Button>
<CheckBox Name="repeatMultiNdefWrite" IsChecked="False" VerticalAlignment="Center"/>
<TextBlock Foreground="White" Margin="5,0,10,0" VerticalAlignment="Center">Repeat Write</TextBlock>
</StackPanel>
</WrapPanel>
</Grid>
<StackPanel Margin="10" DockPanel.Dock="Top">
Expand Down Expand Up @@ -398,7 +408,7 @@
Lock Tags
</TextBlock>
<TextBlock Foreground="White" TextWrapping="Wrap">
This operation instruct the Tappy to lock every tag it encounters until
This operation instruct the Tappy to lock a tag it encounters until
you either issue a stop command or another instruction. Tag locking is an
irreversible operation that prevents the locked memory from ever being
rewritten, so it is advisable to make certain that any tags you lock
Expand Down
137 changes: 124 additions & 13 deletions Example/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ namespace TapTrack.Demo
using NdefLibrary.Ndef;
using System.Text;
using Tcmp.CommandFamilies.System;
using System.Management;
using System.Text.RegularExpressions;

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
Expand Down Expand Up @@ -151,36 +154,60 @@ private void AddUID(ResponseFrame frame, Exception e)
private void WriteURLButton_Click(object sender, RoutedEventArgs e)
{
string url = string.Copy(urlTextBox.Text);
Command cmd = new WriteUri((byte)timeout.Value, (bool)lockCheckBox.IsChecked, new NdefUri(url));
Callback repeatCommand = null;
bool repeat = (bool)repeatUrlWrite.IsChecked;
Action sendCommand = () => tappy.SendCommand(cmd, ResponseCallback + repeatCommand);

ShowPendingStatus("Waiting for tap");

Command cmd = new WriteUri((byte)timeout.Value, (bool)lockCheckBox.IsChecked, new NdefUri(url));
repeatCommand = (ResponseFrame frame, Exception exc) =>
{
if (repeat)
{
if (CheckForErrorsOrTimeout(frame, exc))
return;
Thread.Sleep(800);
ShowPendingStatus("Waiting for tap");
Dispatcher.BeginInvoke(sendCommand);
}
};

tappy.SendCommand(cmd, ResponseCallback);
tappy.SendCommand(cmd, ResponseCallback + repeatCommand);
}

private void WriteUrlWithTagMirror_Click(object sender, RoutedEventArgs e)
{
string temp = string.Copy(urlTextBox.Text);
bool willLock = (bool)lockCheckBox.IsChecked;
bool repeat = (bool)repeatUrlWrite.IsChecked;
byte timeoutValue = (byte)timeout.Value;
Command detectTag = new DetectSingleTagUid(timeoutValue, DetectTagSetting.Type2Type4AandMifare);
ShowPendingStatus("Waiting for tap");
Callback detectTagCallback = null;

Callback writeCallback = (ResponseFrame frame, Exception exc) =>
{
if (CheckForErrorsOrTimeout(frame, exc))
return;
ShowSuccessStatus();

if (repeat)
{
Thread.Sleep(1000);
ShowPendingStatus("Waiting for tap");
Task.Run(() => tappy.SendCommand(detectTag, detectTagCallback));
}
};

Callback detectTagCallback = (ResponseFrame frame, Exception exc) =>
detectTagCallback = (ResponseFrame frame, Exception exc) =>
{
if (CheckForErrorsOrTimeout(frame, exc))
return;

Tag tag = new Tag(frame.Data);
Command write = new WriteUri(timeoutValue, willLock, temp.Replace("[uid]", tag.UidToString()));
ShowPendingStatus("Tag detected, please hold steady while tag is written");
Task.Run(() => tappy.SendCommand(write, writeCallback));
};

Expand All @@ -193,11 +220,26 @@ private void WriteUrlWithTagMirror_Click(object sender, RoutedEventArgs e)

private void WriteTextButton_Click(object sender, RoutedEventArgs e)
{
Command cmd = new WriteText((byte)timeout.Value, (bool)lockCheckBox.IsChecked, TextBox.Text ?? "");
ShowPendingStatus("Waiting for tap");

Command cmd = new WriteText((byte)timeout.Value, (bool)lockCheckBox.IsChecked, TextBox.Text);
Callback repeatCommand = null;
bool repeat = (bool)repeatTextWrite.IsChecked;
Action sendCommand = () => tappy.SendCommand(cmd, ResponseCallback + repeatCommand);

tappy.SendCommand(cmd, ResponseCallback);
repeatCommand = (ResponseFrame frame, Exception exc) =>
{
if (repeat)
{
if (CheckForErrorsOrTimeout(frame, exc))
return;
Thread.Sleep(1000);
ShowPendingStatus("Waiting for tap");
Dispatcher.BeginInvoke(sendCommand);
}
};

tappy.SendCommand(cmd, ResponseCallback + repeatCommand);
}

//
Expand Down Expand Up @@ -229,8 +271,23 @@ private void WriteMultNdef(object send, RoutedEventArgs e)
ShowPendingStatus("Waiting for tap");

Command cmd = new WriteCustomNdef((byte)timeout.Value, (bool)lockCheckBox.IsChecked, message);
Callback repeatCommand = null;
bool repeat = (bool)repeatMultiNdefWrite.IsChecked;
Action sendCommand = () => tappy.SendCommand(cmd, ResponseCallback + repeatCommand);

tappy.SendCommand(cmd, ResponseCallback);
repeatCommand = (ResponseFrame frame, Exception exc) =>
{
if (repeat)
{
if (CheckForErrorsOrTimeout(frame, exc))
return;
Thread.Sleep(1000);
ShowPendingStatus("Waiting for tap");
Dispatcher.BeginInvoke(sendCommand);
}
};

tappy.SendCommand(cmd, ResponseCallback + repeatCommand);
}

private void AddTextRowButton_Click(object sender, RoutedEventArgs e)
Expand Down Expand Up @@ -404,7 +461,14 @@ private void AutoDetectButton_Click(object sender, RoutedEventArgs e)
if (window.Protocol == CommunicationProtocol.Usb)
batteryTab.Visibility = Visibility.Hidden;
else if (window.Protocol == CommunicationProtocol.Bluetooth)
{
batteryTab.Visibility = Visibility.Visible;
if (GetBluegigaDevice() == null)
{
ShowFailStatus("Please insert BLED112 dongle");
return;
}
}

tappy.SwitchProtocol(window.Protocol);

Expand All @@ -417,8 +481,15 @@ private void AutoDetectButton_Click(object sender, RoutedEventArgs e)
ShowSuccessStatus($"Connected to {tappy.DeviceName}");
if (window.Protocol == CommunicationProtocol.Bluetooth)
{
Command cmd = new EnableDataThrottling(10, 5);
tappy.SendCommand(cmd);
try
{
Command cmd = new EnableDataThrottling(10, 5);
tappy.SendCommand(cmd);
}
catch
{

}
}
}
else
Expand All @@ -437,10 +508,15 @@ private void SettingsButton_Click(object sender, RoutedEventArgs e)

private void ShowPendingStatus(string message)
{
statusPopup.IsOpen = true;
statusText.Content = "Pending";
statusMessage.Content = message;
ImageBehavior.SetAnimatedSource(statusImage, (BitmapImage)FindResource("Pending"));
Action show = () =>
{
statusPopup.IsOpen = true;
statusText.Content = "Pending";
statusMessage.Content = message;
ImageBehavior.SetAnimatedSource(statusImage, (BitmapImage)FindResource("Pending"));
};

Dispatcher.BeginInvoke(show);
}

private void ShowSuccessStatus(string message = "")
Expand Down Expand Up @@ -628,7 +704,15 @@ private void ConfigSuccess(ResponseFrame frame, Exception e)

private void disconnectButton_Click(object sender, RoutedEventArgs e)
{
tappy.Disconnect();
try
{
tappy.Disconnect();
ShowSuccessStatus("Disconnect was successful");
}
catch
{
ShowFailStatus("Disconnect was unsuccessful");
}
}

private void firmwareVersionButton_Click(object sender, RoutedEventArgs e)
Expand Down Expand Up @@ -737,5 +821,32 @@ private void disableType2Button_Click(object sender, RoutedEventArgs e)

tappy.SendCommand(cmd, Type2Callback);
}

private string Search(string searchLocation)
{
ManagementObjectCollection comPortDevices;
ManagementObjectSearcher searcher = new ManagementObjectSearcher($"Select * From {searchLocation}");
comPortDevices = searcher.Get();

foreach (ManagementObject device in comPortDevices)
{
string name = device["Name"] as string;

if (name?.Contains("Bluegiga Bluetooth Low Energy") ?? false)
{
Debug.WriteLine($"Found {device["Name"]}");
Match match = Regex.Match(name, @"\(([^)]*)\)");
if (match.Groups.Count > 1)
return match.Groups[1].Value;
}
}

return null;
}

private string GetBluegigaDevice()
{
return Search("Win32_SerialPort") ?? Search("Win32_pnpEntity");
}
}
}
4 changes: 2 additions & 2 deletions Example/Styles/TapTrack Styles.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid ToolTip="Settings">
<Grid ToolTip="Settings (Tag locking, timeout)">
<Image Name="Pressed" Visibility="Hidden" Source="{StaticResource Settings_highlight}"/>
<Image Name="Unpressed" Visibility="Visible" Source="{StaticResource Settings}"/>
</Grid>
Expand Down Expand Up @@ -421,7 +421,7 @@
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Grid Name="Container" ToolTip="Confiure Tappy Settings" Background="White" Margin="10">
<Grid Name="Container" ToolTip="Configure Tappy Settings" Background="White" Margin="10">
<Image Width="30" Height="30" Stretch="Fill" Name="Pressed" Visibility="Hidden" Source="{StaticResource Settings_highlight}"/>
<Image Width="30" Height="30" Stretch="Fill" Name="Unpressed" Visibility="Visible" Source="{StaticResource Settings_dark}"/>
</Grid>
Expand Down
1 change: 1 addition & 0 deletions Example/Tappy Tcmp Demo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Drawing" />
<Reference Include="System.Management" />
<Reference Include="System.Xml" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Core" />
Expand Down
2 changes: 1 addition & 1 deletion TapTrack.Tcmp.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.25123.0
VisualStudioVersion = 14.0.25420.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tappy Tcmp Demo", "Example\Tappy Tcmp Demo.csproj", "{0A4DA2F5-AECC-4D4A-A942-B1CCC9CA5237}"
EndProject
Expand Down

0 comments on commit 68bfa3b

Please sign in to comment.