Skip to content

Commit 89ffe59

Browse files
committed
stringIn
allows textField to receive input from customized drivers
1 parent f236d26 commit 89ffe59

File tree

4 files changed

+73
-8
lines changed

4 files changed

+73
-8
lines changed

examples/codeCtrl/codeCtrl/codeCtrl.ino

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ on this example only using
2222

2323
#include <Arduino.h>
2424
#include <menu.h>
25-
#include <menuIO/serialIn.h>
26-
#include <menuIO/serialOut.h>
25+
#include <menuIO/serialIO.h>
26+
#include <menuIO/stringIn.h>
2727
#include <menuIO/chainStream.h>
2828

2929
using namespace Menu;
@@ -131,7 +131,12 @@ MENU_OUTPUTS(out,MAX_DEPTH
131131
,NONE//must have 2 items at least
132132
);
133133

134+
stringIn<0> strIn;//buffer size: 2^5 = 32 bytes, eventually use 0 for a single byte
134135
serialIn serial(Serial);
136+
// use this commented lines if you want your stringIn object to be used as part or normal menu input
137+
// menuIn* inputsList[]={&serial,&strIn};
138+
// chainStream<sizeof(inputsList)> in(inputsList);
139+
// NAVROOT(nav,mainMenu,MAX_DEPTH,in,out);
135140
NAVROOT(nav,mainMenu,MAX_DEPTH,serial,out);
136141

137142
result alert(menuOut& o,idleEvent e) {
@@ -155,6 +160,7 @@ void setup() {
155160
pinMode(LEDPIN, OUTPUT);
156161
pinMode(NAV_BTN,INPUT_PULLUP);
157162
pinMode(SEL_BTN,INPUT_PULLUP);
163+
Serial.println("menu 4.x");
158164
}
159165

160166
#define SOFT_DEBOUNCE_MS 100
@@ -172,9 +178,17 @@ void loop() {
172178
nav.doNav(upCmd);
173179
delay(SOFT_DEBOUNCE_MS);
174180
}
175-
nav.poll();//also do serial input
176-
//or deal with charater input directly
177-
// if (Serial.available()) nav.active().parseInput(nav.node(),Serial.read());
178-
//nav.doOutput();
181+
//if stringIn is a regular input then we should write to it here, before poll
182+
// strIn.write(...);//just put the character you want to send
183+
// nav.poll();//also do serial or stringIn input
184+
// or deal with charater input directly... (if you have your own input driver)
185+
if (Serial.available()) {
186+
//of course menu can read from Serial or even stringIn (se above how to use stringIn as a regular menu input)
187+
//but here we demonstrate the use of stringIn in direct call, by writing the data to stream and then call parseInput
188+
if (strIn.write(Serial.read()))//so we just transfer data from serial to strIn
189+
// nav.active().parseInput(nav.node(),strIn);//and then let target parse input
190+
nav.doInput(strIn);
191+
}
192+
nav.doOutput();//if not doing poll the we need to do output "manualy"
179193
digitalWrite(LEDPIN, ledCtrl);
180194
}

examples/codeCtrl/platformio.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ board = nanoatmega328
2424
framework = arduino
2525
upload_port=/dev/ttyUSB*
2626
upload_flags=-V
27-
build_flags = -DNODEBUG
27+
build_flags = -DDEBUG
2828

2929
; [env:uno]
3030
; platform = atmelavr

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=ArduinoMenu library
2-
version=4.1.11
2+
version=4.2.0
33
author=Rui Azevedo, [email protected]
44
maintainer=neu-rah, [email protected]
55
sentence=AVR generic menu/interactivity system

src/menuIO/stringIn.h

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/* -*- C++ -*- */
2+
/********************
3+
Feb.2018 Rui Azevedo - ruihfazevedo(@rrob@)gmail.com
4+
www.r-site.net
5+
6+
string input driver
7+
***/
8+
9+
#ifndef RSITE_ARDUINO_MENU_HW_STRINGIN
10+
#define RSITE_ARDUINO_MENU_HW_STRINGIN
11+
#include "../menu.h"
12+
13+
namespace Menu {
14+
//using a circular buffer
15+
//attention, if using sz=0 (2^0=1) we just override previous character
16+
template<uint8_t sz=0>//this size is 2^sz
17+
class stringIn:public menuIn {
18+
public:
19+
static_assert(sz<7,"ArduinoMenu: max buffer size is 2^7");
20+
enum Mask:uint8_t {
21+
nBytes=1<<sz,
22+
mask=nBytes-1
23+
};
24+
char data[nBytes]={0};
25+
uint8_t head=0;
26+
uint8_t at=0;
27+
size_t write(uint8_t o) override {
28+
if (sz&&(at==mask&(head-1))) return 0;//buffer full!
29+
data[at]=o;
30+
at++;
31+
at&=mask;
32+
return 1;
33+
}
34+
int available() override {return sz?at-head:data[head]!=0;}
35+
int peek() override {return available()?data[head]:0;}
36+
int read() override {
37+
if (available()) {
38+
if (sz&&(head==at)) return -1;
39+
int r=data[head];
40+
if(!sz) data[head]=0;
41+
head++;
42+
head&=mask;
43+
return r;
44+
}
45+
return -1;
46+
}
47+
void flush() override {at=head=0;}//just throw away all data!
48+
};
49+
}
50+
51+
#endif

0 commit comments

Comments
 (0)