light.c File Reference


Detailed Description

AVR32 UC3 Control Panel light sensor module.

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

Definition in file light.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 LIGHT_GETCONF_MAXLEN   64
#define SENSOR_LIGHT_CONFIG_FILE   "A:/CFG/LIGHT.TXT"

Functions

Bool b_light_get_value (xLogDef *pxLog)
 Get the current light value.
Bool b_light_init (void)
 Init the light sensor channel.
eExecStatus e_light_get_config (signed portCHAR **ppcStringReply)
 Get the light sensor config.
eExecStatus e_light_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_light_alarm = FALSE
static Bool b_light_alarm_max = pdFALSE
static Bool b_light_alarm_min = pdFALSE
unsigned int ul_light_lograte = 5
static unsigned int ul_light_max = 100
static unsigned int ul_light_min = 0
xSemaphoreHandle xCFGMutex
 The CFG system mutex.


Define Documentation

#define LIGHT_GETCONF_MAXLEN   64

Max string length of a get config.

Definition at line 68 of file light.c.

Referenced by e_light_get_config().

#define SENSOR_LIGHT_CONFIG_FILE   "A:/CFG/LIGHT.TXT"

Path to the config file.

Definition at line 71 of file light.c.

Referenced by b_light_init(), and e_light_set_config().


Function Documentation

Bool b_light_get_value ( xLogDef pxLog  ) 

Get the current light value.

Get the current light sensor value.

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

Definition at line 264 of file light.c.

References adc, b_light_alarm, b_light_alarm_max, b_light_alarm_min, LogDef::pcStringLog, LogDef::pfFreeStringLog, sprintf(), ul_light_max, ul_light_min, and v_SMTP_Post().

Referenced by b_sensor_get_value().

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

Bool b_light_init ( void   ) 

Init the light sensor channel.

Returns:
TRUE upon success, FALSE if error.

Definition at line 108 of file light.c.

References b_light_alarm, config_file_get_value(), SENSOR_LIGHT_CONFIG_FILE, ul_light_lograte, ul_light_max, ul_light_min, x_supervisor_SemaphoreGive(), x_supervisor_SemaphoreTake(), and xCFGMutex.

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

eExecStatus e_light_get_config ( signed portCHAR **  ppcStringReply  ) 

Get the light 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 165 of file light.c.

References b_light_alarm, LIGHT_GETCONF_MAXLEN, SHELL_ERRMSG_MEMALLOC, SHELL_EXECSTATUS_KO, SHELL_EXECSTATUS_OK, sprintf(), ul_light_lograte, ul_light_max, and ul_light_min.

00166 {
00167    // Alloc space for the reply.
00168    *ppcStringReply = (signed portCHAR *)pvPortMalloc( LIGHT_GETCONF_MAXLEN );
00169    if( NULL == *ppcStringReply )
00170    {
00171       *ppcStringReply = (signed portCHAR *)SHELL_ERRMSG_MEMALLOC;
00172       return( SHELL_EXECSTATUS_KO );
00173    }
00174 
00175    // Build the string.
00176    sprintf( (char *)*ppcStringReply, "lograte=%d\r\n""min=%d%%\r\n""max=%d%%\r\n""alarm=%s\r\n",
00177             ul_light_lograte, ul_light_min, ul_light_max, ((b_light_alarm == pdTRUE) ? "on" : "off"));
00178 
00179    return( SHELL_EXECSTATUS_OK );
00180 }

eExecStatus e_light_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 191 of file light.c.

References b_light_alarm, config_file_set_value(), SENSOR_LIGHT_CONFIG_FILE, SENSOR_MSG_ALARM_OFF, SENSOR_MSG_ALARM_ON, SENSOR_MSG_CONFIG_SET, SHELL_ERRMSG_CONFIGERROR, SHELL_EXECSTATUS_KO, SHELL_EXECSTATUS_OK_NO_FREE, ul_light_lograte, ul_light_max, and ul_light_min.

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

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 92 of file light.c.

Referenced by b_light_get_value(), b_potentiometer_get_value(), b_temperature_get_value(), and bsensor_start().

Bool b_light_alarm = FALSE [static]

Alarm upon event.

Definition at line 85 of file light.c.

Referenced by b_light_get_value(), b_light_init(), e_light_get_config(), and e_light_set_config().

Bool b_light_alarm_max = pdFALSE [static]

Alarm for max is pending ?

Definition at line 87 of file light.c.

Referenced by b_light_get_value().

Bool b_light_alarm_min = pdFALSE [static]

Alarm for min is pending ?

Definition at line 89 of file light.c.

Referenced by b_light_get_value().

unsigned int ul_light_lograte = 5

Value for Log rate.

Definition at line 76 of file light.c.

Referenced by b_light_init(), e_light_get_config(), e_light_set_config(), and portTASK_FUNCTION().

unsigned int ul_light_max = 100 [static]

Max Value for alarm.

Definition at line 82 of file light.c.

Referenced by b_light_get_value(), b_light_init(), e_light_get_config(), and e_light_set_config().

unsigned int ul_light_min = 0 [static]

Min value for alarm.

Definition at line 79 of file light.c.

Referenced by b_light_get_value(), b_light_init(), e_light_get_config(), and e_light_set_config().

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