Created
January 4, 2011 17:53
-
-
Save geoffyoungs/765104 to your computer and use it in GitHub Desktop.
Dream Cheeky Mini RC Control from linux
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'usb' | |
require 'pp' | |
class StutsCar | |
attr_reader :handle | |
def initialize(handle) | |
@handle = handle | |
@handle.public_methods(false) | |
@handle.usb_detach_kernel_driver_np(0, 0) rescue nil | |
@handle.usb_claim_interface(0) rescue nil | |
@handle.usb_claim_interface(0) rescue nil | |
end | |
def forward | |
msg(0x01) | |
end | |
def reverse | |
msg 0x8 | |
end | |
def forward_and_right | |
msg(0x2) | |
end | |
def forward_and_left | |
msg 0x20 | |
end | |
def reverse_and_left | |
msg 0x10 | |
end | |
def reverse_and_right | |
msg 0x04 | |
end | |
def stop | |
msg(0) | |
end | |
def set_config(num) | |
@handle.usb_control_msg(0, 9, num, 0, "", 0) | |
end | |
def read_descriptor | |
buf = ("\0"*4) | |
@handle.usb_control_msg(0x81, 0x6, 0x2200, 0, buf, 0) | |
end | |
def class_request_out | |
@handle.usb_control_msg(0x21, 0xA, 0, 0, "", 0) | |
end | |
private | |
def msg(byte) | |
b = '' << byte | |
@handle.usb_control_msg(0x21, 9, 0x0200, 0, b, 0) | |
end | |
end | |
bus = nil | |
device = nil | |
USB.devices.each do |dev| | |
begin | |
if '0a81' == ("%04x" % dev.idVendor) && | |
'0702' == ("%04x" % dev.idProduct) | |
bus = dev.bus.dirname | |
device = dev.filename | |
end | |
rescue | |
end | |
end | |
dev = nil | |
if bus.nil? && device.nil? | |
exit -1 | |
else | |
dev = USB.find_bus(bus.to_i).find_device(device.to_i) | |
end | |
dev.open do |h| | |
car = StutsCar.new(h) | |
car.set_config(1) | |
car.class_request_out | |
# car.forward | |
# sleep 0.5 | |
# car.stop | |
# sleep 0.5 | |
# car.reverse | |
# car.reverse_and_right | |
# sleep 0.1 | |
# car.reverse | |
# sleep 0.7 | |
# car.stop | |
5.times do | |
car.forward_and_right | |
sleep 0.2 | |
car.stop | |
sleep 0.2 | |
car.reverse_and_left | |
sleep 0.2 | |
car.stop | |
sleep 0.2 | |
end | |
car.handle.usb_release_interface(0) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment