src/led.c
changeset 93 16c8ceea8f18
parent 92 0d84d95790d9
child 94 bdf4c86be6b2
equal deleted inserted replaced
92:0d84d95790d9 93:16c8ceea8f18
     1 /*
       
     2 This file is part of CanFestival, a library implementing CanOpen Stack.
       
     3 
       
     4  Author: CANopen Canada (canfestival@canopencanada.ca)
       
     5 
       
     6 See COPYING file for copyrights details.
       
     7 
       
     8 This library is free software; you can redistribute it and/or
       
     9 modify it under the terms of the GNU Lesser General Public
       
    10 License as published by the Free Software Foundation; either
       
    11 version 2.1 of the License, or (at your option) any later version.
       
    12 
       
    13 This library is distributed in the hope that it will be useful,
       
    14 but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    16 Lesser General Public License for more details.
       
    17 
       
    18 You should have received a copy of the GNU Lesser General Public
       
    19 License along with this library; if not, write to the Free Software
       
    20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
       
    21 
       
    22 	DS-303-3
       
    23 	LED implementation
       
    24 */
       
    25 
       
    26 #include <stdlib.h>
       
    27 #include <string.h>
       
    28 
       
    29 #include <sys/time.h>
       
    30 #include <signal.h>
       
    31 
       
    32 #include <applicfg.h>
       
    33 
       
    34 #include <data.h>
       
    35 #include <can_driver.h>
       
    36 
       
    37 #include "led.h"
       
    38 
       
    39 
       
    40 void led_start_timer(CO_Data *, UNS32 t0);
       
    41 void led_stop_timer(void);
       
    42 void led_set_green(UNS8 on);
       
    43 void led_set_red(UNS8 on);
       
    44 void led_callback(CO_Data* d, UNS32 id);
       
    45 void led_set_redgreen(CO_Data *d, unsigned char state);
       
    46 
       
    47 
       
    48 /* 0 = always off, 1 = always on, 2 = flashing */
       
    49 static UNS8 led_state_red, led_state_green; 
       
    50 
       
    51 static UNS16 led_sequence_red, led_seq_index_red;
       
    52 static UNS16 led_sequence_green, led_seq_index_green;
       
    53 
       
    54 static UNS8 led_error_code = LED_NO_ERROR;
       
    55 
       
    56 const char *led_sequence_table[6] = /* up and downs of the sequence */
       
    57 {
       
    58 	"01",           /* flickering */
       
    59 	"01",           /* blinking */
       
    60 	"100000",       /* single flash */
       
    61 	"10100000",     /* double flash */
       
    62 	"1010100000",   /* triple flash */
       
    63 	"101010100000"  /* quadruple flash */
       
    64 };
       
    65 
       
    66 
       
    67 void led_set_state(CO_Data *d, int state)
       
    68 {
       
    69 /*printf("led_set_state(%x)\n", state); */
       
    70 
       
    71 	switch(state)
       
    72 	{
       
    73 		case Initialisation:
       
    74 /*
       
    75 	must create a timer for the leds with the scheduler
       
    76 */
       
    77 			break;
       
    78 		case LED_AUTOBITRATE:
       
    79 			led_state_green = 2;
       
    80 			led_sequence_green = 0;
       
    81 			break;
       
    82 		case Pre_operational:
       
    83 			led_state_green = 2;
       
    84 			led_sequence_green = 1;
       
    85 			break;
       
    86 		case Stopped:
       
    87 			led_state_green = 2;
       
    88 			led_sequence_green = 2;
       
    89 			break;
       
    90 		case LED_PRG_DOWNLOAD:
       
    91 			led_state_green = 2;
       
    92 			led_sequence_green = 4;
       
    93 			break;
       
    94 		case Operational:
       
    95 			led_state_green = 1;
       
    96 			break;
       
    97 	}
       
    98 
       
    99 	if (state == LED_AUTOBITRATE)
       
   100 		led_start_timer(d, 50);
       
   101 
       
   102 	else if (led_state_green < 2  &&  led_state_red < 2)
       
   103 	{
       
   104 		led_stop_timer();
       
   105 
       
   106 		/*led_set_green(led_state_green); */
       
   107 		/*led_set_red(led_state_red); */
       
   108 	}
       
   109 
       
   110 	else
       
   111 		led_start_timer(d, 200);
       
   112 }
       
   113 
       
   114 
       
   115 void led_set_error(CO_Data *d, UNS8 error)
       
   116 {
       
   117 	if (error == LED_NO_ERROR)
       
   118 	{
       
   119 		led_error_code = error;
       
   120 
       
   121 		led_state_green = 0;
       
   122 	}
       
   123 
       
   124 	else if (error == LED_AUTOBITRATE)
       
   125 	{
       
   126 		led_error_code = error;
       
   127 
       
   128 		led_state_red = 2;
       
   129 		led_sequence_red = 0;
       
   130 
       
   131 		led_start_timer(d, 50);
       
   132 	}
       
   133 
       
   134 	else if (error > led_error_code)
       
   135 	{
       
   136 		led_error_code = error;
       
   137 
       
   138 		if (error & LED_INVALID_CONFIG)
       
   139 		{
       
   140 			led_state_red = 2;
       
   141 			led_sequence_red = 1;
       
   142 		}
       
   143 		
       
   144 		else if (error & LED_WARNING_LIMIT_REACH)
       
   145 		{
       
   146 			led_state_red = 2;
       
   147 			led_sequence_red = 2;
       
   148 		}
       
   149 
       
   150 		else if (error & LED_ERROR_CTRL_EVENT)
       
   151 		{
       
   152 			led_state_red = 2;
       
   153 			led_sequence_red = 3;
       
   154 		}
       
   155 
       
   156 		else if (error & LED_SYNC_ERROR)
       
   157 		{
       
   158 			led_state_red = 2;
       
   159 			led_sequence_red = 4;
       
   160 		}
       
   161 
       
   162 		else if (error & LED_EVENT_TIMER_ERROR)
       
   163 		{
       
   164 			led_state_red = 2;
       
   165 			led_sequence_green = 5;
       
   166 		}
       
   167 
       
   168 		else if (error & LED_BUS_OFF)
       
   169 		{
       
   170 			led_state_green = 1;
       
   171 		}
       
   172 
       
   173 		led_start_timer(d, 200);
       
   174 		/*led_set_red(led_state_red); */
       
   175 	}
       
   176 
       
   177 	if (led_state_green < 2  &&  led_state_red < 2)
       
   178 	{
       
   179 		led_stop_timer();
       
   180 
       
   181 		/*led_set_green(led_state_green); */
       
   182 		/*led_set_red(led_state_red); */
       
   183 	}
       
   184 }
       
   185 
       
   186 
       
   187 void led_start_timer(CO_Data* d, UNS32 tm)
       
   188 {
       
   189 	SetAlarm(d, 0, &led_callback, MS_TO_TIMEVAL(tm), MS_TO_TIMEVAL(tm));
       
   190 
       
   191 	led_seq_index_red = 0;
       
   192 	led_seq_index_green = 0;
       
   193 }
       
   194 
       
   195 
       
   196 void led_stop_timer(void)
       
   197 {
       
   198 }
       
   199 
       
   200 
       
   201 void led_callback(CO_Data *d, UNS32 id)
       
   202 {
       
   203 	UNS8 bits = 0;
       
   204 
       
   205 	/* RED LED */
       
   206 	if (led_sequence_table[led_sequence_red][led_seq_index_red] == '1')
       
   207 	{
       
   208 		if (led_state_red > 0)
       
   209 			bits = 1;
       
   210 			/* led_set_red(1); */
       
   211 	}
       
   212 	else	
       
   213 	{	
       
   214 		/*if (led_state_red != 1)
       
   215 			led_set_red(0);*/
       
   216 	}
       
   217 
       
   218 	led_seq_index_red++;
       
   219 	if (led_seq_index_red > strlen(led_sequence_table[led_sequence_red]))
       
   220 		led_seq_index_red = 0;
       
   221 
       
   222 	/* GREEN LED */
       
   223 	if (led_sequence_table[led_sequence_green][led_seq_index_green] == '1')
       
   224 	{
       
   225 		if (led_state_green > 0)
       
   226 			bits = bits | 2;
       
   227 			/* led_set_green(1); */
       
   228 	}
       
   229 	else	
       
   230 	{	
       
   231 		/* if (led_state_green != 1)
       
   232 			led_set_green(0); */
       
   233 	}
       
   234 
       
   235 	led_seq_index_green++;
       
   236 	if (led_seq_index_green > strlen(led_sequence_table[led_sequence_green]))
       
   237 		led_seq_index_green = 0;
       
   238 
       
   239 	/*led_set_redgreen(d, bits); */
       
   240 }
       
   241 
       
   242 
       
   243 
       
   244 
       
   245 
       
   246 /*
       
   247 char state(state);
       
   248 
       
   249 
       
   250 Input function to set all the bihaviour indicator
       
   251 typical state are:
       
   252 	NoError
       
   253 		RedLED=off
       
   254 	AutoBitRate_LSS
       
   255 		RedLED=flickering
       
   256 		GreenLED=flickering
       
   257 	InvalidConfiguration
       
   258 		RedLED=blinking
       
   259 	WarningLimitReached
       
   260 		RedLED=singleflash
       
   261 	ErrorControlEvent
       
   262 		RedLED=doubleflash
       
   263 	SyncError
       
   264 		RedLED=tripleflash
       
   265 	EventTimerError
       
   266 		RedLED=quadrupleflash
       
   267 	BusOFF
       
   268 		RedLED=on
       
   269 	PRE_OPERATIONAL	
       
   270 		GreenLED=blinking
       
   271 	STOPPED
       
   272 		GreenLED=singleflash
       
   273 	Programm_Firmware_download
       
   274 		GreenLED=tripleflash
       
   275 	OPERATIONNAL
       
   276 		GreenLED=on
       
   277 */
       
   278 
       
   279 /*
       
   280 case LEDbihaviour:
       
   281 	on
       
   282 
       
   283 	flickeringerror
       
   284 		RedLED(on)
       
   285 		RedLED(off)
       
   286 	flickeringerror
       
   287 		GreenLED(off)
       
   288 		GreenLED(on)
       
   289 	blinking
       
   290 
       
   291 	singleflash
       
   292 
       
   293 	doubleflash
       
   294 
       
   295 	tripleflash
       
   296 
       
   297 	quadrupleflash
       
   298 
       
   299 	off
       
   300 */
       
   301 
       
   302 /*
       
   303 char  LED(bitLEDs);
       
   304 */
       
   305 
       
   306 /*
       
   307 Output function to call the driver.
       
   308 	if bit=0, then turn LED Off
       
   309 	if bit=1, then turn LED On
       
   310 
       
   311 bit#	color		name
       
   312 0	red		error/status
       
   313 1	green		run/status
       
   314 2
       
   315 3
       
   316 4
       
   317 5
       
   318 6	
       
   319 7
       
   320 */
       
   321