Skip to content

Commit da3affe

Browse files
authored
Merge pull request #215 from vladkozlov69/master
Support for UART displays (Surenoo)
2 parents 0145163 + 7011edd commit da3affe

File tree

3 files changed

+272
-0
lines changed

3 files changed

+272
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/********************
2+
Arduino generic menu system
3+
4+
Rui Azevedo - ruihfazevedo(@rrob@)gmail.com
5+
6+
output: Serial
7+
input: Serial
8+
*/
9+
10+
#include <Arduino.h>
11+
12+
#include <menu.h>
13+
#include <menuIO/serialOut.h>
14+
#include <menuIO/chainStream.h>
15+
#include <menuIO/serialIn.h>
16+
#include <menuIO/uartOut.h>
17+
#include <SoftwareSerial.h>
18+
19+
using namespace Menu;
20+
21+
#define LEDPIN LED_BUILTIN
22+
23+
SoftwareSerial ss(11, 10);
24+
25+
26+
#define MAX_DEPTH 2
27+
28+
// define menu colors --------------------------------------------------------
29+
#define C_BLACK 0
30+
#define C_BLUE 3
31+
#define C_GRAY 7
32+
#define C_WHITE 15
33+
#define C_YELLOW 31
34+
#define C_RED 1
35+
#define C_GREEN 2
36+
//each color is in the format:
37+
// {{disabled normal,disabled selected},{enabled normal,enabled selected, enabled editing}}
38+
const colorDef<uint8_t> colors[] MEMMODE={
39+
{{C_BLACK,C_BLACK}, {C_BLACK,C_BLUE,C_BLUE}},//bgColor
40+
{{C_GRAY,C_GRAY}, {C_WHITE,C_WHITE,C_WHITE}},//fgColor
41+
{{C_WHITE,C_BLACK}, {C_YELLOW,C_YELLOW,C_RED}},//valColor
42+
{{C_WHITE,C_BLACK}, {C_WHITE,C_YELLOW,C_YELLOW}},//unitColor
43+
{{C_WHITE,C_GRAY}, {C_BLACK,C_BLUE,C_WHITE}},//cursorColor
44+
{{C_WHITE,C_YELLOW},{C_GREEN,C_WHITE,C_WHITE}},//titleColor
45+
};
46+
47+
48+
#define offsetX 0
49+
#define offsetY 0
50+
#define U8_Width 240
51+
#define U8_Height 320
52+
53+
#define LEDPIN LED_BUILTIN
54+
55+
56+
int timeOn=10;
57+
int timeOff=90;
58+
59+
MENU(mainMenu, "Settings menu", Menu::doNothing, Menu::noEvent, Menu::wrapStyle
60+
,FIELD(timeOn,"On","ms",0,100,10,1, Menu::doNothing, Menu::noEvent, Menu::noStyle)
61+
,FIELD(timeOff,"Off","ms",0,100,10,1,Menu::doNothing, Menu::noEvent, Menu::noStyle)
62+
,EXIT("<Back")
63+
);
64+
65+
serialIn serial(Serial);
66+
67+
MENU_INPUTS(in,&serial);
68+
69+
MENU_OUTPUTS(out,MAX_DEPTH
70+
,UART_OUT(ss,colors,uartOut::systemFont::font24,offsetX,offsetY,{0,0,U8_Width/uartOut::systemFont::font24*2,U8_Height/uartOut::systemFont::font24})
71+
,SERIAL_OUT(Serial)
72+
);
73+
74+
NAVROOT(nav,mainMenu,MAX_DEPTH,in,out);
75+
76+
void setup() {
77+
pinMode(LEDPIN, OUTPUT);
78+
Serial.begin(115200);
79+
ss.begin(115200);
80+
while(!Serial);
81+
Serial.println("Menu 4.x");
82+
Serial.println("Use keys + - * /");
83+
Serial.println("to control the menu navigation");
84+
}
85+
86+
void loop() {
87+
nav.doInput();
88+
if (nav.changed(0)) {
89+
//only draw if menu changed for gfx device
90+
nav.doOutput();
91+
}
92+
digitalWrite(LEDPIN, HIGH);
93+
delay(timeOn);
94+
digitalWrite(LEDPIN, LOW);
95+
delay(timeOff);
96+
}
97+

src/macros.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
#define TFT_OUT(...) ON(TFT_OUT,__COUNTER__,__VA_ARGS__)
8383
#define U8GLIB_OUT(...) ON(U8GLIB_OUT,__COUNTER__,__VA_ARGS__)
8484
#define U8G2_OUT(...) ON(U8G2_OUT,__COUNTER__,__VA_ARGS__)
85+
#define UART_OUT(...) ON(UART_OUT,__COUNTER__,__VA_ARGS__)
8586
#define UCG_OUT(...) ON(UCG_OUT,__COUNTER__,__VA_ARGS__)
8687
#define U8X8_OUT(...) ON(U8X8_OUT,__COUNTER__,__VA_ARGS__)
8788
#define UTFT_OUT(...) ON(UTFT_OUT,__COUNTER__,__VA_ARGS__)
@@ -130,6 +131,11 @@ Menu::idx_t id##Tops##n[md];\
130131
PANELS(id##Panels##n,__VA_ARGS__);\
131132
Menu::u8g2Out id##n(gfx,color,id##Tops##n,id##Panels##n,fontW,fontH,offsetX,offsetY);
132133

134+
#define VAR_UART_OUT(id,md,n,gfx,color,font,offsetX,offsetY,...)\
135+
Menu::idx_t id##Tops##n[md];\
136+
PANELS(id##Panels##n,__VA_ARGS__);\
137+
Menu::uartOut id##n(gfx,color,id##Tops##n,id##Panels##n,font,offsetX,offsetY);
138+
133139
#define VAR_UCG_OUT(id,md,n,gfx,color,fontW,fontH,offsetX,offsetY,...)\
134140
Menu::idx_t id##Tops##n[md];\
135141
PANELS(id##Panels##n,__VA_ARGS__);\
@@ -153,6 +159,7 @@ Menu::utftOut id##n(gfx,color,id##Tops##n,id##Panels##n,fontW,fontH);
153159
#define REF_ADAGFX_OUT(id,md,n,...) &id##n,
154160
#define REF_U8GLIB_OUT(id,md,n,...) &id##n,
155161
#define REF_U8G2_OUT(id,md,n,...) &id##n,
162+
#define REF_UART_OUT(id,md,n,...) &id##n,
156163
#define REF_UCG_OUT(id,md,n,...) &id##n,
157164
#define REF_U8X8_OUT(id,md,n,...) &id##n,
158165
#define REF_UTFT_OUT(id,md,n,...) &id##n,

src/menuIO/uartOut.h

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
/* -*- C++ -*- */
2+
3+
/********************
4+
Oct. 2016 Stephen Denne https://github.com/datacute
5+
Based on U8GLibOut.h from Rui Azevedo - ruihfazevedo(@rrob@)gmail.com
6+
Original from: https://github.com/christophepersoz
7+
8+
Use graphics screens as menu output, based on U8G2 graphic display
9+
www.r-site.net
10+
11+
***/
12+
#ifndef RSITE_ARDUINOP_MENU_UART
13+
#define RSITE_ARDUINOP_MENU_UART
14+
#include "menuDefs.h"
15+
16+
namespace Menu {
17+
18+
class uartOut:public gfxOut {
19+
public:
20+
enum systemFont {
21+
font16 = 16,
22+
font24 = 24,
23+
font32 = 32
24+
};
25+
int8_t offsetX=0;
26+
int8_t offsetY=0;
27+
int cursorX = 0;
28+
int cursorY = 0;
29+
char buf[100];
30+
int currentColor = 0;
31+
int m_bgColor = 0;
32+
systemFont m_Font;
33+
Stream& stream;
34+
const colorDef<uint8_t> (&colors)[nColors];
35+
uartOut(
36+
Stream& stream,
37+
const colorDef<uint8_t> (&c)[nColors],
38+
idx_t* t,
39+
panelsList &p,
40+
systemFont font = font24,
41+
idx_t offsetX=0,
42+
idx_t offsetY=0,
43+
int fontMarginY=1
44+
) :gfxOut(font/2,font,t,p,(styles)(menuOut::redraw|menuOut::rasterDraw)),stream(stream),colors(c)
45+
{
46+
this->offsetX=offsetX;
47+
this->offsetY=offsetY;
48+
this->fontMarginY=fontMarginY;
49+
this->m_Font = font;
50+
}
51+
52+
size_t write(uint8_t ch) override
53+
{
54+
sprintf(buf,"SBC(%d);DCV%d(%d,%d,'%c',%d);", m_bgColor, m_Font, cursorX, cursorY - resY + fontMarginY, (char)ch, currentColor);
55+
sendCommand(buf);
56+
cursorX = cursorX + resX;
57+
return 1;
58+
}
59+
60+
inline uint8_t getColor(colorDefs color=bgColor,bool selected=false,status stat=enabledStatus,bool edit=false) const
61+
{
62+
return memByte(&(stat==enabledStatus?colors[color].enabled[selected+edit]:colors[color].disabled[selected]));
63+
}
64+
65+
void setBgColor(colorDefs c,bool selected=false,status s=enabledStatus,bool edit=false)
66+
{
67+
m_bgColor = getColor(c,selected,s,edit);
68+
}
69+
70+
void setColor(colorDefs c,bool selected=false,status s=enabledStatus,bool edit=false) override
71+
{
72+
currentColor = getColor(c,selected,s,edit);
73+
}
74+
75+
void clearLine(idx_t ln,idx_t panelNr=0,colorDefs color=bgColor,bool selected=false,status stat=enabledStatus,bool edit=false) override
76+
{
77+
const panel p=panels[panelNr];
78+
setBgColor(color,selected,stat,edit);
79+
drawBox(p.x*resX + offsetX, (p.y+ln)*resY + offsetY, maxX()*resX, resY);
80+
}
81+
82+
void clear() override
83+
{
84+
sendCommand("CLR(7);");
85+
panels.reset();
86+
}
87+
88+
void clear(idx_t panelNr) override
89+
{
90+
const panel p=panels[panelNr];
91+
setBgColor(bgColor,false,enabledStatus,false);;
92+
drawBox(p.x*resX + offsetX,p.y*resY + offsetY,p.w*resX,p.h*resY);
93+
panels.nodes[panelNr]=NULL;
94+
}
95+
96+
void box(idx_t panelNr,idx_t x,idx_t y,idx_t w=1,idx_t h=1,colorDefs c=bgColor,bool selected=false,status stat=enabledStatus,bool edit=false) override
97+
{
98+
const panel p=panels[panelNr];
99+
setBgColor(c, selected, stat, edit);
100+
drawFrame((p.x+x)*resX,(p.y+y)*resY, w*resX, h*resY);
101+
}
102+
103+
void rect(idx_t panelNr,idx_t x,idx_t y,idx_t w=1,idx_t h=1,colorDefs c=bgColor,bool selected=false,status stat=enabledStatus,bool edit=false) override
104+
{
105+
const panel p=panels[panelNr];
106+
setBgColor(c, selected, stat, edit);
107+
drawBox((p.x+x)*resX, (p.y+y)*resY, w*resX, h*resY);
108+
}
109+
110+
void setCursor(idx_t x,idx_t y,idx_t panelNr=0) override
111+
{
112+
const panel p=panels[panelNr];
113+
cursorX = (p.x+x)*resX+fontMarginX + offsetX;
114+
cursorY = (p.y+y+1)*resY-fontMarginY + offsetY;
115+
}
116+
117+
void drawCursor(idx_t ln,bool selected,status stat,bool edit=false,idx_t panelNr=0) override
118+
{
119+
const panel p=panels[panelNr];
120+
setBgColor(cursorColor,selected,stat);
121+
drawFrame(p.x*resX + offsetX, (p.y+ln)*resY + offsetY, maxX()*resX, resY);
122+
}
123+
124+
idx_t printRaw(const char* at,idx_t len) override
125+
{
126+
sprintf_P(buf,PSTR("SBC(%d);DCV%d(%d,%d,'%S',%d);"), m_bgColor, m_Font, cursorX, cursorY - resY + fontMarginY, at, currentColor);
127+
sendCommand(buf);
128+
cursorX = cursorX + strlen_P(at)*resX;
129+
return strlen_P(at);
130+
}
131+
132+
private:
133+
134+
void checkStreamReady()
135+
{
136+
int cnt = 0, timeOutCounter = 1000;
137+
while (timeOutCounter-- > 0)
138+
{
139+
delay(1);
140+
if (stream.available() > 0)
141+
{
142+
stream.read();
143+
if (++cnt == 4) break;
144+
}
145+
}
146+
}
147+
148+
void sendCommand(char * cmd)
149+
{
150+
stream.println(cmd);
151+
//Serial.println(cmd);
152+
checkStreamReady();
153+
}
154+
155+
void drawFrame(int x, int y, int w, int h)
156+
{
157+
sprintf(buf, "BOX(%d,%d,%d,%d,%d);", x, y, x + w, y + h, m_bgColor);
158+
sendCommand(buf);
159+
}
160+
161+
void drawBox(int x, int y, int w, int h)
162+
{
163+
sprintf(buf, "BOXF(%d,%d,%d,%d,%d);", x, y, x + w, y + h, m_bgColor);
164+
sendCommand(buf);
165+
}
166+
};
167+
}
168+
#endif //RSITE_ARDUINOP_MENU_UART

0 commit comments

Comments
 (0)