7  Pushbutton

This chapter is based on the projectlet:

- 05_button

7.1 Projectlet Goals

Goals of this projectlet are:

  • Demonstrate the application of the toolkit library
  • Support the onboard pushbutton
  • Support an external pushbutton - which will require specifying and initializing the hardware
  • Reflect the state of the pushbutton (pushed/released) in setting the LED On or Off

7.2 Dual applications

This projectlet delivers the solutions in two different applications button for the onboard pushbutton and button_ext for an external pushbutton. The significant difference between the two are the hardware specification and the initialization required. The external application pushbutton is mapped to a particular GPIO pin. In addition the interface is configured to match the design of the pushbutton itself.

Listing 7.1: Connect and initialize external pushbutton
File : toolkit-button.adb

0004 |    procedure Connect_Ext_Button (point : GPIO_Point := Ext_Button_Point ) is
0005 |    begin
0006 |       Enable_Clock (point);
0007 |       Configure_IO (point, (Mode_In, Resistors => Pull_Up));
0008 |    end Connect_Ext_Button;

As indicated, the clock is enabled explicitly for this interface, and further configuration is required. While the mode Input is expected the pull_up resistor specification which depends on the pushbutton design and the available onboard digital circuitry is more involved and is outside the scope of this book.

The application itself is demonstrative:

Listing 7.2: External button application
File : button_ext.adb

0009 | procedure Button_ext is
0010 | begin
0011 |    stm32.gpio.Clear( stm32.board.Red_LED ) ;
0012 |    toolkit.button.Connect_Ext_Button ;
0013 |    loop
0014 |       if stm32.gpio.Set(toolkit.button.Ext_Button_Point) then
0015 |          stm32.gpio.Clear( stm32.board.Red_LED ) ;
0016 |       else
0017 |          stm32.gpio.Set( stm32.board.Red_LED ) ;
0018 |       end if ;
0019 |       delay 0.1 ;
0020 |    end loop ;
0021 | end Button_ext;

The button is monitored at 10Hz reflecting the state setting or clearing the red LED. Clearly the LED hardware has not been abstracted out but easily done. In the operation itself however, the application health LED is included automatically and the green LED blinks independent of the main loop which monitors the button state.