-
-
Save yhara/4f38a0a00aba26d7ec71e09eba0c6b2b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# 以下のコードを読み込むと、Brickクラスのメソッドが追加されます。 | |
# | |
# LEDを光らせる | |
# @brick.set_led(色) | |
# 色は0〜9の数値 | |
# 例: @brick.set_led(6) | |
# | |
# 音を鳴らす | |
# @brick.play_tone(音量, 周波数, 長さ) | |
# 音量は0〜100の数値 | |
# 周波数は250〜10000の数値 | |
# 長さはミリ秒単位 (1000を指定すると1秒間鳴る) | |
# 例: @brick.play_tone(80, 1200, 500) | |
module EV3 | |
module UiWriteSubcodes | |
LED = 27 # ev3sources/lms2012/lms2012/source/bytecodes.h | |
end | |
module SoundSubCodes | |
BREAK = 0 | |
TONE = 1 | |
PLAY = 2 | |
REPEAT = 3 | |
end | |
module Commands | |
class UiWrite < Base | |
# color: 0-9 | |
def set_led(color) | |
if !color.is_a?(Integer) || !(0..9).include?(color) | |
raise "invalid color: #{color.inspect}" | |
end | |
@message += | |
ByteCodes::UI_WRITE.bytes + | |
[UiWriteSubcodes::LED] + | |
[color] | |
return self | |
end | |
end | |
class Sound < Base | |
def stop_sound | |
@message += | |
ByteCodes::SOUND.bytes + | |
[SoundSubCodes::BREAK] | |
return self | |
end | |
def set_tone(volume, freq, duration) | |
@message += | |
ByteCodes::SOUND.bytes + | |
[SoundSubCodes::TONE] + | |
[volume] + | |
ArgumentSize::SHORT.bytes + | |
[freq].pack("v*").bytes + | |
ArgumentSize::SHORT.bytes + | |
[duration].pack("v*").bytes | |
return self | |
end | |
end | |
end | |
class Brick | |
def set_led(color) | |
Commands::UiWrite.new(@connection). | |
set_led(color). | |
command_type(CommandType::DIRECT_COMMAND_NO_REPLY). | |
send_command | |
end | |
# volume: 0-100 | |
# freq: 250-10000 | |
# duratinon: ミリ秒 (1000 = 1秒間) | |
def play_tone(volume, freq, duration) | |
Commands::Sound.new(@connection). | |
set_tone(volume, freq, duration). | |
command_type(CommandType::DIRECT_COMMAND_NO_REPLY). | |
send_command | |
end | |
def stop_sound | |
Commands::Sound.new(@connection). | |
stop_sound. | |
command_type(CommandType::DIRECT_COMMAND_NO_REPLY). | |
send_command | |
end | |
end | |
end | |
# usage: | |
# @brick.set_led(6) | |
# | |
=begin | |
LED_BLACK = 0, <- turn off LED | |
LED_GREEN = 1, | |
LED_RED = 2, | |
LED_ORANGE = 3, | |
LED_GREEN_FLASH = 4, | |
LED_RED_FLASH = 5, | |
LED_ORANGE_FLASH = 6, | |
LED_GREEN_PULSE = 7, | |
LED_RED_PULSE = 8, | |
LED_ORANGE_PULSE = 9, | |
Refereces: | |
- http://analyticphysics.com/Diversions/Assembly%20Language%20Programming%20for%20LEGO%20Mindstorms%20EV3.htm | |
- EV3 Firmware Developer Kit documentation | |
https://education.lego.com/en-us/support/mindstorms-ev3/developer-kits | |
=end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment