[Interest] Looking for help or for a QT developer

Cyril HAENEL chaenel at free.fr
Sat Apr 28 10:53:03 CEST 2012


Hello, I have a very small QT application to do for my personal use but 
after some days of unsuccessful I am looking for help.
I am sure a QT guru can do this application in less than half an hour !!

I am an electronic and C programmer enthusiast (not C++!!) . For a 
project which use a TFT touchscreen I want to simulate the TFT 
touchscreen on my computer to be able to develop and test all my 
embedded GUI application on my computer instead on the microcontroller 
target. My code is portable and can be compiled on a computer, I have a 
good hardware abstraction layer to hide the hardware specificity of the 
board/computer. The big advantage is that I don't need the board to 
develop my project, just need to recompile for my microcontroller and 
download in target when everything is OK.

Thus what I need is just a QT app which simulate a TFT screen by 
creating an 480x272 image in which my C application (through very simple 
API) can change a pixel color (in RGB565 mode), get a pixel color, and 
get the X/Y mouse position and buttons states.

Thus the QT application job is :

- Create an empty 480x272 white image
- Refresh the image on the window every 50ms for example (20Hz refresh 
rate is sufficient for me), no need to refresh the image each time a 
pixel is changed by my C program.
- Create a thread to call a *C* function which will be the main of my C 
application, which never return and will use the emulated TFT screen 
through the API.

My C program needs access to the emulated TFT screen by calling 3 
functions :

void setPixelColor ( unsigned short x, unsigned short y, unsigned color)

unsigned short getPixelColor ( unsigned short x, unsigned short y)

unsigned char getTouchScreenState ( unsigned char *x,  unsigned char *y  
) --> return FALSE if mouse left button is not pressed, TRUE if left 
button pressed with x and y for the cursor coordinates)

The difficulty seems to be having a C++ QT part which periodicaly 
refresh a local image in the window, and a *C* thread which need to 
access the image pixels and mouse status.

Do you think someone can help me ? I already wrote (under linux) a very 
horrible code, it works a little but crashes after some time with this 
king of error :

QApplication: Invalid Display* argument
QPixmap: It is not safe to use pixmaps outside the GUI thread
calculateurVEframebuffer: xcb_io.c :176 : process_responses:  
L'assertion « !(req && current_request && !(((long) (req->sequence) - 
(long) (current_request)) <= 0)) » a échoué.
Abandon

Or sometime my 50ms periodic function is not called anymore, thus 
display refresh stops :(

Here is my horrible code (main.cpp) :

#include <qapplication.h>
#include <qpainter.h>
#include <qlabel.h>
#include <qpixmap.h>
#include <qpaintdevice.h>
#include <qtimer.h>
#include <stdio.h>
#include <signal.h>
#include <sys/time.h>
#include <pthread.h>


QApplication* app = new QApplication( 0, 0 );
QImage* image = new QImage(480,272, QImage::Format_ARGB32);
QLabel myLabel;




// Bridge with the C program part

extern "C" {
int mainDemo (void);
// Change pixel color on the emulated TFT screen
void _PutPixel(short x, short y, short color );
// Get pixel color on the emulated TFT screen
unsigned short _GetPixel(short x, short y );
// Indicate mouse position on the emulated TFT screen and mouse buttons 
statePermet de connaitre la position X/Y de la souris sur l'ecran TFT 
virtuel et l'état des boutons
extern short mouseX, mouseY, mouseState;
}



// API used by the C program part

#define GFX_COLOR unsigned short
#define RGBConvert(red, green, blue)    (GFX_COLOR) (((((GFX_COLOR)(red) 
& 0xF8) >> 3) << 11) | ((((GFX_COLOR)(green) & 0xFC) >> 2) << 5) | 
(((GFX_COLOR)(blue) & 0xF8) >> 3))


void _PutPixel(short x, short y, short color )
{
  QRgb pixel;
  pixel = qRgb( ((color>>11)&0b11111)<<3, ((color>>5)&0b111111)<<2, 
(color&0b11111)<<3 );
  image->setPixel( x, y, pixel );
}


unsigned short _GetPixel(short x, short y )
{
  QRgb color;

  color = image->pixel(x, y);

  return( RGBConvert( (color>>16)&0xFF,(color>>8)&0xFF,color&0xFF ) );
}








// Periodic function called every 50ms the simulate the TFT screen 
refresh by reloading the
// image in the window

static void timer50msInt ( int sig )
{
  QPoint mousePos;
  QRect geom;
  int mouseButton, x, y;

  signal( SIGALRM, timer50msInt );

  // Image reload
  myLabel.setPixmap(QPixmap::fromImage(*image));

  // Get mouse coordinate and convert in window mouse coordinate with 
the window position
  mousePos = QCursor::pos();
  x = mousePos.x();
  y = mousePos.y();
  geom = myLabel.geometry();
  x = x - geom.x();
  y = y - geom.y();

  // Get mouse buttons state
  mouseButton = app->mouseButtons();

  // Pass mouse coordinate and mouse button states to the C program part
  mouseX = x;
  mouseY = y;
  mouseState = mouseButton;

// printf( "%d %d %d\n", x, y, mouseButton );
}




// Launch 50ms periodic function

void _timerInit ( void )
{
  struct itimerval timer;

  timer.it_interval.tv_sec = 0;
  timer.it_interval.tv_usec = 50000;

  timer.it_value.tv_sec = timer.it_interval.tv_sec;
  timer.it_value.tv_usec = timer.it_interval.tv_usec;

  signal( SIGALRM, timer50msInt );

  setitimer( ITIMER_REAL, &timer, NULL );
}








// Thread used to call my C program part which will use the virtual TFT 
touchscreen

void* monAppli ( void *ptr )
{
  usleep( 500000 );

   // Launch the 50ms periodic function
  _timerInit();

  // My C program part which use the virtual TFT touchscreen
  mainDemo();

  return 0;
}






int main (int argc, char* argv[])
{
  pthread_t thread;

  // Thread creation for my C program part
  pthread_create( &thread, NULL, monAppli, (void*) NULL);

  // Loading of the image whish simulate the TFT screen content
  myLabel.setPixmap(QPixmap::fromImage(*image));
  myLabel.show();

  return app->exec() ;
}






Regards
Cyril HAENEL




-- 

Cyril Haenel
Registered Linux User #332632




More information about the Interest mailing list