-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSignalController
More file actions
76 lines (69 loc) · 2.95 KB
/
SignalController
File metadata and controls
76 lines (69 loc) · 2.95 KB
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class SignalController : MonoBehaviour {
//stores the current state of each buzzer on the sculptic
private char[] msg = { '0', '0', '0', '0', '0', '0', '0' };
//stores the state of the sculptic's onboard LED
bool ledState = false;
//this function accepts an integer representing which finger (or part of the palm) to work with, and an integer representing the amount of buzz to apply (0 is none, 9 is max)
//since the function is called once every frame, it makes sure that it only sends POST requests to the particle photon if some point of data has changed
public void sendSignal(int finger, int value = 0) {
//checks to see if any data has changed
if (msg[finger] != (char)(value + '0')) {
string send = "";
msg[finger] = (char)(value + '0');
//compiles data string
for (int i = 0; i < msg.Length; i++) {
send += msg[i];
}
//Debug.Log("Sent: " + send);
//upload with keyword vibe to access vibeControl function
StartCoroutine(Upload("vibe", send));
}
}
//this function accepts a boolean, and based on that sets the LED on the sculptic to on or off
//will also check to ensure a state change has occurred before sending requests
public void ledToggle(bool on) {
//check to see if data has changed
if (on != ledState) {
string state = "off";
if (on) {
state = "on";
}
ledState = on;
//use led keyword for ledToggle function
StartCoroutine(Upload("led", state));
}
}
//resets all buzzing to off
public void reset() {
for (int i = 0; i < msg.Length; i++) {
msg[i] = '0';
}
sendSignal(0);
}
//these values allow connection to the particle photon
//they will vary from chip to chip
private const string device_id = "1c0035001347343438323536";
private const string access_token = "1704b5f5c56fdef88e4bc3ea40de83600ae09f4b";
//coroutine in chanrge of sending POST requests to the particle photon
//accepts a string holding a function keyword, and a string containing arguments for that function
IEnumerator Upload(string func, string msg) {
//pack relevant data into payload
WWWForm form = new WWWForm();
form.AddField("access_token", access_token);
form.AddField("arg", msg);
//sends request
UnityWebRequest www = UnityWebRequest.Post("https://api.particle.io/v1/devices/" + device_id + "/" + func, form);
//Debug.Log(www.url);
yield return www.SendWebRequest();
//make sure it worked
if (www.isNetworkError || www.isHttpError) {
Debug.Log(www.error);
} else {
//Debug.Log("Form upload complete!");
}
}
}