/* * ARDUINO CLASS - version 1.0 - 30-12-2005 * this Actionscript class makes it easier to connect Flash to the Arduino Board (www.arduino.cc) * # copyleft beltran berrocal, 2005 - b@progetto25zero1.com - www.progetto25zero1.com * # updates and examples: www.progetto25zero1.com/b/tools/Arduino * * # credits must also be given to: * Yaniv Steiner and the instant soup crew (instantsoup.interaction-ivrea.it) for generating the original flash client * * # you will also need the serialProxy developed by Stefano Busti(1999) and David A. Mellis(2005) * that can be found either on the Arduino Site (www.arduino.cc) or redistributed with this example (see update url) * *--------------------------------------------------------------- * * # METHODS & DESCRIPTIONS * * @@ CONSTRUCTOR * @@ creates the Arduino Object inside Flash * usage: * var portNumber = 5333; //read the serialProxy documentation to understand this * var hostIpAdress = "127.0.0.1"; //optional it deafaults to this * var ArduinoInstance:Arduino = new Arduino(portNumber, hostIpAdress); * * @@ CONNECT * @@ connects to the XMLSocket server, you must have provided a port and a host adress via the constructor * usage: * ArduinoInstance.connect() * * @@ DISCONNECT * @@ disconnects from the XMLSocket server * usage: * ArduinoInstance.disconnect() * * @@ SEND * @@ sends data to Arduino via the XMLSocket server(the serialProxy) * usage: * ArduinoInstance.send("some data here"); * * ## EVENT: onDataReceived * ## handler of a listener object that listens to data sent from Arduino through the XMLSocket server(the serial Proxy) * usage: * Arduino_Listener = new Object(); //create a listener object * Arduino_Listener.onDataReceived = function() { * //handle the received data in here * } * ArduinoInstance.addEventListener("onReceiveData",Arduino_Listener); //register to listen to the events * * ## OTHER EVENTS: onConnect, onConnectError, onDisconnect * usage: use in the same way as the onDataReceived event * *----------------------------------------------------------------------------- * LICENCE * Copyright (C) 2005 beltran berrocal | b@progetto25zero1.com | * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License * as published by the Free Software Foundation; either version 2.1 of the License * * You should have received a copy of the GNU Lesser General Public License along with this library; * Alternatively you can find it here http://www.gnu.org/licenses/lgpl.html * * Brief Explanation of the Licence: * - you can you use, redistribute, modify this software for free, * - you can put it into commercial projects * - but if you modify/enhance this Library you should release it under the same licence or alternatively under the GPL licence * - in all cases you should also credit the original author and all contributors * * *----------------------------------------------------------------------------- */ import mx.events.EventDispatcher; class Arduino extends XMLSocket { private var _connected :Boolean = false; // is connected? private var _host :String = "127.0.0.1"; // Host name or IP address private var _port :Number = 5333 //read the config file of the socket server for this one //EVENT DISPATCH MIXIN function addEventListener(){} function removeEventListener(){} function dispatchEvent(){} function dispatchQueue(){} //constructor - provide a port number and a host ip adress //read the documentation of the SerialProxy to better understandwhat this means function Arduino(port, host) { //initialize super(); mx.events.EventDispatcher.initialize(this); //trace("** Arduino ** initilizing constructor"); //check if the selected port is correct or set default if(port == undefined) { trace("** Arduino ** default port:"+_port+" initialized! to change it use new Arduino(onPortNumber)") } else if ((_port < 1024) || (_port > 65535)) { trace("** Arduino ** Port must be from 1024 to 65535 ! read the Flash Documentation and the serProxy config file to better understand") } else { _port = port; } //check for host or set default if(host != undefined) { _host = host; } //register and override responder functions this.onConnect = onConnectToSocket; this.onClose = onDisconnectSocket; this.onData = onDataReceived; //autoconnect connect(); } //--------------------------------------- // CONNECT and DISCONNECT + responders //--------------------------------------- //connect to the XMLsocket public function connect () { trace("** Arduino ** Connecting to "+_host+":"+_port+" . . ."); super.connect(_host,_port); } //disconnects from the xmlsocket (not Arduino itself) public function disconnect () { if (_connected) { trace("** Arduino ** disconnecting"); this.close() _connected = false; } } //XMLsocket responders private function onConnectToSocket (success) { //trace("** Arduino ** onConnectToSocket"); if (success) { _connected = true; //launch event trace ("** Arduino ** Connection established.") e_connectToSocket(); } else { trace ("** Arduino ** Connection failed! you must launch the serialProxy first"); e_connectToSocketError(); } } //XMLsocket responders private function onDisconnectSocket (success) { _connected = false; //trace ("** Arduino ** onDisconnectSocket ** Connection closed by remote host."); //launch event e_disconnectSocket() } //--------------------------------------- // SEND and receive data from server //--------------------------------------- //sends data to arduino public function send(dataStr:String) { //trace("** Arduino ** send:" + dataStr) if (_connected) { if (dataStr.length) { //trace("** Arduino ** Sending \""+dataStr+"\""); super.send(dataStr); } } } //overrides XMLSocket.onData in order to have pure string and not the full XML object private function onDataReceived (str:String) { //trace("** Arduino ** onDataReceived str:"+str); //launch event e_onReceiveData(str) } //--------------------------------------- // EVENTS //--------------------------------------- private function e_connectToSocket(){ var evt = {target:this, type:"onConnect"}; dispatchEvent(evt); } private function e_connectToSocketError(){ var evt = {target:this, type:"onConnectError"}; dispatchEvent(evt); } private function e_disconnectSocket(){ var evt = {target:this, type:"onDisconnect"}; dispatchEvent(evt); } private function e_onReceiveData (str:String){ var evt = {target:this, type:"onReceiveData"}; evt.data = str; dispatchEvent(evt); } }