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

PSYCHICDOLL

Get Adobe Flash player
by alumican_net 27 May 2011
/**
 * Copyright alumican_net ( http://wonderfl.net/user/alumican_net )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/loii
 */

package
{
    import com.bit101.components.HUISlider;
    import com.bit101.components.TextArea;
    import flash.display.Sprite;
    import flash.events.ErrorEvent;
    import flash.events.Event;
    import funnel.*;
    import funnel.gui.*;
    import funnel.ui.*;
    
    /**
     * PSYCHICDOLL (LEDOLL + MindSet + Gainer)
     * 
     * LEDOLL  http://www.youtube.com/watch?v=q8tBX0x_R7U
     * MindSet http://neurosky.com/
     * Gainer  http://gainer.cc/
     * 
     * @author Yukiya Okuda<alumican.net>
     */
    [SWF(backgroundColor=0xffffff, frameRate=60)]
    public class Main extends Sprite
    {
        //Arduino
        private var _arduino:Arduino;
        private var _attLED:LED;
        private var _medLED:LED;
        
        //MindSet
        private var _mindSet:MindSet;
        
        //Interface
        private var _attentionSlider:HUISlider;
        private var _meditationSlider:HUISlider;
        
        private var _socketLog:TextArea;
        private var _rawEegLog:TextArea;
        private var _signalLog:TextArea;
        private var _eyeBlinkLog:TextArea;
        
        private var _minSlider:HUISlider;
        private var _maxSlider:HUISlider;
        
        private var _minValue:Number = 0.3;
        private var _maxValue:Number = 0.7;
        
        //Constructor
        public function Main():void
        {
            _setupArduino();
            _setupMindSet();
            _setupInterface();
        }
        
        private function _setupArduino():void
        {
            var config:Configuration = Arduino.FIRMATA;
            config.setDigitalPinMode(10, PWM);
            config.setDigitalPinMode(11, PWM);
            _arduino = new Arduino(config);
            _attLED = new LED(_arduino.digitalPin(10));
            _medLED = new LED(_arduino.digitalPin(11));
        }
        
        private function _setupMindSet():void
        {
            _mindSet = new MindSet();
            _mindSet.addEventListener(MindSet.SOCKET_CONNECT, _mindSetSocketConnectHandler);
            _mindSet.addEventListener(MindSet.SOCKET_ERROR, _mindSetSocketErrorHandler);
            _mindSet.addEventListener(MindSet.DATA_SIGNAL, _mindSetDataSignalHandler);
            _mindSet.addEventListener(MindSet.DATA_EEG_RAW, _mindSetDataEegRawHandler);
            _mindSet.addEventListener(MindSet.DATA_EYE_BLINK, _mindSetDataEyeBlinkHandler);
            _mindSet.connect();
        }
        
        private function _setupInterface():void
        {
            //----------------------------------------
            _attentionSlider = new HUISlider(this, 14, 10, "ATTENTION", null);
            _attentionSlider.width = 300;
            
            _meditationSlider = new HUISlider(this, 10, 30, "MEDITATION", null);
            _meditationSlider.width = 300;
            
            _rawEegLog = new TextArea(this);
            _rawEegLog.x      = 10;
            _rawEegLog.y      = 60;
            _rawEegLog.width  = 220;
            _rawEegLog.height = 50;
            
            _eyeBlinkLog = new TextArea(this);
            _eyeBlinkLog.x      = 10;
            _eyeBlinkLog.y      = 130;
            _eyeBlinkLog.width  = 220;
            _eyeBlinkLog.height = 50;
            
            _signalLog = new TextArea(this);
            _signalLog.x      = 10;
            _signalLog.y      = 200;
            _signalLog.width  = 220;
            _signalLog.height = 150;
            
            _minSlider = new HUISlider(this, 300, 10, "MIN", function(e:Event):void
            {
                _minValue = _minSlider.value;
            });
            _minSlider.width -= 30;
            _minSlider.minimum = 0;
            _minSlider.maximum = 1;
            _minSlider.value = 0.3;
            
            _maxSlider = new HUISlider(this, 300, 30, "MAX", function(e:Event):void
            {
                _maxValue = _maxSlider.value;
            });
            _maxSlider.width -= 30;
            _maxSlider.minimum = 0;
            _maxSlider.maximum = 1;
            _maxSlider.value = 0.6;
            
            //----------------------------------------
            _arduino.gui = addChild( new ArduinoGUI() ) as ArduinoGUI;
        }
        
        private function _mindSetSocketConnectHandler(e:Event):void
        {
            trace("connect");
        }
        
        private function _mindSetSocketErrorHandler(e:ErrorEvent):void
        {
            trace("error : ", e.text);
        }
        
        private function _mindSetDataSignalHandler(e:Event):void 
        {
            trace("attention  : ", _mindSet.attention);
            trace("meditation : ", _mindSet.meditation, "\n");
            
            //----------------------------------------
            _attentionSlider.value = 100 * _mindSet.attention;
            _meditationSlider.value = 100 * _mindSet.meditation;
            
            _log("----------SIGNAL----------\n"
                + "Signal Level : " + _mindSet.signalLevel + "\n\n"
                + "Alpha (Low)  : " + _mindSet.lowAlpha + "\n"
                + "Alpha (High) : " + _mindSet.highAlpha + "\n"
                + "Beta (Low)   : " + _mindSet.lowBeta + "\n"
                + "Beta (High)  : " + _mindSet.highBeta + "\n"
                + "Gamma (Low)  : " + _mindSet.lowGamma + "\n"
                + "Gamma (High) : " + _mindSet.highGamma + "\n"
                + "Delta        : " + _mindSet.delta + "\n"
                + "Theta        : " + _mindSet.theta
            , _signalLog);
            
            //----------------------------------------
            
          //  /*
            if (_mindSet.attention > _mindSet.meditation)
            {
                _attLED.intensity = _adjustValue(Math.abs(_mindSet.attention - _mindSet.meditation));
                _medLED.intensity = 0;
            }
            else
            {
                _attLED.intensity = 0;
                _medLED.intensity = _adjustValue(Math.abs(_mindSet.attention - _mindSet.meditation));
            }
          //  */


            /*
            _attLED.intensity = _adjustValue(_mindSet.attention);
            _medLED.intensity = _adjustValue(_mindSet.meditation);
            */
        }
        
        private function _adjustValue(value:Number):Number
        {
            return (value <= _minValue) ? 0 : 
                   (value >= _maxValue) ? 1 : ((value - _minValue) / (_maxValue - _minValue));
        }
        
        private function _mindSetDataEegRawHandler(e:Event):void 
        {
            _log("----------RAW EEG----------\n"
                + "Raw Eeg : " + _mindSet.rawEeg
            , _rawEegLog);
        }
        
        private function _mindSetDataEyeBlinkHandler(e:Event):void 
        {
            _log("----------EYE BLINK----------\n"
                + "Blink Strength : " + _mindSet.blinkStrength
            , _eyeBlinkLog);
        }
        
        private function _log(m:*, target:TextArea = null):void
        {
            if (target)
            {
                target.text = String(m);
            }
            else
            {
                trace(String(m));
            }
        }
    }
}

/**
 * MindSet
 * @author Yukiya Okuda<alumican.net>
 */
import com.adobe.serialization.json.JSON;
import com.adobe.serialization.json.JSONParseError;
import flash.events.ErrorEvent;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.events.SecurityErrorEvent;
import flash.net.Socket;

internal class MindSet extends EventDispatcher
{
    //Event
    static public const DATA_SIGNAL:String = "dataSignal";
    static public const DATA_EEG_RAW:String = "dataEegRaw";
    static public const DATA_EYE_BLINK:String = "dataEyeBlink";
    
    static public const SOCKET_CONNECT:String = "socketConnect";
    static public const SOCKET_ERROR:String = "socketError";
    static public const SOCKET_CLOSE:String = "socketClose";
    
    //Sensor Value
    public function get signalLevel():Number { return _signalLevel; }
    private var _signalLevel:Number;
    
    public function get attention():Number { return _attention; }
    private var _attention:Number;
    
    public function get meditation():Number { return _meditation; }
    private var _meditation:Number;
    
    public function get lowAlpha():Number { return _lowAlpha; }
    private var _lowAlpha:Number;
    
    public function get highAlpha():Number { return _highAlpha; }
    private var _highAlpha:Number;
    
    public function get lowBeta():Number { return _lowBeta; }
    private var _lowBeta:Number;
    
    public function get highBeta():Number { return _highBeta; }
    private var _highBeta:Number;
    
    public function get lowGamma():Number { return _lowGamma; }
    private var _lowGamma:Number;
    
    public function get highGamma():Number { return _highGamma; }
    private var _highGamma:Number;
    
    public function get delta():Number { return _delta; }
    private var _delta:Number;
    
    public function get theta():Number { return _theta; }
    private var _theta:Number;
    
    public function get rawEeg():Number { return _rawEeg; }
    private var _rawEeg:Number;
    
    public function get lowRawEeg():Number { return _lowRawEeg; }
    private var _lowRawEeg:Number;
    
    public function get highRawEeg():Number { return _highRawEeg; }
    private var _highRawEeg:Number;
    
    public function get blinkStrength():Number { return _blinkStrength; }
    private var _blinkStrength:Number;
    
    //Connector
    private var _socket:Socket;
    
    public function MindSet():void
    {
        _signalLevel = _attention = _meditation = _lowAlpha = _highAlpha = _lowBeta = _highBeta =
        _lowGamma = _highGamma = _delta = _theta = _rawEeg = _lowRawEeg = _highRawEeg = _blinkStrength = 0;
        
    }
    
    public function connect():void
    {
        close();
        _socket = new Socket();
        _socket.addEventListener(Event.CONNECT, _socketConnectHandler);
        _socket.addEventListener(ProgressEvent.SOCKET_DATA, _socketDataHandler);
        _socket.addEventListener(IOErrorEvent.IO_ERROR, _socketErrorHandler);
        _socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, _socketErrorHandler);
        _socket.addEventListener(Event.CLOSE, _socketCloseHandler);
        _socket.connect("127.0.0.1", 13854);
    }
    
    public function close():void
    {
        if (!_socket) return;
        _socket.removeEventListener(Event.CONNECT, _socketConnectHandler);
        _socket.removeEventListener(ProgressEvent.SOCKET_DATA, _socketDataHandler);
        _socket.removeEventListener(IOErrorEvent.IO_ERROR, _socketErrorHandler);
        _socket.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, _socketErrorHandler);
        _socket.removeEventListener(Event.CLOSE, _socketCloseHandler);
        try
        {
            _socket.close();
        }
        catch (erroe:Error) { }
        _socket = null;
    }
    
    //Socket Handler
    private function _socketConnectHandler(e:Event):void 
    {
        _socket.writeUTFBytes(JSON.encode( { enableRawOutput : true, format : "Json" } ));
        dispatchEvent(new Event(SOCKET_CONNECT));
    }
    
    private function _socketErrorHandler(e:ErrorEvent):void 
    {
        close();
        dispatchEvent(new ErrorEvent(SOCKET_ERROR, false, false, e.text));
    }
    
    private function _socketCloseHandler(e:Event):void 
    {
        close();
        dispatchEvent(new Event(SOCKET_CLOSE));
    }
    
    private function _socketDataHandler(e:ProgressEvent):void 
    {
        var packets:Array = _socket.readUTFBytes(_socket.bytesAvailable).split(/\r/);
        _socket.flush();
        
        for each (var packet:String in packets)
        {
            if (packet == "") continue;
            try
            {
                var data:Object = JSON.decode(packet);
                //----------------------------------------
                if (data["poorSignalLevel"] != null)
                {
                    _signalLevel = 1 - data["poorSignalLevel"] / 200; //[0, 1]
                    if(_signalLevel == 0) {
                        _attention = _meditation = _lowAlpha = _highAlpha = _lowBeta = 
                        _highBeta = _lowGamma = _highGamma = _delta = _theta = 0;
                    }
                    else
                    {
                        _attention = data["eSense"]["attention"] / 100; //[0, 1]
                        _meditation = data["eSense"]["meditation"] / 100; //[0, 1]
                        _lowAlpha = data["eegPower"]["lowAlpha"];
                        _highAlpha = data["eegPower"]["highAlpha"];
                        _lowBeta = data["eegPower"]["lowBeta"];
                        _highBeta = data["eegPower"]["highBeta"];
                        _lowGamma = data["eegPower"]["lowGamma"];
                        _highGamma = data["eegPower"]["highGamma"];
                        _delta = data["eegPower"]["delta"];
                        _theta = data["eegPower"]["theta"];
                    }
                    
                    dispatchEvent(new Event(DATA_SIGNAL));
                }
                //----------------------------------------
                if (data["blinkStrength"] != null)
                {
                    _blinkStrength = data["blinkStrength"];
                    dispatchEvent(new Event(DATA_EYE_BLINK));
                }
                //----------------------------------------
                if (data["rawEeg"] != null)
                {
                    _rawEeg = data["rawEeg"];
                    _lowRawEeg = (int(_rawEeg) >> 8) & 0xff;
                    _highRawEeg = int(_rawEeg) & 0xff;
                    dispatchEvent(new Event(DATA_EEG_RAW));
                }
            }
            catch (error:JSONParseError)
            {
            }                    
        }
    }
}