-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbacklog.cpp
191 lines (156 loc) · 4.21 KB
/
backlog.cpp
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
#include "Arduino.h"
#include "backlog.h"
/*! \brief Constructor, init everything to zero.
*/
Backlog::Backlog()
{
pCurrentInputString = NULL;
pputs = NULL;
BacklogOutputPointer = 0;
for (int i = 0; i < BACKLOG_STEPS; i++)
StringBacklog[i] = NULL;
}
/*! \brief Set the puts callback.
Set the puts callback function, in our case the output to the II keyb interface.
*/
void Backlog::SetPuts(int (*pputs_funcp)(const char *string))
{
pputs = pputs_funcp;
}
/*! \brief Erase the current input on the screen.
The current typed input is erased with backspaces.
*/
void Backlog::EraseCurrent(void)
{
uint16_t bufferlen = strlen(pCurrentInputString);
for (int i = 0; i < bufferlen; i++)
{
if (pputs)
{
pputs(0x08); //Backspace
delay(CHAR_OUT_DELAY);
}
}
}
/*! \brief Output the current string vie puts.
The current active string ist typed to the screen via puts.
*/
void Backlog::OutputCurrent(void)
{
uint16_t bufferlen = strlen(pCurrentInputString);
for (int i = 0; i < bufferlen; i++)
{
if (pputs)
{
pputs(pCurrentInputString[i]);
delay(CHAR_OUT_DELAY);
}
}
}
/*! \brief Go one string back in the backlog.
Erases the current input from the screen and goes one back ion the log.
*/
void Backlog::OneBack(void)
{
uint8_t uiNewIndex = BacklogOutputPointer;
ChangeToIndex(uiNewIndex);
uiNewIndex++;
if (uiNewIndex > BACKLOG_STEPS - 1)
uiNewIndex = BACKLOG_STEPS - 1;
BacklogOutputPointer = uiNewIndex;
}
/*! \brief Go one string forward in the backlog.
Erases the current input from the screen and goes one foreward ion the log.
*/
void Backlog::OneForeward(void)
{
uint8_t uiNewIndex = BacklogOutputPointer;
if (uiNewIndex)
uiNewIndex--;
ChangeToIndex(uiNewIndex);
BacklogOutputPointer = uiNewIndex;
}
/*! \brief Change the screen to a specific index from the backlog.
Changes the active display to a specific string from the backlog.
*/
void Backlog::ChangeToIndex(uint8_t uiNewIndex)
{
EraseCurrent();
delete pCurrentInputString;
if (StringBacklog[uiNewIndex])
{
pCurrentInputString = new char[BUFFER_STEPS + (BUFFER_STEPS * (strlen(StringBacklog[uiNewIndex]) / BUFFER_STEPS)) ];
strcpy(pCurrentInputString, StringBacklog[uiNewIndex]);
OutputCurrent();
}
}
/*! \brief Stores the current string in the backlog.
The current string is stored in the backlog and the log is advanced one step.
*/
void Backlog::BacklogCurrString(void)
{
BacklogOutputPointer = 0;
if (StringBacklog[BACKLOG_STEPS - 1])
delete StringBacklog[BACKLOG_STEPS - 1];
for (int i = BACKLOG_STEPS - 1; i > 0; i--)
{
StringBacklog[i] = StringBacklog[i - 1];
}
StringBacklog[0] = pCurrentInputString;
pCurrentInputString = NULL;
#if 0
Serial.println("Current Backlog:");
for (int i = 0; i < BACKLOG_STEPS; i++)
{
if (StringBacklog[i])
Serial.println(StringBacklog[i]);
}
#endif
}
/*! \brief If the current typed string exceeds BUFFER_STEPS the buffer is increased.
*
If the current string gets bigger than BUFFER_STEPS another BUFFER_STEPS is added.
*/
void Backlog::IncreaseBuffer(void)
{
if (NULL == pCurrentInputString)
{
pCurrentInputString = new char[BUFFER_STEPS];
memset(pCurrentInputString, 0, BUFFER_STEPS);
}
else
{
uint16_t bufferlen = strlen(pCurrentInputString);
if (((bufferlen % BUFFER_STEPS) == (BUFFER_STEPS - 2)) && (bufferlen > 0))
{
char *pnewCurrentInputString = new char[bufferlen + BUFFER_STEPS];
memcpy(pnewCurrentInputString, pCurrentInputString, bufferlen + 1);
delete [] pCurrentInputString;
pCurrentInputString = pnewCurrentInputString;
}
}
}
/*! \brief Add one typed char to the current input string.
*
On char is added, or if 0x0d is received, the string is stored and the backlog advanced.
*/
void Backlog::addchar(char c)
{
IncreaseBuffer();
if (0x0d == c) // Finished
{
BacklogCurrString();
}
else if (0x08 == c) // Backspace)
{
uint16_t bufferlen = strlen(pCurrentInputString);
if (bufferlen)
pCurrentInputString[bufferlen - 1] = 0x00;
}
else
{
uint16_t bufferlen = strlen(pCurrentInputString);
pCurrentInputString[bufferlen] = c;
pCurrentInputString[bufferlen + 1] = 0x00;
}
}