Subversion Repositories freemyipod

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 theseven 1
//
2
//
3
//    Copyright 2010 TheSeven
4
//
5
//
427 farthen 6
//    This file is part of emCORE.
2 theseven 7
//
427 farthen 8
//    emCORE is free software: you can redistribute it and/or
2 theseven 9
//    modify it under the terms of the GNU General Public License as
10
//    published by the Free Software Foundation, either version 2 of the
11
//    License, or (at your option) any later version.
12
//
427 farthen 13
//    emCORE is distributed in the hope that it will be useful,
2 theseven 14
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
15
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
//    See the GNU General Public License for more details.
17
//
18
//    You should have received a copy of the GNU General Public License along
427 farthen 19
//    with emCORE.  If not, see <http://www.gnu.org/licenses/>.
2 theseven 20
//
21
//
22
 
23
 
24
#include "global.h"
25
#include "console.h"
26
#include "lcdconsole.h"
25 theseven 27
#include "usb/dbgconsole.h"
2 theseven 28
#include "format.h"
14 theseven 29
#include "thread.h"
2 theseven 30
#include <stdarg.h>
31
#include <limits.h>
32
 
33
 
34
struct for_cprintf
35
{
36
    unsigned int consoles;
37
    size_t bytes;
38
};
39
 
40
 
14 theseven 41
struct mutex console_mutex;
26 theseven 42
struct mutex console_readmutex;
14 theseven 43
 
44
 
45
void console_init()
46
{
47
    mutex_init(&console_mutex);
26 theseven 48
    mutex_init(&console_readmutex);
14 theseven 49
}
50
 
51
void cputc_internal(unsigned int consoles, char string) ICODE_ATTR;
52
void cputc_internal(unsigned int consoles, char string)
53
{
85 theseven 54
#ifdef HAVE_LCD
257 theseven 55
    if (consoles & 1) lcdconsole_putc(string, LCDCONSOLE_FGCOLOR, LCDCONSOLE_BGCOLOR);
85 theseven 56
#endif
57
#ifdef HAVE_USB
25 theseven 58
    if (consoles & 2) dbgconsole_putc(string);
85 theseven 59
#endif
14 theseven 60
}
61
 
2 theseven 62
static int cprfunc(void* ptr, unsigned char letter)
63
{
64
    struct for_cprintf* pr = (struct for_cprintf*)ptr;
14 theseven 65
    cputc_internal(pr->consoles, letter);
2 theseven 66
    pr->bytes++;
67
    return true;
68
}
69
 
70
int cprintf(unsigned int consoles, const char* fmt, ...)
71
{
72
    va_list ap;
73
    struct for_cprintf pr;
74
 
75
    pr.consoles = consoles;
76
    pr.bytes = 0;
77
 
14 theseven 78
    mutex_lock(&console_mutex, TIMEOUT_BLOCK);
2 theseven 79
    va_start(ap, fmt);
80
    format(cprfunc, &pr, fmt, ap);
81
    va_end(ap);
14 theseven 82
    mutex_unlock(&console_mutex);
2 theseven 83
 
84
    return pr.bytes;
85
}
86
 
87
int cvprintf(unsigned int consoles, const char* fmt, va_list ap)
88
{
89
    struct for_cprintf pr;
90
 
91
    pr.consoles = consoles;
92
    pr.bytes = 0;
93
 
14 theseven 94
    mutex_lock(&console_mutex, TIMEOUT_BLOCK);
2 theseven 95
    format(cprfunc, &pr, fmt, ap);
14 theseven 96
    mutex_unlock(&console_mutex);
2 theseven 97
 
98
    return pr.bytes;
99
}
100
 
101
void cputc(unsigned int consoles, char string)
102
{
14 theseven 103
    mutex_lock(&console_mutex, TIMEOUT_BLOCK);
104
    cputc_internal(consoles, string);
105
    mutex_unlock(&console_mutex);
2 theseven 106
}
107
 
108
void cputs(unsigned int consoles, const char* string)
109
{
14 theseven 110
    mutex_lock(&console_mutex, TIMEOUT_BLOCK);
85 theseven 111
#ifdef HAVE_LCD
257 theseven 112
    if (consoles & 1) lcdconsole_puts(string, LCDCONSOLE_FGCOLOR, LCDCONSOLE_BGCOLOR);
85 theseven 113
#endif
114
#ifdef HAVE_USB
25 theseven 115
    if (consoles & 2) dbgconsole_puts(string);
85 theseven 116
#endif
14 theseven 117
    mutex_unlock(&console_mutex);
2 theseven 118
}
119
 
25 theseven 120
void cwrite(unsigned int consoles, const char* string, size_t length)
121
{
122
    mutex_lock(&console_mutex, TIMEOUT_BLOCK);
85 theseven 123
#ifdef HAVE_LCD
257 theseven 124
    if (consoles & 1) lcdconsole_write(string, length, LCDCONSOLE_FGCOLOR, LCDCONSOLE_BGCOLOR);
85 theseven 125
#endif
126
#ifdef HAVE_USB
25 theseven 127
    if (consoles & 2) dbgconsole_write(string, length);
85 theseven 128
#endif
25 theseven 129
    mutex_unlock(&console_mutex);
130
}
131
 
2 theseven 132
void cflush(unsigned int consoles)
133
{
14 theseven 134
    mutex_lock(&console_mutex, TIMEOUT_BLOCK);
85 theseven 135
#ifdef HAVE_LCD
14 theseven 136
    if (consoles & 1) lcdconsole_update();
85 theseven 137
#endif
14 theseven 138
    mutex_unlock(&console_mutex);
2 theseven 139
}
26 theseven 140
 
141
int cgetc(unsigned int consoles, int timeout)
142
{
143
    int result;
144
    mutex_lock(&console_readmutex, TIMEOUT_BLOCK);
85 theseven 145
#ifdef HAVE_USB
26 theseven 146
    if ((consoles & 2) && (result = dbgconsole_getc(timeout)) != -1) return result;
85 theseven 147
#endif
214 theseven 148
    mutex_unlock(&console_readmutex);
26 theseven 149
}
150
 
151
int cread(unsigned int consoles, char* buffer, size_t length, int timeout)
152
{
153
    int result;
154
    mutex_lock(&console_readmutex, TIMEOUT_BLOCK);
85 theseven 155
#ifdef HAVE_USB
26 theseven 156
    if ((consoles & 2) && (result = dbgconsole_read(buffer, length, timeout))) return result;
85 theseven 157
#endif
214 theseven 158
    mutex_unlock(&console_readmutex);
26 theseven 159
}
160
 
161
void creada(unsigned int consoles, char* buffer, size_t length, int timeout)
162
{
163
    int result;
164
    mutex_lock(&console_readmutex, TIMEOUT_BLOCK);
165
    while (length)
166
    {
85 theseven 167
#ifdef HAVE_USB
26 theseven 168
        if (length && (consoles & 2) && (result = dbgconsole_read(buffer, length, timeout)))
169
        {
170
            buffer = &buffer[result];
171
            length -= result;
172
        }
85 theseven 173
#endif
26 theseven 174
    }
214 theseven 175
    mutex_unlock(&console_readmutex);
26 theseven 176
}