/*
INSPIRED BY by Mike Chambers

http://www.mikechambers.com

ONE POTENTIOMETER ONLY

TinkerProxy: http://code.google.com/p/tinkerit/wiki/TinkerProxy

*/

package {
import flash.events.Event;
import flash.display.Sprite;
import flash.net.Socket;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.events.SecurityErrorEvent;
import flash.utils.Endian;
import flash.text.TextField;

public class arduino2serial extends Sprite {

//Character that delineates the end of a message received
//from the Arduino

private static const EOL_DELIMITER:String = "\n";

//socket we will use to connect to TinkerProxy
private var _socket:Socket;

//Address where TinkerProxy is located. Will usually be
//localhost / 127.0.0.1

private var _proxyAddress:String = "127.0.0.1";
// ====== >>>>>>>>>>>>>> private var _proxyAddress:String = "localhost";
//port TinkerProxy is listening on

private var _proxyPort:uint = 5331;

private var GREEN:Sprite = new Sprite();

private var DISPLAY:TextField = new TextField();


//constructor

public function arduino2serial() {//listen for when we are added to the stage
addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}

private function onAddedToStage(event:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);

DISPLAY.x = 100;
DISPLAY.y = 380;

DISPLAY.text = "hola!";

addChild(DISPLAY);

//create a Sprite to add to the stage.
//draw a green square in the Sprite


GREEN.graphics.beginFill(0x00FF00);
GREEN.graphics.drawRect(0,0, 200,100);
GREEN.graphics.endFill();

//Add Sprite to the display list
addChild(GREEN);

//position it
GREEN.x = 200;
GREEN.y = 100;


_socket = new Socket();

//Register for socket events

//socket connected
_socket.addEventListener( Event.CONNECT, onConnect );

//socket closed
_socket.addEventListener( Event.CLOSE, onClose );

//data received from socket
_socket.addEventListener( ProgressEvent.SOCKET_DATA, onSocketData );

//Error connecting
_socket.addEventListener( IOErrorEvent.IO_ERROR, onIOError );

//Security Error
_socket.addEventListener( SecurityErrorEvent.SECURITY_ERROR, onSecurityError );

//need to set Endianness for Socket. THIS IS IMPORTANT
//If this is set incorrectly, the Arduino will not be able to understand
//all of the data sent from Flash.
//
//See:
//
http://www.mikechambers.com/blog/2010/08/01/sending-multibyte-numbers-from-actionscript-to-arduino/

_socket.endian = Endian.LITTLE_ENDIAN;

//connect
_socket.connect(_proxyAddress, _proxyPort);
}

//called when we connect to the proxy server
private function onConnect(event:Event):void
{
/*
note, you cannot reliably write data to the socket here.

Im not sure if this is a Flash player issue, or a timing issue
with the proxy.
*/

trace("Socket Connected");
}

/*
This function / event handler is called when data is received
on the socket.

However, it is important to remember that the event is called as
data is received, which means that not all of the data may be available
when it is called.

For example, if you sent the string "Hello World" from Arduino,
then the event handler might be called twice. Once with "Hello Wo" and
a second time with "rld".

"Because of this, in most cases, you need to buffer the data, and parse
out messages, looking for a character (that you specify) that delineates the
end of a message.

If you just want to send a single character back from Arduino, then this
is not necessary. However, the handler below is generic, and already does all
of the buffering, so you can just use it.
*/

//string to hold data as it comes in.
private var buffer:String = "";

//event called when data arrives on the socket from the
//arduino


private function onSocketData(event:ProgressEvent):void
{
//get the string sent from the Arduino. This could be any binary data
//but in our case, we are sending strings.
//In general, it is much easier to just always send strings from
//Arduino, and then parse then in ActionScript

var data:String = _socket.readUTFBytes( _socket.bytesAvailable );

//copy the newly arrived data into the buffer string.
buffer += data;

//completed message from the server
var msg:String;
var index:int;

//loop through the buffer until it contains no more
//end of message delimiter


while((index = buffer.indexOf(EOL_DELIMITER)) > -1)
{
//extract the message from the beginning to where the delimiter is
//we don't include the delimiter


msg = buffer.substring(0, index);

//remove the message from the buffer

buffer = buffer.substring(index + 1);

//trace out the message (or do whatever you want with it)
//trace("Message Received from Arduino : " + msg);
//trace(Number(msg));


trace(msg);
DISPLAY.text = msg;


GREEN.rotationZ = Number(msg) / 10;
//GREEN.graphics.beginFill(Number(msg)*10);


//GREEN.graphics.beginFill(0x00FF00);
///GREEN.graphics.drawRect(0,0, 200,100);
//GREEN.graphics.endFill();


///GREEN.width = Number(msg)/2;
///GREEN.height = Number(msg)/4;


///GREEN.graphics.endFill();

}

}

 

//called when the socket is closed
private function onClose(event:Event):void {
trace("Socket Closed");
}

//called if an error occurs while connecting to the socket.
private function onIOError(event:IOErrorEvent):void {
trace("IOErrorEvent : Did you run SERPROXY?" + event.text);
}

//called when there is a security error. Usually if you try to connect to a socket
//when the SWF doesn't have permission.
//See:

//http://www.adobe.com/devnet/flashplayer/articles/fplayer9_security_04.html
//In most cases, when testing locally, this will not be an issue.

private function onSecurityError(event:SecurityErrorEvent):void {
trace("SecurityErrorEvent : " + event.text);
}
}
}

////// ARDUINO CODE
////// ONE POTENTIOMETER

/*
void setup() {
Serial.begin(57600);
}

void loop() {
int sensorValue = analogRead(A0);
Serial.print(sensorValue);
Serial.print("\n");
}
*/