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