Sections
You are here: Home Wiki OSC and AutoIt Script

OSC and AutoIt Script

AutoIt Script is a freeware scripting language for Microsoft Windows that can be used to create GUI programs. In this case I use AutoIt Script to control the LEDs on my makecontroller board and make them blink on and off. AutoIt Script development environment is available from http://www.autoitscript.com

;====================================================
; Example OSC Communication Demo for MakingThings.com
; using AutoItScript, http://www.autoitscript.com
;
; This script written by Khan Gorlewski and may
; be freely redistributed. No warranty for its use.
; thanks! Khan
;
;====================================================

Global $socket, $message, $packet

; the full path to mchelper on your computer; if not correct, you will be prompted for the new path, however edit this if need be.
$mchelper_path = "C:\Program Files\mchelper\mchelper.exe"
$title = "Make Controller Helper" ; the title of the mchelper window; never changes
$IP = "127.0.0.1" ; the host on which mchelper is running
$board_address = "COM3" ; I am using usb and com3 is what mchelper reports to use, try a different value if having trouble with your setup.
$port_number = 11000 ; the standard port between this script and mchelper. Do not change unless you know what you are doing.


; do something before entering the continuous loop, ie get things in their initial states or positions
Func My_Program_Init()
    ; this will show the board arrival message, but nothing else because after it is executed we enter the My_Program_Loop
    ; ShowPacket() will retrieve any incoming packet messages and display a popupbox with its contents. Program execution
    ; will pause until the user closes the dialogbox and then the program will continue where it left off.
    ShowPacket()
EndFunc   

; your program code goes in between Func My_Program_Loop() and EndFunc
; use the autoit help file to learn how to write your program ie, loops, operators, functions and gui controls. Let your imagination run wild!
; remember to have your program sleep by specifying a time in milliseconds even if not needed so you don't consume CPU
Func My_Program_Loop()
    Sleep(1000) ; time to pause in milliseconds
    LEDSwitchOff(0) ; choose from LEDSwitchOn(), LEDSwitchOff(), LEDToggleState(), ReadAnalogSensor(), ActuatorSwitchOn(), ActuatorSwitchOff(), ActuatorToggleState(), ServerMotorRotate()
    LEDSwitchOff(1)
    LEDSwitchOff(2)
    LEDSwitchOff(3)
    SendPacket() ; send all four previous commands in one packet. All leds will appear to switch on at the same time.   
    Sleep(1000) ; time to pause in milliseconds
    LEDSwitchOn(0)
    LEDSwitchOn(1)
    LEDSwitchOn(2)
    LEDSwitchOn(3)
    SendPacket() ; send all four previous commands in one packet. In this case all leds will appear to switch off at the same time.
    ; use the autoit help file to learn to write your own code for this program loop!
EndFunc

; ======================================================
; DO NOT EDIT BELOW UNLESS YOU KNOW WHAT YOU ARE DOING
; ======================================================

MCHelperInit()

My_Program_Init()

; now that we are connected run the program loop; the actual 'inside' code for the loop is above ...
While 1
    My_Program_Loop()
WEnd

Func DoConnect()
    ToolTip("Connecting to mchelper ... please wait")
    ; wait a little bit just to be sure the mchelper has fully loaded!
    sleep(4000)
    ; clear the previous tooltip message
    ToolTip("")
    ; bring the mchelper window into view so we can see what is going on
    WinActivate($title)
    ; initialize TCP functionality for this script
    TCPStartup()
    ; connect to mchelper using our variables
    $socket = TCPConnect($IP, $port_number)
    ; if unsuccessful display our error message
    If $socket = -1 Then FailedConnect()
EndFunc


$LED_Error = "The LED you specified does not exist. There are only leds numbered 0 through 3; four total."

Func LEDSwitchOn($number)
    ; check to see that $number is with bounds
    If $number < 0 OR $number > 3 Then
        MsgBox(0, "Out of Bounds", $LED_Error)
        Return
    EndIf
    ; here we are forming the xml packet, the packet will be finalized in the SendPacket() Function
    $packet = $packet & "  <MESSAGE NAME=""/appled/" & $number & "/state"">" & _
    "    <ARGUMENT TYPE=""i"" VALUE=""1""/>" & _
    "  </MESSAGE>"
   
EndFunc

Func LEDSwitchOff($number)
    ; check to see that $number is with bounds
    If $number < 0 OR $number > 3 Then
        MsgBox(0, "Out of Bounds", $LED_Error)
        Return
    EndIf
    ; ; here we are forming the xml packet, the packet will be finalized in the SendPacket() Function
    $packet = $packet & "  <MESSAGE NAME=""/appled/" & $number & "/state"">" & _
    "    <ARGUMENT TYPE=""i"" VALUE=""0""/>" & _
    "  </MESSAGE>"
EndFunc

Func LEDToggleState($number)

EndFunc

Func ReadAnalogSensor($number)

EndFunc

Func ActuatorSwitchOn($number)

EndFunc

Func ActuatorSwitchOff($number)

EndFunc

Func ActuatorToggleState($number)
   
EndFunc

Func ServoMotorRotate($speed, $position)
   
   
EndFunc

Func FailedConnect()
    MsgBox(0, "Error", "Could not connect to mchelper, maybe there is a port configuration error or perhaps more likely your make controller board is not plugged in the computer. In order for packet communication to take place, the make controller must be plugged-in." & @CRLF & @CRLF & "Listening on port: " & $port_number & " on host: " & $IP & "; check your mchelper dialog window for errors or plugin your make controller board to fix this problem.")
    MsgBox(0, "", "Now exiting your osc demo program, however mchelper is still running. Figure out what going on by looking at the mchelper program window dialogs.")
    Exit
EndFunc

Func GetPacket()
    $message =  BinaryToString(TCPRecv($socket, 2048))
EndFunc
Func ShowPacket()
    $message =  BinaryToString(TCPRecv($socket, 2048))
    If $message Then
        MsgBox(0, "The make controller has sent the following message:", $message)
    EndIf
EndFunc

Func SendPacket()
    $packet = "<OSCPACKET  PORT=""10000"" ADDRESS=""" & $board_address & """ TIME=""0"">" & $packet & "</OSCPACKET>"
    TCPSend($socket, $packet)
    ClearPacket()
EndFunc
Func ClearPacket()
    $packet = ""   
EndFunc

Func MCHelperInit()
    If ProcessExists("mchelper.exe") Then
        DoConnect()
    Else
        MsgBox(0, "", "Press OK to launch mchelper. It is required to communicate with your make controller board.")
        If not(FileExists($mchelper_path)) Then MsgBox(0, "", "Error locating mchelper. You will now be given a chance to locate it yourself.")
        While not(FileExists($mchelper_path))
            $mchelper_path = FileSelectFolder("Select the mchelper folder, " & @CRLF & "usually C:\Program Files\mchelper\ ...", "") & "mchelper.exe"
            If @error = 1 Then
                MsgBox(0, "Canceling ...", "Unfortunately you must have mchelper installed on your system to use this osc demo communication script." & @CRLF & "The program will now exit. Thank you! To download mchelper, go to the http://www.makingthings.com website.")
                Exit
            ElseIf @error = 0 Then
                MsgBox(0, "File Not Found", "The file """ & $mchelper_path & """ does not exist. Please make sure mchelper.exe is installed in the folder you have chosen. Usually mchelper is found in the folder ""C:\Program Files\mchelper\"", however there were problems locating the mchelper executable automatically. You will now be given a chance to try again.")
            EndIf
        WEnd
        ShellExecute($mchelper_path, "", @SW_MAXIMIZE)
        While 1
            If WinExists($title) Then
                DoConnect()
                ExitLoop
            EndIf
            sleep(1000)
        WEnd
    EndIf
EndFunc



Document Actions
Log in


Forgot your password?
New user?