Thursday, March 24, 2016

Introduction to LCDs technology

This article gives an overview of the LCD technologies. It is a first step for who might want to design an embedded system with a color (touch) display.

How does it work


An LCD is an array of Liquid Crystal segments.
These segments are polarized and organized randomly when not in an electrical field.
When an electrical field is applied, the crystals align to the field and gate the amount of light
 than can pass through them.

Each LCD segment is aligned with an electric field.
A backlight is required to drive light through aligned crystal.

Types of display


There are passive (STN) and active displays (TFT).
They both consist of a grid of rows and columns electrical signals connected perpendicularly to every segment in the LCD.
An integrated chip controls which row and column are activated to enable/disable the segment at the intersection.

The passive screens apply a small bias current to row/column to generate a field in the intersection.
The active screens have a transistor per segment. This transistor is activated when the row/column of a segment is selected.
Color filters allow the generation of specific color (R, G or B) at a segment which means that a pixel consists of 3 segments.
TFT displays can drive 3 segments (or 1 pixel) per clock with levels of color that depend on the number of data lines (8 bits, 15 bits, 16 bits, 24 bits...).

LCD panel technologies for embedded display



From now on, this article will focus on TFT displays.


Common embedded screen resolutions


Resolution
Pixels
Diagonal
QVGA (Quarter Video Graphics Array)
320x240
3.5”
WQVGA (Wide Quarter VGA)
480x272
4.3”
VGA (Video Graphics Array)
640x480
5.7”
WVGA (Wide VGA)
800x480
7”
SVGA (Super VGA)
800x600
10.4”
XGA (extended graphics array)
1024x768
12.1” or 15”
WXGA (Wide Extended Graphics Array)
1280x800
12.1”


LCD Signals


The TFT LCD displays require these type of timing signals:

VSYNC: Vertical Sync. Used to reset the LCD row pointer to the top of display
HSYNC: Horizontal Sync. Used to reset the LCD column pointer to the edge of the display
D0...Dxx: Data lines. Typically, the encoding of R, G and B color levels.
LCDCLK: LCD Clock. Refresh rate.

The other common signals are:
LCD power signals
LCD backlight power
[Optional] Touchscreen signal (usually 4/5-wires for resistive, I²C link for capacitive)

Frame Buffer


The frame buffer is the memory allocated to store bit mapped image sent to the LCD. This data is used to periodically refresh the display.
The frame buffer can be calculated (Width pixels)*(Height pixels)*(Bytes for colors depth).
Example: For a 24bits SVGA the frame buffer size is 800*600*4 = 1.92 Mb. It gives an idea of the databus speed for a refresh rate of 60 Hz : 1.92*60 = 115.2 Mb/s 

Touchscreen technologies


The following array gives a quick overview of the different types of touch screens.


5-Wire resistive
Capacitive
Projected Capacitive
Surface Acoustic Wave
Infrared
Transmissivity/Clarity
Good
75-85%
Very Good
90-98%
Very Good
90-98%
Very Good
90-98%
Best
95-100%
Activate with any object
Best
Any object
Poor
Finger or capacitive stylus
Good
Same as capacitive + surgical glove
Good
Finger, gloved hand, soft/pliable stylus
Very Good
Most objects
High sensitivity
(Light touch)
Good
Very Good
Very Good
Very Good
Best
Accuracy
Very Good
Good
Best
Very Good
Very Good


Credits






Wednesday, March 2, 2016

Convert SQL Server Compact database (*.sdf) to SQLite3 (*.db)

To do this, you will need:


  • Visual Studio 2012 or 2013 (2010 would probably work too. However, it won't work with 2015 as Microsoft dropped the support for SQL Server Compact databases)

In Visual Studio:


  • Open your project (or create one) with Visual Studio.
  • On the left-side panel, open Data Sources and click on Add New Data Source -> DataBase -> DataSet
  • If your database is based on SQL Server Compact 3.5 or older:

Click on New Connection:


Make sure SQL Server Compact 4.0 is selected.
Click on Create and name a new database. Validate with OK.
Back to the wizard, click now on New Connection and Browse. Pick your database file.
Validate.


  • If your database is based on SQL Server Compact 4.0:


Click on New Connection then Browse and pick your database file.

You should now see your database in the Server Explorer (left panel).


After rebooting your Visual Studio, you should now see a new icon in the Server Explorer :



  • Click on the new blue icon.

Right click on your database (the original) and select the following entry:


Follow the instructions.

The internal scripts will generate a *.sql output with all of the instructions required to create the database back.

With SQLite3 tools:


  • Install the sqlite3 tools from here.
  • Open a command line (ensure that the location of the sqlite tools is in the path).
  • Finally type:

sqlite3.exe DB.db ".read db.sql"
(replace db.sql with the name of your generated sql file)

Tuesday, March 1, 2016

Windows IoT Core on Raspberry Pi 2

Pre-requisite

If you want to run Windows IoT Core on your RPI, you must have a desktop computer running Windows 10. The tools used to prepare the SD card cannot run otherwise.

Download tools

Go to : http://ms-iot.github.io/content/en-US/Downloads.htm
and download:

  • IoT Dashboard
  • Windows IoT for Raspberry Pi 2 (iso image)


Install the dashboard.
Open the iso image and run the *.msi installer.

Prepare SD card

Plug the SD card to your desktop computer.

As explained here, start the IoT Dashboard and follow the instructions (Set-up a new device -> ...).

Boot

Plug the SD Card to your raspberry pi and power it up. After a long minute, this is what you should see on the screen:



Select your language, and here your are, on your Windows Iot Core "desktop"




Windows IoT has no GUI stack and no command line prompt (but supports remote PowerShell connections). It is a minimalist operating system (or core) with Microsoft's Universal Application Platform (UAP) that is intended to run custom applications.


Write and deploy an App

Download Visual Studio 2015 Community and install it on your desktop computer.
During install, make sure to check the custom installation checkbox and select Universal Windows App Development Tools -> Tools and Windows SDK.

Start Visual Studio and Click on New Project.
Select Visual C# -> Windows -> Universal -> Blank Project.
Name it and click OK.

The Windows IoT COre image comes now without msvsmon.exe, the debugger listener required to deploy/debug on your raspberry pi. The first thing to do is to select "ARM" for the platform instead of x86 and "Remote machine" on the left. A popup will appear to select the remote device: normally, you should see your device in the list. Otherwise, enter its IP address or name.
Select Universal for the authentication method and click OK.
Now build and deploy your application. On the first deployment, Visual Studio will ship the debugger tools to your remote device. At the end of the process, your app should be running on your raspberry pi.

Note: Having a Windows 10 desktop computer is mandatory. You won't be able to deploy with an older version.
Note: Ensure that the "Update 1" is already installed with your Visual Studio setup (otherwise try to install from here)


Porting a VB.NET app to linux with Mono

I recently faced an interesting challenge : porting an existing VB.NET industrial application to linux. The code was running on Windows CE 5.0 until then with the compact framework 3.5.
This work was motivated by the desire of the company to reduce the costs of our future equipments. Knowing that my company sells no more than a hundreds of products per year, I came up with the raspberry pi solution.

Why the raspberry pi ?

Even though there are a lot of competitors now (banana pi, lemon pi, solidrun products...), the raspberry pi is still the most attractive:

  •  35 $/ unit
  •  Free to use for commercial purpose
  •  Huge community
  •  Lots of additional modules

What about the performances ?

The board that I try to replace is based on an ARM Cortex A5 (500 Mhz) with 256 Mb of RAM. It provides I²C, SPI, UART lines, a LAN interface, and comes with a 6" LCD touchscreen.

The raspberry pi 2 offers the same interfaces with a similar CPU clocked at a higher frequency (900 MHz). Recently, the raspberry pi has been offered an officiel 7" LCD touchscreen for only 80 $. Plus, the raspberry pi 2 has 1 Gb of RAM making it more than suitable for the repacement of the other board.

VB.NET

I am a big fan of C# .NET. I think it is an elegant and powerful language, very easy to learn, with a great documentation. Never used VB though. Fortunately, I found this:

http://www.harding.edu/fmccown/vbnet_csharp_comparison.html

and it is quite helpful for those coming from the C# syntax.

NET on linux

I've heard about Mono for many years but never experienced it. This was going to be the first time.
Recently, Microsoft started the .NET Foundation : an organization around an open-source, collaborative project for .NET technologies to make them portable.
One can now checkout the .NET source code and compile it on its machine. Does it mean that we can run .NET Core on the raspberry ? Not exactly. At this day, the .NET Core is only available with Windows 10 IoT Core. With linux, you only have one option: Mono.

For my porting work, I only used Mono.
The linux distribution I worked with is the now famous raspbian (Debian).

Install Mono

Installing Mono is straightforward as everything is explained on:

http://www.mono-project.com/docs/getting-started/install/linux/

Additionally,  I had to install the Mono VB compiler:

sudo apt-get install mono-basic

Note: I also tried to install Mono on my old raspberry pi 1, but the install failed (script error). After looking on the internet, it seems that the first version of raspberry pi is not compatible (or so they say...).

Porting : Step 1

Before checking out the project's repository on the rpi and trying to compile it, we need to ensure that it is portable. So I installed Xamarin Studio on my Windows desktop and I tried to compile the VB project for windows. I think everybody knows what happened : it failed... miserably.

The vs2008 vbproj file was not compatible with Xamarin studio so I had to create a new one from the IDE and inserted the existing code.
I added the same options (starting class, splash screen, ...).
I added the required references : fortunately, all of the assemblies were available for Windows but one (a third party assembly compiled for Windows CE used to display a fancy keyboard. Not critical).
I compiled .... about 1000 errors were signaled.

For most of them, they were due to missing Imports or non-declared variables (used in loops for example). The last ones were more annoying : OpenNetCF-dependent code, differences between Windows Forms and WinCE Forms, P/Invokes, .... It took me 2 days to correct them all. Once done, I found out that you can set some compiler options for VB : Explicit, Strict... Maybe turning them OFF immediately would have helped me. Anyway, it was too late and at least, the code gained in clarity.

At the end of this step, my code compiled successfully.

Porting : Step 2

The second step was to execute the code on my desktop computer. Once again.... failure ! Exceptions here and there.
Again, I found some parts of code where file paths were hard-coded (WinCE "/Hard Disk" root).
Following Mono advices, I used IO.Path.Combine wherever possible as well as IO.Path.DirectorySeparatorChar.
Other minor errors appeared but I corrected them easily and the application finally booted on my desktop. (Happy Face)

Note: Facing some unexplained NullExceptions, I had to debug my application with Visual Studio instead of Xamarin. VS gives you more details about the exception and stops right where they happened while Mono sometimes signals the exception from the main thread (this is particularly true when an exception occurs in a control handler or within invoked methods).

Porting : Step 3

This step is just an experiment really. Naively, I deployed the application on my raspberry pi with all the dependencies (I personnaly used scp for this) and ran:

mono myApp.exe

Well, the result was not disastrous as the splash screen appeared on the screen for a while. Unfortunately, the app stopped with a crash. The stack strace was not very explicit, indicating that a disposed object can not be used (somewhere in the internals, following Application.Run).


Porting : Step 4

The idea now, is to checkout the code on the raspberry pi and to compile it with Mono. For convenience, I used MonoDevelop:

sudo apt-get install monodevelop

Once checked out, open your vbproj project file with MonoDevelop. Normally, you should not see any error as we already clean them up in Step 1.
Make sure that you made a local copy of the third-party assemblies that the application needs. Reference them from MonoDevelop.

Ok, let's compile ! .... Failure again. But this time, the error is way too ambiguous to be interpreted :

Compiler crashed with code : 255



-_- ... I disabled Explicit, Stric, Infer options and tried again : same result. In MonoDevelop option, you can uncheck MSBuild option to force the compilation with vbnc instead of using xbuild, a clone of MSBuild. The build failed due to a bad syntax of the command, so I wrote my own vbnc command with all sources, references, resources specified and I experimented the same crash. The stacktrace was not referencing any file or part of code. And that was it, I could not get further due to a compiler limitation. I checked the vbnc compiler project and the last commit was 1-year old. Finally, with some basic console logtraces, I managed to find out that the crash was being caused by the database loading.

Porting : Step 5

The database, of course ! I did not think about it but working with SQL Server Compact database on linux is not possible. So I had to find a solution to convert the existing database (sdf file) to a more common and portable one. Check this post for a full tutorial.

After doing that, with a fresh and working SQlite3 database file, my application finally booted properly. However, another bug came out: the application was not responding to clicks (no event fired)...
I did not go through this one as I spent too much time already on what was supposed to be a demo project. If anyone has a solution for this bug, please comment.

Conclusion

Porting an existing VB.NET code to linux was a long journey that ended with a failure. The VB compiler provided by Mono is not rock-solid and can give you a bad time. C# seems to be better implemented and it is a shame that the existing code was not written with it.
Despite of the result, I consider Mono as an unbelievably complete solution. I think that the developers did a great job.
Now that the .NET Core is public and that a synergy exists with Mono/Xamarin, I am sure that someday, porting my code will be easier.

Porting the code to a linux environment was just a challenge really. I will now try to do the same with Windows IoT Core instead, the real successor of Windows CE. According to Microsoft, the Iot Core can run Win32 apps and knowing that my app was running on my desktop computer, I have good chances to see the app running on my rasperry pi.

EDIT: A few words on my quick test with Windows IoT Core. I overlooked a very important detail with .NET Core: the existing .NET Framework assemblies are incompatible with .NET Core which means that the third party assemblies that I was using in my application can not run on Windows IoT Core. As I don't have the sources of these assemblies, the porting of the application to Windows IoT Core is a dead-end.

 
biz.