-
Notifications
You must be signed in to change notification settings - Fork 11
t02lc
olikraus edited this page Oct 24, 2016
·
2 revisions
This tutoral will show how to read and process input from the user.
This is how m2tklib
works:
- Detect a user input and convert the input into an internal
m2tklib
"event". This step depends on the hardware. - Process the "event". This is independent from the hardware
- Display the result (see previous Tutorial)
All three steps are handled by "callback" procedures.
-
Event Source: Translate Hardware events into
m2tklib
"events". Within the Arduino Environment this is usuallym2_es_arduino
(Others arem2_es_arduino_rotary_encoder
orm2_es_arduino_serial
). -
Event Handler: The event handler defines which and how events are handled. Bigger event handlers like the
m2_eh_6bs
can handle many events (hardware buttons) but also need more memory than small event handlers like them2_eh_2bs
which supports two buttons. In principle all event handlers can be used with all input fields. But if there is only a very limited number of hardware buttons, than it might be less comfortable for the user to enter data. On the other hand, sometimes it is required to build really small devices with only a view number of buttons. -
Graphics Handler: The graphics handler allows
m2tklib
to operate independently from the underlaying output device. For each supported output device, there is at least one graphics handler. Additionally the graphics handler defines the style how the user input fields (elements) are displayed. For theLiquidCrystal
Library there is currently only one handler:m2_gh_lc
(see complete listhere).
The example uses the two button handler m2_eh_2bd
:
M2tk m2(&el_num, m2_es_arduino, m2_eh_2bd, m2_gh_lc);
The setup part connects the pins of the Arduino hardware with the events:
m2.setPin(M2_KEY_SELECT, 4);
m2.setPin(M2_KEY_NEXT, 3);
m2.setPin(M2_KEY_PREV, 5);
m2.setPin(M2_KEY_EXIT, 2);
This is the complete example (U32Plain
). The user can enter an unsigned 32bit variable (M2_U32NUM
-Field).
#include <LiquidCrystal.h>
#include "M2tk.h"
#include "m2ghlc.h"
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
uint8_t uiKeySelectPin = 10;
uint8_t uiKeyNextPin = 9;
uint32_t number = 1234;
M2_U32NUM(el_num, "a1c4", &number);
M2tk m2(&el_num, m2_es_arduino, m2_eh_2bs, m2_gh_lc);
void setup() {
m2_SetLiquidCrystal(&lcd, 16, 2);
m2.setPin(M2_KEY_SELECT, uiKeySelectPin);
m2.setPin(M2_KEY_NEXT, uiKeyNextPin);
}
void loop() {
m2.checkKey();
m2.checkKey();
if ( m2.handleKey() )
m2.draw();
m2.checkKey();
}
There are two more procedures in the main loop
:
-
m2.checkKey()
: Debounce and detect hardware buttons. Translate hardware events into internal events and put them into an event queue.checkKey()
should be called as often as possible. -
m2.handleKey()
: This procedure will process one event from the queue. A non-zero will be returned if a refresh of the display is required. The first call to this procedure will return a non-zero value.
- Proper setup of
m2tklib
requires three callback procedures:-
Event Source: Translates hardware events (button press) to internal events. This is always
m2_es_arduino
for the Arduino Envornment. -
Event Handler: Execute the event. Use
m2_eh_2bs
if there are two buttons connected to the Arduino Board. -
Graphics Handler: Depends on the output device. Use
m2_gh_lc
for theLiquidCrystal
library.
-
Event Source: Translates hardware events (button press) to internal events. This is always
- Calls to
m2.checkKey()
will read and translate hardware events. - Calls to
m2.handleKey()
will modify the content of the menu.
- Previous Tutorial: Tutorial 1: Hello World
- Next Tutorial: Tutorial 3: User Interface Design
- Wiki Start Page