-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdisplayscore.cpp
247 lines (218 loc) · 6.56 KB
/
displayscore.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
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
#include <Arduino.h>
#include <avr/pgmspace.h>
#include "displayscore.h"
#include "smallFont.h"
#include <EEPROM.h>
// Video text memory:
// - one line (of 32 characters) in standard mode,
// - four lines (of 16 characters) in zoomed mode
static uint8_t textBuffer[64];
const uint8_t HISCORE_MAGIC = 42;
/*--------------------------------------------------------------*/
void resetScore()
{
score = 0;
}
/*--------------------------------------------------------------*/
void setScore( SCORE_TYPE points )
{
score = points;
}
/*--------------------------------------------------------------*/
SCORE_TYPE getScore()
{
return( score );
}
/*--------------------------------------------------------------*/
bool updateHighScorePoints()
{
if ( score > getHighScorePoints() )
{
setHighScorePoints( score );
return( true );
}
return( false );
}
/*--------------------------------------------------------------*/
void setHighScorePoints( SCORE_TYPE points )
{
_hiScore.score = points;
}
/*--------------------------------------------------------------*/
SCORE_TYPE getHighScorePoints()
{
return( _hiScore.score );
}
/*--------------------------------------------------------------*/
// simple checksum by adding all values which should result in 0x42, not a real CRC ;)
uint8_t calcHighScoreCRC()
{
uint8_t sum = 0;
uint8_t *hiScore = (uint8_t *)&_hiScore;
uint8_t *hiScoreEnd = hiScore + sizeof( _hiScore );
// sum up all values
while ( hiScore < hiScoreEnd )
{
sum += *hiScore++;
}
return( sum );
}
/*--------------------------------------------------------------*/
// Attention: The returned string has a fixed size of 3 bytes
// and is *not* terminated with zero!
uint8_t *getHighScoreName()
{
return( _hiScore.name );
}
/*--------------------------------------------------------------*/
void initHighScoreStruct( uint16_t gameAddr )
{
// read stored structure to _hiScore variable
readHighScoreFromEEPROM( gameAddr );
// structure not valid? (no former hiscore stored)
if ( calcHighScoreCRC() != HISCORE_MAGIC )
{
// initialize with some default values
_hiScore.score = 0;
memset( _hiScore.name, '?', sizeof(_hiScore.name) );
_hiScore.crcFix = 0;
// and store it (CRC is calculated while storing)
storeHighScoreToEEPROM( gameAddr );
}
}
/*--------------------------------------------------------------*/
// read HISCORE struct from EEPROM
void readHighScoreFromEEPROM( uint16_t gameAddr )
{
uint8_t *hiScore = (uint8_t *)&_hiScore;
uint8_t *hiScoreEnd = hiScore + sizeof( _hiScore );
while ( hiScore < hiScoreEnd )
{
*hiScore++ = EEPROM.read( gameAddr++ );
}
}
/*--------------------------------------------------------------*/
// store HISCORE struct to EEPROM
void storeHighScoreToEEPROM( uint16_t gameAddr )
{
// generate new value to adjust the sum of all bytes to 0x42,
// so just set the crcFix to '0' and sum up the rest
_hiScore.crcFix = 0;
_hiScore.crcFix = HISCORE_MAGIC - calcHighScoreCRC();
uint8_t *hiScore = (uint8_t *)&_hiScore;
uint8_t *hiScoreEnd = hiScore + sizeof( _hiScore );
while ( hiScore < hiScoreEnd )
{
EEPROM.update( gameAddr++, *hiScore++ );
}
}
/*--------------------------------------------------------------*/
void addScore( SCORE_TYPE points )
{
score += points;
}
/*--------------------------------------------------------------*/
// Converts 'value' to 5 decimal digits (if SCORE_TYPE equals uint16_t)
// Not the most elegant version, but the shortest for uint16_t
void convertValueToDigits( SCORE_TYPE value, uint8_t *digits )
{
static SCORE_TYPE dividerList[] = { 10000, 1000, 100, 10, 1, 0 };
SCORE_TYPE *divider = dividerList;
// wait for the first divider which is smaller than the value
while ( *divider > value ) { divider++; }
// if we reach zero, the divider pointer has to be reset to '1'
if ( value == 0 ) { divider = ÷rList[4]; }
do
{
uint8_t digit = '0';
while( value >= *divider )
{
digit++;
value -= *divider;
}
// store digit
*digits++ = digit;
// next divider
divider++;
}
while ( *divider != 0 );
}
/*--------------------------------------------------------------*/
// Displays a line of ASCII character from the smallFont in the
// top line of the screen. To save flash memory, the font ranges
// only from '0' to 'Z'.
uint8_t displayText( uint8_t x, uint8_t y )
{
// top line?
if ( y == 0 )
{
// find appropriate character in text array (font width is 4 px)
uint8_t value = textBuffer[x >> 2];
// is it a valid character?
if ( value != 0 )
{
// get the column value
return( pgm_read_byte( characterFont3x5 + ( ( value - '0' ) << 2 ) + ( x & 0x03) ) );
}
}
return( 0x00 );
}
/*--------------------------------------------------------------*/
// Display zoomed ASCII character from the smallFont in four
// lines of 16 characters. The zoom factor is fixed to '2'.
// If bit 7 is set, the character will be displayed inverted.
// To save flash memory, the font ranges only from '0' to 'Z'.
uint8_t displayZoomedText( uint8_t x, uint8_t y )
{
// Find appropriate character in text array:
// Font width is 4 px, zoom is 2x, so fetch a new character every 8 pixels
uint8_t value = textBuffer[((y >> 1) << 4) + ( x >> 3)];
// is it a valid character?
if ( value != 0 )
{
// MSB set? -> inverse video
uint8_t reverse = value & 0x80;
// remove MSB from value
value -= reverse;
// return the column value
value = ( pgm_read_byte( characterFont3x5 + ( ( value - '0' ) << 2 ) + ( ( x >> 1 ) & 0x03 ) ) );
if ( ( y & 0x01 ) == 0 )
{
// upper line
value = ( pgm_read_byte( nibbleZoom + ( value & 0x0f ) ) );
}
else
{
// lower line
value = ( pgm_read_byte( nibbleZoom + ( value >> 4 ) ) );
}
// invert?
if ( reverse )
{
// invert pixels
value = value ^ 0xff;
}
}
// Please move along, there is nothing to be seen here...
return( value );
}
/*--------------------------------------------------------------*/
void clearTextBuffer()
{
memset( textBuffer, 0x00, sizeof( textBuffer ) );
}
/*--------------------------------------------------------------*/
void printText( uint8_t x, const uint8_t *text, uint8_t textLength )
{
memcpy( textBuffer + x, text, textLength );
}
/*--------------------------------------------------------------*/
void pgm_printText( uint8_t x, const uint8_t *text, uint8_t textLength )
{
memcpy_P( textBuffer + x, text, textLength );
}
/*--------------------------------------------------------------*/
uint8_t *getTextBuffer()
{
return( textBuffer );
}