|
| 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