lib/x10/cm17a_device.rb in x10-cm17a-0.9.0 vs lib/x10/cm17a_device.rb in x10-cm17a-1.0.0
- old
+ new
@@ -30,113 +30,32 @@
@address = X10.make_address(house, unit)
end
# Turn the device on.
def on
- @controller.command(@house, @unit, X10::Cm17a::ON, 0)
- @level = 1.0 if @on == false
- @on = true
+ @controller.command(@house, @unit, :on, 0)
end
# Turn the device off.
def off
- @controller.command(@house, @unit, X10::Cm17a::OFF, 0)
- @level = 0.0
- @on = false
+ @controller.command(@house, @unit, :off, 0)
end
- # Adjust the brightness level.
+ # Adjust the brightness level by +steps+. If +steps+ is
+ # positive, the brightness level is increased that many steps.
+ # If +steps+ is negative, the brightness level is decreased by
+ # <tt>|steps|</tt>.
#
- # The level adjustment is given as a number between 0 and 1. An
- # adjustment amount of 1 will adjust a completely dim device
- # into a completely bright device. An adjustment of 0.5 will be
- # one half of full range.
- #
- # Negative adjustment amounts dim the device. Positive
- # adjustments brighten the device.
- def adjust(amount)
- steps = steps_for(amount)
- while steps > 0
- step = (steps > 6 ? 6 : steps)
- if amount > 0
- brighten(step)
- else
- dim(step)
- end
- steps -= step
+ # The controller limits the step size to 6, so larger steps must
+ # be broken up.
+ def step(steps)
+ direction = (steps > 0) ? :brighten : :dim
+ nsteps = steps.abs
+ while nsteps > 0
+ n = (nsteps > 6) ? 6 : nsteps
+ @controller.command(@house, @unit, direction, n)
+ nsteps -= n
end
- if @level
- @level += amount
- @level = 0.0 if @level < 0
- @level = 1.0 if @level > 1
- end
end
-
- private
-
- # -- Questionable Methods --------------------------------------
-
- # The following methods are provided as private methods. They
- # are used in testing, but I am uncertain whether they should be
- # part of the public interface, due to the uncertaintity of
- # there implementation.
-
- # Is the device on?
- #
- # Since the CM17A protocol is write only, we can't really sense
- # the on/off state of the device, so we remember the last
- # commanded state. This approach has the following
- # disadvantages:
- #
- # * The on/off state is indeterminate until the first command is
- # issued. Asking for the on/off state before the first
- # command will throw an X10::X10Error exception.
- # * If there is more than one device object controlling a single
- # physical device, then the on/off state can be invalidated by
- # second device object.
- def on?
- fail X10::X10Error, "On/Off state is unknown" if @on.nil?
- @on
- end
-
- # Is the device off?
- #
- # See also the discussion in the on? method.
- def off?
- ! on?
- end
-
- # Return the current brightness level between 0 (very dim) and
- # 1.0 (very bright) of the device.
- #
- # Again, since the CM17A protocol is write only, the same
- # caveats that apply to on? and off? are also applicable here.
- #
- # Furthermore, the real brightness level is a mystery. The
- # protocol docs say that stepping the brighteness level changes
- # it by approximately 5%. However, there seems to be only 10 or
- # so steps from completely dim to completely bright.
- def level
- fail X10::X10Error, "Level is unknown" if @level.nil?
- @level
- end
-
- # -- Helper Commands -------------------------------------------
-
- # Brightness steps for the given amount.
- def steps_for(amount)
- (amount * 10).round.abs
- end
-
- # Send a dim command for the given number of steps.
- def dim(steps)
- @controller.command(@house, @unit, X10::Cm17a::DIM, steps)
- end
-
- # Send a brighten command for the given number of steps.
- def brighten(steps)
- @controller.command(@house, @unit, X10::Cm17a::BRIGHTEN, steps)
- end
-
end
end
end