#include "ruby.h" #include "cm17a.h" #include #include #include #include #include #if HAVE_FCNTL_H #include #endif #if HAVE_SYS_IOCTL_H #include #endif #if HAVE_SYS_TIME_H #include #endif #if HAVE_SYS_TYPES_H #include #endif #if HAVE_UNISTD_H #include #endif #if HAVE_TERMIO_H #include #endif static X10_DEVICE fd; static VALUE mX10; static VALUE mCm17a; static VALUE cCm17aController; static VALUE eX10Error; /* * call-seq: * X10::Cm17a::Controller.new() * X10::Cm17a::Controller.new(device_name) * * Create a new CM17A controller on the serial device named * +device_name+. If no device name is given, then a default device * will be selected (/dev/ttyS0 for linux and COM1 * for windows). */ static VALUE cm17a_init(int argc, VALUE *argv, VALUE self) { const char * device_name = DEFAULT_SERIAL_DEVICE; if (argc > 0) device_name = STR2CSTR(argv[0]); fd = cm17a_open_device(device_name); if (fd == INVALID_X10_DEVICE) rb_raise(eX10Error, "Unable to open cm17a device '%s'", device_name); return self; } /* * call-seq: * controller.command(house, unit, command_code, steps) * * Send a command to the CM17A controller. The X10 unit to get the * address is given by the +house+ code (0-15) and the +unit+ code * (0-15). The command must be one of the following constants: * * * X10::Cm17a::ON -- Turn the device on. * * X10::Cm17a::OFF -- Turn the device off. * * X10::Cm17a::DIM -- Dim the device by +steps+ steps. * * X10::Cm17a::BRIGHT -- Brighten the device by +steps+ steps. * * Note that the unit code is ignored for the BRIGHT and * DIM commands. The bright/dim commands will effect the * last addressed unit. */ static VALUE cm17a_ruby_command( VALUE self, VALUE rhouse, VALUE rdevice, VALUE rcommand, VALUE rsteps) { int house = NUM2INT(rhouse); int device = NUM2INT(rdevice); int command = NUM2INT(rcommand); int steps = NUM2INT(rsteps); cm17a_command(fd, house, device, command, steps); return Qnil; } /* * Close the controller device. */ static VALUE cm17a_close(VALUE self) { cm17a_close_device(fd); return Qnil; } void Init_cm17a_api() { mX10 = rb_define_module("X10"); mCm17a = rb_define_module_under(mX10, "Cm17a"); cCm17aController = rb_define_class_under(mCm17a, "Controller", rb_cObject); eX10Error = rb_eval_string("X10::X10Error"); rb_define_method(cCm17aController, "initialize", cm17a_init, -1); rb_define_method(cCm17aController, "close", cm17a_close, 0); rb_define_method(cCm17aController, "command", cm17a_ruby_command, 4); rb_define_const(mCm17a, "ON", INT2NUM(CM17A_ON)); rb_define_const(mCm17a, "OFF", INT2NUM(CM17A_OFF)); rb_define_const(mCm17a, "BRIGHTEN", INT2NUM(CM17A_BRIGHTEN)); rb_define_const(mCm17a, "DIM", INT2NUM(CM17A_DIM)); }