Upgrade to Pro — share decks privately, control downloads, hide ads and more …

如何使用Swift遙控你的樂高

 如何使用Swift遙控你的樂高

MarkFly

June 25, 2019
Tweet

More Decks by MarkFly

Other Decks in Programming

Transcript

  1. iOS

  2. 感應器比較 英⽂ 別名 縮寫 概念 加速計 Accelerometer 重⼒感應器 G-Sensor 對空間

    陀螺儀 Gyroscope 地感器 GYRO- Sensor 對⾃⾝ 磁⼒計 Magnetic 磁感器 M-Sensor 對地球 加速計/陀螺儀/磁⼒計是什麼,3軸/6軸/9軸感測器⼜是什麼?
  3. COREMOTION • CMMotionManager • 沒有Singleton • 加速計 • isAccelerometerAvailable •

    被動 • accelerometerUpdateInterval • startAccelerometerUpdates(queue, withHandler) • 主動 • startAccelerometerUpdates() • accelerometerData.acceleration
  4. COREMOTION • CMMotionManager • 陀螺儀/磁⼒計, 也是那些屬性那些⽅法, ⼀模模⼀樣樣, 換個名字⽽已 • CM四⼤天王除了上述三個之外,

    還有⼀個deviceMotion, 尚未研究... • 計步器:CMStepCounter > CMPedometer • 氣壓計:CMAltimeter
  5. EXTERNALACCESSORY • EASession • InputStream • OutputStream • StreamDelegate •

    stream(_ aStream: Stream, handle eventCode: Stream.Event)
  6. COMMANDS - OFFICIAL DESCRIPTION • DirectCommands (Ev3DirectCommand.swift) These commands are

    "one off" commands and are executed immediately. These are great if you only need to do a single operation at a time, but can be very slow when calling a series of them in very quick succession. There are a lot of other existing direct commands. You can just look in the code. A few are described further down. • BatchCommands (Ev3Command.swift) These commands are queued up until the queue of a command is send to the brick. You can imagine such a batch command as a program, which is build with the SDK, then send to the brick and then executed. • SystemCommands (Ev3SystemCommand.swift) System commands are also "one off" but cannot be batched. These commands are for uploading files and other system-level functions. At the moment there are no system commands implemented yet.
  7. EV3DIRECTCOMMAND //起動⾺達(動⼒是絕對值, 速度是百分比) • public func turnMotorAtPower(onPorts ports: OutputPort, withPower

    power: Int16) • public func turnMotorAtSpeed(onPorts ports: OutputPort, withSpeed speed: Int16) //根據三種步驟啟動⾺達 • public func stepMotorAtSpeed(ports: OutputPort, speed: Int16, rampUpSteps: UInt32, constantSteps: UInt32, rampDownSteps: UInt32, brake: Bool) • public func stepMotorAtPower(ports: OutputPort, power: Int16, rampUpSteps: UInt32, constantSteps: UInt32, rampDownSteps: UInt32, brake: Bool) //上述便捷⽅法(up跟down step為0) • public func stepMotorAtSpeed(ports: OutputPort, speed: Int16, steps: UInt32, brake: Bool) • public func stepMotorAtPower(ports: OutputPort, power: Int16, steps: UInt32, brake: Bool)
  8. //讓兩個⾺達保持同步(例如走直線) • public func stepMotorSync(ports: OutputPort, speed: Int16, turnRatio: Int16,

    step: UInt32, brake: Bool) • public func timeMotorSync(ports: OutputPort, speed: Int16, turnRatio: Int16, time: UInt32, brake: Bool) //起動⾺達並限定時間長度 • public func turnMotorAtSpeedForTime(ports: OutputPort, speed: Int16, milliseconds: UInt32, brake: Bool) • public func turnMotorAtSpeedForTime(ports: OutputPort, speed: Int16, msRampUp: UInt32, msConstant: UInt32, msRampDown: UInt32, brake: Bool) //停⽌⾺達 • public func stopMotor(onPorts ports: OutputPort, withBrake brake: Bool) EV3DIRECTCOMMAND
  9. //開燈(紅/綠/橘) • public func setLedPattern(ledPattern: LedPattern) //取得資訊類 • public func

    getFirmwareVersion(receivedFirmware: @escaping (String?) -> Void) • public func getDeviceName(port: InputPort, receivedDeviceName: @escaping (String?) -> Void) • public func getBatteryLevel(receivedBatLevel: @escaping (UInt8?) -> Void) • public func isBrickButtonPressed(button: BrickButton, receivedButtonState: @escaping (Bool?) -> Void) EV3DIRECTCOMMAND
  10. //播放類 • public func playSound(volume: UInt8, filename: String) • public

    func playTone(volume: UInt8, frequency: UInt16, duration: UInt16) //讀取原始資料 • public func readyRaw(port: InputPort, mode: UInt8, receivedRaw: @escaping (NSData?) -> Void) //清除計數 • public func clearCount(ports: OutputPort) //執⾏⽤⼾的程式 • public func programStart(name: String, debug: Bool) EV3DIRECTCOMMAND