Test pagina

Uit RoboWiki
Ga naar: navigatie, zoeken
//-----------------------------------------------------------------------------
// DemoPFKey.cpp
//
// Copyright (c) 2013-2015 Joep Suijs - All rights reserved.        
//
// This demo shows how to use the Programmable Function Keys.
//
// RobotLib tags: RED_SAMPLE
//-----------------------------------------------------------------------------
// Deze file is onderdeel van RobotLib.
// Zie http://wiki.robotmc.org/index.php?title=RobotLib voor meer informatie.
//
// Copyright: This library and the ideas, software, models, pictures contained
// herein are and are distributed under the Free Software Foundation General 
// Public License version 2.0. See the FSF GPL page for more information.
// It is explicitly forbidden to use any of the copyrighted materials for
// commercial purposes, including books.
//-----------------------------------------------------------------------------
// Deze file is een RODE SAMPLE file.
// Een RODE file is een standaard file, die voor iedere toepassing gelijk is.
// SAMPLE files zijn voorbeeld files. Plaats deze in de projectdirectory en 
// pas ze aan voor het project.
//-----------------------------------------------------------------------------
// tags_end
//-----------------------------------------------------------------------------            
 
//-------------
// OVERVIEW
//-------------
/*
   Up to 14 keys of the remote control (RC) can be configured* to be
   'programmable function keys' (PFKeys). If one of the keys is pressed,
   the 'PFKey handler' is called with the key # as parameter value.
 
   You can swich between PFKey handlers from your program or on the console.
   This enables re-use of your scarce RC keys in different contexts.
 
   This demo shows how to create your own PFKey handler, how to register it
   and how to re-activate it.
   This allows you to link your own actions to the PFKeys.     
 
   Notes:                            
   * The active PFKey handler handles all function keys.
   * On first call to PFKeyHandlerSet(), the PFKey handler is registered and 
     activated. 
   * Only one PFKey handler can be active at the time.
   * When PFKeyHandlerSet() is called with another PFKey handler, the previous 
     one remains registered but is no longer active.
   * Call PFKeyHandlerSet() again to re-activate a PFKey handler in your program.
   * Use console command pflist to see all registered PFKey handlers.
   * Use console command pfset <#> to activate registered PFKey handler.
   * Console command pf <#> is equivalent to pressing PFKey # on your remote. 
   * The mapping of RC (remote control) codes to PFKeys is configured in 
     'RobotLib_conf.h'.  
*/     
 
//-------------
// DECLARATIONS 
//-------------
 
// For access from other source files, add a prototype of
// your PFKey handler to project.h like
void PFKeyDemo(int Nr);
 
//-------------
// INSTANCES 
//-------------
 
// No instances required.
 
//-----------------------------------------------------------------------------            
// DemoSetup - 
//-----------------------------------------------------------------------------            
//-----------------------------------------------------------------------------            
void DemoSetup()
{       
   printf("DemoSetup for PFKey.\n");
 
   // The only setup required is registration of our PFKey handler.
   PFKeyHandlerSet(FP_FNAME(PFKeyDemo)); 
 
   // Did you notice the FP_FNAME macro?
   // This stands for 'FunctionPointer_FunctionNAME' and expands  
   //
   //    PFKeyHandlerSet(FP_FNAME(PFKeyDemo));
   // to 
   //    PFKeyHandlerSet(PFKeyDemo, "PFKeyDemo");  
   //
   // So it provides (as required) two parameters to PFKeyHandlerSet,
   // the pointer to the function (it's address in target memory) and
   // its name in the source code. Neat trick, isn't it?  
   //
   // The name of the PFKey handler is used for logging and by
   // the 'pf list' console command.          
   //
   // You could of course provide two parameters yourself and this would
   // allow for name different from the fucction. Just make sure you 
   // update both parameters when approriate. 
 
}
 
//----------------------------------------------------------------------------- 
// CliCmd_Demo -           
//-----------------------------------------------------------------------------            
//-----------------------------------------------------------------------------            
void CliCmd_Demo(int ParamCounter, int *Params)
{  
   printf("Demo command for PFKey\n");      
   printf("Use commands 'pflist' and 'pfset' to manage PFKEY handlers runtime\n");
   printf("Command 'pf <n>' is equivalent to pressing PF key n\n\n");
 
   // re-activate our PFKey handler
   PFKeyHandlerSet(FP_FNAME(PFKeyDemo)); 
 
   printf("Handler 'PFKeyDemo' re-activated\n");      
}                                
 
//-------------
// OTHER CODE 
//-------------
 
//-----------------------------------------------------------------------------
// PFKeyDemo - PFKeys handler demo
//-----------------------------------------------------------------------------
// This function handles the 'Programmable Function Keys'
//  
// If this handler is active and a PFKey is pressed, the handler gets
// called once, with the function key number as parameter, where 1 is
// the first function key.
//-----------------------------------------------------------------------------
void PFKeyDemo(int Nr)
{
   switch(Nr) {   // case of PFKeyDemo
      case 1 :    // PFKey 1
         printf("PFKey #1 pressed\n");
         break;             
 
      case 2 :    // PFKey 2
         printf("PFKey #2 pressed\n");
         break;         
 
      case 3 :    // PFKey 3
         printf("PFKey #3 pressed\n");
         break;
 
      case 10 :    // PFKey 10
         printf("PFKey #10 pressed\n");
         break;         
   }  
}