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 "FreeRTOS.h"
00050
00051 #include "datalog.h"
00052 #include "shell.h"
00053 #include "board.h"
00054 #include "gpio.h"
00055 #include "adc.h"
00056 #include "sensor.h"
00057 #include "config_file.h"
00058 #include "fsaccess.h"
00059
00060 #include "BasicSMTP.h"
00061
00062
00063
00065 #define TEMPERATURE_GETCONF_MAXLEN 64
00066
00068 #define SENSOR_TEMP_CONFIG_FILE "A:/CFG/TEMP.TXT"
00069
00070
00071
00072
00079 const unsigned short temperature_code[]={
00080 0x3B4,0x3B0,0x3AB,0x3A6,0x3A0,0x39A,0x394,0x38E,0x388,0x381,0x37A,0x373,
00081 0x36B,0x363,0x35B,0x353,0x34A,0x341,0x338,0x32F,0x325,0x31B,0x311,0x307,
00082 0x2FC,0x2F1,0x2E6,0x2DB,0x2D0,0x2C4,0x2B8,0x2AC,0x2A0,0x294,0x288,0x27C,
00083 0x26F,0x263,0x256,0x24A,0x23D,0x231,0x225,0x218,0x20C,0x200,0x1F3,0x1E7,
00084 0x1DB,0x1CF,0x1C4,0x1B8,0x1AC,0x1A1,0x196,0x18B,0x180,0x176,0x16B,0x161,
00085 0x157,0x14D,0x144,0x13A,0x131,0x128,0x11F,0x117,0x10F,0x106,0xFE,0xF7,
00086 0xEF,0xE8,0xE1,0xDA,0xD3,0xCD,0xC7,0xC0,0xBA,0xB5,0xAF,0xAA,0xA4,0x9F,
00087 0x9A,0x96,0x91,0x8C,0x88,0x84,0x80,0x7C,0x78,0x74,0x71,0x6D,0x6A,0x67,
00088 0x64,0x61,0x5E,0x5B,0x58,0x55,0x53,0x50,0x4E,0x4C,0x49,0x47,0x45,0x43,
00089 0x41,0x3F,0x3D,0x3C,0x3A,0x38};
00090
00092 unsigned int ul_temp_lograte = 5;
00093
00095 static int l_temp_min = -50;
00096
00098 static int l_temp_max = 100;
00099
00101 static Bool b_temp_alarm = pdFALSE;
00103 static Bool b_temp_alarm_max = pdFALSE;
00105 static Bool b_temp_alarm_min = pdFALSE;
00106
00108 static volatile avr32_adc_t * adc= (volatile avr32_adc_t *) &AVR32_ADC;
00109
00111 extern xSemaphoreHandle xCFGMutex;
00112
00113
00114 extern int sprintf(char *out, const char *format, ...);
00115
00116
00122 Bool b_temperature_init ( void )
00123 {
00124 portCHAR token[6];
00125 portCHAR * unit;
00126
00127
00128 if( pdTRUE == x_supervisor_SemaphoreTake( xCFGMutex, 0 ) )
00129 {
00130
00131 if (config_file_get_value(SENSOR_TEMP_CONFIG_FILE, "alarm" , token) >= 0)
00132 {
00133
00134 if (!strcmp(token, "on"))
00135 {
00136 b_temp_alarm = pdTRUE;
00137 }
00138 }
00139 if (config_file_get_value(SENSOR_TEMP_CONFIG_FILE, "min" , token) >= 0)
00140 {
00141 unit = strpbrk(token , "C");
00142 if (unit != NULL)
00143 {
00144 *unit = '\0';
00145 }
00146 l_temp_min = atol(token);
00147 }
00148 if (config_file_get_value(SENSOR_TEMP_CONFIG_FILE, "max" , token) >= 0)
00149 {
00150 unit = strpbrk(token , "C");
00151 if (unit != NULL)
00152 {
00153 *unit = '\0';
00154 }
00155 l_temp_max = atol(token);
00156 }
00157 if (config_file_get_value(SENSOR_TEMP_CONFIG_FILE, "lograte" , token) >= 0)
00158 {
00159 ul_temp_lograte = atoi(token);
00160 }
00161
00162 x_supervisor_SemaphoreGive( xCFGMutex );
00163 }
00164
00165 gpio_enable_module_pin( ADC_TEMPERATURE_PIN , ADC_TEMPERATURE_FUNCTION );
00166
00167 return (TRUE);
00168 }
00169
00179 eExecStatus e_temperature_get_config( signed portCHAR **ppcStringReply )
00180 {
00181
00182 *ppcStringReply = (signed portCHAR *)pvPortMalloc( TEMPERATURE_GETCONF_MAXLEN );
00183 if( NULL == *ppcStringReply )
00184 {
00185 *ppcStringReply = (signed portCHAR *)SHELL_ERRMSG_MEMALLOC;
00186 return( SHELL_EXECSTATUS_KO );
00187 }
00188
00189 sprintf( (char *)*ppcStringReply, "lograte=%d\r\n""min=%dC\r\n""max=%dC\r\n""alarm=%s\r\n",
00190 ul_temp_lograte, l_temp_min, l_temp_max, ((b_temp_alarm == pdTRUE) ? "on" : "off") );
00191 return( SHELL_EXECSTATUS_OK );
00192 }
00193
00203 eExecStatus e_temperature_set_config( signed portCHAR **ppcStringReply, int ac, signed portCHAR *av[] )
00204 {
00205 portCHAR * token;
00206
00207
00208 if (config_file_set_value(SENSOR_TEMP_CONFIG_FILE, ac, av) != 0)
00209 {
00210 *ppcStringReply = (signed portCHAR *)SHELL_ERRMSG_CONFIGERROR;
00211
00212 return( SHELL_EXECSTATUS_KO );
00213 }
00214
00215
00216 if (!strcmp((char *)av[0] , "alarm"))
00217 {
00218 if (!strcmp((char *)av[1] , "on"))
00219 {
00220 b_temp_alarm = pdTRUE;
00221 *ppcStringReply = (signed portCHAR *)SENSOR_MSG_ALARM_ON;
00222 return( SHELL_EXECSTATUS_OK_NO_FREE );
00223 }
00224 else if (!strcmp( (char *)av[1], "off"))
00225 {
00226 b_temp_alarm = pdFALSE;
00227 *ppcStringReply = (signed portCHAR *)SENSOR_MSG_ALARM_OFF;
00228 return( SHELL_EXECSTATUS_OK_NO_FREE );
00229 }
00230 else
00231 {
00232 *ppcStringReply = (signed portCHAR *)SHELL_ERRMSG_CONFIGERROR;
00233 return( SHELL_EXECSTATUS_KO );
00234 }
00235 }
00236
00237 else if (!strcmp((char *)av[0] , "lograte"))
00238 {
00239 ul_temp_lograte = atoi((char *)av[1]);
00240 *ppcStringReply = (signed portCHAR *)SENSOR_MSG_CONFIG_SET;
00241 return( SHELL_EXECSTATUS_OK_NO_FREE );
00242 }
00243
00244 else if (!strcmp((char *)av[0] , "min"))
00245 {
00246 token = strpbrk((char *)av[1] , "C");
00247 if (token != NULL)
00248 {
00249 *token = '\0';
00250 }
00251 l_temp_min = atoi((char *)av[1]);
00252 *ppcStringReply = (signed portCHAR *)SENSOR_MSG_CONFIG_SET;
00253 return( SHELL_EXECSTATUS_OK_NO_FREE );
00254 }
00255
00256 else if (!strcmp((char *)av[0] , "max"))
00257 {
00258 token = strpbrk((char *)av[1] , "C");
00259 if (token != NULL)
00260 {
00261 *token = '\0';
00262 }
00263 l_temp_max = atoi((char *)av[1]);
00264 *ppcStringReply = (signed portCHAR *)SENSOR_MSG_CONFIG_SET;
00265 return( SHELL_EXECSTATUS_OK_NO_FREE );
00266 }
00267
00268 else
00269
00270 {
00271 *ppcStringReply = (signed portCHAR *)SHELL_ERRMSG_CONFIGERROR;
00272 return( SHELL_EXECSTATUS_KO );
00273 }
00274 }
00275
00283 Bool b_temperature_get_value( xLogDef *pxLog )
00284 {
00285 int i_current_val, value, index = 0;
00286
00287
00288
00289 adc_enable( adc, ADC_TEMPERATURE_CHANNEL );
00290
00291 adc_start( adc );
00292
00293 value = adc_get_value( adc, ADC_TEMPERATURE_CHANNEL );
00294
00295 adc_disable( adc, ADC_TEMPERATURE_CHANNEL );
00296
00297 if(value > temperature_code[0])
00298 {
00299 i_current_val = -20;
00300 }
00301 else
00302 {
00303 while(temperature_code[index++] > value);
00304 i_current_val = (index - 1 - 20);
00305 }
00306
00307
00308 pxLog->pcStringLog = pvPortMalloc( 12*sizeof( char ) );
00309 if( NULL == pxLog->pcStringLog )
00310 {
00311 return( FALSE );
00312 }
00313 pxLog->pfFreeStringLog = vPortFree;
00314
00315
00316
00317 if( i_current_val <= l_temp_min )
00318 {
00319 sprintf( pxLog->pcStringLog, "%3dC | min", i_current_val );
00320
00321 if (( b_temp_alarm == pdTRUE ) && ( b_temp_alarm_min == pdFALSE ))
00322 {
00323
00324
00325 b_temp_alarm_min = pdTRUE;
00326
00327 b_temp_alarm_max = pdFALSE;
00328
00329 v_SMTP_Post("Min Temp Alarm", NULL);
00330 }
00331 }
00332 else if( i_current_val >= l_temp_max )
00333 {
00334 sprintf( pxLog->pcStringLog, "%3dC | max", i_current_val );
00335
00336 if (( b_temp_alarm == pdTRUE ) && ( b_temp_alarm_max == pdFALSE ))
00337 {
00338
00339
00340 b_temp_alarm_max = pdTRUE;
00341
00342 b_temp_alarm_min = pdFALSE;
00343
00344 v_SMTP_Post("Max Temp Alarm", NULL);
00345 }
00346 }
00347 else
00348 {
00349 sprintf( pxLog->pcStringLog, "%3dC", i_current_val );
00350
00351 if ( b_temp_alarm == pdTRUE )
00352 {
00353
00354 b_temp_alarm_max = pdFALSE;
00355 b_temp_alarm_min = pdFALSE;
00356 }
00357 }
00358
00359 return( TRUE );
00360 }