Merge the "Replaygain Off" option into the replaygain type; eliminate the "On/Off...
[kugel-rb/myfork.git] / firmware / backlight.c
blob12acca00a7b75aeb05e16de2bb62e95357c1c671
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
43 #ifndef SIMULATOR
44 #include "backlight-target.h"
45 #else
46 #include "backlight-sim.h"
47 #endif
49 #if (CONFIG_BACKLIGHT_FADING == BACKLIGHT_FADING_SW_SETTING) \
50 || (CONFIG_BACKLIGHT_FADING == BACKLIGHT_FADING_SW_HW_REG)
51 #include "backlight-sw-fading.h"
52 #endif
54 #if defined(HAVE_BACKLIGHT) && defined(BACKLIGHT_FULL_INIT)
56 #define BACKLIGHT_FADE_IN_THREAD \
57 (CONFIG_BACKLIGHT_FADING & (BACKLIGHT_FADING_SW_SETTING \
58 |BACKLIGHT_FADING_SW_HW_REG \
59 |BACKLIGHT_FADING_PWM) )
61 #define BACKLIGHT_THREAD_TIMEOUT HZ
63 enum {
64 BACKLIGHT_ON,
65 BACKLIGHT_OFF,
66 BACKLIGHT_TMO_CHANGED,
67 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
68 BACKLIGHT_BRIGHTNESS_CHANGED,
69 #endif
70 #ifdef HAVE_REMOTE_LCD
71 REMOTE_BACKLIGHT_ON,
72 REMOTE_BACKLIGHT_OFF,
73 REMOTE_BACKLIGHT_TMO_CHANGED,
74 #endif
75 #if defined(_BACKLIGHT_FADE_BOOST) || defined(_BACKLIGHT_FADE_ENABLE)
76 BACKLIGHT_FADE_FINISH,
77 #endif
78 #ifdef HAVE_LCD_SLEEP
79 LCD_SLEEP,
80 #endif
81 #ifdef HAVE_BUTTON_LIGHT
82 BUTTON_LIGHT_ON,
83 BUTTON_LIGHT_OFF,
84 BUTTON_LIGHT_TMO_CHANGED,
85 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
86 BUTTON_LIGHT_BRIGHTNESS_CHANGED,
87 #endif
88 #endif /* HAVE_BUTTON_LIGHT */
89 #ifdef BACKLIGHT_DRIVER_CLOSE
90 BACKLIGHT_QUIT,
91 #endif
94 static void backlight_thread(void);
95 static long backlight_stack[DEFAULT_STACK_SIZE/sizeof(long)];
96 static const char backlight_thread_name[] = "backlight";
97 static struct event_queue backlight_queue;
98 #ifdef BACKLIGHT_DRIVER_CLOSE
99 static unsigned int backlight_thread_id = 0;
100 #endif
102 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
103 int backlight_brightness = DEFAULT_BRIGHTNESS_SETTING;
104 #endif
105 static int backlight_timer SHAREDBSS_ATTR;
106 static int backlight_timeout_normal = 5*HZ;
107 #if CONFIG_CHARGING
108 static int backlight_timeout_plugged = 5*HZ;
109 #endif
110 #ifdef HAS_BUTTON_HOLD
111 static int backlight_on_button_hold = 0;
112 #endif
113 static void backlight_timeout_handler(void);
115 #ifdef HAVE_BUTTON_LIGHT
116 static int buttonlight_timer;
117 static int buttonlight_timeout = 5*HZ;
119 /* Update state of buttonlight according to timeout setting */
120 static void buttonlight_update_state(void)
122 buttonlight_timer = buttonlight_timeout;
124 /* Buttonlight == OFF in the setting? */
125 if (buttonlight_timer < 0)
127 buttonlight_timer = 0; /* Disable the timeout */
128 _buttonlight_off();
130 else
131 _buttonlight_on();
134 /* external interface */
135 void buttonlight_on(void)
137 queue_remove_from_head(&backlight_queue, BUTTON_LIGHT_ON);
138 queue_post(&backlight_queue, BUTTON_LIGHT_ON, 0);
141 void buttonlight_off(void)
143 queue_post(&backlight_queue, BUTTON_LIGHT_OFF, 0);
146 void buttonlight_set_timeout(int value)
148 buttonlight_timeout = HZ * value;
149 queue_post(&backlight_queue, BUTTON_LIGHT_TMO_CHANGED, 0);
152 int buttonlight_get_current_timeout(void)
154 return buttonlight_timeout;
157 #endif /* HAVE_BUTTON_LIGHT */
159 #ifdef HAVE_REMOTE_LCD
160 static int remote_backlight_timer;
161 static int remote_backlight_timeout_normal = 5*HZ;
162 #if CONFIG_CHARGING
163 static int remote_backlight_timeout_plugged = 5*HZ;
164 #endif
165 #ifdef HAS_REMOTE_BUTTON_HOLD
166 static int remote_backlight_on_button_hold = 0;
167 #endif
168 #endif /* HAVE_REMOTE_LCD */
170 #ifdef HAVE_LCD_SLEEP
171 #ifdef HAVE_LCD_SLEEP_SETTING
172 const signed char lcd_sleep_timeout_value[10] =
174 -1, 0, 5, 10, 15, 20, 30, 45, 60, 90
176 static int lcd_sleep_timeout = 10*HZ;
177 #else
178 /* Target defines needed value */
179 #define lcd_sleep_timeout LCD_SLEEP_TIMEOUT
180 #endif
182 static int lcd_sleep_timer SHAREDDATA_ATTR = 0;
184 static void backlight_lcd_sleep_countdown(bool start)
186 if (!start)
188 /* Cancel the LCD sleep countdown */
189 lcd_sleep_timer = 0;
190 return;
193 /* Start LCD sleep countdown */
194 if (lcd_sleep_timeout < 0)
196 lcd_sleep_timer = 0; /* Setting == Always */
197 /* Ensure lcd_sleep() is called from backlight_thread() */
198 #if (CONFIG_BACKLIGHT_FADING == BACKLIGHT_FADING_PWM)
199 queue_post(&backlight_queue, LCD_SLEEP, 0);
200 #else
201 lcd_sleep();
202 #endif
204 else
206 lcd_sleep_timer = lcd_sleep_timeout;
209 #endif /* HAVE_LCD_SLEEP */
211 #if (CONFIG_BACKLIGHT_FADING == BACKLIGHT_FADING_SW_SETTING) \
212 || (CONFIG_BACKLIGHT_FADING == BACKLIGHT_FADING_SW_HW_REG)
213 static int backlight_fading_type = (FADING_UP|FADING_DOWN);
214 static int backlight_fading_state = NOT_FADING;
215 #endif
218 /* backlight fading */
219 #if (CONFIG_BACKLIGHT_FADING == BACKLIGHT_FADING_PWM)
220 #define BL_PWM_INTERVAL 5 /* Cycle interval in ms */
221 #define BL_PWM_BITS 8
222 #define BL_PWM_COUNT (1<<BL_PWM_BITS)
224 /* s15.16 fixed point variables */
225 static int32_t bl_fade_in_step = ((BL_PWM_INTERVAL*BL_PWM_COUNT)<<16)/300;
226 static int32_t bl_fade_out_step = ((BL_PWM_INTERVAL*BL_PWM_COUNT)<<16)/2000;
227 static int32_t bl_dim_fraction = 0;
229 static int bl_dim_target = 0;
230 static int bl_dim_current = 0;
231 static enum {DIM_STATE_START, DIM_STATE_MAIN} bl_dim_state = DIM_STATE_START;
232 static bool bl_timer_active = false;
234 static void backlight_isr(void)
236 int timer_period = (TIMER_FREQ*BL_PWM_INTERVAL/1000);
237 bool idle = false;
239 switch (bl_dim_state)
241 /* New cycle */
242 case DIM_STATE_START:
243 bl_dim_current = bl_dim_fraction >> 16;
245 if (bl_dim_current > 0 && bl_dim_current < BL_PWM_COUNT)
247 _backlight_on_isr();
248 timer_period = (timer_period * bl_dim_current) >> BL_PWM_BITS;
249 bl_dim_state = DIM_STATE_MAIN;
251 else
253 if (bl_dim_current)
254 _backlight_on_isr();
255 else
256 _backlight_off_isr();
257 if (bl_dim_current == bl_dim_target)
258 idle = true;
260 if (bl_dim_current < bl_dim_target)
262 bl_dim_fraction = MIN(bl_dim_fraction + bl_fade_in_step,
263 (BL_PWM_COUNT<<16));
265 else if (bl_dim_current > bl_dim_target)
267 bl_dim_fraction = MAX(bl_dim_fraction - bl_fade_out_step, 0);
269 break;
271 /* Dim main screen */
272 case DIM_STATE_MAIN:
273 _backlight_off_isr();
274 timer_period = (timer_period * (BL_PWM_COUNT - bl_dim_current))
275 >> BL_PWM_BITS;
276 bl_dim_state = DIM_STATE_START;
277 break ;
279 if (idle)
281 #if defined(_BACKLIGHT_FADE_BOOST) || defined(_BACKLIGHT_FADE_ENABLE)
282 queue_post(&backlight_queue, BACKLIGHT_FADE_FINISH, 0);
283 #endif
284 timer_unregister();
285 bl_timer_active = false;
287 #ifdef HAVE_LCD_SLEEP
288 if (bl_dim_current == 0)
289 backlight_lcd_sleep_countdown(true);
290 #endif
292 else
293 timer_set_period(timer_period);
296 static void backlight_switch(void)
298 if (bl_dim_target > (BL_PWM_COUNT/2))
300 _backlight_on_normal();
301 bl_dim_fraction = (BL_PWM_COUNT<<16);
303 else
305 _backlight_off_normal();
306 bl_dim_fraction = 0;
308 #ifdef HAVE_LCD_SLEEP
309 backlight_lcd_sleep_countdown(true);
310 #endif
314 static void backlight_release_timer(void)
316 #ifdef _BACKLIGHT_FADE_BOOST
317 cpu_boost(false);
318 #endif
319 timer_unregister();
320 bl_timer_active = false;
321 backlight_switch();
324 static void backlight_dim(int value)
326 /* protect from extraneous calls with the same target value */
327 if (value == bl_dim_target)
328 return;
330 bl_dim_target = value;
332 if (bl_timer_active)
333 return ;
335 if (timer_register(0, backlight_release_timer, 2, 0, backlight_isr
336 IF_COP(, CPU)))
338 #ifdef _BACKLIGHT_FADE_BOOST
339 /* Prevent cpu frequency changes while dimming. */
340 cpu_boost(true);
341 #endif
342 bl_timer_active = true;
344 else
345 backlight_switch();
348 static void backlight_setup_fade_up(void)
350 if (bl_fade_in_step > 0)
352 #ifdef _BACKLIGHT_FADE_ENABLE
353 _backlight_hw_enable(true);
354 #endif
355 backlight_dim(BL_PWM_COUNT);
357 else
359 bl_dim_target = BL_PWM_COUNT;
360 bl_dim_fraction = (BL_PWM_COUNT<<16);
361 _backlight_on_normal();
365 static void backlight_setup_fade_down(void)
367 if (bl_fade_out_step > 0)
369 backlight_dim(0);
371 else
373 bl_dim_target = bl_dim_fraction = 0;
374 _backlight_off_normal();
375 #ifdef HAVE_LCD_SLEEP
376 backlight_lcd_sleep_countdown(true);
377 #endif
381 void backlight_set_fade_in(int value)
383 if (value > 0)
384 bl_fade_in_step = ((BL_PWM_INTERVAL*BL_PWM_COUNT)<<16) / value;
385 else
386 bl_fade_in_step = 0;
389 void backlight_set_fade_out(int value)
391 if (value > 0)
392 bl_fade_out_step = ((BL_PWM_INTERVAL*BL_PWM_COUNT)<<16) / value;
393 else
394 bl_fade_out_step = 0;
397 #elif (CONFIG_BACKLIGHT_FADING == BACKLIGHT_FADING_SW_SETTING) \
398 || (CONFIG_BACKLIGHT_FADING == BACKLIGHT_FADING_SW_HW_REG)
400 void backlight_set_fade_out(bool value)
402 if(value) /* on */
403 backlight_fading_type |= FADING_DOWN;
404 else
405 backlight_fading_type &= FADING_UP;
408 void backlight_set_fade_in(bool value)
410 if(value) /* on */
411 backlight_fading_type |= FADING_UP;
412 else
413 backlight_fading_type &= FADING_DOWN;
416 static void backlight_setup_fade_up(void)
418 if (backlight_fading_type & FADING_UP)
420 if (backlight_fading_state == NOT_FADING)
422 /* make sure the backlight is at lowest level */
423 _backlight_on();
425 backlight_fading_state = FADING_UP;
427 else
429 backlight_fading_state = NOT_FADING;
430 _backlight_fade_update_state(backlight_brightness);
431 _backlight_on();
432 _backlight_set_brightness(backlight_brightness);
436 static void backlight_setup_fade_down(void)
438 if (backlight_fading_type & FADING_DOWN)
440 backlight_fading_state = FADING_DOWN;
442 else
444 backlight_fading_state = NOT_FADING;
445 _backlight_fade_update_state(MIN_BRIGHTNESS_SETTING-1);
446 _backlight_off();
447 #if (CONFIG_BACKLIGHT_FADING == BACKLIGHT_FADING_SW_HW_REG)
448 /* write the lowest brightness level to the hardware so that
449 * fading up is glitch free */
450 _backlight_set_brightness(MIN_BRIGHTNESS_SETTING);
451 #endif
452 #ifdef HAVE_LCD_SLEEP
453 backlight_lcd_sleep_countdown(true);
454 #endif
457 #endif /* CONFIG_BACKLIGHT_FADING */
459 static inline void do_backlight_off(void)
461 backlight_timer = 0;
462 #if BACKLIGHT_FADE_IN_THREAD
463 backlight_setup_fade_down();
464 #else
465 _backlight_off();
466 /* targets that have fading need to start the countdown when done with
467 * fading */
468 #ifdef HAVE_LCD_SLEEP
469 backlight_lcd_sleep_countdown(true);
470 #endif
471 #endif
474 /* Update state of backlight according to timeout setting */
475 static void backlight_update_state(void)
478 int timeout = backlight_get_current_timeout();
480 /* Backlight == OFF in the setting? */
481 if (UNLIKELY(timeout < 0))
483 do_backlight_off();
484 #if (CONFIG_BACKLIGHT_FADING == BACKLIGHT_FADING_SW_SETTING) \
485 || (CONFIG_BACKLIGHT_FADING == BACKLIGHT_FADING_SW_HW_REG)
486 /* necessary step to issue fading down when the setting is selected */
487 queue_post(&backlight_queue, SYS_TIMEOUT, 0);
488 #endif
490 else
492 backlight_timer = timeout;
494 #ifdef HAVE_LCD_SLEEP
495 backlight_lcd_sleep_countdown(false); /* wake up lcd */
496 #endif
498 #if BACKLIGHT_FADE_IN_THREAD
499 backlight_setup_fade_up();
500 #else
501 _backlight_on();
502 #endif
506 #ifdef HAVE_REMOTE_LCD
507 /* Update state of remote backlight according to timeout setting */
508 static void remote_backlight_update_state(void)
510 int timeout = remote_backlight_get_current_timeout();
511 /* Backlight == OFF in the setting? */
512 if (timeout < 0)
514 remote_backlight_timer = 0; /* Disable the timeout */
515 _remote_backlight_off();
517 else
519 remote_backlight_timer = timeout;
520 _remote_backlight_on();
523 #endif /* HAVE_REMOTE_LCD */
525 void backlight_thread(void)
527 struct queue_event ev;
528 bool locked = false;
530 while(1)
532 #if (CONFIG_BACKLIGHT_FADING == BACKLIGHT_FADING_SW_SETTING) \
533 || (CONFIG_BACKLIGHT_FADING == BACKLIGHT_FADING_SW_HW_REG)
534 if (backlight_fading_state != NOT_FADING)
535 queue_wait_w_tmo(&backlight_queue, &ev, FADE_DELAY);
536 else
537 #endif
538 queue_wait_w_tmo(&backlight_queue, &ev, BACKLIGHT_THREAD_TIMEOUT);
539 switch(ev.id)
540 { /* These events must always be processed */
541 #ifdef _BACKLIGHT_FADE_BOOST
542 case BACKLIGHT_FADE_FINISH:
543 cpu_boost(false);
544 break;
545 #endif
546 #ifdef _BACKLIGHT_FADE_ENABLE
547 case BACKLIGHT_FADE_FINISH:
548 _backlight_hw_enable((bl_dim_current|bl_dim_target) != 0);
549 break;
550 #endif
552 #ifndef SIMULATOR
553 /* Here for now or else the aggressive init messes up scrolling */
554 #ifdef HAVE_REMOTE_LCD
555 case SYS_REMOTE_PLUGGED:
556 lcd_remote_on();
557 lcd_remote_update();
558 break;
560 case SYS_REMOTE_UNPLUGGED:
561 lcd_remote_off();
562 break;
563 #elif defined HAVE_REMOTE_LCD_AS_MAIN
564 case SYS_REMOTE_PLUGGED:
565 lcd_on();
566 lcd_update();
567 break;
569 case SYS_REMOTE_UNPLUGGED:
570 lcd_off();
571 break;
572 #endif /* HAVE_REMOTE_LCD/ HAVE_REMOTE_LCD_AS_MAIN */
573 #endif /* !SIMULATOR */
574 case SYS_USB_CONNECTED:
575 usb_acknowledge(SYS_USB_CONNECTED_ACK);
576 break;
578 case SYS_USB_DISCONNECTED:
579 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
580 break;
582 #ifdef BACKLIGHT_DRIVER_CLOSE
583 /* Get out of here */
584 case BACKLIGHT_QUIT:
585 return;
586 #endif
588 if (locked)
589 continue;
591 switch(ev.id)
592 { /* These events are only processed if backlight isn't locked */
593 #ifdef HAVE_REMOTE_LCD
594 case REMOTE_BACKLIGHT_TMO_CHANGED:
595 case REMOTE_BACKLIGHT_ON:
596 remote_backlight_update_state();
597 break;
599 case REMOTE_BACKLIGHT_OFF:
600 remote_backlight_timer = 0; /* Disable the timeout */
601 _remote_backlight_off();
602 break;
603 #endif /* HAVE_REMOTE_LCD */
605 case BACKLIGHT_TMO_CHANGED:
606 case BACKLIGHT_ON:
607 backlight_update_state();
608 break;
610 case BACKLIGHT_OFF:
611 do_backlight_off();
612 break;
613 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
614 case BACKLIGHT_BRIGHTNESS_CHANGED:
615 backlight_brightness = (int)ev.data;
616 _backlight_set_brightness((int)ev.data);
617 #if (CONFIG_BACKLIGHT_FADING == BACKLIGHT_FADING_SW_SETTING) \
618 || (CONFIG_BACKLIGHT_FADING == BACKLIGHT_FADING_SW_HW_REG)
619 /* receive backlight brightness */
620 _backlight_fade_update_state((int)ev.data);
621 #endif
622 break;
623 #endif /* HAVE_BACKLIGHT_BRIGHTNESS */
624 #ifdef HAVE_LCD_SLEEP
625 case LCD_SLEEP:
626 lcd_sleep();
627 break;
628 #endif
629 #ifdef HAVE_BUTTON_LIGHT
630 case BUTTON_LIGHT_TMO_CHANGED:
631 case BUTTON_LIGHT_ON:
632 buttonlight_update_state();
633 break;
635 case BUTTON_LIGHT_OFF:
636 buttonlight_timer = 0;
637 _buttonlight_off();
638 break;
639 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
640 case BUTTON_LIGHT_BRIGHTNESS_CHANGED:
641 _buttonlight_set_brightness((int)ev.data);
642 break;
643 #endif /* HAVE_BUTTONLIGHT_BRIGHTNESS */
644 #endif /* HAVE_BUTTON_LIGHT */
646 case SYS_POWEROFF: /* Lock backlight on poweroff so it doesn't */
647 locked = true; /* go off before power is actually cut. */
648 /* fall through */
649 #if CONFIG_CHARGING
650 case SYS_CHARGER_CONNECTED:
651 case SYS_CHARGER_DISCONNECTED:
652 #endif
653 backlight_update_state();
654 #ifdef HAVE_REMOTE_LCD
655 remote_backlight_update_state();
656 #endif
657 break;
658 case SYS_TIMEOUT:
659 #if (CONFIG_BACKLIGHT_FADING == BACKLIGHT_FADING_SW_SETTING) \
660 || (CONFIG_BACKLIGHT_FADING == BACKLIGHT_FADING_SW_HW_REG)
661 if (backlight_fading_state != NOT_FADING)
663 if ((_backlight_fade_step(backlight_fading_state)))
664 { /* finished fading */
665 #ifdef HAVE_LCD_SLEEP
666 if (backlight_fading_state == FADING_DOWN)
667 { /* start sleep countdown */
668 backlight_lcd_sleep_countdown(true);
670 #endif
671 backlight_fading_state = NOT_FADING;
674 else
675 #endif /* CONFIG_BACKLIGHT_FADING */
676 backlight_timeout_handler();
677 break;
679 } /* end while */
682 static void backlight_timeout_handler(void)
684 if(backlight_timer > 0)
686 backlight_timer -= BACKLIGHT_THREAD_TIMEOUT;
687 if(backlight_timer <= 0)
689 do_backlight_off();
692 #ifdef HAVE_LCD_SLEEP
693 else if(lcd_sleep_timer > 0)
695 lcd_sleep_timer -= BACKLIGHT_THREAD_TIMEOUT;
696 if(lcd_sleep_timer <= 0)
698 lcd_sleep();
701 #endif /* HAVE_LCD_SLEEP */
702 #ifdef HAVE_REMOTE_LCD
703 if(remote_backlight_timer > 0)
705 remote_backlight_timer -= BACKLIGHT_THREAD_TIMEOUT;
706 if(remote_backlight_timer <= 0)
708 _remote_backlight_off();
711 #endif /* HAVE_REMOVE_LCD */
712 #ifdef HAVE_BUTTON_LIGHT
713 if (buttonlight_timer > 0)
715 buttonlight_timer -= BACKLIGHT_THREAD_TIMEOUT;
716 if (buttonlight_timer <= 0)
718 _buttonlight_off();
721 #endif /* HAVE_BUTTON_LIGHT */
724 void backlight_init(void)
726 queue_init(&backlight_queue, true);
728 if (_backlight_init())
730 #if (CONFIG_BACKLIGHT_FADING == BACKLIGHT_FADING_PWM)
731 /* If backlight is already on, don't fade in. */
732 bl_dim_target = BL_PWM_COUNT;
733 bl_dim_fraction = (BL_PWM_COUNT<<16);
734 #endif
736 /* Leave all lights as set by the bootloader here. The settings load will
737 * call the appropriate backlight_set_*() functions, only changing light
738 * status if necessary. */
739 #ifdef BACKLIGHT_DRIVER_CLOSE
740 backlight_thread_id =
741 #endif
742 create_thread(backlight_thread, backlight_stack,
743 sizeof(backlight_stack), 0, backlight_thread_name
744 IF_PRIO(, PRIORITY_USER_INTERFACE)
745 IF_COP(, CPU));
748 #ifdef BACKLIGHT_DRIVER_CLOSE
749 void backlight_close(void)
751 unsigned int thread = backlight_thread_id;
753 /* Wait for thread to exit */
754 if (thread == 0)
755 return;
757 backlight_thread_id = 0;
759 queue_post(&backlight_queue, BACKLIGHT_QUIT, 0);
760 thread_wait(thread);
762 #endif /* BACKLIGHT_DRIVER_CLOSE */
764 void backlight_on(void)
766 queue_remove_from_head(&backlight_queue, BACKLIGHT_ON);
767 queue_post(&backlight_queue, BACKLIGHT_ON, 0);
770 void backlight_off(void)
772 queue_post(&backlight_queue, BACKLIGHT_OFF, 0);
775 /* returns true when the backlight is on,
776 * and optionally when it's set to always off. */
777 bool is_backlight_on(bool ignore_always_off)
779 int timeout = backlight_get_current_timeout();
780 return (backlight_timer > 0) /* countdown */
781 || (timeout == 0) /* always on */
782 || ((timeout < 0) && !ignore_always_off);
785 /* return value in ticks; 0 means always on, <0 means always off */
786 int backlight_get_current_timeout(void)
788 #ifdef HAS_BUTTON_HOLD
789 if ((backlight_on_button_hold != 0)
790 #ifdef HAVE_REMOTE_LCD_AS_MAIN
791 && remote_button_hold()
792 #else
793 && button_hold()
794 #endif
796 return (backlight_on_button_hold == 2) ? 0 : -1;
797 /* always on or always off */
798 else
799 #endif
800 #if CONFIG_CHARGING
801 if (power_input_present())
802 return backlight_timeout_plugged;
803 else
804 #endif
805 return backlight_timeout_normal;
808 void backlight_set_timeout(int value)
810 backlight_timeout_normal = HZ * value;
811 queue_post(&backlight_queue, BACKLIGHT_TMO_CHANGED, 0);
814 #if CONFIG_CHARGING
815 void backlight_set_timeout_plugged(int value)
817 backlight_timeout_plugged = HZ * value;
818 queue_post(&backlight_queue, BACKLIGHT_TMO_CHANGED, 0);
820 #endif /* CONFIG_CHARGING */
822 #ifdef HAS_BUTTON_HOLD
823 /* Hold button change event handler. */
824 void backlight_hold_changed(bool hold_button)
826 if (!hold_button || (backlight_on_button_hold > 0))
827 /* if unlocked or override in effect */
828 backlight_on();
831 void backlight_set_on_button_hold(int index)
833 if ((unsigned)index >= 3)
834 /* if given a weird value, use default */
835 index = 0;
837 backlight_on_button_hold = index;
838 queue_post(&backlight_queue, BACKLIGHT_TMO_CHANGED, 0);
840 #endif /* HAS_BUTTON_HOLD */
842 #ifdef HAVE_LCD_SLEEP_SETTING
843 void lcd_set_sleep_after_backlight_off(int index)
845 if ((unsigned)index >= sizeof(lcd_sleep_timeout_value))
846 /* if given a weird value, use default */
847 index = 3;
849 lcd_sleep_timeout = HZ * lcd_sleep_timeout_value[index];
851 if (is_backlight_on(true))
852 /* Timer will be set when bl turns off or bl set to on. */
853 return;
855 /* Backlight is Off */
856 if (lcd_sleep_timeout < 0)
857 lcd_sleep_timer = 1; /* Always - sleep next tick */
858 else
859 lcd_sleep_timer = lcd_sleep_timeout; /* Never, other */
861 #endif /* HAVE_LCD_SLEEP_SETTING */
863 #ifdef HAVE_REMOTE_LCD
864 void remote_backlight_on(void)
866 queue_post(&backlight_queue, REMOTE_BACKLIGHT_ON, 0);
869 void remote_backlight_off(void)
871 queue_post(&backlight_queue, REMOTE_BACKLIGHT_OFF, 0);
874 void remote_backlight_set_timeout(int value)
876 remote_backlight_timeout_normal = HZ * value;
877 queue_post(&backlight_queue, REMOTE_BACKLIGHT_TMO_CHANGED, 0);
880 #if CONFIG_CHARGING
881 void remote_backlight_set_timeout_plugged(int value)
883 remote_backlight_timeout_plugged = HZ * value;
884 queue_post(&backlight_queue, REMOTE_BACKLIGHT_TMO_CHANGED, 0);
886 #endif /* CONFIG_CHARGING */
888 #ifdef HAS_REMOTE_BUTTON_HOLD
889 /* Remote hold button change event handler. */
890 void remote_backlight_hold_changed(bool rc_hold_button)
892 if (!rc_hold_button || (remote_backlight_on_button_hold > 0))
893 /* if unlocked or override */
894 remote_backlight_on();
897 void remote_backlight_set_on_button_hold(int index)
899 if ((unsigned)index >= 3)
900 /* if given a weird value, use default */
901 index = 0;
903 remote_backlight_on_button_hold = index;
904 queue_post(&backlight_queue, REMOTE_BACKLIGHT_TMO_CHANGED, 0);
906 #endif /* HAS_REMOTE_BUTTON_HOLD */
908 /* return value in ticks; 0 means always on, <0 means always off */
909 int remote_backlight_get_current_timeout(void)
911 #ifdef HAS_REMOTE_BUTTON_HOLD
912 if (remote_button_hold() && (remote_backlight_on_button_hold != 0))
913 return (remote_backlight_on_button_hold == 2)
914 ? 0 : -1; /* always on or always off */
915 else
916 #endif
917 #if CONFIG_CHARGING
918 if (power_input_present())
919 return remote_backlight_timeout_plugged;
920 else
921 #endif
922 return remote_backlight_timeout_normal;
925 /* returns true when the backlight is on, and
926 * optionally when it's set to always off */
927 bool is_remote_backlight_on(bool ignore_always_off)
929 int timeout = remote_backlight_get_current_timeout();
930 return (remote_backlight_timer > 0) /* countdown */
931 || (timeout == 0) /* always on */
932 || ((timeout < 0) && !ignore_always_off);
935 #endif /* HAVE_REMOTE_LCD */
937 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
938 void backlight_set_brightness(int val)
940 if (val < MIN_BRIGHTNESS_SETTING)
941 val = MIN_BRIGHTNESS_SETTING;
942 else if (val > MAX_BRIGHTNESS_SETTING)
943 val = MAX_BRIGHTNESS_SETTING;
945 queue_post(&backlight_queue, BACKLIGHT_BRIGHTNESS_CHANGED, val);
947 #endif /* HAVE_BACKLIGHT_BRIGHTNESS */
949 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
950 void buttonlight_set_brightness(int val)
952 if (val < MIN_BRIGHTNESS_SETTING)
953 val = MIN_BRIGHTNESS_SETTING;
954 else if (val > MAX_BRIGHTNESS_SETTING)
955 val = MAX_BRIGHTNESS_SETTING;
957 queue_post(&backlight_queue, BUTTON_LIGHT_BRIGHTNESS_CHANGED, val);
959 #endif /* HAVE_BUTTONLIGHT_BRIGHTNESS */
961 #else /* !defined(HAVE_BACKLIGHT) || !defined(BACKLIGHT_FULL_INIT)
962 -- no backlight, empty dummy functions */
964 #if defined(HAVE_BACKLIGHT) && !defined(BACKLIGHT_FULL_INIT)
965 void backlight_init(void)
967 (void)_backlight_init();
968 _backlight_on();
970 #endif
972 void backlight_on(void) {}
973 void backlight_off(void) {}
974 void buttonlight_on(void) {}
975 void backlight_set_timeout(int value) {(void)value;}
977 bool is_backlight_on(bool ignore_always_off)
979 (void)ignore_always_off;
980 return true;
982 #ifdef HAVE_REMOTE_LCD
983 void remote_backlight_on(void) {}
984 void remote_backlight_off(void) {}
985 void remote_backlight_set_timeout(int value) {(void)value;}
987 bool is_remote_backlight_on(bool ignore_always_off)
989 (void)ignore_always_off;
990 return true;
992 #endif /* HAVE_REMOTE_LCD */
993 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
994 void backlight_set_brightness(int val) { (void)val; }
995 #endif
996 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
997 void buttonlight_set_brightness(int val) { (void)val; }
998 #endif
999 #endif /* defined(HAVE_BACKLIGHT) && defined(BACKLIGHT_FULL_INIT) */