Class: Mi100

Inherits:
Object
  • Object
show all
Defined in:
lib/mi100/morsecoder.rb,
lib/mi100.rb,
lib/mi100/version.rb

Overview

morsecoder.rb Copyright © 2014 Masami Yamakawa

This software is released under the MIT License. opensource.org/lisenses/mit-license.php

Defined Under Namespace

Classes: Morsecoder

Constant Summary collapse

DEFAULT_RETRIES =
5
READ_TIMEOUT =
5000
WRITE_TIMEOUT =
5000
SHORT_READ_TIMEOUT =
1
CMD_PING =
"H"
CMD_GET_POWER_LEVEL =
"H"
CMD_STOP =
"S"
CMD_MOVE_FORWARD =
"F"
CMD_MOVE_BACKWARD =
"B"
CMD_SPIN_RIGHT =
"R"
CMD_SPIN_LEFT =
"L"
"D"
CMD_TONE =
"T"
CMD_GET_LIGHT =
"P"
DEFAULT_MOVE_DURATION =
300
DEFAULT_SPIN_DURATION =
140
600
DEFAULT_TONE_DURATION =
300
DEFAULT_MOVE_DIRECTION =
"FORWARD"
DEFAULT_SPIN_DIRECTION =
"RIGHT"
FREQUENCY =
{ DO:   262,
  DOS:  277,
  RE:   294,
  RES:  311,
  MI:   330,
  FA:   349,
  FAS:  370,
  SO:   392,
  SOS:  415,
  LA:   440,
  LAS:  466,
  SI:   494,
  HDO:  523,
  HDOS: 554,
  HRE:  587,
  HRES: 622,
  HMI:  659,
  HFA:  698,
  HFAS: 740,
  HSO:  784,
  HSOS: 831,
  HLA:  880,
  HSI:  988,
}
VERSION =
"0.3.1"

Instance Method Summary collapse

Constructor Details

#initialize(dev) ⇒ Mi100

Returns a new instance of Mi100.



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/mi100.rb', line 62

def initialize(dev)
  retries_left = DEFAULT_RETRIES
  begin
    initialize_serialport dev
  rescue Errno::ENOENT
    puts "Retry Bluetooth connection: #{retries_left.to_s}"
    retries_left -= 1
    retry unless retries_left < 0
    puts "Bluetooth connection failed."
    raise
  end
  
end

Instance Method Details

#badObject



197
198
199
200
201
202
# File 'lib/mi100.rb', line 197

def bad
  duration = 400.0
  freq = 100
  tone freq, duration
  sleep duration / 1000.0
end


154
155
156
157
158
159
160
161
162
# File 'lib/mi100.rb', line 154

def blink(r = nil, g = nil, b = nil, duration = DEFAULT_BLINK_DURATION)
  r ||= rand(100)+1
  g ||= rand(100)+1
  b ||= rand(100)+1
  r = 100 if r > 100
  g = 100 if g > 100
  b = 100 if b > 100
  send_command_get_response "#{CMD_BLINK_LED},#{r.to_s},#{g.to_s},#{b.to_s},#{duration.to_s}"
end

#closeObject



76
77
78
79
# File 'lib/mi100.rb', line 76

def close
  sleep 2
  @sp.close
end

#goodObject



188
189
190
191
192
193
194
195
# File 'lib/mi100.rb', line 188

def good
  freqs = [440,880,1760]
  duration = 100.0
  freqs.each do |freq|
    tone freq, duration
    sleep duration / 1000.0
  end
end

#lightObject



91
92
93
94
# File 'lib/mi100.rb', line 91

def light
  response = send_command_get_response CMD_GET_LIGHT
  response[1].to_i
end

#morse_frequencyObject



209
210
211
# File 'lib/mi100.rb', line 209

def morse_frequency
  Morsecoder.default_frequency
end

#morse_frequency=(frequency) ⇒ Object



213
214
215
# File 'lib/mi100.rb', line 213

def morse_frequency=(frequency)
  Morsecoder.default_frequency = frequency
end

#morse_unitObject



217
218
219
# File 'lib/mi100.rb', line 217

def morse_unit
  Morsecoder.default_unit
end

#morse_unit=(millisec) ⇒ Object



221
222
223
# File 'lib/mi100.rb', line 221

def morse_unit=(millisec)
  Morsecoder.default_unit = millisec
end

#move(direction = DEFAULT_MOVE_DIRECTION, duration = DEFAULT_MOVE_DURATION) ⇒ Object



96
97
98
99
# File 'lib/mi100.rb', line 96

def move(direction = DEFAULT_MOVE_DIRECTION, duration = DEFAULT_MOVE_DURATION)
  cmd = direction.upcase == "BACKWARD" ? CMD_MOVE_BACKWARD : CMD_MOVE_FORWARD
  send_command_get_response "#{cmd},#{duration.to_s}"
end

#move_backward(duration) ⇒ Object



110
111
112
# File 'lib/mi100.rb', line 110

def move_backward(duration)
  send_command_get_response "#{CMD_MOVE_BACKWARD},#{duration.to_s}"
end

#move_backward!(duration) ⇒ Object



142
143
144
# File 'lib/mi100.rb', line 142

def move_backward!(duration)
  sendln "#{CMD_MOVE_BACKWARD},#{duration.to_s}"
end

#move_forward(duration) ⇒ Object



106
107
108
# File 'lib/mi100.rb', line 106

def move_forward(duration)
  send_command_get_response "#{CMD_MOVE_FORWARD},#{duration.to_s}"
end

#move_forward!(duration) ⇒ Object



138
139
140
# File 'lib/mi100.rb', line 138

def move_forward!(duration)
  sendln "#{CMD_MOVE_FORWARD},#{duration.to_s}"
end

#moveb(duration = DEFAULT_MOVE_DURATION) ⇒ Object



126
127
128
# File 'lib/mi100.rb', line 126

def moveb(duration = DEFAULT_MOVE_DURATION)
  move_backward duration
end

#movef(duration = DEFAULT_MOVE_DURATION) ⇒ Object



122
123
124
# File 'lib/mi100.rb', line 122

def movef(duration = DEFAULT_MOVE_DURATION)
  move_forward duration
end

#pingObject



81
82
83
# File 'lib/mi100.rb', line 81

def ping
  send_command_get_response CMD_PING
end

#powerObject



85
86
87
88
89
# File 'lib/mi100.rb', line 85

def power
  response = send_command_get_response CMD_GET_POWER_LEVEL
  voltage = response[1].to_i / 1000.0
  voltage.round 2
end

#sound(pitch = "?", duration = DEFAULT_TONE_DURATION) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/mi100.rb', line 175

def sound(pitch = "?", duration = DEFAULT_TONE_DURATION)

  if pitch.instance_of?(String)
    pitch = FREQUENCY.keys[rand(FREQUENCY.size)].to_s if pitch == "?"
    frequency = FREQUENCY[pitch.upcase.to_sym]
  else
    frequency = pitch
  end
  
  tone frequency, duration if frequency
  sleep duration.to_f / 1000.0
end

#spin(direction = DEFAULT_SPIN_DIRECTION, duration = DEFAULT_SPIN_DURATION) ⇒ Object



101
102
103
104
# File 'lib/mi100.rb', line 101

def spin(direction = DEFAULT_SPIN_DIRECTION, duration = DEFAULT_SPIN_DURATION)
  cmd = direction.upcase == "LEFT" ? CMD_SPIN_LEFT : CMD_SPIN_RIGHT
  send_command_get_response "#{cmd},#{duration.to_s}"
end

#spin_left(duration) ⇒ Object



118
119
120
# File 'lib/mi100.rb', line 118

def spin_left(duration)
  send_command_get_response "#{CMD_SPIN_LEFT},#{duration.to_s}"
end

#spin_left!(duration) ⇒ Object



150
151
152
# File 'lib/mi100.rb', line 150

def spin_left!(duration)
  sendln "#{CMD_SPIN_LEFT},#{duration.to_s}"
end

#spin_right(duration) ⇒ Object



114
115
116
# File 'lib/mi100.rb', line 114

def spin_right(duration)
  send_command_get_response "#{CMD_SPIN_RIGHT},#{duration.to_s}"
end

#spin_right!(duration) ⇒ Object



146
147
148
# File 'lib/mi100.rb', line 146

def spin_right!(duration)
  sendln "#{CMD_SPIN_RIGHT},#{duration.to_s}"
end

#spinl(duration = DEFAULT_SPIN_DURATION) ⇒ Object



134
135
136
# File 'lib/mi100.rb', line 134

def spinl(duration = DEFAULT_SPIN_DURATION)
  spin_left duration
end

#spinr(duration = DEFAULT_SPIN_DURATION) ⇒ Object



130
131
132
# File 'lib/mi100.rb', line 130

def spinr(duration = DEFAULT_SPIN_DURATION)
  spin_right duration
end

#stopObject



164
165
166
# File 'lib/mi100.rb', line 164

def stop
  send_command_get_response CMD_STOP
end

#talk(str) ⇒ Object



204
205
206
207
# File 'lib/mi100.rb', line 204

def talk(str)
  morsecoder = Morsecoder.new str
  morsecoder.each {|frequency, duration| sound(frequency,duration)}
end

#tone(frequency = nil, duration = DEFAULT_TONE_DURATION) ⇒ Object



168
169
170
171
172
173
# File 'lib/mi100.rb', line 168

def tone(frequency = nil, duration = DEFAULT_TONE_DURATION)
  frequency ||= rand(4186 - 28) + 28
  frequency = 4186 if frequency > 4186
  frequency = 28 if frequency < 28
  send_command_get_response "#{CMD_TONE},#{frequency.to_s},#{duration.to_s}"
end