在F#中将MonoBrickFirmware.dll库ev3 Button值作为十六进制

时间:2014-11-11 16:09:43

标签: f#

            let buts: ButtonEvents = new ButtonEvents()

            let event =
                match buts with
                    | add_UpPressed     ->
                        Vehicle(pen, pen).Forward(SPEED)
                    | add_DownPressed   ->
                        Vehicle(pen, pen).Backward(SPEED)
                    | UpReleased    ->
                        Vehicle(pen, pen).Off()
                    | DownReleased  ->
                        Vehicle(pen, pen).Off()
                    | RightPressed  ->
                        Vehicle(assembly, assembly).Forward(SPEED)
                    | LeftPressed   ->
                        Vehicle(assembly, assembly).Backward(SPEED)
                    | RightReleased ->
                        Vehicle(assembly, assembly).Off()
                    | LeftReleased  ->
                        Vehicle(assembly, assembly).Off()

这是我在F#中的代码,我使用的是MonoBrickFirmware.dll库。为了获得Button的价值,我试图定义" buts"。

但它不起作用,但仍然无法将Button的值设为十六进制或其他类型。

1 个答案:

答案 0 :(得分:1)

按钮事件在F#中以下列方式工作(您只需将代码复制到新创建的F#控制台应用程序中的Program.fs文件中):

module MyEV3FSharpExperiment

open MonoBrickFirmware.UserInput
open MonoBrickFirmware.Display
open System.Threading


// The main function that is called when the EXE is run on MonoBrick.
[<EntryPoint>]
let main argv = 
    let terminateProgram = new ManualResetEvent(false)
    let touchSensor = EV3TouchSensor(SensorPort.In1)
    // Initialize the button event
    let buts = new ButtonEvents ()
    // Add a handler function to the up button pressed event.
    buts.add_UpPressed((fun  () -> 
        LcdConsole.WriteLine("Sensor value:" + touchSensor.ReadAsString())
        () // The function is expeced to return a unit.
    ))
    // Add a handler for the event of the Escape button being pressed.
    buts.add_EscapePressed (fun () ->
        let _ =terminateProgram.Set()
        () // The function is expeced to return a unit.
    )

    // Add more event handlers here

    // Wait for the escape button to be pressed to quit. Program
    // execution will block at the following line until terminate event is set.
    let _ = terminateProgram.WaitOne()
    0 // The return value of the main method.

请注意,使用--standalone编译器开关编译代码是值得的。在Visual Studio 2013中,您可以在项目属性中设置它(右键单击解决方案资源管理器中的项目 - &gt;属性),然后从左侧选择“构建”并将--standalone写入other flags字段。当您转到项目设置中的“应用程序”选项卡时,选择目标运行时为“F#3.0(FSharp.Core 4.3.0.0)”,框架为“.Net Framework 4.0”,输出类型为“控制台应用程序” ”