From d958452e4831228b08a0d6b376b60f7d7d1beea4 Mon Sep 17 00:00:00 2001 From: /jedi/ Date: Mon, 5 Nov 2018 10:53:59 +0100 Subject: [PATCH] add dynamic buffers --- avr.Makefile | 1 + inc/col.h | 9 +++++--- inc/example.h | 6 +++-- inc/gfx/buffer_1.h | 40 ++++++++++++++++++++++++++++++++ inc/gfx/buffer_32.h | 37 ++++++++++++++++++++++++++++++ inc/gfx/canvas.h | 16 +++++++++++-- inc/gfx/screen.h | 8 ++++++- inc/hal.h | 1 + src/col.cpp | 47 +++++++++++++++++++++----------------- src/example.cpp | 53 +++++++++++++++---------------------------- src/gfx/buffer_1.cpp | 5 ++++ src/gfx/buffer_32.cpp | 5 ++++ src/gfx/canvas.cpp | 11 +++++++-- src/main.cpp | 4 +++- 14 files changed, 176 insertions(+), 67 deletions(-) create mode 100644 inc/gfx/buffer_1.h create mode 100644 inc/gfx/buffer_32.h create mode 100644 src/gfx/buffer_1.cpp create mode 100644 src/gfx/buffer_32.cpp diff --git a/avr.Makefile b/avr.Makefile index b670676..61f5651 100644 --- a/avr.Makefile +++ b/avr.Makefile @@ -49,6 +49,7 @@ LDFLAGS = -Llib -lssd1306 #==== Programming Options (avrdude) ============================================ AVRDUDE_PROGRAMMER = arduino +AVRDUDE_PORT = /dev/arduinoACM AVRDUDE_BAUD = 115200 #AVRDUDE_NO_VERIFY = -V diff --git a/inc/col.h b/inc/col.h index 6ca0c68..9fe8675 100644 --- a/inc/col.h +++ b/inc/col.h @@ -2,10 +2,13 @@ #ifndef _COL_H_ #define _COL_H_ -class Node; -typedef void (*drawfun_t)(int offset, int size, Node*); +class canvas; +#include "gfx/canvas.h" -void draw_menu(int, int, Node*); +class Node; +typedef void (*drawfun_t)(int offset, int size, Node*, canvas& c); + +void draw_menu(int, int, Node*, canvas& c); class Drawable{ public: diff --git a/inc/example.h b/inc/example.h index d9d3730..213b6d5 100644 --- a/inc/example.h +++ b/inc/example.h @@ -2,8 +2,10 @@ #ifndef _EXAMPLE_H_ #define _EXAMPLE_H_ -void draw_foo(int offset, int size, Node* ptr); +#include "gfx/canvas.h" -void draw_blub(int offset, int size, Node* ptr); +void draw_foo(int offset, int size, Node* ptr, canvas& c); + +void draw_blub(int offset, int size, Node* ptr, canvas& c); #endif diff --git a/inc/gfx/buffer_1.h b/inc/gfx/buffer_1.h new file mode 100644 index 0000000..3ce889b --- /dev/null +++ b/inc/gfx/buffer_1.h @@ -0,0 +1,40 @@ +// +// Created by jedi on 11/2/18. +// + +#ifndef MGL_DMXMENU_BUFFER_1_H +#define MGL_DMXMENU_BUFFER_1_H + + +#include +#include + +class buffer_1 { +private: + int *ptr_; + int size_; + constexpr static const int block_ = 8 * sizeof(int); +public: + buffer_1(int size) : size_(size) { + ptr_ = (int *) malloc(size * sizeof(int)); + } + + bool get(int i) { + if (i >= size_) return 0; + return ptr_[i/block_] & 1 << (i%block_); + } + + void set(int i, bool v) { + if (i < size_) { + ptr_[i/block_] &= ~(1 << (i%block_)); + ptr_[i/block_] |= v << (i%block_); + } + } + + ~buffer_1() { + free(ptr_); + } +}; + + +#endif //MGL_DMXMENU_BUFFER_1_H diff --git a/inc/gfx/buffer_32.h b/inc/gfx/buffer_32.h new file mode 100644 index 0000000..751c868 --- /dev/null +++ b/inc/gfx/buffer_32.h @@ -0,0 +1,37 @@ +// +// Created by jedi on 11/2/18. +// + +#ifndef MGL_DMXMENU_BUFFER_8_H +#define MGL_DMXMENU_BUFFER_8_H + + +#include +#include + +class buffer_32 { +private: + int size_; + void *ptr; +public: + buffer_32(int size) : size_(size) { + ptr = (uint8_t *) malloc(size * 4); + } + + bool get(int i) { + if (i >= size_) return 0; + return ((int *) ptr)[i]; + } + + void set(int i, bool v) { + if (i < size_) + ((int *) ptr)[i] = v; + } + + ~buffer_32() { + free(ptr); + } +}; + + +#endif //MGL_DMXMENU_BUFFER_8_H diff --git a/inc/gfx/canvas.h b/inc/gfx/canvas.h index 48d4a04..0ee451e 100644 --- a/inc/gfx/canvas.h +++ b/inc/gfx/canvas.h @@ -8,19 +8,31 @@ #include "hal.h" #include "gfx/font.h" #include "fonts/basic_5x4.h" +#include "gfx/buffer_1.h" +#include "gfx/buffer_32.h" + +class screen; class canvas { private: const int width_; const int height_; + buffer_1 buffer_; + + void draw_onto_(screen &); + public: canvas(int w, int h); + void clear(); + void draw(int x, int 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); + void print(int x, int y, char *str, font & = basic_5x4); + + friend screen; }; diff --git a/inc/gfx/screen.h b/inc/gfx/screen.h index 2c14a77..ebdaec7 100644 --- a/inc/gfx/screen.h +++ b/inc/gfx/screen.h @@ -7,7 +7,6 @@ #include "hal.h" -//template class screen { private: const int width_; @@ -22,6 +21,13 @@ public: hal_draw(x,y); } + 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, char* str){ } diff --git a/inc/hal.h b/inc/hal.h index 3eb0af0..a692c64 100644 --- a/inc/hal.h +++ b/inc/hal.h @@ -2,6 +2,7 @@ #ifndef _HAL_H_ #define _HAL_H_ +class Layout; #include "col.h" #define I_LEFT 0 diff --git a/src/col.cpp b/src/col.cpp index 058bf4f..bdb036e 100644 --- a/src/col.cpp +++ b/src/col.cpp @@ -1,57 +1,62 @@ +#include #include "hal.h" #include "col.h" -void Layout::render(){ - for(int j = 0; j < allocated_; j++){ - Node * node; - if((node = slots[j].node) && node->view.render){ - node->view.render(slots[j].pos_,slots[j].size_,node); +void Layout::render() { + for (int j = 0; j < allocated_; j++) { + Node *node; + if ((node = slots[j].node) && node->view.render) { + screen s(128, 32); + canvas c(slots[j].size_, 32); + node->view.render(slots[j].pos_, slots[j].size_, node, c); + s.draw(slots[j].pos_, 0, c); + } } } -int Layout::apply(Node* node){ +int Layout::apply(Node* node) { int rootslot = allocate_(node, maxslots); int minsize = pack_(rootslot); expand_(minsize); return 0; } -int Layout::pack_(int usedslots){ +int Layout::pack_(int usedslots) { int minsum = 0; int i; - for(i = 0; iview.minsize>maxwidth_) break; - minsum+=slots[i].node->view.minsize; + for (i = 0; i < usedslots; i++) { + if (!slots[i].node) break; + if (minsum + slots[i].node->view.minsize > maxwidth_) break; + minsum += slots[i].node->view.minsize; } allocated_ = i; return minsum; } -int Layout::expand_(int packedsize){ +int Layout::expand_(int packedsize) { int diff = maxwidth_ - packedsize; int pos = 0; - for(int i = allocated_-1; i>=0; i--){ - slots[i].pos_=pos; - int size = slots[i].node->view.minsize + (diff*slots[i].node->view.minsize)/packedsize; - if(size > slots[i].node->view.maxsize) + for (int i = allocated_ - 1; i >= 0; i--) { + slots[i].pos_ = pos; + int size = slots[i].node->view.minsize + (diff * slots[i].node->view.minsize) / packedsize; + if (size > slots[i].node->view.maxsize) size = slots[i].node->view.maxsize; - slots[i].size_=size; - pos+=size; + slots[i].size_ = size; + pos += size; } return 0; } -int Layout::allocate_(Node* node, int depth){ - if(Node* next = node->getChild()){ +int Layout::allocate_(Node* node, int depth) { + if (Node *next = node->getChild()) { int i = allocate_(next, depth) + 1; slots[i].node = node; return i; - }else{ + } else { slots[0].node = node; return 0; } diff --git a/src/example.cpp b/src/example.cpp index 101da8c..566af6b 100644 --- a/src/example.cpp +++ b/src/example.cpp @@ -1,29 +1,23 @@ -#include "hal.h" #include "col.h" #include "example.h" -#include "../inc/hal.h" +#include "gfx/canvas.h" char peng[] = {'_','_','_','_','_',0}; int j = 0; -void draw_foo(int offset, int size, Node* ptr){ +void draw_foo(int offset, int size, Node* ptr, canvas& c){ Value* val = (Value*) ptr; - /*debug("|"); - if(ptr) - debug(ptr->title_); - //print(ptr->state); - debug("|");*/ - - for(int i = 0; i<32; i++) - hal_draw(offset,i); - for(int i = 0; ititle_); + for(int i = 0; i<32; i++) + c.draw(0,i); + for(int i = 0; ititle_); peng[0]=input[0]?' ':'#'; peng[1]=input[1]?' ':'#'; peng[2]=input[2]?' ':'#'; @@ -31,32 +25,21 @@ void draw_foo(int offset, int size, Node* ptr){ j++; j%=26; peng[4] = 'a'+j; - hal_print(offset+5 , 20, peng); + c.print(5 , 20, peng); } -void draw_blub(int offset, int size, Node* ptr){ +void draw_blub(int offset, int size, Node* ptr, canvas& c){ Value* val = (Value*) ptr; - /*debug("#"); - if(ptr) - debug(ptr->title_); - //print(ptr->state); - debug("#");*/ for(int i = 0; i<32;i++) - hal_draw(offset,i); + c.draw(0,i); - hal_print(offset+5 , 10, ptr->title_); + c.print(5 , 10, ptr->title_); } -void draw_menu(int offset, int size, Node* val){ - /*if(val&&val->title_){ - debug("_"); - debug(val->title_); - debug("_"); - //print(ptr->state); - }*/ - +void draw_menu(int offset, int size, Node* val, canvas& c){ + for(int i = 0; i<32;i++) - hal_draw(offset,i); + c.draw(0,i); for(int i = 0; i<32 && i