Tag: Programming
Resurrection.
by DW on Sep.18, 2012, under Electronics, Programming
It fell to the wayside a long time ago, but it never truly died. I of course mean the robot/electronics type thing I was building based on a Netduino. Unfortunately the XBee WiFi Module didn’t prove to be the solution to giving it WiFi connectivity back last year. I’m sure it’s a perfectly good piece of hardware, but unfortunately I was never able to configure it correctly, even using 2 different USB adapters. The software was temperamental, and would often lose connectivity with the XBee.
Some recent Googling of the problem again however turned up different results. A simple search for ‘Netduino WiFi’ turned up a post on their forums from late August, indicating the WiFly might be the solution (http://forums.netduino.com/index.php?/topic/5218-netduino-wifi/)! Luckily for me it is available as an XBee Module, and I already had the XBee Shield.
This proved to be a dream to set up and get working! I simply plugged it in, followed some simple instructions (http://www.tinkerfailure.com/2012/02/setting-up-the-wifly-rn-xv & http://www.newspinrobotics.com/index.php/blog/getting-a-wi-fly-module-up-and-running) and was connected directly to an Ad-Hoc network it created. From there I was able to connect it to my home WiFi network, and get all the settings correct so I could send UDP packets to my Netduino as planned. No messing. No hassle. It just worked.
My problem then came when I updated the Netduino firmware to version 4.2, and the .NET Micro Framework SDK to version 4.2. The update of both went absolutely fine. But when it was complete I found I could no longer compile and run my simple motor driver test code. First of all I have to make sure there was a Reference added to ‘SecretLabs.NETMF.Hardware.PWM’ (the old one) as the updated .NET Micro Framework included new PWM functionality. This also meant adding ‘SecretLabs.NETMF.Hardware.PWM’ in place of PWM where the 4 motors are defined. This meant it still correctly used the ‘old’ PWM functions. But it wasn’t the full fix.
The code would compile and run, but the motors would not move. Using debugging, I traced the main problem to an error in the motor driver code where the value for the PWM pulse period is calculated. In my original code, I used a very large number without any trouble here.
uint period = (uint)(1000000000D / (double)freq);
But this now caused an exception, and stopped the program from running, so the motors would not turn. I reduced this value from ’1000000000D’ to only ’1000D’ and found it worked. I still had full control of the motors, including speed and direction. So if you are using this Motor Shield Driver, and update your Netduino firmware and .NET Micro Framework, please also update this value. You may find other values work too, but I can confirm ’1000D’ works.
Now I’ve resurrected this, and have achieved the bare minimum I originally set out for, I will continue. I will hopefully think up other things to add to it, or things I want it to do. I’m also going to build the hardware into something more presentable than it currently is. Although it is never going to be a Sphero.
The Forgotten Child(Window): How to Implement Pop-ups on Windows Phone 7.5 Mango – Part 2
by DW on Nov.30, 2011, under App Development, Programming
In my last post, I explain how to get the correct Silverlight assembly referenced in developing a Windows Phone 7.5 application, so you can implement the ChildWindow object. This allows you to add Pop-up dialogue boxes to your application that by default are not possible!
Now, I’m going to show you how to implement the ChildWindow, design your pop-up, and use it in your Windows Phone 7 app. I have tried to keep it simple, and focus on implementing the ChildWindow only. I have not included the other functionality I use in my app. There are many developer resources available that can help you can learn Windows Phone development, such as here at the Windows Phone App Hub, but these resources do not include the ChildWindow class I am explaining here.
First, add a new Windows Phone User Control to your project, and name it, for example ‘Popup.xaml‘. (Right click your project, Click Add -> New Item. Choose Windows Phone User Control from Silverlight for Windows Phone). This will create your control and display the visual and code XAML editors side by side.
You can design your control whichever way you prefer. You may drag controls from the toolbox onto the visual side, or you may type this in the code editor. As it is a pop-up, you should probably keep it simple. I have used just 2 TextBlocks, 1 TextBox and 2 Buttons.
The next step is very important. You must change the XAML to reference ‘System.Windows.Controls‘ and change it from a UserControl to a ChildWindow. It is also important to set the size and position relevant to the screen size. Remember, Windows Phone 7 handsets currently have a 480 x 800 (in portrait) resolution. I have created the pop-up to be 440 wide, not quite the full width of the screen, and only 200 high. (If you were designing your app to work in landscape, you would need to adjust the width and height to be appropriate.) I have positioned it towards the top of the screen, so that the software touch screen keyboard does not cover it. If you have the pop-up too low, it would be behind the keyboard and difficult to use.
Your Popup.xaml code outline should look like this:
<tk:ChildWindow x:Class="ExampleProject.Popup"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:tk="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
VerticalAlignment="Top"
HorizontalAlignment="Center"
Margin="0,40,0,0"
HasCloseButton="false" Height="200" Width="440">
<Grid x:Name="LayoutRoot">
<!--LayoutRoot is the root grid where all page content is placed-->
</Grid>
</tk:ChildWindow>
Next, for any Buttons you have used, make sure the Button Click events have been generated. Now go into ‘Popup.xaml.cs‘, and begin by adding reference to ‘System.Windows.Controls‘ at the top with the line ‘using System.Windows.Controls;‘. Change the base class for your pop-up from ‘UserControl‘ to ‘ChildWindow‘.
The outline of Popup.xaml.cs will now look like this:
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace ExampleProject
{
public partial class Popup : ChildWindow
{
public string HostName { get; set; }
public Popup()
{
InitializeComponent();
}
}
}
I am using my pop-up to input a network device Host name, which is then used to set the destination server of a UDP socket when broadcasting data. I have defined that as a public variable in Popup.xaml.cs, so it can be set, and then read back in my main app. I store this as a setting in my app, so it can be saved to memory, and loaded in future. I also have two buttons, ‘OK’ to set the Host name, and ‘Cancel’ to close the Pop-up without doing anything.
I use a Connect button in MainPage.xaml.cs to create an instance of the pop-up and display it. The existing value of Host name is loaded from ‘settings’ and passed to the pop-up window. Important: If you are developing a ‘Silverlight and XNA Application‘, as I am here, you may need to turn off the XNA rendering to display the Pop-up. When I first tried this, my pop-up would not display over the XNA graphics I was rendering at the time.
Opening the Pop-up from MainPage.xaml.cs:
private void btnConnect_Click(object sender, RoutedEventArgs e)
{
// Set the sharing mode of the graphics device to turn off XNA rendering
SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(false);
Popup popup = new Popup();
popup.HostName = settings.HostName;
popup.Closed += new EventHandler(OnPopupShow);
popup.Show();
}
In Popup.xaml.cs, it then takes the current Host name, and puts it into the text box:
protected override void OnOpened()
{
base.OnOpened();
this.txtHostName.Text = this.HostName;
this.txtHostName.SelectAll();
}
Finally the two buttons in Popup.xaml.cs control how it functions in MainPage.xaml.cs:
private void btnOK_Click(object sender, RoutedEventArgs e)
{
// Value is good so close this childwindow
this.HostName = this.txtHostName.Text.Trim();
this.DialogResult = true;
}
private void btnCancel_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = false;
}
Back in MainPage.xaml.cs I use the OnPopupShow event that occurs when the pop-up is closed, to decide what to do with the Host name. If the ‘OK’ button was pressed, the pop-up will return ‘true’, and so the Host name entered in the TextBox will be used and saved into the app settings. If ‘Cancel’ was pressed, the pop-up returns ‘false’, and so my application ignores any text that was entered, and will continue to use the existing saved Host name. Important: As we turned off XNA rendering to allow us to display the Pop-up correctly, you can now turn it back on as the Pop-up has closed.
Handling the result of the Pop-up with the OnPopupShow event:
private void OnPopupShow(object sender, EventArgs e)
{
Popup popup = sender as Popup;
if (popup.DialogResult == true)
{
settings.HostName = popup.HostName;
settings.Save();
// Set the sharing mode of the graphics device to turn on XNA rendering
SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(true);
}
}
This is a simple implementation of a ChildWindow in a Windows Phone 7 application, used to get the destination Host name for a UDP socket. As I said at the start, I have focussed just on the ChildWindow to display and use a pop-up, and left out the other functionality of my application.
This should now allow you to create your own Pop-up, with whatever controls you need, and use it in your own application. Another application of it could be entering a username and password.
Have fun developing Windows Phone 7 apps – hopefully you will find this useful. If you have any questions, or get stuck, post a comment and I’ll try to help!
The Forgotten Child(Window): How to Implement Pop-ups on Windows Phone 7.5 Mango – Part 1
by DW on Nov.28, 2011, under App Development, Programming
In the comparatively basic Windows Phone 7 app I am currently writing, I wanted to do something I thought would be simple. It should be simple. To have a dialog box pop-up and allow me to input data into a text box. This could be useful to get a username and password, for example, without navigating to a completely different ‘page’ in the app. Or it could just be to pop-up and display information messages in a different place on the screen, rather than across the top using a ‘MessageBox’.
I could not just use a ‘MessageBox’ because I need to add at least a TextBox control on to the pop-up. I’ve done this in other software I have written, and those have only been basic applications at best, written in C# (or even at work, the dreaded, depressing VBA!). But for some reason, for Windows Phone 7, this has been made very difficult!
Silverlight includes exactly the object needed – the ChildWindow. But although Silverlight for Windows Phone includes the System.Windows.Controls namespace, where ChildWindow is found, that particular feature is absent from the phone version.
Instead, to be able to implement a ChildWindow in a Windows Phone 7 application, you must manually reference the full System.Windows.Controls.dll in your project, from the main Silverlight SDK. (Right Click ‘References’, Click ‘Add Reference’, Click ‘Browse’, then locate and select System.Windows.Controls.dll). On my machine it was found here: “C:\Program Files (x86)\Microsoft SDKs\Silverlight\v4.0\Libraries\Client\System.Windows.Controls.dll” (Please check your machine – the Silverlight SDK may be installed in a different location.)
If you do not have the Silverlight SDK installed, please download it here: http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=18149
This includes the Silverlight 4.0 SDK, and will configure it for use in Visual Studio 2010.
Once you have the correct reference, you are ready to add a Pop-up (ChildWindow) to your Windows Phone 7 application! If you are familiar with using the ChildWindow from other Silverlight development, you’re sorted.
If not, stay tuned for my next post which will explain specifically how to use this and get a Pop-up dialog box in a Windows Phone 7 application!










Recent Comments