Forgot this very minor tweak.
[kugel-rb.git] / firmware / backlight.c
blob4f33a6167f80b01b63909a0bfa2b804241859221
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;
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 case SYS_USB_DISCONNECTED:
581 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
582 break;
584 #ifdef BACKLIGHT_DRIVER_CLOSE
585 /* Get out of here */
586 case BACKLIGHT_QUIT:
587 return;
588 #endif
590 if (locked)
591 continue;
593 switch(ev.id)
594 { /* These events are only processed if backlight isn't locked */
595 #ifdef HAVE_REMOTE_LCD
596 case REMOTE_BACKLIGHT_TMO_CHANGED:
597 case REMOTE_BACKLIGHT_ON:
598 remote_backlight_update_state();
599 break;
601 case REMOTE_BACKLIGHT_OFF:
602 remote_backlight_timer = 0; /* Disable the timeout */
603 _remote_backlight_off();
604 break;
605 #endif /* HAVE_REMOTE_LCD */
607 case BACKLIGHT_TMO_CHANGED:
608 case BACKLIGHT_ON:
609 backlight_update_state();
610 break;
612 case BACKLIGHT_OFF:
613 do_backlight_off();
614 break;
615 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
616 case BACKLIGHT_BRIGHTNESS_CHANGED:
617 backlight_brightness = (int)ev.data;
618 _backlight_set_brightness((int)ev.data);
619 #if (CONFIG_BACKLIGHT_FADING == BACKLIGHT_FADING_SW_SETTING) \
620 || (CONFIG_BACKLIGHT_FADING == BACKLIGHT_FADING_SW_HW_REG)
621 /* receive backlight brightness */
622 _backlight_fade_update_state((int)ev.data);
623 #endif
624 break;
625 #endif /* HAVE_BACKLIGHT_BRIGHTNESS */
626 #ifdef HAVE_LCD_SLEEP
627 case LCD_SLEEP:
628 lcd_sleep();
629 break;
630 #endif
631 #ifdef HAVE_BUTTON_LIGHT
632 case BUTTON_LIGHT_TMO_CHANGED:
633 case BUTTON_LIGHT_ON:
634 buttonlight_update_state();
635 break;
637 case BUTTON_LIGHT_OFF:
638 buttonlight_timer = 0;
639 _buttonlight_off();
640 break;
641 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
642 case BUTTON_LIGHT_BRIGHTNESS_CHANGED:
643 _buttonlight_set_brightness((int)ev.data);
644 break;
645 #endif /* HAVE_BUTTONLIGHT_BRIGHTNESS */
646 #endif /* HAVE_BUTTON_LIGHT */
648 case SYS_POWEROFF: /* Lock backlight on poweroff so it doesn't */
649 locked = true; /* go off before power is actually cut. */
650 /* fall through */
651 #if CONFIG_CHARGING
652 case SYS_CHARGER_CONNECTED:
653 case SYS_CHARGER_DISCONNECTED:
654 #endif
655 backlight_update_state();
656 #ifdef HAVE_REMOTE_LCD
657 remote_backlight_update_state();
658 #endif
659 break;
660 case SYS_TIMEOUT:
661 #if (CONFIG_BACKLIGHT_FADING == BACKLIGHT_FADING_SW_SETTING) \
662 || (CONFIG_BACKLIGHT_FADING == BACKLIGHT_FADING_SW_HW_REG)
663 if (backlight_fading_state != NOT_FADING)
665 if ((_backlight_fade_step(backlight_fading_state)))
666 { /* finished fading */
667 #ifdef HAVE_LCD_SLEEP
668 if (backlight_fading_state == FADING_DOWN)
669 { /* start sleep countdown */
670 backlight_lcd_sleep_countdown(true);
672 #endif
673 backlight_fading_state = NOT_FADING;
676 else
677 #endif /* CONFIG_BACKLIGHT_FADING */
678 backlight_timeout_handler();
679 break;
681 } /* end while */
684 static void backlight_timeout_handler(void)
686 if(backlight_timer > 0)
688 backlight_timer -= BACKLIGHT_THREAD_TIMEOUT;
689 if(backlight_timer <= 0)
691 do_backlight_off();
694 #ifdef HAVE_LCD_SLEEP
695 else if(lcd_sleep_timer > 0)
697 lcd_sleep_timer -= BACKLIGHT_THREAD_TIMEOUT;
698 if(lcd_sleep_timer <= 0)
700 lcd_sleep();
703 #endif /* HAVE_LCD_SLEEP */
704 #ifdef HAVE_REMOTE_LCD
705 if(remote_backlight_timer > 0)
707 remote_backlight_timer -= BACKLIGHT_THREAD_TIMEOUT;
708 if(remote_backlight_timer <= 0)
710 _remote_backlight_off();
713 #endif /* HAVE_REMOVE_LCD */
714 #ifdef HAVE_BUTTON_LIGHT
715 if (buttonlight_timer > 0)
717 buttonlight_timer -= BACKLIGHT_THREAD_TIMEOUT;
718 if (buttonlight_timer <= 0)
720 _buttonlight_off();
723 #endif /* HAVE_BUTTON_LIGHT */
726 void backlight_init(void)
728 queue_init(&backlight_queue, true);
730 if (_backlight_init())
732 #if (CONFIG_BACKLIGHT_FADING == BACKLIGHT_FADING_PWM)
733 /* If backlight is already on, don't fade in. */
734 bl_dim_target = BL_PWM_COUNT;
735 bl_dim_fraction = (BL_PWM_COUNT<<16);
736 #endif
738 /* Leave all lights as set by the bootloader here. The settings load will
739 * call the appropriate backlight_set_*() functions, only changing light
740 * status if necessary. */
741 #ifdef BACKLIGHT_DRIVER_CLOSE
742 backlight_thread_id =
743 #endif
744 create_thread(backlight_thread, backlight_stack,
745 sizeof(backlight_stack), 0, backlight_thread_name
746 IF_PRIO(, PRIORITY_USER_INTERFACE)
747 IF_COP(, CPU));
750 #ifdef BACKLIGHT_DRIVER_CLOSE
751 void backlight_close(void)
753 unsigned int thread = backlight_thread_id;
755 /* Wait for thread to exit */
756 if (thread == 0)
757 return;
759 backlight_thread_id = 0;
761 queue_post(&backlight_queue, BACKLIGHT_QUIT, 0);
762 thread_wait(thread);
764 #endif /* BACKLIGHT_DRIVER_CLOSE */
766 void backlight_on(void)
768 queue_remove_from_head(&backlight_queue, BACKLIGHT_ON);
769 queue_post(&backlight_queue, BACKLIGHT_ON, 0);
772 void backlight_off(void)
774 queue_post(&backlight_queue, BACKLIGHT_OFF, 0);
777 /* returns true when the backlight is on,
778 * and optionally when it's set to always off. */
779 bool is_backlight_on(bool ignore_always_off)
781 int timeout = backlight_get_current_timeout();
782 return (backlight_timer > 0) /* countdown */
783 || (timeout == 0) /* always on */
784 || ((timeout < 0) && !ignore_always_off);
787 /* return value in ticks; 0 means always on, <0 means always off */
788 int backlight_get_current_timeout(void)
790 #ifdef HAS_BUTTON_HOLD
791 if ((backlight_on_button_hold != 0)
792 #ifdef HAVE_REMOTE_LCD_AS_MAIN
793 && remote_button_hold()
794 #else
795 && button_hold()
796 #endif
798 return (backlight_on_button_hold == 2) ? 0 : -1;
799 /* always on or always off */
800 else
801 #endif
802 #if CONFIG_CHARGING
803 if (power_input_present())
804 return backlight_timeout_plugged;
805 else
806 #endif
807 return backlight_timeout_normal;
810 void backlight_set_timeout(int value)
812 backlight_timeout_normal = HZ * value;
813 queue_post(&backlight_queue, BACKLIGHT_TMO_CHANGED, 0);
816 #if CONFIG_CHARGING
817 void backlight_set_timeout_plugged(int value)
819 backlight_timeout_plugged = HZ * value;
820 queue_post(&backlight_queue, BACKLIGHT_TMO_CHANGED, 0);
822 #endif /* CONFIG_CHARGING */
824 #ifdef HAS_BUTTON_HOLD
825 /* Hold button change event handler. */
826 void backlight_hold_changed(bool hold_button)
828 if (!hold_button || (backlight_on_button_hold > 0))
829 /* if unlocked or override in effect */
830 backlight_on();
833 void backlight_set_on_button_hold(int index)
835 if ((unsigned)index >= 3)
836 /* if given a weird value, use default */
837 index = 0;
839 backlight_on_button_hold = index;
840 queue_post(&backlight_queue, BACKLIGHT_TMO_CHANGED, 0);
842 #endif /* HAS_BUTTON_HOLD */
844 #ifdef HAVE_LCD_SLEEP_SETTING
845 void lcd_set_sleep_after_backlight_off(int index)
847 if ((unsigned)index >= sizeof(lcd_sleep_timeout_value))
848 /* if given a weird value, use default */
849 index = 3;
851 lcd_sleep_timeout = HZ * lcd_sleep_timeout_value[index];
853 if (is_backlight_on(true))
854 /* Timer will be set when bl turns off or bl set to on. */
855 return;
857 /* Backlight is Off */
858 if (lcd_sleep_timeout < 0)
859 lcd_sleep_timer = 1; /* Always - sleep next tick */
860 else
861 lcd_sleep_timer = lcd_sleep_timeout; /* Never, other */
863 #endif /* HAVE_LCD_SLEEP_SETTING */
865 #ifdef HAVE_REMOTE_LCD
866 void remote_backlight_on(void)
868 queue_post(&backlight_queue, REMOTE_BACKLIGHT_ON, 0);
871 void remote_backlight_off(void)
873 queue_post(&backlight_queue, REMOTE_BACKLIGHT_OFF, 0);
876 void remote_backlight_set_timeout(int value)
878 remote_backlight_timeout_normal = HZ * value;
879 queue_post(&backlight_queue, REMOTE_BACKLIGHT_TMO_CHANGED, 0);
882 #if CONFIG_CHARGING
883 void remote_backlight_set_timeout_plugged(int value)
885 remote_backlight_timeout_plugged = HZ * value;
886 queue_post(&backlight_queue, REMOTE_BACKLIGHT_TMO_CHANGED, 0);
888 #endif /* CONFIG_CHARGING */
890 #ifdef HAS_REMOTE_BUTTON_HOLD
891 /* Remote hold button change event handler. */
892 void remote_backlight_hold_changed(bool rc_hold_button)
894 if (!rc_hold_button || (remote_backlight_on_button_hold > 0))
895 /* if unlocked or override */
896 remote_backlight_on();
899 void remote_backlight_set_on_button_hold(int index)
901 if ((unsigned)index >= 3)
902 /* if given a weird value, use default */
903 index = 0;
905 remote_backlight_on_button_hold = index;
906 queue_post(&backlight_queue, REMOTE_BACKLIGHT_TMO_CHANGED, 0);
908 #endif /* HAS_REMOTE_BUTTON_HOLD */
910 /* return value in ticks; 0 means always on, <0 means always off */
911 int remote_backlight_get_current_timeout(void)
913 #ifdef HAS_REMOTE_BUTTON_HOLD
914 if (remote_button_hold() && (remote_backlight_on_button_hold != 0))
915 return (remote_backlight_on_button_hold == 2)
916 ? 0 : -1; /* always on or always off */
917 else
918 #endif
919 #if CONFIG_CHARGING
920 if (power_input_present())
921 return remote_backlight_timeout_plugged;
922 else
923 #endif
924 return remote_backlight_timeout_normal;
927 /* returns true when the backlight is on, and
928 * optionally when it's set to always off */
929 bool is_remote_backlight_on(bool ignore_always_off)
931 int timeout = remote_backlight_get_current_timeout();
932 return (remote_backlight_timer > 0) /* countdown */
933 || (timeout == 0) /* always on */
934 || ((timeout < 0) && !ignore_always_off);
937 #endif /* HAVE_REMOTE_LCD */
939 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
940 void backlight_set_brightness(int val)
942 if (val < MIN_BRIGHTNESS_SETTING)
943 val = MIN_BRIGHTNESS_SETTING;
944 else if (val > MAX_BRIGHTNESS_SETTING)
945 val = MAX_BRIGHTNESS_SETTING;
947 queue_post(&backlight_queue, BACKLIGHT_BRIGHTNESS_CHANGED, val);
949 #endif /* HAVE_BACKLIGHT_BRIGHTNESS */
951 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
952 void buttonlight_set_brightness(int val)
954 if (val < MIN_BRIGHTNESS_SETTING)
955 val = MIN_BRIGHTNESS_SETTING;
956 else if (val > MAX_BRIGHTNESS_SETTING)
957 val = MAX_BRIGHTNESS_SETTING;
959 queue_post(&backlight_queue, BUTTON_LIGHT_BRIGHTNESS_CHANGED, val);
961 #endif /* HAVE_BUTTONLIGHT_BRIGHTNESS */
963 #else /* !defined(HAVE_BACKLIGHT) || !defined(BACKLIGHT_FULL_INIT)
964 -- no backlight, empty dummy functions */
966 #if defined(HAVE_BACKLIGHT) && !defined(BACKLIGHT_FULL_INIT)
967 void backlight_init(void)
969 (void)_backlight_init();
970 _backlight_on();
972 #endif
974 void backlight_on(void) {}
975 void backlight_off(void) {}
976 void backlight_set_timeout(int value) {(void)value;}
978 bool is_backlight_on(bool ignore_always_off)
980 (void)ignore_always_off;
981 return true;
983 #ifdef HAVE_REMOTE_LCD
984 void remote_backlight_on(void) {}
985 void remote_backlight_off(void) {}
986 void remote_backlight_set_timeout(int value) {(void)value;}
988 bool is_remote_backlight_on(bool ignore_always_off)
990 (void)ignore_always_off;
991 return true;
993 #endif /* HAVE_REMOTE_LCD */
994 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
995 void backlight_set_brightness(int val) { (void)val; }
996 #endif
998 #ifdef HAVE_BUTTON_LIGHT
999 void buttonlight_on(void) {}
1000 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
1001 void buttonlight_set_brightness(int val) { (void)val; }
1002 #endif
1003 #endif /* HAVE_BUTTON_LIGHT */
1005 #endif /* defined(HAVE_BACKLIGHT) && defined(BACKLIGHT_FULL_INIT) */