-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
580 lines (506 loc) · 16.1 KB
/
main.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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
// Main.c
// Runs on LM4F120/TM4C123
// You may use, edit, run or distribute this file
// You are free to change the syntax/organization of this file
// Jonathan W. Valvano 2/20/17, valvano@mail.utexas.edu
// Modified by Sile Shu 10/4/17, ss5de@virginia.edu
// Modified by Mustafa Hotaki 7/29/18, mkh3cf@virginia.edu
// Modified by Joseph Chen, Arjun Deopujari, and Nick Moon - November 2019
// Joseph Chen github page: https://github.com/walrusa/
// Nicholas Moon github page: https://github.com/NicholasMoon
/* **** Did not delete large blocks of commented code in order for viewers to look at original code and make sense of changes **** */
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "OS.h"
#include "tm4c123gh6pm.h"
#include "LCD.h"
#include <string.h>
#include "UART.h"
#include "FIFO.h"
#include "joystick.h"
#include "PORTE.h"
#include "BSP.h"
#include <math.h>
#include <time.h>
// Constants
#define BGCOLOR LCD_BLACK
#define CROSSSIZE 5
#define PERIOD 4000000000 // DAS 20Hz sampling period in system time units
#define PSEUDOPERIOD 8000000
#define LIFETIME 1000
#define RUNLENGTH 600 // 30 seconds run length
Sema4Type LCDFree;
uint16_t origin[2]; // The original ADC value of x,y if the joystick is not touched, used as reference
int16_t x = 63; // horizontal position of the crosshair, initially 63
int16_t y = 63; // vertical position of the crosshair, initially 63
int16_t z = 63;
int16_t prevx, prevy; // Previous x and y values of the crosshair
uint8_t select; // joystick push
uint8_t area[2];
uint32_t PseudoCount;
uint16_t xdata[16] = {0};
uint16_t ydata[16] = {0};
uint16_t zdata[16] = {0};
uint32_t currentXSum;
uint32_t currentYSum;
uint32_t currentZSum;
uint16_t currentIndex;
uint16_t currentXAverage;
uint16_t currentYAverage;
uint16_t currentZAverage;
uint16_t dataPoints = 0;
uint16_t currentRoll = 1;
char xstring[] = {'X', ':', 0x00};
char ystring[] = {'Y', ':', 0x00};
char zstring[] = {'Z', ':', 0x00};
char loading[] = {'L', 'O', 'A', 'D', 'I', 'N', 'G'};
char roll[] = {0x00};
char sideArr[] = {0x00};
char sideNameArr[] = {0x00};
uint8_t steadySamples;
uint8_t rollingSamples;
uint8_t done;
uint8_t sides;
uint8_t inDiceMode;
uint8_t inControlMode;
uint8_t killThreads;
void controlScreen(void);
void bufferState(void);
unsigned long NumCreated; // Number of foreground threads created
unsigned long NumSamples; // Incremented every ADC sample, in Producer
unsigned long UpdateWork; // Incremented every update on position values
unsigned long Calculation; // Incremented every cube number calculation
//---------------------User debugging-----------------------
unsigned long DataLost; // data sent by Producer, but not received by Consumer
long MaxJitter; // largest time jitter between interrupts in usec
#define JITTERSIZE 64
unsigned long const JitterSize=JITTERSIZE;
unsigned long JitterHistogram[JITTERSIZE]={0,};
unsigned long TotalWithI1;
unsigned short MaxWithI1;
void Device_Init(void){
BSP_LCD_Init();
BSP_Joystick_Init();
BSP_LCD_FillScreen(LCD_BLACK);
BSP_Accelerometer_Init();
Output_Init();
}
//------------------Task 1--------------------------------
// background thread executed at 20 Hz
//******** Producer ***************
int UpdatePosition(uint16_t rawx, uint16_t rawy, jsDataType* data){
if (rawx > origin[0]){
x = x + ((rawx - origin[0]) >> 9);
}
else{
x = x - ((origin[0] - rawx) >> 9);
}
if (rawy < origin[1]){
y = y + ((origin[1] - rawy) >> 9);
}
else{
y = y - ((rawy - origin[1]) >> 9);
}
if (x > 127){
x = 127;}
if (x < 0){
x = 0;}
if (y > 120 - CROSSSIZE){
y = 120 - CROSSSIZE;}
if (y < 0){
y = 0;}
data->x = x; data->y = y;
return 1;
}
void Producer(void){
// uint16_t rawX,rawY; // raw adc value
// uint8_t select;
// jsDataType data;
// unsigned static long LastTime; // time at previous ADC sample
// unsigned long thisTime; // time at current ADC sample
// long jitter; // time between measured and expected, in us
// if (NumSamples < RUNLENGTH){
// BSP_Joystick_Input(&rawX,&rawY,&select);
// thisTime = OS_Time(); // current time, 12.5 ns
// UpdateWork += UpdatePosition(rawX,rawY,&data); // calculation work
// NumSamples++; // number of samples
// if(JsFifo_Put(data) == 0){ // send to consumer
// DataLost++;
// }
// //calculate jitter
// if(UpdateWork > 1){ // ignore timing of first interrupt
// unsigned long diff = OS_TimeDifference(LastTime,thisTime);
// if(diff > PERIOD){
// jitter = (diff-PERIOD+4)/8; // in 0.1 usec
// }
// else{
// jitter = (PERIOD-diff+4)/8; // in 0.1 usec
// }
// if(jitter > MaxJitter){
// MaxJitter = jitter; // in usec
// } // jitter should be 0
// if(jitter >= JitterSize){
// jitter = JITTERSIZE-1;
// }
// JitterHistogram[jitter]++;
// }
// LastTime = thisTime;
// }
while (1) {
jsDataType data;
uint16_t rawX, rawY, rawZ; // To hold raw adc values
BSP_Accelerometer_Input(&rawX, &rawY, &rawZ);
data.x = rawX;
data.y = rawY;
data.z = rawZ;
JsFifo_Put(data);
if (!inDiceMode){ NumCreated--; OS_Kill(); }
OS_Suspend();
}
}
//--------------end of Task 1-----------------------------
//------------------Task 2--------------------------------
// background thread executes with SW1 button
// one foreground task created with button push
// foreground treads run for 1 sec and die
// ***********ButtonWork*************
void ButtonWork(void){
uint32_t StartTime,CurrentTime,ElapsedTime;
StartTime = OS_MsTime();
ElapsedTime = 0;
OS_bWait(&LCDFree);
BSP_LCD_FillScreen(BGCOLOR);
while (ElapsedTime < LIFETIME){
CurrentTime = OS_MsTime();
ElapsedTime = CurrentTime - StartTime;
BSP_LCD_Message(0,5,0,"Life Time:",LIFETIME);
BSP_LCD_Message(1,0,0,"Horizontal Area:",area[0]);
BSP_LCD_Message(1,1,0,"Vertical Area:",area[1]);
BSP_LCD_Message(1,2,0,"Elapsed Time:",ElapsedTime);
OS_Sleep(50);
}
BSP_LCD_FillScreen(BGCOLOR);
OS_bSignal(&LCDFree);
OS_Kill(); // done, OS does not return from a Kill
}
//************SW1Push*************
// Called when SW1 Button pushed
// Adds another foreground task
// background threads execute once and return
void SW1Push(void){
if(OS_MsTime() > 20 ){ // debounce
if(OS_AddThread(&ButtonWork,128,4)){
NumCreated++;
}
OS_ClearMsTime(); // at least 20ms between touches
}
}
//--------------end of Task 2-----------------------------
//------------------Task 3--------------------------------
//******** Consumer ***************
// foreground thread, accepts data from producer
// Display crosshair and its positions
// inputs: none
// outputs: none
void Consumer(void){
// while(NumSamples < RUNLENGTH){
// jsDataType data;
// JsFifo_Get(&data);
// OS_bWait(&LCDFree);
//
// BSP_LCD_DrawCrosshair(prevx, prevy, LCD_BLACK); // Draw a black crosshair
// BSP_LCD_DrawCrosshair(data.x, data.y, LCD_RED); // Draw a red crosshair
// BSP_LCD_Message(1, 5, 3, "X: ", x);
// BSP_LCD_Message(1, 5, 12, "Y: ", y);
// OS_bSignal(&LCDFree);
// prevx = data.x;
// prevy = data.y;
// }
while (inDiceMode) {
jsDataType data;
JsFifo_Get(& data);
x = data.x;
y = data.y;
z = data.z;
OS_bWait(&LCDFree);
if (done == 0) {
if (dataPoints < 100) {
BSP_LCD_DrawString(0 , 0 , "Dice mode activated" , 0x07E0);
if ((dataPoints & 8) == 0) {
BSP_LCD_DrawString(7, 6, loading, 0xFFFF);
}
else if ((dataPoints & 8) == 8) {
BSP_LCD_DrawString(7, 6, loading, 0x0000);
}
dataPoints++;
}
else if (dataPoints < 116) {
xdata[currentIndex] = x;
ydata[currentIndex] = y;
zdata[currentIndex] = z;
currentXSum += xdata[currentIndex];
currentYSum += ydata[currentIndex];
currentZSum += zdata[currentIndex];
currentIndex++;
if (currentIndex > 15) {
currentIndex = 0;
}
dataPoints++;
if ((dataPoints & 8) == 8) {
BSP_LCD_DrawString(7, 6, loading, 0xFFFF);
}
else if ((dataPoints & 8) == 8) {
BSP_LCD_DrawString(7, 6, loading, 0x0000);
}
}
else if (dataPoints == 116) {
BSP_LCD_DrawString(0 , 0 , "Dice mode activated" , 0x0000);
BSP_LCD_DrawString(7, 6, loading, 0x0000);
currentXSum -= xdata[currentIndex];
currentYSum -= ydata[currentIndex];
currentZSum -= zdata[currentIndex];
xdata[currentIndex] = x;
ydata[currentIndex] = y;
zdata[currentIndex] = z;
currentXSum += xdata[currentIndex];
currentYSum += ydata[currentIndex];
currentZSum += zdata[currentIndex];
currentIndex++;
if (currentIndex > 15) {
currentIndex = 0;
}
currentXAverage = currentXSum >> 4;
currentYAverage = currentYSum >> 4;
currentZAverage = currentZSum >> 4;
if (abs(x - currentXAverage) < 30 && abs(y - currentYAverage) < 30 && abs(z - currentZAverage) < 30) {
printf("%d, %d, %d, %d, %d, %d, %d \n", x, y, z, currentRoll, currentXAverage, currentYAverage, currentZAverage);
BSP_LCD_MessageBig (0, 4, 7, roll, currentRoll, 8);
}
else {
currentRoll = rand() % sides + 1;
printf("%d, %d, %d, %d, %d, %d, %d \n", x, y, z, currentRoll, currentXAverage, currentYAverage, currentZAverage);
BSP_LCD_MessageBig (0, 4, 7, roll, currentRoll, 8);
}
dataPoints++;
}
else {
currentXSum -= xdata[currentIndex];
currentYSum -= ydata[currentIndex];
currentZSum -= zdata[currentIndex];
xdata[currentIndex] = x;
ydata[currentIndex] = y;
zdata[currentIndex] = z;
currentXSum += xdata[currentIndex];
currentYSum += ydata[currentIndex];
currentZSum += zdata[currentIndex];
currentIndex++;
if (currentIndex > 15) {
currentIndex = 0;
}
currentXAverage = currentXSum >> 4;
currentYAverage = currentYSum >> 4;
currentZAverage = currentZSum >> 4;
if (abs(x - currentXAverage) < 30 && abs(y - currentYAverage) < 30 && abs(z - currentZAverage) < 30) {
steadySamples++;
printf("%d, %d, %d, %d, %d, %d, %d \n", x, y, z, currentRoll, currentXAverage, currentYAverage, currentZAverage);
BSP_LCD_MessageBig (0, 4, 7, roll, currentRoll, 8);
}
else {
rollingSamples++;
steadySamples = 0;
currentRoll = rand() % sides + 1;
printf("%d, %d, %d, %d, %d, %d, %d \n", x, y, z, currentRoll, currentXAverage, currentYAverage, currentZAverage);
BSP_LCD_MessageBig (0, 4, 7, roll, currentRoll, 8);
}
dataPoints++;
}
}
//BSP_LCD_Message (1, 12, 1, xstring, x);
//BSP_LCD_Message (1, 12, 8, ystring, y);
//BSP_LCD_Message (1, 12, 15, zstring, z);
OS_bSignal(&LCDFree);
if (steadySamples > 20 && rollingSamples > 0) {
done = 1;
}
if (!inDiceMode){ NumCreated--; OS_Kill(); }}
if (!inDiceMode){ NumCreated--; OS_Kill(); }
//OS_Kill(); // done
}
//--------------end of Task 3-----------------------------
//------------------Task 4--------------------------------
void incrementSides(void){
sides++;
if (sides == 10){
sides = 2;
}
//BSP_LCD_Message(1,3,4,"Sides: " , sides);
BSP_LCD_MessageBig(0,7,8,sideArr , sides, 6);
}
void exitDiceMode(void){
inDiceMode = 0;
inControlMode = 0;
killThreads = 1;
NumCreated += OS_AddThread(&bufferState , 128 , 1);
done = 0;
}
void reset(void){
dataPoints = 0;
currentIndex = 0;
currentXSum = 0;
currentYSum = 0;
currentZSum = 0;
currentRoll = 1;
steadySamples = 0;
rollingSamples = 0;
done = 0;
}
void enterDiceMode(void){
volatile unsigned long i;
inDiceMode = 1;
inControlMode = 0;
dataPoints = 0;
currentIndex = 0;
currentXSum = 0;
currentYSum = 0;
currentZSum = 0;
steadySamples = 0;
rollingSamples = 0;
done = 0;
OS_bWait(&LCDFree);
BSP_LCD_FillScreen(0);
//BSP_LCD_DrawString(0 , 0 , "Dice mode activated" , 0x07E0);
OS_bSignal(&LCDFree);
//OS_AddPeriodicThread(&Producer,PERIOD,1); // 2 kHz real time sampling of PD3
//NumCreated += OS_AddThread(&Interpreter, 128,2);
NumCreated += OS_AddThread(&Consumer, 128,1);
NumCreated += OS_AddThread(&Producer, 128,1);
OS_AddSW1Task(&exitDiceMode , 1);
OS_AddSW2Task(&reset , 1);
}
void enterBufferState(void){
inControlMode = 0;
inDiceMode = 0;
NumCreated += OS_AddThread(&bufferState , 128 , 1);
}
void controlScreen(void){
OS_bWait(&LCDFree);
BSP_LCD_FillScreen(BGCOLOR);
BSP_LCD_DrawString(0 , 0 , "Button1: " , 0x07E0);
BSP_LCD_DrawString(2 , 1 , "Increment sides" , 0x07E0);
BSP_LCD_DrawString(0 , 2 , "Button2: " , 0x07E0);
BSP_LCD_DrawString(2 , 3 , "Ret to buffer state" , 0x07E0);
BSP_LCD_DrawString(7, 5, "(Sides)", LCD_CYAN);
BSP_LCD_MessageBig(0,7,8,sideArr , sides, 6);
OS_bSignal(&LCDFree);
OS_AddSW1Task(&incrementSides , 1);
OS_AddSW2Task(&enterBufferState , 1);
while(1){
if (inDiceMode || !inControlMode){
NumCreated--; OS_Kill();
}
}
}
void enterControlMode(void){
NumCreated += OS_AddThread(&controlScreen , 128 , 1);
inControlMode = 1;
inDiceMode = 0;
}
void bufferState(void){
OS_bWait(&LCDFree);
BSP_LCD_FillScreen(BGCOLOR);
BSP_LCD_DrawString(0 , 0 , "Button1: " , 0x07E0);
BSP_LCD_DrawString(2 , 1 , "Enter dice mode" , 0x07E0);
BSP_LCD_DrawString(0 , 2 , "Button2: " , 0x07E0);
BSP_LCD_DrawString(2 , 3 , "Enter edit mode" , 0x07E0);
OS_bSignal(&LCDFree);
OS_AddSW1Task(&enterDiceMode , 1);
OS_AddSW2Task(&enterControlMode , 1);
while (1){
if (inControlMode || inDiceMode){
NumCreated--;
OS_Kill();
}
}
}
//--------------end of Task 4-----------------------------
//------------------Task 5--------------------------------
void drawCircle(int p1, int p2 , int radius){
int x,y,i;
for(x=(p1-radius);x<=(p1+radius);x++)
{
y = sqrt(radius*radius - pow((x-p1),2)) + p2;
BSP_LCD_DrawPixel(x,y,0x0000);
for (i = p2; i < y; i++){
BSP_LCD_DrawPixel(x , i , 0x0000);
}
}
for(x=(p1-radius);x<=(p1+radius);x++)
{
y = p2 - sqrt(radius*radius - pow((x-p1),2));
BSP_LCD_DrawPixel(x,y,0x0000);
for (i = p2; i > y; i--){
BSP_LCD_DrawPixel(x , i , 0x0000);
}
}
}
void startScreen(void){
int32_t wait = 0;
OS_bWait(&LCDFree);
OS_ClearMsTime();
BSP_LCD_FillScreen(BGCOLOR);
BSP_LCD_DrawString(5 , 0 , "Digital Dice" , 0x07E0);
OS_ClearMsTime();
while (wait < 3){
BSP_LCD_FillRect(32,32,64,64, 0xFFFF);
drawCircle(47 , 47 , 10);
drawCircle(81 , 81 , 10);
while (OS_MsTime() < 1000){ }
OS_ClearMsTime();
BSP_LCD_FillRect(32,32,64,64, 0x0000);
while (OS_MsTime() < 1000){}
OS_ClearMsTime();
wait++;
}
//BSP_LCD_FillScreen(BGCOLOR);
//BSP_LCD_DrawString(0 , 0 , "Done" , 0x07E0);
//OS_ClearMsTime();
//while (OS_MsTime() < 1000){}
inDiceMode = 0;
inControlMode = 0;
sides = 6;
NumCreated += OS_AddThread(&bufferState , 128 , 1);
NumCreated--;
OS_bSignal(&LCDFree);
OS_Kill();
}
//--------------end of Task 5-----------------------------
//******************* Main Function**********
int main(void){
OS_Init(); // initialize, disable interrupts
Device_Init();
//CrossHair_Init();
currentIndex = 0;
currentXSum = 0;
currentYSum = 0;
currentZSum = 0;
DataLost = 0; // lost data between producer and consumer
NumSamples = 0;
MaxJitter = 0; // in 1us units
//********initialize communication channels
JsFifo_Init();
srand(100);
//*******attach background tasks***********
//OS_AddSW1Task(&SW1Push,2);
//OS_AddPeriodicThread(&Producer,PERIOD,1); // 2 kHz real time sampling of PD3
NumCreated = 0 ;
// create initial foreground threads
//NumCreated += OS_AddThread(&Interpreter, 128,2);
//NumCreated += OS_AddThread(&Consumer, 128,1);
//NumCreated += OS_AddThread(&Producer, 128,1);
//NumCreated += OS_AddThread(&CubeNumCalc, 128,1);
NumCreated += OS_AddThread(&startScreen , 128 , 1);
OS_InitSemaphore(&LCDFree, 1);
OS_Launch(TIME_2MS); // doesn't return, interrupts enabled in here
return 0; // this never executes
}