Merge remote-tracking branch 'origin/master' into windows

Conflicts:
	emulator/qt/ll_tft.cpp
This commit is contained in:
t-moe
2015-06-08 15:31:24 +02:00
70 changed files with 6054 additions and 4691 deletions

View File

@@ -1,5 +1,19 @@
/**************************************************************************************************************************************
* Project: discoverpixy
* Website: https://github.com/t-moe/discoverpixy
* Authors: Aaron Schmocker, Timo Lang
* Institution: BFH Bern University of Applied Sciences
* File: emulator/qt/ll_filesystem.cpp
*
* Version History:
* Date Autor Email SHA Changes
* 2015-05-10 timolang@gmail.com e2bce8f Added filesystem module, tests and implementation for it in emulator.
* 2015-06-07 timolang@gmail.com 4b5768c Improved Comments in whole emulator. Finalized emulator section in docu.
*
**************************************************************************************************************************************/
extern "C" {
#include "ll_filesystem.h"
#include "ll_filesystem.h"
}
#include <QFile>
#include <QDir>
@@ -7,69 +21,78 @@ extern "C" {
#include <QFileInfoList>
#include <QDateTime>
QDir rootdir ("./emulated"); //Create a QDir which points to the "root" of the emulated filesystem
QDir rootdir("./emulated"); //Create a QDir which points to the "root" of the emulated filesystem
bool ll_filesystem_init() {
if(!rootdir.exists()) { //if our root dir is nonexistent
bool ll_filesystem_init()
{
if (!rootdir.exists()) { //if our root dir is nonexistent
qWarning() << "Filesystem can not be emulated because the 'emulated' folder does not exist";
return false; //mark an error
}
return true;
}
DIRECTORY_STRUCT* ll_filesystem_dir_open(const char* path) {
DIRECTORY_STRUCT* ll_filesystem_dir_open(const char* path)
{
QDir d(rootdir); //Make a copy of the rootdir QDir instance
d.cd(path); //Change the directory to the passed path
if(!d.exists()) {
if (!d.exists()) {
return NULL; //mark an error
}
DIRECTORY_STRUCT* directory = new DIRECTORY_STRUCT();
//get all files and directories which are important to us. Filter out . and .. symlinks (linux)
QFileInfoList entries = d.entryInfoList(QDir::NoDotAndDotDot|QDir::Files|QDir::Dirs|QDir::Readable|QDir::Writable|QDir::Hidden|QDir::System);
QFileInfoList entries = d.entryInfoList(QDir::NoDotAndDotDot | QDir::Files | QDir::Dirs | QDir::Readable | QDir::Writable | QDir::Hidden | QDir::System);
//Fill the directory structure for the user
directory->path = path;
directory->num_files = entries.count();
directory->files = new FILE_STRUCT[directory->num_files]; //allocate array of file structs
for(int i=0; i<entries.count(); i++){ //foreach file that we found
for (int i = 0; i < entries.count(); i++) { //foreach file that we found
QFileInfo fi = entries.at(i);
FILE_STRUCT* entry = &(directory->files[i]); //get the pointer to the current filestruct (which should be filled)
entry->fattrib = 0;
entry->fname = new char[fi.fileName().length()+1]; //reserve memory for filename
strcpy(entry->fname,fi.fileName().toStdString().c_str()); //copy filename into struct
if(fi.isDir()) { //it's a direcory
entry->fattrib|=F_DIR; //set directory attribute
entry->fname = new char[fi.fileName().length() + 1]; //reserve memory for filename
strcpy(entry->fname, fi.fileName().toStdString().c_str()); //copy filename into struct
if (fi.isDir()) { //it's a direcory
entry->fattrib |= F_DIR; //set directory attribute
entry->fsize = 0;
} else { //it's a file
entry->fsize = fi.size(); //set filesize
}
if(fi.isHidden()) { //the file is hidden
entry->fattrib|=F_HID;
if (fi.isHidden()) { //the file is hidden
entry->fattrib |= F_HID;
}
if(!fi.isWritable()) { //the file is not writable
entry->fattrib|=F_RDO; //set readonly attribue
if (!fi.isWritable()) { //the file is not writable
entry->fattrib |= F_RDO; //set readonly attribue
}
//Set date & time of file in structure
QDateTime dt = fi.lastModified();
entry->fdate.year = dt.date().year()-1980; //year is realtive to 1980
entry->fdate.year = dt.date().year() - 1980; //year is realtive to 1980
entry->fdate.month = dt.date().month();
entry->fdate.day = dt.date().day();
entry->ftime.hour = dt.time().hour();
entry->ftime.min = dt.time().minute();
entry->ftime.sec = dt.time().second()/2;
entry->ftime.sec = dt.time().second() / 2;
}
return directory; //return filled directory struct
}
void ll_filesystem_dir_close(DIRECTORY_STRUCT* dir) {
if(dir!=NULL) { //passed handle is valid
for(int i=0; i<dir->num_files; i++) { //foreach file
void ll_filesystem_dir_close(DIRECTORY_STRUCT* dir)
{
if (dir != NULL) { //passed handle is valid
for (int i = 0; i < dir->num_files; i++) { //foreach file
delete dir->files[i].fname; //delete filename buffer
}
delete[] dir->files; //delete file array
delete dir; //delete structure itself
}
@@ -81,17 +104,20 @@ struct QT_FILE_HANDLE : FILE_HANDLE { //..derived from the FILE_HANDLE (of the F
};
FILE_HANDLE* ll_filesystem_file_open(const char* filename) {
if(!rootdir.exists()) {
FILE_HANDLE* ll_filesystem_file_open(const char* filename)
{
if (!rootdir.exists()) {
return NULL;
}
QString filepath = rootdir.absoluteFilePath(filename); //get the absolute path to the requested file
QFile* f = new QFile(filepath); //create a QFile instance to the requested file
if(!f->exists()) { //File does not exist
if (!f->exists()) { //File does not exist
return NULL; //mark error
}
if(!f->open(QFile::ReadWrite)) { //try to open the file, it it fails then ...
if (!f->open(QFile::ReadWrite)) { //try to open the file, it it fails then ...
return NULL; //... mark error
}
@@ -100,35 +126,42 @@ FILE_HANDLE* ll_filesystem_file_open(const char* filename) {
QT_FILE_HANDLE* fh = new QT_FILE_HANDLE(); //Create Structure to return to user
fh->file = f;
fh->fname = filename;
fh->fpos =0;
fh->fpos = 0;
fh->fsize = f->size();
return fh;
}
void ll_filesystem_file_close(FILE_HANDLE* handle) {
if(handle!=NULL) { //passed handle is valid
void ll_filesystem_file_close(FILE_HANDLE* handle)
{
if (handle != NULL) { //passed handle is valid
QT_FILE_HANDLE* fh = static_cast<QT_FILE_HANDLE*>(handle); //cast pointer to QT_FILE_HANDLE
if(fh->file->isOpen()) { //if the file is still open
if (fh->file->isOpen()) { //if the file is still open
fh->file->close(); //close the file
}
delete fh->file; //delete QFile instance
delete fh; //delete the fle
}
}
FILE_STATUS ll_filesystem_file_seek(FILE_HANDLE* handle, uint32_t offset) {
if(handle==NULL) {
return F_INVALIDPARAM;
}
QT_FILE_HANDLE* fh = static_cast<QT_FILE_HANDLE*>(handle); //cast pointer to QT_FILE_HANDLE
if(!fh->file->isOpen()) { //file is not open
return F_DISKERROR;
}
if(offset>=fh->file->size()) { //offset exeeds filesize
FILE_STATUS ll_filesystem_file_seek(FILE_HANDLE* handle, uint32_t offset)
{
if (handle == NULL) {
return F_INVALIDPARAM;
}
if(fh->file->seek(offset)) { //try to seek to desired offset
QT_FILE_HANDLE* fh = static_cast<QT_FILE_HANDLE*>(handle); //cast pointer to QT_FILE_HANDLE
if (!fh->file->isOpen()) { //file is not open
return F_DISKERROR;
}
if (offset >= fh->file->size()) { //offset exeeds filesize
return F_INVALIDPARAM;
}
if (fh->file->seek(offset)) { //try to seek to desired offset
fh->fpos = offset; //update offset in FILE_HANDLE (for user)
return F_OK;
} else { //seek failed
@@ -136,49 +169,64 @@ FILE_STATUS ll_filesystem_file_seek(FILE_HANDLE* handle, uint32_t offset) {
}
}
FILE_STATUS ll_filesystem_file_read(FILE_HANDLE* handle, uint8_t* buf, uint32_t size) {
if(handle==NULL || buf==NULL) {
FILE_STATUS ll_filesystem_file_read(FILE_HANDLE* handle, uint8_t* buf, uint32_t size)
{
if (handle == NULL || buf == NULL) {
return F_INVALIDPARAM;
}
QT_FILE_HANDLE* fh = static_cast<QT_FILE_HANDLE*>(handle); //cast pointer to QT_FILE_HANDLE
if(!fh->file->isOpen()) { //file is not open
if (!fh->file->isOpen()) { //file is not open
return F_DISKERROR;
}
if(!fh->file->isReadable()) { //file is not readable
if (!fh->file->isReadable()) { //file is not readable
return F_EACCESS;
}
qint64 bytesRead = fh->file->read((char*)buf,size); //try to read desired amount of bytes
if(bytesRead<0) { //read failed
qint64 bytesRead = fh->file->read((char*)buf, size); //try to read desired amount of bytes
if (bytesRead < 0) { //read failed
return F_DISKERROR;
}
fh->fpos+=bytesRead; //increase file position (for user)
if(bytesRead!=size) { //we got less bytes than expected
fh->fpos += bytesRead; //increase file position (for user)
if (bytesRead != size) { //we got less bytes than expected
return F_EOF; //we reached the end of the file
} else {
return F_OK;
}
}
FILE_STATUS ll_filesystem_file_write(FILE_HANDLE* handle, uint8_t* buf, uint32_t size) {
if(handle==NULL) {
FILE_STATUS ll_filesystem_file_write(FILE_HANDLE* handle, uint8_t* buf, uint32_t size)
{
if (handle == NULL) {
return F_INVALIDPARAM;
}
QT_FILE_HANDLE* fh = static_cast<QT_FILE_HANDLE*>(handle); //cast pointer to QT_FILE_HANDLE
if(!fh->file->isOpen()) { //file is not open
if (!fh->file->isOpen()) { //file is not open
return F_DISKERROR;
}
if(!fh->file->isWritable()) { //file is not writable
if (!fh->file->isWritable()) { //file is not writable
return F_EACCESS;
}
qint64 bytesWritten = fh->file->write((char*)buf,size); //try to write desired amount of bytes
if(bytesWritten<0) { //write failed
qint64 bytesWritten = fh->file->write((char*)buf, size); //try to write desired amount of bytes
if (bytesWritten < 0) { //write failed
return F_DISKERROR;
}
fh->fpos+=bytesWritten; //increase file position (for user)
if(bytesWritten!=size) { //we wrote less bytes than expected
fh->fpos += bytesWritten; //increase file position (for user)
if (bytesWritten != size) { //we wrote less bytes than expected
return F_EOF; //we reached the end of the file
} else {
return F_OK;
}
}

View File

@@ -1,3 +1,21 @@
/**************************************************************************************************************************************
* Project: discoverpixy
* Website: https://github.com/t-moe/discoverpixy
* Authors: Aaron Schmocker, Timo Lang
* Institution: BFH Bern University of Applied Sciences
* File: emulator/qt/ll_system.cpp
*
* Version History:
* Date Autor Email SHA Changes
* 2015-04-03 timolang@gmail.com 1f2af9f Added more tft functions to common and emulator. Fixed eventloop.
* 2015-04-03 timolang@gmail.com cab8609 Integrated pixy into emulator. Pixy is no longer in the common/libs folder but in emulator/libs and discovery/libs
* 2015-04-25 timolang@gmail.com 3d1e4b2 Simplified code a bit. Emulator does not work stable when replugging pixy.
* 2015-04-25 timolang@gmail.com 0858b0d Fixed some bugs when receiving large data.
* 2015-05-09 timolang@gmail.com c652b6b Improved Emulator Gui
* 2015-06-07 timolang@gmail.com 4b5768c Improved Comments in whole emulator. Finalized emulator section in docu.
*
**************************************************************************************************************************************/
#include <QThread>
#include <QApplication>
@@ -5,19 +23,23 @@ extern "C" {
#include "ll_system.h"
}
bool ll_system_init() {
bool ll_system_init()
{
return true; //nothing to initialize here, apart from the stuff which is done in the main method.
}
void ll_system_delay(uint32_t msec) {
QThread::msleep(msec); //Let the app_process() Thread sleep
void ll_system_delay(uint32_t msec)
{
QThread::msleep(msec); //Let the app_process() Thread sleep
}
void ll_system_process() {
QApplication::processEvents(); //Process pending qt events
QThread::msleep(1); //Sleep for 1ms, to keep the cpu load down
void ll_system_process()
{
QApplication::processEvents(); //Process pending qt events
QThread::msleep(1); //Sleep for 1ms, to keep the cpu load down
}
void ll_system_toggle_led() {
void ll_system_toggle_led()
{
//No led emulated :(
}

View File

@@ -1,3 +1,22 @@
/**************************************************************************************************************************************
* Project: discoverpixy
* Website: https://github.com/t-moe/discoverpixy
* Authors: Aaron Schmocker, Timo Lang
* Institution: BFH Bern University of Applied Sciences
* File: emulator/qt/ll_tft.cpp
*
* Version History:
* Date Autor Email SHA Changes
* 2015-04-03 timolang@gmail.com 51089aa Refactored Project Structure for use with emulator
* 2015-04-03 timolang@gmail.com 1f2af9f Added more tft functions to common and emulator. Fixed eventloop.
* 2015-04-03 timolang@gmail.com 1aa9194 Fixed Drawing of rects in Emulator. Got frames from pixy to emulator. Slooooow.
* 2015-04-27 aaron@duckpond.ch aed90ef Drawcircle added (emulator)
* 2015-04-27 timolang@gmail.com e249fb2 Added font support
* 2015-05-15 timolang@gmail.com b08a897 Added tft method to draw a bmp from filesystem. Added another font to emulator.
* 2015-06-07 timolang@gmail.com 4b5768c Improved Comments in whole emulator. Finalized emulator section in docu.
*
**************************************************************************************************************************************/
#include "mainwindow.h"
extern "C" {
@@ -6,50 +25,60 @@ extern "C" {
MainWindow* mainwindow;
bool ll_tft_init() {
mainwindow = new MainWindow(); //create the designed window
mainwindow->show(); //open it (non blocking)
return true;
bool ll_tft_init()
{
mainwindow = new MainWindow(); //create the designed window
mainwindow->show(); //open it (non blocking)
return true;
}
//the following functions redirect the call to the mainwindow, to a function with the same signature
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_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) {
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_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_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_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 uint16_t* dat) {
mainwindow->draw_bitmap_unscaled(x,y,width,height,dat);
void ll_tft_draw_bitmap_unscaled(uint16_t x, uint16_t y, uint16_t width, uint16_t height, const uint16_t* dat)
{
mainwindow->draw_bitmap_unscaled(x, y, width, height, dat);
}
void ll_tft_draw_circle(uint16_t x, uint16_t y, uint16_t r, uint16_t color) {
mainwindow->draw_circle(x,y,r,color);
void ll_tft_draw_circle(uint16_t x, uint16_t y, uint16_t r, uint16_t color)
{
mainwindow->draw_circle(x, y, r, color);
}
uint8_t ll_tft_num_fonts() {
uint8_t ll_tft_num_fonts()
{
return 2; //we have two fonts (see below)
}
//Helper function to get the QFont to the corresponding font number
//Note: only return monospaced fonts!!!
QFont get_font(uint8_t fontnum) {
QFont get_font(uint8_t fontnum)
{
switch(fontnum) {
case 0:
return QFont("Courier New",8);
@@ -61,23 +90,37 @@ QFont get_font(uint8_t fontnum) {
}
uint8_t ll_tft_font_height(uint8_t fontnum) {
uint8_t ll_tft_font_height(uint8_t fontnum)
{
QFont f = get_font(fontnum);
if(f == QFont()) return -1;
if (f == QFont()) {
return -1;
}
QFontMetrics m(f); //use font metcris object to calculate height of font
return m.height();
}
uint8_t ll_tft_font_width(uint8_t fontnum) {
uint8_t ll_tft_font_width(uint8_t fontnum)
{
QFont f = get_font(fontnum);
if(f == QFont()) return -1;
if (f == QFont()) {
return -1;
}
QFontMetrics m(f); //use font metcris object to calculate width of font
return m.averageCharWidth();
}
void ll_tft_draw_char(uint16_t x, uint16_t y, uint16_t color, uint16_t bgcolor, uint8_t font, char c) {
void ll_tft_draw_char(uint16_t x, uint16_t y, uint16_t color, uint16_t bgcolor, uint8_t font, char c)
{
QFont f = get_font(font);
if(f == QFont()) return; //if the font is the default-font, we want to abort.
mainwindow->draw_char(x,y,color,bgcolor,f,c);
}
if (f == QFont()) {
return; //if the font is the default-font, we want to abort.
}
mainwindow->draw_char(x, y, color, bgcolor, f, c);
}

View File

@@ -1,10 +1,25 @@
/**************************************************************************************************************************************
* Project: discoverpixy
* Website: https://github.com/t-moe/discoverpixy
* Authors: Aaron Schmocker, Timo Lang
* Institution: BFH Bern University of Applied Sciences
* File: emulator/qt/ll_touch.cpp
*
* Version History:
* Date Autor Email SHA Changes
* 2015-04-27 timolang@gmail.com 259d446 Added touch support to emulator. Implemented basic touch function.
* 2015-06-01 timolang@gmail.com eb573bc Finalized calibration. Fixed a bug in screen module.
*
**************************************************************************************************************************************/
extern "C" {
#include "ll_touch.h"
#include "touch.h"
}
bool ll_touch_init() {
bool ll_touch_init()
{
touch_set_value_convert_mode(false); //tell the touch module that we don't need calibration or value conversion
return true;
}

View File

@@ -1,3 +1,21 @@
/**************************************************************************************************************************************
* Project: discoverpixy
* Website: https://github.com/t-moe/discoverpixy
* Authors: Aaron Schmocker, Timo Lang
* Institution: BFH Bern University of Applied Sciences
* File: emulator/qt/main.cpp
*
* Version History:
* Date Autor Email SHA Changes
* 2015-04-03 timolang@gmail.com 51089aa Refactored Project Structure for use with emulator
* 2015-04-03 timolang@gmail.com 1f2af9f Added more tft functions to common and emulator. Fixed eventloop.
* 2015-04-03 timolang@gmail.com cab8609 Integrated pixy into emulator. Pixy is no longer in the common/libs folder but in emulator/libs and discovery/libs
* 2015-04-07 timolang@gmail.com 383fc86 Simplified Emulator (main)
* 2015-04-25 timolang@gmail.com 3d1e4b2 Simplified code a bit. Emulator does not work stable when replugging pixy.
* 2015-06-07 timolang@gmail.com 4b5768c Improved Comments in whole emulator. Finalized emulator section in docu.
*
**************************************************************************************************************************************/
#include <QApplication>
#include <QtConcurrent/QtConcurrent>
@@ -7,19 +25,18 @@ extern "C" {
void app_process(); //Processes one eventloop of the app
}
void app_loop() {
while(!QApplication::closingDown()) { //as long as the application is not terminating
void app_loop()
{
while (!QApplication::closingDown()) { //as long as the application is not terminating
app_process(); //let the application process it's events
}
}
int main(int argc, char* argv[]) {
QApplication app(argc,argv); //Process qt-specific commandline arguments and create event loop
int main(int argc, char* argv[])
{
QApplication app(argc, argv); //Process qt-specific commandline arguments and create event loop
app_init(); //Let the application initialize it self
QtConcurrent::run(&app_loop); //Start a thread that executes app_loop
return app.exec(); //Run the event loop until the last window is closed.
}

View File

@@ -1,3 +1,29 @@
/**************************************************************************************************************************************
* Project: discoverpixy
* Website: https://github.com/t-moe/discoverpixy
* Authors: Aaron Schmocker, Timo Lang
* Institution: BFH Bern University of Applied Sciences
* File: emulator/qt/mainwindow.cpp
*
* Version History:
* Date Autor Email SHA Changes
* 2015-04-03 timolang@gmail.com 51089aa Refactored Project Structure for use with emulator
* 2015-04-03 timolang@gmail.com 1f2af9f Added more tft functions to common and emulator. Fixed eventloop.
* 2015-04-03 timolang@gmail.com c570bda Fixed bug in color conversion
* 2015-04-03 timolang@gmail.com 867f766 Some gui changes.
* 2015-04-03 timolang@gmail.com 1aa9194 Fixed Drawing of rects in Emulator. Got frames from pixy to emulator. Slooooow.
* 2015-04-04 timolang@gmail.com 0636381 Improved tff_draw_bitmap_unscaled in emulator.
* 2015-04-27 timolang@gmail.com 259d446 Added touch support to emulator. Implemented basic touch function.
* 2015-04-27 aaron@duckpond.ch aed90ef Drawcircle added (emulator)
* 2015-04-27 timolang@gmail.com e249fb2 Added font support
* 2015-05-09 timolang@gmail.com c652b6b Improved Emulator Gui
* 2015-05-25 timolang@gmail.com 911760e Added "Mouse Position"-Label to Emulator.
* 2015-06-01 timolang@gmail.com 06227da Added calibrate screen (WIP). fixed bug in emulator drawing.
* 2015-06-07 timolang@gmail.com 4b5768c Improved Comments in whole emulator. Finalized emulator section in docu.
* 2015-06-07 timolang@gmail.com 8752356 Started with tests in docu. fixed a small bug in emulator when drawing a rectangle.
*
**************************************************************************************************************************************/
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
@@ -6,33 +32,36 @@
#include <QMouseEvent>
extern "C" {
#include "touch.h"
#include "tft.h"
#include "touch.h"
#include "tft.h"
}
#define DISPLAY_WIDTH 320
#define DISPLAY_HEIGHT 240
//function to calculate QColor out of a RGB565 16bit color
QColor QColorFromRGB565(uint16_t color) {
QColor QColorFromRGB565(uint16_t color)
{
//interpolate colors
int R8 = (int) floor( (color>>(5+6)) * 255.0 / 31.0 + 0.5);
int G8 = (int) floor( ((color>>5)&0x3F) * 255.0 / 63.0 + 0.5);
int B8 = (int) floor( (color&0x1F) * 255.0 / 31.0 + 0.5);
return QColor::fromRgb(R8,G8,B8);
int R8 = (int) floor((color >> (5 + 6)) * 255.0 / 31.0 + 0.5);
int G8 = (int) floor(((color >> 5) & 0x3F) * 255.0 / 63.0 + 0.5);
int B8 = (int) floor((color & 0x1F) * 255.0 / 31.0 + 0.5);
return QColor::fromRgb(R8, G8, B8);
}
//function to calculate QRgb out of a RGB565 16bit color
QRgb QRgbFromRGB565(uint16_t color) {
QRgb QRgbFromRGB565(uint16_t color)
{
//interpolate colors
int R8 = (int) floor( (color>>(5+6)) * 255.0 / 31.0 + 0.5);
int G8 = (int) floor( ((color>>5)&0x3F) * 255.0 / 63.0 + 0.5);
int B8 = (int) floor( (color&0x1F) * 255.0 / 31.0 + 0.5);
return qRgb(R8,G8,B8);
int R8 = (int) floor((color >> (5 + 6)) * 255.0 / 31.0 + 0.5);
int G8 = (int) floor(((color >> 5) & 0x3F) * 255.0 / 63.0 + 0.5);
int B8 = (int) floor((color & 0x1F) * 255.0 / 31.0 + 0.5);
return qRgb(R8, G8, B8);
}
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), image(DISPLAY_WIDTH,DISPLAY_HEIGHT, QImage::Format_RGB16), ui(new Ui::MainWindow){
MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), image(DISPLAY_WIDTH, DISPLAY_HEIGHT, QImage::Format_RGB16), ui(new Ui::MainWindow)
{
ui->setupUi(this);
image.fill(Qt::black); //clear display buffer
currentScale = 1; //start with scale factor 1
@@ -44,13 +73,13 @@ void MainWindow::draw_line(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, u
{
QPainter painter(&(image));
painter.setPen(QColorFromRGB565(color));
painter.drawLine(x1,y1,x2,y2);
painter.drawLine(x1, y1, x2, y2);
update();
}
void MainWindow::draw_pixel(uint16_t x, uint16_t y, uint16_t color)
{
image.setPixel(x,y,QRgbFromRGB565(color));
image.setPixel(x, y, QRgbFromRGB565(color));
update();
}
@@ -64,31 +93,32 @@ void MainWindow::draw_rectangle(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t
{
QPainter painter(&(image));
painter.setPen(QColorFromRGB565(color));
painter.drawRect(qMin(x1,x2),qMin(y1,y2),abs((int)x2-(int)x1),abs((int)y2-(int)y1));
painter.drawRect(qMin(x1, x2), qMin(y1, y2), abs((int)x2 - (int)x1), abs((int)y2 - (int)y1));
update();
}
void MainWindow::fill_rectangle(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color)
{
QPainter painter(&(image));
painter.fillRect(qMin(x1,x2),qMin(y1,y2),abs((int)x2-(int)x1)+1,abs((int)y2-(int)y1)+1,QColorFromRGB565(color));
painter.fillRect(qMin(x1, x2), qMin(y1, y2), abs((int)x2 - (int)x1) + 1, abs((int)y2 - (int)y1) + 1, QColorFromRGB565(color));
update();
}
void MainWindow::draw_bitmap_unscaled(uint16_t x, uint16_t y, uint16_t width, uint16_t height, const uint16_t *dat)
void MainWindow::draw_bitmap_unscaled(uint16_t x, uint16_t y, uint16_t width, uint16_t height, const uint16_t* dat)
{
//Creating a new image and access it directly is faster than setPixel
QImage img(width,height,QImage::Format_RGB32); //create a new image
QImage img(width, height, QImage::Format_RGB32); //create a new image
for(int yi=0; yi<height; yi++) { //for each line
for (int yi = 0; yi < height; yi++) { //for each line
uint32_t* line = (uint32_t*)img.scanLine(yi); //get the pointer to the imagedata of the current line
for(int xi=0; xi<width; xi++) { //for each column
*line++=QRgbFromRGB565(dat[yi*width+xi]); //set pixel
for (int xi = 0; xi < width; xi++) { //for each column
*line++ = QRgbFromRGB565(dat[yi * width + xi]); //set pixel
}
}
QPainter p(&image);
p.drawImage(x,y,img); //draw created image
p.drawImage(x, y, img); //draw created image
update();
}
@@ -96,7 +126,7 @@ void MainWindow::draw_circle(uint16_t x, uint16_t y, uint16_t r, uint16_t color)
{
QPainter painter(&(image));
painter.setPen(QColorFromRGB565(color));
painter.drawEllipse(QPoint(x,y), r, r);
painter.drawEllipse(QPoint(x, y), r, r);
update();
}
@@ -105,67 +135,70 @@ void MainWindow::draw_char(uint16_t x, uint16_t y, uint16_t color, uint16_t bgco
QPainter painter(&(image));
painter.setFont(font);
if(bgcolor!=TRANSPARENT) { //background color is not transparent
painter.setBackgroundMode(Qt::OpaqueMode);
painter.setBackground(QColorFromRGB565(bgcolor)); //set text background
if (bgcolor != TRANSPARENT) { //background color is not transparent
painter.setBackgroundMode(Qt::OpaqueMode);
painter.setBackground(QColorFromRGB565(bgcolor)); //set text background
}
painter.setPen(QColorFromRGB565(color)); //set fontcolor
y+=QFontMetrics(font).ascent(); //use y pos as highest point of char, instead of baseline
painter.drawText(QPoint(x,y), QString(QChar(c))); //draw char
y += QFontMetrics(font).ascent(); //use y pos as highest point of char, instead of baseline
painter.drawText(QPoint(x, y), QString(QChar(c))); //draw char
update();
}
void MainWindow::paintEvent(QPaintEvent *)
void MainWindow::paintEvent(QPaintEvent*)
{
//this method is called whenever the window needs to be redrawn (or after update() is called)
QPainter painter(this);
//this method is called whenever the window needs to be redrawn (or after update() is called)
QPainter painter(this);
//Create a QRectF which represents the rectangle to draw the buffered image to
QRectF imgRect (ui->widgetDisplay->geometry().topLeft(),QSizeF(DISPLAY_WIDTH*currentScale,DISPLAY_HEIGHT*currentScale));
//Create a QRectF which represents the rectangle to draw the buffered image to
QRectF imgRect(ui->widgetDisplay->geometry().topLeft(), QSizeF(DISPLAY_WIDTH * currentScale, DISPLAY_HEIGHT * currentScale));
painter.drawImage(imgRect,image); //draw buffer
painter.setPen(QPen(Qt::green,2)); //set border color
painter.drawRect(imgRect.adjusted(-1,-1,1,1)); //draw border
painter.drawImage(imgRect, image); //draw buffer
painter.setPen(QPen(Qt::green, 2)); //set border color
painter.drawRect(imgRect.adjusted(-1, -1, 1, 1)); //draw border
}
void MainWindow::mousePressEvent(QMouseEvent *evt)
void MainWindow::mousePressEvent(QMouseEvent* evt)
{
//the mouse was pressed
checkAndSendEvent(evt->pos(),true);
checkAndSendEvent(evt->pos(), true);
}
void MainWindow::mouseReleaseEvent(QMouseEvent *evt)
void MainWindow::mouseReleaseEvent(QMouseEvent* evt)
{
//the mouse was released
checkAndSendEvent(evt->pos(),false);
checkAndSendEvent(evt->pos(), false);
}
void MainWindow::mouseMoveEvent(QMouseEvent *evt)
void MainWindow::mouseMoveEvent(QMouseEvent* evt)
{
//the mouse was released
checkAndSendEvent(evt->pos(),true);
checkAndSendEvent(evt->pos(), true);
}
bool MainWindow::eventFilter(QObject *obj, QEvent *evt)
bool MainWindow::eventFilter(QObject* obj, QEvent* evt)
{
if(obj==ui->widgetDisplay) { //we received a redirect event from the target rectangle
switch(evt->type()) {
case QEvent::MouseMove: //it's a mouse move event
{
QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(evt); //get mouse event
QPoint p = (mouseEvent->pos()-QPoint(1,1))/currentScale; //calculate correct corrdinates (undo scale)
if(p.x()<DISPLAY_WIDTH && p.y()<DISPLAY_HEIGHT) { //mouse position not out of bounds
//set mouse position text
ui->txtMousePos->setText(QString("Mouse Position: (%1,%2)").arg(p.x()).arg(p.y()));
}
if (obj == ui->widgetDisplay) { //we received a redirect event from the target rectangle
switch (evt->type()) {
case QEvent::MouseMove: { //it's a mouse move event
QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(evt); //get mouse event
QPoint p = (mouseEvent->pos() - QPoint(1, 1)) / currentScale; //calculate correct corrdinates (undo scale)
if (p.x() < DISPLAY_WIDTH && p.y() < DISPLAY_HEIGHT) { //mouse position not out of bounds
//set mouse position text
ui->txtMousePos->setText(QString("Mouse Position: (%1,%2)").arg(p.x()).arg(p.y()));
}
break;
default: break;
}
}
return false;
break;
default:
break;
}
}
return false;
}
@@ -177,15 +210,18 @@ MainWindow::~MainWindow()
void MainWindow::checkAndSendEvent(QPoint pos, bool down)
{
QPoint p = pos - ui->widgetDisplay->geometry().topLeft(); //make coordinate relative to target area (0,0)
p/=currentScale; //undo scaling
if(p.x()<0 || p.y()<0 || p.x() >= DISPLAY_WIDTH || p.y() >= DISPLAY_HEIGHT) return; //abort if out of bounds
p /= currentScale; //undo scaling
touch_add_raw_event(p.x(),p.y(),down?TOUCH_DOWN:TOUCH_UP); //submit touch event to touch module (common)
if (p.x() < 0 || p.y() < 0 || p.x() >= DISPLAY_WIDTH || p.y() >= DISPLAY_HEIGHT) {
return; //abort if out of bounds
}
touch_add_raw_event(p.x(), p.y(), down ? TOUCH_DOWN : TOUCH_UP); //submit touch event to touch module (common)
}
void MainWindow::on_cboZoom_currentIndexChanged(int index)
{
currentScale=index+1; //zoom factor 1 is the 0th entry, zoom factor 2 is the 1st entry, zoom factor 3 is the 2nd entry
currentScale = index + 1; //zoom factor 1 is the 0th entry, zoom factor 2 is the 1st entry, zoom factor 3 is the 2nd entry
update(); //force redraw
}

View File

@@ -1,3 +1,24 @@
/**************************************************************************************************************************************
* Project: discoverpixy
* Website: https://github.com/t-moe/discoverpixy
* Authors: Aaron Schmocker, Timo Lang
* Institution: BFH Bern University of Applied Sciences
* File: emulator/qt/mainwindow.h
*
* Version History:
* Date Autor Email SHA Changes
* 2015-04-03 timolang@gmail.com 51089aa Refactored Project Structure for use with emulator
* 2015-04-03 timolang@gmail.com 1f2af9f Added more tft functions to common and emulator. Fixed eventloop.
* 2015-04-03 timolang@gmail.com 1aa9194 Fixed Drawing of rects in Emulator. Got frames from pixy to emulator. Slooooow.
* 2015-04-27 timolang@gmail.com 259d446 Added touch support to emulator. Implemented basic touch function.
* 2015-04-27 aaron@duckpond.ch aed90ef Drawcircle added (emulator)
* 2015-04-27 timolang@gmail.com e249fb2 Added font support
* 2015-05-09 timolang@gmail.com c652b6b Improved Emulator Gui
* 2015-05-25 timolang@gmail.com 911760e Added "Mouse Position"-Label to Emulator.
* 2015-06-07 timolang@gmail.com 4b5768c Improved Comments in whole emulator. Finalized emulator section in docu.
*
**************************************************************************************************************************************/
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
@@ -5,7 +26,8 @@
#include <QMutex>
#include <stdint.h>
namespace Ui {
namespace Ui
{
class MainWindow;
}
@@ -14,33 +36,33 @@ class MainWindow : public QMainWindow
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
explicit MainWindow(QWidget* parent = 0);
void draw_line(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color);
void draw_pixel(uint16_t x,uint16_t y,uint16_t color);
void draw_pixel(uint16_t x, uint16_t y, uint16_t color);
void clear(uint16_t color);
void draw_rectangle(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color);
void fill_rectangle(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color);
void draw_bitmap_unscaled(uint16_t x, uint16_t y, uint16_t width, uint16_t height, const uint16_t *dat);
void draw_bitmap_unscaled(uint16_t x, uint16_t y, uint16_t width, uint16_t height, const uint16_t* dat);
void draw_circle(uint16_t x, uint16_t y, uint16_t r, uint16_t color);
void draw_char(uint16_t x, uint16_t y, uint16_t color, uint16_t bgcolor, QFont font, char c);
protected:
bool eventFilter(QObject * obj , QEvent * env);
void paintEvent(QPaintEvent * evt);
void mousePressEvent(QMouseEvent* evt);
void mouseReleaseEvent(QMouseEvent* evt);
void mouseMoveEvent(QMouseEvent* evt);
bool eventFilter(QObject* obj , QEvent* env);
void paintEvent(QPaintEvent* evt);
void mousePressEvent(QMouseEvent* evt);
void mouseReleaseEvent(QMouseEvent* evt);
void mouseMoveEvent(QMouseEvent* evt);
~MainWindow();
private slots:
void on_cboZoom_currentIndexChanged(int index); //slot that is called when the zoomlevel changed
void on_cboZoom_currentIndexChanged(int index); //slot that is called when the zoomlevel changed
private:
QImage image; //Display buffer
int currentScale; //current scale factor
void checkAndSendEvent(QPoint pos, bool down);
Ui::MainWindow *ui;
Ui::MainWindow* ui;
};
#endif // MAINWINDOW_H