Class: LittleWire::Nunchuck

Inherits:
Object
  • Object
show all
Defined in:
lib/littlewire/gadgets/nunchuck.rb

Defined Under Namespace

Classes: NunchuckFrame

Constant Summary collapse

BusAddress =

Address of Nunchuck on i2c bus

82

Instance Method Summary collapse

Constructor Details

#initialize(wire, interval = 0) ⇒ Nunchuck

Returns a new instance of Nunchuck.



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/littlewire/gadgets/nunchuck.rb', line 11

def initialize wire, interval = 0
  @wire = wire
  
  raise "Nunchuck requires LittleWire firmware 1.3 or newer" unless wire.version_hex >= 0x13
  
  # config i2c to reliably talk with nunchuck devices
  i2c.delay = interval # The fastest option of 0 seems to work. Neat!
  
  # initialize the nunchuck
  set_register 0xf0, 0x55
  set_register 0xfb, 0x00
  sample # do a sample, to get the ball rolling
end

Instance Method Details

#sampleObject



25
26
27
28
29
30
31
32
# File 'lib/littlewire/gadgets/nunchuck.rb', line 25

def sample
  # tell device to gather new sensor data
  i2c.transmit BusAddress, [0]
  # load sample in to PC
  data = i2c.request(BusAddress, 6)
  
  LittleWire::Nunchuck::NunchuckFrame.new(data)
end