In-Line Props

I am working on a brownbag for entity framework using Northwind as my database. When coding up a POCO, I decided to use the in-line property setting available since VS2008:

Product product = new Product()
{
    ProductId = (Int32)sqlDataReader[0],
    ProductName = (String)sqlDataReader[1],
    Supplier = new Supplier(),
    Category = new Category(),
    QuantityPerUnit = (String)sqlDataReader[4],
    UnitPrice = (Int16)sqlDataReader[5],
    UnitsInStock = (Int16)sqlDataReader[6],
    UnitsOnOrder = (Int16)sqlDataReader[7],
    ReorderLevel = (Int16)sqlDataReader[8],
    Discontinued = (Boolean)sqlDataReader[9]
};

The problem is that when I ran it, one of the properties was cast incorrectly. However, by using in-line, I can’t tell which one is the problem.

clip_image002

If I did it the OFW (Old-Fashion Way) like this":

Product product = new Product();
product.ProductId = (Int32)sqlDataReader[0];
product.ProductName = (String)sqlDataReader[1];
product.Supplier = new Supplier();
product.Category = new Category();
product.QuantityPerUnit = (String)sqlDataReader[4];
product.UnitPrice = (Decimal)sqlDataReader[5];
product.UnitsInStock = (Int16)sqlDataReader[6];
product.UnitsOnOrder = (Int16)sqlDataReader[7];
product.ReorderLevel = (Int16)sqlDataReader[8];
product.Discontinued = (Boolean)sqlDataReader[9];

I would have had the break on 1 line and easily identified the offending line of code:

clip_image004

The morale of the story is the same as many of my other stories – separate – don’t be lazy.

Phidgets + Netduino = Yummy!

Rob Seder sent me a bunch of his old Phidget hardware last week.  It was like Christmas in February!  Since I was working with Netduino the last week, I wondered how easily Phidgets and Netduinos can interface.  In a word – very.  I took one of the sensor cables with one end Rob had already stripped and plugged the intact end into a Phidget force sensor.  I then took the stripped wires and put them into the respective ports on the Netduino (Red = 5V supply, Black = Ground, White = AnalogIn0).  The wiring looks like this:

Blog01

 

I then coded up a simple app like so:

private static void ReadForceSensor()
{   
    AnalogInput analogInput = new AnalogInput(Pins.GPIO_PIN_A0);
    analogInput.SetRange(0, 4095);

    OutputPort digitalOutput = new OutputPort(Pins.GPIO_PIN_D13, false);

    int inputValue = 0;
    while (true)
    {
        inputValue =  analogInput.Read();
        Debug.Print("Input = " + inputValue.ToString());
        if (inputValue > 10)
        {
            digitalOutput.Write(true);
        }
        else
        {
            digitalOutput.Write(false);
        }
        Thread.Sleep(250);
    }
}

Sure enough, the Netduino was reading the voltage coming in from the Phidget push button:

The thread '<No Name>' (0x2) has exited with code 0 (0x0).
Input = 0
Input = 0
Input = 0
Input = 0
Input = 12
Input = 2097
Input = 4095
Input = 4095
Input = 12
Input = 0
Input = 4
Input = 3350
Input = 4095
Input = 4095
Input = 4095
Input = 28
Input = 0
Input = 4
Input = 0
Input = 0
Input = 0
Input = 4
Input = 0

 

I then wanted to compare the values coming in.  Since, the integer value I set in the analogInput.SetRange determines the min and max value, I need to calibrate to the expected values of the Phidget.  I swapped out the pressure sensor for the Slider Sensor so I could hold each value. Turning to the documentation, the range for the Slider control is 0 to 1000 so I jammed those values into SetRange.

Blog02

 

When I attached the Slider to the 8/8/8 Phidget Interface Kit (PIK), the values match exactly.  For example, putting the slider to the far right gives a value of 0, far left 1000, middle 500, etc…

 

image

However, detaching the slider from the PIK and hooking it up to the Netduino, I get a different value:

inputValue = 565

inputValue = 662

inputValue = 688

inputValue = 701

inputValue = 703

inputValue = 692

inputValue = 701

inputValue = 703

inputValue = 685

Not only that, when there is no sensor attached, I am still getting readings:

inputValue = 271

inputValue = 387

inputValue = 430

inputValue = 462

inputValue = 478

inputValue = 481

inputValue = 478

inputValue = 462

inputValue = 456

Something is wrong here.  When I push the slider to far right, I get 0, as expected:

inputValue = 0

inputValue = 3

inputValue = 0

inputValue = 0

inputValue = 3

inputValue = 0

but it about 75% of the way to the left, I get:

inputValue = 955

inputValue = 961

inputValue = 938

inputValue = 961

inputValue = 945

inputValue = 917

inputValue = 961

Interestingly, no matter what value I put as the top bound on SetRange, I am still getting a max out by 3/4 of the way:

inputValue = 9589

inputValue = 9550

inputValue = 9540

inputValue = 9384

inputValue = 9569

inputValue = 9384

inputValue = 9540

When you read the Technical Information on the Slider Control, it talked about the SensorValue is (Vin * 200) when you build your own analog to digital converter. I need to know the voltage reference and input voltage range.  Humm, sounds like I have to finally read the documentation

Unit Testing Netduinos

I got a basic Netduino over the weekend.  I created the basic “Hello World” example (make the on-board LED blink found in the Getting Started Guide).  I then tried to lift that code and load it into a test.  I thought about using a unit tests project with Rhino Mocks for unit testing and then an integration tests project for hooking up to the actual board.  To that end, I converted the Hello World Console Project into a Class Library Project.  I then renamed Main into something better and removed the static keyword.  I then added 2 test projects (one for unit, 1 for integration) and added a reference to the Netduino class project. 

The working code looks like this:

public class BoardLight
{
    public void LightBoardWhenButtonIsPressed(int interval)
    {
        OutputPort outputPort = new OutputPort(Pins.ONBOARD_LED, false);
        while (true)
        {
            outputPort.Write(true);
            Thread.Sleep(interval);
            outputPort.Write(false);
            Thread.Sleep(interval);
        }
    }

}

and the Integration Test looks like this (note the lack of an assert statement for the integration tests):

[TestClass]
public class BoardLightTests
{
    [TestMethod]
    public void LightBoardWhenButtonIsPressed_Success()
    {
        BoardLight boardLight = new BoardLight();
        boardLight.LightBoardWhenButtonIsPressed(250);
    }
}

I immediately got a warning like this:

Warning 1 The project ‘Tff.NetduinoExamples’ cannot be referenced. The referenced project is targeted to a different framework family (.NETMicroFramework)

A quick Bing search gives this post which is pretty close to the same problem.  MSFT’s answer makes sense to me:

Posted by Microsoft on 5/25/2010 at 11:28 AM

This is because it is not guaranteed that there will be runtime failures – it depends on which frameworks are installed on the deployment machine. If the deployment machine has the right list of framework families then your application will run fine, else there will be runtime failures. We are merely trying to warn you of the possibility of runtime failures on the deployment machine so that you can take appropriate action.

Undaunted, I attempted to add a reference from my test project to the class library, however, the reference wouldn’t take:

image

 

And not suprisingly, when I pressed on and hit CTRL+R+T to see my test, it failed:

Test method Tff.NetduinoExamples.Tests.Unit.BoardLightTests.LightBoardWhenButtonIsPressed_Success threw exception:

System.IO.FileNotFoundException: Could not load file or assembly ‘Microsoft.SPOT.Hardware, Version=4.1.2821.0, Culture=neutral, PublicKeyToken=null’ or one of its dependencies. The system cannot find the file specified.WRN: Assembly binding logging is turned OFF.

To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.

Note: There is some performance penalty associated with assembly bind failure logging.

To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].

Kinda intesting, looks like MSTest doesn’t “just work” when coupled with Microsoft Micro Framework.  I posted this to the Netduino board here, no responses yet…

In the meantime, the entire project setup got me thinking – ok you have a project for unit tests where you can use a mocking framework and then a project for integration tests so you can actually hook up the board and run. Going back to the mocking framework – what actually are you testing? All of the examples show a light going on or nothing. How do you mock behavior and not post-action state? Do I build a testing rig that somehow evaluates that the method ran (some kind og diagnoatic trace?)

Turning to Osherove’s The Art Of Unit Testing, I checked out his section called “testing for event-related activities” on page 124. He recommends creating an inline anonymous delegate hooked into the event call (in this case, view.Load) with a tracking variable (in this case, loadFired). This smacks me of unit testing for unit testing sake. You can just run the event and see if there is an exception raised for the same kind validation.

What I realized is that the Netduino API needs some kind of pre and post variables and I use to verify state after I call a given method. Since I am a whole 24 hours into the API, my hope is that I have yet to discover those properties so I can check pre and post state….

HTML5 24-Hour Trainer Book Review

I just finished HTML5 24-hour trainer.

clip_image001

About 85% of the book had nothing to do with HTML5 specifically. Rather, it was about learning basic HTML with some CSS thrown in. If you are complete novice in HTML, this is a good book to get you up and running. If you have coded in HTML for more than a couple of months, the book is of limited value. At the end, around chapter 26, Lowrey starts digging into HTML5 specifically and he presented the new tags well. Chapter 27 is particularly important because of the focus on HTML5 semantics. I think the book should be retitled “Learn HTML and CSS” with a byline of “including HTML5”.

On a side note, how excited are people that code for a living about HTML5? By reemphasizing java script and deemphasizing Flash/Silverlight, the amount of code for a Rich Internet Application will go up dramatically and require many more developer hours. Debugging massive amounts of JavaScript when a couple of lines of C# would have done the trick might seem frustrating, but at least the meter will be running…

Gitting Started

I wanted to put all of Panzer General Portable on GitHub. I started with putting Git on my machine:

image

Not knowing anything about Git, I opened up Panzer General Portable and in Solution Explorer, clicked on “New Git Repository”. Suddenly, I have all these yellow plus signs next to the objects in Solution explorer:

image

I the famous words of Tommy Boy “whadyado?”

I then hoped over to Github, set up an account, and followed the instructions to install Git itself:

image

Following the directions, I created a private key.

image

However, when I went to upload it to github, I ran into this little problem on my local file system:

image

The .pub extension is used by Microsoft publisher.

image

Doh!

I then opened with notepad – but didn’t change the file association….

image

Following the rest of the directions on GitHub, I created a Panzer General Repository:

image

And I also added .xap to extensions that are not sourced:

image

I then made a change in Visual Studio:

image

and opened Git-Pending Changes

image

Sure enough, the changes took.  However, I am missing the critical piece – I still can’t push my changes from the local Git Repro to the GitHub one.  I went back to the directions for VS and the directions for Git and looking at the screen shots, they assume that you have Git Extensions already installed.  Oooohhhh, back to Extension Manager:

image

I then installed (and picked check boxes with impunity):

image

I then could then get all of the GitHub commands in VS2010 (Push, Pull, etc…):

image

I then tried to push my local Repro to GitHib and I got the following message:

image

 

I then loaded the SSH Key (making sure that I used the key generator from the existing Key from GitHub setup):

image

And what do you know, the project is now on Github: https://github.com/jamessdixon/PanzerGeneralPortable

Primary Keys and Entity Framework

 

If you are creating EF classes from an existing database schema and one of your classes has a composite key of all of the fields like this:

image

You didn’t define a primary key in the database.  Once you go back and do that, your EF class will reflect it:

image