digiusb 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,28 @@
1
+ #!/usr/bin/env ruby
2
+ $:.unshift File.dirname(__FILE__) + "/../lib"
3
+ require 'digiusb'
4
+ require 'io/console'
5
+
6
+ puts "Looking for Digispark running DigiUSB..."
7
+
8
+ sleep 0.25 until DigiUSB.sparks.length > 0
9
+ spark = DigiUSB.sparks.last
10
+
11
+ puts "Attached to #{spark}"
12
+ puts "Type control + c to exit"
13
+
14
+ begin
15
+ loop do
16
+ print spark.getc
17
+
18
+ begin
19
+ char = IO.console.read_nonblock(1)
20
+ spark.putc char
21
+ rescue Errno::EAGAIN
22
+ end
23
+ end
24
+ rescue LIBUSB::ERROR_PIPE
25
+ puts "Digispark disconnected or crashed"
26
+ rescue Interrupt
27
+ puts ""
28
+ end
@@ -0,0 +1,13 @@
1
+ require 'digiusb'
2
+
3
+ spark = DigiUSB.sparks.last
4
+
5
+ print spark.puts "Jake! Jake! Good Morning! Whatcha' doin' buddy?"
6
+ print msg = spark.gets
7
+ print spark.puts "What are you #{msg.downcase.gsub(/[^a-z]/, '')} about, dude?"
8
+ print spark.gets
9
+ print spark.puts "Haha! Beep! Boop! Boooop! Beep! Boop!"
10
+ print spark.gets
11
+ print spark.puts "Huh! I like it!"
12
+ print spark.puts "Weeeeoooooooo!"
13
+ print spark.puts "Woooooah! Algebraic!"
@@ -0,0 +1,117 @@
1
+ require 'libusb'
2
+
3
+ # simple IO-like read/write access to a digispark using the DigiUSB library
4
+ class DigiUSB
5
+ ProductID = 0x05df
6
+ VendorID = 0x16c0
7
+ Manufacturer = "digistump.com"
8
+ Timeout = 1_000 # one second till device crashes due to lack of calling DigiUSB.refresh()
9
+
10
+ def initialize device
11
+ @device = device
12
+ @handle = nil
13
+ end
14
+
15
+ def self.sparks
16
+ usb = LIBUSB::Context.new
17
+ usb.devices.select { |device|
18
+ device.idProduct == ProductID && device.idVendor == VendorID && device.manufacturer == "digistump.com"
19
+ }.map { |handle|
20
+ self.new(handle)
21
+ }
22
+ end
23
+
24
+
25
+ # read a single character
26
+ def getc
27
+ control_transfer(
28
+ bRequest: 0x01, # hid get report
29
+ dataIn: 1
30
+ )
31
+ end
32
+
33
+ # write a single character
34
+ def putc character
35
+ character = [character].pack('c') if character.is_a? Integer
36
+ raise "Cannot putc more than one byte" if character.bytesize > 1
37
+ raise "Cannot putc fewer than one byte" if character.bytesize < 1
38
+
39
+ control_transfer(
40
+ bRequest: 0x09, # hid set report
41
+ wIndex: character.ord
42
+ )
43
+ end
44
+
45
+ # get a string up until the first newline
46
+ def gets
47
+ chars = ""
48
+ chars += getc() until chars.include? "\n"
49
+ return chars
50
+ end
51
+
52
+ # write a string followed by a newline
53
+ def puts string = ""
54
+ write "#{string}\n"
55
+ end
56
+ alias_method :println, :puts
57
+
58
+ # write a string
59
+ def write string
60
+ string.each_byte do |byte|
61
+ putc byte
62
+ end
63
+ string
64
+ end
65
+ alias_method :print, :write
66
+
67
+ # read a certain number of bytes and return them as a string
68
+ def read bytes = 1
69
+ chars = ""
70
+ chars += getc() until chars.length == bytes
71
+ return chars
72
+ end
73
+
74
+ def inspect
75
+ "<Digispark:#{@device.product}:@#{@device.bus_number}->#{@device.device_address}>"
76
+ end
77
+ alias_method :to_s, :inspect
78
+
79
+
80
+
81
+ private
82
+
83
+ def io
84
+ unless @handle
85
+ @handle = @device.open
86
+ end
87
+
88
+ @handle
89
+ end
90
+
91
+ def control_transfer(opts = {}) #:nodoc:
92
+ io.control_transfer({
93
+ wIndex: 0,
94
+ wValue: 0,
95
+ bmRequestType: usb_request_type(opts),
96
+ timeout: Timeout
97
+ }.merge opts)
98
+ rescue LIBUSB::ERROR_TIMEOUT, LIBUSB::ERROR_IO
99
+ raise ErrorCrashed
100
+ end
101
+
102
+ # calculate usb request type
103
+ def usb_request_type opts #:nodoc:
104
+ value = LIBUSB::REQUEST_TYPE_CLASS | LIBUSB::RECIPIENT_DEVICE
105
+ value |= LIBUSB::ENDPOINT_OUT if opts.has_key? :dataOut
106
+ value |= LIBUSB::ENDPOINT_IN if opts.has_key? :dataIn
107
+ return value
108
+ end
109
+ end
110
+
111
+ class DigiUSB::ErrorCrashed < StandardError
112
+ def initialize
113
+ super("Digispark has crashed, most likely due to not running DigiUSB.refresh() often enough in device program #{$!.class.name}")
114
+ end
115
+ end
116
+
117
+
@@ -0,0 +1 @@
1
+ Do whatever you like with it!
@@ -0,0 +1,21 @@
1
+ DigiUSB is a little rubygem for talking to Digisparks using the DigiUSB library provided by Digistump. It works a little bit like a digi-serial port - you can send digi-bytes to it and read digi-bytes out, and includes some common digi-methods like gets, puts, print, and write, so you can use it a bit like an IO. DigiUSB also comes with a little terminal called digiterm, which you can use to sort of telnet in to your sparks!
2
+
3
+ Sometimes it's lonely on the internet so I made a virtual friend as an example. You can find it in the examples folder - open jake.ino up in the digispark version of the arduino software and upload it in the usual way. Once that's done pop open your terminal and enter 'digiterm'
4
+
5
+ Type in lines like these to play with your new best friend!:->
6
+
7
+ Jake! Jake! Good Morning! Whatcha' doin' buddy?
8
+ What are you meditating about, dude?
9
+ Haha! Beep! Boop! Boooop! Beep! Boop!
10
+ Huh! I like it!
11
+ Weeeeoooooooo!
12
+ Woooooah! Algebraic!
13
+
14
+ Eventually I found myself too busy to play with Jake but I didn't want him to get lonely, so I made a robot to play with him. You can setup your own friend for Jake too! Just create a cron job or alarm or whatever on your computer to run finn.rb from the examples folder however often you want. I chose once per second. You might want to opt for a more frequent interval!
15
+
16
+ I hope this all makes sense and is useful to someone.
17
+
18
+ As usual, I'm sorry I killed your cat. It was an accident, but I wont accept any liability for that or anything else digiusb might do to you or your beloved friends. What were you doing keeping all those knives in the catroom anyway?!
19
+
20
+
21
+ Bluebie
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: digiusb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Bluebie
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: libusb
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 0.2.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 0.2.0
30
+ description: ! "A tiny library for talking to digispark (a small arduino clone) over
31
+ usb a bit like talking\n to a real arduino through a serial port! This library
32
+ works with the DigiUSB library built in to Digistump's\n version of the Arduino
33
+ software. Also includes a tiny terminal tool, kinda like telnet for digisparks"
34
+ email: a@creativepony.com
35
+ executables:
36
+ - digiterm
37
+ extensions: []
38
+ extra_rdoc_files: []
39
+ files:
40
+ - lib/digiusb.rb
41
+ - readme.txt
42
+ - license.txt
43
+ - examples/finn.rb
44
+ - !binary |-
45
+ YmluL2RpZ2l0ZXJt
46
+ homepage: http://github.com/Bluebie/digiusb.rb
47
+ licenses: []
48
+ post_install_message:
49
+ rdoc_options:
50
+ - --main
51
+ - lib/digiusb.rb
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ! '>='
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubyforge_project:
68
+ rubygems_version: 1.8.24
69
+ signing_key:
70
+ specification_version: 3
71
+ summary: a tiny library for talking to digisparks
72
+ test_files: []
73
+ has_rdoc: