mzyy94
8/2/2014 - 8:37 PM

Ruby C extension impl 4 ambxlight

Ruby C extension impl 4 ambxlight

#include <stdio.h>
#include <ruby.h>
#include <libambxlight/libambxlight.h>

VALUE cAmbxlight;

VALUE device_count;

VALUE wAmbxlight_change_color_rgb(VALUE self, VALUE red, VALUE green, VALUE blue)
{
        int r = NUM2INT(red);
        int g = NUM2INT(green);
        int b = NUM2INT(blue);
        libambxlight_device *device;

        Data_Get_Struct(self, libambxlight_device, device);
        libambxlight_change_color_rgb(*device, (unsigned char)r, (unsigned char)g, (unsigned char)b);

        return Qnil;
}

VALUE wAmbxlight_change_color_rgb_with_fade(VALUE self, VALUE red, VALUE green, VALUE blue, VALUE millisec)
{
        int r = NUM2INT(red);
        int g = NUM2INT(green);
        int b = NUM2INT(blue);
        int msec = NUM2INT(millisec);
        libambxlight_device *device;

        Data_Get_Struct(self, libambxlight_device, device);
        libambxlight_change_color_rgb_with_fade(*device, (unsigned char)r, (unsigned char)g, (unsigned char)b, (unsigned int)msec);

        return Qnil;
}

void wAmbxlight_free(libambxlight_device *device)
{
        libambxlight_device_close(*device);
}

static VALUE wAmbxlight_alloc(VALUE klass)
{
        libambxlight_device *device = ALLOC(libambxlight_device);
        return Data_Wrap_Struct(klass, 0, wAmbxlight_free, device);
}

VALUE wAmbxlight_initialize(VALUE self, VALUE x)
{
        int index = NUM2INT(x);
        libambxlight_device *device;

        Data_Get_Struct(self, libambxlight_device, device);
        device->minor = index;
        libambxlight_device_open(device);

        return Qnil;
}

void Init_ambxlight()
{
        libambxlight_device **device;
        int count;

        cAmbxlight = rb_define_class("Ambxlight", rb_cObject);

        rb_define_alloc_func(cAmbxlight, wAmbxlight_alloc);
        rb_define_private_method(cAmbxlight, "initialize", RUBY_METHOD_FUNC(wAmbxlight_initialize), 1);
        rb_define_method(cAmbxlight, "change_color_rgb", RUBY_METHOD_FUNC(wAmbxlight_change_color_rgb), 3);
        rb_define_method(cAmbxlight, "change_color_rgb_with_fade", RUBY_METHOD_FUNC(wAmbxlight_change_color_rgb_with_fade), 4);

        count = libambxlight_get_device_list(&device);
        libambxlight_free_device_list(device);
        device_count = INT2NUM(count);

        rb_define_class_variable(cAmbxlight, "@@device_count", device_count);
        rb_define_alloc_func(cAmbxlight, wAmbxlight_alloc);
}
require 'mkmf'

have_header('libambxlight/libambxlight.h') && have_library('ambxlight') && create_makefile('ambxlight');
require 'ambxlight'

a = Ambxlight.new 1
# turn off light
a.change_color_rgb 0, 0, 0 
# change color to red with 2000 msec fading
a.change_color_rgb_with_fade 255, 0, 0, 2000
sleep 2
# change color to blue with 2000 msec fading
a.change_color_rgb_with_fade 0, 0, 255, 2000
sleep 2
# change color to green with 2000 msec fading
a.change_color_rgb_with_fade 0, 255, 0, 2000
sleep 2
# turn off light with 2000 msec fading
a.change_color_rgb_with_fade 0, 0, 0, 2000
sleep 2