littlewire 0.9.7 → 0.9.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 672e15656164047ad4895db0099cea33b13bb861
4
- data.tar.gz: 79ecc40349391da72aa766b11c7f2ec667a565ae
3
+ metadata.gz: 3ce81eaa4836881938aeca5d6d4300f49f90fcef
4
+ data.tar.gz: f8b8ba8827f9b23abfe77339b4684bc250bb04ef
5
5
  SHA512:
6
- metadata.gz: 7959de400ac104e7671353af22aa9207d1d74a71ecee220ec2629bc87b08956a745eb5d8d29a01e1501fb906fb69440e8e53726854bf5ff156d28355973bc70b
7
- data.tar.gz: f1e7674e363807d7d8e1af74d0504a9f0d5740c31ff04d5be17ec73814a5e2a0d65b4a9cb5886ecf279f1fda5a29eba74057300965086391d9897ecf1dac31bf
6
+ metadata.gz: 2ec2ac2d655f97325571c7ff1f4605a5c3ba0e1f451c4e92ae408f8863b55f26d919cb71d4d2181081caa06f3ec6ced62cefc844ff9317031cc2da76c5733f6b
7
+ data.tar.gz: 1290c8bd90d384ebcf25d3f7dd535d04609e70826566bc5327b2f4ce04a18a2d938170a97cb5f2839852f8a432d6de687f3a38b33709aaac942186035ad372a0
@@ -0,0 +1,79 @@
1
+ #!/usr/bin/env ruby
2
+ __dir__ ||= File.dirname(__FILE__)
3
+ #$:.unshift File.join(__dir__, '..', 'lib')
4
+ require 'thor'
5
+ require 'pp'
6
+ require 'littlewire'
7
+ require 'littlewire/gadgets/micronucleus'
8
+
9
+ class LittleWireUtility < Thor
10
+ desc "install [version]", "Install a specific firmware on to the littlewire device"
11
+ def install version = 'latest'
12
+ path = File.join(__dir__, "..", "firmware", "#{version}.hex")
13
+ raise "Unknown Version" unless File.file? path
14
+
15
+ data = HexProgram.new(open path).binary
16
+
17
+ retried = false
18
+ begin
19
+ puts "Will upload to a littlewire which has been updated with the micronucleus bootloader, or to a digispark."
20
+ puts "Plug in micronucleus device now: (waiting)"
21
+ sleep 0.25 while Micronucleus.all.length == 0
22
+
23
+ nucleus = Micronucleus.all.first
24
+ puts "Attached to device: #{nucleus.inspect}"
25
+
26
+ sleep(0.25) # some time to think?
27
+ puts "Writing program in to device's memory"
28
+ nucleus.program = data
29
+
30
+ puts "Great! Starting new littlewire firmware..."
31
+ nucleus.finished # let thinklet know it can go do other things now if it likes
32
+ rescue LIBUSB::ERROR_IO => err
33
+ unless retried
34
+ retried = true
35
+ retry
36
+ end
37
+ raise err
38
+ end
39
+
40
+ puts "All done!"
41
+ puts "If littlewire doesn't automatically appear in a few seconds, unplug and replug device from USB port"
42
+ end
43
+
44
+ desc "firmwares", "List all versions which can be installed via install command"
45
+ def firmwares
46
+ puts "Available LittleWire Firmware:"
47
+ Dir[File.join(__dir__, "..", "firmware", "*.hex")].each do |filename|
48
+ puts File.basename(filename, '.hex')
49
+ end
50
+ end
51
+
52
+ desc "version", "Which version of the ruby library is this?"
53
+ def version
54
+ puts "Library Version: #{LittleWire.version}"
55
+
56
+ wire = LittleWire.connect
57
+ puts "Device Firmware: #{wire.version}" if wire
58
+
59
+ latest_path = File.join(__dir__, "..", "firmware", "#{LittleWire::SupportedVersions.first}.hex")
60
+ if LittleWire::SupportedVersions.index(wire.version) != 0 and File.exists? latest_path
61
+ puts "An updated firmware is available, version #{LittleWire::SupportedVersions.first}"
62
+ puts "To update, run:"
63
+ puts " littlewire.rb install #{LittleWire::SupportedVersions.first}"
64
+ puts ""
65
+ puts "If you bought your LittleWire as a kit from Seeed Studios, you may need to first"
66
+ puts "install the Micronucleus bootloader as described on the littlewire.cc website."
67
+ puts ""
68
+ end
69
+ end
70
+
71
+ desc "racer", "Attach a Wii Nunchuck and play a game"
72
+ def racer
73
+ ruby_vm = File.join(RbConfig::CONFIG['bindir'], RbConfig::CONFIG['ruby_install_name'])
74
+ system(ruby_vm, File.join(__dir__, '..', 'examples', 'i2c', 'nunchuck.rb'))
75
+ end
76
+ end
77
+
78
+ LittleWireUtility.start(ARGV)
79
+
@@ -0,0 +1,33 @@
1
+ require 'littlewire'
2
+
3
+ # Make a class which represents a digispark with the digistump eeprom shield attached
4
+ # with LittleWire installed
5
+ class MicrochipEEPROM
6
+ def initialize wire = LittleWire.connect, address = 0x50
7
+ raise "EEPROM device not responding" unless wire.i2c.address_responds? address
8
+
9
+ @wire = wire
10
+ @device = address
11
+
12
+ @wire.i2c.delay = 10
13
+ end
14
+
15
+ def read address
16
+ @wire.i2c.transmit @device, [(address >> 8) & 0xFF, address & 0xFF]
17
+ @wire.i2c.request(@device, 1)
18
+ end
19
+
20
+ def write address, data
21
+ @wire.i2c.transmit @device, [address >> 8, address & 0xFF, data]
22
+ sleep 0.01 # time to write data
23
+ end
24
+ end
25
+
26
+ eep = MicrochipEEPROM.new
27
+
28
+ number = rand(127)
29
+ puts "Random number is #{number}"
30
+ puts "Writing to eeprom at address 0"
31
+ eep.write 0, number
32
+ puts "Reading back..."
33
+ puts eep.read(0)
@@ -0,0 +1,80 @@
1
+ # This little example shows the nunchuck gadget extension
2
+ # The Nunchuck gadget lets you easily connect with Nintendo Wii-compatible
3
+ # 'Nunchuck' devices using the I2C feature of the LittleWire device.
4
+ require 'littlewire'
5
+ require 'littlewire/gadgets/nunchuck'
6
+
7
+ wire = LittleWire.connect
8
+ wire.pin_mode :ds5, :output
9
+ wire.digital_write :ds5, :low
10
+
11
+ car = 0.0
12
+ wall = 0.0
13
+ wall_width = 16
14
+ constraint = 20.0
15
+
16
+ puts DATA.read
17
+ puts ''
18
+ puts "Point your nunchuck at the screen, and press C button to start your engines!"
19
+ sleep 0.1 until wire.nunchuck.sample.buttons.c
20
+
21
+ seconds = 0.0
22
+ seconds_since_gearshift = 0.0
23
+ last_full_second = 0
24
+ step = 0.1
25
+
26
+ GearshiftInterval = 10.0
27
+
28
+ loop do
29
+ controller = wire.nunchuck.sample
30
+ car += controller.accelerometer.x * 2.0
31
+
32
+ wall += rand(3) - 1
33
+ wall = -20.0 if wall < -constraint
34
+ wall = +20.0 if wall > +constraint
35
+
36
+ real_wall = wall + constraint * 2 + 1
37
+ real_car = car + constraint * 2 + 1
38
+
39
+ string = " " * 80
40
+ string[real_wall - wall_width / 2] = '|'
41
+ string[real_wall + wall_width / 2] = '|'
42
+ string[real_car] = '5'
43
+
44
+ if seconds > last_full_second + 5
45
+ string[0...10] = seconds.round.to_s.ljust(10, ' ')
46
+ last_full_second = seconds.round
47
+ end
48
+ puts string
49
+
50
+ raise "Crashed your car!" if real_car < real_wall - (wall_width / 2)
51
+ raise "Crashed your car!" if real_car > real_wall + (wall_width / 2)
52
+
53
+ sleep step
54
+ seconds += step
55
+ seconds_since_gearshift += step
56
+ if seconds_since_gearshift > GearshiftInterval
57
+ step *= 0.8
58
+ seconds_since_gearshift = 0.0
59
+ end
60
+ end
61
+
62
+ __END__
63
+ ______ __ __ ____ ____ ____
64
+ /_ __/ / / / / / __ \ / __ ) / __ \
65
+ / / / / / / / /_/ / / __ | / / / /
66
+ / / / /_/ / / _, _/ / /_/ / / /_/ /
67
+ /_/ \____/ /_/ |_| /_____/ \____/
68
+ ____ ___ ______ ______ ____
69
+ / __ \ / | / ____/ / ____/ / __ \
70
+ / /_/ / / /| | / / / __/ / /_/ /
71
+ / _, _/ / ___ | / /___ / /___ / _, _/
72
+ /_/ |_| /_/ |_| \____/ /_____/ /_/ |_|
73
+ ______ _ __ ______ ____ ______ __ ___ ______
74
+ / ____/ | |/ / /_ __/ / __ \ / ____/ / |/ / / ____/
75
+ / __/ | / / / / /_/ / / __/ / /|_/ / / __/
76
+ / /___ / | / / / _, _/ / /___ / / / / / /___
77
+ /_____/ /_/|_| /_/ /_/ |_| /_____/ /_/ /_/ /_____/
78
+
79
+ Motion Nunchuck Ninja Edition 2.1 - Insert Coin
80
+ Published by Mechanum Industries for PC-DOS, Amega, VIC 20, and iPhone 5c
@@ -0,0 +1,5 @@
1
+ # This little example searches the i2c bus and lists all the responsive addresses
2
+ require 'littlewire'
3
+
4
+ wire = LittleWire.connect
5
+ puts "Active Addresses: #{wire.i2c.search.inspect}"
@@ -0,0 +1,44 @@
1
+ # Send up to 64 colours to a string of WS2812 LEDs or 800khz (version 2) Adafruit Flora NeoPixels
2
+ # Any 800khz mode ws2811 pixels will work
3
+ require 'littlewire'
4
+ wire = LittleWire.connect
5
+
6
+ if wire == nil
7
+ puts "Couldn't find an attached LittleWire device"
8
+ exit
9
+ end
10
+
11
+ if ARGV.length != 2
12
+ puts DATA.read # print out the little ascii art help
13
+ exit
14
+ end
15
+
16
+ pin = ARGV[0].gsub(/[^0-9a-zA-Z]/, '').downcase
17
+ color = ARGV[1]
18
+
19
+ puts "Setting 64 pixels connected to #{pin} to be #{color}"
20
+ wire.ws2811(pin).send([color] * 64)
21
+
22
+ puts "All done!"
23
+
24
+ __END__
25
+
26
+ all.rb sets 64 pixels connected to a littlewire (or digispark) pin to
27
+ a specific colour. You can specify this colour by CSS name 'red'
28
+ or by web-style hex code '#ff0000', or with CSS-style RGB constructs
29
+ '(rgb(255, 0, 0)'. Run all.rb with two arguments - the output pin name
30
+ followed by the colour.
31
+
32
+ LittleWire connector: | Digispark Board:
33
+ /-----\ | _________
34
+ pin1 | o o | vcc | _____| o| ds5
35
+ pin2 | o o | pin4 | |----- o| (usb - not available)
36
+ pin3 | o o | gnd | |----- o| (usb - not available)
37
+ \-----/ | |----- o| ds2
38
+ |----- o| ds1
39
+ |_o_o_o__o| ds0
40
+ 5 g v
41
+ v n c
42
+ d c
43
+
44
+ Example: ruby all.rb pin4 aqua
@@ -0,0 +1,28 @@
1
+ require 'littlewire'
2
+ wire = LittleWire.connect
3
+ pin = :pin4
4
+ speed = 1
5
+ num_leds = 64
6
+
7
+ hue = 0.0
8
+
9
+ loop do
10
+ puts "red"
11
+ wire.ws2811(pin).send(['red'] * num_leds)
12
+ sleep speed
13
+
14
+ puts "green"
15
+ wire.ws2811(pin).send(['green'] * num_leds)
16
+ sleep speed
17
+
18
+ puts "blue"
19
+ wire.ws2811(pin).send(['blue'] * num_leds)
20
+ sleep speed
21
+ # red = 1.0 + (Math.sin(hue) * 127)
22
+ # green = 1.0 + (Math.sin(hue + ((Math::PI*2.0) / 3)) * 127)
23
+ # blue = 1.0 + (Math.sin(hue - ((Math::PI*2.0) / 3)) * 127)
24
+ #
25
+ # wire.ws2811(pin).send([Colorist::Color.from_rgb(red, green, blue)] * num_leds)
26
+ #
27
+ # hue += 0.5
28
+ end
@@ -0,0 +1,43 @@
1
+ # Send up to 64 colours to a string of WS2812 LEDs or 800khz (version 2) Adafruit Flora NeoPixels
2
+ # Any 800khz mode ws2811 pixels will work
3
+ require 'littlewire'
4
+ wire = LittleWire.connect
5
+
6
+ puts DATA.read # print out the little ascii art thing at the end of this file
7
+ puts "Which pin to use for data output?"
8
+ print "Enter pin number: "
9
+ output_pin = gets.gsub(/[^0-9a-zA-Z]/, '').downcase
10
+
11
+ puts "Blacking out strip"
12
+ wire.ws2811(output_pin).black!
13
+
14
+ colors = []
15
+
16
+ # print "Enter 1st color: "
17
+ # wire.ws2811.colors = [gets.strip.to_color]
18
+ # wire.ws2811.output(output_pin) # output our first color
19
+
20
+ titles = ['1st', '2nd', '3rd']
21
+
22
+ 63.times do |idx|
23
+ print "Enter #{titles[idx] || "#{idx + 1}th"} color: "
24
+ gotten = gets.strip
25
+ break if gotten.empty?
26
+ colors << gotten.to_color
27
+ wire.ws2811(output_pin).send colors
28
+ end
29
+
30
+ puts "All done!"
31
+
32
+ __END__
33
+ LittleWire connector: | Digispark Board:
34
+ /-----\ | _________
35
+ pin1 | o o | vcc | _____| o| ds5
36
+ pin2 | o o | pin4 | |----- o| (usb - not available)
37
+ pin3 | o o | gnd | |----- o| (usb - not available)
38
+ \-----/ | |----- o| ds2
39
+ |----- o| ds1
40
+ |_o_o_o__o| ds0
41
+ 5 g v
42
+ v n c
43
+ d c
@@ -0,0 +1,26 @@
1
+ require 'littlewire'
2
+ wire = LittleWire.connect
3
+ num_pixels = ARGV.first.to_i
4
+ pin = :pin4
5
+
6
+ lit = 0
7
+ loop do
8
+
9
+ text_output = ['-'] * num_pixels
10
+ text_output[lit] = '*'
11
+ puts text_output.join
12
+
13
+ wire.ws2811(pin).send num_pixels.times.map { |idx|
14
+ if idx == lit
15
+ 'white'.to_color
16
+ else
17
+ 'black'.to_color
18
+ end
19
+ }
20
+
21
+ lit += 1
22
+ lit %= num_pixels
23
+
24
+ sleep 0.005
25
+ end
26
+
@@ -0,0 +1,17 @@
1
+ require 'littlewire'
2
+ include Colorist
3
+ wire = LittleWire.connect
4
+ num_pixels = ARGV.first.to_i
5
+
6
+ loop do
7
+ position = (Time.now.to_f / 60) % 1
8
+ wire.ws2811.colors = num_pixels.times.map do |idx|
9
+ brightness = Math.sin((position - (idx.to_f / num_pixels)) * Math::PI * 2) * 255
10
+ Color.from_rgb(0, brightness.to_i, 0)
11
+ end
12
+
13
+ wire.ws2811.output :pin4
14
+
15
+ sleep 1.0 / 60.0
16
+ end
17
+
@@ -0,0 +1,93 @@
1
+ require 'littlewire'
2
+ wire = LittleWire.connect
3
+ pin = :pin4
4
+ num_leds = 19
5
+ MaxBPM = 500
6
+ AveragedTaps = 8
7
+
8
+ puts "Outputtting data to LittleWire pin named '#{pin}'"
9
+
10
+ strobe_duration = 0.01
11
+ bpm = 30
12
+ last_tap = Time.now.to_f
13
+ tap_bpms = []
14
+ last_strobe = Time.now.to_f
15
+ reset = false
16
+
17
+ wire.ws2811(pin).black!
18
+
19
+ puts "Enter BPM and press enter to set it"
20
+ puts "Or, tap enter key (leave blank) to the beat to detect BPM"
21
+ puts "Or, enter z to halve current bpm and x to double it, followed by enter"
22
+
23
+ Thread.abort_on_exception = true
24
+ interface = Thread.start do
25
+ loop do
26
+ print "Enter BPM: "
27
+ str = $stdin.gets.strip
28
+ time = Time.now.to_f
29
+ new_bpm = bpm
30
+
31
+ if str == 'z'
32
+ new_bpm = bpm / 2.0
33
+ tap_bpms.map! { |x| x / 2.0 }
34
+
35
+ elsif str == 'x'
36
+ new_bpm = bpm * 2.0
37
+ tap_bpms.map! { |x| x * 2 }
38
+
39
+ elsif str.empty?
40
+ # calculate bpm from last tap and this one
41
+ tap_bpm = 60.0 / (time - last_tap)
42
+
43
+ # add tap to list and compute average
44
+ tap_bpms.push tap_bpm
45
+ tap_bpms = tap_bpms.last(AveragedTaps)
46
+
47
+ # compute average
48
+ avg = tap_bpms.reduce(:+) / tap_bpms.length
49
+ nearest_subset = tap_bpms.sort { |a,b| (a - avg).abs <=> (b - avg).abs }.first(tap_bpms.length / 2)
50
+
51
+ new_bpm = nearest_subset.reduce(:+) / nearest_subset.length if nearest_subset.length >= 2
52
+
53
+ # because strobes are mainly used with electro music, and electro music is
54
+ # usually whole number BPMs, round it to the nearest full bpm
55
+ new_bpm = new_bpm.round
56
+
57
+ # set last strobe and last tap to now, so a new strobe begins immediately
58
+ last_strobe = last_tap = time
59
+ reset = true # ask main thread to skip timers and start a new strobe now
60
+
61
+ puts "TAP!"
62
+ else # numbers were typed in presumably!
63
+ new_bpm = (str.to_f rescue bpm)
64
+ tap_bpms.clear # reset tap bpm system
65
+ end
66
+
67
+ new_bpm = MaxBPM if new_bpm > MaxBPM
68
+ new_bpm = 30.0 if new_bpm < 30.0
69
+ bpm = new_bpm
70
+ puts "Set to #{bpm}"
71
+ end
72
+ end
73
+
74
+ loop do
75
+ last_strobe = Time.now.to_f
76
+ wire.ws2811(pin).set ['white'] * num_leds
77
+
78
+ while Time.now.to_f - last_strobe <= strobe_duration and !reset
79
+ Thread.pass
80
+ end
81
+
82
+ wire.ws2811(pin).set ['black'] * num_leds
83
+ blackout_duration = (60.0 / bpm) - strobe_duration
84
+
85
+ while Time.now.to_f - last_strobe <= 60.0 / bpm and !reset
86
+ Thread.pass
87
+ end
88
+
89
+ last_strobe += 60.0 / bpm
90
+
91
+ #sleep((60.0 / bpm) - strobe_duration) if blackout_duration > 0
92
+ reset = false
93
+ end