Ravn::HAL::Device::

Rmc class

Ravn Rifle-Mounted Controller device adapter

Portions of this file were adapted from the ruby-hid library, used under the terms of the MIT license:

   Copyright (c) 2015 Andrew Faraday

   Permission is hereby granted, free of charge, to any person obtaining a copy
   of this software and associated documentation files (the "Software"), to deal
   in the Software without restriction, including without limitation the rights
   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
   copies of the Software, and to permit persons to whom the Software is
   furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be included in
   all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
   THE SOFTWARE.

Refs: - rubygems.org/gems/ruby_hid - www.kernel.org/doc/html/latest/input/joydev/joystick-api.html - docs.google.com/document/d/1MEKZb9hTe3MaBTIhZ6xWYGHynFVtUeU4RkjodvK41ZA

Constants

AXIS_CODE_MAP

Map of axis event codes to device event types

BUTTON_CODE_MAP

Map of button event codes to device event types.

EVENT_TYPES

JS_EVENT_BUTTON 0x01 /* button pressed/released / JS_EVENT_AXIS 0x02 / joystick moved / JS_EVENT_INIT 0x80 / initial state of device */

Event

Event type for raw input data Event = Struct.new( :tv_sec, :tv_usec, :type, :code, :value ) do

MESSAGE_TYPE_MAP

Map of device event types to BDE message types:

BLE Joystick | SOC | Odin Event | GPIO | Button —————-|——-|—————————- Button 1 = On | P0.31 | “Mode-Shift”Button 2 = On | P0.02 | “Clear Display”Button 3 = On | P0.17 | “Joystick Center Switch”Button 4 = On | P0.24 | “Menu”Button 5 = On | P0.29 | “Back”Y-axis = -127 | P0.22 | “Joystick Forward”Y-axis = 127 | P0.15 | “Joystick Back”X-axis = 127 | P0.20 | “Joystick Right”X-axis = -127 | P0.13 | “Joystick Left”

Attributes

axis_offset R

The axis_offset setting

axis_states R

Recorded axis states for coalescing axis events

block_size R

The block_size setting

device RW

The input device filehandle

event_format R

The event_format setting

neutral_offset R

The neutral_offset setting

thread R

The thread reading from the device file.

timer R

Concurrent::TimerTask that periodically checks for device presence.

Public Class Methods

new( * )

Create a new device adapter for a Rifle-Mounted Controller.

# File lib/ravn/hal/device/rmc.rb, line 156
def initialize( * )
        @device = nil
        @axis_states = Hash.new { |h, k| h[ k ] = 0 }

        @thread         = nil
        @block_size     = self.class.block_size
        @event_format   = self.class.event_format
        @axis_offset    = self.class.axis_offset
        @neutral_offset = self.class.neutral_offset
        @timer          = self.make_check_device_timer

        super
end

Public Instance Methods

check_device()

If the device hasn’t been set up and is available, attempt to start it. If it has disappeared, stop what we’re doing, recover if it comes back.

# File lib/ravn/hal/device/rmc.rb, line 237
def check_device
        if self.class.device_file.exist?
                unless self.device
                        self.open_device( self.class.device_file )
                        self.start_listening
                end
        else
                self.stop
                self.log.debug "No input device present."
        end
end
gather_device_info()

Return a Hash that contains information describing this device for intra-device communication; overridden to set the correct vendor.

# File lib/ravn/hal/device/rmc.rb, line 230
def gather_device_info
        return super.merge( vendor: 'ravn' )
end
handle_terminated_event()

Concurrent::Actor API — shut down cleanly.

# File lib/ravn/hal/device/rmc.rb, line 223
def handle_terminated_event
        self.stop
end
start()

Start listening for incoming input events and translating them to BDE messages.

# File lib/ravn/hal/device/rmc.rb, line 209
def start
        self.timer.execute
end
stop()

Stop the device.

# File lib/ravn/hal/device/rmc.rb, line 215
def stop
        @device&.close
        @device = nil
        @thread&.join
end

Protected Instance Methods

each_event( &block )

Iterate over events read from the input device. Returns an enumerator if called without a block, or yields raw events to the block if one is provided.

# File lib/ravn/hal/device/rmc.rb, line 294
def each_event( &block )
        iter = self.make_event_iterator
        return iter unless block
        return iter.each( &block )
end
make_check_device_timer()

Build a Concurrent::TimerTask that opens the RMC device if present, or ensures it is closed if not.

# File lib/ravn/hal/device/rmc.rb, line 256
def make_check_device_timer
        timer = Concurrent::TimerTask.new { self.check_device }
        timer.execution_interval = self.class.status_frequency
        timer.add_observer( Ravn::LoggingTaskObserver.new(:rmc_device_check_timer) )

        return timer
end
make_event_iterator()

Create a Enumerator that yields raw events from the input device.

# File lib/ravn/hal/device/rmc.rb, line 302
def make_event_iterator
        return Enumerator.new do |yielder|
                while self.device
                        data = self.device.read( self.block_size )
                        self.log.debug "Raw data: %p" % [ data ]
                        unless data.bytesize == self.block_size
                                self.log.error "Malformed input data (incorrect size): %p" % [ data ]
                                next
                        end

                        raw_fields = data.unpack( self.event_format )
                        unless raw_fields.all?
                                self.log.error "Malformed input data (unpack failed): %p" % [ data ]
                                next
                        end

                        event = Event.new( *raw_fields )
                        yielder.yield( event )
                end

        rescue Errno::ENODEV, IOError => err
                self.log.warn "%s error while listening to device: %s" % [ err.class.name, err.message ]
                @device = nil
        end
end
map_axis_event( axis_event )

Map the event from axis+value to a single type.

# File lib/ravn/hal/device/rmc.rb, line 371
def map_axis_event( axis_event )
        if ( axis = AXIS_CODE_MAP[axis_event.code] )
                case axis
                when :x
                        if axis_event.value > 0
                                return :x_right
                        elsif axis_event.value < 0
                                return :x_left
                        else
                                self.log.debug "Not mapping the release axis event."
                                return nil
                        end
                when :y
                        if axis_event.value > 0
                                return :y_down
                        elsif axis_event.value < 0
                                return :y_up
                        else
                                self.log.debug "Not mapping the release axis event."
                                return nil
                        end
                else
                        raise "Unknown axis %p!" % [ axis ]
                end
        else
                self.log.debug "Unhandled axis event: %p" % [ axis_event ]
                return nil
        end
end
open_device( device_file )

Open the input device.

# File lib/ravn/hal/device/rmc.rb, line 266
def open_device( device_file )
        @device = self.class.device_file.open( 'rb' )
        self.log.info "Opened input device %s" % [ self.class.device_file ]
rescue Errno::ENOENT => err
        self.log.error "%p while opening device file: %s" % [ err.class, err.message ]
end
start_listening()

Start reading input events.

# File lib/ravn/hal/device/rmc.rb, line 275
def start_listening
        @thread = Thread.new do
                Thread.current.name = "RMC I/O loop"
                self.each_event do |input_event|
                        if ( type = self.translate_input_event( input_event ) )
                                message = Ravn::HAL::Message.new( type, data: {value: input_event.value} )
                                self.log.debug "Sending translated input event: %p" % [ message ]
                                self.filter_up( message )
                        else
                                self.log.debug "Couldn't translate input event: %p" % [ input_event ]
                        end
                end
        end
end
translate_axis_event( axis_event )

Return the code of the given axis_event into a BDE message type, returning nil if a message shouldn’t be sent.

# File lib/ravn/hal/device/rmc.rb, line 361
def translate_axis_event( axis_event )
        axis_type = self.map_axis_event( axis_event ) or return nil
        message_type = MESSAGE_TYPE_MAP[ axis_type ] or
                raise "Unmapped axis %p" % [ axis_type ]

        return message_type
end
translate_button_event( button_event )

Return the code of the given button_event into a BDE message type.

# File lib/ravn/hal/device/rmc.rb, line 347
def translate_button_event( button_event )
        if ( button = BUTTON_CODE_MAP[button_event.code] )
                message_type = MESSAGE_TYPE_MAP[ button ] or
                        raise "Unmapped button %p" % [ button ]
                return message_type
        else
                self.log.debug "Unhandled button event: %p" % [ button_event ]
                return nil
        end
end
translate_input_event( input_event )

Translate the type and code of the given input_event into a BDE message type.

# File lib/ravn/hal/device/rmc.rb, line 330
def translate_input_event( input_event )
        self.log.debug "Translating input event: %p" % [ input_event ]
        case EVENT_TYPES[ input_event.type ]
        when :button
                self.log.debug "  button event!"
                return self.translate_button_event( input_event )
        when :axis
                self.log.debug "  axis event!"
                return self.translate_axis_event( input_event )
        else
                self.log.debug "Unhandled RMC input event type: %p" % [ input_event ]
                return nil
        end
end