In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

N900 sensor connection

For Media Lab Helsinki class http://mlab.taik.fi/paja/
Connects to Nokia N900 via http://yunta.dainet.pl/programming/fizikal/

(Needs to be built for Flash Player 9 to run on the phone.)
/**
 * Copyright forresto ( http://wonderfl.net/user/forresto )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/vKIU
 */

package {
    // For Media Lab Helsinki class http://mlab.taik.fi/paja/
    // Connects to Nokia N900 via http://yunta.dainet.pl/programming/fizikal/
    
    
    //import PhysicalN900.*;
    
    import flash.display.Shape;
    import flash.display.Sprite;
    import flash.events.Event;
    
    
    public class N900ex1 extends Sprite {
        
        private var ip:String="localhost";
        private var port:String="10001";
        
        private var input1:String="";
        private var input2:String="";
        private var input3:String="";
        private var method:String="acceleration";
        
        private var a:Array;
        
        private var posX:Number = 200;
        private var posY:Number = 200;
        private var speedX:Number = 0;
        private var speedY:Number = 0;
        
        private var myPhysicalN900:PhysicalN900;
        
        private var myDrawing:Shape;
        
        public function N900ex1() {
            // Setup
            myPhysicalN900 = new PhysicalN900(ip, port);
            
            var counter:int = 0;
            stage.addEventListener(Event.ENTER_FRAME, myLoop);
            
            myDrawing = new Shape();
            addChild(myDrawing);
            myDrawing.graphics.lineStyle(3, 0xCC00FF, .25);
            myDrawing.graphics.moveTo(posX-1, posY-1);
            myDrawing.graphics.lineTo(posX,posY);
            
        }
        
        private function myLoop(e:Event):void{
            checkAcceleration();
            posX+=speedX;
            posY+=speedY;
            myDrawing.graphics.lineTo(posX,posY);
        }
        
        private function checkAcceleration():void{
            port="10001";
            method="acceleration";
            myPhysicalN900.requestData(gotAccelData,gotError,method,input1,input2,input3);
        }
        private function gotAccelData(type:String, data:String):void{
            //trace(str);
            a = data.split(" ");
            speedX = a[0]/100;
            speedY = a[1]/100;    
        }
        
        private function gotData(type:String, data:String):void{
            //trace(str);
        }
        private function gotError(str:String, str2:String):void{
            //trace(str);
        }
        
        
    }
    
}    





// PhysicalN900 library version 1.0

import flash.events.*;
import flash.net.*;
import flash.system.Security;
import flash.utils.getTimer;


class PhysicalN900 {
    
    private var responseHandler:Function;
    private var errorHandler:Function;
    private var activeMethod:String;
    
    public function PhysicalN900(url:String, port:String) {
        //loading policy
        Security.loadPolicyFile("http://" + url + ":" + port);
        
        // start listening to events
        loader.addEventListener(Event.COMPLETE, completeHandler);
        loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
        loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
        
        request=new URLRequest("http://"+url+":"+port);
    }
    
    public function requestData(handler:Function,errorHandler_:Function, method:String,str1:String=null, str2:String=null, str3:String=null):void {
        errorHandler=errorHandler_;
        
        if (lastRequestedTime==0) {
            responseHandler=handler;
            activeMethod=method;
            // Prepare XML
            var myXMLRPC:XML=new XML(xmlRpcTemplate);
            // Add method name
            myXMLRPC.methodName[0]=method;
            // Add data
            if (str1&&str1.length!=0) {
                myXMLRPC.params[0].appendChild(new XML("<param><value><string>" + str1 +  "</string></value></param>"));
            }
            if (str2&&str2.length!=0) {
                myXMLRPC.params[0].appendChild(new XML("<param><value><string>" + str2 +  "</string></value></param>"));
            }
            if (str3&&str3.length!=0) {
                myXMLRPC.params[0].appendChild(new XML("<param><value><string>" + str3 +  "</string></value></param>"));
            }
            
            // Submit the XML
            request.method=URLRequestMethod.POST;
            request.contentType="text/xml; charset=utf-8";
            request.data = "<?xml version=\"1.0\"?>" + myXMLRPC.toString() + "/n";
            try {
                lastRequestedTime=getTimer();
                loader.load(request);
            } catch (error:Error) {
                errorHandler(activeMethod, "[Flash error] Unable to load requested document.");
            }
            //trace(myXMLRPC);
        } else {
            errorHandler(activeMethod, "[Flash warning] Cannot request until the last request completed.");
        }
    }
    
    private var xmlRpcTemplate:String="<methodCall><methodName>echo</methodName><params></params></methodCall>";
    private var request:URLRequest;
    private var loader:URLLoader = new URLLoader();
    private var lastRequestedTime:uint=0;
    
    private function completeHandler(event:Event):void {
        var xml:XML=new XML(loader.data);
        lastRequestedTime=0;
        
        var data:String;
        if (xml.fault[0]) {
            data=xml.fault[0].value[0].struct[0].member[1].value[0].string[0];
            errorHandler(activeMethod, "[Server error] " + data);
        } else {
            //trace(xml);
            data=xml.params[0].param[0].value[0].string[0];
            responseHandler(activeMethod, data);
        }
        
    }
    
    // Error handling
    private function securityErrorHandler(event:SecurityErrorEvent):void {
        errorHandler(activeMethod, "[Flash error] " + event.toString());
        lastRequestedTime=0;
    }
    
    private function ioErrorHandler(event:IOErrorEvent):void {
        errorHandler(activeMethod, "[Flash error] " + event.toString());
        lastRequestedTime=0;
    }
}