Wednesday, April 16, 2008

Computer Dashboard, part 7 - Software

Back to Part 6

The journey is almost complete. Just one last link in the chain. We've covered everything that happens after the signal leaves the computer via the USB cable, but how do we send the appropriate information to the USB board?

The USB board comes with a software component that lets you set the values of the pins via any number of programming languages. I chose C# because I had Visual Studio and the component was compatible. Comprende? I wrote a little C# program that queries the Microsoft system software many times a second as to the status of the CPU, the memory usage, disk usage, and network usage, and also whether there are any new emails. Here's a code snippet:

// setup of adapters and monitors
currAdapter = "CPUPercentageAdapter";
currMonitor = "CPUMonitor";
((Adapter)adapters[currAdapter]).Device = meters[0];
currAdapter = "CPUYellowAdapter";
((BinaryAdapter)adapters[currAdapter]).OnAt = 50;
((BinaryAdapter)adapters[currAdapter]).OffAt = 85;
((Adapter)adapters[currAdapter]).Device = yellowlights[0];
currAdapter = "CPURedAdapter";
((BinaryAdapter)adapters[currAdapter]).OnAt = 75;
((BinaryAdapter)adapters[currAdapter]).OffAt = 110;
((Adapter)adapters[currAdapter]).Device = redlights[0];

I structured the software around an Adapter and Subscriber model - an Adapter talks to one of the devices on the dashboard, and it can Subscribe to a Monitor. So if the CPU meter Adapter subscribes to the CPU Monitor, it will show the current CPU status. You can see in that code that I'm also setting some threshold values, so the CPU yellow light comes on at 50% usage, and the red light comes on at 75% usage.

All the gucky bits where you actually play with bits and do math and stuff are tucked away in the Adapter and USBDevice classes, so at the top level, you just hook up an Adapter to a Device, and Subscribe to a Monitor, and it magically shows up on the dashboard. The picture shows all the components in the chain.


Kazilar said...

Warning! See Please Here

Iestyn Lewis said...

Ignore the spammer, please. I turned comment moderation on. What a pain.