summaryrefslogtreecommitdiffstats
path: root/firmware/backlight.c
blob: 4dcdea917ccbf25d73b9560c8169df78d7b7053a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2002 by Linus Nielsen Feltzing
 *
 * All files in this archive are subject to the GNU General Public License.
 * See the file COPYING in the source tree root for full license agreement.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include "config.h"
#include <stdlib.h>
#include "sh7034.h"
#include "kernel.h"
#include "thread.h"
#include "i2c.h"
#include "debug.h"
#include "rtc.h"
#include "usb.h"

#define BACKLIGHT_ON 1
#define BACKLIGHT_OFF 2

static void backlight_thread(void);
static char backlight_stack[DEFAULT_STACK_SIZE];
static char backlight_thread_name[] = "backlight";
static struct event_queue backlight_queue;

static int backlight_timer;
static int backlight_timeout = 5;

static char timeout_value[19] =
{
    -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30, 45, 60, 90
};

void backlight_thread(void)
{
    struct event ev;
    
    while(1)
    {
        queue_wait(&backlight_queue, &ev);
        switch(ev.id)
        {
            case BACKLIGHT_ON:
                backlight_timer = HZ*timeout_value[backlight_timeout];
                if(backlight_timer < 0)
                {
#ifdef HAVE_RTC
                    /* Disable square wave */
                    rtc_write(0x0a, rtc_read(0x0a) & ~0x40);
#else
                    PADR |= 0x4000;
#endif  
                }
                else if(backlight_timer)
                {
#ifdef HAVE_RTC
                    /* Enable square wave */
                    rtc_write(0x0a, rtc_read(0x0a) | 0x40);
#else
                    PADR &= ~0x4000;
#endif
                }
                break;
                
            case BACKLIGHT_OFF:
#ifdef HAVE_RTC
                /* Disable square wave */
                rtc_write(0x0a, rtc_read(0x0a) & ~0x40);
#else
                PADR |= 0x4000;
#endif
                break;
                
            case SYS_USB_CONNECTED:
                /* Tell the USB thread that we are safe */
                DEBUGF("backlight_thread got SYS_USB_CONNECTED\n");
                usb_acknowledge(SYS_USB_CONNECTED_ACK);
                break;

            case SYS_USB_DISCONNECTED:
                usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
                break;
        }
    }
}

void backlight_on(void)
{
    queue_post(&backlight_queue, BACKLIGHT_ON, NULL);
}

void backlight_off(void)
{
    queue_post(&backlight_queue, BACKLIGHT_OFF, NULL);
}

void backlight_time(int value)
{
    backlight_timeout = value;
    backlight_on();
}

void backlight_tick(void)
{
    if(backlight_timer)
    {
        backlight_timer--;
        if(backlight_timer == 0)
        {
            backlight_off();
        }
    }
}

void backlight_init(void)
{
    queue_init(&backlight_queue);
    create_thread(backlight_thread, backlight_stack,
                  sizeof(backlight_stack), backlight_thread_name);

    PAIOR |= 0x4000;
    
    backlight_on();
}