summaryrefslogtreecommitdiffstats
path: root/firmware/scroll_engine.c
blob: 74c08c1da51025ee9d75496702a8cb9f49706c31 (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2007 by Michael Sevakis
 *
 * LCD scrolling thread and scheduler
 *
 * Much collected and combined from the various Rockbox LCD drivers.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include <stdio.h>
#include "config.h"
#include "gcc_extensions.h"
#include "cpu.h"
#include "kernel.h"
#include "thread.h"
#include "usb.h"
#include "lcd.h"
#include "font.h"
#if !defined(BOOTLOADER)
#include "misc.h"
#include "settings.h"
#endif
#ifdef HAVE_REMOTE_LCD
#include "lcd-remote.h"
#endif
#include "scroll_engine.h"

static const char scroll_tick_table[18] = {
 /* Hz values [f(x)=100.8/(x+.048)]:
    1, 1.25, 1.55, 2, 2.5, 3.12, 4, 5, 6.25, 8.33, 10, 12.5, 16.7, 20, 25, 33, 49.2, 96.2 */
    100, 80, 64, 50, 40, 32, 25, 20, 16, 12, 10, 8, 6, 5, 4, 3, 2, 1
};

#include "drivers/lcd-scroll.c"

#ifdef HAVE_REMOTE_LCD
/* copied from lcd-remote-1bit.c */
/* Compile 1 bit vertical packing LCD driver for remote LCD */
#undef LCDFN
#define LCDFN(fn) lcd_remote_ ## fn
#undef LCDM
#define LCDM(ma) LCD_REMOTE_ ## ma

#include "drivers/lcd-scroll.c"
#endif /* HAVE_REMOTE_LCD */

#if defined(HAVE_REMOTE_LCD) && !defined(BOOTLOADER)
static struct event_queue scroll_queue SHAREDBSS_ATTR;

static void sync_display_ticks(void)
{
    lcd_scroll_info.last_scroll =
    lcd_remote_scroll_info.last_scroll = current_tick;
}

static bool scroll_process_message(int delay)
{
    struct queue_event ev;

    /* just poll once for negative delays */
    if (delay < 0)
        delay = TIMEOUT_NOBLOCK;

    do
    {
        long tick = current_tick;
        queue_wait_w_tmo(&scroll_queue, &ev, delay);

        switch (ev.id)
        {
        case SYS_TIMEOUT:
            return false;
        case SYS_USB_CONNECTED:
            usb_acknowledge(SYS_USB_CONNECTED_ACK);
            usb_wait_for_disconnect(&scroll_queue);
            sync_display_ticks();
            return true;
#if (CONFIG_PLATFORM & PLATFORM_NATIVE)
        case SYS_REMOTE_PLUGGED:
            if (!remote_initialized)
                sync_display_ticks();
#endif
        }

        delay -= current_tick - tick;
    }
    while (delay > 0);

    return false;
}
#endif /* HAVE_REMOTE_LCD && !BOOTLOADER */

#if !defined(BOOTLOADER)
static void scroll_thread(void);
static const char scroll_name[] = "scroll";
static void scroll_thread(void) NORETURN_ATTR;

#ifdef HAVE_REMOTE_LCD
static void scroll_thread(void)
{
    enum
    {
        SCROLL_LCD        = 0x1,
        SCROLL_LCD_REMOTE = 0x2,
    };

    sync_display_ticks();

    while ( 1 )
    {
        long delay;
        int scroll;
        long tick_lcd, tick_remote;

        tick_lcd = lcd_scroll_info.last_scroll + lcd_scroll_info.ticks;
        delay = current_tick;

        if (
#if (CONFIG_PLATFORM & PLATFORM_NATIVE)
            !remote_initialized ||
#endif
            (tick_remote = lcd_remote_scroll_info.last_scroll +
                           lcd_remote_scroll_info.ticks,
             TIME_BEFORE(tick_lcd, tick_remote)))
        {
            scroll = SCROLL_LCD;
            delay = tick_lcd - delay;
        }
        /* TIME_BEFORE(tick_remote, tick_lcd) */
        else if (tick_lcd != tick_remote)
        {
            scroll = SCROLL_LCD_REMOTE;
            delay = tick_remote - delay;
        }
        else
        {
            scroll = SCROLL_LCD | SCROLL_LCD_REMOTE;
            delay = tick_lcd - delay;
        }

        if (scroll_process_message(delay))
            continue;

        if (scroll & SCROLL_LCD)
        {
#if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
            if (lcd_active())
#endif
                lcd_scroll_worker();
            lcd_scroll_info.last_scroll = current_tick;
        }

        if (scroll == (SCROLL_LCD | SCROLL_LCD_REMOTE))
            yield();

        if (scroll & SCROLL_LCD_REMOTE)
        {
            lcd_remote_scroll_worker();
            lcd_remote_scroll_info.last_scroll = current_tick;
        }
    }
}
#else /* !HAVE_REMOTE_LCD */
static void scroll_thread(void)
{
    while (1)
    {
        sleep(lcd_scroll_info.ticks);
#if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
        if (lcd_active())
#endif
            lcd_scroll_worker();
    }
}
#endif /* !HAVE_REMOTE_LCD */

void scroll_init(void)
{
    static char scroll_stack[DEFAULT_STACK_SIZE*3];
#ifdef HAVE_REMOTE_LCD
    queue_init(&scroll_queue, true);
#endif
    create_thread(scroll_thread, scroll_stack,
                  sizeof(scroll_stack), 0, scroll_name
                  IF_PRIO(, PRIORITY_USER_INTERFACE)
                  IF_COP(, CPU));
}
#else /* BOOTLOADER */
void scroll_init(void)
{
    /* DUMMY */
}
#endif /* BOOTLOADER*/