joystick.c File Reference


Detailed Description

AVR32 UC3 Control Panel joystick sensor module.

Author:
Atmel Corporation: http://www.atmel.com
Support and FAQ: http://support.atmel.no/

Definition in file joystick.c.

#include <string.h>
#include "compiler.h"
#include "gpio.h"
#include "FreeRTOS.h"
#include "task.h"
#include "datalog.h"
#include "shell.h"
#include "board.h"
#include "sensor.h"
#include "config_file.h"
#include "fsaccess.h"
#include "MMI.h"
#include "BasicSMTP.h"

Go to the source code of this file.

Defines

#define JS_EVENT_DOWN   0x10
#define JS_EVENT_LEFT   0x02
#define JS_EVENT_PUSH   0x01
#define JS_EVENT_RIGHT   0x04
#define JS_EVENT_UP   0x08
#define JS_GETCONF_MAXLEN   16
#define JS_NB_EVENT   32
#define SENSOR_JS_CONFIG_FILE   "A:/CFG/JS.TXT"

Functions

Bool b_joystick_get_value (xLogDef *pxLog)
 Get the current joystick state.
Bool b_joystick_init (void)
 Init the joystick sensor.
eExecStatus e_joystick_get_config (signed portCHAR **ppcStringReply)
 Get the joystick sensor config.
eExecStatus e_joystick_set_config (signed portCHAR **ppcStringReply, int ac, signed portCHAR *av[])
 Set the sensor config.
static portBASE_TYPE prvjoystick_ISR_NonNakedBehaviour (void)
 joystick interrupt handler. Here, declarations should be done
void v_joystick_stop (void)
 Stop the joystick sensor.
void vjoystick_ISR (void)
 joystick naked interrupt handler.

Variables

const signed portCHAR * acpc_js_events [JS_NB_EVENT]
static Bool bAlarm = FALSE
static U32 u32LastCountValue = 0
static unsigned short x_joystick = 0
xSemaphoreHandle xCFGMutex
 The CFG system mutex.


Define Documentation

#define JS_EVENT_DOWN   0x10

Definition at line 80 of file joystick.c.

Referenced by prvjoystick_ISR_NonNakedBehaviour().

#define JS_EVENT_LEFT   0x02

Definition at line 77 of file joystick.c.

Referenced by prvjoystick_ISR_NonNakedBehaviour().

#define JS_EVENT_PUSH   0x01

Mask for events on joystick.

Definition at line 76 of file joystick.c.

Referenced by prvjoystick_ISR_NonNakedBehaviour().

#define JS_EVENT_RIGHT   0x04

Definition at line 78 of file joystick.c.

Referenced by prvjoystick_ISR_NonNakedBehaviour().

#define JS_EVENT_UP   0x08

Definition at line 79 of file joystick.c.

Referenced by prvjoystick_ISR_NonNakedBehaviour().

#define JS_GETCONF_MAXLEN   16

Max string length of a get config.

Definition at line 70 of file joystick.c.

#define JS_NB_EVENT   32

Max number of events on joystick.

Definition at line 73 of file joystick.c.

#define SENSOR_JS_CONFIG_FILE   "A:/CFG/JS.TXT"

Path to the config file.

Definition at line 83 of file joystick.c.

Referenced by b_joystick_init(), and e_joystick_set_config().


Function Documentation

Bool b_joystick_get_value ( xLogDef pxLog  ) 

Get the current joystick state.

Parameters:
pxLog a Log structure.
Returns:
TRUE upon success, FALSE if error.

Definition at line 254 of file joystick.c.

References acpc_js_events, LogDef::pcStringLog, LogDef::pfFreeStringLog, and x_joystick.

Referenced by b_sensor_get_value().

00255 {
00256 
00257    // Build the log string.
00258    pxLog->pfFreeStringLog = NULL; // We use a constant.
00259    pxLog->pcStringLog = (portCHAR *)acpc_js_events[x_joystick];
00260 
00261    return( TRUE );
00262 }

Bool b_joystick_init ( void   ) 

Init the joystick sensor.

Returns:
TRUE upon success, FALSE if error.

Definition at line 133 of file joystick.c.

References bAlarm, config_file_get_value(), SENSOR_JS_CONFIG_FILE, vjoystick_ISR(), x_supervisor_SemaphoreGive(), x_supervisor_SemaphoreTake(), and xCFGMutex.

00134 {
00135 portCHAR token[6];
00136 
00137    // Get the xCFGMutex.
00138    if( pdTRUE == x_supervisor_SemaphoreTake( xCFGMutex, 0 ) )
00139    {
00140        // get the field
00141        if (config_file_get_value(SENSOR_JS_CONFIG_FILE, "alarm" , token) >= 0)
00142        {
00143          // update value
00144          if (!strcmp(token, "on"))
00145          {
00146            bAlarm = pdTRUE;
00147          }
00148        }
00149      // Release the xCFGMutex.
00150      x_supervisor_SemaphoreGive( xCFGMutex );
00151    }
00152    /* configure joystick up to produce IT on all state change */
00153    gpio_enable_pin_interrupt(GPIO_JOYSTICK_UP , GPIO_PIN_CHANGE);
00154    /* configure joystick down to produce IT on all state change */
00155    gpio_enable_pin_interrupt(GPIO_JOYSTICK_DOWN , GPIO_PIN_CHANGE);
00156    /* configure joystick right to produce IT on all state change */
00157    gpio_enable_pin_interrupt(GPIO_JOYSTICK_RIGHT , GPIO_PIN_CHANGE);
00158    /* configure joystick left to produce IT on all state change */
00159    gpio_enable_pin_interrupt(GPIO_JOYSTICK_LEFT , GPIO_PIN_CHANGE);
00160    /* configure joystick press to produce IT on all state change */
00161    gpio_enable_pin_interrupt(GPIO_JOYSTICK_PUSH , GPIO_PIN_CHANGE);
00162    /* Disable all interrupts */
00163    Disable_global_interrupt();
00164    /* register joystick handler on level 3 */
00165    INTC_register_interrupt( (__int_handler)&vjoystick_ISR, AVR32_GPIO_IRQ_0 + (GPIO_JOYSTICK_UP/8), AVR32_INTC_INT3);
00166    INTC_register_interrupt( (__int_handler)&vjoystick_ISR, AVR32_GPIO_IRQ_0 + (GPIO_JOYSTICK_PUSH/8), AVR32_INTC_INT3);
00167    /* Enable all interrupts */
00168    Enable_global_interrupt();
00169    return (TRUE);
00170 }

eExecStatus e_joystick_get_config ( signed portCHAR **  ppcStringReply  ) 

Get the joystick sensor config.

Parameters:
ppcStringReply Input/Output. The response string. NEVER NULL AS INPUT. A malloc for the response string is performed here; the caller must free this string.
Returns:
the status of the command execution.

Definition at line 195 of file joystick.c.

References bAlarm, SENSOR_MSG_ALARM_OFF, SENSOR_MSG_ALARM_ON, and SHELL_EXECSTATUS_OK_NO_FREE.

00196 {
00197    // if alarm is set, set status to ON
00198    if ( bAlarm == TRUE )
00199      *ppcStringReply = (signed portCHAR *)SENSOR_MSG_ALARM_ON;
00200    else
00201      // set status to OFF
00202      *ppcStringReply = (signed portCHAR *)SENSOR_MSG_ALARM_OFF;
00203    return( SHELL_EXECSTATUS_OK_NO_FREE );
00204 }

eExecStatus e_joystick_set_config ( signed portCHAR **  ppcStringReply,
int  ac,
signed portCHAR *  av[] 
)

Set the sensor config.

Parameters:
ppcStringReply Input/Output. The response string. NEVER NULL AS INPUT.
ac Input. Number of args
av Input. pointer to args
Returns:
the status of the command execution.

Definition at line 215 of file joystick.c.

References bAlarm, config_file_set_value(), SENSOR_JS_CONFIG_FILE, SENSOR_MSG_ALARM_OFF, SENSOR_MSG_ALARM_ON, SHELL_ERRMSG_CONFIGERROR, SHELL_EXECSTATUS_KO, and SHELL_EXECSTATUS_OK_NO_FREE.

00216 {
00217 
00218   // set the field value
00219   if (config_file_set_value(SENSOR_JS_CONFIG_FILE, ac, av) != 0)
00220   {
00221     *ppcStringReply = (signed portCHAR *)SHELL_ERRMSG_CONFIGERROR;
00222     // return error
00223     return( SHELL_EXECSTATUS_KO );
00224   }
00225   // alarm=on
00226   if (!strcmp((char *)av[1] , "on"))
00227   {
00228     bAlarm = pdTRUE;
00229     *ppcStringReply = (signed portCHAR *)SENSOR_MSG_ALARM_ON;
00230     return( SHELL_EXECSTATUS_OK_NO_FREE );
00231   }
00232   // alarm=off
00233   else if (!strcmp( (char *)av[1], "off"))
00234   {
00235     bAlarm = pdFALSE;
00236     *ppcStringReply = (signed portCHAR *)SENSOR_MSG_ALARM_OFF;
00237     return( SHELL_EXECSTATUS_OK_NO_FREE );
00238   }
00239   // error
00240   else
00241   {
00242     *ppcStringReply = (signed portCHAR *)SHELL_ERRMSG_CONFIGERROR;
00243     return( SHELL_EXECSTATUS_KO );
00244   }
00245 }

static portBASE_TYPE prvjoystick_ISR_NonNakedBehaviour ( void   )  [static]

joystick interrupt handler. Here, declarations should be done

Definition at line 293 of file joystick.c.

References acpc_js_events, bAlarm, DATALOG_ID_JS, LogDef::id, JS_EVENT_DOWN, JS_EVENT_LEFT, JS_EVENT_PUSH, JS_EVENT_RIGHT, JS_EVENT_UP, LogDef::pcStringLog, LogDef::pfFreeStringLog, pxdatalog_log_alloc_init_FromISR, u32LastCountValue, v_SMTP_PostFromISR(), vMMI_UserMenuDisplayNextItem(), vMMI_UserMenuDisplayPreviousItem(), vMMI_UserMenuValidateItem(), x_datalog_AddLogFromISR(), and x_joystick.

Referenced by vjoystick_ISR().

00294 {
00295   xLogDef *pxLog;
00296   U32     u32CurrentCountValue = xTaskGetTickCount();
00297 
00298 
00299   // Debouncing: ignore joystick events occuring in less than 250ms before the last
00300   // valid joystick event.
00301   if((( u32CurrentCountValue >= u32LastCountValue )
00302         && ( u32CurrentCountValue - u32LastCountValue <= 250))
00303       ||
00304        (( u32CurrentCountValue < u32LastCountValue )
00305         && ( u32CurrentCountValue + (0xFFFFFFFF - u32LastCountValue) <= 250)))
00306   {
00307     // Clear all interrupts flag (it's no use checking which event has bouncing).
00308     gpio_clear_pin_interrupt_flag(GPIO_JOYSTICK_PUSH);
00309     Clr_bits(x_joystick, JS_EVENT_PUSH);
00310     gpio_clear_pin_interrupt_flag(GPIO_JOYSTICK_LEFT);
00311     Clr_bits(x_joystick, JS_EVENT_LEFT);
00312     gpio_clear_pin_interrupt_flag(GPIO_JOYSTICK_RIGHT);
00313     Clr_bits(x_joystick, JS_EVENT_RIGHT);
00314     gpio_clear_pin_interrupt_flag(GPIO_JOYSTICK_UP);
00315     Clr_bits(x_joystick, JS_EVENT_UP);
00316     gpio_clear_pin_interrupt_flag(GPIO_JOYSTICK_DOWN);
00317     Clr_bits(x_joystick, JS_EVENT_DOWN);
00318     return(pdFALSE);
00319   }
00320   else
00321   {
00322     // Update last valid joystick event timestamp.
00323     u32LastCountValue = u32CurrentCountValue;
00324 
00325     // Check all events.
00326     if (gpio_get_pin_interrupt_flag(GPIO_JOYSTICK_UP))
00327     {
00328        /* input is pulled up, if 1 : input is not active */
00329        if (gpio_get_pin_value(GPIO_JOYSTICK_UP))
00330        {
00331          /* clear bit UP */
00332          Clr_bits(x_joystick, JS_EVENT_UP);
00333        }
00334        else
00335        {
00336          Set_bits(x_joystick, JS_EVENT_UP);
00337        }
00338        /* allow new interrupt : clear the IFR flag */
00339        gpio_clear_pin_interrupt_flag(GPIO_JOYSTICK_UP);
00340      }
00341      if (gpio_get_pin_interrupt_flag(GPIO_JOYSTICK_DOWN))
00342      {
00343        /* input is pulled up, if 1 : input is not active */
00344        if (gpio_get_pin_value(GPIO_JOYSTICK_DOWN))
00345        {
00346          Clr_bits(x_joystick, JS_EVENT_DOWN);
00347        }
00348        else
00349        {
00350          Set_bits(x_joystick, JS_EVENT_DOWN);
00351        }
00352        /* allow new interrupt : clear the IFR flag */
00353        gpio_clear_pin_interrupt_flag(GPIO_JOYSTICK_DOWN);
00354      }
00355      if (gpio_get_pin_interrupt_flag(GPIO_JOYSTICK_LEFT))
00356      {
00357        /* input is pulled up, if 1 : input is not active */
00358        if (gpio_get_pin_value(GPIO_JOYSTICK_LEFT))
00359        {
00360          Clr_bits(x_joystick, JS_EVENT_LEFT);
00361        }
00362        else
00363        {
00364          Set_bits(x_joystick, JS_EVENT_LEFT);
00365    #ifdef MMILCD_ENABLE
00366          vMMI_UserMenuDisplayPreviousItem(pdTRUE);
00367    #endif
00368        }
00369        /* allow new interrupt : clear the IFR flag */
00370        gpio_clear_pin_interrupt_flag(GPIO_JOYSTICK_LEFT);
00371      }
00372      if (gpio_get_pin_interrupt_flag(GPIO_JOYSTICK_RIGHT))
00373      {
00374        /* input is pulled up, if 1 : input is not active */
00375        if (gpio_get_pin_value(GPIO_JOYSTICK_RIGHT))
00376        {
00377          Clr_bits(x_joystick, JS_EVENT_RIGHT);
00378        }
00379        else
00380        {
00381          Set_bits(x_joystick, JS_EVENT_RIGHT);
00382    #ifdef MMILCD_ENABLE
00383          vMMI_UserMenuDisplayNextItem(pdTRUE);
00384    #endif
00385        }
00386        /* allow new interrupt : clear the IFR flag */
00387        gpio_clear_pin_interrupt_flag(GPIO_JOYSTICK_RIGHT);
00388      }
00389      if (gpio_get_pin_interrupt_flag(GPIO_JOYSTICK_PUSH))
00390      {
00391        /* input is pulled up, if 1 : input is not active */
00392        if (gpio_get_pin_value(GPIO_JOYSTICK_PUSH))
00393        {
00394          Clr_bits(x_joystick, JS_EVENT_PUSH);
00395        }
00396        else
00397        {
00398          Set_bits(x_joystick, JS_EVENT_PUSH);
00399          if ( bAlarm == pdTRUE )
00400          {
00401            // post alarm to SMTP task
00402            v_SMTP_PostFromISR("Joystick Alarm", NULL);
00403          }
00404 
00405    #ifdef MMILCD_ENABLE
00406          vMMI_UserMenuValidateItem(pdTRUE);
00407    #endif
00408        }
00409        /* allow new interrupt : clear the IFR flag */
00410        gpio_clear_pin_interrupt_flag(GPIO_JOYSTICK_PUSH);
00411      }
00412   }
00413   // Alloc and init a log.
00414   pxLog = pxdatalog_log_alloc_init_FromISR();
00415   if( NULL == pxLog )
00416     return( pdFALSE );
00417   // Init the id field of the log.
00418   pxLog->id = DATALOG_ID_JS;
00419   /* set log event string */
00420   pxLog->pcStringLog = (portCHAR *)acpc_js_events[x_joystick];
00421   /* set free function pointer */
00422   pxLog->pfFreeStringLog = NULL;
00423   /* add the log entry */
00424   return( x_datalog_AddLogFromISR( pxLog ) );
00425 }

void v_joystick_stop ( void   ) 

Stop the joystick sensor.

Definition at line 175 of file joystick.c.

00176 {
00177   /* Disable joystick input change ITs. */
00178   gpio_disable_pin_interrupt( GPIO_JOYSTICK_UP );
00179   gpio_disable_pin_interrupt( GPIO_JOYSTICK_DOWN );
00180   gpio_disable_pin_interrupt( GPIO_JOYSTICK_RIGHT );
00181   gpio_disable_pin_interrupt( GPIO_JOYSTICK_LEFT );
00182   gpio_disable_pin_interrupt( GPIO_JOYSTICK_PUSH );
00183 }

void vjoystick_ISR ( void   ) 

joystick naked interrupt handler.

Definition at line 273 of file joystick.c.

References prvjoystick_ISR_NonNakedBehaviour().

Referenced by b_joystick_init().

00274 {
00275  /* This ISR can cause a context switch, so the first statement must be a
00276      call to the portENTER_SWITCHING_ISR() macro.  This must be BEFORE any
00277      variable declarations. */
00278   portENTER_SWITCHING_ISR();
00279 
00280   prvjoystick_ISR_NonNakedBehaviour();
00281 
00282   portEXIT_SWITCHING_ISR();
00283 }


Variable Documentation

const signed portCHAR* acpc_js_events[JS_NB_EVENT]

Initial value:

 {
   (signed portCHAR *)"RELEASE",(signed portCHAR *)"PRESS",(signed portCHAR *)"LEFT",(signed portCHAR *)"LEFT/PRESS",
   (signed portCHAR *)"RIGHT",(signed portCHAR *)"RIGHT/PRESS",(signed portCHAR *)"?",(signed portCHAR *)"?",
   (signed portCHAR *)"UP",(signed portCHAR *)"UP/PRESS",(signed portCHAR *)"UP/LEFT",(signed portCHAR *)"UP/LEFT/PRESS",
   (signed portCHAR *)"UP/RIGHT",(signed portCHAR *)"UP/RIGHT/PRESS",(signed portCHAR *)"?",(signed portCHAR *)"?",
   (signed portCHAR *)"DOWN",(signed portCHAR *)"DOWN/PRESS",(signed portCHAR *)"DOWN/LEFT",(signed portCHAR *)"DOWN/LEFT/PRESS",
   (signed portCHAR *)"DOWN/RIGHT",(signed portCHAR *)"DOWN/RIGHT/PRESS",(signed portCHAR *)"?",(signed portCHAR *)"?",
   (signed portCHAR *)"?",(signed portCHAR *)"?",(signed portCHAR *)"?",(signed portCHAR *)"?",
   (signed portCHAR *)"?",(signed portCHAR *)"?",(signed portCHAR *)"?",(signed portCHAR *)"?",
}
Joystick events string, regarding bit field value.

Definition at line 88 of file joystick.c.

Referenced by b_joystick_get_value(), and prvjoystick_ISR_NonNakedBehaviour().

Bool bAlarm = FALSE [static]

Alarm upon event.

Definition at line 103 of file joystick.c.

Referenced by b_joystick_init(), e_joystick_get_config(), e_joystick_set_config(), and prvjoystick_ISR_NonNakedBehaviour().

U32 u32LastCountValue = 0 [static]

Debouncing variable.

Definition at line 106 of file joystick.c.

Referenced by prvjoystick_ISR_NonNakedBehaviour().

unsigned short x_joystick = 0 [static]

Joystick events.

Definition at line 100 of file joystick.c.

Referenced by b_joystick_get_value(), and prvjoystick_ISR_NonNakedBehaviour().

xSemaphoreHandle xCFGMutex

The CFG system mutex.

The CFG system mutex.

Definition at line 203 of file supervisor.c.


Generated on Fri Feb 19 02:22:46 2010 for AVR32 - Control Panel demonstration. by  doxygen 1.5.5