summaryrefslogtreecommitdiff
path: root/firmware/backlight.c
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/backlight.c')
-rw-r--r--firmware/backlight.c126
1 files changed, 114 insertions, 12 deletions
diff --git a/firmware/backlight.c b/firmware/backlight.c
index b8becfa69f..fe9c215af5 100644
--- a/firmware/backlight.c
+++ b/firmware/backlight.c
@@ -8,6 +8,8 @@
8 * $Id$ 8 * $Id$
9 * 9 *
10 * Copyright (C) 2002 by Linus Nielsen Feltzing 10 * Copyright (C) 2002 by Linus Nielsen Feltzing
11 * Additional work by Martin Ritter (2007) and Thomas Martitz (2008)
12 * for backlight thread fading
11 * 13 *
12 * This program is free software; you can redistribute it and/or 14 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License 15 * modify it under the terms of the GNU General Public License
@@ -46,6 +48,9 @@
46#define BACKLIGHT_FULL_INIT 48#define BACKLIGHT_FULL_INIT
47#endif 49#endif
48 50
51#ifdef USE_BACKLIGHT_SW_FADING
52#include "backlight-thread-fading.h"
53#endif
49#ifdef SIMULATOR 54#ifdef SIMULATOR
50/* TODO: find a better way to do it but we need a kernel thread somewhere to 55/* TODO: find a better way to do it but we need a kernel thread somewhere to
51 handle this */ 56 handle this */
@@ -134,6 +139,10 @@ static int backlight_timeout_plugged = 5*HZ;
134static int backlight_on_button_hold = 0; 139static int backlight_on_button_hold = 0;
135#endif 140#endif
136 141
142#ifdef HAVE_BACKLIGHT_BRIGHTNESS
143int backlight_brightness = DEFAULT_BRIGHTNESS_SETTING;
144#endif
145
137#ifdef HAVE_BUTTON_LIGHT 146#ifdef HAVE_BUTTON_LIGHT
138static int buttonlight_timer; 147static int buttonlight_timer;
139int _buttonlight_timeout = 5*HZ; 148int _buttonlight_timeout = 5*HZ;
@@ -221,6 +230,12 @@ void backlight_lcd_sleep_countdown(bool start)
221} 230}
222#endif /* HAVE_LCD_SLEEP */ 231#endif /* HAVE_LCD_SLEEP */
223 232
233#ifdef USE_BACKLIGHT_SW_FADING
234static int backlight_fading_type = (FADING_UP|FADING_DOWN);
235static int backlight_fading_state = NOT_FADING;
236#endif
237
238
224#if defined(HAVE_BACKLIGHT_PWM_FADING) && !defined(SIMULATOR) 239#if defined(HAVE_BACKLIGHT_PWM_FADING) && !defined(SIMULATOR)
225/* backlight fading */ 240/* backlight fading */
226#define BL_PWM_INTERVAL 5 /* Cycle interval in ms */ 241#define BL_PWM_INTERVAL 5 /* Cycle interval in ms */
@@ -397,6 +412,66 @@ void backlight_set_fade_out(int value)
397} 412}
398#endif /* defined(HAVE_BACKLIGHT_PWM_FADING) && !defined(SIMULATOR) */ 413#endif /* defined(HAVE_BACKLIGHT_PWM_FADING) && !defined(SIMULATOR) */
399 414
415#ifdef USE_BACKLIGHT_SW_FADING
416
417void backlight_set_fade_out(bool value)
418{
419 if(value) /* on */
420 backlight_fading_type |= FADING_DOWN;
421 else
422 backlight_fading_type &= FADING_UP;
423}
424
425void backlight_set_fade_in(bool value)
426{
427 if(value) /* on */
428 backlight_fading_type |= FADING_UP;
429 else
430 backlight_fading_type &= FADING_DOWN;
431}
432
433static void backlight_set_up_fade_up(void)
434{
435 if (backlight_fading_type & FADING_UP)
436 {
437 if (backlight_fading_state == NOT_FADING)
438 {
439 /* make sure the backlight is at lowest level */
440 _backlight_on();
441 }
442 backlight_fading_state = FADING_UP;
443 }
444 else
445 {
446 backlight_fading_state = NOT_FADING;
447 _backlight_fade_update_state(backlight_brightness);
448 _backlight_on();
449 _backlight_set_brightness(backlight_brightness);
450 }
451}
452
453static void backlight_set_up_fade_down(void)
454{
455 if (backlight_fading_type & FADING_DOWN)
456 {
457 backlight_fading_state = FADING_DOWN;
458 }
459 else
460 {
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
471 }
472}
473#endif /* USE_BACKLIGHT_SW_FADING */
474
400/* Update state of backlight according to timeout setting */ 475/* Update state of backlight according to timeout setting */
401static void backlight_update_state(void) 476static void backlight_update_state(void)
402{ 477{
@@ -424,15 +499,25 @@ static void backlight_update_state(void)
424 backlight_timeout = backlight_timeout_normal; 499 backlight_timeout = backlight_timeout_normal;
425 500
426 /* Backlight == OFF in the setting? */ 501 /* Backlight == OFF in the setting? */
427 if (backlight_timeout < 0) 502 if (UNLIKELY(backlight_timeout < 0))
428 { 503 {
429 backlight_timer = 0; /* Disable the timeout */ 504 backlight_timer = 0; /* Disable the timeout */
505#ifdef USE_BACKLIGHT_SW_FADING
506 backlight_set_up_fade_down();
507 /* necessary step to issue fading down when the setting is selected */
508 queue_post(&backlight_queue, SYS_TIMEOUT, 0);
509#else
430 _backlight_off(); 510 _backlight_off();
511#endif
431 } 512 }
432 else 513 else
433 { 514 {
434 backlight_timer = backlight_timeout; 515 backlight_timer = backlight_timeout;
516#if defined(USE_BACKLIGHT_SW_FADING)
517 backlight_set_up_fade_up();
518#else
435 _backlight_on(); 519 _backlight_on();
520#endif
436 } 521 }
437} 522}
438 523
@@ -478,7 +563,15 @@ void backlight_thread(void)
478 563
479 while(1) 564 while(1)
480 { 565 {
481 queue_wait(&backlight_queue, &ev); 566#if defined(USE_BACKLIGHT_SW_FADING)
567 if (backlight_fading_state)
568 queue_wait_w_tmo(&backlight_queue, &ev, FADE_DELAY);
569 else
570#endif
571 queue_wait(&backlight_queue, &ev);
572/*
573#endif
574*/
482 switch(ev.id) 575 switch(ev.id)
483 { /* These events must always be processed */ 576 { /* These events must always be processed */
484#ifdef _BACKLIGHT_FADE_BOOST 577#ifdef _BACKLIGHT_FADE_BOOST
@@ -558,9 +651,12 @@ void backlight_thread(void)
558 651
559 case BACKLIGHT_OFF: 652 case BACKLIGHT_OFF:
560 backlight_timer = 0; /* Disable the timeout */ 653 backlight_timer = 0; /* Disable the timeout */
654#ifndef USE_BACKLIGHT_SW_FADING
561 _backlight_off(); 655 _backlight_off();
656#else
657 backlight_set_up_fade_down();
658#endif /* USE_BACKLIGHT_SW_FADING */
562 break; 659 break;
563
564#ifdef HAVE_LCD_SLEEP 660#ifdef HAVE_LCD_SLEEP
565 case LCD_SLEEP: 661 case LCD_SLEEP:
566 lcd_sleep(); 662 lcd_sleep();
@@ -589,6 +685,12 @@ void backlight_thread(void)
589 remote_backlight_update_state(); 685 remote_backlight_update_state();
590#endif 686#endif
591 break; 687 break;
688#if defined(USE_BACKLIGHT_SW_FADING)
689 case SYS_TIMEOUT:
690 if ((_backlight_fade_step(backlight_fading_state)))
691 backlight_fading_state = NOT_FADING;
692 break;
693#endif /* USE_BACKLIGHT_SW_FADING */
592 } 694 }
593 } /* end while */ 695 } /* end while */
594} 696}
@@ -597,8 +699,7 @@ static void backlight_tick(void)
597{ 699{
598 if(backlight_timer) 700 if(backlight_timer)
599 { 701 {
600 backlight_timer--; 702 if(--backlight_timer == 0)
601 if(backlight_timer == 0)
602 { 703 {
603 backlight_off(); 704 backlight_off();
604 } 705 }
@@ -606,20 +707,17 @@ static void backlight_tick(void)
606#ifdef HAVE_LCD_SLEEP 707#ifdef HAVE_LCD_SLEEP
607 else if(lcd_sleep_timer) 708 else if(lcd_sleep_timer)
608 { 709 {
609 lcd_sleep_timer--; 710 if(--lcd_sleep_timer == 0)
610 if(lcd_sleep_timer == 0)
611 { 711 {
612 /* Queue on bl thread or freeze! */ 712 /* Queue on bl thread or freeze! */
613 queue_post(&backlight_queue, LCD_SLEEP, 0); 713 queue_post(&backlight_queue, LCD_SLEEP, 0);
614 } 714 }
615 } 715 }
616#endif /* HAVE_LCD_SLEEP */ 716#endif /* HAVE_LCD_SLEEP */
617
618#ifdef HAVE_REMOTE_LCD 717#ifdef HAVE_REMOTE_LCD
619 if(remote_backlight_timer) 718 if(remote_backlight_timer)
620 { 719 {
621 remote_backlight_timer--; 720 if(--remote_backlight_timer == 0)
622 if(remote_backlight_timer == 0)
623 { 721 {
624 remote_backlight_off(); 722 remote_backlight_off();
625 } 723 }
@@ -628,8 +726,7 @@ static void backlight_tick(void)
628#ifdef HAVE_BUTTON_LIGHT 726#ifdef HAVE_BUTTON_LIGHT
629 if (buttonlight_timer) 727 if (buttonlight_timer)
630 { 728 {
631 buttonlight_timer--; 729 if (--buttonlight_timer == 0)
632 if (buttonlight_timer == 0)
633 { 730 {
634 buttonlight_off(); 731 buttonlight_off();
635 } 732 }
@@ -832,6 +929,11 @@ void backlight_set_brightness(int val)
832 val = MAX_BRIGHTNESS_SETTING; 929 val = MAX_BRIGHTNESS_SETTING;
833 930
834 _backlight_set_brightness(val); 931 _backlight_set_brightness(val);
932 backlight_brightness = val;
933#ifdef USE_BACKLIGHT_SW_FADING
934 /* receive backlight brightness */
935 _backlight_fade_update_state(val);
936#endif
835} 937}
836#endif /* HAVE_BACKLIGHT_BRIGHTNESS */ 938#endif /* HAVE_BACKLIGHT_BRIGHTNESS */
837 939