Skip to content

Commit 9297431

Browse files
author
Nico
committed
Added final version of code into MASTER
2 parents 5516f18 + 81ff2b0 commit 9297431

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+2494
-41
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Debug/
2+
Release/
23
*.lock
34
bin/
45
obj/
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

MotionTest_Console/MotionData.cs

+17
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Linq;
55
using System.Text;
66
using System.Threading.Tasks;
7+
using System.Timers;
78

89
namespace BallInAMaze
910
{
@@ -32,6 +33,10 @@ public class MotionData
3233
// Start Condition that is sent to Board at startup
3334
private readonly byte[] StartCondition = new byte[2] { 0xAF, 0xFE };
3435

36+
private Timer timer;
37+
38+
private int test;
39+
3540
/// <summary>
3641
/// CTOR
3742
/// </summary>
@@ -96,6 +101,18 @@ public MotionData(int PortNumber)
96101
StartUpFailed?.Invoke(this, EventArgs.Empty);
97102
return;
98103
}
104+
105+
timer = new Timer();
106+
timer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
107+
timer.Interval = 5000; // 1 sec
108+
timer.Enabled = true;
109+
timer.AutoReset = false;
110+
}
111+
112+
private void OnTimedEvent(object sender, ElapsedEventArgs e)
113+
{
114+
byte[] stop = { 0x01 };
115+
mPort.Write(stop, 0, 1);
99116
}
100117

101118
// THIS method can be exchanged if we are not longer

MotionTest_Console/Program.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
using System.Text;
66
using System.Threading.Tasks;
77

8-
namespace MotionTest_Console
8+
namespace BallInAMaze
99
{
1010
class Program
1111
{
1212
static void Main(string[] args)
13-
{
13+
{
14+
var game = new TheGame(0, 0, 0);
15+
while (true) ;
1416
}
1517
}
1618
}

MotionTest_Console/TheGame.cs

+15-2
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public int BallPosition_Y
7676
// --------------------------------------------------
7777
public TheGame(int GameField_Width, int GameField_Height, int PortNumber)
7878
{
79-
field = new GameField(GameField_Width, GameField_Height);
79+
//field = new GameField(GameField_Width, GameField_Height);
8080
data = new MotionData(PortNumber);
8181
data.NewDataAvailable += Data_NewDataArrived;
8282
data.StartUpFailed += Data_StartUpFailed;
@@ -98,7 +98,20 @@ private void Data_StartUpFailed(object sender, EventArgs e)
9898

9999
private void Data_NewDataArrived(object sender, EventArgs e)
100100
{
101-
//Console.WriteLine("X: {0} Y: {1} Z: {2}", data.Axis_X.ToString().PadRight(8), data.Axis_Y.ToString().PadRight(8), data.Axis_Z.ToString().PadRight(8));
101+
// Calculate movement of ball
102+
int Diff_X = Old_Value_X + data.Axis_X;
103+
int Diff_Y = Old_Value_Y + data.Axis_Y;
104+
105+
double move_x = Diff_X / 5000.0;
106+
move_x = Math.Round(move_x, 1);
107+
double move_y = Diff_Y / 5000.0;
108+
move_y = Math.Round(move_y, 1);
109+
110+
// Save new values
111+
Old_Value_X = data.Axis_X;
112+
Old_Value_Y = data.Axis_Y;
113+
114+
Console.WriteLine("X: {0} Y: {1} Z: {2}", move_x.ToString().PadRight(8), move_y.ToString().PadRight(8), data.Axis_Z.ToString().PadRight(8));
102115

103116
// Kalibrierung nach StartUp
104117
// SetBallPosition + Überprüfung + Event an ModelView wenn Kugel in Loch oder Kugel in Ziel

Xilinx_Board/Ball_InAMaze_Game/.cproject

+38-30
Large diffs are not rendered by default.

Xilinx_Board/Ball_InAMaze_Game/.project

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<buildSpec>
99
<buildCommand>
1010
<name>org.eclipse.cdt.managedbuilder.core.genmakebuilder</name>
11+
<triggers>clean,full,incremental,</triggers>
1112
<arguments>
1213
</arguments>
1314
</buildCommand>

Xilinx_Board/Ball_InAMaze_Game/src/GPIO.c

+17-6
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@
2323
#define GPIO_DEVICE_ID XPAR_XGPIOPS_0_DEVICE_ID
2424
#define GPIO_AXI0_DEVICE_ID XPAR_GPIO_0_DEVICE_ID
2525

26-
#define GPIO_PS_BUTTON_OFFSET 0 //MIO#0
27-
#define GPIO_PS_LED_R_OFFSET 52 //MIO#52 (906+52=958)
28-
#define GPIO_PS_LED_G_OFFSET 53 //MIO#53 (906+53=959)
26+
#define GPIO_PS_BUTTON_OFFSET 0 //MIO#0
27+
#define GPIO_PS_LED_R_OFFSET 52 //MIO#52 (906+52=958)
28+
#define GPIO_PS_LED_G_OFFSET 53 //MIO#53 (906+53=959)
2929

3030
// ----------------------------------------------
3131
// PRIVATE VARIABLES
32-
static const u32 PSRedLedPin = 52; /* MIO_52 = Red LED */
33-
static const u32 PSGreenLedPin = 53; /* MIO53 = Green LED */
32+
static const u32 PsButtonPin = 0; /* MIO_0 = PS Pushbutton */
33+
static const u32 PSRedLedPin = 52; /* MIO_52 = Red LED */
34+
static const u32 PSGreenLedPin = 53; /* MIO53 = Green LED */
3435

3536
// GPIO Instances
3637
static XGpioPs PS_Gpio; /* The driver instance for PS GPIO Device. */
@@ -90,6 +91,13 @@ void GPIO_Set_PL_Color(unsigned char led_color)
9091
}
9192
}
9293

94+
bool GPIO_Get_PS_Button(void)
95+
{
96+
unsigned char PsButtonStatus;
97+
PsButtonStatus = XGpioPs_ReadPin(&PS_Gpio, PsButtonPin);
98+
return (PsButtonStatus == 0 ? false : true);
99+
}
100+
93101
int GPIO_Init()
94102
{
95103
XGpioPs_Config *ConfigPtr;
@@ -103,6 +111,9 @@ int GPIO_Init()
103111
return XST_FAILURE;
104112
}
105113

114+
/* Set the direction for the PS button pin to be input. */
115+
XGpioPs_SetDirectionPin(&PS_Gpio, PsButtonPin, 0x0);
116+
106117
/* Set the direction for the LED pins to be outputs */
107118
XGpioPs_SetDirectionPin(&PS_Gpio, PSRedLedPin, 1);
108119
XGpioPs_SetDirectionPin(&PS_Gpio, PSGreenLedPin, 1);
@@ -120,4 +131,4 @@ int GPIO_Init()
120131
/* Set the direction for all LED signals as outputs */
121132
XGpio_SetDataDirection(&PL_Gpio0, 1, 0x00); //All outputs
122133
return 0;
123-
}
134+
}

Xilinx_Board/Ball_InAMaze_Game/src/GPIO.h

+5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
#ifndef SRC_GPIO_H_
99
#define SRC_GPIO_H_
1010

11+
// ----------------------------------------------
12+
// INCLUDES
13+
#include <stdbool.h>
14+
1115
// ----------------------------------------------
1216
// DEFINES
1317
#define LED_COLOR_OFF 0
@@ -20,5 +24,6 @@
2024
int GPIO_Init(void);
2125
void GPIO_Set_PS_Color(unsigned char led_color);
2226
void GPIO_Set_PL_Color(unsigned char led_color);
27+
bool GPIO_Get_PS_Button(void);
2328

2429
#endif /* SRC_GPIO_H_ */

Xilinx_Board/Ball_InAMaze_Game/src/main.c

+41-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
// to be used to BLOCK wait for UART data
2828
#define UART_RECV_BYTE() XUartPs_RecvByte(XPAR_PS7_UART_1_BASEADDR)
2929

30+
// to be used to clear RECV buffer of UART
31+
#define UART_CLEAR_RECV_BUF() XUartPs_WriteReg(XPAR_PS7_UART_1_BASEADDR, XUARTPS_CR_OFFSET, (u32)XUARTPS_CR_RXRST);
32+
3033
// START condition to be recieved from APPLICATION
3134
#define START_CONDITION 0xAFFE
3235

@@ -52,12 +55,49 @@ int main()
5255
// Init LIS2DS12 Sensor
5356
LIS2DS12_Init();
5457

58+
START:
59+
// Discard all DATA in RECEIVE BUFFER
60+
UART_CLEAR_RECV_BUF();
61+
5562
// STARTUP PHASE --> ***BLOCKING*** until start condition is received
56-
if(StartUp_Phase() != SUCCESS) return FAILURE;
63+
if(StartUp_Phase() != SUCCESS)
64+
{
65+
// While USER has NOT pressed RESET button --> wake up every 100 ms and check
66+
while(!GPIO_Get_PS_Button())
67+
{
68+
// Send data every 100 ms (10 times in a second)
69+
usleep(100000);
70+
}
71+
72+
// Then go back to START
73+
goto START;
74+
}
5775

5876
// Prevent PROGRAM from EXITING
5977
while(1)
6078
{
79+
if (GPIO_Get_PS_Button())
80+
{
81+
goto START;
82+
}
83+
84+
// Check if Application sent command ...
85+
if(XUartPs_IsReceiveData(XPAR_PS7_UART_1_BASEADDR))
86+
{
87+
// DECODE COMMAND
88+
switch(UART_RECV_BYTE())
89+
{
90+
case 0x01:
91+
// STOP COMMAND --> go to START
92+
goto START;
93+
case 0x02:
94+
break;
95+
default:
96+
// Unknown COMMAND
97+
break;
98+
}
99+
}
100+
61101
// Read and Print Motion values
62102
motion = LIS2DS12_Get_Motion();
63103

Binary file not shown.

ball_in_a_maze/ActiveCOMPorts.cs

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Collections.ObjectModel;
4+
using System.ComponentModel;
5+
using System.IO.Ports;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Threading;
9+
using System.Threading.Tasks;
10+
11+
namespace ball_in_a_maze
12+
{
13+
class ActiveCOMPorts
14+
{
15+
public ActiveCOMPorts()
16+
{
17+
Worker = new BackgroundWorker();
18+
Worker.WorkerSupportsCancellation = true;
19+
Worker.DoWork += OnWorkerDoWork;
20+
Worker.RunWorkerAsync();
21+
}
22+
23+
public event EventHandler PortsUpdated;
24+
25+
internal void OnConnectionEstablished(object sender, EventArgs e)
26+
{
27+
Worker.CancelAsync();
28+
}
29+
30+
private void OnWorkerDoWork(object sender, DoWorkEventArgs e)
31+
{
32+
while (true)
33+
{
34+
Thread.Sleep(1000);
35+
Ports = SerialPort.GetPortNames();
36+
37+
if (Worker.CancellationPending)
38+
return;
39+
}
40+
}
41+
42+
private string[] ports;
43+
44+
public string[] Ports
45+
{
46+
get { return ports; }
47+
set {
48+
if(!Equals(ports, value))
49+
{
50+
ports = value;
51+
PortsUpdated(this, EventArgs.Empty);
52+
}
53+
}
54+
}
55+
56+
57+
public BackgroundWorker Worker { get; set; }
58+
}
59+
}

ball_in_a_maze/App.config

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
5+
</startup>
6+
</configuration>

ball_in_a_maze/App.xaml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Application x:Class="ball_in_a_maze.App"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:local="clr-namespace:ball_in_a_maze">
5+
<Application.Resources>
6+
7+
</Application.Resources>
8+
</Application>

ball_in_a_maze/App.xaml.cs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Configuration;
4+
using System.Data;
5+
using System.Linq;
6+
using System.Threading.Tasks;
7+
using System.Windows;
8+
9+
namespace ball_in_a_maze
10+
{
11+
/// <summary>
12+
/// Interaktionslogik für "App.xaml"
13+
/// </summary>
14+
public partial class App : Application
15+
{
16+
public App()
17+
{
18+
MainView view = new MainView();
19+
ViewModel viewModel = new ViewModel(view);
20+
view.Show();
21+
}
22+
}
23+
}

ball_in_a_maze/Game/GameField.cs

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace ball_in_a_maze
8+
{
9+
public class GameField
10+
{
11+
// ------------- W / X --------------
12+
// - -
13+
// - -
14+
// H / Y H / Y
15+
// - -
16+
// - -
17+
// - -
18+
// ------------- W / X --------------
19+
public enum GameElements
20+
{
21+
Border = 0,
22+
Empty = 1,
23+
Hole = 2,
24+
Finish = 3
25+
};
26+
public GameElements[,] PlayField { get; set; }
27+
public int Width { get; set; }
28+
public int Height { get; set; }
29+
public double BallPosition_X_W { get; set; }
30+
public double BallPosition_Y_H { get; set; }
31+
32+
public void PrintPlayField_Console()
33+
{
34+
if (PlayField == null)
35+
return;
36+
37+
// Reset all fields to Empty blocks
38+
for (int w = 0; w < Width; w++)
39+
{
40+
for (int h = 0; h < Height; h++)
41+
{
42+
if ( (BallPosition_Y_H == h) && (BallPosition_X_W == w))
43+
Console.Write('*');
44+
else
45+
Console.Write(PlayField[w, h] == GameElements.Empty ? ' ' : '#');
46+
}
47+
Console.WriteLine();
48+
}
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)