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
//
6
//    This file is part of emBIOS.
7
//
8
//    emBIOS is free software: you can redistribute it and/or
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
//
13
//    emBIOS is distributed in the hope that it will be useful,
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
19
//    with emBIOS.  If not, see <http://www.gnu.org/licenses/>.
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 <stdio.h>
31
#include <stdarg.h>
32
#include <stdbool.h>
33
#include <limits.h>
34
 
35
 
36
struct for_cprintf
37
{
38
    unsigned int consoles;
39
    size_t bytes;
40
};
41
 
42
 
14 theseven 43
struct mutex console_mutex;
44
 
45
 
46
void console_init()
47
{
48
    mutex_init(&console_mutex);
49
}
50
 
51
void cputc_internal(unsigned int consoles, char string) ICODE_ATTR;
52
void cputc_internal(unsigned int consoles, char string)
53
{
54
    if (consoles & 1) lcdconsole_putc(string, 0, -1);
25 theseven 55
    if (consoles & 2) dbgconsole_putc(string);
14 theseven 56
}
57
 
2 theseven 58
static int cprfunc(void* ptr, unsigned char letter)
59
{
60
    struct for_cprintf* pr = (struct for_cprintf*)ptr;
14 theseven 61
    cputc_internal(pr->consoles, letter);
2 theseven 62
    pr->bytes++;
63
    return true;
64
}
65
 
66
int cprintf(unsigned int consoles, const char* fmt, ...)
67
{
68
    va_list ap;
69
    struct for_cprintf pr;
70
 
71
    pr.consoles = consoles;
72
    pr.bytes = 0;
73
 
14 theseven 74
    mutex_lock(&console_mutex, TIMEOUT_BLOCK);
2 theseven 75
    va_start(ap, fmt);
76
    format(cprfunc, &pr, fmt, ap);
77
    va_end(ap);
14 theseven 78
    mutex_unlock(&console_mutex);
2 theseven 79
 
80
    return pr.bytes;
81
}
82
 
83
int cvprintf(unsigned int consoles, const char* fmt, va_list ap)
84
{
85
    struct for_cprintf pr;
86
 
87
    pr.consoles = consoles;
88
    pr.bytes = 0;
89
 
14 theseven 90
    mutex_lock(&console_mutex, TIMEOUT_BLOCK);
2 theseven 91
    format(cprfunc, &pr, fmt, ap);
14 theseven 92
    mutex_unlock(&console_mutex);
2 theseven 93
 
94
    return pr.bytes;
95
}
96
 
97
void cputc(unsigned int consoles, char string)
98
{
14 theseven 99
    mutex_lock(&console_mutex, TIMEOUT_BLOCK);
100
    cputc_internal(consoles, string);
101
    mutex_unlock(&console_mutex);
2 theseven 102
}
103
 
104
void cputs(unsigned int consoles, const char* string)
105
{
14 theseven 106
    mutex_lock(&console_mutex, TIMEOUT_BLOCK);
107
    if (consoles & 1) lcdconsole_puts(string, 0, -1);
25 theseven 108
    if (consoles & 2) dbgconsole_puts(string);
14 theseven 109
    mutex_unlock(&console_mutex);
2 theseven 110
}
111
 
25 theseven 112
void cwrite(unsigned int consoles, const char* string, size_t length)
113
{
114
    mutex_lock(&console_mutex, TIMEOUT_BLOCK);
115
    if (consoles & 1) lcdconsole_write(string, length, 0, -1);
116
    if (consoles & 2) dbgconsole_write(string, length);
117
    mutex_unlock(&console_mutex);
118
}
119
 
2 theseven 120
void cflush(unsigned int consoles)
121
{
14 theseven 122
    mutex_lock(&console_mutex, TIMEOUT_BLOCK);
123
    if (consoles & 1) lcdconsole_update();
124
    mutex_unlock(&console_mutex);
2 theseven 125
}