Last active
November 12, 2017 12:40
-
-
Save mzyy94/4b89b685f71aaf2c7888 to your computer and use it in GitHub Desktop.
Ruby C extension impl 4 ambxlight
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'mkmf' | |
have_header('libambxlight/libambxlight.h') && have_library('ambxlight') && create_makefile('ambxlight'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment