potentiometer.c File Reference


Detailed Description

AVR32 UC3 Control Panel potentiometer sensor module.

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

Definition in file potentiometer.c.

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

Go to the source code of this file.

Defines

#define POT_GETCONF_MAXLEN   64
#define SENSOR_POT_CONFIG_FILE   "A:/CFG/POT.TXT"

Functions

Bool b_potentiometer_get_value (xLogDef *pxLog)
 Get the current potentiometer value.
Bool b_potentiometer_init (void)
 Init the potentiometer channel.
eExecStatus e_potentiometer_get_config (signed portCHAR **ppcStringReply)
 Get the potentiometer sensor config.
eExecStatus e_potentiometer_set_config (signed portCHAR **ppcStringReply, int ac, signed portCHAR *av[])
 Set the sensor config.
int sprintf (char *out, const char *format,...)

Variables

static volatile avr32_adc_t * adc = (volatile avr32_adc_t *) &AVR32_ADC
static Bool b_pot_alarm = FALSE
static Bool b_pot_alarm_max = pdFALSE
static Bool b_pot_alarm_min = pdFALSE
unsigned int ul_pot_lograte = 5
static unsigned int ul_pot_max = 100
static unsigned int ul_pot_min = 0
xSemaphoreHandle xCFGMutex
 The CFG system mutex.


Define Documentation

#define POT_GETCONF_MAXLEN   64

Max string length of a get config.

Definition at line 66 of file potentiometer.c.

Referenced by e_potentiometer_get_config().

#define SENSOR_POT_CONFIG_FILE   "A:/CFG/POT.TXT"

Path for the config file

Definition at line 69 of file potentiometer.c.

Referenced by b_potentiometer_init(), and e_potentiometer_set_config().


Function Documentation

Bool b_potentiometer_get_value ( xLogDef pxLog  ) 

Get the current potentiometer value.

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

Definition at line 260 of file potentiometer.c.

References adc, b_pot_alarm, b_pot_alarm_max, b_pot_alarm_min, LogDef::pcStringLog, LogDef::pfFreeStringLog, sprintf(), ul_pot_max, ul_pot_min, and v_SMTP_Post().

Referenced by b_sensor_get_value().

00261 {
00262    int i_current_val;
00263 
00264 
00265    /* enable channel for sensor */
00266    adc_enable( adc, ADC_POTENTIOMETER_CHANNEL );
00267    /* start conversion */
00268    adc_start( adc );
00269    /* get value for sensor */
00270    i_current_val = adc_get_value( adc, ADC_POTENTIOMETER_CHANNEL ) * 100 / ADC_MAX_VALUE;
00271    /* Disable channel for sensor */
00272    adc_disable( adc, ADC_POTENTIOMETER_CHANNEL );
00273 
00274    // Alloc memory for the log string.
00275    pxLog->pcStringLog = pvPortMalloc( 16*sizeof( char ) );
00276    if( NULL == pxLog->pcStringLog )
00277    {
00278       return( FALSE );
00279    }
00280    pxLog->pfFreeStringLog = vPortFree; // Because pvPortMalloc() was used to
00281                                        // alloc the log string.
00282 
00283    // Build the log string.
00284    if( i_current_val <= ul_pot_min )
00285    {
00286       sprintf( pxLog->pcStringLog, "%3d%% | min", i_current_val );
00287       // if alarms have to be checked and no alarm for min was pending
00288       if (( b_pot_alarm == pdTRUE ) && ( b_pot_alarm_min == pdFALSE ))
00289       {
00290         // alarm has been taken into account, 
00291         // don't reenter this test before leaving min area
00292         b_pot_alarm_min = pdTRUE;
00293         // allow alarm if max is reached
00294         b_pot_alarm_max = pdFALSE;
00295         // post alarm to SMTP task
00296         v_SMTP_Post("Min Potentiometer Alarm", NULL);
00297       }
00298    }
00299    else if( i_current_val >= ul_pot_max )
00300    {
00301       sprintf( pxLog->pcStringLog, "%3d%% | max", i_current_val );
00302       // if alarms have to be checked and no alarm for max was pending
00303       if (( b_pot_alarm == pdTRUE ) && ( b_pot_alarm_max == pdFALSE ))
00304       {
00305         // alarm has been taken into account, 
00306         // don't reenter this test before leaving max area
00307         b_pot_alarm_max = pdTRUE;
00308         // allow alarm if min is reached
00309         b_pot_alarm_min = pdFALSE;
00310         // post alarm to SMTP task
00311         v_SMTP_Post("Max Potentiometer Alarm", NULL);
00312       }
00313    }
00314    else
00315    {
00316       sprintf( pxLog->pcStringLog, "%3d%%", i_current_val );
00317       // if alarms have to be checked
00318       if ( b_pot_alarm == pdTRUE )
00319       {
00320         // no alarm is pending 
00321         b_pot_alarm_max = pdFALSE;
00322         b_pot_alarm_min = pdFALSE;
00323       }
00324    }
00325 
00326    return( TRUE );
00327 }

Bool b_potentiometer_init ( void   ) 

Init the potentiometer channel.

Init the potentiometer sensor channel.

Returns:
TRUE upon success, FALSE if error.

Definition at line 104 of file potentiometer.c.

References b_pot_alarm, config_file_get_value(), SENSOR_POT_CONFIG_FILE, ul_pot_lograte, ul_pot_max, ul_pot_min, x_supervisor_SemaphoreGive(), x_supervisor_SemaphoreTake(), and xCFGMutex.

00105 {
00106 portCHAR token[6];
00107 portCHAR * percent;
00108 
00109    // Get the xCFGMutex.
00110    if( pdTRUE == x_supervisor_SemaphoreTake( xCFGMutex, 0 ) )
00111    {
00112        // get the field
00113        if (config_file_get_value(SENSOR_POT_CONFIG_FILE, "alarm" , token) >= 0)
00114        {
00115          // update value
00116          if (!strcmp(token, "on"))
00117          {
00118            b_pot_alarm = pdTRUE;
00119          }
00120        }
00121        if (config_file_get_value(SENSOR_POT_CONFIG_FILE, "min" , token) >= 0)
00122        {
00123          percent = strpbrk(token , "%");
00124          if (percent != NULL)
00125          {
00126            *percent = '\0';
00127          }
00128          sscanf(token, "%u", &ul_pot_min);
00129        }
00130        if (config_file_get_value(SENSOR_POT_CONFIG_FILE, "max" , token) >= 0)
00131        {
00132          percent = strpbrk(token , "%");
00133          if (percent != NULL)
00134          {
00135            *percent = '\0';
00136          }
00137          sscanf(token, "%u", &ul_pot_max);
00138        }
00139        if (config_file_get_value(SENSOR_POT_CONFIG_FILE, "lograte" , token) >= 0)
00140        {
00141          sscanf(token, "%u", &ul_pot_lograte);
00142        }
00143      // Release the xCFGMutex.
00144      x_supervisor_SemaphoreGive( xCFGMutex );
00145    }
00146    /* enable pin for sensor */
00147    gpio_enable_module_pin( ADC_POTENTIOMETER_PIN , ADC_POTENTIOMETER_FUNCTION );
00148 
00149    return (TRUE);
00150 }

eExecStatus e_potentiometer_get_config ( signed portCHAR **  ppcStringReply  ) 

Get the potentiometer 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 161 of file potentiometer.c.

References b_pot_alarm, POT_GETCONF_MAXLEN, SHELL_ERRMSG_MEMALLOC, SHELL_EXECSTATUS_KO, SHELL_EXECSTATUS_OK, sprintf(), ul_pot_lograte, ul_pot_max, and ul_pot_min.

00162 {
00163    // Alloc space for the reply.
00164    *ppcStringReply = (signed portCHAR *)pvPortMalloc( POT_GETCONF_MAXLEN );
00165    if( NULL == *ppcStringReply )
00166    {
00167       *ppcStringReply = (signed portCHAR *)SHELL_ERRMSG_MEMALLOC;
00168       return( SHELL_EXECSTATUS_KO );
00169    }
00170 
00171    // Build the string.
00172    sprintf( (char *)*ppcStringReply, "lograte=%d\r\n""min=%d%%\r\n""max=%d%%\r\n""alarm=%s\r\n",
00173             ul_pot_lograte, ul_pot_min, ul_pot_max, ((b_pot_alarm == pdTRUE) ? "on" : "off"));
00174 
00175    return( SHELL_EXECSTATUS_OK );
00176 }

eExecStatus e_potentiometer_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 187 of file potentiometer.c.

References b_pot_alarm, config_file_set_value(), SENSOR_MSG_ALARM_OFF, SENSOR_MSG_ALARM_ON, SENSOR_MSG_CONFIG_SET, SENSOR_POT_CONFIG_FILE, SHELL_ERRMSG_CONFIGERROR, SHELL_EXECSTATUS_KO, SHELL_EXECSTATUS_OK_NO_FREE, ul_pot_lograte, ul_pot_max, and ul_pot_min.

00188 {
00189 portCHAR * token;
00190 
00191   if (config_file_set_value(SENSOR_POT_CONFIG_FILE, ac, av) != 0)
00192   {
00193     *ppcStringReply = (signed portCHAR *)SHELL_ERRMSG_CONFIGERROR;
00194     // return error
00195     return( SHELL_EXECSTATUS_KO );
00196   }
00197   if (!strcmp((char *)av[0] , "alarm"))
00198   {
00199     if (!strcmp((char *)av[1] , "on"))
00200     {
00201       b_pot_alarm = pdTRUE;
00202       *ppcStringReply = (signed portCHAR *)SENSOR_MSG_ALARM_ON;
00203       return( SHELL_EXECSTATUS_OK_NO_FREE );
00204     }
00205     else if (!strcmp( (char *)av[1], "off"))
00206     {
00207       b_pot_alarm = pdFALSE;
00208       *ppcStringReply = (signed portCHAR *)SENSOR_MSG_ALARM_OFF;
00209       return( SHELL_EXECSTATUS_OK_NO_FREE );
00210     }
00211     else
00212     {
00213       *ppcStringReply = (signed portCHAR *)SHELL_ERRMSG_CONFIGERROR;
00214       return( SHELL_EXECSTATUS_KO );
00215     }
00216   }
00217   else if (!strcmp((char *)av[0] , "lograte"))
00218   {
00219     ul_pot_lograte = atoi((char *)av[1]);
00220     *ppcStringReply = (signed portCHAR *)SENSOR_MSG_CONFIG_SET;
00221     return( SHELL_EXECSTATUS_OK_NO_FREE );
00222   }
00223   else if (!strcmp((char *)av[0] , "min"))
00224   {
00225     token = strpbrk((char *)av[1] , "%");
00226     if (token != NULL)
00227     {
00228       *token = '\0';
00229     }
00230     ul_pot_min = atoi((char *)av[1]);
00231     *ppcStringReply = (signed portCHAR *)SENSOR_MSG_CONFIG_SET;
00232     return( SHELL_EXECSTATUS_OK_NO_FREE );
00233   }
00234   else if (!strcmp((char *)av[0] , "max"))
00235   {
00236     token = strpbrk((char *)av[1] , "%");
00237     if (token != NULL)
00238     {
00239       *token = '\0';
00240     }
00241     ul_pot_max = atoi((char *)av[1]);
00242     *ppcStringReply = (signed portCHAR *)SENSOR_MSG_CONFIG_SET;
00243     return( SHELL_EXECSTATUS_OK_NO_FREE );
00244   }
00245   else
00246 
00247   {
00248     *ppcStringReply = (signed portCHAR *)SHELL_ERRMSG_CONFIGERROR;
00249     return( SHELL_EXECSTATUS_KO );
00250   }
00251 }

int sprintf ( char *  out,
const char *  format,
  ... 
)

Definition at line 293 of file printf-stdarg.c.

00294 {
00295         va_list args;
00296         
00297         va_start( args, format );
00298         return print( &out, format, args );
00299 }


Variable Documentation

volatile avr32_adc_t* adc = (volatile avr32_adc_t *) &AVR32_ADC [static]

ADC cell.

Definition at line 89 of file potentiometer.c.

Bool b_pot_alarm = FALSE [static]

Bool b_pot_alarm_max = pdFALSE [static]

Alarm for max is pending ?

Definition at line 84 of file potentiometer.c.

Referenced by b_potentiometer_get_value().

Bool b_pot_alarm_min = pdFALSE [static]

Alarm for min is pending ?

Definition at line 86 of file potentiometer.c.

Referenced by b_potentiometer_get_value().

unsigned int ul_pot_lograte = 5

Value for the log rate.

Definition at line 73 of file potentiometer.c.

Referenced by b_potentiometer_init(), e_potentiometer_get_config(), e_potentiometer_set_config(), and portTASK_FUNCTION().

unsigned int ul_pot_max = 100 [static]

unsigned int ul_pot_min = 0 [static]

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