ruxbee 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
@@ -0,0 +1,3 @@
1
+ module Ruxbee
2
+ VERSION = "0.1.0"
3
+ end