Subversion Repositories freemyipod

Rev

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"
27
#include "format.h"
14 theseven 28
#include "thread.h"
2 theseven 29
#include <stdio.h>
30
#include <stdarg.h>
31
#include <stdbool.h>
32
#include <limits.h>
33
 
34
 
35
struct for_cprintf
36
{
37
    unsigned int consoles;
38
    size_t bytes;
39
};
40
 
41
 
14 theseven 42
struct mutex console_mutex;
43
 
44
 
45
void console_init()
46
{
47
    mutex_init(&console_mutex);
48
}
49
 
50
void cputc_internal(unsigned int consoles, char string) ICODE_ATTR;
51
void cputc_internal(unsigned int consoles, char string)
52
{
53
    if (consoles & 1) lcdconsole_putc(string, 0, -1);
54
}
55
 
2 theseven 56
static int cprfunc(void* ptr, unsigned char letter)
57
{
58
    struct for_cprintf* pr = (struct for_cprintf*)ptr;
14 theseven 59
    cputc_internal(pr->consoles, letter);
2 theseven 60
    pr->bytes++;
61
    return true;
62
}
63
 
64
int cprintf(unsigned int consoles, const char* fmt, ...)
65
{
66
    va_list ap;
67
    struct for_cprintf pr;
68
 
69
    pr.consoles = consoles;
70
    pr.bytes = 0;
71
 
14 theseven 72
    mutex_lock(&console_mutex, TIMEOUT_BLOCK);
2 theseven 73
    va_start(ap, fmt);
74
    format(cprfunc, &pr, fmt, ap);
75
    va_end(ap);
14 theseven 76
    mutex_unlock(&console_mutex);
2 theseven 77
 
78
    return pr.bytes;
79
}
80
 
81
int cvprintf(unsigned int consoles, const char* fmt, va_list ap)
82
{
83
    struct for_cprintf pr;
84
 
85
    pr.consoles = consoles;
86
    pr.bytes = 0;
87
 
14 theseven 88
    mutex_lock(&console_mutex, TIMEOUT_BLOCK);
2 theseven 89
    format(cprfunc, &pr, fmt, ap);
14 theseven 90
    mutex_unlock(&console_mutex);
2 theseven 91
 
92
    return pr.bytes;
93
}
94
 
95
void cputc(unsigned int consoles, char string)
96
{
14 theseven 97
    mutex_lock(&console_mutex, TIMEOUT_BLOCK);
98
    cputc_internal(consoles, string);
99
    mutex_unlock(&console_mutex);
2 theseven 100
}
101
 
102
void cputs(unsigned int consoles, const char* string)
103
{
14 theseven 104
    mutex_lock(&console_mutex, TIMEOUT_BLOCK);
105
    if (consoles & 1) lcdconsole_puts(string, 0, -1);
106
    mutex_unlock(&console_mutex);
2 theseven 107
}
108
 
109
void cflush(unsigned int consoles)
110
{
14 theseven 111
    mutex_lock(&console_mutex, TIMEOUT_BLOCK);
112
    if (consoles & 1) lcdconsole_update();
113
    mutex_unlock(&console_mutex);
2 theseven 114
}