Patrick Bader's Blog a blog about software development

21May/113

ScreenBrightness V2.1

I updated my Screen Brightness Tool to version 2.1. You can now specify a command-line parameter to set the brightness directly. When a parameter is specified the tool will be closed immediately after setting the brightness. Any previously running instance of the tool will not be closed.

You can download the new version here or from the projects page.

Have fun with the new feature! Feedback is welcome.

Filed under: Uncategorized 3 Comments
18Apr/111

Negating Booleans in C#

I was recently asked whether there is a short way to negate the value of a boolean variable. Surely you can simply asign the result of the negated variable to itself:

bool a;
[...]
a = !a;

So what is the problem here? Absolutely nothing of course. But now think of some common use case, for example in UI code:

MyForm.MyCheckbox.Checked = !MyForm.MyCheckbox.Checked;

Now, can you think of a way in C# to only write the variable name once?

There is indeed a way using compound assignment operators. The following code does the negation:

MyForm.MyCheckbox.Checked ^= true;

Well, I admit that this is not the clearest solution to the problem of negation, but it surely solves it. There is also an advantage or difference between this code and the initial. In the initial code "MyForm.MyCheckbox.Checked" will be evaluated twice and in the latter only once. Eric Lippert already wrote about this in his Blog.
Whether you will be using this obscure syntax or not is up to you. I for myself favour code where the intentions of the author are obvious which clearly is not the case here.

Tagged as: , 1 Comment
9Dec/100

foreach and lambdas in C#

I recently encountered a bug in a C# application I was writing and it took me quite a while to fix. The situation is as follows: I had an array with some kind of data I wanted to be processed by a thread pool. So my first approach was iterating over the array with a foreach loop and passing a lambda to the thread pool as argument. The code looked like this:

1
2
3
4
5
6
7
8
9
object someState;
int items[] = {1, 2, 3, 4, 5};
 
foreach(int item in items)
{
	ThreadPool.QueueUserWorkItem((unused) => {
		System.Console.WriteLine(item);
	});
}

so, what's wrong with this code? Without the thread pool, nothing at all, it runs just fine, but with the thread pool the following text was written to the console:

5
5
5
5
5

When I saw the output, I was like: "what the hell is going wrong here?". The problem with the code above is: lambdas in C# capture their variables by-reference. For each iteration of the loop, a new value will be assigned to the item variable, an since this variable is captured by the lambda by-reference, all WorkItems in the thread pool will be influenced by the assignment of the loop. In my case the loop run completely, before the first WorkItem was executed. So the value of item was always the value which was last assigned by the loop.
A quick fix for this was assigning the loop variable to a local variable in the for loop:

1
2
3
4
5
6
7
8
9
10
object someState;
int items[] = {1, 2, 3, 4, 5};
 
foreach(int item in items)
{
        int localItem = item;
	ThreadPool.QueueUserWorkItem((unused) => {
		System.Console.WriteLine(localItem);
	});
}

so, each lambda references its own local variable. Another solution is to pass the item explicitly to each WorkItem:

1
2
3
4
5
6
7
8
9
10
object someState;
int items[] = {1, 2, 3, 4, 5};
 
foreach(int item in items)
{
	ThreadPool.QueueUserWorkItem((localItem) => {
		System.Console.WriteLine(localItem);
	},
        localItem);
}

The disadvantage here is: You have to make the variable explicitly available in the lambda, by passing it as a parameter.

Side note:
The upcoming C++0x release will also contain lambda expressions but it will allow you to specify whether a variable shall be bound by value or reference.

Filed under: C#, Uncategorized No Comments
12Sep/103

Master Thesis Presentation [2nd Update]

This time a posting in German since the presentation will be held in German:
Am Dienstag den 14.09.2010 werde ich eine Präsentation über meine Master Thesis mit dem Titel: "GPU-unterstützte Bildverarbeitung und Bilderkennung
im Kontext einer Multi-Touch-Anwendung" halten.
Der Vortrag findet an der Hochschule der Medien in Stuttgart statt.
Beginn ist 10:30 Uhr und der Vortrag wird ca. 45 Minuten dauern.
[Update]Das ganze findet in Raum 135 statt.[/Update]
Interessierte sind herzlich eingeladen teilzunehmen.

[2. Update]Der Vortrag ist als Videostream hier abrufbar.[/2. Update]

7Sep/102

it’s about time…

I finally finished my Master's Thesis about GPU based image processing in the context of a multi-touch application. As I wrote in older posts I have built a prototype of an LCD based multi-touch screen. The PlayStation Eye camera is used to track blobs and fiducials using IR light and the images from the cam are processed on the GPU to extract positions, IDs, etc. For the implementation which will possibly be published in a later post, I used OpenCL, which allows programming on various heterogenous hardware. The visualization is done using solely OpenGL 3.2 Core Profile.

The thesis in in german and can be downloaded here. Feel free to read and comment it.

29Jul/100

ScreenBrightness v2

I finally updated my ScreenBrightness tool to fix some crashes when running at startup.
The new version can be found in the Projects page.
Feel free to give some feedback!

Filed under: Uncategorized No Comments
23Jan/1023

Adjusting screen brightness with shortcuts

A while ago I installed Windows 7 on my Sony laptop. The good thing about it is it runs quite stable and most things just work. The bad thing some of the shortcuts with the blue "Fn" key don't work anymore. Adjusting the volume works perfectly fine, but setting the brightness of the LCD backlight does not.
Since Sony is not going to release any Windows 7 64 bit drivers for my model, I finally decided to write a little programm myself. This took several hours of searching for the right API to use. There are actualy three different ones:

  1. The first API I tried is called the Monitor Configuration of the Win32 API but did not work for my laptop (some kind of I2C transmission error occured)
  2. The Backlight Control Interface using IOCTL was the first approach that worked for me, but getting the current brightness is not supported on newer Windows versions
  3. So I ended up using WMI which is available for .NET and works surprisingly quite well.

Further usage information and a downloadable binary can be found on my Projects page
If you encounter bugs or have any suggestions please do not hesitate and give some feedback.

2Dec/090

Bandpass Filtering and LCD Testing

As promised here are some results of a test session with the LCD. I ordered a bandpass filter for 850 nm near-IR light for the PS3 camera a while ago at ebay. The filter blocks all light except in a small range around 850 nm wavelength.

I am trying to build a multi-touch screen with the DSI technique, explained here but instead of using a projector and some special projection surface I will be using the recently disassembled LCD.

So why not using a projector? The answer is quite simple: I don't own one ;) But there are some other facts that lead me to try it with an LCD:

  • Projectors need quite a lot of distance from the projection surface but I am not intending to build a multi-touch table.
  • Even HD projectors which are quite expensive do have very limited resolution in relation to the size of the projection surface. So small text can hardly be read on such a table.
  • I wanted to build something smaller so one person is able to reach the whole screen from one location.

There are basically two issues with the LCD approach:

  1. The camera is located behind the LC-panel thus has to track blobs through the panel which normally is covered by the backlight (with a white opaque layer) and some metal. This directly leads to the second problem.
  2. The backlight has to be replaced with one that is itself transparent.

My first approach was to use Plexiglass Endlighten for both backlight and IR illumination but I was not able to detect any blobs with the Plexiglass behind the LC-panel. So I bought another Plexiglass Endlighten which is located in front of the LC-panel for blob tracking purposes only.

I took some picture to test whether IR light is blocked on dark regions on the LCD image as visible light is. The results were quite surprising, the visibility of IR light is not affected by the picture shown by the LC-panel at all as can be seen below.

Display with normal Backlight on

Display with backlight on.

Backlight and IR lighting on

Backlight and IR lighting on

Only the IR lighting is on.

Only IR lighting on.

Both backlight and IR lighting on but with bandpass filter on top of the camera.

Both backlight and IR lighting on. Bandpass filter on top of the camera.

My next step will be to build a prototype and experiment with different types of LEDs for a brighter backlight.

Filed under: Uncategorized No Comments
19Nov/092

LCD Disassembly

Quite some time passed since my last post. If you were wondering what I was doing in the meantime and why news took so long, read on.

After the successful testing in my last post, I wanted to try the whole thing on with an LCD. The problem with that was how to get one. Luckily my brother has an old Sony SDM-S71R with 17" which is not needed anymore. It's not really big, but for a first test it will do.

Disassembling the whole thing was quite easy, only the first part, getting the plastic casing off, was a bit tricky. If you are unsure how to do it, search online for 'Sony SDM-S71R Service Manual'. After removing some plastic and metal casing two circuit boards will be revealed. Both of them shown below:

Power supply of the Sony LCD

Power supply of the Sony LCD

This one is the power supply for both the electronics controlling the LCD and the four fluorescent tubes for the backlight. Since I do not want to use the original backlight I do not need the high voltage part. That made me look for some alternative to the power supply. The pinout of the connection to the lcd controller is described directly on the board and is shown below:

Wiring of the connector from the LCD power supply to the controller

Wiring of the connector from the LCD power supply to the controller

The purple, grey, white, and black cables on the right need not be connected. So there are only the +12V, +5V, and ground wires left. These voltages are also used in computers, so the choice was easy: I used the power supply of an old PC.

Next is the controller board:

lcd_controller_board

Nothing exciting here aside from the many wires on the right that go to the display itself.

The display consists of the LC layer on top of a glass plate and the backlight with some plastic films between them. Again some pictures of the parts ordered top-down:

LC Layer with controller

LC Layer with controller

Diffuser with some other plastic films on top of a plastic panel and a white paper

Diffuser with some other plastic films on top of a plastic panel and a white paper

Fluorescence tubes that shine from the side into the plastic panel

Fluorescence tubes that shine from the side into the plastic panel

These are the most important parts of the LCD. If you have any questions about the disassembly, just ask.

So much for the LCD disassembly. I've already done some testing with it in combination with a self-made backlight, some IR LEDs, and a bandpass filter for the camera. But this will be the topic of the next post,...

3Nov/091

Blob Detection Reloaded

Today, I finally got the 50 Osram SFH 4350 LEDs I ordered. These shine at 850 nm, a wavelength at which the PS3 Eye is quite sensitive. The first thing to do with these LEDs was to test them somehow. Since I haven't got an IR bandpass filter for the cam (I ordered one from ebay), I had to test them without the filter.

So how did I run the test? A few days ago I ordered a sample of some Plexiglass EndLighten, which is available here. It's a special glass that shines diffuse when light shines from the side into the sheet. The final setup was simple: I connected 8 LEDs in row with a 1 ohm resistor and powered the setting with 12 Volts, the LEDs were attached to the Plexiglass and and everything was filmed with the cam.

A Screenshot with blobs detected on my hands is below, the other blobs in the surface come from a screw clamp.

blob_detection_hand

stay tune for some more news in the near future ;)