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

Feature/move control to items #1

Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/compile-arduino.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: Compile Examples
on: [pull_request_target]
on: [pull_request_target, pull_request]

jobs:
compile:
Expand Down
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)
#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