Class: Nabaztag

Inherits:
Object
  • Object
show all
Defined in:
lib/nabaztag.rb,
lib/nabaztag/message.rb,
lib/nabaztag/response.rb,
lib/nabaztag/choreography.rb

Overview

Nabaztag allows control of the text-to-speech, ear control, and choreography features of Nabaztag devices.

To use this library, you need to know the MAC of the device (written on the base) and its API token. The token must be obtained from www.nabaztag.com/vl/FR/api_prefs.jsp .

The API allows different commands to be dispatched simultaneously; in order to achieve this, this library queues commands until they are sent.

E.g.

nabaztag = Nabaztag.new(mac, token)
nabaztag.say('bonjour')   # Nothing sent yet
nabaztag.move_ears(4, 4)  # Still not sent
nabaztag.send             # Messages sent

This also means that if two conflicting commands are issued without an intervening send, only the latter will be carried out.

However, beware! The API doesn’t seem to respond well if multiple commands are sent in a short period of time: it can become confused and send erroneous commands to the device.

In addition, the choreography command does not seem to play well with other commands: if text-to-speech and choreography are sent in one request, only the speech will get through to the rabbit.

With version 2 of the API, it is now possible to specify a voice for the message. The default is determined by the rabbit’s language (claire22s for French; heather22k for English). The voice’s language overrides that of the rabbit: i.e. a French rabbit will speak in English when told to use an English voice.

The known voices are grouped by language in the Nabaztag::VOICES constant, but no attempt is made to validate against this list, as Violet may introduce additional voices in future.

Defined Under Namespace

Classes: Choreography, Message, Response, ServiceError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mac, token) ⇒ Nabaztag

Create a new Nabaztag instance to communicate with the device with the given MAC address and service token (see class overview for explanation of token).



50
51
52
53
54
# File 'lib/nabaztag.rb', line 50

def initialize(mac, token)
  @mac, @token = mac, token
  @message = new_message
  @ear_positions = [nil, nil]
end

Instance Attribute Details

#macObject (readonly)

Returns the value of attribute mac.



43
44
45
# File 'lib/nabaztag.rb', line 43

def mac
  @mac
end

#messageObject (readonly)

Returns the value of attribute message.



43
44
45
# File 'lib/nabaztag.rb', line 43

def message
  @message
end

#tokenObject (readonly)

Returns the value of attribute token.



43
44
45
# File 'lib/nabaztag.rb', line 43

def token
  @token
end

#voiceObject

Returns the value of attribute voice.



44
45
46
# File 'lib/nabaztag.rb', line 44

def voice
  @voice
end

Instance Method Details

#barkObject

Make the rabbit bark.



98
99
100
101
# File 'lib/nabaztag.rb', line 98

def bark
  say('ouah ouah')
  nil
end

#bark!Object

Bark immediately.



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

def bark!
  bark
  send
end

#choreography(title = nil, &blk) ⇒ Object

Creates a new choreography message based on the actions instructed in the block. The commands are evaluated in the context of a new Choreography instance.

E.g.

nabaztag.choreography do
  together { led :middle, :green ; led :left, :red }
  led :right, :yellow
  together { led :left, :off ; led :right, :off}
  ...
end


142
143
144
145
146
# File 'lib/nabaztag.rb', line 142

def choreography(title=nil, &blk)
  message.chortitle = title
  message.chor = Choreography.new(&blk).build
  nil
end

#choreography!(title = nil, &blk) ⇒ Object

Creates choreography and sends it immediately.



151
152
153
154
# File 'lib/nabaztag.rb', line 151

def choreography!(title=nil, &blk)
  choreography(title, &blk)
  send
end

#ear_positionsObject

Send a message immediately to get the ear positions.



72
73
74
75
76
77
# File 'lib/nabaztag.rb', line 72

def ear_positions
  ear_message = new_message
  ear_message.ears = 'ok'
  response = ear_message.send
  return [response.left_ear, response.right_ear]
end

#move_ears(left, right) ⇒ Object

Set the position of the left and right ears between 0 and 16. Use nil to avoid moving an ear. Note that these positions are not given in degrees, and that it is not possible to specify the direction of movement. For more precise ear control, use choreography instead.



116
117
118
119
120
# File 'lib/nabaztag.rb', line 116

def move_ears(left, right)
  message.posleft = left if left
  message.posright = right if right
  nil
end

#move_ears!(left, right) ⇒ Object

Move ears immediately.



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

def move_ears!(left, right)
  move_ears(left, right)
  send
end

#new_messageObject



156
157
158
# File 'lib/nabaztag.rb', line 156

def new_message
  return Message.new(mac, token)
end

#say(text) ⇒ Object

Say text.



82
83
84
85
# File 'lib/nabaztag.rb', line 82

def say(text)
  message.tts = text
  nil
end

#say!(text) ⇒ Object

Say text immediately.



90
91
92
93
# File 'lib/nabaztag.rb', line 90

def say!(text)
  say(text)
  send
end

#sendObject

Send all pending messages



59
60
61
62
63
64
65
66
67
# File 'lib/nabaztag.rb', line 59

def send
  @message.voice = voice
  response = @message.send
  @message = new_message
  unless response.success?
    raise ServiceError, response.raw
  end
  return response
end