add dynamic buffers
This commit is contained in:
parent
3f3c40a9b1
commit
d958452e48
14 changed files with 176 additions and 67 deletions
|
@ -49,6 +49,7 @@ LDFLAGS = -Llib -lssd1306
|
|||
#==== Programming Options (avrdude) ============================================
|
||||
|
||||
AVRDUDE_PROGRAMMER = arduino
|
||||
AVRDUDE_PORT = /dev/arduinoACM
|
||||
AVRDUDE_BAUD = 115200
|
||||
|
||||
#AVRDUDE_NO_VERIFY = -V
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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
|
||||
|
|
40
inc/gfx/buffer_1.h
Normal file
40
inc/gfx/buffer_1.h
Normal file
|
@ -0,0 +1,40 @@
|
|||
//
|
||||
// Created by jedi on 11/2/18.
|
||||
//
|
||||
|
||||
#ifndef MGL_DMXMENU_BUFFER_1_H
|
||||
#define MGL_DMXMENU_BUFFER_1_H
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
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
|
37
inc/gfx/buffer_32.h
Normal file
37
inc/gfx/buffer_32.h
Normal file
|
@ -0,0 +1,37 @@
|
|||
//
|
||||
// Created by jedi on 11/2/18.
|
||||
//
|
||||
|
||||
#ifndef MGL_DMXMENU_BUFFER_8_H
|
||||
#define MGL_DMXMENU_BUFFER_8_H
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
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
|
|
@ -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;
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -7,7 +7,6 @@
|
|||
|
||||
#include "hal.h"
|
||||
|
||||
//template <int WIDTH, int HEIGHT>
|
||||
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){
|
||||
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
#ifndef _HAL_H_
|
||||
#define _HAL_H_
|
||||
|
||||
class Layout;
|
||||
#include "col.h"
|
||||
|
||||
#define I_LEFT 0
|
||||
|
|
47
src/col.cpp
47
src/col.cpp
|
@ -1,57 +1,62 @@
|
|||
|
||||
#include <gfx/screen.h>
|
||||
#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; i<usedslots; i++){
|
||||
if(!slots[i].node) break;
|
||||
if(minsum+slots[i].node->view.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;
|
||||
}
|
||||
|
|
|
@ -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; i<size; i++)
|
||||
hal_draw(offset+i,31-i%32);
|
||||
for(int i = 0; i<size; i++)
|
||||
hal_draw(offset+i,31-(i+16)%32);
|
||||
|
||||
hal_print(offset+5 , 10, ptr->title_);
|
||||
for(int i = 0; i<32; i++)
|
||||
c.draw(0,i);
|
||||
for(int i = 0; i<size; i++)
|
||||
c.draw(i,31-i%32);
|
||||
for(int i = 0; i<size; i++)
|
||||
c.draw(i,31-(i+16)%32);
|
||||
|
||||
c.print(5 , 10, ptr->title_);
|
||||
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<size;i++)
|
||||
hal_draw(offset+(i%size),i);
|
||||
c.draw((i%size),i);
|
||||
}
|
||||
|
|
5
src/gfx/buffer_1.cpp
Normal file
5
src/gfx/buffer_1.cpp
Normal file
|
@ -0,0 +1,5 @@
|
|||
//
|
||||
// Created by jedi on 11/2/18.
|
||||
//
|
||||
|
||||
#include "gfx/buffer_1.h"
|
5
src/gfx/buffer_32.cpp
Normal file
5
src/gfx/buffer_32.cpp
Normal file
|
@ -0,0 +1,5 @@
|
|||
//
|
||||
// Created by jedi on 11/2/18.
|
||||
//
|
||||
|
||||
#include "gfx/buffer_32.h"
|
|
@ -4,11 +4,18 @@
|
|||
|
||||
#include "gfx/canvas.h"
|
||||
|
||||
canvas::canvas(int w, int h) : width_(w), height_(h) {
|
||||
canvas::canvas(int w, int h) : width_(w), height_(h), buffer_(w*h) {
|
||||
clear();
|
||||
}
|
||||
|
||||
void canvas::clear() {
|
||||
for (int x = 0; x < width_; x++)
|
||||
for (int y = 0; y < height_; y++)
|
||||
buffer_.set(x * height_ + y, false);
|
||||
}
|
||||
|
||||
void canvas::draw(int x, int y) {
|
||||
hal_draw(x, y);
|
||||
buffer_.set(x * height_ + y, true);
|
||||
}
|
||||
|
||||
void canvas::print(int x, int y, const char *str, font& f) {
|
||||
|
|
|
@ -53,13 +53,15 @@ int main() {
|
|||
|
||||
//render
|
||||
hal_init();
|
||||
//render(l);
|
||||
render(l);
|
||||
|
||||
/*
|
||||
canvas c(64,32);
|
||||
char foo[] = "ABCDEFGHIJKLMOPQRSTUVWXYZ";
|
||||
c.print(1,1,"HELLO");
|
||||
c.print(1,7,"Foo Bar");
|
||||
c.print(1,13,foo);
|
||||
*/
|
||||
//s.draw(0,0,c);
|
||||
|
||||
#ifdef LINUX
|
||||
|
|
Loading…
Reference in a new issue