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 #include "gpio.h"
00050
00051 #include "FreeRTOS.h"
00052
00053 #include "datalog.h"
00054 #include "shell.h"
00055 #include "board.h"
00056 #include "adc.h"
00057 #include "sensor.h"
00058 #include "config_file.h"
00059 #include "fsaccess.h"
00060
00061 #include "BasicSMTP.h"
00062
00063
00064
00066 #define POT_GETCONF_MAXLEN 64
00067
00069 #define SENSOR_POT_CONFIG_FILE "A:/CFG/POT.TXT"
00070
00071
00073 unsigned int ul_pot_lograte = 5;
00074
00076 static unsigned int ul_pot_min = 0;
00077
00079 static unsigned int ul_pot_max = 100;
00080
00082 static Bool b_pot_alarm = FALSE;
00084 static Bool b_pot_alarm_max = pdFALSE;
00086 static Bool b_pot_alarm_min = pdFALSE;
00087
00089 static volatile avr32_adc_t * adc= (volatile avr32_adc_t *) &AVR32_ADC;
00090
00092 extern xSemaphoreHandle xCFGMutex;
00093
00094
00095 extern int sprintf(char *out, const char *format, ...);
00096
00097
00098
00104 Bool b_potentiometer_init ( void )
00105 {
00106 portCHAR token[6];
00107 portCHAR * percent;
00108
00109
00110 if( pdTRUE == x_supervisor_SemaphoreTake( xCFGMutex, 0 ) )
00111 {
00112
00113 if (config_file_get_value(SENSOR_POT_CONFIG_FILE, "alarm" , token) >= 0)
00114 {
00115
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
00144 x_supervisor_SemaphoreGive( xCFGMutex );
00145 }
00146
00147 gpio_enable_module_pin( ADC_POTENTIOMETER_PIN , ADC_POTENTIOMETER_FUNCTION );
00148
00149 return (TRUE);
00150 }
00151
00161 eExecStatus e_potentiometer_get_config( signed portCHAR **ppcStringReply )
00162 {
00163
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
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 }
00177
00187 eExecStatus e_potentiometer_set_config( signed portCHAR **ppcStringReply, int ac, signed portCHAR *av[] )
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
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 }
00252
00260 Bool b_potentiometer_get_value( xLogDef *pxLog )
00261 {
00262 int i_current_val;
00263
00264
00265
00266 adc_enable( adc, ADC_POTENTIOMETER_CHANNEL );
00267
00268 adc_start( adc );
00269
00270 i_current_val = adc_get_value( adc, ADC_POTENTIOMETER_CHANNEL ) * 100 / ADC_MAX_VALUE;
00271
00272 adc_disable( adc, ADC_POTENTIOMETER_CHANNEL );
00273
00274
00275 pxLog->pcStringLog = pvPortMalloc( 16*sizeof( char ) );
00276 if( NULL == pxLog->pcStringLog )
00277 {
00278 return( FALSE );
00279 }
00280 pxLog->pfFreeStringLog = vPortFree;
00281
00282
00283
00284 if( i_current_val <= ul_pot_min )
00285 {
00286 sprintf( pxLog->pcStringLog, "%3d%% | min", i_current_val );
00287
00288 if (( b_pot_alarm == pdTRUE ) && ( b_pot_alarm_min == pdFALSE ))
00289 {
00290
00291
00292 b_pot_alarm_min = pdTRUE;
00293
00294 b_pot_alarm_max = pdFALSE;
00295
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
00303 if (( b_pot_alarm == pdTRUE ) && ( b_pot_alarm_max == pdFALSE ))
00304 {
00305
00306
00307 b_pot_alarm_max = pdTRUE;
00308
00309 b_pot_alarm_min = pdFALSE;
00310
00311 v_SMTP_Post("Max Potentiometer Alarm", NULL);
00312 }
00313 }
00314 else
00315 {
00316 sprintf( pxLog->pcStringLog, "%3d%%", i_current_val );
00317
00318 if ( b_pot_alarm == pdTRUE )
00319 {
00320
00321 b_pot_alarm_max = pdFALSE;
00322 b_pot_alarm_min = pdFALSE;
00323 }
00324 }
00325
00326 return( TRUE );
00327 }