Hi guys,
Being a complete software programming noob i've been struggling with the m2tklib for quite some time now. I've only experience with the more basic programming tasks but now i'm trying to implement a user interface using a graphic lcd screen for which I use glcd in combination with the m2tklib.
Because i'm loosing pins on the arduino at a rapid pace I decided to put five buttons on one analog input using different resistor values for reading the buttons out.
The problem I face right now is to connect the buttons to the actual program, i've found only one example in which the analog input is used for multiple buttons using the m2tklib in which I could scroll through a list of multiple chars. But, whenever I try to create a menu system (don't even know if i'm doing this right, because also the alignment is pretty bad) I am not able to navigate through the actual menu or go into one of the sub menu's.
Anyone who has experience with this and would like to help me out on this one?
Complete code I'm working with at the moment:
#include <glcd.h> // inform Arduino IDE that we will use GLCD library
#include "M2tk.h"
#include "utility/m2ghglcd.h"
#include "fonts/Arial14.h"
int read_buttons()
{
uint16_t adc_key_in = analogRead(5);
if (adc_key_in > 880 && adc_key_in < 900) return M2_KEY_SELECT;
if (adc_key_in > 757 && adc_key_in < 777) return M2_KEY_DATA_UP;
if (adc_key_in > 498 && adc_key_in < 518) return M2_KEY_DATA_DOWN;
if (adc_key_in > 224 && adc_key_in < 244) return M2_KEY_PREV;
if (adc_key_in > 826 && adc_key_in < 846) return M2_KEY_NEXT;
return M2_KEY_NONE;
}
uint8_t m2_es_arduino_analog_input(m2_p ep, uint8_t el_list)
{
switch(el_list)
{
case M2_ES_MSG_GET_KEY:
return read_buttons();
case M2_ES_MSG_INIT:
return 0;
}
return 0;
}
M2_EXTERN_VLIST(el_list1);
M2_EXTERN_VLIST(el_list2);
M2_EXTERN_VLIST(el_list3);
M2tk m2(&el_list1, m2_es_arduino, m2_eh_2bs, m2_gh_glcd_uffs);
void fn_Mode(m2_el_fnarg_p fnarg){
m2.setRoot(&el_list2);
}
M2_LABEL(el_label1, NULL, "Main Menu");
M2_BUTTON(el_Mode, NULL, " Mode ", fn_Mode);
M2_BUTTON(el_TimeSettings, NULL, " Time settings ", fn_Mode);
M2_BUTTON(el_Manual, NULL, " Manual ", fn_Mode);
M2_BUTTON(el_Favourites, NULL, " Favourites ", fn_Mode);
M2_LIST(list1) = { &el_label1, &el_Mode};
M2_VLIST(el_list1, NULL, list1);
M2_LABEL(el_label2, NULL, "Select Mode");
M2_ROOT(el_ok2, NULL, " Return to Main menu ", &el_list1);
M2_LIST(list2) = { &el_label2, &el_Mode};
M2_VLIST(el_list2, NULL, list2);
M2_LABEL(el_label3, NULL, "Time settings");
M2_ROOT(el_ok3, NULL, " Return to Main menu ", &el_list1);
M2_LIST(list3) = { &el_label3, &el_TimeSettings};
M2_VLIST(el_list3, NULL, list3);
M2_ALIGN(top_centered_toplevel, "W64H10", &el_list1);
//const char * C1 = "Mode";
//M2_BUTTON(mode_button, "C1", &C1, bm_return_to_last_menu_cb);
//M2tk m2(&top_el, m2_es_arduino_analog_input, m2_eh_4bs, m2_gh_glcd_uffs);
//M2tk m2(&el_list1, m2_es_arduino_analog_input, m2_eh_6bs, m2_gh_glcd_uffs);
void setup() {
m2.setFont(0, m2_System5x7 );
}
void loop() {
m2.checkKey();
m2.checkKey();
if ( m2.handleKey())
m2.draw();
m2.checkKey();
}
I'm not sure but i'm suspecting I need to make a change to this part (uint8_t el_list) in order to make a connection between the keys and the menu. But I could be wrong offcourse (that would be very plausible I guess).
uint8_t m2_es_arduino_analog_input(m2_p ep, uint8_t el_list)
{
switch(el_list)
{
case M2_ES_MSG_GET_KEY:
return read_buttons();
case M2_ES_MSG_INIT:
return 0;
}
return 0;
}
Many thanks in advance!