00001
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046 #include <string.h>
00047
00048 #include "compiler.h"
00049
00050 #include "gpio.h"
00051
00052
00053 #include "FreeRTOS.h"
00054
00055 #include "datalog.h"
00056 #include "shell.h"
00057 #include "board.h"
00058 #include "adc.h"
00059 #include "sensor.h"
00060 #include "config_file.h"
00061 #include "fsaccess.h"
00062
00063 #include "BasicSMTP.h"
00064
00065
00066
00068 #define LIGHT_GETCONF_MAXLEN 64
00069
00071 #define SENSOR_LIGHT_CONFIG_FILE "A:/CFG/LIGHT.TXT"
00072
00073
00074
00076 unsigned int ul_light_lograte = 5;
00077
00079 static unsigned int ul_light_min = 0;
00080
00082 static unsigned int ul_light_max = 100;
00083
00085 static Bool b_light_alarm = FALSE;
00087 static Bool b_light_alarm_max = pdFALSE;
00089 static Bool b_light_alarm_min = pdFALSE;
00090
00092 static volatile avr32_adc_t * adc= (volatile avr32_adc_t *) &AVR32_ADC;
00093
00095 extern xSemaphoreHandle xCFGMutex;
00096
00097
00098
00099 extern int sprintf(char *out, const char *format, ...);
00100
00101
00102
00108 Bool b_light_init ( void )
00109 {
00110 portCHAR token[6];
00111 portCHAR * percent;
00112
00113
00114 if( pdTRUE == x_supervisor_SemaphoreTake( xCFGMutex, 0 ) )
00115 {
00116
00117 if (config_file_get_value(SENSOR_LIGHT_CONFIG_FILE, "alarm" , token) >= 0)
00118 {
00119
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
00148 x_supervisor_SemaphoreGive( xCFGMutex );
00149 }
00150
00151 gpio_enable_module_pin( ADC_LIGHT_PIN , ADC_LIGHT_FUNCTION );
00152
00153 return (TRUE);
00154 }
00155
00165 eExecStatus e_light_get_config( signed portCHAR **ppcStringReply )
00166 {
00167
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
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 }
00181
00191 eExecStatus e_light_set_config( signed portCHAR **ppcStringReply, int ac, signed portCHAR *av[] )
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
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 }
00256
00264 Bool b_light_get_value( xLogDef *pxLog )
00265 {
00266 int i_current_val;
00267
00268
00269
00270 adc_enable( adc, ADC_LIGHT_CHANNEL );
00271
00272 adc_start( adc );
00273
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
00280 adc_disable( adc, ADC_LIGHT_CHANNEL );
00281
00282
00283 pxLog->pcStringLog = pvPortMalloc( 16*sizeof( char ) );
00284 if( NULL == pxLog->pcStringLog )
00285 {
00286 return( FALSE );
00287 }
00288 pxLog->pfFreeStringLog = vPortFree;
00289
00290
00291
00292 if( i_current_val <= ul_light_min )
00293 {
00294 sprintf( pxLog->pcStringLog, "%3d%% | min", i_current_val );
00295
00296 if (( b_light_alarm == pdTRUE ) && ( b_light_alarm_min == pdFALSE ))
00297 {
00298
00299
00300 b_light_alarm_min = pdTRUE;
00301
00302 b_light_alarm_max = pdFALSE;
00303
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
00311 if (( b_light_alarm == pdTRUE ) && ( b_light_alarm_max == pdFALSE ))
00312 {
00313
00314
00315 b_light_alarm_max = pdTRUE;
00316
00317 b_light_alarm_min = pdFALSE;
00318
00319 v_SMTP_Post("Max Light Alarm", NULL);
00320 }
00321 }
00322 else
00323 {
00324 sprintf( pxLog->pcStringLog, "%3d%%", i_current_val );
00325
00326 if ( b_light_alarm == pdTRUE )
00327 {
00328
00329 b_light_alarm_max = pdFALSE;
00330 b_light_alarm_min = pdFALSE;
00331 }
00332 }
00333
00334 return( TRUE );
00335 }