-
Notifications
You must be signed in to change notification settings - Fork 1
/
Driver.cs
89 lines (76 loc) · 2.91 KB
/
Driver.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
/*
* 13th July 2017
* Basic Code for change of colors when data gets sent
* Node IDs, Location and Path are all public variables which the user can put in
* Wait time between change of colors is also a Public Variable
* Version 1.1
*/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO.Ports;
public class Driver : MonoBehaviour {
SerialPort sp = new SerialPort("\\\\.\\COM12", 9600);
public string Path;
/*
* 0 --> BaseStation
* 1 --> Node_1
* 2 --> Node_2
* 3 --> Node_3
* 4 --> Node_4
* 5 --> Node_5
* 6 --> Node_6
*/
//Array Of GameObjects representing Nodes (inclusive of the BaseStation)
public GameObject[] Nodes = new GameObject[20];
//Different Materials for Different States of the Nodes
public Material ActivatedMaterial; // When Data gets sent Node turns green
public Material DefaultMaterial; // Default color of all Nodes and Base Station is Yellow
public Material FailureMaterial; // When there is loss of connection, Node Turns Red
//TimeLapse between each Node State Change
public float WaitTimeInSeconds;
int NoOfNodes;
void Start()
{
sp.Open();
Path = sp.ReadLine();
NoOfNodes = Nodes.Length;
StartCoroutine (DriverMethod (WaitTimeInSeconds));
//print(Path);
}
IEnumerator DriverMethod(float waitTime)
{
int l = Path.Length;
int i = 0;
while (i<l) {
if (Path[i] == '+')
{
i++;
print("Postive");
int index = Path[i] - '0'; //Converting Character into integer
print(index);
for (int j = 0; j < NoOfNodes; j++)
{
if (j == index) Nodes[j].GetComponent<Renderer>().material = ActivatedMaterial; // sets the node color to green
else Nodes[j].GetComponent<Renderer>().material = DefaultMaterial; // sets it back to yellow ; Default state
}
yield return new WaitForSecondsRealtime(waitTime); // Time Delay
}
else if (Path[i] == '-')
{
i++;
print("Negative");
int index = Path[i] - '0'; //Converting Character into integer
print(index);
for (int j = 0; j < NoOfNodes; j++)
{
if (j == index) Nodes[j].GetComponent<Renderer>().material = FailureMaterial; // sets the node color to green
else Nodes[j].GetComponent<Renderer>().material = DefaultMaterial; // sets it back to yellow ; Default state
}
yield return new WaitForSecondsRealtime(waitTime); // Time Delay
}
else
i++;
}
}
}