ruxbee 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE +11 -0
- data/README.md +36 -0
- data/README.rdoc +233 -0
- data/Rakefile +6 -0
- data/agpl.txt +661 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/examples/listen_api.rb +35 -0
- data/icon.jpg +0 -0
- data/lib/ruxbee/at_commands.rb +28 -0
- data/lib/ruxbee/config.rb +96 -0
- data/lib/ruxbee/frame/at_command.rb +35 -0
- data/lib/ruxbee/frame/at_command_response.rb +33 -0
- data/lib/ruxbee/frame/base_frame.rb +32 -0
- data/lib/ruxbee/frame/explicit_addressing_command.rb +52 -0
- data/lib/ruxbee/frame/explicit_rx_indicator.rb +23 -0
- data/lib/ruxbee/frame/frame.rb +128 -0
- data/lib/ruxbee/frame/io_data_sample_rx_indicator.rb +10 -0
- data/lib/ruxbee/frame/modem_status.rb +32 -0
- data/lib/ruxbee/frame/receive_packet.rb +6 -0
- data/lib/ruxbee/frame/receive_packet_16.rb +29 -0
- data/lib/ruxbee/frame/received_frame.rb +16 -0
- data/lib/ruxbee/frame/remote_command_request.rb +34 -0
- data/lib/ruxbee/frame/remote_command_response.rb +22 -0
- data/lib/ruxbee/frame/transmit_request.rb +7 -0
- data/lib/ruxbee/frame/transmit_status.rb +6 -0
- data/lib/ruxbee/rfmodule.rb +85 -0
- data/lib/ruxbee/version.rb +3 -0
- data/lib/ruxbee/xbee_api.rb +642 -0
- data/lib/ruxbee/xbee_cmd.rb +554 -0
- data/lib/ruxbee.rb +37 -0
- data/ruxbee.gemspec +34 -0
- metadata +140 -0
@@ -0,0 +1,85 @@
|
|
1
|
+
require 'serialport'
|
2
|
+
require 'ruxbee/config'
|
3
|
+
module XBee
|
4
|
+
##
|
5
|
+
# This is it, the base class where it all starts. Command mode or API mode, version 1 or version 2, all XBees descend
|
6
|
+
# from this class.
|
7
|
+
class RFModule
|
8
|
+
#include XBee
|
9
|
+
include Config
|
10
|
+
|
11
|
+
attr_accessor :xbee_serialport, :xbee_uart_config, :guard_time, :command_mode_timeout, :command_character, :node_discover_timeout, :node_identifier, :operation_mode, :api_mode, :transmission_mode
|
12
|
+
attr_reader :serial_number, :hardware_rev, :firmware_rev
|
13
|
+
|
14
|
+
VERSION = "2.1"
|
15
|
+
|
16
|
+
def version
|
17
|
+
VERSION
|
18
|
+
end
|
19
|
+
|
20
|
+
##
|
21
|
+
# This is the way we instantiate XBee modules now, via this factory method. It will ultimately autodetect what
|
22
|
+
# flavor of XBee module we're using and return the most appropriate subclass to control that module.
|
23
|
+
def initialize(xbee_usbdev_str, uart_config, operation_mode = :AT, transmission_mode = :SYNC)
|
24
|
+
unless uart_config.kind_of?(XBeeUARTConfig)
|
25
|
+
raise "uart_config must be an instance of XBeeUARTConfig for this to work"
|
26
|
+
end
|
27
|
+
unless operation_mode == :AT || operation_mode == :API
|
28
|
+
raise "XBee operation_mode must be either :AT or :API"
|
29
|
+
end
|
30
|
+
unless transmission_mode == :SYNC || transmission_mode == :ASYNC
|
31
|
+
raise "XBee transmission_mode must be either :SYNC (Synchronous) or :ASYNC (Asynchronous)"
|
32
|
+
end
|
33
|
+
self.xbee_uart_config = uart_config
|
34
|
+
@xbee_serialport = SerialPort.new( xbee_usbdev_str, uart_config.baud, uart_config.data_bits, uart_config.stop_bits, uart_config.parity )
|
35
|
+
@xbee_serialport.read_timeout = self.read_timeout(:short)
|
36
|
+
@guard_time = GuardTime.new
|
37
|
+
@command_mode_timeout= CommandModeTimeout.new
|
38
|
+
@command_character = CommandCharacter.new
|
39
|
+
@node_discover_timeout = NodeDiscoverTimeout.new
|
40
|
+
@node_identifier = NodeIdentifier.new
|
41
|
+
@operation_mode = operation_mode
|
42
|
+
@api_mode = ApiEnableMode.new
|
43
|
+
@transmission_mode = transmission_mode
|
44
|
+
end
|
45
|
+
|
46
|
+
def in_command_mode
|
47
|
+
sleep self.guard_time.in_seconds
|
48
|
+
@xbee_serialport.write(self.command_character.value * 3)
|
49
|
+
sleep self.guard_time.in_seconds
|
50
|
+
@xbee_serialport.read(3)
|
51
|
+
# actually do some work now ...
|
52
|
+
yield if block_given?
|
53
|
+
# Exit command mode
|
54
|
+
@xbee_serialport.write("ATCN\r")
|
55
|
+
@xbee_serialport.read(3)
|
56
|
+
end
|
57
|
+
|
58
|
+
##
|
59
|
+
# XBee response times vary based on both hardware and firmware versions. These
|
60
|
+
# constants may need to be adjusted for your devices, but these will
|
61
|
+
# work fine for most cases. The unit of time for a timeout constant is ms
|
62
|
+
def read_timeout(type = :short)
|
63
|
+
case type
|
64
|
+
when :short
|
65
|
+
1200
|
66
|
+
when :long
|
67
|
+
3000
|
68
|
+
else 3000
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
##
|
73
|
+
# a method for getting results from any Ruby SerialPort object. Not ideal, but seems effective enough.
|
74
|
+
def getresults sp
|
75
|
+
results = ""
|
76
|
+
while (c = sp.getc) do
|
77
|
+
results += "#{c.chr}"
|
78
|
+
end
|
79
|
+
|
80
|
+
# deal with multiple lines
|
81
|
+
results.gsub!( "\r", "\n")
|
82
|
+
end
|
83
|
+
|
84
|
+
end
|
85
|
+
end
|