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

Use ASCII symbols for control commands #202

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
9f19483
Apply suggested changes
ShishkinDmitriy Sep 14, 2024
45284f4
Merge branch 'move-logic-to-items' of https://github.com/ShishkinDmit…
ShishkinDmitriy Sep 14, 2024
6199d0d
Use ASCII symbols for control
ShishkinDmitriy Sep 16, 2024
8e7ef8d
Add input layer
ShishkinDmitriy Sep 16, 2024
dcb4801
Add docs
ShishkinDmitriy Sep 16, 2024
839b23f
Merge branch 'move-logic-to-items' of https://github.com/ShishkinDmit…
ShishkinDmitriy Sep 16, 2024
e0fc8d3
Merge branch 'feature/move-control-to-items' into move-logic-to-items
forntoh Sep 16, 2024
ecb056c
Rename handle to process
ShishkinDmitriy Sep 16, 2024
3342827
Merge branch 'move-logic-to-items' of https://github.com/ShishkinDmit…
ShishkinDmitriy Sep 16, 2024
e980135
Rename in examples
ShishkinDmitriy Sep 16, 2024
42d41a0
Fix spell checks
ShishkinDmitriy Sep 16, 2024
4e117db
Merge branch 'feature/move-control-to-items' into move-logic-to-items
forntoh Sep 16, 2024
d428cf8
Merge branch 'feature/move-control-to-items' into move-logic-to-items
forntoh Sep 16, 2024
8fdf221
Merge branch 'feature/move-control-to-items' into move-logic-to-items
forntoh Sep 16, 2024
636d6cb
Merge branch 'feature/move-control-to-items' into move-logic-to-items
forntoh Sep 16, 2024
9cc52f5
Format code
ShishkinDmitriy Sep 16, 2024
43dcf06
Merge branch 'move-logic-to-items' of https://github.com/ShishkinDmit…
ShishkinDmitriy Sep 16, 2024
62802fc
Format code
ShishkinDmitriy Sep 16, 2024
6fe0ec1
Rename loop to observe
ShishkinDmitriy Sep 17, 2024
035eb58
Handle DEL on Mac
ShishkinDmitriy Sep 17, 2024
dc56767
Handle \r\n for ENTER
ShishkinDmitriy Sep 17, 2024
6de7fd9
Merge branch 'feature/move-control-to-items' into move-logic-to-items
forntoh Sep 17, 2024
d75915f
Move cpp to h
ShishkinDmitriy Sep 17, 2024
9a0bfdd
Rename diagrams
ShishkinDmitriy Sep 17, 2024
2ebc14b
Merge branch 'move-logic-to-items' of https://github.com/ShishkinDmit…
ShishkinDmitriy Sep 17, 2024
4d3f13a
Fix spell check
ShishkinDmitriy Sep 17, 2024
b891cb8
Format code
ShishkinDmitriy Sep 17, 2024
2a41e25
Update visibility of functions in LcdMenu.h
forntoh Sep 17, 2024
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
49 changes: 49 additions & 0 deletions docs/classes.puml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
@startuml
' https://www.plantuml.com/plantuml/uml/

hide empty members

class LcdMenu {
+virtual bool handle(char c)
forntoh marked this conversation as resolved.
Show resolved Hide resolved
#virtual bool up()
#virtual bool down()
#virtual bool enter()
#virtual bool back()
}

class MenuItem {
+virtual bool handle(char c)
}

class ItemCommand {
+bool handle(char c) override
#bool enter() override
}

class ItemList {
+bool handle(char c) override
#bool up() override
#bool down() override
#bool enter() override
#bool back() override
}

class ItemInput {
+bool handle(char c) override
#bool up() override
#bool down() override
#bool enter() override
#bool back() override
#bool left() override
#bool right() override
#bool backspace() override
#bool typeChar(char c) override
#bool clear() override
}

LcdMenu::handle -r-> MenuItem::handle
ItemCommand -u-|> MenuItem
ItemList -u-|> MenuItem
ItemInput -u-|> MenuItem

@enduml
45 changes: 45 additions & 0 deletions examples/KeyboardAdapter/KeyboardAdapter.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Usage example of `KeyboardAdapter`.
*/
#define DEBUG

#include <ItemInputCharset.h>
#include <LcdMenu.h>
#include <input/KeyboardAdapter.h>
#include <interface/LiquidCrystalI2CAdapter.h>

#define LCD_ROWS 2
#define LCD_COLS 16

// Create your charset
const char* charset = "0123456789";

// Declare the call back function
void inputCallback(char* value);

MAIN_MENU(
ITEM_INPUT_CHARSET("Con", "0123456", charset, inputCallback),
ITEM_BASIC("Connect to WiFi"),
ITEM_BASIC("Blink SOS"),
ITEM_BASIC("Blink random"));

LiquidCrystalI2CAdapter lcdAdapter(0x27, LCD_COLS, LCD_ROWS);
LcdMenu menu(lcdAdapter);
KeyboardAdapter keyboard(&menu, &Serial);

void setup() {
Serial.begin(9600);
menu.initialize(mainMenu);
}

void loop() {
keyboard.observe();
}
/**
* Define callback
*/
void inputCallback(char* value) {
// Do stuff with value
Serial.print(F("# "));
Serial.println(value);
}
4 changes: 2 additions & 2 deletions examples/SimpleInput/SimpleInput.ino
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void loop() {
char command = Serial.read();

if (!processWithSimpleCommand(&navConfig, command) && command != '\n') {
menu.type(command);
menu.process(command);
}
}
/**
Expand All @@ -56,4 +56,4 @@ void loop() {
void inputCallback(char* value) {
Serial.print(F("# "));
Serial.println(value);
}
}
11 changes: 10 additions & 1 deletion src/ItemCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,19 @@ class ItemCommand : public MenuItem {
*/
void setCallBack(fptr callback) { this->callback = callback; };

void enter() override {
bool process(const unsigned char c) override {
switch (c) {
case ENTER: return enter();
default: return false;
}
}

protected:
bool enter() {
if (callback != NULL) {
callback();
}
return true;
};
};

Expand Down
81 changes: 47 additions & 34 deletions src/ItemInput.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
* @brief Item that allows user to input string information.
*
* ┌────────────────────────────┐
* │ . . . │
* │ > T E X T : V A L U E │
* │ . . . │
* └────────────────────────────┘
*
* Additionally to `text` this item has string `value`.
Expand Down Expand Up @@ -141,13 +139,34 @@ class ItemInput : public MenuItem {
*/
fptrStr getCallbackStr() { return callback; }

void up() override {}
bool process(const unsigned char c) override {
if (isprint(c)) {
return typeChar(c);
}
switch (c) {
case ENTER: return enter();
case BACK: return back();
case UP: return display->getEditModeEnabled();
case DOWN: return display->getEditModeEnabled();
case LEFT: return left();
case RIGHT: return right();
case BACKSPACE: return backspace();
case CLEAR: return clear();
default: return false;
}
}

void down() override {}
void draw(uint8_t row) override {
uint8_t maxCols = display->getMaxCols();
static char* buf = new char[maxCols];
substring(value, view, viewSize, buf);
display->drawItem(row, text, ':', buf);
}

void enter() override {
protected:
bool enter() {
if (display->getEditModeEnabled()) {
return;
return false;
}
// Move cursor to the latest index
uint8_t length = strlen(value);
Expand All @@ -161,11 +180,11 @@ class ItemInput : public MenuItem {
display->setEditModeEnabled(true);
display->resetBlinker(constrainBlinkerPosition(strlen(text) + 2 + cursor - view));
display->drawBlinker();
return true;
};

void back() override {
bool back() {
if (!display->getEditModeEnabled()) {
return;
return false;
}
display->clearBlinker();
display->setEditModeEnabled(false);
Expand All @@ -176,14 +195,14 @@ class ItemInput : public MenuItem {
if (callback != NULL) {
callback(value);
}
return true;
};

void left() override {
bool left() {
if (!display->getEditModeEnabled()) {
return;
return false;
}
if (cursor == 0) {
return;
return true;
}
cursor--;
if (cursor < view) {
Expand All @@ -193,14 +212,14 @@ class ItemInput : public MenuItem {
display->resetBlinker(constrainBlinkerPosition(display->getBlinkerPosition() - 1));
// Log
printCmd(F("LEFT"), value[display->getBlinkerPosition() - (strlen(text) + 2)]);
return true;
};

void right() override {
bool right() {
if (!display->getEditModeEnabled()) {
return;
return false;
}
if (cursor == strlen(value)) {
return;
return true;
}
cursor++;
if (cursor > (view + viewSize - 1)) {
Expand All @@ -210,28 +229,21 @@ class ItemInput : public MenuItem {
display->resetBlinker(constrainBlinkerPosition(display->getBlinkerPosition() + 1));
// Log
printCmd(F("RIGHT"), value[display->getBlinkerPosition() - (strlen(text) + 2)]);
return true;
};

void draw(uint8_t row) override {
uint8_t maxCols = display->getMaxCols();
static char* buf = new char[maxCols];
substring(value, view, viewSize, buf);
display->drawItem(row, text, ':', buf);
}

/**
* Execute a "backspace cmd" on menu
*
* *NB: Works only for `ItemInput` type*
*
* Removes the character at the current cursor position.
*/
void backspace() override {
bool backspace() {
if (!display->getEditModeEnabled()) {
return;
return false;
}
if (strlen(value) == 0 || cursor == 0) {
return;
return true;
}
remove(value, cursor - 1, 1);
printCmd(F("BACKSPACE"), value);
Expand All @@ -241,16 +253,16 @@ class ItemInput : public MenuItem {
}
MenuItem::draw();
display->resetBlinker(constrainBlinkerPosition(display->getBlinkerPosition() - 1));
return true;
}

/**
* Display text at the cursor position
* used for `Input` type menu items
* @param character character to append
*/
void typeChar(const char character) override {
bool typeChar(const char character) {
if (!display->getEditModeEnabled()) {
return;
return false;
}
uint8_t length = strlen(value);
if (cursor < length) {
Expand All @@ -274,14 +286,14 @@ class ItemInput : public MenuItem {
display->resetBlinker(constrainBlinkerPosition(display->getBlinkerPosition() + 1));
// Log
printCmd(F("TYPE-CHAR"), character);
return true;
}

/**
* Clear the value of the input field
*/
void clear() override {
bool clear() {
if (!display->getEditModeEnabled()) {
return;
return false;
}
//
// set the value
Expand All @@ -294,6 +306,7 @@ class ItemInput : public MenuItem {
//
MenuItem::draw();
display->resetBlinker(constrainBlinkerPosition(strlen(text) + 2));
return true;
}
};

Expand Down
Loading
Loading