-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
132 lines (105 loc) · 4.4 KB
/
Form1.cs
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
namespace Stackoverflow_77490286.WinForms
{
public partial class Form1 : Form
{
bool serialPortIsOpen = true;
int currentIndex = 0;
string command = "";
public Form1()
{
InitializeComponent();
//Move the visual avatar to the starting position
MoveAvatar(listBox1.Items[0].ToString());
}
private void MoveAvatar(string point)
{
var currentCoordinates = point.Split('.');
var current_x = Convert.ToInt32(currentCoordinates[0]);
var current_y = Convert.ToInt32(currentCoordinates[1]);
btnAvatar.Location = new System.Drawing.Point(current_x, current_y);
}
private void btnSendData_Click(object sender, EventArgs e)
{
if (serialPortIsOpen && listBox1.Items.Count > 1)
{
currentIndex = 0; // Reset the index to start from the first item
timer1.Interval = 750; // in mili-second
timer1.Start();
}
else
{
MessageBox.Show("Port Closed or Not Enough Data");
}
}
private void btnCalculatePattern_Click(object sender, EventArgs e)
{
// The first item of the pattern is always (0,0)
listBox2.Items.Add($"0.0");
for (int i = 0; i < listBox1.Items.Count - 1; i++)
{
var currentCoordinates = listBox1.Items[i].ToString().Split('.');
var current_x = Convert.ToInt32(currentCoordinates[0]);
var current_y = Convert.ToInt32(currentCoordinates[1]);
var nextCoordinates = listBox1.Items[i + 1].ToString().Split('.');
var next_x = Convert.ToInt32(nextCoordinates[0]);
var next_y = Convert.ToInt32(nextCoordinates[1]);
var x = next_x - current_x;
var y = next_y - current_y;
listBox2.Items.Add($"{x}.{y}");
}
btnCalculatePattern.Enabled = false;
btnSendData.Enabled = true;
}
private void UpdateCoordinates(string startingPoint)
{
// The startingPoint is the ending point of the last iteration
listBox1.Items[0] = startingPoint;
var startingPointSplit = startingPoint.Split(".");
var startingPoint_x = Convert.ToInt32(startingPointSplit[0]);
var startingPoint_y = Convert.ToInt32(startingPointSplit[1]);
// Calculate the rest of the coordinates for this iteration from
// the starting point and the pattern
var x = startingPoint_x;
var y = startingPoint_y;
for (int i = 0; i < listBox1.Items.Count; i++)
{
var pattern = listBox2.Items[i].ToString().Split('.');
var pattern_x = Convert.ToInt32(pattern[0]);
var pattern_y = Convert.ToInt32(pattern[1]);
x += pattern_x;
y += pattern_y;
listBox1.Items[i] = $"{x}.{y}";
}
}
private void SendDataToArduino()
{
// Send commands with the current state of Coordinates
if (currentIndex < listBox1.Items.Count)
{
command = listBox1.Items[currentIndex].ToString();
command = command.Replace(": ", ""); // Remove the colon
command = command.Replace(",", ""); // Remove comma if needed
//We move the visual avatar and print the coordinates to the console
// to simulate the motor receiving the commands
// serialPort1.WriteLine(command);
Console.WriteLine(command);
MoveAvatar(command);
textBox3.Text = command;
currentIndex++; // Move to the next item
}
else
{
// Tüm veriler gönderildi, işlemi yeniden başlat
currentIndex = 0;
// Update the Coordinates in the listBox with the Pattern information
// passing the last point of the current iteration
// as the starting point of the next iteration.
UpdateCoordinates(command);
}
}
private void timer1_Tick(object sender, EventArgs e)
{
SendDataToArduino();
}
}
}