Topic: tech dcc pc src prev next

tech dcc pc src > menu.h

#ifndef HERMES_MENU_H
#define HERMES_MENU_H

#include <stdbool.h>
#include <stdlib.h>

#include "millis.h"

typedef unsigned short int adc_type;
typedef unsigned char switch_type;

// The number of the switch pressed, whether for a long or short time.
#define HERMES_SWITCH_NO(sw) ((sw) & 0xf)
// The switch was pressed for a long time.
#define HERMES_SWITCH_IS_LONG(sw) (((sw) & 0xf0) > 0)
// The switch was pressed for a short time.
#define HERMES_SWITCH_IS_SHORT(sw) (((sw) & 0xf0) == 0)
// Convert a switch number to a long press duration.
#define HERMES_SWITCH_LONG(sw) ((switch_type)((sw) | 0x10))
// Convert a switch number to a short press duration.
#define HERMES_SWITCH_SHORT(sw) ((switch_type)((sw) & 0x0f))

// The 'null' button returned when no button is pressed.
#define HERMES_SWITCH_NULL 0
// The 'back' button to be used to go up to the previous screen.
#define HERMES_SWITCH_BACK 9
// The 'loco' button is used to select a locomotive.
#define HERMES_SWITCH_LOCO 10
// The button used to enter function mode.
#define HERMES_SWITCH_FUNCTION 11
// The button used to enter accessory mode.
#define HERMES_SWITCH_ACCESSORY 12

// Rotary encoder button codes.
#define HERMES_RE_SEL 13
#define HERMES_RE_CW 14
#define HERMES_RE_ACW 15

// To be called when a pin change interrupt is fired.  Reads from the rotary
// encoder rotation
void Menu_Pcint();

// Rescan the keypad (should be called often).
// Rescan the keypad by enabling each of the three read lines in turn and
// reading from each of the four read lines (giving 3*4 = 12 outputs).
void Menu_RescanKeypad();

void Menu_Finish();
void Menu_Redraw();

// Simulate a button press.  Useful to pass a button press from one view to another.
void Menu_FakeButton(switch_type sw);

/*
 * Run the menu event loop.  This loop monitors inputs for button presses,
 * calls the supplied draw function as required and calls the update function
 * as often as possible.
 */
void Menu_EventLoop(
        void(*sw)(void*, switch_type),
        void(*draw)(void*),
        void(*update)(void*),
        void *context
        );

/*
 * Display a number selector to get an integer using the rotary encoder.
 *
 * Parameters:
 *  question - String displayed on the top line of the LCD.
 *  def - Default value, returned if the user did not enter a value.
 *  max - Highest value that may be entered.
 */
int Menu_GetInt(const char *question, int def, int max);
bool Menu_Question(const char *question);

/*
 * Choose a value from a list.
 *
 * Parameters:
 *  question - String displayed on the top line of the LCD.
 *  option - Null-terminated list of null-terminated options for each value.
 *  def - Option selected when the list starts.
 *
 * Returns the index in 'options' of the selected option, or -1 if 'back' is
 * pressed (no option selected).
 */
int Menu_GetOption(
        const char *question,
        const char *options[],
        unsigned char def
        );

/*
 * Get the current current value to a precision of one byte.
 *
 * Parameters:
 *  current - unsigned char to write the value of the track current to.
 *
 * Returns true if the value was written to '*current', false otherwise.
 */
bool Menu_Current(adc_type *current);

/*
 * Draw a list view.  Intended for custom list views, which require more logic
 * than Menu_GetOption.
 *
 * Parameters:
 *  title - displayed at the top of the view
 *  text - text of this option
 *  index - number of the option in the list
 *  count - total number of options in the list
 */
void Menu_DrawList(
        const char *title,
        const char *text,
        unsigned char index,
        unsigned char count
        );

/*
 * Display a static notice.  The notice can be dismissed by pressing any
 * button.
 */
switch_type Menu_Notice(const char *text);

// Display a notice that will automatically be dismissed after a timeout.
switch_type Menu_TimedNotice(const char *text, millis_type timeout);

#endif