Embedded Systems October 2000 Vol13_11

Issue link:

Contents of this Issue


Page 80 of 181

Over the years, I have found Ada code to be readable, reusable, and maintainable. Although these same code features can be achieved in other languages, Ada95 establishes a higher minimum standard in each of these areas. address Regi s t er .Write (My_Register, Va Lue); Of course, the Register class doesn't really provide a vcr'y useful abstraction by itself. Any code that creates and uses a Regi ster still has to know the actual address of the under- lying register and the meaning of each value that can be written to or read from il. But the good thing is that Ada, unlike C++, allows the pro- grammel- to separate the OOP primi- tive of encapsulation from that of inheritance. So the overhead is at least minimiLed here. In order to show the value of absu-acting beyond this point, let's con- sider the simplest embedded pro- gram-what Mr. Barr de 'cribed as the embedded version of "Hello, World" in the opening chapter of his book-one that Llashes an LED. As you' ll see, even this simple program can benefit from an object-oriented implementation. To make a device like an LED more useful, we need to take the lIcxt step and hide the addresses of its registers and the ~pecific values read from and wriuen to them. Our goal is to sepa- rate the functionality of the device from the implementation detail>. What is the function of an LED? It is a light, that is either on or off'. As an application programmer, we would like to be able to turn it on and ofC preferably without including any magic numbers (addresses or register values) in our code. Listing 2 ~hows the definition of a class that presents ju~t such an abstract interface for an LED. Ol cour~c, tuming any LED on and 011 will probably illvolve a register. BlIt no code outside the ~cope of the L ight cla~s lIeed worry about such details. Nul' ~hould other code be affected if those details change on the next revision of the hardware. The resulting reduction in module cou- pling i ' a major advantage of OOP. The end result of all this is an object-oriented program that is not overly complex, yet accomplishes the useful purpose of separating the func- tion and use of the device fl'Om hard- ware details that may change in the future. One of the more imeresting side effects is that we could easily extend this Light class, through inher- itallce, to support multi-color LEOs (say, red, green, and blue in a single package) 01- an eight-segment display. If you look at Listing 3 you can see how the complexity is hidden within the implementation. Listing 4 shows how simple it becomes to usc this c1as '. Benefits of Ada Over the years, 1 have found Ada code to be readable, reusable, and maintain- able. Although these same code fea- tures can be achieved in other lan- guage~, Ada95 establishes a higher min- imulll standard ill each of 111ese areas. 1t'~ a fact that people maintain code and, for that reason, mu~t read it many more times than it is wrillen. Even without comments or careful coding, 1 think the previous examples show the readability inherent in the Ada95 language. Similarly, it should be clear that the Light c1as~ could be L1sed in a vadety of ways on a variety of hardware platforms. If platforllls have a single memorY-lIlapped register to control the LED, the only nece~sary changes are the address and values. By keeping the applicatioll software awa)1 frolll the direct man ipulation of regis- tel~, we are able to e n~urt' that tht' c1iellt code can be rell~ed alld 0111)' the implemclltation of our class lila), ha\e to change. The object-oriented principles of encapsulation, abstraction, modulari- ty, and hierarchy lay the foundation for good programming. Ada95 takes advantage of these principles and enforces them: • /c.'/I,wjJsulation. Encapsulation is what keeps the application code from knowing or needing to know the implementation details of the class. This means that when you change an address or a way of doing things, you don't have to change every piece of source code that depends on the subprograms that are in your interface. Encapsulation in the above exam- ple i implemented by declaring the type Light.Object as private. No client can then access the values in that record directly • Ab~traction. When I woke up this Illornillg, I tied my shoes. Long ago, 1 learned how to make my hands work tOgether to tie my shoes. Before that, I learned how to make my hands work at all. The idea that I don 't have to think about allythillg but tying my shoes b abstraction. In the above exam- ple, we can think of turning the LED 011 and off. We don 't need to thillk of setting vallles in a register • Modulari.ty. Modularity is the ability to break a problem or its solution into smaller pieces. The classes in object-oriellted programming lan- guages allows us to do this. In Ada95, we specifically use packages alld can go one step further and use child package~ • Ilil'l'Wthy. Hierarchy is the idea of al ... anging e1elllcllts of a problem into a lIeelike structure. For' exam- ple, a digital di~play output may be built using several LEOs. In this case, you could combine them Embedded Systems Programming OCIOBER 2000 79

Articles in this issue

Archives of this issue

view archives of EETimes - Embedded Systems October 2000 Vol13_11