// Use this for initialization
		void Start () {
		
			arduino = Arduino.global;			
			arduino.Log = (s) => Debug.Log("Arduino: " +s);
			arduino.Setup(ConfigurePins);
			StartCoroutine(BlinkLoop());
		}
		void Start () {
		
			arduino = Arduino.global;
			arduino.Log = (s) => Debug.Log("Arduino: " +s);
			arduino.Setup(ConfigurePins);
			
			cube = GameObject.Find("Cube");
		}
Beispiel #3
0
		void Start( )
		{
		    arduino = Arduino.global;
		    arduino.Setup(ConfigurePins);
		 
		    StartCoroutine(loop());
		 
		}
Beispiel #4
0
		// Use this for initialization
		void Start () {
		
			arduino = Arduino.global;
			arduino.Log = (s) => Debug.Log("Arduino: " +s);
			
			arduino.Setup(ConfigurePins);

			ConfigurePins ();
		}
	void Start () {		
		
		if (!UniduinoSetupHelpers.SerialPortAvailable())
		{
#if UNITY_EDITOR
				
			UniduinoSetupHelpers.OpenSetupWindow();	
#else
			Debug.LogError("Uniduino SerialPort Support must be installed: is libMonoPosixHelper on your path?");
#endif
		}
		
		arduino = Arduino.global; 						// convenience, alias the global arduino singleton
		arduino.Log = (s) => Debug.Log("Arduino: " +s); // Attach arduino logs to Debug.Log
		hookEvents();									// set up event callbacks for received data

	}
Beispiel #6
0
		public static Arduino global { get { return instance; } } // conveniently expose the singleton for the common case where only one arduino is connected
				
		/// <summary>
		/// Automatically connect to the arduino if properly configured.
		/// </summary>
		void Awake () {		
					
			Log("Arduino awake");
			if (instance == null) instance = this; // track the first instance that was created as a convenience, but dont preclude multiple uniduino's coexisting
	
			DontDestroyOnLoad(this);
			
			if (AutoConnect)
			{
				Log("AutoConnecting...");
				if (PortName == null || PortName.Length == 0 && Arduino.guessPortName().Length > 0)
				{
					PortName = Arduino.guessPortName();
				}
				
				Connect();								
			}

		}
		void drawPinGUI(Arduino.Pin pin)
		{
			GUIStyle green_button = new GUIStyle(GUI.skin.button);
			green_button.normal.textColor = Color.green;
			
			GUIStyle gray_button = new GUIStyle(GUI.skin.button);
			gray_button.normal.textColor = Color.gray;
					
			bool show_analog_report_button = false;
			bool show_digital_report_button = false;
							
			if (!pin_ui_states.ContainsKey(pin))
				pin_ui_states[pin] = new PinUI();
			
			var ui = pin_ui_states[pin];
			
			foreach ( var pc in pin.capabilities)
			{			
				if (pc.Mode == PinMode.ANALOG)
					show_analog_report_button = true;
				
				if (pc.Mode == PinMode.OUTPUT)
					show_digital_report_button = true;
				
			}		
			
			GUILayout.BeginHorizontal();
			
			string label = "";
			
			label = "D"+pin.number.ToString() + ":" + arduino.digitalRead(pin.number).ToString();
			if (pin.analog_number >= 0)
				label += " A"+pin.analog_number.ToString() + ":" + arduino.analogRead(pin.analog_number).ToString();
			
			GUILayout.Label(label, GUILayout.Width(100));
			
			// write test
			GUILayout.BeginHorizontal(GUILayout.Width(test_column_width));
			
	
			if (ui.last_pin_mode == PinMode.OUTPUT)
			{
						
				if (GUILayout.Button(ui.test_state ? "HIGH": "low", ui.test_state ? green_button : gray_button, GUILayout.Width(50))) 
				{
					ui.test_state = !ui.test_state;
					arduino.digitalWrite(pin.number, ui.test_state ? 1 : 0); 
					
					// NB: this appears not to interfere input pins for now, but the semantics are not 
					// well-defined so this could break later if either firmata implementation changes 
					// to autamatically update the pin mode on a digitalWrite
				}
			} else if (ui.last_pin_mode == PinMode.PWM || ui.last_pin_mode == PinMode.SERVO)
			{
				float current_pwm_value; 
				float.TryParse(ui.pwm_value_buffer, out current_pwm_value);
				float pwm_value = GUILayout.HorizontalSlider(current_pwm_value, 0, ui.last_pin_mode == PinMode.SERVO ? 180 : 255, GUILayout.Height(21), GUILayout.Width(50));
				
				if (pwm_value != current_pwm_value)
				{
					arduino.pinMode(pin.number, (int)ui.last_pin_mode);
					arduino.analogWrite(pin.number, (int)pwm_value);
					ui.pwm_value_buffer = pwm_value.ToString();
				}
						
				/* Old style: text entry of pwm values--could be useful for some
				ui.pwm_value_buffer = GUILayout.TextField(ui.pwm_value_buffer, GUILayout.Width(30));
				int pwm_value;
				if (GUILayout.Button("!",GUILayout.Width(20)) && int.TryParse(ui.pwm_value_buffer, out pwm_value))
				{
					arduino.pinMode(pin.number, (int)PinMode.PWM);
					arduino.analogWrite(pin.number, pwm_value);
				}
				*/
				
				
			} else
			{
				GUILayout.Label(""); // workaround unity gui silliness 	
			}
			GUILayout.EndHorizontal();
			
						
			foreach ( var pc in pin.capabilities)
			{
				
				if (GUILayout.Button(pc.Mode.ToString(), ui.last_pin_mode==pc.Mode ? green_button : gray_button))
				{				
					arduino.pinMode(pin.number, pc.mode);
					ui.last_pin_mode = pc.Mode;
					if (pc.Mode == PinMode.ANALOG)
					{										
						// mirror behavior of StardardFirmata. note that this behavior is different from digital ports, which do require an explicit reportDigital call to enable reporting
						ui.reporting_analog = true; 
						//	arduino.reportAnalog(k, (byte)analog_pins_reporting[k]);					
					}
				}
			}		
			
			GUILayout.FlexibleSpace();
			
			if (show_analog_report_button)
			{			
				if (GUILayout.Button("Analog", ui.reporting_analog ? green_button : gray_button ))
				{
					ui.reporting_analog = !ui.reporting_analog;
					arduino.reportAnalog(pin.analog_number, (byte)(ui.reporting_analog ? 1 : 0));
				}
			}
			
			if (show_digital_report_button)
			{			
				if (GUILayout.Button("Digital", ui.reporting_digital ? green_button : gray_button ))
				{
					ui.reporting_digital = !ui.reporting_digital;
					arduino.reportDigital((byte)pin.port, (byte)(ui.reporting_digital ? 1 : 0));
					
					
					foreach (var p in pin_ui_states.Keys)
					{
						if (p.port == pin.port)
						{
							pin_ui_states[p].reporting_digital = ui.reporting_digital;	
						}					
					}
				}
				
			}		
			GUILayout.EndHorizontal();
					
		}