Difference between revisions of "The Queue of Events"

From Hydrover
Jump to: navigation, search
(Processing Events)
(Processing Events)
Line 26: Line 26:
 
= Processing Events =
 
= Processing Events =
  
In [https://en.wikipedia.org/wiki/Event-driven_programming Event-driven programming] the readmain loop of a program
+
In [https://en.wikipedia.org/wiki/Event-driven_programming Event-driven programming] the main loop of a program
 
takes care of fetch, process and/or dispatch incoming events, the following code is an example of a minimal one.
 
takes care of fetch, process and/or dispatch incoming events, the following code is an example of a minimal one.
  
Line 82: Line 82:
 
switch( ev.opcode )
 
switch( ev.opcode )
 
{
 
{
// script stop command
+
// <script stop> command from console
 
//
 
//
 
case SevEmergExit:
 
case SevEmergExit:
Line 89: Line 89:
 
}
 
}
  
// push button  
+
// push button activate
 
//
 
//
 
case SevDinActive:
 
case SevDinActive:
 
{
 
{
+
if( ev.p0 == 0 )
 +
{
 +
dCursor( 0, 10 );
 +
dPrintf("Button Pressed");
 +
{
 +
}
 +
 
 +
// push button deactivate
 +
//
 +
case SevDinInactive:
 +
{
 +
if( ev.p0 == 0 )
 +
{
 +
dCursor( 0, 10 );
 +
dPrintf("Button Released");
 +
{
 
}
 
}
 
}
 
}

Revision as of 16:57, 21 June 2017

Introduction

The queue of events is the queue where the system send events such as: input change, user action, encoder or transducer match, and from where the user's program get events to process and react to.

Structure of an Event

An event is defined in the library as an array of values:

[.opcode, .p0, .p1, .p2, .p3]

where:

  • .opcode is used to uniquely identify the event
  • .p0, .p1, .p2, .p3 contains informations specific to that event

Events' Library Functions

The library functions for events management are:

  • #define BASE_EVENT(%1) Event:%1[.opcode, .p0, .p1, .p2, .p3] is a macro to help in creating an event container
  • SysEvEnable() enable the system to post events into the queue
  • SysEvDisable() disable the system to post events into the queue
  • EventGet( BASE_EVENT(ev) ) get an event from the queue, if an event is not available will wait for
  • EventSend( BASE_EVENT(ev) ) send an event to the queue, can be used to inject custom and/or simulated events

Processing Events

In Event-driven programming the main loop of a program takes care of fetch, process and/or dispatch incoming events, the following code is an example of a minimal one.

/***************************************************************
*
*		Minimal Event Processing Loop
*
*       program.p
*
***************************************************************/

// include the library for H6105-D
//
#include <hydlib_cp>

// main function
//
main()
{
	// clear display
	//
	dClear();
	
	// select system font
	//
	dFont( system5x7 );

	// place cursor
	//
	dCursor( 0, 0 );
	
	// print on display
	//
	dPrintf("Processing Events");

	// declare an event container
	//
	new BASE_EVENT(ev);

	// enable system events
	//
	SysEvEnable();
	
	// main loop
	//
	for( ;; )
	{
		// get new event from queue
		//
		EventGet( ev );
		
		// process some events
		//
		switch( ev.opcode )
		{
			// <script stop> command from console
			//
			case SevEmergExit:
			{
				break;
			}

			// push button activate
			//
			case SevDinActive:
			{
				if( ev.p0 == 0 )
				{
					dCursor( 0, 10 );
					dPrintf("Button Pressed");
				{
			}

			// push button deactivate
			//
			case SevDinInactive:
			{
				if( ev.p0 == 0 )
				{
					dCursor( 0, 10 );
					dPrintf("Button Released");
				{
			}
		}
	}

	
	// program end
}