Sunday, May 26, 2013

SeeedStudio TFT v2...

Finally had a chance to sit and play with the SeeedStudio TFT v2.0.    I will be playing with this device a bit more for but for now I was able to examine the their API and run through a few of their demo application.

First a look at some of the sample applications.  I looked at two of them.  The first is the "paint" program.  It demonstrates the basic functionality of the TFT display.  The first mistake I made that is that the library provided by SeeedStudio actually comes in two parts.  The first part is called "SeeedTFTv2" - upon attempting to build the "paint" it was clear that I also needed to download the "SeeedTouchScreen" library to enable any of the touch functionality.  I was a little perplexed by this, it is something that I have to dig into a bit more as it seemed as if all the functionality to get user input from the screen is located in the "SeeedTouchScreen" library.  While the "SeeedTVTv2" contains the code the write to the display.


Not to be thwarted (despite this curiosity) I pressed forward.  As shown above you can see sample program as run on an Uno device.  In order to draw anything resembling a line the user will have to take their time and slowly draw the line across the screen.  I can't blame the library or the screen - this is more of a function (it seems to me) that checking the screen press occurs with each clock tick on the Arduino.

I then went on took look at the "shapes" sample program.  Show below.


I adjusted this program so that when the screen was pressed rather than draw the circles from the inside to the outer edges it would draw them from the outer edge to the inside.  Again, one must press for a bit until the program registers the press and action changes on the screen.  Below you can see my changes.  The "Point" object is only available from the "SeeedTouchScreen", it collects to the "z" value - or the pressure of the finger press.



So I am left wondering how best to use this device.  I need to see if this device will generate interrupts to make the screen more responsive to user inputs or I might have to adjust the pressure to see if that will increase the responsiveness.  As it stands I am not sure I could use this as a means to quickly stop a function or motion of robot that needed to stopped "immediately".

The graphics are reasonable for a screen this size and offer up a decent quantity of (65K) of color choices in a  320x240 pixel display area. 

Saturday, May 18, 2013

Maker Faire and the next Arduino project...

There's been a lull in my Arduino programming as of late.  Seems the last time I had some passion around building something with that platform was about 3-4 months ago when I purchased a number of parts to build a dual-axle controlled track system using the Adafruit motor shield.  While this is still on my workbench I got frustrated with Tamiya dual transmission kit - mine seems to have a problem where one axle spins more freely than the other.  This, if you have guessed, will cause the track system to "pull" in one direction, much like when your car is out of alignment.  Since I wasn't using a stepper motor I couldn't correct the problem with micro adjustments to align the tracks in the direction I wanted to go.  I am not even convinced I could do this without a compass censor and a stepper motor.  I will take a look at this in a few weeks and see what can be done about it.  In the mean time I managed to find something more interesting to do - something I found at this weekend's Maker Faire.


Yes I am aware TFT displays aren't something new.  But this one looked interesting.  It pushes off the processing of the display from the main CPU and provides a SPI interface. This prevents burning up a number of pins on the Arduino - which for some projects can be a nice feature.  I was able to get this one from Seeed Studios for a reasonable price and it seems to be well supported by the vendor.  In fact the reps from Seeed Studios brought with them a working GPRS cell phone built on the Arduino platform that used this screen for the key pad.  What was even more wild was they built an enclosure for this project using a 3D printer (yea, a 100% contract free, open hardware, open source, cell phone).  I haven't yet decided what to do with this screen - maybe an easy game like Simon or Tic-Tac-Toe? 


Wednesday, May 8, 2013

Why use System.Runtime.Caching.MemoryCache?

I ran across a problem this week while putting in the finishing touches of a data access layer for a series of WCF services.  These services needed to pull information from an IBMi (AS/400) "database" (e.g actually the pre-1980's file system).  I won't go too far into the problem, but the need was I had to cache information regarding what environment and/or library to invoke the program running on the IBMi.  This information I decided was likely best stored in a type Dictionary<string, string> - as I could look up the name of the program and easily retrieve the library that I needed be in to invoke the program.

So then, how best to cache this information between successive calls to a WCF service?

Well, as it turns out the IIS worker process in version 5.1 and greater is quite handy.  So long as the application pool isn't recycled either from a time-out or by manually recycling the pool the object you define as your ServiceContract will remain in memory.  Meaning any object bearing the static definition within this object will remain, well, static.

So my first thought on solving this problem was to simply define a Dictionary<string, string> object as a private static member of my ServiceContract class, load it up, and then use it within any object that my ServiceContract class needs into order to its work.  Basically something like this.

 public class Service1 : IService1  
 {  
   private static Dictionary<string, string> myDictionary = new Dictionary<string, string>();  
   
   public string GetData(int value)  
   {  
    Service1.myDictionary.Add(myDictionary.Count.ToString(), value.ToString());  
    return string.Format("You entered: {0}", value);  
   }  
 }  

See any problems yet?

If your guess was that in order to use the static "myDictionary" object I would need to carry the object to each and every object that requires access to the list of the items within the dictionary then you can immediately see my folly.  If you aren't planning on a having many (or any) helper objects then this really isn't a big deal.  The problem I faced was that the ServiceContract object  of the WCF services I was coding for was about 8 or 9 layers above my data access layer.  I was really going to be popular changing all the objects between the top most ServiceContract and my object - especially since ALL of the objects didn't really care about or need the contents of the Dictionary class.

Enter the MemoryCache

MemoryCache is an object located in the System.Runtime.Caching assembly.  This object was added a few years ago with the release of .NET 4.0.   It never really caught my attention until this problem came up - but essential it is a way to store away any objects you might need someplace else.  Or put another way, if you are an old C programmer (like me), it is a way you can tuck away global variables (or objects in this case) for reuse in other places in your application.  The API for this object can be found here.  Also a quick search on this object will provide some information and other sample code on its use.  The purpose here really isn't to describe the API but rather to provide a practical example of its use.

So on we go with my first demo project that began using the MemoryCache object.  My first set of changes really just swapped out the Dictionary object with the MemoryCache object.  While I didn't solve my problem identified above, this step provided me with some understand of how this object worked as I was successful in creating, storing, retrieving, and updating the Dictionary object with successive calls to my test WCF service.  Below outlines the changes I made to my "GetData" method.  

 public class Service1 : IService1  
 {  
   private static ObjectCache myCache = MemoryCache.Default;  
   
   public string GetData(int value)  
   {  
    Dictionary<string, string> myDictionary = (Dictionary<string, string>)Service1.myCache.Get("ALIST");  
   
    if ( myDictionary==null )  
    {  
      CacheItemPolicy policy = new CacheItemPolicy();  
      policy.Priority = CacheItePolicyPriority.Default;  
      myDictionary = new Dictionary<string,string>();  
      Service1.myCache.Set("ALIST", myDictionary, policy);  
    }  
   
    myDictionary.Add(myDictionary.Count.ToString(), value.ToString());  
   
    return string.Format("OK");  
   }  
 }  

I also added a new method that retrieved a complete list of Dictionary items to the client so I could keep checking if the MemoryCache (and its embedded Dictionary object) would retain the information I needed to keep around.

 public List<Avalue> GetAllCalls()  
 {  
   List<Avalue> values = new List<Avalue>();  
   
   Dictionary<string, string> myDictionary = (Dictionary<string, string>)Service1.myCache.Get("ALIST");  
   
   if ( myDictionary != null)  
   {  
    foreach(var pair in myDictionary)  
    {  
      Avalue aValue = new Avalue() { CallTime = pair.Key, CallValue = pari.Value };  
      values.Add(aValue);  
    }  
   }  
   
   return values;  
 }  

So once this second version of my prototype was working I went to see if another object, which would be put in and out scope during another method call of my service, could get a handle to the same Dictionary object and update it for me.  If this could work then I could avoid having to change all the objects between my data access layer and the ServiceContract object.

In order to test this I created another object within the WCF project called "DoSomething"  which is defined below.

 public class DoSomething  
 {  
   private ObjectCache doSomethingCache;  
   
   public DoSomething()  
   {  
    doSomethingCache = MemoryCache.Default;  
   }  
   
   public void DoIt(int value)  
   {  
    Dictionary<string, string> myDictionary = (Dictionary<string, string>)doSomethingCache.Get("ALIST");  
   
    if ( myDictionary!=null )  
    {  
      myDictionary.Add("DoSomething " + myDictionary.Count.ToString(), "DoIt" + value.ToString());  
    }  
   }  
 }  

You'll notice that within the constructor of this object it is obtaining a handle to the MemoryCache object.  And then it uses this handle, within the DoIt method, to pull out a Dictionary object where it then adds an entry.  You'll also notice that the handle to the MemoryCache goes out scope along with lifespan of this object.

The final version of my service adds a new method called SetNewItem.  This method creates a DoSomething object, calls the DoIt method and returns, causing the DoSomething object to go out of scope and eventually get picked for garbage collections.

 public string SetNewItem(int value)  
 {  
   DoSomething something = new DoSomething();  
   
   something.DoIt(value);  
   
   return ( "OK");  
 }  

You then see that I didn't need to pass around the Dictionary or the MemoryCache object and that the DoSomething object appears to be capable of obtaining a handle of Dictionary object.

So, the question is did it actually work?  Did the MemoryCache manage to stay around between calls?  Was the Dictionary object populated correctly?  The answer if is course yes.  Below is a screen shot of the WinForms application which calls the Service1 web service.  I invoked the GetData method a few times as well as the SetNewItem method.  Each of these calls were given a random number between 0 and 42 by the client.  After calling these methods a number of times I requested a full list of the contents of the Dictionary object to display within a list box on the screen.


As illustrated above you see can that the Dictionary object is alive and providing me a cached list of items that I can pull from pretty much any place within my running process.  Once the application pool was recycled I pulled down the list again.  However, because the pool was recycled there were no more entries in the Dictionary.