src/led.c
changeset 0 4472ee7c6c3e
child 3 d9cf34cd6823
equal deleted inserted replaced
-1:000000000000 0:4472ee7c6c3e
       
     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 
       
    46 
       
    47 // 0 = always off, 1 = always on, 2 = flashing
       
    48 static UNS8 led_state_red, led_state_green; 
       
    49 
       
    50 static UNS16 led_sequence_red, led_seq_index_red;
       
    51 static UNS16 led_sequence_green, led_seq_index_green;
       
    52 
       
    53 static UNS8 led_error_code = LED_NO_ERROR;
       
    54 
       
    55 const char *led_sequence_table[6] = // up and downs of the sequence
       
    56 {
       
    57 	"01",           // flickering
       
    58 	"01",           // blinking
       
    59 	"100000",       // single flash
       
    60 	"10100000",     // double flash
       
    61 	"1010100000",   // triple flash
       
    62 	"101010100000"  // quadruple flash
       
    63 };
       
    64 
       
    65 
       
    66 void led_set_state(CO_Data *d, int state)
       
    67 {
       
    68 	switch(state)
       
    69 	{
       
    70 		case Initialisation:
       
    71 /*
       
    72 	must create a timer for the leds with the scheduler
       
    73 */
       
    74 			break;
       
    75 		case LED_AUTOBITRATE:
       
    76 			led_state_green = 2;
       
    77 			led_sequence_green = 0;
       
    78 			break;
       
    79 		case Pre_operational:
       
    80 			led_state_green = 2;
       
    81 			led_sequence_green = 1;
       
    82 			break;
       
    83 		case Stopped:
       
    84 			led_state_green = 2;
       
    85 			led_sequence_green = 2;
       
    86 			break;
       
    87 		case LED_PRG_DOWNLOAD:
       
    88 			led_state_green = 2;
       
    89 			led_sequence_green = 4;
       
    90 			break;
       
    91 		case Operational:
       
    92 			led_state_green = 1;
       
    93 			break;
       
    94 	}
       
    95 
       
    96 	if (state == LED_AUTOBITRATE)
       
    97 		led_start_timer(d, 50);
       
    98 
       
    99 	else if (led_state_green < 2  &&  led_state_red < 2)
       
   100 	{
       
   101 		led_stop_timer();
       
   102 
       
   103 		led_set_green(led_state_green);
       
   104 		led_set_red(led_state_red);
       
   105 	}
       
   106 
       
   107 	else
       
   108 		led_start_timer(d, 200);
       
   109 }
       
   110 
       
   111 
       
   112 void led_set_error(CO_Data *d, UNS8 error)
       
   113 {
       
   114 	if (error == LED_NO_ERROR)
       
   115 	{
       
   116 		led_error_code = error;
       
   117 
       
   118 		led_state_green = 0;
       
   119 	}
       
   120 
       
   121 	else if (error == LED_AUTOBITRATE)
       
   122 	{
       
   123 		led_error_code = error;
       
   124 
       
   125 		led_state_red = 2;
       
   126 		led_sequence_red = 0;
       
   127 
       
   128 		led_start_timer(d, 50);
       
   129 	}
       
   130 
       
   131 	else if (error > led_error_code)
       
   132 	{
       
   133 		led_error_code = error;
       
   134 
       
   135 		if (error & LED_INVALID_CONFIG)
       
   136 		{
       
   137 			led_state_red = 2;
       
   138 			led_sequence_red = 1;
       
   139 		}
       
   140 		
       
   141 		else if (error & LED_WARNING_LIMIT_REACH)
       
   142 		{
       
   143 			led_state_red = 2;
       
   144 			led_sequence_red = 2;
       
   145 		}
       
   146 
       
   147 		else if (error & LED_ERROR_CTRL_EVENT)
       
   148 		{
       
   149 			led_state_red = 2;
       
   150 			led_sequence_red = 3;
       
   151 		}
       
   152 
       
   153 		else if (error & LED_SYNC_ERROR)
       
   154 		{
       
   155 			led_state_red = 2;
       
   156 			led_sequence_red = 4;
       
   157 		}
       
   158 
       
   159 		else if (error & LED_EVENT_TIMER_ERROR)
       
   160 		{
       
   161 			led_state_red = 2;
       
   162 			led_sequence_green = 5;
       
   163 		}
       
   164 
       
   165 		else if (error & LED_BUS_OFF)
       
   166 		{
       
   167 			led_state_green = 1;
       
   168 		}
       
   169 
       
   170 		led_start_timer(d, 200);
       
   171 		led_set_red(led_state_red);
       
   172 	}
       
   173 
       
   174 	if (led_state_green < 2  &&  led_state_red < 2)
       
   175 	{
       
   176 		led_stop_timer();
       
   177 
       
   178 		led_set_green(led_state_green);
       
   179 		led_set_red(led_state_red);
       
   180 	}
       
   181 }
       
   182 
       
   183 
       
   184 void led_start_timer(CO_Data* d, UNS32 tm)
       
   185 {
       
   186 	SetAlarm(d, 0, &led_callback, MS_TO_TIMEVAL(tm), MS_TO_TIMEVAL(tm));
       
   187 
       
   188 	led_seq_index_red = 0;
       
   189 	led_seq_index_green = 0;
       
   190 }
       
   191 
       
   192 
       
   193 void led_stop_timer(void)
       
   194 {
       
   195 }
       
   196 
       
   197 
       
   198 void led_callback(CO_Data *d, UNS32 id)
       
   199 {
       
   200 	unsigned char bits = 0;
       
   201 
       
   202 	// RED LED
       
   203 	if (led_sequence_table[led_sequence_red][led_seq_index_red] == '1')
       
   204 	{
       
   205 		if (led_state_red > 0)
       
   206 			bits = 1;
       
   207 			/* led_set_red(1); */
       
   208 	}
       
   209 	else	
       
   210 	{	
       
   211 		/*if (led_state_red != 1)
       
   212 			led_set_red(0);*/
       
   213 	}
       
   214 
       
   215 	led_seq_index_red++;
       
   216 	if (led_seq_index_red > strlen(led_sequence_table[led_sequence_red]))
       
   217 		led_seq_index_red = 0;
       
   218 
       
   219 	// GREEN LED
       
   220 	if (led_sequence_table[led_sequence_green][led_seq_index_green] == '1')
       
   221 	{
       
   222 		if (led_state_green > 0)
       
   223 			bits = bits | 2;
       
   224 			/* led_set_green(1); */
       
   225 	}
       
   226 	else	
       
   227 	{	
       
   228 		/* if (led_state_green != 1)
       
   229 			led_set_green(0); */
       
   230 	}
       
   231 
       
   232 	led_seq_index_green++;
       
   233 	if (led_seq_index_green > strlen(led_sequence_table[led_sequence_green]))
       
   234 		led_seq_index_green = 0;
       
   235 
       
   236 	led_set_redgreen(bits);
       
   237 }
       
   238 
       
   239 
       
   240 
       
   241 
       
   242 
       
   243 /*
       
   244 char state(state);
       
   245 
       
   246 
       
   247 Input function to set all the bihaviour indicator
       
   248 typical state are:
       
   249 	NoError
       
   250 		RedLED=off
       
   251 	AutoBitRate_LSS
       
   252 		RedLED=flickering
       
   253 		GreenLED=flickering
       
   254 	InvalidConfiguration
       
   255 		RedLED=blinking
       
   256 	WarningLimitReached
       
   257 		RedLED=singleflash
       
   258 	ErrorControlEvent
       
   259 		RedLED=doubleflash
       
   260 	SyncError
       
   261 		RedLED=tripleflash
       
   262 	EventTimerError
       
   263 		RedLED=quadrupleflash
       
   264 	BusOFF
       
   265 		RedLED=on
       
   266 	PRE_OPERATIONAL	
       
   267 		GreenLED=blinking
       
   268 	STOPPED
       
   269 		GreenLED=singleflash
       
   270 	Programm_Firmware_download
       
   271 		GreenLED=tripleflash
       
   272 	OPERATIONNAL
       
   273 		GreenLED=on
       
   274 */
       
   275 
       
   276 /*
       
   277 case LEDbihaviour:
       
   278 	on
       
   279 
       
   280 	flickeringerror
       
   281 		RedLED(on)
       
   282 		RedLED(off)
       
   283 	flickeringerror
       
   284 		GreenLED(off)
       
   285 		GreenLED(on)
       
   286 	blinking
       
   287 
       
   288 	singleflash
       
   289 
       
   290 	doubleflash
       
   291 
       
   292 	tripleflash
       
   293 
       
   294 	quadrupleflash
       
   295 
       
   296 	off
       
   297 */
       
   298 
       
   299 /*
       
   300 char  LED(bitLEDs);
       
   301 */
       
   302 
       
   303 /*
       
   304 Output function to call the driver.
       
   305 	if bit=0, then turn LED Off
       
   306 	if bit=1, then turn LED On
       
   307 
       
   308 bit#	color		name
       
   309 0	red		error/status
       
   310 1	green		run/status
       
   311 2
       
   312 3
       
   313 4
       
   314 5
       
   315 6	
       
   316 7
       
   317 */
       
   318