implement input iteraction

This commit is contained in:
j3d1 2018-11-06 18:30:21 +01:00
parent c822a75df0
commit 6af3988056
12 changed files with 229 additions and 110 deletions

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

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

View file

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

View file

@ -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<typename ... Args>
@ -58,6 +59,14 @@ public:
node *getCursor() {
return cursor_;
}
node *getParent() {
return parent_;
}
void setCursor(node *c) {
cursor_ = c;
}
};

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

View file

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

View file

@ -1,6 +1,5 @@
#include "hal/hal.h"
bool input[4];
#ifdef LINUX
#include "hal/linux.cpp"

View file

@ -4,34 +4,43 @@
#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;
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<<PC2);
input[1]=in & (1<<PC3);
input[2]=in & (1<<PC4);
input[3]=in & (1<<PC5);
while (true) {
canvas.clear();
render();
canvas.blt(0, 0);

View file

@ -7,16 +7,12 @@
//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) {
@ -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;

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