Files
maptool/lib/maptool/outputs.rb
T

149 lines
5.5 KiB
Ruby
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
require "json"
require_relative "can"
module Maptool
# Outputs on the bus — relay modules for now, no current measured — and
# the bindings that drive them from a keypad's channels (docs/design.adoc,
# "Switches and the keypad"): maptool is the controller, the module only
# switches. The bindings are the screen profile's (public/drive/road.json)
# with an "output" in place of an "action":
# { "input": "pad.K6", "output": "relays.2", "mode": "hold", "led": "red" }
# Modes: toggle, on, off, hold (on while the key is held — off when it's
# let go or the keypad goes silent), pulse (on for "pulse_s", 1 s by default).
#
# can.json: "outputs": [ { "name": "relays", "node": 48, "channels": 8, "simulated": true,
# "labels": { "1": "Lights", "2": "Winch in" } } ]
# A module's protocol, ours (after LD's native CAN control): RPDO1
# 0x200+node, a bit per channel (byte 0: 1–8, byte 1: 9–16), the whole
# state in every frame, sent on a change and every REPEAT_S — the module
# turns everything off after 2 s without one. "simulated": no frames,
# the state is just kept here (/sim.html shows it).
class Outputs
REPEAT_S = 0.1
MODES = %w[toggle on off hold pulse].freeze
Module = Struct.new(:name, :node, :channels, :simulated, :labels, :state, keyword_init: true)
attr_reader :modules
# send: a block that puts a Frame on the bus (not called for simulated modules)
def initialize(configs, &send)
@modules = configs.map do |c|
Module.new(name: c.fetch("name"), node: Integer(c.fetch("node", 0)), channels: c.fetch("channels", 8),
simulated: c.fetch("simulated", false), labels: c.fetch("labels", {}), state: Array.new(c.fetch("channels", 8), false))
end
@send = send
@mutex = Mutex.new
@pulses = {}
@held = {} # channel (the key) → outputs it holds on
end
def find(output)
name, ch = output.to_s.split(".", 2)
m = @modules.find { |x| x.name == name }
i = Integer(ch, exception: false)
[m, i - 1] if m && i&.between?(1, m.channels)
end
def on?(output)
m, i = find(output)
m ? @mutex.synchronize { m.state[i] } : nil
end
def set(output, value)
m, i = find(output)
raise ArgumentError, "no output #{output}" unless m
changed = @mutex.synchronize do
next false if m.state[i] == value
m.state[i] = value
@pulses.delete(output) unless value
true
end
transmit(m) if changed
changed
end
# A keypad event ("pad.K6", "pad.K6 up") through the bindings that have an output.
def input(event, bindings, at: Time.now.to_f)
up = event.end_with?(" up")
channel = event.delete_suffix(" up")
bindings.each do |b|
next unless b["output"] && b["input"] == channel && find(b["output"]) # a profile's output this car hasn't: skipped
output = b["output"]
case b.fetch("mode", "toggle")
when "toggle" then set(output, !on?(output)) unless up
when "on" then set(output, true) unless up
when "off" then set(output, false) unless up
when "hold"
set(output, !up)
up ? @held.delete(channel) : (@held[channel] ||= []) << output
when "pulse"
next if up
set(output, true)
@mutex.synchronize { @pulses[output] = at + b.fetch("pulse_s", 1).to_f }
end
end
end
# A keypad gone silent: what it held goes off (the fail-safe).
def keypad_lost(device)
@held.select { |channel, _| channel.start_with?("#{device}.") }.each do |channel, outputs|
outputs.each { |o| set(o, false) }
@held.delete(channel)
end
end
# Every REPEAT_S: pulses ending, the state sent again (the module's watchdog).
def tick(at: Time.now.to_f)
ended = @mutex.synchronize { @pulses.select { |_, till| till <= at }.keys }
ended.each { |o| set(o, false) }
@modules.each { |m| transmit(m) }
end
def frame(m)
bits = m.state.each_with_index.sum { |on, i| on ? 1 << i : 0 }
Can::Frame.new(0x200 + m.node, [bits].pack("Q<").byteslice(0, (m.channels + 7) / 8), false)
end
def snapshot
@mutex.synchronize do
@modules.map { |m| { name: m.name, node: m.node, simulated: m.simulated, channels: (1..m.channels).map { |n| { n: n, label: m.labels[n.to_s], on: m.state[n - 1] } } } }
end
end
private
def transmit(m)
return if m.simulated || !@send
@send.call(frame(m))
rescue StandardError => e
warn "outputs: #{m.name}: #{e.message}"
end
end
end
if $PROGRAM_NAME == __FILE__
sent = []
out = Maptool::Outputs.new([{ "name" => "relays", "node" => 48 }]) { |f| sent << f }
b = [{ "input" => "pad.K1", "output" => "relays.1" }, { "input" => "pad.K6", "output" => "relays.2", "mode" => "hold" },
{ "input" => "pad.K7", "output" => "relays.3", "mode" => "pulse", "pulse_s" => 2 }]
out.input("pad.K1", b)
raise "toggle" unless out.on?("relays.1")
out.input("pad.K6", b)
raise "hold" unless out.on?("relays.2")
out.keypad_lost("pad")
raise "fail-safe" if out.on?("relays.2")
out.input("pad.K7", b, at: 100)
out.tick(at: 101)
raise "pulse early" unless out.on?("relays.3")
out.tick(at: 102.1)
raise "pulse" if out.on?("relays.3")
out.input("pad.K1", [{ "input" => "pad.K1", "output" => "winch.1" }]) # no such module: nothing, no error
raise sent.last.inspect unless sent.last.id == 0x230 && sent.last.data == "\x01".b
puts "ok"
end