Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for inspection of menu state #42

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions src/GEM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,72 @@ void GEM::setMenuPageCurrent(GEMPage& menuPageCurrent) {
_menuPageCurrent = &menuPageCurrent;
}

bool GEM::isEditing() {
return _editValueMode;
}

int GEM::availableOptions() {
if(_editValueMode) {
switch (_editValueType) {
case GEM_VAL_INTEGER:
case GEM_VAL_BYTE:
// position 0 allows 0-9, ' ', '-'
// later positions don't allow '-'
return _editValueCursorPosition == 0 ? 12 : 11;
case GEM_VAL_CHAR:
return GEM_CHAR_CODE_TILDA - GEM_CHAR_CODE_SPACE;
case GEM_VAL_SELECT:
{
GEMItem* menuItemTmp = _menuPageCurrent->getCurrentMenuItem();
GEMSelect* select = menuItemTmp->select;
return select->getLength();
}
case GEM_VAL_FLOAT:
case GEM_VAL_DOUBLE:
// position 0 allows 0-9, ' ', '-'
// later positions don't allow '-' but do allow '.'
return 12;
}
} else {
return _menuPageCurrent->itemsCount;
}
}

int GEM::currentOption() {
if(_editValueMode) {
char chr;
switch (_editValueType) {
case GEM_VAL_INTEGER:
case GEM_VAL_BYTE:
case GEM_VAL_FLOAT:
case GEM_VAL_DOUBLE:
chr = _valueString[_editValueVirtualCursorPosition];
switch (chr) {
case GEM_CHAR_CODE_MINUS:
return 11;
case GEM_CHAR_CODE_SPACE:
// in position 0 we have minus before space
return (_editValueCursorPosition == 0) ? 12 : 11;
case GEM_CHAR_CODE_DOT:
return 13;
default:
return chr - GEM_CHAR_CODE_0;
}
case GEM_VAL_CHAR:
chr = _valueString[_editValueVirtualCursorPosition];
return chr - GEM_CHAR_CODE_SPACE;
case GEM_VAL_SELECT:
{
GEMItem* menuItemTmp = _menuPageCurrent->getCurrentMenuItem();
GEMSelect* select = menuItemTmp->select;
return _valueSelectNum = select->getSelectedOptionNum(menuItemTmp->linkedVariable);
}
}
} else {
return _menuPageCurrent->currentItemNum;
}
}

//====================== CONTEXT OPERATIONS

void GEM::clearContext() {
Expand Down Expand Up @@ -413,6 +479,7 @@ void GEM::menuItemSelect() {
case GEM_ITEM_VAL:
if (!menuItemTmp->readonly) {
enterEditValueMode();
if (menuItemTmp->enterAction) menuItemTmp->enterAction(menuItemTmp);
}
break;
case GEM_ITEM_LINK:
Expand Down
3 changes: 3 additions & 0 deletions src/GEM.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ class GEM {
void init(); // Init the menu (load necessary sprites into RAM of the SparkFun Graphic LCD Serial Backpack, display GEM splash screen, etc.)
void reInit(); // Reinitialize the menu (apply GEM specific settings to AltSerialGraphicLCD library)
void setMenuPageCurrent(GEMPage& menuPageCurrent); // Set supplied menu page as current
bool isEditing();
int availableOptions();
int currentOption();

/* CONTEXT OPERATIONS */

Expand Down
32 changes: 21 additions & 11 deletions src/GEMItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,49 +38,54 @@
#include "GEMItem.h"
#include "constants.h"

GEMItem::GEMItem(const char* title_, byte& linkedVariable_, GEMSelect& select_, void (*saveAction_)())
GEMItem::GEMItem(const char* title_, byte& linkedVariable_, GEMSelect& select_, void (*saveAction_)(), void (*enterAction_)(GEMItem *item))
: title(title_)
, linkedVariable(&linkedVariable_)
, linkedType(GEM_VAL_SELECT)
, type(GEM_ITEM_VAL)
, select(&select_)
, saveAction(saveAction_)
, enterAction(enterAction_)
{ }

GEMItem::GEMItem(const char* title_, int& linkedVariable_, GEMSelect& select_, void (*saveAction_)())
GEMItem::GEMItem(const char* title_, int& linkedVariable_, GEMSelect& select_, void (*saveAction_)(), void (*enterAction_)(GEMItem *item))
: title(title_)
, linkedVariable(&linkedVariable_)
, linkedType(GEM_VAL_SELECT)
, type(GEM_ITEM_VAL)
, select(&select_)
, saveAction(saveAction_)
, enterAction(enterAction_)
{ }

GEMItem::GEMItem(const char* title_, char* linkedVariable_, GEMSelect& select_, void (*saveAction_)())
GEMItem::GEMItem(const char* title_, char* linkedVariable_, GEMSelect& select_, void (*saveAction_)(), void (*enterAction_)(GEMItem *item))
: title(title_)
, linkedVariable(linkedVariable_)
, linkedType(GEM_VAL_SELECT)
, type(GEM_ITEM_VAL)
, select(&select_)
, saveAction(saveAction_)
, enterAction(enterAction_)
{ }

GEMItem::GEMItem(const char* title_, float& linkedVariable_, GEMSelect& select_, void (*saveAction_)())
GEMItem::GEMItem(const char* title_, float& linkedVariable_, GEMSelect& select_, void (*saveAction_)(), void (*enterAction_)(GEMItem *item))
: title(title_)
, linkedVariable(&linkedVariable_)
, linkedType(GEM_VAL_SELECT)
, type(GEM_ITEM_VAL)
, select(&select_)
, saveAction(saveAction_)
, enterAction(enterAction_)
{ }

GEMItem::GEMItem(const char* title_, double& linkedVariable_, GEMSelect& select_, void (*saveAction_)())
GEMItem::GEMItem(const char* title_, double& linkedVariable_, GEMSelect& select_, void (*saveAction_)(), void (*enterAction_)(GEMItem *item))
: title(title_)
, linkedVariable(&linkedVariable_)
, linkedType(GEM_VAL_SELECT)
, type(GEM_ITEM_VAL)
, select(&select_)
, saveAction(saveAction_)
, enterAction(enterAction_)
{ }

//---
Expand Down Expand Up @@ -132,48 +137,53 @@ GEMItem::GEMItem(const char* title_, double& linkedVariable_, GEMSelect& select_

//---

GEMItem::GEMItem(const char* title_, byte& linkedVariable_, void (*saveAction_)())
GEMItem::GEMItem(const char* title_, byte& linkedVariable_, void (*saveAction_)(), void (*enterAction_)(GEMItem *item))
: title(title_)
, linkedVariable(&linkedVariable_)
, linkedType(GEM_VAL_BYTE)
, type(GEM_ITEM_VAL)
, saveAction(saveAction_)
, enterAction(enterAction_)
{ }

GEMItem::GEMItem(const char* title_, int& linkedVariable_, void (*saveAction_)())
GEMItem::GEMItem(const char* title_, int& linkedVariable_, void (*saveAction_)(), void (*enterAction_)(GEMItem *item))
: title(title_)
, linkedVariable(&linkedVariable_)
, linkedType(GEM_VAL_INTEGER)
, type(GEM_ITEM_VAL)
, saveAction(saveAction_)
, enterAction(enterAction_)
{ }

GEMItem::GEMItem(const char* title_, char* linkedVariable_, void (*saveAction_)())
GEMItem::GEMItem(const char* title_, char* linkedVariable_, void (*saveAction_)(), void (*enterAction_)(GEMItem *item))
: title(title_)
, linkedVariable(linkedVariable_)
, linkedType(GEM_VAL_CHAR)
, type(GEM_ITEM_VAL)
, saveAction(saveAction_)
, enterAction(enterAction_)
{ }

GEMItem::GEMItem(const char* title_, boolean& linkedVariable_, void (*saveAction_)())
GEMItem::GEMItem(const char* title_, boolean& linkedVariable_, void (*saveAction_)(), void (*enterAction_)(GEMItem *item))
: title(title_)
, linkedVariable(&linkedVariable_)
, linkedType(GEM_VAL_BOOLEAN)
, type(GEM_ITEM_VAL)
, saveAction(saveAction_)
, enterAction(enterAction_)
{ }

GEMItem::GEMItem(const char* title_, float& linkedVariable_, void (*saveAction_)())
GEMItem::GEMItem(const char* title_, float& linkedVariable_, void (*saveAction_)(), void (*enterAction_)(GEMItem *item))
: title(title_)
, linkedVariable(&linkedVariable_)
, linkedType(GEM_VAL_FLOAT)
, type(GEM_ITEM_VAL)
, precision(GEM_FLOAT_PREC)
, saveAction(saveAction_)
, enterAction(enterAction_)
{ }

GEMItem::GEMItem(const char* title_, double& linkedVariable_, void (*saveAction_)())
GEMItem::GEMItem(const char* title_, double& linkedVariable_, void (*saveAction_)(), void (*enterAction_)(GEMItem *item))
: title(title_)
, linkedVariable(&linkedVariable_)
, linkedType(GEM_VAL_DOUBLE)
Expand Down
23 changes: 12 additions & 11 deletions src/GEMItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ class GEMItem {
@param 'select_' - reference to GEMSelect option select
@param 'saveAction_' - pointer to callback function executed when associated variable is successfully saved
*/
GEMItem(const char* title_, byte& linkedVariable_, GEMSelect& select_, void (*saveAction_)());
GEMItem(const char* title_, int& linkedVariable_, GEMSelect& select_, void (*saveAction_)());
GEMItem(const char* title_, char* linkedVariable_, GEMSelect& select_, void (*saveAction_)());
GEMItem(const char* title_, float& linkedVariable_, GEMSelect& select_, void (*saveAction_)());
GEMItem(const char* title_, double& linkedVariable_, GEMSelect& select_, void (*saveAction_)());
GEMItem(const char* title_, byte& linkedVariable_, GEMSelect& select_, void (*saveAction_)(), void (*enterAction_)(GEMItem *item) = nullptr);
GEMItem(const char* title_, int& linkedVariable_, GEMSelect& select_, void (*saveAction_)(), void (*enterAction_)(GEMItem *item) = nullptr);
GEMItem(const char* title_, char* linkedVariable_, GEMSelect& select_, void (*saveAction_)(), void (*enterAction_)(GEMItem *item) = nullptr);
GEMItem(const char* title_, float& linkedVariable_, GEMSelect& select_, void (*saveAction_)(), void (*enterAction_)(GEMItem *item) = nullptr);
GEMItem(const char* title_, double& linkedVariable_, GEMSelect& select_, void (*saveAction_)(), void (*enterAction_)(GEMItem *item) = nullptr);
/*
Constructors for menu item that represents option select, w/o callback
@param 'title_' - title of the menu item displayed on the screen
Expand All @@ -95,12 +95,12 @@ class GEMItem {
@param 'linkedVariable_' - reference to variable that menu item is associated with (either byte, int, char*, boolean, float, or double)
@param 'saveAction_' - pointer to callback function executed when associated variable is successfully saved
*/
GEMItem(const char* title_, byte& linkedVariable_, void (*saveAction_)());
GEMItem(const char* title_, int& linkedVariable_, void (*saveAction_)());
GEMItem(const char* title_, char* linkedVariable_, void (*saveAction_)());
GEMItem(const char* title_, boolean& linkedVariable_, void (*saveAction_)());
GEMItem(const char* title_, float& linkedVariable_, void (*saveAction_)());
GEMItem(const char* title_, double& linkedVariable_, void (*saveAction_)());
GEMItem(const char* title_, byte& linkedVariable_, void (*saveAction_)(), void (*enterAction_)(GEMItem *item) = nullptr);
GEMItem(const char* title_, int& linkedVariable_, void (*saveAction_)(), void (*enterAction_)(GEMItem *item) = nullptr);
GEMItem(const char* title_, char* linkedVariable_, void (*saveAction_)(), void (*enterAction_)(GEMItem *item) = nullptr);
GEMItem(const char* title_, boolean& linkedVariable_, void (*saveAction_)(), void (*enterAction_)(GEMItem *item) = nullptr);
GEMItem(const char* title_, float& linkedVariable_, void (*saveAction_)(), void (*enterAction_)(GEMItem *item) = nullptr);
GEMItem(const char* title_, double& linkedVariable_, void (*saveAction_)(), void (*enterAction_)(GEMItem *item) = nullptr);
/*
Constructors for menu item that represents variable, w/o callback
@param 'title_' - title of the menu item displayed on the screen
Expand Down Expand Up @@ -165,6 +165,7 @@ class GEMItem {
GEMItem* menuItemNext;
void (*buttonAction)();
void (*saveAction)();
void (*enterAction)(GEMItem *item);
GEMItem* getMenuItemNext(); // Get next menu item, excluding hidden ones
};

Expand Down
67 changes: 67 additions & 0 deletions src/GEM_adafruit_gfx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,72 @@ void GEM_adafruit_gfx::setMenuPageCurrent(GEMPage& menuPageCurrent) {
_menuPageCurrent = &menuPageCurrent;
}

bool GEM_adafruit_gfx::isEditing() {
return _editValueMode;
}

int GEM_adafruit_gfx::availableOptions() {
if(_editValueMode) {
switch (_editValueType) {
case GEM_VAL_INTEGER:
case GEM_VAL_BYTE:
// position 0 allows 0-9, ' ', '-'
// later positions don't allow '-'
return _editValueCursorPosition == 0 ? 12 : 11;
case GEM_VAL_CHAR:
return GEM_CHAR_CODE_TILDA - GEM_CHAR_CODE_SPACE;
case GEM_VAL_SELECT:
{
GEMItem* menuItemTmp = _menuPageCurrent->getCurrentMenuItem();
GEMSelect* select = menuItemTmp->select;
return select->getLength();
}
case GEM_VAL_FLOAT:
case GEM_VAL_DOUBLE:
// position 0 allows 0-9, ' ', '-'
// later positions don't allow '-' but do allow '.'
return 12;
}
} else {
return _menuPageCurrent->itemsCount;
}
}

int GEM_adafruit_gfx::currentOption() {
if(_editValueMode) {
char chr;
switch (_editValueType) {
case GEM_VAL_INTEGER:
case GEM_VAL_BYTE:
case GEM_VAL_FLOAT:
case GEM_VAL_DOUBLE:
chr = _valueString[_editValueVirtualCursorPosition];
switch (chr) {
case GEM_CHAR_CODE_MINUS:
return 11;
case GEM_CHAR_CODE_SPACE:
// in position 0 we have minus before space
return (_editValueCursorPosition == 0) ? 12 : 11;
case GEM_CHAR_CODE_DOT:
return 13;
default:
return chr - GEM_CHAR_CODE_0;
}
case GEM_VAL_CHAR:
chr = _valueString[_editValueVirtualCursorPosition];
return chr - GEM_CHAR_CODE_SPACE;
case GEM_VAL_SELECT:
{
GEMItem* menuItemTmp = _menuPageCurrent->getCurrentMenuItem();
GEMSelect* select = menuItemTmp->select;
return _valueSelectNum = select->getSelectedOptionNum(menuItemTmp->linkedVariable);
}
}
} else {
return _menuPageCurrent->currentItemNum;
}
}

//====================== CONTEXT OPERATIONS

void GEM_adafruit_gfx::clearContext() {
Expand Down Expand Up @@ -442,6 +508,7 @@ void GEM_adafruit_gfx::menuItemSelect() {
case GEM_ITEM_VAL:
if (!menuItemTmp->readonly) {
enterEditValueMode();
if (menuItemTmp->enterAction) menuItemTmp->enterAction(menuItemTmp);
}
break;
case GEM_ITEM_LINK:
Expand Down
3 changes: 3 additions & 0 deletions src/GEM_adafruit_gfx.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ class GEM_adafruit_gfx {
void init(); // Init the menu (load necessary sprites into RAM of the SparkFun Graphic LCD Serial Backpack, display GEM splash screen, etc.)
void reInit(); // Reinitialize the menu (apply GEM specific settings to AltSerialGraphicLCD library)
void setMenuPageCurrent(GEMPage& menuPageCurrent); // Set supplied menu page as current
bool isEditing();
int availableOptions();
int currentOption();

/* CONTEXT OPERATIONS */

Expand Down
Loading