From 1f2af9f2fbc0c9027d158a18abffecbb8e1d8cbd Mon Sep 17 00:00:00 2001 From: t-moe Date: Fri, 3 Apr 2015 17:45:57 +0200 Subject: [PATCH] Added more tft functions to common and emulator. Fixed eventloop. --- common/app/app.c | 15 ++++++++- common/lowlevel/ll_system.h | 2 ++ common/lowlevel/ll_tft.h | 18 ++++------- common/system/system.c | 8 +++++ common/system/system.h | 4 +++ common/tft/tft.c | 24 +++++++++++++- common/tft/tft.h | 15 +++++++++ emulator/Makefile | 17 +++++----- emulator/qt/emulatorqt.pro | 3 +- emulator/qt/ll_system.cpp | 7 ++++ emulator/qt/ll_tft.cpp | 27 ++++++++++++++++ emulator/qt/main.cpp | 20 ++++++------ emulator/qt/mainwindow.cpp | 64 +++++++++++++++++++++++++++++++++---- emulator/qt/mainwindow.h | 9 +++++- 14 files changed, 194 insertions(+), 39 deletions(-) create mode 100644 common/lowlevel/ll_system.h create mode 100644 common/system/system.c create mode 100644 common/system/system.h create mode 100644 emulator/qt/ll_system.cpp diff --git a/common/app/app.c b/common/app/app.c index beaa39c..45f97b8 100644 --- a/common/app/app.c +++ b/common/app/app.c @@ -1,14 +1,27 @@ #include "app.h" #include "tft.h" +#include "system.h" #include void app_init() { + system_init(); tft_init(); + + + //only testwise + tft_clear(WHITE); + tft_draw_pixel(0,0,GREEN); + tft_draw_pixel(320-1,240-1,GREEN); + tft_draw_line(10,10,30,40,RGB(0xFF,0,0xFF)); + tft_draw_pixel(100,100,RED); + tft_draw_rectangle(120,120,20,30,BLUE); + tft_fill_rectangle(200,120,30,30,GREEN); - tft_draw_line(10,10,30,40,0b11111); } + +//app event loop void app_process() { /// printf("hello world\n"); diff --git a/common/lowlevel/ll_system.h b/common/lowlevel/ll_system.h new file mode 100644 index 0000000..89e18b9 --- /dev/null +++ b/common/lowlevel/ll_system.h @@ -0,0 +1,2 @@ + +bool ll_system_init(); diff --git a/common/lowlevel/ll_tft.h b/common/lowlevel/ll_tft.h index e01255c..84ea107 100644 --- a/common/lowlevel/ll_tft.h +++ b/common/lowlevel/ll_tft.h @@ -1,14 +1,10 @@ #include - -#ifdef __cplusplus -extern "C" { -#endif - - bool ll_tft_init(); - void ll_tft_draw_line(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color); - -#ifdef __cplusplus -} -#endif +bool ll_tft_init(); +void ll_tft_clear(uint16_t color); +void ll_tft_draw_line(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color); +void ll_tft_draw_pixel(uint16_t x,uint16_t y,uint16_t color); +void ll_tft_draw_rectangle(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2, uint16_t color); +void ll_tft_fill_rectangle(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2, uint16_t color); +void ll_tft_draw_bitmap_unscaled(uint16_t x, uint16_t y, uint16_t width, uint16_t height, const uint8_t* dat); diff --git a/common/system/system.c b/common/system/system.c new file mode 100644 index 0000000..01387d6 --- /dev/null +++ b/common/system/system.c @@ -0,0 +1,8 @@ +#include "system.h" +#include "ll_system.h" + + +bool system_init() { + return ll_system_init(); +} + diff --git a/common/system/system.h b/common/system/system.h new file mode 100644 index 0000000..98e9b50 --- /dev/null +++ b/common/system/system.h @@ -0,0 +1,4 @@ +#include + + +bool system_init(); diff --git a/common/tft/tft.c b/common/tft/tft.c index c3ab120..3ac7ce6 100644 --- a/common/tft/tft.c +++ b/common/tft/tft.c @@ -1,13 +1,35 @@ #include "tft.h" #include "ll_tft.h" - +//it might seems pointless to forward all the functions but we might also introduce functions which have some logic here bool tft_init() { return ll_tft_init(); } +void tft_clear(uint16_t color) { + ll_tft_clear(color); +} + void tft_draw_line(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color) { ll_tft_draw_line(x1,y1,x2,y2,color); } + + +void tft_draw_pixel(uint16_t x,uint16_t y,uint16_t color) { + ll_tft_draw_pixel(x,y,color); +} + +void tft_draw_rectangle(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2, uint16_t color) { + //could be implemented with 4 lines instead of introducing a ll func + ll_tft_draw_rectangle(x1,y1,x2,y2,color); +} + +void tft_fill_rectangle(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2, uint16_t color) { + ll_tft_fill_rectangle(x1,y1,x2,y2,color); +} + +void tft_draw_bitmap_unscaled(uint16_t x, uint16_t y, uint16_t width, uint16_t height, const uint8_t* dat) { + ll_tft_draw_bitmap_unscaled(x,y,width,height,dat); +} diff --git a/common/tft/tft.h b/common/tft/tft.h index be7a5fc..cb35060 100644 --- a/common/tft/tft.h +++ b/common/tft/tft.h @@ -1,5 +1,20 @@ #include #include + +#define RGB(r,g,b) ((((r) & 0xF8) << 8) | (((g) & 0xFC) << 3) | (((b) & 0xF8) >> 3)) +#define RED RGB(255,0,0) +#define GREEN RGB(0,255,0) +#define BLUE RGB(0,0,255) +#define WHITE RGB(255,255,255) +#define BLACK RGB(0,0,0) +#define HEX(h) (RGB(((h)>>16),((h)>>8),(h))) + + bool tft_init(); +void tft_clear(uint16_t color); void tft_draw_line(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color); +void tft_draw_pixel(uint16_t x,uint16_t y,uint16_t color); +void tft_draw_rectangle(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2, uint16_t color); +void tft_fill_rectangle(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2, uint16_t color); +void tft_draw_bitmap_unscaled(uint16_t x, uint16_t y, uint16_t width, uint16_t height, const uint8_t* dat); diff --git a/emulator/Makefile b/emulator/Makefile index 3ae1bb5..7c9ef71 100644 --- a/emulator/Makefile +++ b/emulator/Makefile @@ -10,17 +10,20 @@ COMMON_DIR=../common #Tools CC=gcc +MAKE=make MKDIR=mkdir -p RM=rm -f RMDIR=rm -rf COMMON_INC=-I$(COMMON_DIR)/lowlevel -I$(COMMON_DIR)/tft +QT_LIB=$(QT_DIR)/libemulatorqt.a + CPPFLAGS= -march=x86-64 -mtune=generic -fPIC $(COMMON_INC) CFLAGS= -O0 -g -LDFLAGS= -L$(QT_DIR) -lQt5Core -lQt5Gui -lQt5Widgets -lemulatorqt -lm -lstdc++ #--specs=nosys.specs -Wl,--gc-sections +LDFLAGS= -L$(QT_DIR) -lQt5Core -lQt5Gui -lQt5Widgets -lemulatorqt -lm -lstdc++ #Finding Input files CFILES=$(shell find . -maxdepth 1 -name '*.c') @@ -31,20 +34,17 @@ OBJS=$(patsubst ./%,$(OBJ_DIR)/%,$(CFILES:.c=.o)) COMMON_OBJS=$(patsubst $(COMMON_DIR)/%,$(OBJ_DIR)/%,$(COMMON_CFILES:.c=.o)) -#Keep the objects files -.SECONDARY: $(OBJS) - #Mark targets which are not "file-targets" -.PHONY: all clean qt +.PHONY: all clean # List of all binaries to build -all: $(BUILD_DIR)/$(TARGET) +all: $(BUILD_DIR)/$(TARGET) $(OBJS) $(QT_LIB) -qt: +$(QT_LIB): cd $(QT_DIR) && qmake &&make #objects to elf -$(BUILD_DIR)/$(TARGET): $(OBJS) $(COMMON_OBJS) |qt +$(BUILD_DIR)/$(TARGET): $(OBJS) $(COMMON_OBJS) $(QT_LIB) @echo Linking... $(MKDIR) $(BUILD_DIR) $(CC) -o $@ $(CFLAGS) $(CPPFLAGS) $^ $(LDFLAGS) @@ -66,6 +66,7 @@ $(OBJ_DIR)/%.o: $(COMMON_DIR)/%.c #Clean Obj files and builded stuff clean: + cd $(QT_DIR) && $(MAKE) clean && $(RM) Makefile && $(RM) *.a $(RMDIR) $(BUILD_DIR) $(OBJ_DIR) diff --git a/emulator/qt/emulatorqt.pro b/emulator/qt/emulatorqt.pro index fa7728f..e604c2e 100644 --- a/emulator/qt/emulatorqt.pro +++ b/emulator/qt/emulatorqt.pro @@ -13,7 +13,8 @@ CONFIG += staticlib SOURCES += \ mainwindow.cpp \ main.cpp \ - ll_tft.cpp + ll_tft.cpp \ + ll_system.cpp HEADERS += \ mainwindow.h \ diff --git a/emulator/qt/ll_system.cpp b/emulator/qt/ll_system.cpp new file mode 100644 index 0000000..0bedff3 --- /dev/null +++ b/emulator/qt/ll_system.cpp @@ -0,0 +1,7 @@ +extern "C" { +#include "ll_system.h" +} + +bool ll_system_init() { + return true; +} diff --git a/emulator/qt/ll_tft.cpp b/emulator/qt/ll_tft.cpp index cbaea26..a29db4c 100644 --- a/emulator/qt/ll_tft.cpp +++ b/emulator/qt/ll_tft.cpp @@ -1,6 +1,10 @@ #include "mainwindow.h" #include + +extern "C" { #include "ll_tft.h" +} + MainWindow* mainwindow; @@ -15,3 +19,26 @@ bool ll_tft_init() { void ll_tft_draw_line(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color) { mainwindow->draw_line(x1,y1,x2,y2,color); } + + +void ll_tft_clear(uint16_t color) { + mainwindow->clear(color); +} + +void ll_tft_draw_pixel(uint16_t x,uint16_t y,uint16_t color) { + mainwindow->draw_pixel(x,y,color); +} + +void ll_tft_draw_rectangle(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2, uint16_t color) { + mainwindow->draw_rectangle(x1,y1,x2,y2,color); +} + +void ll_tft_fill_rectangle(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2, uint16_t color) { + mainwindow->fill_rectangle(x1,y1,x2,y2,color); +} + +void ll_tft_draw_bitmap_unscaled(uint16_t x, uint16_t y, uint16_t width, uint16_t height, const uint8_t* dat) { + mainwindow->draw_bitmap_unscaled(x,y,width,height,dat); +} + + diff --git a/emulator/qt/main.cpp b/emulator/qt/main.cpp index 8bb406c..7886ab5 100644 --- a/emulator/qt/main.cpp +++ b/emulator/qt/main.cpp @@ -3,29 +3,29 @@ extern "C" { - void qt_init(int argc, char* argv[]); - int qt_execute(); - void app_init(); - void app_process(); + void qt_init(int argc, char* argv[]); //Will be called at the top of the main() function + int qt_execute(); //Will be called after calling qt_execute + void app_init(); //Initializes the app + void app_process(); //Processes one eventloop of the app } -QApplication* app_ptr; +QApplication* app_ptr=NULL; void qt_init(int argc, char* argv[]) { - static QApplication app(argc,argv); - app_ptr = &app; + app_ptr = new QApplication(argc,argv); app_init(); } void app_loop() { - while(1) { + while(QApplication::activeWindow()!=NULL) { app_process(); QApplication::processEvents(); } } int qt_execute() { - QtConcurrent::run(&app_loop); - return app_ptr->exec(); + int ret= app_ptr->exec(); + delete app_ptr; + return ret; } diff --git a/emulator/qt/mainwindow.cpp b/emulator/qt/mainwindow.cpp index c17923b..1aa5c51 100644 --- a/emulator/qt/mainwindow.cpp +++ b/emulator/qt/mainwindow.cpp @@ -4,22 +4,26 @@ #include #include + +#define DISPLAY_WIDTH 320 +#define DISPLAY_HEIGHT 240 +#define DISPLAY_X 100 +#define DISPLAY_Y 100 + QColor colorFromRGB565(uint16_t color) { int R8 = (int) floor( (color>>(5+6)) * 255.0 / 31.0 + 0.5); int G8 = (int) floor( ((color>>5)&0x2F) * 255.0 / 63.0 + 0.5); int B8 = (int) floor( (color&0x1F) * 255.0 / 31.0 + 0.5); return QColor::fromRgb(R8,G8,B8); - } MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), - pixmap(320,240), + pixmap(DISPLAY_WIDTH,DISPLAY_HEIGHT), ui(new Ui::MainWindow) { ui->setupUi(this); - } void MainWindow::draw_line(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color) @@ -32,14 +36,61 @@ void MainWindow::draw_line(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, u render_mutex.unlock(); } +void MainWindow::draw_pixel(uint16_t x, uint16_t y, uint16_t color) +{ + render_mutex.lock(); + QPainter painter(&(pixmap)); + painter.setPen(colorFromRGB565(color)); + painter.drawPoint(x,y); + render_mutex.unlock(); +} + +void MainWindow::clear(uint16_t color) +{ + render_mutex.lock(); + pixmap.fill(colorFromRGB565(color)); + render_mutex.unlock(); +} + +void MainWindow::draw_rectangle(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color) +{ + render_mutex.lock(); + QPainter painter(&(pixmap)); + painter.setPen(colorFromRGB565(color)); + painter.drawRect(x1,y1,x2,y2); + render_mutex.unlock(); +} + +void MainWindow::fill_rectangle(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color) +{ + render_mutex.lock(); + QPainter painter(&(pixmap)); + painter.fillRect(x1,y1,x2,y2,colorFromRGB565(color)); + render_mutex.unlock(); +} + +void MainWindow::draw_bitmap_unscaled(uint16_t x, uint16_t y, uint16_t width, uint16_t height, const uint8_t *dat) +{ + render_mutex.lock(); + QPainter painter(&(pixmap)); + + for(int yi=0; yi