summaryrefslogtreecommitdiff
path: root/source/display/console.cpp
blob: 411b5243bd73de620395acca8450d00780e45e3b (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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#include "display/console.h"

#include <cinttypes>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstring>

#include "tp/JFWSystem.h"

namespace libtp::display
{
    Console::Console( uint8_t protection ): m_Protect( protection )
    {
        // Init
        libtp::display::setConsole( true, 25 );

        this->m_Line = 0;
        this->m_Col = 0;

        // Display initial information, then lock line 1 (could be overwritten manually)
        *this << _PROJECT_NAME " - v" _VERSION " " _VARIANT << "\n"
              << "Powered by github/zsrtp/libtp_rel"
              << "\n"
              << "Created by AECX & Zephiles"
              << "\n"
              << "Build ID: " << _BUILDID << "\n"
              << "Version: " << _VERSION " " _VARIANT "\n\n";
    }

    void Console::parse( const char* text )
    {
        // Store the console pointer once
        tp::jfw_system::SystemConsole* console = tp::jfw_system::systemConsole;

        while ( *text != '\0' )
        {
            if ( *text == '\n' )
            {
                this->m_Col = 0;
                setLine( m_Line + 1 );
            }
            else if ( *text == '\r' )
            {
                this->m_Col = 0;
            }
            else
            {
                // Adjust if otherwise out of bounds
                if ( this->m_Col >= 48 )
                {
                    // Null terminate this line
                    console->consoleLine[this->m_Line].line[this->m_Col] = '\0';

                    setLine( m_Line + 1 );
                    this->m_Col = 0;
                }

                // Print to current line and col
                console->consoleLine[this->m_Line].line[this->m_Col] = *text;
                this->m_Col++;
            }

            // Advance the pointer
            text++;
        }     // End of while

        console->consoleLine[this->m_Line].line[this->m_Col] = '\0';
    }

    void Console::setLine( uint8_t line )
    {
        tp::jfw_system::SystemConsole* console = tp::jfw_system::systemConsole;

        // Clear following lines
        if ( line < m_Line )
        {
            display::clearConsole( line, 0 );
        }

        m_Line = line;

        if ( this->m_Line >= 25 )
        {
            this->m_Line = 24;

            // Move all lines up by one starting at protected + 1
            for ( uint8_t i = m_Protect + 1; i < 25; i++ )
            {
                strncpy( console->consoleLine[i - 1].line, console->consoleLine[i].line, 48 );
            }
        }
    }

    Console& operator<<( Console& console, const char* text )
    {
        console.parse( text );
        return console;
    }

    Console& operator<<( Console& console, char* text )
    {
        console.parse( text );
        return console;
    }

    Console& operator<<( Console& console, char chr )
    {
        char buf[1];
        buf[0] = chr;
        console.parse( buf );
        return console;
    }

    Console& operator<<( Console& console, uint8_t n )
    {
        // Custom print this
        char buf[15];

        sprintf( buf, "%02" PRIx8, n );
        console.parse( buf );

        return console;
    }

    Console& operator<<( Console& console, int8_t n )
    {
        // Custom print this
        char buf[15];

        sprintf( buf, "%" PRId8, n );
        console.parse( buf );

        return console;
    }

    Console& operator<<( Console& console, uint16_t n )
    {
        // Custom print this
        char buf[15];

        sprintf( buf, "%04" PRIx16, n );
        console.parse( buf );

        return console;
    }

    Console& operator<<( Console& console, int16_t n )
    {
        // Custom print this
        char buf[15];

        sprintf( buf, "%" PRId16, n );
        console.parse( buf );

        return console;
    }

    Console& operator<<( Console& console, uint32_t n )
    {
        // Custom print this
        char buf[15];

        sprintf( buf, "%08" PRIx32, n );
        console.parse( buf );

        return console;
    }

    Console& operator<<( Console& console, int32_t n )
    {
        // Custom print this
        char buf[15];

        sprintf( buf, "%" PRId32, n );
        console.parse( buf );

        return console;
    }

    Console& operator<<( Console& console, uint64_t n )
    {
        return console << static_cast<uint32_t>( n >> 32 ) << static_cast<uint32_t>( n );
    }

    Console& operator<<( Console& console, int64_t n )
    {
        return console << static_cast<int32_t>( n >> 32 ) << static_cast<int32_t>( n );
    }

    Console& operator<<( Console& console, float n )
    {
        // Custom print this
        char buf[15];

        // Is there a cinttype for float?
        sprintf( buf, "%.4f", n );
        console.parse( buf );

        return console;
    }

    Console& operator<<( Console& console, void* ptr )
    {
        // Custom print this
        char buf[15];

        sprintf( buf, "0x%" PRIxPTR, reinterpret_cast<uint32_t>( ptr ) );
        console.parse( buf );

        return console;
    }

    void clearConsole( uint8_t from, uint8_t count )
    {
        // Load the console as a local pointer to avoid loading it each loop
        tp::jfw_system::SystemConsole* console = tp::jfw_system::systemConsole;

        // If count is 0 we want to clear all lines; -1 because last line is debug info
        std::size_t linecount =
            sizeof( tp::jfw_system::SystemConsole::consoleLine ) / sizeof( tp::jfw_system::ConsoleLine ) - 1;

        if ( from > linecount )
        {
            from = 0;
        }

        // count = 0 ?              - remaining lines
        // from + count > linecount - remaining lines
        // else                     - count
        count = ( count == 0 ? ( linecount - from ) : ( ( from + count ) > linecount ? ( linecount - from ) : count ) );

        std::size_t lineLength = sizeof( tp::jfw_system::ConsoleLine::line );

        for ( uint8_t i = from; i < from + count; i++ )
        {
            memset( console->consoleLine[i].line, 0x0, lineLength );
        }

        return;
    }

    void setConsole( bool state, uint8_t lines )
    {
        // Load the console as a local pointer to avoid loading it each loop
        tp::jfw_system::SystemConsole* console = tp::jfw_system::systemConsole;

        console->consoleEnabled = state;

        for ( uint8_t line = 0; line < lines; line++ )
        {
            console->consoleLine[line].showLine = state;
        }
    }

    void setConsoleColor( uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha )
    {
        // Load the console as a local pointer to save memory on each use
        tp::jfw_system::SystemConsole* console = tp::jfw_system::systemConsole;

        console->consoleColor[0] = red;
        console->consoleColor[1] = green;
        console->consoleColor[2] = blue;
        console->consoleColor[3] = alpha;
        return;
    }

    char* print( uint8_t line, const char* text )
    {
        // Get the max size minus one, to insure that the last char is always NULL
        std::size_t max = sizeof( tp::jfw_system::ConsoleLine::line ) - 1;
        return strncpy( tp::jfw_system::systemConsole->consoleLine[line].line, text, max );
    }
}     // namespace libtp::display