Merge pull request #10 from mikrogl/jedi/dev

Jedi/dev
This commit is contained in:
j3d1 2018-11-08 14:39:55 +01:00 committed by GitHub
commit 0cb6b56cc9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
21 changed files with 329 additions and 197 deletions

View file

@ -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

View file

@ -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

28
inc/buttons.h Normal file
View file

@ -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

View file

@ -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);

View file

@ -1,15 +0,0 @@
//
// 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>
#endif //MGL_DMXMENU_BUFFER_8_H

View file

@ -9,7 +9,6 @@
#include "gfx/font.h"
#include "fonts/basic_5x4.h"
#include "gfx/buffer.h"
#include "gfx/buffer_32.h"
class screen;

View file

@ -11,18 +11,19 @@
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;
};
private:
const int maxwidth_;
const int maxheight_;
int allocated_;
screen screen_;
screen& screen_;
int allocate_(node *, int);
@ -34,8 +35,9 @@ private:
public:
static const int maxslots = 10;
Slot slots[maxslots];
node *cursor;
layout(int width) : maxwidth_(width), screen_(width, 32) {
layout(screen& s) : maxwidth_(s.getWidth()), maxheight_(s.getHeight()), screen_(s) {
}

View file

@ -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();
};

View file

@ -2,21 +2,14 @@
#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_print(int x, int y, const char* str);
void hal_render();
void render();
void quit();
extern bool input[4];
#ifndef LINUX
@ -25,7 +18,4 @@ void operator delete(void * ptr);
#endif
extern const int SCREEN_WIDTH;
extern const int SCREEN_HEIGHT;
#endif

View file

@ -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) {
}
@ -30,17 +31,19 @@ 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<typename ... Args>
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 +51,22 @@ public:
node *getChild() {
return child_;
}
node *getNext() {
return next_;
}
node *getCursor() {
return cursor_;
}
node *getParent() {
return parent_;
}
void setCursor(node *c) {
cursor_ = c;
}
};

View file

@ -42,3 +42,7 @@ $(OBJDIR):
clean:
@$(REMOVEDIR) "$(OBJDIR)"
@echo clean $(OBJDIR)
run: $(OBJDIR)/$(TARGET)
@echo run $(OBJDIR)/$(TARGET)
@$(OBJDIR)/$(TARGET)

20
src/buttons.cpp Normal file
View file

@ -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];
}
}
}

View file

@ -1,4 +1,5 @@
#include "buttons.h"
#include "example.h"
#include "node.h"
#include "gfx/canvas.h"
@ -9,15 +10,15 @@ int j = 0;
void fooValue::render(canvas& c){
for(int i = 0; i<32; i++)
for(int i = 0; i<c.getHeight(); i++)
c.draw(0,i);
c.print(5 , 10, title_);
peng[0] = input[0] ? 'L' : ' ';
peng[1] = input[1] ? 'R' : ' ';
peng[2] = input[2] ? 'U' : ' ';
peng[3] = input[3] ? 'D' : ' ';
c.print(10 , 10, title_);
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;
@ -25,16 +26,22 @@ void fooValue::render(canvas& c){
}
void blubValue::render(canvas& c){
for(int i = 0; i<32;i++)
for(int i = 0; i<c.getHeight();i++)
c.draw(0,i);
c.print(5 , 10, title_);
c.print(3 , 15, title_);
}
void menu::render(canvas& c){
for(int i = 0; i<32;i++)
for(int i = 0; i<c.getHeight();i++)
c.draw(0,i);
c.print(5, 10, title_);
int r = 0;
node* ptr = getChild();
while(ptr){
c.print(3,6*r+2, ptr->title_);
ptr = ptr->getNext();
r++;
}
}

View file

@ -1,5 +0,0 @@
//
// Created by jedi on 11/2/18.
//
#include "gfx/buffer_32.h"

View file

@ -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) {

View file

@ -5,7 +5,6 @@
#include "node.h"
#include "gfx/layout.h"
void layout::render() {
for (int j = 0; j < allocated_; j++) {
node *node_ptr;
@ -19,8 +18,10 @@ void layout::render() {
int layout::apply(node* node) {
int rootslot = allocate_(node, maxslots);
int minsize = pack_(rootslot);
int minsize = pack_(rootslot + 1);
expand_(minsize);
if (cursor == nullptr)
cursor = slots[rootslot].content->getCursor();
return 0;
}
@ -45,14 +46,16 @@ 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);
if(slots[i].canvas_)
delete slots[i].canvas_;
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;

View file

@ -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_;
}

View file

@ -1,9 +1,5 @@
#include "hal/hal.h"
const int SCREEN_WIDTH = 128;
const int SCREEN_HEIGHT = 32;
bool input[4];
#ifdef LINUX
#include "hal/linux.cpp"

View file

@ -4,35 +4,39 @@
#include "ssd1306.h"
#include "nano_gfx.h"
#include "buttons.h"
#include <stdlib.h>
#include <stdint.h>
#include <hal/hal.h>
uint8_t canvasData[SCREEN_WIDTH*(SCREEN_HEIGHT/8)];
NanoCanvas canvas(SCREEN_WIDTH, SCREEN_HEIGHT, canvasData);
uint8_t canvasData[128 * (32 / 8)];
NanoCanvas canvas(128, 32, canvasData);
void hal_init(){
void hal_init_screen(int w, int h) {
ssd1306_128x32_i2c_init();
ssd1306_fillScreen( 0x00 );
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<<PC2);
input[1]=in & (1<<PC3);
input[2]=in & (1<<PC4);
input[3]=in & (1<<PC5);
canvas.clear();
while (true) {
ssd1306_fillScreen( 0x00 );
render();
canvas.blt(0, 0);
}

View file

@ -7,88 +7,45 @@
//Using SDL and standard IO
#include <SDL2/SDL.h>
#include <cstdio>
#include <buttons.h>
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) {
printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
exit(1);
}
//Create window
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);
}
/* //Create window
window = SDL_CreateWindow( "SDL Tutorial", 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN|SDL_WINDOW_BORDERLESS );
if( window == NULL ) {
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);
}
}
void hal_draw(int x, int y){
SDL_RenderDrawPoint(renderer, x, y);
}
void hal_print(int x, int y, const char* str){
//TODO delete
}
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;
}
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;
}
break;
case SDL_QUIT:
quit = true;
break;
}
} else {
SDL_Delay(10);
}
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
SDL_RenderClear(renderer);
@ -103,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;

View file

@ -8,10 +8,12 @@
#include "gfx/screen.h"
#include "gfx/canvas.h"
#include "buttons.h"
#ifdef LINUX
#include <SDL2/SDL.h>
#include <stdio.h>
#include <iostream>
extern SDL_Window* window;
@ -20,47 +22,89 @@ 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("foo",
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)
)
);
///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();
return 0;
hal_render();
return 0;
}
void render(){
lp->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_UP:
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;
}
}
}