Refactor alac decoder as preparation for upcoming m4a changes. The alac decoder does...
[kugel-rb.git] / firmware / backlight.c
blobd24fc1342f1ee8cffb4027014260cbe14410c827
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 by Linus Nielsen Feltzing
11 * Additional work by Martin Ritter (2007) and Thomas Martitz (2008)
12 * for backlight thread fading
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 * KIND, either express or implied.
22 ****************************************************************************/
23 #include "config.h"
24 #include <stdlib.h>
25 #include "cpu.h"
26 #include "kernel.h"
27 #include "thread.h"
28 #include "i2c.h"
29 #include "debug.h"
30 #include "rtc.h"
31 #include "usb.h"
32 #include "power.h"
33 #include "system.h"
34 #include "button.h"
35 #include "timer.h"
36 #include "backlight.h"
37 #include "lcd.h"
38 #include "screendump.h"
40 #ifdef HAVE_REMOTE_LCD
41 #include "lcd-remote.h"
42 #endif
44 #ifndef SIMULATOR
45 #include "backlight-target.h"
46 #else
47 #include "backlight-sim.h"
48 #endif
50 #if defined(HAVE_BACKLIGHT) && defined(BACKLIGHT_FULL_INIT)
52 #if (CONFIG_BACKLIGHT_FADING == BACKLIGHT_FADING_SW_SETTING) \
53 || (CONFIG_BACKLIGHT_FADING == BACKLIGHT_FADING_SW_HW_REG)
54 #include "backlight-sw-fading.h"
55 #endif
57 #define BACKLIGHT_FADE_IN_THREAD \
58 (CONFIG_BACKLIGHT_FADING & (BACKLIGHT_FADING_SW_SETTING \
59 |BACKLIGHT_FADING_SW_HW_REG \
60 |BACKLIGHT_FADING_PWM) )
62 #define BACKLIGHT_THREAD_TIMEOUT HZ
64 enum {
65 BACKLIGHT_ON,
66 BACKLIGHT_OFF,
67 BACKLIGHT_TMO_CHANGED,
68 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
69 BACKLIGHT_BRIGHTNESS_CHANGED,
70 #endif
71 #ifdef HAVE_REMOTE_LCD
72 REMOTE_BACKLIGHT_ON,
73 REMOTE_BACKLIGHT_OFF,
74 REMOTE_BACKLIGHT_TMO_CHANGED,
75 #endif
76 #if defined(_BACKLIGHT_FADE_BOOST) || defined(_BACKLIGHT_FADE_ENABLE)
77 BACKLIGHT_FADE_FINISH,
78 #endif
79 #ifdef HAVE_LCD_SLEEP
80 LCD_SLEEP,
81 #endif
82 #ifdef HAVE_BUTTON_LIGHT
83 BUTTON_LIGHT_ON,
84 BUTTON_LIGHT_OFF,
85 BUTTON_LIGHT_TMO_CHANGED,
86 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
87 BUTTON_LIGHT_BRIGHTNESS_CHANGED,
88 #endif
89 #endif /* HAVE_BUTTON_LIGHT */
90 #ifdef BACKLIGHT_DRIVER_CLOSE
91 BACKLIGHT_QUIT,
92 #endif
95 static void backlight_thread(void);
96 static long backlight_stack[DEFAULT_STACK_SIZE/sizeof(long)];
97 static const char backlight_thread_name[] = "backlight";
98 static struct event_queue backlight_queue SHAREDBSS_ATTR;
99 #ifdef BACKLIGHT_DRIVER_CLOSE
100 static unsigned int backlight_thread_id = 0;
101 #endif
103 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
104 int backlight_brightness = DEFAULT_BRIGHTNESS_SETTING;
105 #endif
106 static int backlight_timer SHAREDBSS_ATTR;
107 static int backlight_timeout_normal = 5*HZ;
108 #if CONFIG_CHARGING
109 static int backlight_timeout_plugged = 5*HZ;
110 #endif
111 #ifdef HAS_BUTTON_HOLD
112 static int backlight_on_button_hold = 0;
113 #endif
114 static void backlight_timeout_handler(void);
116 #ifdef HAVE_BUTTON_LIGHT
117 static int buttonlight_timer;
118 static int buttonlight_timeout = 5*HZ;
120 /* Update state of buttonlight according to timeout setting */
121 static void buttonlight_update_state(void)
123 buttonlight_timer = buttonlight_timeout;
125 /* Buttonlight == OFF in the setting? */
126 if (buttonlight_timer < 0)
128 buttonlight_timer = 0; /* Disable the timeout */
129 _buttonlight_off();
131 else
132 _buttonlight_on();
135 /* external interface */
136 void buttonlight_on(void)
138 queue_remove_from_head(&backlight_queue, BUTTON_LIGHT_ON);
139 queue_post(&backlight_queue, BUTTON_LIGHT_ON, 0);
142 void buttonlight_off(void)
144 queue_post(&backlight_queue, BUTTON_LIGHT_OFF, 0);
147 void buttonlight_set_timeout(int value)
149 buttonlight_timeout = HZ * value;
150 queue_post(&backlight_queue, BUTTON_LIGHT_TMO_CHANGED, 0);
153 int buttonlight_get_current_timeout(void)
155 return buttonlight_timeout;
158 #endif /* HAVE_BUTTON_LIGHT */
160 #ifdef HAVE_REMOTE_LCD
161 static int remote_backlight_timer;
162 static int remote_backlight_timeout_normal = 5*HZ;
163 #if CONFIG_CHARGING
164 static int remote_backlight_timeout_plugged = 5*HZ;
165 #endif
166 #ifdef HAS_REMOTE_BUTTON_HOLD
167 static int remote_backlight_on_button_hold = 0;
168 #endif
169 #endif /* HAVE_REMOTE_LCD */
171 #ifdef HAVE_LCD_SLEEP
172 #ifdef HAVE_LCD_SLEEP_SETTING
173 const signed char lcd_sleep_timeout_value[10] =
175 -1, 0, 5, 10, 15, 20, 30, 45, 60, 90
177 static int lcd_sleep_timeout = 10*HZ;
178 #else
179 /* Target defines needed value */
180 #define lcd_sleep_timeout LCD_SLEEP_TIMEOUT
181 #endif
183 static int lcd_sleep_timer SHAREDDATA_ATTR = 0;
185 static void backlight_lcd_sleep_countdown(bool start)
187 if (!start)
189 /* Cancel the LCD sleep countdown */
190 lcd_sleep_timer = 0;
191 return;
194 /* Start LCD sleep countdown */
195 if (lcd_sleep_timeout < 0)
197 lcd_sleep_timer = 0; /* Setting == Always */
198 /* Ensure lcd_sleep() is called from backlight_thread() */
199 #if (CONFIG_BACKLIGHT_FADING == BACKLIGHT_FADING_PWM)
200 queue_post(&backlight_queue, LCD_SLEEP, 0);
201 #else
202 lcd_sleep();
203 #endif
205 else
207 lcd_sleep_timer = lcd_sleep_timeout;
210 #endif /* HAVE_LCD_SLEEP */
212 #if (CONFIG_BACKLIGHT_FADING == BACKLIGHT_FADING_SW_SETTING) \
213 || (CONFIG_BACKLIGHT_FADING == BACKLIGHT_FADING_SW_HW_REG)
214 static int backlight_fading_type = (FADING_UP|FADING_DOWN);
215 static int backlight_fading_state = NOT_FADING;
216 #endif
219 /* backlight fading */
220 #if (CONFIG_BACKLIGHT_FADING == BACKLIGHT_FADING_PWM)
221 #define BL_PWM_INTERVAL 5 /* Cycle interval in ms */
222 #define BL_PWM_BITS 8
223 #define BL_PWM_COUNT (1<<BL_PWM_BITS)
225 /* s15.16 fixed point variables */
226 static int32_t bl_fade_in_step = ((BL_PWM_INTERVAL*BL_PWM_COUNT)<<16)/300;
227 static int32_t bl_fade_out_step = ((BL_PWM_INTERVAL*BL_PWM_COUNT)<<16)/2000;
228 static int32_t bl_dim_fraction = 0;
230 static int bl_dim_target = 0;
231 static int bl_dim_current = 0;
232 static enum {DIM_STATE_START, DIM_STATE_MAIN} bl_dim_state = DIM_STATE_START;
233 static bool bl_timer_active = false;
235 static void backlight_isr(void)
237 int timer_period = (TIMER_FREQ*BL_PWM_INTERVAL/1000);
238 bool idle = false;
240 switch (bl_dim_state)
242 /* New cycle */
243 case DIM_STATE_START:
244 bl_dim_current = bl_dim_fraction >> 16;
246 if (bl_dim_current > 0 && bl_dim_current < BL_PWM_COUNT)
248 _backlight_on_isr();
249 timer_period = (timer_period * bl_dim_current) >> BL_PWM_BITS;
250 bl_dim_state = DIM_STATE_MAIN;
252 else
254 if (bl_dim_current)
255 _backlight_on_isr();
256 else
257 _backlight_off_isr();
258 if (bl_dim_current == bl_dim_target)
259 idle = true;
261 if (bl_dim_current < bl_dim_target)
263 bl_dim_fraction = MIN(bl_dim_fraction + bl_fade_in_step,
264 (BL_PWM_COUNT<<16));
266 else if (bl_dim_current > bl_dim_target)
268 bl_dim_fraction = MAX(bl_dim_fraction - bl_fade_out_step, 0);
270 break;
272 /* Dim main screen */
273 case DIM_STATE_MAIN:
274 _backlight_off_isr();
275 timer_period = (timer_period * (BL_PWM_COUNT - bl_dim_current))
276 >> BL_PWM_BITS;
277 bl_dim_state = DIM_STATE_START;
278 break ;
280 if (idle)
282 #if defined(_BACKLIGHT_FADE_BOOST) || defined(_BACKLIGHT_FADE_ENABLE)
283 queue_post(&backlight_queue, BACKLIGHT_FADE_FINISH, 0);
284 #endif
285 timer_unregister();
286 bl_timer_active = false;
288 #ifdef HAVE_LCD_SLEEP
289 if (bl_dim_current == 0)
290 backlight_lcd_sleep_countdown(true);
291 #endif
293 else
294 timer_set_period(timer_period);
297 static void backlight_switch(void)
299 if (bl_dim_target > (BL_PWM_COUNT/2))
301 _backlight_on_normal();
302 bl_dim_fraction = (BL_PWM_COUNT<<16);
304 else
306 _backlight_off_normal();
307 bl_dim_fraction = 0;
309 #ifdef HAVE_LCD_SLEEP
310 backlight_lcd_sleep_countdown(true);
311 #endif
315 static void backlight_release_timer(void)
317 #ifdef _BACKLIGHT_FADE_BOOST
318 cpu_boost(false);
319 #endif
320 timer_unregister();
321 bl_timer_active = false;
322 backlight_switch();
325 static void backlight_dim(int value)
327 /* protect from extraneous calls with the same target value */
328 if (value == bl_dim_target)
329 return;
331 bl_dim_target = value;
333 if (bl_timer_active)
334 return ;
336 if (timer_register(0, backlight_release_timer, 2, backlight_isr
337 IF_COP(, CPU)))
339 #ifdef _BACKLIGHT_FADE_BOOST
340 /* Prevent cpu frequency changes while dimming. */
341 cpu_boost(true);
342 #endif
343 bl_timer_active = true;
345 else
346 backlight_switch();
349 static void backlight_setup_fade_up(void)
351 if (bl_fade_in_step > 0)
353 #ifdef _BACKLIGHT_FADE_ENABLE
354 _backlight_hw_enable(true);
355 #endif
356 backlight_dim(BL_PWM_COUNT);
358 else
360 bl_dim_target = BL_PWM_COUNT;
361 bl_dim_fraction = (BL_PWM_COUNT<<16);
362 _backlight_on_normal();
366 static void backlight_setup_fade_down(void)
368 if (bl_fade_out_step > 0)
370 backlight_dim(0);
372 else
374 bl_dim_target = bl_dim_fraction = 0;
375 _backlight_off_normal();
376 #ifdef HAVE_LCD_SLEEP
377 backlight_lcd_sleep_countdown(true);
378 #endif
382 void backlight_set_fade_in(int value)
384 if (value > 0)
385 bl_fade_in_step = ((BL_PWM_INTERVAL*BL_PWM_COUNT)<<16) / value;
386 else
387 bl_fade_in_step = 0;
390 void backlight_set_fade_out(int value)
392 if (value > 0)
393 bl_fade_out_step = ((BL_PWM_INTERVAL*BL_PWM_COUNT)<<16) / value;
394 else
395 bl_fade_out_step = 0;
398 #elif (CONFIG_BACKLIGHT_FADING == BACKLIGHT_FADING_SW_SETTING) \
399 || (CONFIG_BACKLIGHT_FADING == BACKLIGHT_FADING_SW_HW_REG)
401 void backlight_set_fade_out(bool value)
403 if(value) /* on */
404 backlight_fading_type |= FADING_DOWN;
405 else
406 backlight_fading_type &= FADING_UP;
409 void backlight_set_fade_in(bool value)
411 if(value) /* on */
412 backlight_fading_type |= FADING_UP;
413 else
414 backlight_fading_type &= FADING_DOWN;
417 static void backlight_setup_fade_up(void)
419 if (backlight_fading_type & FADING_UP)
421 if (backlight_fading_state == NOT_FADING)
423 /* make sure the backlight is at lowest level */
424 _backlight_on();
426 backlight_fading_state = FADING_UP;
428 else
430 backlight_fading_state = NOT_FADING;
431 _backlight_fade_update_state(backlight_brightness);
432 _backlight_on();
433 _backlight_set_brightness(backlight_brightness);
437 static void backlight_setup_fade_down(void)
439 if (backlight_fading_type & FADING_DOWN)
441 backlight_fading_state = FADING_DOWN;
443 else
445 backlight_fading_state = NOT_FADING;
446 _backlight_fade_update_state(MIN_BRIGHTNESS_SETTING-1);
447 _backlight_off();
448 #if (CONFIG_BACKLIGHT_FADING == BACKLIGHT_FADING_SW_HW_REG)
449 /* write the lowest brightness level to the hardware so that
450 * fading up is glitch free */
451 _backlight_set_brightness(MIN_BRIGHTNESS_SETTING);
452 #endif
453 #ifdef HAVE_LCD_SLEEP
454 backlight_lcd_sleep_countdown(true);
455 #endif
458 #endif /* CONFIG_BACKLIGHT_FADING */
460 static inline void do_backlight_off(void)
462 backlight_timer = 0;
463 #if BACKLIGHT_FADE_IN_THREAD
464 backlight_setup_fade_down();
465 #else
466 _backlight_off();
467 /* targets that have fading need to start the countdown when done with
468 * fading */
469 #ifdef HAVE_LCD_SLEEP
470 backlight_lcd_sleep_countdown(true);
471 #endif
472 #endif
475 /* Update state of backlight according to timeout setting */
476 static void backlight_update_state(void)
479 int timeout = backlight_get_current_timeout();
481 /* Backlight == OFF in the setting? */
482 if (UNLIKELY(timeout < 0))
484 do_backlight_off();
485 #if (CONFIG_BACKLIGHT_FADING == BACKLIGHT_FADING_SW_SETTING) \
486 || (CONFIG_BACKLIGHT_FADING == BACKLIGHT_FADING_SW_HW_REG)
487 /* necessary step to issue fading down when the setting is selected */
488 if (queue_empty(&backlight_queue))
489 queue_post(&backlight_queue, SYS_TIMEOUT, 0);
490 #endif
492 else
494 backlight_timer = timeout;
496 #ifdef HAVE_LCD_SLEEP
497 backlight_lcd_sleep_countdown(false); /* wake up lcd */
498 #endif
500 #if BACKLIGHT_FADE_IN_THREAD
501 backlight_setup_fade_up();
502 #else
503 _backlight_on();
504 #endif
508 #ifdef HAVE_REMOTE_LCD
509 /* Update state of remote backlight according to timeout setting */
510 static void remote_backlight_update_state(void)
512 int timeout = remote_backlight_get_current_timeout();
513 /* Backlight == OFF in the setting? */
514 if (timeout < 0)
516 remote_backlight_timer = 0; /* Disable the timeout */
517 _remote_backlight_off();
519 else
521 remote_backlight_timer = timeout;
522 _remote_backlight_on();
525 #endif /* HAVE_REMOTE_LCD */
527 void backlight_thread(void)
529 struct queue_event ev;
530 bool locked = false;
532 while(1)
534 #if (CONFIG_BACKLIGHT_FADING == BACKLIGHT_FADING_SW_SETTING) \
535 || (CONFIG_BACKLIGHT_FADING == BACKLIGHT_FADING_SW_HW_REG)
536 if (backlight_fading_state != NOT_FADING)
537 queue_wait_w_tmo(&backlight_queue, &ev, FADE_DELAY);
538 else
539 #endif
540 queue_wait_w_tmo(&backlight_queue, &ev, BACKLIGHT_THREAD_TIMEOUT);
541 switch(ev.id)
542 { /* These events must always be processed */
543 #ifdef _BACKLIGHT_FADE_BOOST
544 case BACKLIGHT_FADE_FINISH:
545 cpu_boost(false);
546 break;
547 #endif
548 #ifdef _BACKLIGHT_FADE_ENABLE
549 case BACKLIGHT_FADE_FINISH:
550 _backlight_hw_enable((bl_dim_current|bl_dim_target) != 0);
551 break;
552 #endif
554 #ifndef SIMULATOR
555 /* Here for now or else the aggressive init messes up scrolling */
556 #ifdef HAVE_REMOTE_LCD
557 case SYS_REMOTE_PLUGGED:
558 lcd_remote_on();
559 lcd_remote_update();
560 break;
562 case SYS_REMOTE_UNPLUGGED:
563 lcd_remote_off();
564 break;
565 #elif defined HAVE_REMOTE_LCD_AS_MAIN
566 case SYS_REMOTE_PLUGGED:
567 lcd_on();
568 lcd_update();
569 break;
571 case SYS_REMOTE_UNPLUGGED:
572 lcd_off();
573 break;
574 #endif /* HAVE_REMOTE_LCD/ HAVE_REMOTE_LCD_AS_MAIN */
575 #endif /* !SIMULATOR */
576 case SYS_USB_CONNECTED:
577 usb_acknowledge(SYS_USB_CONNECTED_ACK);
578 break;
580 #ifdef BACKLIGHT_DRIVER_CLOSE
581 /* Get out of here */
582 case BACKLIGHT_QUIT:
583 return;
584 #endif
586 if (locked)
587 continue;
589 switch(ev.id)
590 { /* These events are only processed if backlight isn't locked */
591 #ifdef HAVE_REMOTE_LCD
592 case REMOTE_BACKLIGHT_TMO_CHANGED:
593 case REMOTE_BACKLIGHT_ON:
594 remote_backlight_update_state();
595 break;
597 case REMOTE_BACKLIGHT_OFF:
598 remote_backlight_timer = 0; /* Disable the timeout */
599 _remote_backlight_off();
600 break;
601 #endif /* HAVE_REMOTE_LCD */
603 case BACKLIGHT_TMO_CHANGED:
604 case BACKLIGHT_ON:
605 backlight_update_state();
606 break;
608 case BACKLIGHT_OFF:
609 do_backlight_off();
610 break;
611 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
612 case BACKLIGHT_BRIGHTNESS_CHANGED:
613 backlight_brightness = (int)ev.data;
614 _backlight_set_brightness((int)ev.data);
615 #if (CONFIG_BACKLIGHT_FADING == BACKLIGHT_FADING_SW_SETTING) \
616 || (CONFIG_BACKLIGHT_FADING == BACKLIGHT_FADING_SW_HW_REG)
617 /* receive backlight brightness */
618 _backlight_fade_update_state((int)ev.data);
619 #endif
620 break;
621 #endif /* HAVE_BACKLIGHT_BRIGHTNESS */
622 #ifdef HAVE_LCD_SLEEP
623 case LCD_SLEEP:
624 lcd_sleep();
625 break;
626 #endif
627 #ifdef HAVE_BUTTON_LIGHT
628 case BUTTON_LIGHT_TMO_CHANGED:
629 case BUTTON_LIGHT_ON:
630 buttonlight_update_state();
631 break;
633 case BUTTON_LIGHT_OFF:
634 buttonlight_timer = 0;
635 _buttonlight_off();
636 break;
637 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
638 case BUTTON_LIGHT_BRIGHTNESS_CHANGED:
639 _buttonlight_set_brightness((int)ev.data);
640 break;
641 #endif /* HAVE_BUTTONLIGHT_BRIGHTNESS */
642 #endif /* HAVE_BUTTON_LIGHT */
644 case SYS_POWEROFF: /* Lock backlight on poweroff so it doesn't */
645 locked = true; /* go off before power is actually cut. */
646 /* fall through */
647 #if CONFIG_CHARGING
648 case SYS_CHARGER_CONNECTED:
649 case SYS_CHARGER_DISCONNECTED:
650 #endif
651 backlight_update_state();
652 #ifdef HAVE_REMOTE_LCD
653 remote_backlight_update_state();
654 #endif
655 break;
656 case SYS_TIMEOUT:
657 #if (CONFIG_BACKLIGHT_FADING == BACKLIGHT_FADING_SW_SETTING) \
658 || (CONFIG_BACKLIGHT_FADING == BACKLIGHT_FADING_SW_HW_REG)
659 if (backlight_fading_state != NOT_FADING)
661 if ((_backlight_fade_step(backlight_fading_state)))
662 { /* finished fading */
663 #ifdef HAVE_LCD_SLEEP
664 if (backlight_fading_state == FADING_DOWN)
665 { /* start sleep countdown */
666 backlight_lcd_sleep_countdown(true);
668 #endif
669 backlight_fading_state = NOT_FADING;
672 else
673 #endif /* CONFIG_BACKLIGHT_FADING */
674 backlight_timeout_handler();
675 break;
677 } /* end while */
680 static void backlight_timeout_handler(void)
682 if(backlight_timer > 0)
684 backlight_timer -= BACKLIGHT_THREAD_TIMEOUT;
685 if(backlight_timer <= 0)
687 do_backlight_off();
690 #ifdef HAVE_LCD_SLEEP
691 else if(lcd_sleep_timer > 0)
693 lcd_sleep_timer -= BACKLIGHT_THREAD_TIMEOUT;
694 if(lcd_sleep_timer <= 0)
696 lcd_sleep();
699 #endif /* HAVE_LCD_SLEEP */
700 #ifdef HAVE_REMOTE_LCD
701 if(remote_backlight_timer > 0)
703 remote_backlight_timer -= BACKLIGHT_THREAD_TIMEOUT;
704 if(remote_backlight_timer <= 0)
706 _remote_backlight_off();
709 #endif /* HAVE_REMOVE_LCD */
710 #ifdef HAVE_BUTTON_LIGHT
711 if (buttonlight_timer > 0)
713 buttonlight_timer -= BACKLIGHT_THREAD_TIMEOUT;
714 if (buttonlight_timer <= 0)
716 _buttonlight_off();
719 #endif /* HAVE_BUTTON_LIGHT */
722 void backlight_init(void)
724 queue_init(&backlight_queue, true);
726 if (_backlight_init())
728 #if (CONFIG_BACKLIGHT_FADING == BACKLIGHT_FADING_PWM)
729 /* If backlight is already on, don't fade in. */
730 bl_dim_target = BL_PWM_COUNT;
731 bl_dim_fraction = (BL_PWM_COUNT<<16);
732 #endif
734 /* Leave all lights as set by the bootloader here. The settings load will
735 * call the appropriate backlight_set_*() functions, only changing light
736 * status if necessary. */
737 #ifdef BACKLIGHT_DRIVER_CLOSE
738 backlight_thread_id =
739 #endif
740 create_thread(backlight_thread, backlight_stack,
741 sizeof(backlight_stack), 0, backlight_thread_name
742 IF_PRIO(, PRIORITY_USER_INTERFACE)
743 IF_COP(, CPU));
746 #ifdef BACKLIGHT_DRIVER_CLOSE
747 void backlight_close(void)
749 unsigned int thread = backlight_thread_id;
751 /* Wait for thread to exit */
752 if (thread == 0)
753 return;
755 backlight_thread_id = 0;
757 queue_post(&backlight_queue, BACKLIGHT_QUIT, 0);
758 thread_wait(thread);
760 #endif /* BACKLIGHT_DRIVER_CLOSE */
762 void backlight_on(void)
764 queue_remove_from_head(&backlight_queue, BACKLIGHT_ON);
765 queue_post(&backlight_queue, BACKLIGHT_ON, 0);
768 void backlight_off(void)
770 queue_post(&backlight_queue, BACKLIGHT_OFF, 0);
773 /* returns true when the backlight is on,
774 * and optionally when it's set to always off. */
775 bool is_backlight_on(bool ignore_always_off)
777 int timeout = backlight_get_current_timeout();
778 return (backlight_timer > 0) /* countdown */
779 || (timeout == 0) /* always on */
780 || ((timeout < 0) && !ignore_always_off);
783 /* return value in ticks; 0 means always on, <0 means always off */
784 int backlight_get_current_timeout(void)
786 #ifdef HAS_BUTTON_HOLD
787 if ((backlight_on_button_hold != 0)
788 #ifdef HAVE_REMOTE_LCD_AS_MAIN
789 && remote_button_hold()
790 #else
791 && button_hold()
792 #endif
794 return (backlight_on_button_hold == 2) ? 0 : -1;
795 /* always on or always off */
796 else
797 #endif
798 #if CONFIG_CHARGING
799 if (power_input_present())
800 return backlight_timeout_plugged;
801 else
802 #endif
803 return backlight_timeout_normal;
806 void backlight_set_timeout(int value)
808 backlight_timeout_normal = HZ * value;
809 queue_post(&backlight_queue, BACKLIGHT_TMO_CHANGED, 0);
812 #if CONFIG_CHARGING
813 void backlight_set_timeout_plugged(int value)
815 backlight_timeout_plugged = HZ * value;
816 queue_post(&backlight_queue, BACKLIGHT_TMO_CHANGED, 0);
818 #endif /* CONFIG_CHARGING */
820 #ifdef HAS_BUTTON_HOLD
821 /* Hold button change event handler. */
822 void backlight_hold_changed(bool hold_button)
824 if (!hold_button || (backlight_on_button_hold > 0))
825 /* if unlocked or override in effect */
826 backlight_on();
829 void backlight_set_on_button_hold(int index)
831 if ((unsigned)index >= 3)
832 /* if given a weird value, use default */
833 index = 0;
835 backlight_on_button_hold = index;
836 queue_post(&backlight_queue, BACKLIGHT_TMO_CHANGED, 0);
838 #endif /* HAS_BUTTON_HOLD */
840 #ifdef HAVE_LCD_SLEEP_SETTING
841 void lcd_set_sleep_after_backlight_off(int index)
843 if ((unsigned)index >= sizeof(lcd_sleep_timeout_value))
844 /* if given a weird value, use default */
845 index = 3;
847 lcd_sleep_timeout = HZ * lcd_sleep_timeout_value[index];
849 if (is_backlight_on(true))
850 /* Timer will be set when bl turns off or bl set to on. */
851 return;
853 /* Backlight is Off */
854 if (lcd_sleep_timeout < 0)
855 lcd_sleep_timer = 1; /* Always - sleep next tick */
856 else
857 lcd_sleep_timer = lcd_sleep_timeout; /* Never, other */
859 #endif /* HAVE_LCD_SLEEP_SETTING */
861 #ifdef HAVE_REMOTE_LCD
862 void remote_backlight_on(void)
864 queue_post(&backlight_queue, REMOTE_BACKLIGHT_ON, 0);
867 void remote_backlight_off(void)
869 queue_post(&backlight_queue, REMOTE_BACKLIGHT_OFF, 0);
872 void remote_backlight_set_timeout(int value)
874 remote_backlight_timeout_normal = HZ * value;
875 queue_post(&backlight_queue, REMOTE_BACKLIGHT_TMO_CHANGED, 0);
878 #if CONFIG_CHARGING
879 void remote_backlight_set_timeout_plugged(int value)
881 remote_backlight_timeout_plugged = HZ * value;
882 queue_post(&backlight_queue, REMOTE_BACKLIGHT_TMO_CHANGED, 0);
884 #endif /* CONFIG_CHARGING */
886 #ifdef HAS_REMOTE_BUTTON_HOLD
887 /* Remote hold button change event handler. */
888 void remote_backlight_hold_changed(bool rc_hold_button)
890 if (!rc_hold_button || (remote_backlight_on_button_hold > 0))
891 /* if unlocked or override */
892 remote_backlight_on();
895 void remote_backlight_set_on_button_hold(int index)
897 if ((unsigned)index >= 3)
898 /* if given a weird value, use default */
899 index = 0;
901 remote_backlight_on_button_hold = index;
902 queue_post(&backlight_queue, REMOTE_BACKLIGHT_TMO_CHANGED, 0);
904 #endif /* HAS_REMOTE_BUTTON_HOLD */
906 /* return value in ticks; 0 means always on, <0 means always off */
907 int remote_backlight_get_current_timeout(void)
909 #ifdef HAS_REMOTE_BUTTON_HOLD
910 if (remote_button_hold() && (remote_backlight_on_button_hold != 0))
911 return (remote_backlight_on_button_hold == 2)
912 ? 0 : -1; /* always on or always off */
913 else
914 #endif
915 #if CONFIG_CHARGING
916 if (power_input_present())
917 return remote_backlight_timeout_plugged;
918 else
919 #endif
920 return remote_backlight_timeout_normal;
923 /* returns true when the backlight is on, and
924 * optionally when it's set to always off */
925 bool is_remote_backlight_on(bool ignore_always_off)
927 int timeout = remote_backlight_get_current_timeout();
928 return (remote_backlight_timer > 0) /* countdown */
929 || (timeout == 0) /* always on */
930 || ((timeout < 0) && !ignore_always_off);
933 #endif /* HAVE_REMOTE_LCD */
935 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
936 void backlight_set_brightness(int val)
938 if (val < MIN_BRIGHTNESS_SETTING)
939 val = MIN_BRIGHTNESS_SETTING;
940 else if (val > MAX_BRIGHTNESS_SETTING)
941 val = MAX_BRIGHTNESS_SETTING;
943 queue_post(&backlight_queue, BACKLIGHT_BRIGHTNESS_CHANGED, val);
945 #endif /* HAVE_BACKLIGHT_BRIGHTNESS */
947 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
948 void buttonlight_set_brightness(int val)
950 if (val < MIN_BRIGHTNESS_SETTING)
951 val = MIN_BRIGHTNESS_SETTING;
952 else if (val > MAX_BRIGHTNESS_SETTING)
953 val = MAX_BRIGHTNESS_SETTING;
955 queue_post(&backlight_queue, BUTTON_LIGHT_BRIGHTNESS_CHANGED, val);
957 #endif /* HAVE_BUTTONLIGHT_BRIGHTNESS */
959 #else /* !defined(HAVE_BACKLIGHT) || !defined(BACKLIGHT_FULL_INIT)
960 -- no backlight, empty dummy functions */
962 #if defined(HAVE_BACKLIGHT) && !defined(BACKLIGHT_FULL_INIT)
963 void backlight_init(void)
965 (void)_backlight_init();
966 _backlight_on();
968 #endif
970 void backlight_on(void) {}
971 void backlight_off(void) {}
972 void backlight_set_timeout(int value) {(void)value;}
974 bool is_backlight_on(bool ignore_always_off)
976 (void)ignore_always_off;
977 return true;
979 #ifdef HAVE_REMOTE_LCD
980 void remote_backlight_on(void) {}
981 void remote_backlight_off(void) {}
982 void remote_backlight_set_timeout(int value) {(void)value;}
984 bool is_remote_backlight_on(bool ignore_always_off)
986 (void)ignore_always_off;
987 return true;
989 #endif /* HAVE_REMOTE_LCD */
990 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
991 void backlight_set_brightness(int val) { (void)val; }
992 #endif
994 #ifdef HAVE_BUTTON_LIGHT
995 void buttonlight_on(void) {}
996 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
997 void buttonlight_set_brightness(int val) { (void)val; }
998 #endif
999 #endif /* HAVE_BUTTON_LIGHT */
1001 #endif /* defined(HAVE_BACKLIGHT) && defined(BACKLIGHT_FULL_INIT) */