wxPostEvent() clarification/use in driver interface Topic is solved

This forum can be used to talk about general design strategies, new ideas and questions in general related to wxWidgets. If you feel your questions doesn't fit anywhere, put it here.
Post Reply
coyoteboy
In need of some credit
In need of some credit
Posts: 7
Joined: Fri Jun 16, 2006 4:11 pm

wxPostEvent() clarification/use in driver interface

Post by coyoteboy »

Hi everyone. I'm really new to wx stuff, done some realtime kernel driver programming mainly so the GUI side of things baffles me :shock: :D

Anyway, i have a driver thats posting information to my GUI via fifos. Theres a constant stream of data from one fifo (indata) and occasional data gets sent back down the other(outdata) to the driver. The latest data needs to be shown in the GUI (its a series of angles) and is then either worked upon and returned in modified form and displayed or just displayed. My plan was to have a main GUI thread spawn a thread that waited for signals from the fifo (it has a signal option to warn when new data is available), read the fifo and then update the GUI with the new data as a constant loop. So i wanted to use PostEvent to instruct the GUI that theres new data to use and update display. Unfortunately I cant seem to figure out what the first parameter of postevent should be - i know its the event handler for the main window but how do i get it?

Any help appreciated, even if you want to suggest a complete re-think, im open to anything and rather desperate at the moment .... ooh err! Thanks for reading!

James

Heres my current code, its partially modified examples. It compiles as it is and runs but no event ever gets captured:

.c file:

Code: Select all

#include <wx>
#include <wx>
#include <wx>
#include <wx>
#include "gui.h"
#include </usr>
#include "signal.h"
#include "unistd.h"
#include "sys/types.h"
//#include "pthread.h"

int outputfifo 	= -1;
int inputfifo	= -1;
int newdata[16];

DEFINE_EVENT_TYPE(wxEVT_MY_CUSTOM_COMMAND)

// it may also be convenient to define an event table macro for this event type
#define EVT_MY_CUSTOM_COMMAND(id, fn) \
    DECLARE_EVENT_TABLE_ENTRY( \
        wxEVT_MY_CUSTOM_COMMAND, id, wxID_ANY, \
        (wxObjectEventFunction)(wxEventFunction) wxStaticCastEvent( wxCommandEventFunction, &fn ), \
(wxObject *) NULL \
  ),

BEGIN_EVENT_TABLE( guiFrame, wxFrame )
	EVT_MENU( Menu_File_Quit, guiFrame::OnQuit )
	EVT_MENU( Menu_File_About, guiFrame::OnAbout )
	EVT_MENU( Menu_1, guiFrame::Send1 )
	EVT_MY_CUSTOM_COMMAND(wxID_ANY, guiFrame::OnProcessCustom)

END_EVENT_TABLE()

IMPLEMENT_APP(guiapp)
	
WorkerThread::ExitCode WorkerThread::Entry()
{
   //Sleep(10);
   wxCommandEvent event(wxEVT_MY_CUSTOM_COMMAND);
   wxPostEvent(m_handler, event);
   return (ExitCode) 0;
} 

bool 
guiapp::OnInit()
{
	guiFrame *frame = new guiFrame( wxT( "Puma 560 Visual Slave" ), wxPoint(50,50), wxSize(450,340) );

	frame->Show(TRUE);
	SetTopWindow(frame);

	inputfifo =  open("/dev/rtf1", O_RDONLY);	
	read(inputfifo,	newdata, 16);
	
	 WorkerThread *thread = new WorkerThread(this);
   	thread->Create(); 
	 thread->Run(); 	
	return TRUE;
} 

guiFrame::guiFrame( const wxString& title, const wxPoint& pos, const wxSize& size )
	: wxFrame((wxFrame *)NULL, -1, title, pos, size)
{
	wxMenu *menuFile = new wxMenu;
	
	menuFile->Append( Menu_File_About, wxT( "&About..." ) );
	menuFile->AppendSeparator();
	menuFile->Append( Menu_File_Quit, wxT( "E&xit" ) );
	menuFile->AppendSeparator();
	menuFile->Append( Menu_1, wxT( "&1s"));
	
	wxMenuBar *menuBar = new wxMenuBar;
	menuBar->Append( menuFile, wxT( "&File" ) );
	
	SetMenuBar( menuBar );
	
	CreateStatusBar();
}

void
guiFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
{
	Close(TRUE);
}


void 
guiFrame::Send1( wxCommandEvent& WXUNUSED( event ) )
{
	SetStatusText( wxT( "Send Position" ));
	/*outputfifo=  open("/dev/rtf0", O_WRONLY);
	if (outputfifo>-1){
	write(outputfifo,"0000000000000000",16);	
	close(outputfifo);}else{wxMessageBox( wxT( "Comms failed" ),
	wxT( "Slave Control" ), wxOK | wxICON_INFORMATION, this );}
*/

}
void 
guiFrame::OnAbout( wxCommandEvent& WXUNUSED( event ) )
{
	wxMessageBox( wxT( "Slave Visual Control" ),
	wxT( "Slave Control" ), wxOK | wxICON_INFORMATION, this );
}

void 
guiFrame::OnProcessCustom(wxCommandEvent& WXUNUSED(event))
{
    wxLogMessage(_T("Got a custom event!"));
}

//EOF
.h file

Code: Select all

#ifndef _GUI_H_
#define _GUI_H_

/**
 * @short Application Main Window
 * @author root <coyoteboyuk>
 * @version 0.1
 */

BEGIN_DECLARE_EVENT_TYPES()
    DECLARE_EVENT_TYPE(wxEVT_MY_CUSTOM_COMMAND, -1)
END_DECLARE_EVENT_TYPES()

class 
guiapp : public wxApp
{
	public:
		virtual bool OnInit();
};

class 
guiFrame : public wxFrame
{
	public:
		guiFrame( const wxString& title, const wxPoint& pos, const wxSize& pos );
		void OnQuit( wxCommandEvent& event );
		void OnAbout( wxCommandEvent& event );
		void Send1( wxCommandEvent& event);
		void OnProcessCustom( wxCommandEvent& event);
	private:
		DECLARE_EVENT_TABLE()
};

enum
{
	Menu_File_Quit = 100,
	Menu_File_About,
	Menu_1
	
};

class WorkerThread : public wxThread
{
   public:
      WorkerThread(wxEvtHandler *handler) :
         m_handler(handler) { }
   protected:
      virtual ExitCode Entry();
   private:
      wxEvtHandler *m_handler;

}; 

#endif // _GUI_H_
//EOF
micros
Super wx Problem Solver
Super wx Problem Solver
Posts: 317
Joined: Sat Mar 18, 2006 10:41 am
Location: Ustek, Bohemia

Post by micros »

Here, in App::OnInit(), you pass the wrong handler to thread's ctor:

Code: Select all

     WorkerThread *thread = new WorkerThread(this); 
...so the thread sends events to the app object. Probably you wanted to write:

Code: Select all

     WorkerThread *thread = new WorkerThread(frame); 
The funny thing is if you did it the other way round, that is send it to the frame and have a handler in app, it would work thanks to propagation of command-events. But it's best sent where it really is handled :)
wx0Pal
In need of some credit
In need of some credit
Posts: 2
Joined: Wed Feb 01, 2012 8:07 pm
Contact:

Re: wxPostEvent() clarification/use in driver interface

Post by wx0Pal »

Way too late to the party, but still an interesting subject (due to the number of useful applications).

I think, if the TS would have an Event Table declared in his/her wxApp class, it would have worked with the event handler he passed in the Thread Constructor originally.

Regards.
Post Reply