Sunday, May 2, 2010

More Generalities

So where to start when designing hardware objects? My approach has been to look around at what has been made so far, starting with the simplest and most common devices. For instance, lighting control. There is the ubiquitous light switch, can't get much simpler than that. In terms of objects and their properties:

Light.on = 1; // turns the light on
Light.on = 0; // turns it off

The next most common lighting controller is the dimmer. It might be implemented like this:

Light.brightness = 50; // set light brightness to 50%
Light.brightness = 0; // set light brightness to 0% (turn it off)

(and so on)

Clearly the "brightness" property can be used to implement the "on" property. Say you want to be able to set the brightness with a slider, and then switch the light on (to that brightness) or off, using a switch.

You could write some code:

int curSwitch = 0; // variable storing switch state, connect to switchChange() event
int curSlider = 100; // variable storing slider position, connect to sliderChange() event

void setBrightness()
{
Light.brightness = (curSwitch == 1) ? curSlider : 0;
}

void switchChange(int state)
{
curSwitch = state;
setBrightness();
}

void sliderChange(int percent)
{
curBrightness = percent;
setBrightness();
}

There is no problem per se, but the hardware object could easily be doing more of the work. By implementing the .on and .brightness properties separately, this same effect can be created by simply setting properties in response to change events.

void switchChange(int state)
{
Light.on = state;
}
void sliderChange(int percent)
{
Light.brightness = percent;
}

My point: when designing hardware objects the goal is not to eliminate redundancy to create a minimal object. The goal is to design an interface that implements common behaviours, offloading these from external processes.









No comments:

Post a Comment