-
Notifications
You must be signed in to change notification settings - Fork 0
/
seg_racer.c
432 lines (333 loc) · 10.6 KB
/
seg_racer.c
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#include _ADC_HEADER_
// #include <stdio.h>
#include <stdlib.h> //rand()
void racer_init(void)
{
adc_takeInput(6);
adc_init();
adc_select(6);
}
//For converting an integer to text-characters without using sprintf...
// Call once with the value to be written, and newVal TRUE
// then call repeatedly with the same value and newVal FALSE
// in order to grab each decimal digit in an entire uint32 from left to
// right.
//Returns 0-9 or -1 if we've reached 0...
int8_t grabDecimal_u32(uint32_t value, uint8_t newVal)
{
static uint32_t divisor = 1000000000;
static uint32_t thisVal;
if(newVal)
{
divisor = 1000000000;
thisVal = value;
}
uint8_t decimal = 0;
if(divisor == 0)
return -1;
while(thisVal >= divisor)
{
decimal++;
thisVal -= divisor;
}
divisor /= 10;
return decimal;
}
//Including raceWidth, and maybe some buffer...
#define RACE_WIDTH_PSEGS 272
//Returns the number of psegs drawn...
uint8_t showScore(uint16_t rowNum, uint32_t score, uint8_t color)
{
//The first few rows aren't displaying?
// rowNum-=6;
int8_t decimal;
uint8_t scoreFound = FALSE;
static uint32_t thisScore;
if(rowNum == 0)
thisScore = score; // goodCount;
decimal = grabDecimal_u32(thisScore, 1);
uint8_t col;
uint8_t charRow = 0;
uint8_t psegs = 0;
for(col=0; col<NUM_PSEGS-3-RACE_WIDTH_PSEGS; col++)
{
psegs++;
// uint8_t charRow = 0;
if(col%8==0)
{
if((decimal != 0) && (decimal != -1))
scoreFound = TRUE;
if((decimal == -1) || (!scoreFound))
charRow = 0;
else
charRow = getCharRow(decimal+'0', rowNum/2);
decimal = grabDecimal_u32(thisScore, 0);
}
if(getbit(col%8, charRow))
addSegfb(1,color);
else
addSegfb(1,_B);
}
return psegs;
}
void racer_loadRow(uint16_t rowNum)
{
#define NEXT_COLOR _R
#define THIS_COLOR _G
#define OVERLAP_COLOR _Y
#define BG_COLOR _C
#define BG_BAD_COLOR _M
int16_t racerPseg = NUM_PSEGS/2 - 2;
adc_startConversion();
while(adc_isBusy())
asm("nop;");
racerPseg = (adc_getValue()>>2)+5;
if(racerPseg > NUM_PSEGS-2)
racerPseg = NUM_PSEGS-2;
segClear();
addSegfb(1, _W);
static uint16_t raceWidth = 16;
static uint8_t trackPosition = 0;
#define TRACK_LENGTH 24 //24*32 = 768
#define TRACK_ROWS (V_COUNT/TRACK_LENGTH)
static uint16_t raceTrack[TRACK_LENGTH*2];
uint8_t bgColor = BG_COLOR;
//racePseg += ((rand()&0xff0)>>4)*2/0xff - 1;
uint8_t thisTrackRow = rowNum/TRACK_ROWS; //(V_COUNT/TRACK_LENGTH);
uint8_t thisPosition = thisTrackRow + (trackPosition? TRACK_LENGTH : 0);
uint8_t nextPosition = thisTrackRow + (trackPosition? 0 : TRACK_LENGTH);
static hfm_t thisSmoothingHFM, nextSmoothingHFM;
static uint8_t thisTrackGrowing, nextTrackGrowing;
/*
static theta_t racerTheta = 0;
if(racerTheta >= SINE_2PI)
racerTheta -= SINE_2PI;
racerPseg = sineRaw8(racerTheta) + -(INT8_MIN);
racerTheta++;
*/
uint8_t outOfTrack = FALSE;
typedef struct _IST_BLAH_
{
uint16_t length;
uint8_t color;
} intermediateSeg_t;
#define ISEGS_PER_WHATEVER 3
#define IS_SEGS (5*ISEGS_PER_WHATEVER)
intermediateSeg_t is[IS_SEGS] =
{ {0,_W}, {0,_W}, {0,_W}, {0,_W}, {0,_W},
{0,_W}, {0,_W}, {0,_W}, {0,_W}, {0,_W},
{0,_W}, {0,_W}, {0,_W}, {0,_W}, {0,_W} };
uint8_t isPos = 0;
#define addSegIS(len, c) \
({\
is[isPos].length = (len); \
is[isPos].color = (c); \
isPos+=ISEGS_PER_WHATEVER; \
{}; \
})
int8_t prevNextPosition;
int8_t prevThisPosition;
static uint16_t thisIS, nextIS;
static uint8_t level = 1;
if(rowNum%TRACK_ROWS == 0) //(V_COUNT/TRACK_LENGTH) == 0)
{
prevNextPosition = nextPosition - 1;
if(prevNextPosition < 0)
prevNextPosition = TRACK_LENGTH*2 - 1; //+prevNextPosition;
prevThisPosition = thisPosition - 1;
if(prevThisPosition < 0)
prevThisPosition = TRACK_LENGTH*2 - 1; //+prevThisPosition;
static theta_t racerTheta=0;
int16_t nextTrackPseg;
nextTrackPseg = sineRaw8(racerTheta) + -(INT8_MIN);
racerTheta+=level;
if(racerTheta >= SINE_2PI)
racerTheta-=SINE_2PI;
/*
int8_t randomVal = rand()&0x03;
switch(randomVal)
{
case 1:
if(nextTrackPseg < NUM_PSEGS-2-raceWidth)
nextTrackPseg += 1;
break;
case 2:
if(nextTrackPseg > 1)
nextTrackPseg -= 1;
break;
default:
break;
}
*/
raceTrack[nextPosition] = nextTrackPseg;
thisIS = raceTrack[prevThisPosition];
nextIS = raceTrack[prevNextPosition];
uint8_t nextHFMdist, thisHFMdist;
if(raceTrack[nextPosition] > nextIS)
{
nextTrackGrowing = TRUE;
nextHFMdist = raceTrack[nextPosition] - nextIS;
}
else
{
nextTrackGrowing = FALSE;
nextHFMdist = nextIS - raceTrack[nextPosition];
}
if(raceTrack[thisPosition] > thisIS)
{
thisTrackGrowing = TRUE;
thisHFMdist = raceTrack[thisPosition] - thisIS;
}
else
{
thisTrackGrowing = FALSE;
thisHFMdist = thisIS - raceTrack[thisPosition];
}
hfm_setup(&nextSmoothingHFM, nextHFMdist, TRACK_ROWS);
hfm_setup(&thisSmoothingHFM, thisHFMdist, TRACK_ROWS);
}
if(thisTrackGrowing)//raceTrack[thisPosition] > raceTrack[prevThisPosition])
thisIS += hfm_nextOutput(&thisSmoothingHFM);
else
thisIS -= hfm_nextOutput(&thisSmoothingHFM);
if(nextTrackGrowing)//raceTrack[nextPosition] > raceTrack[prevNextPosition])
nextIS += hfm_nextOutput(&nextSmoothingHFM);
else
nextIS -= hfm_nextOutput(&nextSmoothingHFM);
if(thisIS < nextIS)
{
addSegIS(thisIS, bgColor);
//Tracks overlap
if(nextIS < thisIS + raceWidth)
{
addSegIS(nextIS - thisIS, THIS_COLOR);
addSegIS(thisIS + raceWidth - nextIS, OVERLAP_COLOR);
addSegIS(nextIS - thisIS, NEXT_COLOR);
//outOfTrack=TRUE;
}
else
{
addSegIS(raceWidth, THIS_COLOR);
addSegIS(nextIS - thisIS - raceWidth, bgColor);
addSegIS(raceWidth, NEXT_COLOR);
}
//addSegIS(NUM_PSEGS-2-nextIS-raceWidth, bgColor);
addSegIS(RACE_WIDTH_PSEGS-nextIS-raceWidth, bgColor);
}
else if(thisIS > nextIS)
{
addSegIS(nextIS, bgColor);
//Tracks overlap
if(thisIS < nextIS + raceWidth)
{
addSegIS(thisIS - nextIS, NEXT_COLOR);
addSegIS(nextIS + raceWidth - thisIS, OVERLAP_COLOR);
addSegIS(thisIS - nextIS, THIS_COLOR);
//outOfTrack=TRUE;
}
else
{
addSegIS(raceWidth, NEXT_COLOR);
addSegIS(thisIS - nextIS - raceWidth, bgColor);
addSegIS(raceWidth, THIS_COLOR);
}
//addSegIS(NUM_PSEGS-2-thisIS-raceWidth, bgColor);
addSegIS(RACE_WIDTH_PSEGS-thisIS-raceWidth, bgColor);
}
else //Positions equal
{
//outOfTrack=TRUE;
addSegIS(thisIS, bgColor);
addSegIS(raceWidth, OVERLAP_COLOR);
//addSegIS(NUM_PSEGS-2-thisIS-raceWidth, bgColor);
addSegIS(RACE_WIDTH_PSEGS-thisIS-raceWidth, bgColor);
}
/*
addSegfb(raceWidth, _W);
addSegfb(NUM_PSEGS-racePseg-raceWidth-2, _R);
*/
uint8_t isSeg;
uint16_t isSegTrackPos = 0;
uint16_t oldTrackPos = 0;
//uint8_t racerFound = FALSE;
for(isSeg = 0; isSeg<IS_SEGS; isSeg++)
{
isSegTrackPos += is[isSeg].length;
//if(!racerFound &&
if((isSegTrackPos >= (uint16_t)racerPseg))
{
//racerFound = TRUE;
uint16_t oldLength = is[isSeg].length;
is[isSeg].length = racerPseg - oldTrackPos;
is[isSeg+1].length = 1;
is[isSeg+1].color = _K;
//Not sure why this shouldn't be -1...
is[isSeg+2].length = oldLength - is[isSeg].length; //- 1;
is[isSeg+2].color = is[isSeg].color;
if((is[isSeg].color != THIS_COLOR) && (is[isSeg].color != OVERLAP_COLOR))
outOfTrack = TRUE;
isSeg++;
break;
}
oldTrackPos = isSegTrackPos;
}
for(isSeg = 0; isSeg<IS_SEGS; isSeg++)
{
if(is[isSeg].length == 0)
continue;
if(outOfTrack && (is[isSeg].color==BG_COLOR))
addSegfb(is[isSeg].length, BG_BAD_COLOR);
else
addSegfb(is[isSeg].length, is[isSeg].color);
}
static uint32_t badCount=0;
static uint32_t goodCount=0;
if(outOfTrack)
badCount++;
else
goodCount++;
addSegfb(1, _B);
uint8_t percentage = goodCount*100/(badCount+goodCount);
static uint32_t lastLevelUpdate = 0;
if((percentage > 50) && (goodCount - lastLevelUpdate > V_COUNT*3))
{
lastLevelUpdate = goodCount;
level++;
}
uint8_t scorePsegs = 0;
if((rowNum>=6) && (rowNum < 22))
scorePsegs = showScore(rowNum-6, level, _C);
else if((rowNum>=24) && (rowNum <40))
scorePsegs = showScore(rowNum-24, goodCount, _G);
else if((rowNum >= 42) && rowNum < 58)
scorePsegs = showScore(rowNum-42, badCount, _R);
else if((rowNum >= 60) && (rowNum < 76))
scorePsegs = showScore(rowNum-60, percentage, _Y);
//The addition of -scorePsegs, below, seems to have cleared up a weird
// problem... the entire display looked right, except some of the
// characters were printing incorrectly (as solid blocks, repeated first
// row?)
// Sometimes these extended-DE's (that extend past the screen)
// cause weirdness like rows which print only half-way then repeat the
// previous row, for the second half.
// Only odd thing remaining, is that it seems to be one pseg too short
// (displaying a column of white next to the score)
// Whatev.
// Also, there's some oddities when viewing with PLL_SYSCLK
// (weird green and magenta lines in the unwritten space)
// Maybe related to similar issues..? ROW_CALC_DELAY too low?
// Yeahp, ROW_CALCULATION_CYCS needs to be >20000, (currently 40000)
//scorePsegs=0;
// Actually, the column of white seems more related to calc-cycs too low
// Actually, 40000 fixed both oddities, (and maybe woulda affected the
// blockiness as well?)
// Hard to test conclusively...
//This should be varied according to the remaining space on the screen
// in order to keep DE constant.
// (it isn't currently, and in fact draws off-screen on showScore lines)
addSegfb(NUM_PSEGS-2-RACE_WIDTH_PSEGS-scorePsegs, _B);
addSegfb(1, _W);
segTerminate();
if(rowNum >= V_COUNT-1)
trackPosition = !trackPosition;
}