Skip to content

Commit

Permalink
Clear screen are when changing page, added a demo.
Browse files Browse the repository at this point in the history
  • Loading branch information
brianvanderburg2 committed Mar 29, 2018
1 parent d53b1c4 commit 075fb57
Show file tree
Hide file tree
Showing 2 changed files with 187 additions and 14 deletions.
38 changes: 24 additions & 14 deletions CmdDeck/CmdDeck.ino
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ struct Buttons {
Button buttons[ROWS*COLS];
int count = 0;
bool modified = false;
bool layoutChanged = true;
bool cleared = false;
bool added = false;
int current = -1;
};

Buttons theButtons;
Expand Down Expand Up @@ -236,38 +238,42 @@ void retrieveTouch()
// Drawing functions
////////////////////

void drawBoxes(int sel = -1, bool clear=false) {
void drawBoxes() {
int x, y, w, h;
int button;
if(clear) {
// TODO: clear button area
}

for(button = 0; button < theButtons.count; button++) {

x = col[theButtons.buttons[button].x];
y = row[theButtons.buttons[button].y];
w = theButtons.buttons[button].w;
h = theButtons.buttons[button].h;

tft.drawRect(x, y, BOXSIZE * w + padding * (w - 1), BOXSIZE * h + padding * (h - 1), button == sel ? RED : WHITE);
tft.drawRect(x, y, BOXSIZE * w + padding * (w - 1), BOXSIZE * h + padding * (h - 1), theButtons.current == button ? RED : WHITE);
}

tft.drawRect(edge,edge,tft.width()-padding,info-edge ,WHITE);
}

void bmp(bool force=false){
int button;
if(force || theButtons.modified || theButtons.layoutChanged) {
if(theButtons.layoutChanged) {
drawBoxes(-1, true);
theButtons.layoutChanged = false;
if(force || theButtons.modified || theButtons.added || theButtons.cleared) {
if(theButtons.cleared) {
tft.fillRect(0, row[0], tft.width(), tft.height(), BLACK);
}

if(theButtons.added || theButtons.cleared) {
drawBoxes();
}

for(button = 0; button < theButtons.count; button++) {
if(force || theButtons.buttons[button].modified) {
if(force || theButtons.buttons[button].modified || theButtons.cleared) {
bmpDraw(theButtons.buttons[button].filename,col[theButtons.buttons[button].x]+1,row[theButtons.buttons[button].y]+1);
theButtons.buttons[button].modified = false;
}
}
theButtons.added = false;
theButtons.cleared = false;
theButtons.modified = false;
}
}
Expand Down Expand Up @@ -326,7 +332,8 @@ void chooseButton(){
h = theButtons.buttons[button].h;

if(X > x && X < x + BOXSIZE * w + padding * (w - 1) && Y > y && Y < y + BOXSIZE * h + padding * (h - 1)) {
drawBoxes(button);
theButtons.current = button;
drawBoxes();
Serial.println(button+1);
break;
}
Expand Down Expand Up @@ -519,7 +526,8 @@ bool processButtonCmd(char* cmd) {
}

// Set button filename and set modified stated
strbufcpy(theButtons.buttons[button].filename, start, 25);
strbufcpy(theButtons.buttons[button].filename, start, 20);
strcat(theButtons.buttons[button].filename, ".bmp");
theButtons.buttons[button].modified = true;
theButtons.modified = true;
return true;
Expand All @@ -543,8 +551,8 @@ bool processAddButton(char* cmd) {
theButtons.buttons[idx].modified = true;

theButtons.count++;
theButtons.layoutChanged = true;
theButtons.modified = true;
theButtons.added = true;
return true;
}

Expand Down Expand Up @@ -582,6 +590,8 @@ void processCmd(char* cmd) {
else if(strcmp(cmd, "clearbuttons") == 0) {
theButtons.count = 0;
theButtons.modified = true;
theButtons.cleared = true;
theButtons.current = -1;
}
else if(strcmp(cmd, "reset") == 0) {
resetFunc();
Expand Down
163 changes: 163 additions & 0 deletions demo/test1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
import serial

class MySerial(serial.Serial):
def write(self, what):
print(what.rstrip())
serial.Serial.write(self, what)

class App(object):

def __init__(self, port):
self._port = port
self._page_stack = []
self._p = None
self._lp = None

def run(self):
# Connect and display initial messages
s = self._s = MySerial(self._port, 9600, timeout=None)
self.dump(5)

# Run the loop
while self._p:
if self._p != self._lp:
self._p.display()
self._lp = self._p

line = s.readline().rstrip()
try:
value = int(line)
self._p.on_button(value)
except ValueError:
print(line.rstrip())

def push_page(self, page):
self._page_stack.append(page)
self._p = page

def pop_page(self, n=1):
self._page_stack = self._page_stack[:-n]
if self._page_stack:
self._p = self._page_stack[-1]
else:
self._p = None

def change_page(self, page):
p = self._page_stack.pop()
self._page_stack.append(page)
self._p = page
return p

def dump(self, timeout=0.1):
s = self._s
s.timeout = timeout
line = s.readline()
while(line):
print(line.rstrip())
line = s.readline()

s.timeout = None

@classmethod
def execute(cls, pgcls, port):
app = cls(port)
page = pgcls(app)
app.push_page(page)
app.run()

class Page(object):

def __init__(self, app):
self._a = app
self._buttons = []

self.add_buttons()

def add_buttons(self):
pass

def display(self):
## Redraw the page
s = self._a._s

s.write("freeze\n")
s.readline()
s.write("clearbuttons\n".encode("utf8"))
s.readline()
for i, b in enumerate(self._buttons):
s.write("addbutton {0}{1}{2}{3}\n".format(b.x, b.y, b.w, b.h).encode("utf8"))
s.readline()
self.set_image(i, b.filename)
s.write("unfreeze\n".encode("utf8"))
s.readline()
self._a.dump()

def add_button(self, x, y, w, h, filename="", callback=None):
self._buttons.append(Button(x, y, w, h, filename, callback))
self._buttons[-1].index = len(self._buttons) - 1

def set_image(self, i, filename):
s = self._a._s

b = self._buttons[i]
b.filename = filename
s.write("button{} {}\n".format(i + 1, b.filename).encode("utf8"))
s.readline()

def on_button(self, i):
i = i - 1
if i >= 0 and i < len(self._buttons):
cb = self._buttons[i].callback
if cb:
cb(self._a, self._buttons[i])

class Button(object):

def __init__(self, x, y, w=1, h=1, filename="", callback=None):
self.x = x
self.y = y
self.w = w
self.h = h
self.filename = filename
self.callback = callback


class MyStartPage(Page):

def add_buttons(self):
self.add_button(1, 1, 2, 2, "0001", self._button1)
self.add_button(1, 3, 2, 2, "0002", self._button2)

def _button1(self, app, button):
self.set_image(button.index, "0001")
p = MyExitPage(self._a)
p._orig = self._a.change_page(p)

def _button2(self, app, button):
self.set_image(button.index, "0004")

p = OtherPage(self._a)
self._a.push_page(p)

class MyExitPage(Page):
def add_buttons(self):
self.add_button(1, 1, 2, 2, "0001", self._yes)
self.add_button(4, 1, 2, 2, "0002", self._no)

def _yes(self, app, button):
self._a.pop_page()

def _no(self, app, button):
self._a.change_page(self._orig)

class OtherPage(Page):

def add_buttons(self):
self.add_button(1, 1, 5, 1, "0009", self._button1)

def _button1(self, app, button):
app.pop_page()


App.execute(MyStartPage, "/dev/ttyACM0")

0 comments on commit 075fb57

Please sign in to comment.