This repository has been archived by the owner on May 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdc_dimmer_command_2.rb
executable file
·52 lines (42 loc) · 1.63 KB
/
dc_dimmer_command_2.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env ruby
#
# Send rvc DC_DIMMER_COMMAND_2 to the canbus.
#
# See RV-C Specification, Table 6.25.6a
require 'optparse'
options = {}
OptionParser.new do |opts|
options[:brightness] = 100
options[:delay] = 255
opts.on('-i', '--instance NUM', Integer, '[REQUIRED] Dimmer instance to control')
opts.on('-c', '--command HEX', String, '[REQUIRED] Hex command code to send (e.g.: 13')
opts.on('-b', '--brightness NUM', Integer, 'Desired level (brightness), 0-100, 250, 251')
opts.on('-d', '--delay NUM', '--duration NUM', Integer, 'Seconds before executing, or duration of command')
opts.on('-h', '--help', 'Display this screen' ) do
puts opts
puts "\nCommon commands:\n"
exit
end
end.parse!(into: options)
if (options[:instance].nil? || options[:command].nil?)
raise OptionParser::MissingArgument.new("instance and command are required arguments")
end
dgn = '1FEDB'
pri = '0x6'
src = '0x99'
# NOTE: These commented lines are the old (CoachProxy) way of doing things.
# The new way (dgn.to_i(16)) is cleaner and SHOULD work, but hasn't been tested
# in all situations yet.
#
# See RV-C Specification, section 3.2
# dgn_hi = dgn.slice(0..2)
# dgn_lo = dgn.slice(3..4)
# bits = sprintf('%b0%b%b%b', pri.to_i(16), dgn_hi.to_i(16), dgn_lo.to_i(16), src.to_i(16))
bits = sprintf('%b0%b%b', pri.to_i(16), dgn.to_i(16), src.to_i(16))
hex_id = bits.to_i(2).to_s(16)
hex_data = sprintf('%02xFF%02x%02x%02x00FFFF',
options[:instance].to_i,
options[:brightness].to_i * 2,
options[:command].to_i(16),
options[:delay].to_i)
system "cansend can0 #{hex_id}##{hex_data}"