Channel

class
Superclass
rb_cObject
Extended With
Loggability

An assigned ANT channel object.

Constants

DEFAULT_EXTENDED_OPTIONS

The default channel options

DEFAULT_NETWORK_NUMBER

The default network number

Public Instance Methods

anchor
channel_number → integer

Return the channel number assigned to the Channel.

static VALUE
rant_channel_channel_number( VALUE self )
{
    rant_channel_t *ptr = rant_get_channel( self );

    return INT2FIX( ptr->channel_num );
}
anchor
close

Close the channel and remove it from the registry.

static VALUE
rant_channel_close( int argc, VALUE *argv, VALUE self )
{
    rant_channel_t *ptr = rant_get_channel( self );
    VALUE timeout;
    VALUE registry = rb_iv_get( rant_cAntChannel, "@registry" );
    unsigned int ulResponseTime = 0;

    rb_scan_args( argc, argv, "01", &timeout );

    if ( RTEST(timeout) )
        ulResponseTime = NUM2UINT( timeout );

    rant_log_obj( self, "info", "Closing channel %d (with timeout %d).", ptr->channel_num, ulResponseTime );
    if ( !ANT_CloseChannel_RTO( ptr->channel_num, ulResponseTime ) ) {
        rb_raise( rb_eRuntimeError, "Failed to close the channel." );
    }
    rant_log_obj( self, "info", "Channel %d closed.", ptr->channel_num );

    rb_hash_delete( registry, INT2FIX( ptr->channel_num ) );

    return Qtrue;
}
anchor
closed? → true or false

Returns true if the channel has been closed; i.e., if it's not longer the registered channel for its channel number.

static VALUE
rant_channel_closed_p( VALUE self )
{
    rant_channel_t *ptr = rant_get_channel( self );
    VALUE registry = rb_iv_get( rant_cAntChannel, "@registry" );
    VALUE channel = rb_hash_lookup( registry, INT2FIX( ptr->channel_num ) );

    return self == channel ? Qfalse : Qtrue;
}
anchor
inspect()

Return a human-readable version of the object suitable for debugging.

# File lib/ant/channel.rb, line 52
def inspect
    return "#<%p:%#x {%d} %#02x on network %d: %d%s>" % [
        self.class,
        self.object_id,
        self.channel_number,
        self.channel_type,
        self.network_number,
        self.extended_options,
        self.closed? ? " (closed)" : "",
    ]
end
anchor
on_event {|channel_num, event_id, data| ... }

Set up a callback for events on the receiving channel.

static VALUE
rant_channel_on_event( int argc, VALUE *argv, VALUE self )
{
    rant_channel_t *ptr = rant_get_channel( self );
    VALUE callback = Qnil;

    rb_scan_args( argc, argv, "0&", &callback );

    if ( !RTEST(callback) ) {
        rb_raise( rb_eLocalJumpError, "block required, but not given" );
    }

    rant_log_obj( self, "debug", "Channel event callback is: %s", RSTRING_PTR(rb_inspect(callback)) );
    ptr->callback = callback;

    ANT_AssignChannelEventFunction( ptr->channel_num, rant_channel_on_event_callback, ptr->buffer );

    return Qtrue;
}
anchor
open( tineout=0 )

Open the channel. If it hasn't completed within timeout seconds, raises a RuntimeError.

static VALUE
rant_channel_open( int argc, VALUE *argv, VALUE self )
{
    rant_channel_t *ptr = rant_get_channel( self );
    VALUE timeout;
    unsigned int ulResponseTime = 0;

    rb_scan_args( argc, argv, "01", &timeout );

    if ( RTEST(timeout) )
        ulResponseTime = NUM2UINT( timeout );

    if ( !ANT_OpenChannel_RTO( ptr->channel_num, ulResponseTime ) ) {
        rb_raise( rb_eRuntimeError, "Failed to open the channel." );
    }

    return Qtrue;
}
anchor
send_acknowledged_data( data )

Send the given data as an acknowledged transmission. The data cannot be longer than 8 bytes in length.

static VALUE
rant_channel_send_acknowledged_data( VALUE self, VALUE data )
{
    rant_channel_t *ptr = rant_get_channel( self );
    UCHAR aucTempBuffer[] = {0, 0, 0, 0, 0, 0, 0, 0};

    StringValue( data );
    if ( RSTRING_LEN(data) > 8 ) {
        rb_raise( rb_eArgError, "Data can't be longer than 8 bytes." );
    }
    strncpy( (char *)aucTempBuffer, StringValuePtr(data), RSTRING_LEN(data) );

    ANT_SendAcknowledgedData( ptr->channel_num, aucTempBuffer );

    return Qtrue;
}
anchor
send_broadcast_data( data )

Send the given data as a broadcast transmission. The data cannot be longer than 8 bytes in length.

static VALUE
rant_channel_send_broadcast_data( VALUE self, VALUE data )
{
    rant_channel_t *ptr = rant_get_channel( self );
    UCHAR aucTempBuffer[8] = {0, 0, 0, 0, 0, 0, 0, 0,};

    StringValue( data );
    if ( RSTRING_LEN(data) > 8 ) {
        rb_raise( rb_eArgError, "Data can't be longer than 8 bytes." );
    }
    strncpy( (char *)aucTempBuffer, StringValuePtr(data), RSTRING_LEN(data) );

    ANT_SendBroadcastData( ptr->channel_num, aucTempBuffer );

    return Qtrue;
}
anchor
send_burst_transfer( data )

Send the given data as one or more burst packets.

static VALUE
rant_channel_send_burst_transfer( VALUE self, VALUE data )
{
    rant_channel_t *ptr = rant_get_channel( self );
    unsigned char *data_s;
    long data_len = RSTRING_LEN( data );
    unsigned short usNumDataPackets = data_len / 8,
        remainingBytes = data_len % 8;

    data_s = ALLOC_N( unsigned char, data_len );
    strncpy( (char *)data_s, StringValuePtr(data), data_len );

    // Pad it to 8-byte alignment
    if ( remainingBytes ) {
        int pad_bytes = (8 - remainingBytes);
        REALLOC_N( data_s, unsigned char, data_len + pad_bytes );
        memset( data_s + data_len, 0, pad_bytes );

        usNumDataPackets += 1;
    }

    VALUE data_string = rb_enc_str_new( (char *)data_s, usNumDataPackets * 8, rb_ascii8bit_encoding() );
    VALUE hexdump = rb_funcall( rant_mAntDataUtilities, rb_intern("hexdump"), 1, data_string );

    rant_log_obj( self, "debug", "Sending burst packets:\n%s", RSTRING_PTR(hexdump) );
    if ( !ANT_SendBurstTransfer(ptr->channel_num, data_s, usNumDataPackets) ) {
        rb_raise( rb_eRuntimeError, "failed to send burst transfer." );
    }

    return Qtrue;
}
anchor
set_channel_id( device_number, device_type, transmission_type, timeout=0 )

Set the channel ID using the combination of the device_number, device_type, and transmission_type. If the assignment hasn't been set in timeout seconds, aborts and returns nil.

static VALUE
rant_channel_set_channel_id( int argc, VALUE *argv, VALUE self )
{
    rant_channel_t *ptr = rant_get_channel( self );
    VALUE device_number, device_type, transmission_type, timeout;
    unsigned short usDeviceNumber;
    unsigned char ucDeviceType,
        ucTransmissionType;
    unsigned int ulResponseTime = 0;
    bool result;

    rb_scan_args( argc, argv, "31", &device_number, &device_type, &transmission_type, &timeout );

    usDeviceNumber = NUM2USHORT( device_number );
    ucDeviceType = NUM2USHORT( device_type );
    ucTransmissionType = NUM2USHORT( transmission_type );

    if ( RTEST(timeout) )
        ulResponseTime = NUM2UINT( timeout );

    result = ANT_SetChannelId_RTO( ptr->channel_num, usDeviceNumber, ucDeviceType,
        ucTransmissionType, ulResponseTime );

    if ( !result ) {
        rb_raise( rb_eRuntimeError, "Failed to set the channel id." );
    }

    return Qtrue;
}
anchor
set_channel_rf_freq( frequency )

Set the ANT RF frequency.

static VALUE
rant_channel_set_channel_rf_freq( VALUE self, VALUE frequency )
{
    rant_channel_t *ptr = rant_get_channel( self );
    unsigned short ucRFFreq = NUM2USHORT( frequency );

    if ( ucRFFreq > 124 ) {
        rb_raise( rb_eArgError, "frequency must be between 0 and 124." );
    }

    ANT_SetChannelRFFreq( ptr->channel_num, ucRFFreq );

    return Qtrue;
}
Also aliased as: set_channel_rf_frequency
anchor
Alias for: set_channel_rf_freq
anchor
set_event_handlers( mod=Ant::Channel::EventCallbacks )

Set up the given mod as the handler module for channel events.

# File lib/ant/channel.rb, line 45
def set_event_handlers( mod=Ant::Channel::EventCallbacks )
    self.extend( mod )
    self.on_event( &self.method(:handle_event_callback) )
end
anchor
unknown()

Channel registry, keyed by channel number.

# File lib/ant/channel.rb, line 34
singleton_class.attr_reader( :registry )