Make Controller Kit

ActionScript 3.0 Documentation

McFlashConnect

com.makingthings.makecontroller.McFlashConnect

Classes:

Back to front page...

Last Updated: August 30, 2007


Class Information

Mchelper (Make Controller Helper) is a separate application that must be run simultaneously with your Flash application. Because Flash can't connect to external devices by itself, Mchelper is a necessary intermediate step - it can connect to the Make Controller over the network and via USB, and then it formats communication with the board into XML that can be fed in and out of Flash. McFlashConnect makes a connection to Mchelper from your Flash movie.

The general idea is that you:

  1. Connect to mchelper.
  2. Receive info about which boards are connected.
  3. Send and/or receive messages to any of those boards.
  4. Receive info when mchelper has disconnected.

McFlashConnect emits a few events of interest - check these out at in the in the McEvent section.

The easiest way to get started is to check out a few of the example files included in the MakingThings download, and start modifying things to fit your project.


Constructor

McFlashConnect

function McFlashConnect( mchelperAddress:String = "localhost", mchelperPort:Number = 11000 )

Create a new instance of McFlashConnect.

Parameters:

mchelperAddress:

An optional string specifying the IP address of the computer running mchelper.

mchelperPort:

An optional Number specifying the port that mchelper is listening on.

Also see: (none)

Properties

mchelperAddress

public var mchelperAddress:String

The IP address of the machine running Mchelper.

Since this usually on the same machine you're running your Flash app on, it's usually best to set this to localhost


Also see: (none)

mchelperPort

public var mchelperPort:Number

The port that Mchelper is listening for connections from McFlashConnect on. Default is 11000.

Also see: (none)

Methods

setDefaultBoard

public function setDefaultBoard( board:Board ) : void

Set the board to send messages to when the send( ) method is used. If no default board is set, you need to specify which board to send to using sendToBoard( ). It can be convenient to set this when a new board arrives.

Example

		function onBoardArrived( event:McEvent )
		{
			var newBoard:Board == event.data;
			mcFlash.setDefaultBoard( newBoard );
		}
		

Parameters:

board:

A Board object indicating which board to send to.

Returns:

(none)

Also see: (none)

getBoardByIPAddress

public function getBoardByIPAddress( address:String ) : Board

From the list of connected boards, find a board with a given IP address.

Example

		// find a board with the IP address 192.168.0.200
		var myBoard:Board = getBoardByIPAddress( "192.168.0.200" );
		

Parameters:

address:

A string specifying the IP address of the board to find.

Returns:

The Board object with the given IP address, or null if it wasn't found.

Also see: (none)

getBoardByUsbLocation

public function getBoardByUsbLocation( location:String ) : Board

From the list of connected boards, find a board at a given USB location. On Windows, this will be a COM port, otherwise it will be the device location.

Example

		// find a board with the USB location COM9
		var myBoard:Board = getBoardByUsbLocation( "COM9" );
		

Parameters:

location:

A string specifying the USB location of the board to find.

Returns:

The Board object with the given USB location, or null if it wasn't found.

Also see: (none)

getBoardsByName

public function getBoardsByName( name:String ) : Array

From the list of connected boards, find all boards with a given name. Because many boards can potentially have the same name, this will return an Array of Board objects with the given name.

Example

		// find all boards named "Make Controller Kit"
		var myBoards:Array = getBoardsByName( "Make Controller Kit" );
		

Parameters:

name:

A string specifying the name of the board to find.

Returns:

An Array with all the boards with the given name. If no boards were found, this Array will be empty.

Also see: (none)

getBoardsBySerialNumber

public function getBoardsBySerialNumber( serialnum:Number ) : Array

From the list of connected boards, find all boards with a given serial number. Because many boards can potentially have the same serial number, this will return an Array of Board objects with the given number.

Example

		// find all boards with the serial number 18934
		var myBoards:Array = getBoardsBySerialNumber( 18934 );
		

Parameters:

serialnum:

A Number specifying the serial number of the board to find.

Returns:

An Array with all the boards with the given serial number. If no boards were found, this Array will be empty.

Also see: (none)

addAddressListener

public function addAddressListener( addr:String, callback:Function ):void

Register a handler for messages with a given OSC address.

You can create a function of your own that will get called whenever an incoming OSC message matches a particular address. This saves you from checking the address of all incoming OSC messages.

Your callback should be in the form:

 myCallback( msg:OscMessage ); 

Example

		mcflash.addAddressListener( "/analogin/7/value", onTrimpot );
		
		// this will get called whenever a message from "/analogin/7/value" is received.
		function onTrimpot( msg:OscMessage ) 
		{
			trace( "new OSC message: " + msg.toString() );
		}
		

Parameters:

addr:

The OSC address to match.

callback:

The function that you want to be called back on.

Returns:

(none)

Also see: (none)

connected

public function connected( ):Boolean

Query whether your Flash movie is currently connected to mchelper.

Example

		// test to see if we're connected.
		if( mcFlash.connected( ) )
		{
			// then do something here...
		}
		

Parameters:

(none)

Returns:

A Boolean value - true if connected, false if not connected.

Also see: (none)

connect

public function connect( ) : void

Connect McFlashConnect up to Mchelper. This will connect using the current values of mchelperAddress and mchelperPort.

Example

 
		mcflash.connect( );
		

Parameters:

(none)

Returns:

(none)

Also see: (none)

disconnect

public function disconnect( ) : void

Disconnect from the mchelper server.

This closes the XML connection to mchelper. This does not need to be called explicitly before closing your movie.

Example

 
		mcflash.disconnect( );
		

Parameters:

(none)

Returns:

(none)

Also see: (none)

send

public function send( address:String, args:Array ) : void

Send a message to the board.

This will send a message to the current defaultBoard, as set by setDefaultBoard( ). If you need to specify the board to send to, use sendToBoard( ).

Example

 
		// Specify the OSC address and argument to send - turn on LED 1
		mcflash.send( "/appled/1/state", [1] );
		
		// Or, to read the state of the LED...
		mcflash.send( "/appled/1/state", [] );
		

Parameters:

address:

The OSC address to send to, as type \b String.

arg:

The value to be sent. Send an empty Array if there are no arguments.

Returns:

(none)

Also see: sendToBoard( )

sendToBoard

public function sendToBoard( address:String, args:Array, board:Board ) : void

Send a message to a given board.

Example

 
		// Specify the OSC address and argument to send - turn on LED 1
		var myBoard:Board = mcflash.getBoardByIPAddress( "192.168.0.200" );
		mcflash.sendToBoard( "/appled/1/state", [1], myBoard );
		

Parameters:

address:

The OSC address of your message.

args:

The value(s) to be sent.

board:

The board to send to.

Returns:

(none)

Also see: (none)

sendMessage

public function sendMessage( oscM:OscMessage ) : void

Send an OscMessage to a board.

This will send a message, of type OscMessage, to the current defaultBoard. If you need to specify another board to send to, use sendMessageToBoard( ).

Example

 
		// create an OscMessage
		var turnOnLed:OscMessage = new OscMessage( "/appled/0/state", [1] );
		// now send it
		mcflash.sendMessage( turnOnLed );
		

Parameters:

oscM:

The message, of type OscMessage, to be sent

Returns:

(none)

Also see: sendMessageToBoard( )

sendMessageToBoard

public function sendMessageToBoard( oscM:OscMessage, board:Board ) : void

Send an OscMessage to a given board.

Example

 
		// create an OscMessage
		var turnOffLed:OscMessage = new OscMessage( "/appled/0/state", [0] );
		// now send it to a specified address
		var myBoard:Board = mcflash.getBoardByIPAddress( "192.168.0.200" );
		mcflash.sendMessageToAddress( turnOffLed, myBoard );
		

Parameters:

oscM:

The message, of type OscMessage, to be sent

board:

The board to send to.

Returns:

(none)

Also see: (none)

sendBundle

public function sendBundle( oscB:Array ) : void

Send an OscBundle to the board.

This will send a bundle to the current defaultBoard. If you need to specify another board to send to, use sendBundleToBoard( ).

It's a good idea to send bundles when possible, in order to reduce the traffic between the board and Flash. If you need to specify the address and port for each message, use sendBundleToAddress( ).

Example

		// create an Array (this Array will be our OscBundle)
		// and stuff our messages into it
		var myOscBundle:Array = new Array( );
		
		// create a couple of OscMessages
		myOscBundle.push( new OscMessage( "/appled/0/state", [0] ) ); // turn off LED 0
		myOscBundle.push( new OscMessage( "/analogin/0/value", [] ) ); // ask for analogin 0

		// now send it
		mcflash.sendBundle( myOscBundle );
		

Parameters:

oscB:

The OscBundle to be sent

Returns:

(none)

Also see: sendBundleToBoard( )

sendBundleToBoard

public function sendBundleToBoard( oscB:Array, board:Board ) : void

Send an OscBundle to a given board.

Example

 
		// create an Array (this Array will be our OscBundle)
		// and stuff our messages into it
		var myOscBundle:Array = new Array( );
		
		// create a couple of OscMessages
		myOscBundle.push( new OscMessage( "/appled/0/state", [0] ) ); // turn off LED 0
		myOscBundle.push( new OscMessage( "/analogin/0/value", [] ) ); // ask for analogin 0
		
		// now send it to a specified board
		var myBoard:Board = mcflash.getBoardByIPAddress( "192.168.0.200" );
		mcflash.sendBundleToAddress( myOscBundle, myBoard );
		

Parameters:

oscB:

The OscBundle to be sent.

board:

The board to send to.

Returns:

(none)

Also see: (none)

The Make Controller Kit is an open source project maintained by MakingThings.
MakingThings code is released under the Apache 2.0 license.
Bug tracker, development wiki and status can be found at http://dev.makingthings.com.
Documentation produced with Zendoc.