pcm-as3525.c : don't use |= on a write-only register (VIC_INT_EN_CLEAR).
[kugel-rb.git] / firmware / backlight.c
blob07cc9532bef212333c389d2e555d2d2c9a27f263
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"
39 #ifdef HAVE_REMOTE_LCD
40 #include "lcd-remote.h"
41 #endif
42 #ifndef SIMULATOR
43 #include "backlight-target.h"
44 #endif
46 #if !defined(BOOTLOADER)
47 /* The whole driver should be built */
48 #define BACKLIGHT_FULL_INIT
49 #endif
51 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
52 int backlight_brightness = DEFAULT_BRIGHTNESS_SETTING;
53 #endif
55 #ifdef USE_BACKLIGHT_SW_FADING
56 #include "backlight-thread-fading.h"
57 #endif
58 #ifdef SIMULATOR
59 /* TODO: find a better way to do it but we need a kernel thread somewhere to
60 handle this */
61 extern void screen_dump(void);
63 static inline void _backlight_on(void)
65 sim_backlight(100);
68 static inline void _backlight_off(void)
70 sim_backlight(0);
73 static inline void _backlight_set_brightness(int val)
75 (void)val;
78 static inline void _buttonlight_on(void)
82 static inline void _buttonlight_off(void)
86 static inline void _buttonlight_set_brightness(int val)
88 (void)val;
90 #ifdef HAVE_REMOTE_LCD
91 static inline void _remote_backlight_on(void)
93 sim_remote_backlight(100);
96 static inline void _remote_backlight_off(void)
98 sim_remote_backlight(0);
100 #endif /* HAVE_REMOTE_LCD */
102 #endif /* SIMULATOR */
104 #if defined(HAVE_BACKLIGHT) && defined(BACKLIGHT_FULL_INIT)
106 enum {
107 BACKLIGHT_ON,
108 BACKLIGHT_OFF,
109 #ifdef HAVE_REMOTE_LCD
110 REMOTE_BACKLIGHT_ON,
111 REMOTE_BACKLIGHT_OFF,
112 #endif
113 #if defined(_BACKLIGHT_FADE_BOOST) || defined(_BACKLIGHT_FADE_ENABLE)
114 BACKLIGHT_FADE_FINISH,
115 #endif
116 #ifdef HAVE_LCD_SLEEP
117 LCD_SLEEP,
118 #endif
119 #ifdef HAVE_BUTTON_LIGHT
120 BUTTON_LIGHT_ON,
121 BUTTON_LIGHT_OFF,
122 #endif
123 #ifdef BACKLIGHT_DRIVER_CLOSE
124 BACKLIGHT_QUIT,
125 #endif
128 static void backlight_thread(void);
129 static long backlight_stack[DEFAULT_STACK_SIZE/sizeof(long)];
130 static const char backlight_thread_name[] = "backlight";
131 static struct event_queue backlight_queue;
132 #ifdef BACKLIGHT_DRIVER_CLOSE
133 static struct thread_entry *backlight_thread_p = NULL;
134 #endif
136 static int backlight_timer SHAREDBSS_ATTR;
137 static int backlight_timeout SHAREDBSS_ATTR;
138 static int backlight_timeout_normal = 5*HZ;
139 #if CONFIG_CHARGING
140 static int backlight_timeout_plugged = 5*HZ;
141 #endif
142 #ifdef HAS_BUTTON_HOLD
143 static int backlight_on_button_hold = 0;
144 #endif
146 #ifdef HAVE_BUTTON_LIGHT
147 static int buttonlight_timer;
148 int _buttonlight_timeout = 5*HZ;
150 /* Update state of buttonlight according to timeout setting */
151 static void buttonlight_update_state(void)
153 buttonlight_timer = _buttonlight_timeout;
155 /* Buttonlight == OFF in the setting? */
156 if (buttonlight_timer < 0)
158 buttonlight_timer = 0; /* Disable the timeout */
159 _buttonlight_off();
161 else
162 _buttonlight_on();
165 /* external interface */
166 void buttonlight_on(void)
168 queue_remove_from_head(&backlight_queue, BUTTON_LIGHT_ON);
169 queue_post(&backlight_queue, BUTTON_LIGHT_ON, 0);
172 void buttonlight_off(void)
174 queue_post(&backlight_queue, BUTTON_LIGHT_OFF, 0);
177 void buttonlight_set_timeout(int value)
179 _buttonlight_timeout = HZ * value;
180 buttonlight_update_state();
183 #endif /* HAVE_BUTTON_LIGHT */
185 #ifdef HAVE_REMOTE_LCD
186 static int remote_backlight_timer;
187 static int remote_backlight_timeout;
188 static int remote_backlight_timeout_normal = 5*HZ;
189 #if CONFIG_CHARGING
190 static int remote_backlight_timeout_plugged = 5*HZ;
191 #endif
192 #ifdef HAS_REMOTE_BUTTON_HOLD
193 static int remote_backlight_on_button_hold = 0;
194 #endif
195 #endif /* HAVE_REMOTE_LCD */
197 #ifdef HAVE_LCD_SLEEP
198 #ifdef HAVE_LCD_SLEEP_SETTING
199 const signed char lcd_sleep_timeout_value[10] =
201 -1, 0, 5, 10, 15, 20, 30, 45, 60, 90
203 static int lcd_sleep_timeout = 10*HZ;
204 #else
205 /* Target defines needed value */
206 static const int lcd_sleep_timeout = LCD_SLEEP_TIMEOUT;
207 #endif
209 static int lcd_sleep_timer = 0;
211 void backlight_lcd_sleep_countdown(bool start)
213 if (!start)
215 /* Cancel the LCD sleep countdown */
216 lcd_sleep_timer = 0;
217 return;
220 /* Start LCD sleep countdown */
221 if (lcd_sleep_timeout < 0)
223 lcd_sleep_timer = 0; /* Setting == Always */
224 lcd_sleep();
226 else
228 lcd_sleep_timer = lcd_sleep_timeout;
231 #endif /* HAVE_LCD_SLEEP */
233 #ifdef USE_BACKLIGHT_SW_FADING
234 static int backlight_fading_type = (FADING_UP|FADING_DOWN);
235 static int backlight_fading_state = NOT_FADING;
236 #endif
239 #if defined(HAVE_BACKLIGHT_PWM_FADING) && !defined(SIMULATOR)
240 /* backlight fading */
241 #define BL_PWM_INTERVAL 5 /* Cycle interval in ms */
242 #define BL_PWM_BITS 8
243 #define BL_PWM_COUNT (1<<BL_PWM_BITS)
245 /* s15.16 fixed point variables */
246 static int32_t bl_fade_in_step = ((BL_PWM_INTERVAL*BL_PWM_COUNT)<<16)/300;
247 static int32_t bl_fade_out_step = ((BL_PWM_INTERVAL*BL_PWM_COUNT)<<16)/2000;
248 static int32_t bl_dim_fraction = 0;
250 static int bl_dim_target = 0;
251 static int bl_dim_current = 0;
252 static enum {DIM_STATE_START, DIM_STATE_MAIN} bl_dim_state = DIM_STATE_START;
253 static bool bl_timer_active = false;
255 static void backlight_isr(void)
257 int timer_period = (TIMER_FREQ*BL_PWM_INTERVAL/1000);
258 bool idle = false;
260 switch (bl_dim_state)
262 /* New cycle */
263 case DIM_STATE_START:
264 bl_dim_current = bl_dim_fraction >> 16;
266 if (bl_dim_current > 0 && bl_dim_current < BL_PWM_COUNT)
268 _backlight_on_isr();
269 timer_period = (timer_period * bl_dim_current) >> BL_PWM_BITS;
270 bl_dim_state = DIM_STATE_MAIN;
272 else
274 if (bl_dim_current)
275 _backlight_on_isr();
276 else
277 _backlight_off_isr();
278 if (bl_dim_current == bl_dim_target)
279 idle = true;
281 if (bl_dim_current < bl_dim_target)
283 bl_dim_fraction = MIN(bl_dim_fraction + bl_fade_in_step,
284 (BL_PWM_COUNT<<16));
286 else if (bl_dim_current > bl_dim_target)
288 bl_dim_fraction = MAX(bl_dim_fraction - bl_fade_out_step, 0);
290 break;
292 /* Dim main screen */
293 case DIM_STATE_MAIN:
294 _backlight_off_isr();
295 timer_period = (timer_period * (BL_PWM_COUNT - bl_dim_current))
296 >> BL_PWM_BITS;
297 bl_dim_state = DIM_STATE_START;
298 break ;
300 if (idle)
302 #if defined(_BACKLIGHT_FADE_BOOST) || defined(_BACKLIGHT_FADE_ENABLE)
303 queue_post(&backlight_queue, BACKLIGHT_FADE_FINISH, 0);
304 #endif
305 timer_unregister();
306 bl_timer_active = false;
308 else
309 timer_set_period(timer_period);
312 static void backlight_switch(void)
314 if (bl_dim_target > (BL_PWM_COUNT/2))
316 _backlight_on_normal();
317 bl_dim_fraction = (BL_PWM_COUNT<<16);
319 else
321 _backlight_off_normal();
322 bl_dim_fraction = 0;
326 static void backlight_release_timer(void)
328 #ifdef _BACKLIGHT_FADE_BOOST
329 cpu_boost(false);
330 #endif
331 timer_unregister();
332 bl_timer_active = false;
333 backlight_switch();
336 static void backlight_dim(int value)
338 /* protect from extraneous calls with the same target value */
339 if (value == bl_dim_target)
340 return;
342 bl_dim_target = value;
344 if (bl_timer_active)
345 return ;
347 if (timer_register(0, backlight_release_timer, 2, 0, backlight_isr
348 IF_COP(, CPU)))
350 #ifdef _BACKLIGHT_FADE_BOOST
351 /* Prevent cpu frequency changes while dimming. */
352 cpu_boost(true);
353 #endif
354 bl_timer_active = true;
356 else
357 backlight_switch();
360 static void _backlight_on(void)
362 #ifdef HAVE_LCD_SLEEP
363 backlight_lcd_sleep_countdown(false);
364 #endif
366 if (bl_fade_in_step > 0)
368 #ifdef _BACKLIGHT_FADE_ENABLE
369 _backlight_hw_enable(true);
370 #endif
371 backlight_dim(BL_PWM_COUNT);
373 else
375 bl_dim_target = BL_PWM_COUNT;
376 bl_dim_fraction = (BL_PWM_COUNT<<16);
377 _backlight_on_normal();
381 static void _backlight_off(void)
383 if (bl_fade_out_step > 0)
385 backlight_dim(0);
387 else
389 bl_dim_target = bl_dim_fraction = 0;
390 _backlight_off_normal();
393 #ifdef HAVE_LCD_SLEEP
394 backlight_lcd_sleep_countdown(true);
395 #endif
398 void backlight_set_fade_in(int value)
400 if (value > 0)
401 bl_fade_in_step = ((BL_PWM_INTERVAL*BL_PWM_COUNT)<<16) / value;
402 else
403 bl_fade_in_step = 0;
406 void backlight_set_fade_out(int value)
408 if (value > 0)
409 bl_fade_out_step = ((BL_PWM_INTERVAL*BL_PWM_COUNT)<<16) / value;
410 else
411 bl_fade_out_step = 0;
413 #endif /* defined(HAVE_BACKLIGHT_PWM_FADING) && !defined(SIMULATOR) */
415 #ifdef USE_BACKLIGHT_SW_FADING
417 void backlight_set_fade_out(bool value)
419 if(value) /* on */
420 backlight_fading_type |= FADING_DOWN;
421 else
422 backlight_fading_type &= FADING_UP;
425 void backlight_set_fade_in(bool value)
427 if(value) /* on */
428 backlight_fading_type |= FADING_UP;
429 else
430 backlight_fading_type &= FADING_DOWN;
433 static void backlight_set_up_fade_up(void)
435 if (backlight_fading_type & FADING_UP)
437 if (backlight_fading_state == NOT_FADING)
439 /* make sure the backlight is at lowest level */
440 _backlight_on();
442 backlight_fading_state = FADING_UP;
444 else
446 backlight_fading_state = NOT_FADING;
447 _backlight_fade_update_state(backlight_brightness);
448 _backlight_on();
449 _backlight_set_brightness(backlight_brightness);
453 static void backlight_set_up_fade_down(void)
455 if (backlight_fading_type & FADING_DOWN)
457 backlight_fading_state = FADING_DOWN;
459 else
461 backlight_fading_state = NOT_FADING;
462 _backlight_fade_update_state(MIN_BRIGHTNESS_SETTING-1);
463 _backlight_off();
464 /* h300/x5/d2 go to the last known brightness level at backight_on(),which
465 * should be the lowest level to keep fading up glitch free
466 * sansa e200/c200 make the backlight on only by setting the brightness,
467 * so this step would be noticeable */
468 #if !defined(SANSA_E200) && !defined(SANSA_C200)
469 _backlight_set_brightness(MIN_BRIGHTNESS_SETTING);
470 #endif
473 #endif /* USE_BACKLIGHT_SW_FADING */
475 /* Update state of backlight according to timeout setting */
476 static void backlight_update_state(void)
478 #ifdef HAS_BUTTON_HOLD
479 if ((backlight_on_button_hold != 0)
480 #ifdef HAVE_REMOTE_LCD_AS_MAIN
481 && remote_button_hold()
482 #else
483 && button_hold()
484 #endif
486 backlight_timeout = (backlight_on_button_hold == 2) ? 0 : -1;
487 /* always on or always off */
488 else
489 #endif
490 #if CONFIG_CHARGING
491 if (power_input_present())
492 backlight_timeout = backlight_timeout_plugged;
493 else
494 #endif
495 backlight_timeout = backlight_timeout_normal;
497 /* Backlight == OFF in the setting? */
498 if (UNLIKELY(backlight_timeout < 0))
500 backlight_timer = 0; /* Disable the timeout */
501 #ifdef USE_BACKLIGHT_SW_FADING
502 backlight_set_up_fade_down();
503 /* necessary step to issue fading down when the setting is selected */
504 queue_post(&backlight_queue, SYS_TIMEOUT, 0);
505 #else
506 _backlight_off();
507 #endif
509 else
511 backlight_timer = backlight_timeout;
512 #if defined(USE_BACKLIGHT_SW_FADING)
513 backlight_set_up_fade_up();
514 #else
515 _backlight_on();
516 #endif
520 #ifdef HAVE_REMOTE_LCD
521 /* Update state of remote backlight according to timeout setting */
522 static void remote_backlight_update_state(void)
524 #ifdef HAS_REMOTE_BUTTON_HOLD
525 if (remote_button_hold() && (remote_backlight_on_button_hold != 0))
526 remote_backlight_timeout = (remote_backlight_on_button_hold == 2)
527 ? 0 : -1; /* always on or always off */
528 else
529 #endif
530 #if CONFIG_CHARGING
531 if (power_input_present())
532 remote_backlight_timeout = remote_backlight_timeout_plugged;
533 else
534 #endif
535 remote_backlight_timeout = remote_backlight_timeout_normal;
537 /* Backlight == OFF in the setting? */
538 if (remote_backlight_timeout < 0)
540 remote_backlight_timer = 0; /* Disable the timeout */
541 _remote_backlight_off();
543 else
545 remote_backlight_timer = remote_backlight_timeout;
546 _remote_backlight_on();
549 #endif /* HAVE_REMOTE_LCD */
551 void backlight_thread(void)
553 struct queue_event ev;
554 bool locked = false;
556 while(1)
558 #if defined(USE_BACKLIGHT_SW_FADING)
559 if (backlight_fading_state)
560 queue_wait_w_tmo(&backlight_queue, &ev, FADE_DELAY);
561 else
562 #endif
563 queue_wait(&backlight_queue, &ev);
565 #endif
567 switch(ev.id)
568 { /* These events must always be processed */
569 #ifdef _BACKLIGHT_FADE_BOOST
570 case BACKLIGHT_FADE_FINISH:
571 cpu_boost(false);
572 break;
573 #endif
574 #ifdef _BACKLIGHT_FADE_ENABLE
575 case BACKLIGHT_FADE_FINISH:
576 _backlight_hw_enable((bl_dim_current|bl_dim_target) != 0);
577 break;
578 #endif
580 #ifndef SIMULATOR
581 /* Here for now or else the aggressive init messes up scrolling */
582 #ifdef HAVE_REMOTE_LCD
583 case SYS_REMOTE_PLUGGED:
584 lcd_remote_on();
585 lcd_remote_update();
586 break;
588 case SYS_REMOTE_UNPLUGGED:
589 lcd_remote_off();
590 break;
591 #elif defined HAVE_REMOTE_LCD_AS_MAIN
592 case SYS_REMOTE_PLUGGED:
593 lcd_on();
594 lcd_update();
595 break;
597 case SYS_REMOTE_UNPLUGGED:
598 lcd_off();
599 break;
600 #endif /* HAVE_REMOTE_LCD/ HAVE_REMOTE_LCD_AS_MAIN */
601 #endif /* !SIMULATOR */
602 #ifdef SIMULATOR
603 /* This one here too for lack of a better place */
604 case SYS_SCREENDUMP:
605 screen_dump();
606 break;
607 #endif
608 case SYS_USB_CONNECTED:
609 /* Tell the USB thread that we are safe */
610 DEBUGF("backlight_thread got SYS_USB_CONNECTED\n");
611 usb_acknowledge(SYS_USB_CONNECTED_ACK);
612 break;
614 case SYS_USB_DISCONNECTED:
615 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
616 break;
618 #ifdef BACKLIGHT_DRIVER_CLOSE
619 /* Get out of here */
620 case BACKLIGHT_QUIT:
621 return;
622 #endif
624 if (locked)
625 continue;
627 switch(ev.id)
628 { /* These events are only processed if backlight isn't locked */
629 #ifdef HAVE_REMOTE_LCD
630 case REMOTE_BACKLIGHT_ON:
631 remote_backlight_update_state();
632 break;
634 case REMOTE_BACKLIGHT_OFF:
635 remote_backlight_timer = 0; /* Disable the timeout */
636 _remote_backlight_off();
637 break;
638 #endif /* HAVE_REMOTE_LCD */
640 case BACKLIGHT_ON:
641 backlight_update_state();
642 break;
644 case BACKLIGHT_OFF:
645 backlight_timer = 0; /* Disable the timeout */
646 #ifndef USE_BACKLIGHT_SW_FADING
647 _backlight_off();
648 #else
649 backlight_set_up_fade_down();
650 #endif /* USE_BACKLIGHT_SW_FADING */
651 break;
652 #ifdef HAVE_LCD_SLEEP
653 case LCD_SLEEP:
654 lcd_sleep();
655 break;
656 #endif
657 #ifdef HAVE_BUTTON_LIGHT
658 case BUTTON_LIGHT_ON:
659 buttonlight_update_state();
660 break;
662 case BUTTON_LIGHT_OFF:
663 buttonlight_timer = 0;
664 _buttonlight_off();
665 break;
666 #endif
668 case SYS_POWEROFF: /* Lock backlight on poweroff so it doesn't */
669 locked = true; /* go off before power is actually cut. */
670 /* fall through */
671 #if CONFIG_CHARGING
672 case SYS_CHARGER_CONNECTED:
673 case SYS_CHARGER_DISCONNECTED:
674 #endif
675 backlight_update_state();
676 #ifdef HAVE_REMOTE_LCD
677 remote_backlight_update_state();
678 #endif
679 break;
680 #if defined(USE_BACKLIGHT_SW_FADING)
681 case SYS_TIMEOUT:
682 if ((_backlight_fade_step(backlight_fading_state)))
683 backlight_fading_state = NOT_FADING;
684 break;
685 #endif /* USE_BACKLIGHT_SW_FADING */
687 } /* end while */
690 static void backlight_tick(void)
692 if(backlight_timer)
694 if(--backlight_timer == 0)
696 backlight_off();
699 #ifdef HAVE_LCD_SLEEP
700 else if(lcd_sleep_timer)
702 if(--lcd_sleep_timer == 0)
704 /* Queue on bl thread or freeze! */
705 queue_post(&backlight_queue, LCD_SLEEP, 0);
708 #endif /* HAVE_LCD_SLEEP */
709 #ifdef HAVE_REMOTE_LCD
710 if(remote_backlight_timer)
712 if(--remote_backlight_timer == 0)
714 remote_backlight_off();
717 #endif /* HAVE_REMOVE_LCD */
718 #ifdef HAVE_BUTTON_LIGHT
719 if (buttonlight_timer)
721 if (--buttonlight_timer == 0)
723 buttonlight_off();
726 #endif /* HAVE_BUTTON_LIGHT */
729 void backlight_init(void)
731 queue_init(&backlight_queue, true);
733 #ifndef SIMULATOR
734 if (_backlight_init())
736 # ifdef HAVE_BACKLIGHT_PWM_FADING
737 /* If backlight is already on, don't fade in. */
738 bl_dim_target = BL_PWM_COUNT;
739 bl_dim_fraction = (BL_PWM_COUNT<<16);
740 # endif
742 #endif
743 /* Leave all lights as set by the bootloader here. The settings load will
744 * call the appropriate backlight_set_*() functions, only changing light
745 * status if necessary. */
746 #ifdef BACKLIGHT_DRIVER_CLOSE
747 backlight_thread_p =
748 #endif
749 create_thread(backlight_thread, backlight_stack,
750 sizeof(backlight_stack), 0, backlight_thread_name
751 IF_PRIO(, PRIORITY_USER_INTERFACE)
752 IF_COP(, CPU));
753 tick_add_task(backlight_tick);
756 #ifdef BACKLIGHT_DRIVER_CLOSE
757 void backlight_close(void)
759 struct thread_entry *thread = backlight_thread_p;
761 /* Wait for thread to exit */
762 if (thread == NULL)
763 return;
765 backlight_thread_p = NULL;
767 queue_post(&backlight_queue, BACKLIGHT_QUIT, 0);
768 thread_wait(thread);
770 #endif /* BACKLIGHT_DRIVER_CLOSE */
772 void backlight_on(void)
774 queue_remove_from_head(&backlight_queue, BACKLIGHT_ON);
775 queue_post(&backlight_queue, BACKLIGHT_ON, 0);
778 void backlight_off(void)
780 queue_post(&backlight_queue, BACKLIGHT_OFF, 0);
783 /* returns true when the backlight is on,
784 * and optionally when it's set to always off. */
785 bool is_backlight_on(bool ignore_always_off)
787 return (backlight_timer > 0) /* countdown */
788 || (backlight_timeout == 0) /* always on */
789 || ((backlight_timeout < 0) && !ignore_always_off);
792 /* return value in ticks; 0 means always on, <0 means always off */
793 int backlight_get_current_timeout(void)
795 return backlight_timeout;
798 void backlight_set_timeout(int value)
800 backlight_timeout_normal = HZ * value;
801 backlight_update_state();
804 #if CONFIG_CHARGING
805 void backlight_set_timeout_plugged(int value)
807 backlight_timeout_plugged = HZ * value;
808 backlight_update_state();
810 #endif /* CONFIG_CHARGING */
812 #ifdef HAS_BUTTON_HOLD
813 /* Hold button change event handler. */
814 void backlight_hold_changed(bool hold_button)
816 if (!hold_button || (backlight_on_button_hold > 0))
817 /* if unlocked or override in effect */
818 backlight_on();
821 void backlight_set_on_button_hold(int index)
823 if ((unsigned)index >= 3)
824 /* if given a weird value, use default */
825 index = 0;
827 backlight_on_button_hold = index;
828 backlight_update_state();
830 #endif /* HAS_BUTTON_HOLD */
832 #ifdef HAVE_LCD_SLEEP_SETTING
833 void lcd_set_sleep_after_backlight_off(int index)
835 if ((unsigned)index >= sizeof(lcd_sleep_timeout_value))
836 /* if given a weird value, use default */
837 index = 3;
839 lcd_sleep_timeout = HZ * lcd_sleep_timeout_value[index];
841 if (backlight_timer > 0 || backlight_get_current_timeout() == 0)
842 /* Timer will be set when bl turns off or bl set to on. */
843 return;
845 /* Backlight is Off */
846 if (lcd_sleep_timeout < 0)
847 lcd_sleep_timer = 1; /* Always - sleep next tick */
848 else
849 lcd_sleep_timer = lcd_sleep_timeout; /* Never, other */
851 #endif /* HAVE_LCD_SLEEP_SETTING */
853 #ifdef HAVE_REMOTE_LCD
854 void remote_backlight_on(void)
856 queue_post(&backlight_queue, REMOTE_BACKLIGHT_ON, 0);
859 void remote_backlight_off(void)
861 queue_post(&backlight_queue, REMOTE_BACKLIGHT_OFF, 0);
864 void remote_backlight_set_timeout(int value)
866 remote_backlight_timeout_normal = HZ * value;
867 remote_backlight_update_state();
870 #if CONFIG_CHARGING
871 void remote_backlight_set_timeout_plugged(int value)
873 remote_backlight_timeout_plugged = HZ * value;
874 remote_backlight_update_state();
876 #endif /* CONFIG_CHARGING */
878 #ifdef HAS_REMOTE_BUTTON_HOLD
879 /* Remote hold button change event handler. */
880 void remote_backlight_hold_changed(bool rc_hold_button)
882 if (!rc_hold_button || (remote_backlight_on_button_hold > 0))
883 /* if unlocked or override */
884 remote_backlight_on();
887 void remote_backlight_set_on_button_hold(int index)
889 if ((unsigned)index >= 3)
890 /* if given a weird value, use default */
891 index = 0;
893 remote_backlight_on_button_hold = index;
894 remote_backlight_update_state();
896 #endif /* HAS_REMOTE_BUTTON_HOLD */
898 /* return value in ticks; 0 means always on, <0 means always off */
899 int remote_backlight_get_current_timeout(void)
901 return remote_backlight_timeout;
904 /* returns true when the backlight is on, and
905 * optionally when it's set to always off */
906 bool is_remote_backlight_on(bool ignore_always_off)
908 return (remote_backlight_timer > 0) /* countdown */
909 || (remote_backlight_timeout == 0) /* always on */
910 || ((remote_backlight_timeout < 0) && !ignore_always_off);
913 #endif /* HAVE_REMOTE_LCD */
915 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
916 void backlight_set_brightness(int val)
918 if (val < MIN_BRIGHTNESS_SETTING)
919 val = MIN_BRIGHTNESS_SETTING;
920 else if (val > MAX_BRIGHTNESS_SETTING)
921 val = MAX_BRIGHTNESS_SETTING;
923 backlight_brightness = val;
924 _backlight_set_brightness(val);
925 #ifdef USE_BACKLIGHT_SW_FADING
926 /* receive backlight brightness */
927 _backlight_fade_update_state(val);
928 #endif
930 #endif /* HAVE_BACKLIGHT_BRIGHTNESS */
932 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
933 void buttonlight_set_brightness(int val)
935 if (val < MIN_BRIGHTNESS_SETTING)
936 val = MIN_BRIGHTNESS_SETTING;
937 else if (val > MAX_BRIGHTNESS_SETTING)
938 val = MAX_BRIGHTNESS_SETTING;
940 _buttonlight_set_brightness(val);
942 #endif /* HAVE_BUTTONLIGHT_BRIGHTNESS */
944 #else /* !defined(HAVE_BACKLIGHT) || !defined(BACKLIGHT_FULL_INIT)
945 -- no backlight, empty dummy functions */
947 #if defined(HAVE_BACKLIGHT) && !defined(BACKLIGHT_FULL_INIT)
948 void backlight_init(void)
950 (void)_backlight_init();
951 _backlight_on();
953 #endif
955 void backlight_on(void) {}
956 void backlight_off(void) {}
957 void buttonlight_on(void) {}
958 void backlight_set_timeout(int value) {(void)value;}
960 bool is_backlight_on(bool ignore_always_off)
962 (void)ignore_always_off;
963 return true;
965 #ifdef HAVE_REMOTE_LCD
966 void remote_backlight_on(void) {}
967 void remote_backlight_off(void) {}
968 void remote_backlight_set_timeout(int value) {(void)value;}
970 bool is_remote_backlight_on(bool ignore_always_off)
972 (void)ignore_always_off;
973 return true;
975 #endif /* HAVE_REMOTE_LCD */
976 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
977 void backlight_set_brightness(int val) { (void)val; }
978 #endif
979 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
980 void buttonlight_set_brightness(int val) { (void)val; }
981 #endif
982 #endif /* defined(HAVE_BACKLIGHT) && defined(BACKLIGHT_FULL_INIT) */