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 R

The input device filehandle

event_format R

The event_format setting

neutral_offset R

The neutral_offset setting

Public Class Methods

new( * )

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

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

        @block_size     = self.class.block_size
        @event_format   = self.class.event_format
        @axis_offset    = self.class.axis_offset
        @neutral_offset = self.class.neutral_offset

        super
end

Public Instance Methods

discover()

Do discovery of rifle-mounted controllers.

# File lib/ravn/hal/device/rmc.rb, line 193
def discover
        self.log.info "Opening input device %s" % [ self.class.device_file ]
        self.open_device( self.class.device_file )
end
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 256
def each_event( &block )
        iter = self.make_event_iterator
        return iter unless block
        return iter.each( &block )
end
make_event_iterator()

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

# File lib/ravn/hal/device/rmc.rb, line 264
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
        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 329
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.warn "Unhandled axis event: %p" % [ axis_event ]
                return nil
        end
end
on_event( event )

Concurrent::Actor API — Handle lifecycle events.

# File lib/ravn/hal/device/rmc.rb, line 227
def on_event( event )
        self.log.warn "Event: %p" % [ event ]
        case event
        when :terminated
                self.log.warn "%s; stopping RMC device" % [ event ]
                self.stop
        end
end
open_device( device_file )

Open the input device.

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

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

# File lib/ravn/hal/device/rmc.rb, line 200
def start
        super
        if self.device
                self.start_listening
        else
                self.log.warn "No input device discovered; skipping start."
        end
end
start_listening()

Start reading input events.

# File lib/ravn/hal/device/rmc.rb, line 238
def start_listening
        @thread = Thread.new do
                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.warn "Sending translated input event: %p" % [ message ]
                                self.filter_up( message )
                        else
                                self.log.warn "Couldn't translate input event: %p" % [ input_event ]
                        end
                end
        end
end
stop()

Stop the device. :TODO: Hook this up to on_event

# File lib/ravn/hal/device/rmc.rb, line 212
def stop
        self.device&.close
        @device = nil
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 319
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 305
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.warn "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 288
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.warn "Unhandled RMC input event type: %p" % [ input_event ]
                return nil
        end
end