Module: X10

Defined in:
lib/x10.rb,
lib/x10/cm17a.rb,
lib/x10/cm17a_device.rb,
lib/x10/cm17a_remote.rb,
ext/cm17a_api/cm17a_api.c

Overview

The X10 Module. This module provides a root namespace for all X10 devices and software. It also provides a few utility methods for use by the X10 controllers and devices.

Defined Under Namespace

Modules: Cm17a, Cm17aRemote Classes: X10Error

Class Method Summary collapse

Class Method Details

.controllerObject

Return the controller currently in use.



48
49
50
# File 'lib/x10.rb', line 48

def controller
  @controller ||= discover_single_controller
end

.controller=(controller) ⇒ Object

Set the controller to be used to create X10 devices. If there is only one X10 controller loaded, then that controller will be used by default. Otherwise, the controller must be explicitly set using this method.



56
57
58
# File 'lib/x10.rb', line 56

def controller=(controller)
  @controller = controller
end

.device(address) ⇒ Object

Create an X10 device at the given address. THe address should be a string specifying the device address is standard X10 nomenclature (e.g. a1 … a16, b1 … b16, … p1 … p16).



42
43
44
45
# File 'lib/x10.rb', line 42

def device(address)
  house, unit = parse_address(address)
  controller.device(house, unit)
end

.discover_single_controllerObject

If there is only one X10 controller class defined in the object space, then use it by default.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/x10.rb', line 86

def discover_single_controller
  controllers = []
  ObjectSpace.each_object(Class) do |c|
	controllers << c if c.x10_controller?
  end
  case controllers.size
  when 0
	fail X10::X10Error, "No X10 Controllers Found"
  when 1
	controllers.first.new
  else
	fail X10::X10Error, "Multiple X10 Controllers Found"
  end      
end

.make_address(house, unit) ⇒ Object

Make a canonical X10 device address from the house number and unit. House and unit numbers are zero based.



62
63
64
# File 'lib/x10.rb', line 62

def make_address(house, unit)
  (house + ?a).chr + (unit+1).to_s
end

.parse_address(address) ⇒ Object

Parse a canonical X10 device adderss into house number and unit number. House and unit numbers are zero based.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/x10.rb', line 68

def parse_address(address)
  address = address.downcase
  if address !~ /^([a-p])(\d+)$/
	fail X10::X10Error, "Bad X10 device address [#{address}]"
  end
  house_letter = $1
  unit = $2.to_i - 1
  
  if unit < 0 || unit > 15
	fail X10::X10Error, "Bad X10 device address [#{address}]"
  end
  
  house = address[0] - ?a
  [house, unit]
end