From 6570f7fafce8949c9779e090b7bd1e7ec0fd624a Mon Sep 17 00:00:00 2001 From: /jedi/ Date: Tue, 6 Nov 2018 09:40:27 +0100 Subject: [PATCH 1/4] support closing the Window with 'q' or ESC on linux --- src/hal/linux.cpp | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/hal/linux.cpp b/src/hal/linux.cpp index 72dbe2e..c5ebd9e 100644 --- a/src/hal/linux.cpp +++ b/src/hal/linux.cpp @@ -18,16 +18,19 @@ void hal_init() { exit(1); } - - /* //Create window - window = SDL_CreateWindow( "SDL Tutorial", 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN|SDL_WINDOW_BORDERLESS ); - if( window == NULL ) { + //Create window + window = SDL_CreateWindow("SDL Tutorial", 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, + SDL_WINDOW_SHOWN); //|SDL_WINDOW_BORDERLESS + if (window == nullptr) { printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() ); exit(1); } - */ - SDL_CreateWindowAndRenderer(SCREEN_WIDTH, SCREEN_HEIGHT, 0, &window, &renderer); + renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); + if (renderer == nullptr) { + printf("Renderer could not be created! SDL_Error: %s\n", SDL_GetError()); + exit(1); + } } @@ -62,6 +65,10 @@ void hal_render() { case SDLK_DOWN: input[I_DOWN]=true; break; + case 'q': + case 0x1b: //ESC + quit = true; + break; } break; @@ -79,6 +86,8 @@ void hal_render() { case SDLK_DOWN: input[I_DOWN]=false; break; + default: + printf("key %x\n", event.key.keysym.sym); } break; From c822a75df0fa0de7c9ae53a48a6cd88017c78196 Mon Sep 17 00:00:00 2001 From: /jedi/ Date: Tue, 6 Nov 2018 16:06:05 +0100 Subject: [PATCH 2/4] some bugfixes --- inc/example.h | 2 +- inc/gfx/buffer_32.h | 15 --------------- inc/gfx/canvas.h | 1 - inc/gfx/layout.h | 5 +++-- inc/gfx/screen.h | 23 +++++++---------------- inc/hal/hal.h | 6 +----- inc/node.h | 16 +++++++++++++--- src/example.cpp | 18 ++++++++++++------ src/gfx/buffer_32.cpp | 5 ----- src/gfx/canvas.cpp | 3 ++- src/gfx/layout.cpp | 7 +++---- src/gfx/screen.cpp | 31 +++++++++++++++++++++++++++++++ src/hal.cpp | 3 --- src/hal/linux.cpp | 19 ++++++++++--------- src/main.cpp | 33 ++++++++++----------------------- 15 files changed, 93 insertions(+), 94 deletions(-) delete mode 100644 inc/gfx/buffer_32.h delete mode 100644 src/gfx/buffer_32.cpp diff --git a/inc/example.h b/inc/example.h index 27aa4bb..7486609 100644 --- a/inc/example.h +++ b/inc/example.h @@ -17,7 +17,7 @@ public: class fooValue : public value { public: - fooValue(const char* str, int val) : value(str, val, 64, 96) { + fooValue(const char* str, int val) : value(str, val, 32, 48) { } void render(canvas &c); diff --git a/inc/gfx/buffer_32.h b/inc/gfx/buffer_32.h deleted file mode 100644 index 869f04f..0000000 --- a/inc/gfx/buffer_32.h +++ /dev/null @@ -1,15 +0,0 @@ -// -// Created by jedi on 11/2/18. -// - -#ifndef MGL_DMXMENU_BUFFER_8_H -#define MGL_DMXMENU_BUFFER_8_H - - -#include -#include - - - - -#endif //MGL_DMXMENU_BUFFER_8_H diff --git a/inc/gfx/canvas.h b/inc/gfx/canvas.h index c65a4e6..d055f13 100644 --- a/inc/gfx/canvas.h +++ b/inc/gfx/canvas.h @@ -9,7 +9,6 @@ #include "gfx/font.h" #include "fonts/basic_5x4.h" #include "gfx/buffer.h" -#include "gfx/buffer_32.h" class screen; diff --git a/inc/gfx/layout.h b/inc/gfx/layout.h index a947dd4..1a9b80f 100644 --- a/inc/gfx/layout.h +++ b/inc/gfx/layout.h @@ -21,8 +21,9 @@ class layout { private: const int maxwidth_; + const int maxheight_; int allocated_; - screen screen_; + screen& screen_; int allocate_(node *, int); @@ -35,7 +36,7 @@ public: static const int maxslots = 10; Slot slots[maxslots]; - layout(int width) : maxwidth_(width), screen_(width, 32) { + layout(screen& s) : maxwidth_(s.getWidth()), maxheight_(s.getHeight()), screen_(s) { } diff --git a/inc/gfx/screen.h b/inc/gfx/screen.h index bb06ef8..d6e2d7e 100644 --- a/inc/gfx/screen.h +++ b/inc/gfx/screen.h @@ -14,28 +14,19 @@ private: const int height_; public: - screen(int w, int h) : width_(w), height_(h) { + screen(int w, int h); - } + void draw(int x, int y); - void draw(int x, int y) { - hal_draw(x, y); - } + void draw(int x, int y, canvas &c); - void draw(int x, int y, canvas &c) { - for (int x_ = 0; x_ < 128; x_++) - for (int y_ = 0; y_ < 32; y_++) - if (c.buffer_.get(x_ * 32 + y_)) - hal_draw(x + x_, y + y_); - } + void print(int x, int y, const char *str, font & = basic_5x4); - void print(int x, int y, const char *str, font & = basic_5x4){ + void print(int x, int y, char *str, font & = basic_5x4); - } + int getWidth(); - void print(int x, int y, char *str, font & = basic_5x4){ - - } + int getHeight(); }; diff --git a/inc/hal/hal.h b/inc/hal/hal.h index efae4f7..6dfe0c9 100644 --- a/inc/hal/hal.h +++ b/inc/hal/hal.h @@ -10,11 +10,10 @@ void debug(const char* str); void hal_init(); +void hal_init_screen(int w, int h); void hal_draw(int x, int y); -void hal_print(int x, int y, const char* str); void hal_render(); void render(); -void quit(); extern bool input[4]; @@ -25,7 +24,4 @@ void operator delete(void * ptr); #endif -extern const int SCREEN_WIDTH; -extern const int SCREEN_HEIGHT; - #endif diff --git a/inc/node.h b/inc/node.h index a838572..0c8114c 100644 --- a/inc/node.h +++ b/inc/node.h @@ -9,9 +9,10 @@ class node { private: - node *parent_ = 0; - node *child_ = 0; - node *next_ = 0; + node *parent_ = nullptr; + node *child_ = nullptr; + node *cursor_ = nullptr; + node *next_ = nullptr; void addNodes(bool b) { } @@ -41,6 +42,7 @@ public: template node(const char *t, node *n, Args ... ns) : title_(t), minsize(24), maxsize(48) { child_ = n; + cursor_ = n; n->parent_ = this; child_->addNodes(true, ns...); } @@ -48,6 +50,14 @@ public: node *getChild() { return child_; } + + node *getNext() { + return next_; + } + + node *getCursor() { + return cursor_; + } }; diff --git a/src/example.cpp b/src/example.cpp index 40d7312..1fe224f 100644 --- a/src/example.cpp +++ b/src/example.cpp @@ -9,11 +9,11 @@ int j = 0; void fooValue::render(canvas& c){ - for(int i = 0; i<32; i++) + for(int i = 0; ititle_); + ptr = ptr->getNext(); + r++; + } } diff --git a/src/gfx/buffer_32.cpp b/src/gfx/buffer_32.cpp deleted file mode 100644 index 2ac949c..0000000 --- a/src/gfx/buffer_32.cpp +++ /dev/null @@ -1,5 +0,0 @@ -// -// Created by jedi on 11/2/18. -// - -#include "gfx/buffer_32.h" diff --git a/src/gfx/canvas.cpp b/src/gfx/canvas.cpp index ba98811..3ff9fcc 100644 --- a/src/gfx/canvas.cpp +++ b/src/gfx/canvas.cpp @@ -15,7 +15,8 @@ void canvas::clear() { } void canvas::draw(int x, int y) { - buffer_.set(x * height_ + y, true); + if (y < height_ && x < width_) + buffer_.set(x * height_ + y, true); } void canvas::print(int x, int y, const char *str, font& f) { diff --git a/src/gfx/layout.cpp b/src/gfx/layout.cpp index 332ad93..93602aa 100644 --- a/src/gfx/layout.cpp +++ b/src/gfx/layout.cpp @@ -5,7 +5,6 @@ #include "node.h" #include "gfx/layout.h" - void layout::render() { for (int j = 0; j < allocated_; j++) { node *node_ptr; @@ -18,7 +17,7 @@ void layout::render() { } int layout::apply(node* node) { - int rootslot = allocate_(node, maxslots); + int rootslot = allocate_(node, maxslots)+1; int minsize = pack_(rootslot); expand_(minsize); return 0; @@ -45,14 +44,14 @@ int layout::expand_(int packedsize) { if (size > slots[i].content->maxsize) size = slots[i].content->maxsize; slots[i].size_ = size; - slots[i].canvas_ = new canvas(size, 32); + slots[i].canvas_ = new canvas(size, maxheight_); pos += size; } return 0; } int layout::allocate_(node* current, int depth) { - if (node* next = current->getChild()) { + if (node* next = current->getCursor()) { int i = allocate_(next, depth) + 1; slots[i].content = current; return i; diff --git a/src/gfx/screen.cpp b/src/gfx/screen.cpp index 8f18dc7..2f491be 100644 --- a/src/gfx/screen.cpp +++ b/src/gfx/screen.cpp @@ -3,3 +3,34 @@ // #include "gfx/screen.h" + +screen::screen(int w, int h) : width_(w), height_(h) { + hal_init_screen(w,h); +} + +void screen::draw(int x, int y) { + hal_draw(x, y); +} + +void screen::draw(int x, int y, canvas &c) { + for (int x_ = 0; x_ < width_; x_++) + for (int y_ = 0; y_ < height_; y_++) + if (c.buffer_.get(x_ * height_ + y_)) + hal_draw(x + x_, y + y_); +} + +void screen::print(int x, int y, const char *str, font& f) { + //f.print(x, y, str); +} + +void screen::print(int x, int y, char *str, font& f) { + //f.print(x, y, str); +} + +int screen::getWidth(){ + return width_; +} + +int screen::getHeight(){ + return height_; +} \ No newline at end of file diff --git a/src/hal.cpp b/src/hal.cpp index 006655f..4ac9e38 100644 --- a/src/hal.cpp +++ b/src/hal.cpp @@ -1,8 +1,5 @@ #include "hal/hal.h" -const int SCREEN_WIDTH = 128; -const int SCREEN_HEIGHT = 32; - bool input[4]; #ifdef LINUX diff --git a/src/hal/linux.cpp b/src/hal/linux.cpp index c5ebd9e..da7ecab 100644 --- a/src/hal/linux.cpp +++ b/src/hal/linux.cpp @@ -13,35 +13,36 @@ SDL_Event event; SDL_Renderer *renderer; void hal_init() { + + +} + + +void hal_init_screen(int width, int height){ if (SDL_Init(SDL_INIT_VIDEO) < 0) { printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError()); exit(1); } //Create window - window = SDL_CreateWindow("SDL Tutorial", 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, + window = SDL_CreateWindow("SDL Tutorial", 0, 0, width, height, SDL_WINDOW_SHOWN); //|SDL_WINDOW_BORDERLESS if (window == nullptr) { - printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() ); - exit(1); - } + printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() ); + exit(1); + } renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); if (renderer == nullptr) { printf("Renderer could not be created! SDL_Error: %s\n", SDL_GetError()); exit(1); } - } void hal_draw(int x, int y){ SDL_RenderDrawPoint(renderer, x, y); } -void hal_print(int x, int y, const char* str){ - //TODO delete -} - void hal_render() { bool quit = false; diff --git a/src/main.cpp b/src/main.cpp index 9e4d8fb..e0373f6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -20,17 +20,17 @@ extern SDL_Renderer *renderer; #endif -layout* lp; + +screen s(128,32); +layout l(s); +menu* root; int main() { - - - //build tree - menu root(nullptr, - new menu("foo1", - new menu("foo2", - new menu("foo3", new fooValue("FOO4",5)) + root = new menu("", + new menu("fooa", + new menu("foob", + new menu("fooc", new fooValue("FOOd",5)) ), new menu("foo", new fooValue("BAR",5)), new menu("foo", new fooValue("BAZ",5)) @@ -40,20 +40,6 @@ int main() { new menu("blub", new blubValue("BLUB",5)) ); - ///root.p(0); - - - //walk tree + allocate slots - //position slots - // 128 x 32 - screen s(128, 32);//<128, 32> s; - - layout l(128); - l.apply(&root); - lp=&l; - - - //render hal_init(); hal_render(); @@ -62,5 +48,6 @@ int main() { } void render(){ - lp->render(); + l.apply(root); + l.render(); } \ No newline at end of file From 6af3988056fe3ff9e36b8ec4ead1c69c3cfcd12f Mon Sep 17 00:00:00 2001 From: /jedi/ Date: Tue, 6 Nov 2018 18:30:21 +0100 Subject: [PATCH 3/4] implement input iteraction --- OBJECTS | 2 +- inc/buttons.h | 28 +++++++++++ inc/gfx/layout.h | 9 ++-- inc/hal/hal.h | 10 +--- inc/node.h | 17 +++++-- src/buttons.cpp | 20 ++++++++ src/example.cpp | 9 ++-- src/gfx/layout.cpp | 6 ++- src/hal.cpp | 1 - src/hal/avr.cpp | 37 +++++++++------ src/hal/linux.cpp | 113 ++++++++++++++++++++++----------------------- src/main.cpp | 87 ++++++++++++++++++++++++++++------ 12 files changed, 229 insertions(+), 110 deletions(-) create mode 100644 inc/buttons.h create mode 100644 src/buttons.cpp diff --git a/OBJECTS b/OBJECTS index 9e4a69d..b8c836d 100644 --- a/OBJECTS +++ b/OBJECTS @@ -1,5 +1,5 @@ TARGET = main -SRC = hal.cpp main.cpp example.cpp +SRC = hal.cpp main.cpp example.cpp buttons.cpp SRC += gfx/screen.cpp gfx/canvas.cpp gfx/font.cpp gfx/layout.cpp gfx/buffer.cpp diff --git a/inc/buttons.h b/inc/buttons.h new file mode 100644 index 0000000..549cdea --- /dev/null +++ b/inc/buttons.h @@ -0,0 +1,28 @@ +// +// Created by jedi on 11/6/18. +// + +#ifndef MGL_DMXMENU_INPUT_H +#define MGL_DMXMENU_INPUT_H + + + +#define I_LEFT 0 +#define I_RIGHT 1 +#define I_UP 2 +#define I_DOWN 3 + + +class buttons { +private: + void onPush(int id, bool state); +public: + bool raw[4]={false,false, false, false}; + bool last[4]={false,false, false, false}; + void poll(); +}; + + +extern buttons input; + +#endif //MGL_DMXMENU_INPUT_H diff --git a/inc/gfx/layout.h b/inc/gfx/layout.h index 1a9b80f..0de8ff8 100644 --- a/inc/gfx/layout.h +++ b/inc/gfx/layout.h @@ -11,11 +11,11 @@ class layout { class Slot { private: - int pos_; - int size_; - canvas *canvas_ = 0; + int pos_ = 0; + int size_ = 0; + canvas *canvas_ = nullptr; public: - node *content; + node *content = nullptr; friend layout; }; @@ -35,6 +35,7 @@ private: public: static const int maxslots = 10; Slot slots[maxslots]; + node *cursor; layout(screen& s) : maxwidth_(s.getWidth()), maxheight_(s.getHeight()), screen_(s) { diff --git a/inc/hal/hal.h b/inc/hal/hal.h index 6dfe0c9..53c0818 100644 --- a/inc/hal/hal.h +++ b/inc/hal/hal.h @@ -2,21 +2,15 @@ #ifndef _HAL_H_ #define _HAL_H_ -#define I_LEFT 0 -#define I_RIGHT 1 -#define I_UP 2 -#define I_DOWN 3 - void debug(const char* str); -void hal_init(); void hal_init_screen(int w, int h); + +void hal_poll_input(bool *ptr); void hal_draw(int x, int y); void hal_render(); void render(); -extern bool input[4]; - #ifndef LINUX void * operator new(unsigned int size); diff --git a/inc/node.h b/inc/node.h index 0c8114c..429434a 100644 --- a/inc/node.h +++ b/inc/node.h @@ -31,12 +31,13 @@ public: virtual void render(canvas &c) = 0; - node(int min = 24, int max = 48) : next_(nullptr), child_(nullptr), parent_(nullptr), minsize(min), maxsize(max) { + explicit node(int min = 24, int max = 48) : next_(nullptr), child_(nullptr), parent_(nullptr), minsize(min), + maxsize(max) { } - node(const char *t, int min = 24, int max = 48) : title_(t), minsize(min), maxsize(max), next_(nullptr), - child_(nullptr), - parent_(nullptr) { + explicit node(const char *t, int min = 24, int max = 48) : title_(t), minsize(min), maxsize(max), next_(nullptr), + child_(nullptr), + parent_(nullptr) { } template @@ -58,6 +59,14 @@ public: node *getCursor() { return cursor_; } + + node *getParent() { + return parent_; + } + + void setCursor(node *c) { + cursor_ = c; + } }; diff --git a/src/buttons.cpp b/src/buttons.cpp new file mode 100644 index 0000000..cece779 --- /dev/null +++ b/src/buttons.cpp @@ -0,0 +1,20 @@ +// +// Created by jedi on 11/6/18. +// + +#include "hal/hal.h" +#include "buttons.h" + + +buttons input; + +void buttons::poll() { + hal_poll_input(raw); + + for(int i = 0; i < 4; i++){ + if(raw[i]!=last[i]){ + onPush(i, raw[i]); + last[i]=raw[i]; + } + } +} \ No newline at end of file diff --git a/src/example.cpp b/src/example.cpp index 1fe224f..6e9a444 100644 --- a/src/example.cpp +++ b/src/example.cpp @@ -1,4 +1,5 @@ +#include "buttons.h" #include "example.h" #include "node.h" #include "gfx/canvas.h" @@ -14,10 +15,10 @@ void fooValue::render(canvas& c){ c.print(10 , 10, title_); - peng[0] = input[0] ? 'L' : ' '; - peng[1] = input[1] ? 'R' : ' '; - peng[2] = input[2] ? 'U' : ' '; - peng[3] = input[3] ? 'D' : ' '; + peng[0] = input.raw[0] ? 'L' : ' '; + peng[1] = input.raw[1] ? 'R' : ' '; + peng[2] = input.raw[2] ? 'U' : ' '; + peng[3] = input.raw[3] ? 'D' : ' '; j++; j %= 10; peng[4] = 'A' + j; diff --git a/src/gfx/layout.cpp b/src/gfx/layout.cpp index 93602aa..01cd19c 100644 --- a/src/gfx/layout.cpp +++ b/src/gfx/layout.cpp @@ -17,9 +17,11 @@ void layout::render() { } int layout::apply(node* node) { - int rootslot = allocate_(node, maxslots)+1; - int minsize = pack_(rootslot); + int rootslot = allocate_(node, maxslots); + int minsize = pack_(rootslot + 1); expand_(minsize); + if (cursor == nullptr) + cursor = slots[rootslot].content->getCursor(); return 0; } diff --git a/src/hal.cpp b/src/hal.cpp index 4ac9e38..37e98d9 100644 --- a/src/hal.cpp +++ b/src/hal.cpp @@ -1,6 +1,5 @@ #include "hal/hal.h" -bool input[4]; #ifdef LINUX #include "hal/linux.cpp" diff --git a/src/hal/avr.cpp b/src/hal/avr.cpp index 31f471f..f560829 100644 --- a/src/hal/avr.cpp +++ b/src/hal/avr.cpp @@ -4,34 +4,43 @@ #include "ssd1306.h" #include "nano_gfx.h" +#include "buttons.h" #include +#include +#include -uint8_t canvasData[SCREEN_WIDTH*(SCREEN_HEIGHT/8)]; -NanoCanvas canvas(SCREEN_WIDTH, SCREEN_HEIGHT, canvasData); +uint8_t *canvasData; +NanoCanvas *canvas; -void hal_init(){ +void hal_init_screen(int w, int h) { ssd1306_128x32_i2c_init(); ssd1306_fillScreen( 0x00 ); + canvasData = new uint8_t[w * (h / 8)]; + canvas = new NanoCanvas(w, h, canvasData); canvas.clear(); + + DDRD = 0; + PORTD |= (1 << PC2) | (1 << PC3) | (1 << PC4) | (1 << PC5); } void hal_draw(int x, int y){ canvas.putPixel(x, y); } + +void hal_poll_input(bool *ptr) { + int in = PIND & 0x3c; + + ptr[0] = in & (1 << PC2); + ptr[1] = in & (1 << PC3); + ptr[2] = in & (1 << PC4); + ptr[3] = in & (1 << PC5); + +} + void hal_render() { - DDRD = 0; - PORTD |= (1 << PC2)|(1 << PC3)|(1 << PC4)|(1 << PC5); - - while (1) { - int in = PIND & 0x3c; - - input[0]=in & (1< #include +#include SDL_Window *window = nullptr; SDL_Event event; SDL_Renderer *renderer; -void hal_init() { - - -} - void hal_init_screen(int width, int height){ if (SDL_Init(SDL_INIT_VIDEO) < 0) { @@ -43,62 +39,13 @@ void hal_draw(int x, int y){ SDL_RenderDrawPoint(renderer, x, y); } + +bool quit = false; + void hal_render() { - bool quit = false; while (!quit) { - if (SDL_PollEvent(&event)) { - - - switch (event.type) { - case SDL_KEYDOWN: - switch (event.key.keysym.sym) { - case SDLK_LEFT: - input[I_LEFT]=true; - break; - case SDLK_RIGHT: - input[I_RIGHT]=true; - break; - case SDLK_UP: - input[I_UP]=true; - break; - case SDLK_DOWN: - input[I_DOWN]=true; - break; - case 'q': - case 0x1b: //ESC - quit = true; - break; - } - break; - - case SDL_KEYUP: - switch (event.key.keysym.sym) { - case SDLK_LEFT: - input[I_LEFT]=false; - break; - case SDLK_RIGHT: - input[I_RIGHT]=false; - break; - case SDLK_UP: - input[I_UP]=false; - break; - case SDLK_DOWN: - input[I_DOWN]=false; - break; - default: - printf("key %x\n", event.key.keysym.sym); - } - break; - - case SDL_QUIT: - quit = true; - break; - } - } else { - SDL_Delay(10); - } SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0); SDL_RenderClear(renderer); @@ -113,6 +60,58 @@ void hal_render() { } +void hal_poll_input(bool *ptr) { + if (SDL_PollEvent(&event)) { + + + switch (event.type) { + case SDL_KEYDOWN: + switch (event.key.keysym.sym) { + case SDLK_LEFT: + ptr[I_LEFT] = true; + break; + case SDLK_RIGHT: + ptr[I_RIGHT] = true; + break; + case SDLK_UP: + ptr[I_UP] = true; + break; + case SDLK_DOWN: + ptr[I_DOWN] = true; + break; + case 'q': + case 0x1b: //ESC + quit = true; + break; + } + break; + + case SDL_KEYUP: + switch (event.key.keysym.sym) { + case SDLK_LEFT: + ptr[I_LEFT] = false; + break; + case SDLK_RIGHT: + ptr[I_RIGHT] = false; + break; + case SDLK_UP: + ptr[I_UP] = false; + break; + case SDLK_DOWN: + ptr[I_DOWN] = false; + break; + } + break; + + case SDL_QUIT: + quit = true; + break; + } + } else { + SDL_Delay(10); + } +} + void debug(const char* str){ std::cout << str << std::flush; diff --git a/src/main.cpp b/src/main.cpp index e0373f6..348b6e6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,10 +8,12 @@ #include "gfx/screen.h" #include "gfx/canvas.h" +#include "buttons.h" #ifdef LINUX #include #include +#include extern SDL_Window* window; @@ -27,27 +29,82 @@ menu* root; int main() { - root = new menu("", - new menu("fooa", - new menu("foob", - new menu("fooc", new fooValue("FOOd",5)) + root = new menu("", + new menu("foo", + new menu("bar", + new menu("baz", + new fooValue("I", 5), + new fooValue("II", 5), + new fooValue("III", 5), + new fooValue("IIII", 5) + ), + new menu("peng", + new fooValue("o", 5), + new fooValue("oo", 5), + new fooValue("o o", 5) + ) + ), + new menu("x", + new fooValue("x x", 5), + new fooValue("xxx", 5) + ), + new menu("y", + new fooValue("y y", 5), + new fooValue("yyy", 5) + ) ), - new menu("foo", new fooValue("BAR",5)), - new menu("foo", new fooValue("BAZ",5)) - ), - new menu("blub", new blubValue("BLUB",5)), - new menu("blub", new blubValue("BLUB",5)), - new menu("blub", new blubValue("BLUB",5)) + new menu("a", + new blubValue("a a", 5), + new blubValue("aa", 5), + new blubValue("aaa", 5) + ), + new menu("b", + new blubValue("b b", 5), + new blubValue("bb", 5), + new blubValue("bbb", 5) + ), + new menu("c", + new blubValue("c c", 5), + new blubValue("cc", 5), + new blubValue("ccc", 5) + ) ); - hal_init(); - hal_render(); - - - return 0; + hal_render(); + return 0; } void render(){ l.apply(root); + input.poll(); l.render(); +} + +void buttons::onPush(int id, bool state) { + if (state) { + node *c; + node *p; + switch (id) { + case I_DOWN: + c = l.cursor; + p = c->getParent(); + if ((c = c->getNext()) || p && (c = p->getChild())) { + p->setCursor(c); + l.cursor = c; + } + break; + case I_RIGHT: + c = l.cursor; + if (c = c->getCursor()) { + l.cursor = c; + } + break; + case I_LEFT: + c = l.cursor; + if (c = c->getParent()) { + l.cursor = c; + } + break; + } + } } \ No newline at end of file From 806fc7c3e5d84b38074e78e9d32c9fc31a5ebb42 Mon Sep 17 00:00:00 2001 From: /jedi/ Date: Thu, 8 Nov 2018 14:37:35 +0100 Subject: [PATCH 4/4] fix memory leak in layout.cpp --- Makefile | 6 +++++- linux.Makefile | 4 ++++ src/gfx/layout.cpp | 2 ++ src/hal/avr.cpp | 19 +++++++------------ src/main.cpp | 26 +++++++++++++------------- 5 files changed, 31 insertions(+), 26 deletions(-) diff --git a/Makefile b/Makefile index 6a32bbf..116ed2a 100644 --- a/Makefile +++ b/Makefile @@ -14,9 +14,13 @@ lib/libssd1306.a: @mkdir -p lib @cp deps/ssd1306/bld/libssd1306.a lib/libssd1306.a -flash: +flash: avr @make -f avr.Makefile flash +run: linux + @make -f linux.Makefile run + + clean: @rm -r build @echo clean all diff --git a/linux.Makefile b/linux.Makefile index 7f3236b..f6f1ca3 100644 --- a/linux.Makefile +++ b/linux.Makefile @@ -42,3 +42,7 @@ $(OBJDIR): clean: @$(REMOVEDIR) "$(OBJDIR)" @echo clean $(OBJDIR) + +run: $(OBJDIR)/$(TARGET) + @echo run $(OBJDIR)/$(TARGET) + @$(OBJDIR)/$(TARGET) diff --git a/src/gfx/layout.cpp b/src/gfx/layout.cpp index 01cd19c..7632851 100644 --- a/src/gfx/layout.cpp +++ b/src/gfx/layout.cpp @@ -46,6 +46,8 @@ int layout::expand_(int packedsize) { if (size > slots[i].content->maxsize) size = slots[i].content->maxsize; slots[i].size_ = size; + if(slots[i].canvas_) + delete slots[i].canvas_; slots[i].canvas_ = new canvas(size, maxheight_); pos += size; } diff --git a/src/hal/avr.cpp b/src/hal/avr.cpp index f560829..461f253 100644 --- a/src/hal/avr.cpp +++ b/src/hal/avr.cpp @@ -9,15 +9,11 @@ #include #include -uint8_t *canvasData; -NanoCanvas *canvas; +uint8_t canvasData[128 * (32 / 8)]; +NanoCanvas canvas(128, 32, canvasData); void hal_init_screen(int w, int h) { ssd1306_128x32_i2c_init(); - ssd1306_fillScreen( 0x00 ); - canvasData = new uint8_t[w * (h / 8)]; - canvas = new NanoCanvas(w, h, canvasData); - canvas.clear(); DDRD = 0; PORTD |= (1 << PC2) | (1 << PC3) | (1 << PC4) | (1 << PC5); @@ -31,17 +27,16 @@ void hal_draw(int x, int y){ void hal_poll_input(bool *ptr) { int in = PIND & 0x3c; - ptr[0] = in & (1 << PC2); - ptr[1] = in & (1 << PC3); - ptr[2] = in & (1 << PC4); - ptr[3] = in & (1 << PC5); + ptr[0] = !(in & (1 << PC2)); + ptr[1] = !(in & (1 << PC3)); + ptr[2] = !(in & (1 << PC4)); + ptr[3] = !(in & (1 << PC5)); } void hal_render() { - while (true) { - canvas.clear(); + ssd1306_fillScreen( 0x00 ); render(); canvas.blt(0, 0); } diff --git a/src/main.cpp b/src/main.cpp index 348b6e6..a415d41 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -31,19 +31,17 @@ int main() { root = new menu("", new menu("foo", - new menu("bar", - new menu("baz", - new fooValue("I", 5), - new fooValue("II", 5), - new fooValue("III", 5), - new fooValue("IIII", 5) - ), - new menu("peng", - new fooValue("o", 5), - new fooValue("oo", 5), - new fooValue("o o", 5) - ) - ), + new menu("baz", + new fooValue("I", 5), + new fooValue("II", 5), + new fooValue("III", 5), + new fooValue("IIII", 5) + ), + new menu("peng", + new fooValue("o", 5), + new fooValue("oo", 5), + new fooValue("o o", 5) + ), new menu("x", new fooValue("x x", 5), new fooValue("xxx", 5) @@ -93,6 +91,8 @@ void buttons::onPush(int id, bool state) { l.cursor = c; } break; + case I_UP: + break; case I_RIGHT: c = l.cursor; if (c = c->getCursor()) {