/mbLib

Library for micro controller (e.g. arudino, esp8266, esp32). Create menu for LCD. Helper functions to format text (time, integer, ...)

Primary LanguageC++GNU General Public License v3.0GPL-3.0

mbLib

Library for microcontroller projects with helper funtions and classes.

  • How to use it
  • MBMenu create menu for LCD
  • MBHelper read string from progmem, format time as string, ...

Demonstration and code explanation

Video: Tutorial

How to use it

  1. Go to arduino library folder and clone from github:
> cd myPathTo/Arduino/libraries
> git clone https://github.com/mchlbrnhrd/mbLib
  1. Open Arduino IDE and execute one of following example codes.
File->Examples->mbLib->menuExampleLCD
File->Examples->mbLib->menuExampleSerial
File->Examples->mbLib->menuExampleLCD_Counter
  1. Connect Arduino board (e.g. Arduino Uno), compile and upload code.
  2. Open serial monitor and type l for left, r for right, e for enter and x for exit. Typing m shows the menu. (Hint: After typing key press <Enter>)

MBMenu

Class to create menu for liquid crystal display (LCD). Designed for easy use. First setup menu by adding nodes with addNode method. There you set the text to display, the layer and a function ID (FID). To navigate in the menu just call right(), left(), enter() and exit(). Then you get the FID and you can take action by calling functions for example.

  • addNode(text, layer, functionID): add new menu entries/nodes
  • buildMenu(): menu is built
  • printMenu(): show complete menu via serial monitor (terminal)
  • left(), right(), enter(), exit(): navigate through menu

LCD menu

Once you implemented the basic code like in the following example it is easy to change the menu or add further nodes.

Example

Menu should look like:

entry layer
1. Foo 0
  1.1 FooA 1
  1.2 FooB 1
      1.2.1 Test1 2
      1.2.2 Test2 2
2. Bar 0
  2.1 BarA 1

Complete examples here: menuExampleLCD.ino and menuExampleSerial.ino

Summarized the main parts of the code:

#include <CMBMenu.hpp>

CMBMenu<100> g_Menu;

void setup()
{
  g_Menu.addNode(0, MenuFoo_pc , MenuFoo);
  g_Menu.addNode(1, MenuFooA_pc, MenuFooA);
  g_Menu.addNode(1, MenuFooB_pc, MenuFooB);
  g_Menu.addNode(2, MenuTest1_pc, MenuTest1);
  g_Menu.addNode(2, MenuTest2_pc, MenuTest2);

  g_Menu.addNode(0, MenuBar_pc, MenuBar);
  g_Menu.addNode(1, MenuBarA_pc, MenuBarA);
  
  const char* info;
  g_Menu.buildMenu(info);
  g_Menu.printMenu();
  printMenuEntry(info);
}

void loop()
{
  // ...
  int fid = 0;

  // determine pressed key
  KeyType key = getKey();

  // call menu methods regarding pressed key
  switch(key) {
    case KeyRight:
      g_Menu.right();
      break;
    case KeyLeft:
      g_Menu.left();
      break;
    // ...
    default:
      break;
  }
  
  // pint/update menu when key was pressed
  // and get current function ID "fid"
  if (KeyNone != key) {
    fid = g_Menu.getInfo(info);
    printMenuEntry(info);
  }

  // do action regarding function ID "fid"
  if ((0 != fid) && (KeyEnter == key) && (!layerChanged)) {
    switch (fid) {
      case MenuFooA:
        FooA();
        break;
      case MenuBarA:
        BarA();
        break;
      case MenuTest1:
        Test1();
        break;
      case MenuTest2:
        Test2();
        break;
      default:
        break;
    }
  }
}

Here terminal output example:

MBHelper

Helper functions:

  • void stringFromPgm(const char* f_StringPgm_pc, String& f_Value);
    get string from progmem (PGM)
  • void lcdCharArrayFromPgm(const uint8_t* f_Pgm_pc, uint8_t* f_Value);
    get char array from progmem (PGM)
  • void formatTime(unsigned long f_Seconds, String& f_Result);
    format time out from "seconds": h:mm:ss example: 135s => 0:02:15
  • void formatTimeMillis(unsigned long f_Milliseconds, String& f_Result);
    format time out from "milliseconds": h:mm:ss.ms
  • void formatInt(int f_Value, int f_Length, String& f_Result);
    format integer