split hal file

This commit is contained in:
j3d1 2018-11-01 14:37:30 +01:00
parent 3e66bd2c38
commit 987aa92a23
8 changed files with 215 additions and 149 deletions

View file

@ -46,7 +46,6 @@ class Node{
Node* getChild(){
return child_;
}
void p(int i);
};
class Value: public Node{
@ -79,7 +78,7 @@ class Layout{
}
int apply(Node*);
Slot slots[maxslots];
void p();
void render();
};
#endif

View file

@ -2,15 +2,23 @@
#ifndef _HAL_H_
#define _HAL_H_
#include "col.h"
void print(const char* str);
void print(char str);
void print(int str);
#define I_LEFT 0
#define I_RIGHT 0
#define I_UP 0
#define I_DOWN 0
void debug(const char* str);
void setup();
void draw(int x, int y);
void render();
void print(int x, int y, const char* str);
void render(Layout&);
void quit();
extern bool input[4];
#ifndef LINUX
void * operator new(unsigned int size);

View file

@ -3,22 +3,8 @@
#include "col.h"
void Node::p(int i){
for(int j = 0; j < i; j++){ print("__"); }
if(view.render)
view.render(0,0,this);
print("\n");
if(child_)
child_->p(i+1);
if(next_)
next_->p(i);
}
void Layout::p(){
void Layout::render(){
for(int j = 0; j < allocated_; j++){
Node * node;
if((node = slots[j].node) && node->view.render){
@ -27,7 +13,6 @@ void Layout::p(){
}
}
int Layout::apply(Node* node){
int rootslot = allocate_(node, maxslots);
int minsize = pack_(rootslot);

View file

@ -2,15 +2,19 @@
#include "hal.h"
#include "col.h"
#include "example.h"
#include "../inc/hal.h"
char peng[] = {'_','_','_','_','_',0};
int j = 0;
void draw_foo(int offset, int size, Node* ptr){
Value* val = (Value*) ptr;
print("|");
/*debug("|");
if(ptr)
print(ptr->title_);
debug(ptr->title_);
//print(ptr->state);
print("|");
debug("|");*/
for(int i = 0; i<32; i++)
draw(offset,i);
@ -18,26 +22,38 @@ void draw_foo(int offset, int size, Node* ptr){
draw(offset+i,31-i%32);
for(int i = 0; i<size; i++)
draw(offset+i,31-(i+16)%32);
print(offset+5 , 10, ptr->title_);
peng[0]=input[0]?' ':'#';
peng[1]=input[1]?' ':'#';
peng[2]=input[2]?' ':'#';
peng[3]=input[3]?' ':'#';
j++;
j%=26;
peng[4] = 'a'+j;
print(offset+5 , 20, peng);
}
void draw_blub(int offset, int size, Node* ptr){
Value* val = (Value*) ptr;
print("#");
/*debug("#");
if(ptr)
print(ptr->title_);
debug(ptr->title_);
//print(ptr->state);
print("#");
debug("#");*/
for(int i = 0; i<32;i++)
draw(offset,i);
print(offset+5 , 10, ptr->title_);
}
void draw_menu(int offset, int size, Node* val){
if(val&&val->title_){
print("_");
print(val->title_);
print("_");
/*if(val&&val->title_){
debug("_");
debug(val->title_);
debug("_");
//print(ptr->state);
}
}*/
for(int i = 0; i<32;i++)
draw(offset,i);

View file

@ -1,122 +1,14 @@
#include "hal.h"
const int SCREEN_WIDTH = 128;
const int SCREEN_HEIGHT = 32;
bool input[4];
#ifdef LINUX
#include <iostream>
//Using SDL and standard IO
#include <SDL2/SDL.h>
#include <stdio.h>
SDL_Window* window = NULL;
SDL_Event event;
SDL_Renderer *renderer;
void setup(){
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, 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);
}
int i;
SDL_CreateWindowAndRenderer(SCREEN_WIDTH, SCREEN_HEIGHT, 0, &window, &renderer);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 0, 255, 255, 255);
}
void draw(int x, int y){
SDL_RenderDrawPoint(renderer, x, y);
}
void render(){
SDL_RenderPresent(renderer);
while (1) {
if (SDL_PollEvent(&event) && event.type == SDL_QUIT)
break;
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
}
void print(const char* str){
std::cout << str << std::flush;
}
void print(char chr){
char arr[] = "_";
arr[0] = chr;
std::cout << arr << std::flush;
}
void print(int str){
std::cout << str << std::flush;
}
#else
#include "hal/linux.cpp"
#endif
#ifdef AVR
#include "ssd1306.h"
#endif
#include "ssd1306.h"
#include "nano_gfx.h"
#include <stdlib.h>
uint8_t canvasData[SCREEN_WIDTH*(SCREEN_HEIGHT/8)];
NanoCanvas canvas(SCREEN_WIDTH, SCREEN_HEIGHT, canvasData);
void setup(){
ssd1306_128x32_i2c_init();
ssd1306_fillScreen( 0x00 );
canvas.clear();
}
void draw(int x, int y){
canvas.putPixel(x, y);
}
void render(){
canvas.blt(0,0);
}
void * operator new(unsigned int size)
{
return malloc(size);
}
void operator delete(void * ptr)
{
free(ptr);
}
void print(const char* str){
//TODO implement
}
void print(char str){
//TODO implement
}
void print(int str){
//TODO implement
}
#include "hal/avr.cpp"
#endif

58
src/hal/avr.cpp Normal file
View file

@ -0,0 +1,58 @@
//
// Created by jedi on 10/30/18.
//
#include "ssd1306.h"
#include "nano_gfx.h"
#include <stdlib.h>
uint8_t canvasData[SCREEN_WIDTH*(SCREEN_HEIGHT/8)];
NanoCanvas canvas(SCREEN_WIDTH, SCREEN_HEIGHT, canvasData);
void setup(){
ssd1306_128x32_i2c_init();
ssd1306_fillScreen( 0x00 );
//ssd1306_setFixedFont(ssd1306xled_font6x8);
ssd1306_setFixedFont(ssd1306xled_font5x7);
}
void draw(int x, int y){
canvas.putPixel(x, y);
}
void print(int x, int y, const char* str){
canvas.printFixed(x, y, str, STYLE_NORMAL);
}
void render(Layout& layout) {
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();
layout.render();
canvas.blt(0, 0);
}
}
void * operator new(unsigned int size)
{
return malloc(size);
}
void operator delete(void * ptr)
{
free(ptr);
}
void debug(const char* str){
//TODO implement
}

109
src/hal/linux.cpp Normal file
View file

@ -0,0 +1,109 @@
//
// Created by jedi on 10/30/18.
//
#include <iostream>
//Using SDL and standard IO
#include <SDL2/SDL.h>
#include <stdio.h>
SDL_Window* window = NULL;
SDL_Event event;
SDL_Renderer *renderer;
void setup(){
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, 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);
}
int i;
SDL_CreateWindowAndRenderer(SCREEN_WIDTH, SCREEN_HEIGHT, 0, &window, &renderer);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 0, 255, 255, 255);
}
void draw(int x, int y){
SDL_RenderDrawPoint(renderer, x, y);
}
void print(int x, int y, const char* str){
//TODO implement
}
void render(Layout& layout) {
bool quit = false;
while (!quit) {
if (SDL_WaitEvent(&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;
}
}
layout.render();
SDL_RenderPresent(renderer);
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
}
void debug(const char* str){
std::cout << str << std::flush;
}

View file

@ -42,8 +42,7 @@ int main() {
//render
setup();
l.p();
render();
render(l);
return 0;
}