dnl $Id$ dnl $URL$ dnl LCD4Linux Drivers conf part dnl dnl Copyright (C) 1999, 2000, 2001, 2002, 2003 Michael Reinelt dnl Copyright (C) 2004 The LCD4Linux Team dnl dnl This file is part of LCD4Linux. dnl dnl LCD4Linux is free software; you can redistribute it and/or modify dnl it under the terms of the GNU General Public License as published by dnl the Free Software Foundation; either version 2, or (at your option) dnl any later version. dnl dnl LCD4Linux is distributed in the hope that it will be useful, dnl but WITHOUT ANY WARRANTY; without even the implied warranty of dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the dnl GNU General Public License for more details. dnl dnl You should have received a copy of the GNU General Public License dnl along with this program; if not, write to the Free Software dnl Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. AC_MSG_CHECKING([which drivers to compile]) AC_ARG_WITH( drivers, [ --with-drivers= compile driver for displays in ,] [ drivers may be separated with commas,] [ 'all' (default) compiles all available drivers,] [ drivers may be excluded with 'all,!',] [ (try 'all,\!' if your shell complains...)] [ possible drivers are:] [ BeckmannEgle, BWCT, CrystalFontz, Curses, Cwlinux, D4D,] [ EA232Graphic, G15, GLCD2USB, HD44780, HD44780-I2C, IRLCD,] [ LCD2USB, LCDLinux, LEDMatrix, LCDTerm, LPH7508, LUIse,] [ LW_ABP, M50530, MatrixOrbital, MatrixOrbitalGX,] [ MilfordInstruments, Noritake, NULL, Pertelian, PHAnderson,] [ PICGraphic, picoLCD, picoLCDGraphic, PNG, PPM, RouterBoard,] [ Sample, serdisplib, ShuttleVFD, SimpleLCD, st2205, T6963,] [ Trefon, ULA200, USBHUB, USBLCD, VNC, WincorNixdorf, X11], drivers=$withval, drivers=all ) drivers=`echo $drivers|sed 's/,/ /g'` for driver in $drivers; do case $driver in !*) val="no" driver=`echo $driver|cut -c 2-` ;; *) val="yes" ;; esac case "$driver" in all) BECKMANNEGLE="yes" BWCT="yes" CRYSTALFONTZ="yes" CURSES="yes" CWLINUX="yes" D4D="yes" EA232graphic="yes" G15="yes" GLCD2USB="yes" HD44780="yes" HD44780_I2C="yes" IRLCD="yes" LCD2USB="yes" LCDLINUX="yes" LCDTERM="yes" LEDMATRIX="yes" LPH7508="yes" LUISE="yes" LW_ABP="yes" M50530="yes" MATRIXORBITAL="yes" MATRIXORBITALGX="yes" MILINST="yes" NORITAKE="yes" NULL="yes" PERTELIAN="yes" PHANDERSON="yes" PICGRAPHIC="yes" PICOLCD="yes" PICOLCDGRAPHIC="yes" PNG="yes" PPM="yes" ROUTERBOARD="yes" SAMPLE="yes" ST2205="yes" SERDISPLIB="yes" SHUTTLEVFD="yes" SIMPLELCD="yes" T6963="yes" Trefon="yes" ULA200="yes" USBHUB="yes" USBLCD="yes" VNC="yes" WINCORNIXDORF="yes" X11="yes" ;; BeckmannEgle) BECKMANNEGLE=$val ;; BWCT) BWCT=$val ;; CrystalFontz) CRYSTALFONTZ=$val ;; Curses) CURSES=$val ;; Cwlinux) CWLINUX=$val ;; D4D) D4D=$val ;; EA232graphic) EA232graphic=$val ;; G15) G15=$val ;; GLCD2USB) GLCD2USB=$val ;; HD44780) HD44780=$val ;; HD44780-I2C) HD44780_I2C=$val ;; IRLCD) IRLCD=$val ;; LCD2USB) LCD2USB=$val ;; LCDLinux) LCDLINUX=$val ;; LCDTerm) LCDTERM=$val ;; LEDMatrix) LEDMATRIX=$val ;; LPH7508) LPH7508=$val ;; LUIse) LUISE=$val ;; LW_ABP) LW_ABP=$val ;; M50530) M50530=$val ;; MatrixOrbital) MATRIXORBITAL=$val ;; MatrixOrbitalGX) MATRIXORBITALGX=$val ;; MilfordInstruments) MILINST=$val ;; Noritake) NORITAKE=$val; ;; NULL) NULL=$val; ;; Pertelian) PERTELIAN=$val ;; PHAnderson) PHANDERSON=$val ;; PICGraphic) PICGRAPHIC=$val ;; picoLCD) PICOLCD=$val ;; picoLCDGraphic) PICOLCDGRAPHIC=$val ;; PNG) PNG=$val ;; PPM) PPM=$val ;; RouterBoard) ROUTERBOARD=$val ;; Sample) SAMPLE=$val ;; serdisplib) SERDISPLIB=$val; ;; ShuttleVFD) SHUTTLEVFD=$val ;; SimpleLCD) SIMPLELCD=$val ;; st2205) ST2205=$val ;; T6963) T6963=$val ;; Trefon) Trefon=$val ;; ULA200) ULA200=$val ;; USBHUB) USBHUB=$val ;; USBLCD) USBLCD=$val ;; VNC) VNC=$val ;; WincorNixdorf) WINCORNIXDORF=$val ;; X11) X11=$val ;; *) AC_MSG_ERROR([Unknown driver '$driver']) ;; esac done AC_MSG_RESULT([done]) # generic display drivers TEXT="no" GRAPHIC="no" IMAGE="no" GPIO="no" # generiv I/O drivers PARPORT="no" SERIAL="no" I2C="no" KEYPAD="no" # generic libraries LIBUSB="no" LIBFTDI="no" if test "$BECKMANNEGLE" = "yes"; then TEXT="yes" GPIO="yes" SERIAL="yes" DRIVERS="$DRIVERS drv_BeckmannEgle.o" AC_DEFINE(WITH_BECKMANNEGLE,1,[Beckmann&Egle driver]) fi if test "$BWCT" = "yes"; then if test "$has_usb" = "true"; then TEXT="yes" DRIVERS="$DRIVERS drv_BWCT.o" LIBUSB="yes" AC_DEFINE(WITH_BWCT,1,[BWCT driver]) else AC_MSG_WARN(usb.h not found: BWCT driver disabled) fi fi if test "$CRYSTALFONTZ" = "yes"; then TEXT="yes" GPIO="yes" SERIAL="yes" KEYPAD="yes" DRIVERS="$DRIVERS drv_Crystalfontz.o" AC_DEFINE(WITH_CRYSTALFONTZ,1,[Crystalfontz driver]) fi if test "$CURSES" = "yes"; then if test "$has_curses" = true; then TEXT="yes" KEYPAD="yes" DRIVERS="$DRIVERS drv_Curses.o" DRVLIBS="$DRVLIBS $CURSES_LIBS" CPPFLAGS="$CPPFLAGS $CURSES_INCLUDES" AC_DEFINE(WITH_CURSES,1,[Curses driver]) else AC_MSG_WARN(curses not found: Curses driver disabled) fi fi if test "$CWLINUX" = "yes"; then TEXT="yes" GPIO="yes" SERIAL="yes" KEYPAD="yes" DRIVERS="$DRIVERS drv_Cwlinux.o" AC_DEFINE(WITH_CWLINUX,1,[CwLinux driver]) fi if test "$D4D" = "yes"; then TEXT="yes" GRAPHIC="yes" SERIAL="yes" DRIVERS="$DRIVERS drv_D4D.o" AC_DEFINE(WITH_D4D,1,[D4D driver]) fi if test "$EA232graphic" = "yes"; then GRAPHIC="yes" SERIAL="yes" GPIO="yes" DRIVERS="$DRIVERS drv_EA232graphic.o" AC_DEFINE(WITH_EA232graphic,1,[Electronic Assembly RS232 graphic driver]) fi if test "$G15" = "yes"; then if test "$has_usb" = "true"; then GRAPHIC="yes" LIBUSB="yes" DRIVERS="$DRIVERS drv_G15.o" AC_DEFINE(WITH_G15,1,[G-15 driver]) else AC_MSG_WARN(usb.h not found: G15 driver disabled) fi fi if test "$GLCD2USB" = "yes"; then if test "$has_usb" = "true"; then GRAPHIC="yes" KEYPAD="yes" DRIVERS="$DRIVERS drv_GLCD2USB.o" LIBUSB="yes" AC_DEFINE(WITH_GLCD2USB,1,[GLCD2USB driver]) else AC_MSG_WARN(usb.h not found: GLCD2USB driver disabled) fi fi if test "$HD44780_I2C" = "yes"; then TEXT="yes" I2C="yes" GPIO="yes" DRIVERS="$DRIVERS drv_HD44780.o" AC_DEFINE(WITH_HD44780,1,[HD44780 driver]) fi if test "$HD44780" = "yes"; then if test "$HD44780_I2C" != "yes"; then if test "$has_parport" = "true"; then TEXT="yes" PARPORT="yes" I2C="yes" GPIO="yes" KEYPAD="yes" DRIVERS="$DRIVERS drv_HD44780.o" AC_DEFINE(WITH_HD44780,1,[HD44780 driver]) else AC_MSG_WARN(asm/io.h or {linux/parport.h and linux/ppdev.h} not found: HD44780 driver disabled) fi else HD44780="no" AC_MSG_WARN(HD44780-i2c enabled disabling HD44780) fi fi if test "$IRLCD" = "yes"; then if test "$has_usb" = "true"; then TEXT="yes" SERIAL="yes" DRIVERS="$DRIVERS drv_IRLCD.o" LIBUSB="yes" AC_DEFINE(WITH_IRLCD,1,[IRLCD driver]) else AC_MSG_WARN(usb.h not found: IRLCD driver disabled) fi fi if test "$LCD2USB" = "yes"; then if test "$has_usb" = "true"; then TEXT="yes" SERIAL="yes" KEYPAD="yes" DRIVERS="$DRIVERS drv_LCD2USB.o" LIBUSB="yes" AC_DEFINE(WITH_LCD2USB,1,[LCD2USB driver]) else AC_MSG_WARN(usb.h not found: LCD2USB driver disabled) fi fi if test "$LCDLINUX" = "yes"; then if test "$has_lcd_linux" = true; then TEXT="yes" DRIVERS="$DRIVERS drv_LCDLinux.o" AC_DEFINE(WITH_LCDLINUX,1,[LCD-Linux driver]) else AC_MSG_WARN(linux/lcd-linux.h or linux/hd44780.h not found: LCD-Linux driver disabled) fi fi if test "$LCDTERM" = "yes"; then TEXT="yes" SERIAL="yes" DRIVERS="$DRIVERS drv_LCDTerm.o" AC_DEFINE(WITH_LCDTERM,1,[LCDTerm driver]) fi if test "$LEDMATRIX" = "yes"; then GRAPHIC="yes" DRIVERS="$DRIVERS drv_LEDMatrix.o" AC_DEFINE(WITH_LEDMATRIX,1,[LEDMatrix driver]) fi if test "$LPH7508" = "yes"; then if test "$has_parport" = "true"; then GRAPHIC="yes" GPIO="yes" PARPORT="yes" DRIVERS="$DRIVERS drv_LPH7508.o" AC_DEFINE(WITH_LPH7508,1,[LPH7508 driver]) else AC_MSG_WARN(asm/io.h or {linux/parport.h and linux/ppdev.h} not found: LPH7508 driver disabled) fi fi if test "$LUISE" = "yes"; then if test "$has_luise" = "true"; then GRAPHIC="yes" DRIVERS="$DRIVERS drv_LUIse.o" DRVLIBS="$DRVLIBS -L/usr/local/lib -lluise" AC_DEFINE(WITH_LUISE,1,[LUIse driver]) else AC_MSG_WARN(luise.h not found: LUIse driver disabled) fi fi if test "$LW_ABP" = "yes"; then TEXT="yes" SERIAL="yes" KEYPAD="yes" DRIVERS="$DRIVERS drv_LW_ABP.o" AC_DEFINE(WITH_LW_ABP,1,[LW ABP driver]) fi if test "$M50530" = "yes"; then if test "$has_parport" = "true"; then TEXT="yes" GPIO="yes" PARPORT="yes" DRIVERS="$DRIVERS drv_M50530.o" AC_DEFINE(WITH_M50530,1,[M50530 driver]) else AC_MSG_WARN(asm/io.h or {linux/parport.h and linux/ppdev.h} not found: M50530 driver disabled) fi fi if test "$MATRIXORBITAL" = "yes"; then TEXT="yes" GPIO="yes" SERIAL="yes" DRIVERS="$DRIVERS drv_MatrixOrbital.o" AC_DEFINE(WITH_MATRIXORBITAL,1,[MatrixOrbital driver]) fi if test "$MATRIXORBITALGX" = "yes"; then if test "$has_usb" = "true"; then GRAPHIC="yes" SERIAL="yes" LIBUSB="yes" DRIVERS="$DRIVERS drv_MatrixOrbitalGX.o" AC_DEFINE(WITH_MATRIXORBITALGX,1,[MatrixOrbitalGX driver]) else AC_MSG_WARN(usb.h not found: MatrixOrbitalGX driver disabled) fi fi if test "$MILINST" = "yes"; then TEXT="yes" SERIAL="yes" DRIVERS="$DRIVERS drv_MilfordInstruments.o" AC_DEFINE(WITH_MILINST,1,[Milford Instruments driver]) fi if test "$NORITAKE" = "yes"; then if test "$has_parport" = "true"; then TEXT="yes" GRAPHIC="yes" PARPORT="yes" DRIVERS="$DRIVERS drv_Noritake.o" AC_DEFINE(WITH_NORITAKE,1,[Noritake driver]) else AC_MSG_WARN(asm/io.h or {linux/parport.h and linux/ppdev.h} not found: NORITAKE driver disabled) fi fi if test "$NULL" = "yes"; then TEXT="yes" DRIVERS="$DRIVERS drv_NULL.o" AC_DEFINE(WITH_NULL,1,[NULL driver]) fi if test "$PERTELIAN" = "yes"; then # select either text or graphics mode TEXT="yes" # select bus: serial (including USB), parallel or i2c SERIAL="yes" DRIVERS="$DRIVERS drv_Pertelian.o" AC_DEFINE(WITH_PERTELIAN,1,[Pertelian driver]) fi if test "$PHANDERSON" = "yes"; then TEXT="yes" SERIAL="yes" DRIVERS="$DRIVERS drv_PHAnderson.o" AC_DEFINE(WITH_PHANDERSON,1,[PHAnderson driver]) fi if test "$PICGRAPHIC" = "yes"; then GRAPHIC="yes" GPIO="yes" SERIAL="yes" DRIVERS="$DRIVERS drv_PICGraphic.o" AC_DEFINE(WITH_PICGRAPHIC,1,[PICGraphic driver]) fi if test "$PICOLCD" = "yes"; then if test "$has_usb" = "true"; then TEXT="yes" GPIO="yes" SERIAL="yes" LIBUSB="yes" DRIVERS="$DRIVERS drv_picoLCD.o" AC_DEFINE(WITH_PICOLCD,1,[picoLCD driver]) else AC_MSG_WARN(usb.h not found: picoLCD driver disabled) fi fi if test "$PICOLCDGRAPHIC" = "yes"; then if test "$has_usb" = "true"; then TEXT="yes" GRAPHIC="yes" GPIO="yes" SERIAL="yes" LIBUSB="yes" DRIVERS="$DRIVERS drv_picoLCDGraphic.o" AC_DEFINE(WITH_PICOLCDGRAPHIC,1,[picoLCDGraphic driver]) else AC_MSG_WARN(usb.h not found: picoLCDGraphic driver disabled) fi fi if test "$PNG" = "yes"; then if test "$has_gd" = "true"; then IMAGE="yes" AC_DEFINE(WITH_PNG,1,[PNG driver]) else AC_MSG_WARN(gd.h not found: PNG driver disabled) fi fi if test "$PPM" = "yes"; then IMAGE="yes" AC_DEFINE(WITH_PPM,1,[PPM driver]) fi if test "$ROUTERBOARD" = "yes"; then if test "$has_io_h" = "true"; then TEXT="yes" GPIO="yes" DRIVERS="$DRIVERS drv_RouterBoard.o" AC_DEFINE(WITH_ROUTERBOARD,1,[RouterBoard driver]) else AC_MSG_WARN(sys/io.h not found: RouterBoard driver disabled) fi fi if test "$SAMPLE" = "yes"; then if test "$has_parport" = "true"; then # select either text or graphics mode TEXT="yes" GRAPHIC="yes" # support for GPIO's GPIO="yes" # select bus: serial (including USB), parallel or i2c SERIAL="yes" PARPORT="yes" #I2C="yes" DRIVERS="$DRIVERS drv_Sample.o" AC_DEFINE(WITH_SAMPLE,1,[Sample driver]) else AC_MSG_WARN(asm/io.h or {linux/parport.h and linux/ppdev.h} not found: SAMPLE driver disabled) fi fi if test "$SERDISPLIB" = "yes"; then if test "$has_serdisplib" = "true"; then GRAPHIC="yes" DRIVERS="$DRIVERS drv_serdisplib.o" DRVLIBS="$DRVLIBS -L/usr/local/lib -lserdisp" AC_DEFINE(WITH_SERDISPLIB,1,[serdisplib driver]) if test "$has_usb" = "true"; then LIBUSB="yes" fi else AC_MSG_WARN(serdisp.h not found: serdisplib driver disabled) fi fi if test "$SHUTTLEVFD" = "yes"; then if test "$has_usb" = "true"; then TEXT="yes" GPIO="yes" DRIVERS="$DRIVERS drv_ShuttleVFD.o" LIBUSB="yes" AC_DEFINE(WITH_SHUTTLEVFD,1,[ShuttleVFD driver]) else AC_MSG_WARN(usb.h not found: ShuttleVFD driver disabled) fi fi if test "$SIMPLELCD" = "yes"; then TEXT="yes" SERIAL="yes" DRIVERS="$DRIVERS drv_SimpleLCD.o" AC_DEFINE(WITH_SIMPLELCD,1,[SimpleLCD driver]) fi if test "$ST2205" = "yes"; then if test "$has_st2205" = "true"; then GRAPHIC="yes" DRIVERS="$DRIVERS drv_st2205.o" DRVLIBS="$DRVLIBS -L/usr/local/lib -lst2205" AC_DEFINE(WITH_ST2205,1,[st2205 driver]) else AC_MSG_WARN(st2205.h not found: st2205 driver disabled) fi fi if test "$T6963" = "yes"; then if test "$has_parport" = "true"; then GRAPHIC="yes" PARPORT="yes" DRIVERS="$DRIVERS drv_T6963.o" AC_DEFINE(WITH_T6963,1,[T6963 driver]) else AC_MSG_WARN(asm/io.h or {linux/parport.h and linux/ppdev.h} not found: T6963 driver disabled) fi fi if test "$Trefon" = "yes"; then if test "$has_usb" = "true"; then TEXT="yes" DRIVERS="$DRIVERS drv_Trefon.o" LIBUSB="yes" AC_DEFINE(WITH_TREFON,1,[TREFON driver]) else AC_MSG_WARN(usb.h not found: Trefon driver disabled) fi fi if test "$ULA200" = "yes"; then if test "$has_ftdi" = "true"; then TEXT="yes" LIBUSB="yes" LIBFTDI="yes" DRIVERS="$DRIVERS drv_ula200.o" AC_DEFINE(WITH_ULA200,1,[ULA200 driver]) else AC_MSG_WARN(ftdi.h not found: ULA200 driver disabled) fi fi if test "$USBHUB" = "yes"; then if test "$has_usb" = "true"; then GPIO="yes" DRIVERS="$DRIVERS drv_USBHUB.o" LIBUSB="yes" AC_DEFINE(WITH_USBHUB,1,[USBHUB driver]) else AC_MSG_WARN(usb.h not found: USB-Hub driver disabled) fi fi if test "$USBLCD" = "yes"; then TEXT="yes" SERIAL="yes" DRIVERS="$DRIVERS drv_USBLCD.o" if test "$has_usb" = "true"; then LIBUSB="yes" fi AC_DEFINE(WITH_USBLCD,1,[USBLCD driver]) fi if test "$VNC" = "yes"; then if test "$has_vncserverlib" = "true"; then GRAPHIC="yes" KEYPAD="yes" DRIVERS="$DRIVERS drv_vnc.o" DRVLIBS="$DRVLIBS -L/usr/local/lib -lvncserver -lz" AC_DEFINE(WITH_VNC,1,[vnc driver]) else AC_MSG_WARN(libvncserver not found: vnc driver disabled) fi fi if test "$WINCORNIXDORF" = "yes"; then TEXT="yes" SERIAL="yes" DRIVERS="$DRIVERS drv_WincorNixdorf.o" AC_DEFINE(WITH_WINCORNIXDORF,1,[WincorNixdorf driver]) fi if test "$X11" = "yes"; then if test "$no_x" = "yes"; then AC_MSG_WARN(X11 headers or libraries not available: X11 driver disabled) else GRAPHIC="yes" KEYPAD="yes" DRIVERS="$DRIVERS drv_X11.o" if test "x$ac_x_libraries" = "x"; then DRVLIBS="$DRVLIBS -lX11" else DRVLIBS="$DRVLIBS -L$ac_x_libraries -lX11" fi CPP_FLAGS="$CPPFLAGS $X_CFLAGS" AC_DEFINE(WITH_X11, 1, [X11 driver]) fi fi # Image driver if test "$IMAGE" = "yes"; then GRAPHIC="yes" DRIVERS="$DRIVERS drv_Image.o" fi if test "$DRIVERS" = ""; then AC_MSG_ERROR([You should activate at least one driver...]) fi # generic text driver if test "$TEXT" = "yes"; then DRIVERS="$DRIVERS drv_generic_text.o" fi # generic graphic driver if test "$GRAPHIC" = "yes"; then DRIVERS="$DRIVERS drv_generic_graphic.o" if test "$has_gd" = "true"; then DRIVERS="$DRIVERS widget_image.o" DRVLIBS="$DRVLIBS -lgd" AC_DEFINE(WITH_GD, 1, [GD library]) AC_DEFINE(WITH_IMAGE, 1, [image widget]) fi fi # generic GPIO driver if test "$GPIO" = "yes"; then DRIVERS="$DRIVERS drv_generic_gpio.o" fi # generic parport driver if test "$PARPORT" = "yes"; then DRIVERS="$DRIVERS drv_generic_parport.o" AC_DEFINE(WITH_PARPORT, 1, [parport bus driver]) fi # generic serial driver if test "$SERIAL" = "yes"; then DRIVERS="$DRIVERS drv_generic_serial.o" AC_DEFINE(WITH_SERIAL, 1, [serial bus driver]) fi # generic i2c driver if test "$I2C" = "yes"; then DRIVERS="$DRIVERS drv_generic_i2c.o" AC_DEFINE(WITH_I2C, 1, [I2C bus driver]) fi # generic keypad driver if test "$KEYPAD" = "yes"; then DRIVERS="$DRIVERS drv_generic_keypad.o" fi # libusb if test "$LIBUSB" = "yes"; then DRVLIBS="$DRVLIBS -lusb" fi # libftdi if test "$LIBFTDI" = "yes"; then DRVLIBS="$DRVLIBS -lftdi" fi if test "$DRIVERS" = ""; then AC_MSG_ERROR([You should include at least one driver...]) fi AC_SUBST(DRIVERS) AC_SUBST(DRVLIBS) 0 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
#include "../stdafx.h"
#include "phidget_jni.h"
#include "com_phidgets_IRPhidget.h"
#include "../cphidgetir.h"

EVENT_VARS(code, Code)
EVENT_VARS(learn, Learn)
EVENT_VARS(rawData, RawData)

static jclass irCode_class; //class
static jmethodID irCode_cons; //constructor
static jmethodID irCode_getData;
static jclass irCodeInfo_class; //class
static jmethodID irCodeInfo_cons; //constructor
static jmethodID irCodeInfo_getEncoding;
static jmethodID irCodeInfo_getLength;
static jmethodID irCodeInfo_getBitCount;
static jmethodID irCodeInfo_getGap;
static jmethodID irCodeInfo_getTrail;
static jmethodID irCodeInfo_getMinRepeat;
static jmethodID irCodeInfo_getCarrierFrequency;
static jmethodID irCodeInfo_getDutyCycle;
static jmethodID irCodeInfo_getHeader;
static jmethodID irCodeInfo_getZero;
static jmethodID irCodeInfo_getOne;
static jmethodID irCodeInfo_getRepeat;
static jmethodID irCodeInfo_getToggleMask;
static jclass irLearn_class; //class
static jmethodID irLearn_cons; //constructor

JNI_LOAD(ir, IR)
	EVENT_VAR_SETUP(ir, learn, Learn, Lcom/phidgets/IRLearnedCode;, V)
	EVENT_VAR_SETUP(ir, rawData, RawData, [I, V)
	EVENT_VAR_SETUP(ir, code, Code, Lcom/phidgets/IRCode;Z, V)

	if (!(irCode_class = (*env)->FindClass(env,"com/phidgets/IRCode")))
		JNI_ABORT_STDERR("Couldn't FindClass com/phidgets/IRCode");
	if (!(irCode_class = (jclass)(*env)->NewGlobalRef(env, irCode_class)))
		JNI_ABORT_STDERR("Couldn't create global ref irCode_class");
	if (!(irCode_cons = (*env)->GetMethodID(env, irCode_class, "<init>", "([SI)V")))
		JNI_ABORT_STDERR("Couldn't get method ID <init> from irCode_class");

	if (!(irCode_getData = (*env)->GetMethodID(env, irCode_class, "getData", "()[S")))
		JNI_ABORT_STDERR("Couldn't get method ID getData from irCode_class");

	if (!(irCodeInfo_class = (*env)->FindClass(env,"com/phidgets/IRCodeInfo")))
		JNI_ABORT_STDERR("Couldn't FindClass com/phidgets/IRCodeInfo");
	if (!(irCodeInfo_class = (jclass)(*env)->NewGlobalRef(env, irCodeInfo_class)))
		JNI_ABORT_STDERR("Couldn't create global ref irCodeInfo_class");
	if (!(irCodeInfo_cons = (*env)->GetMethodID(env, irCodeInfo_class, "<init>", "(II[I[I[III[IILcom/phidgets/IRCode;III)V")))
		JNI_ABORT_STDERR("Couldn't get method ID <init> from irCodeInfo_class");

	if (!(irCodeInfo_getEncoding = (*env)->GetMethodID(env, irCodeInfo_class, "getEncoding", "()I")))
		JNI_ABORT_STDERR("Couldn't get method ID getEncoding from irCodeInfo_class");
	if (!(irCodeInfo_getLength = (*env)->GetMethodID(env, irCodeInfo_class, "getLength", "()I")))
		JNI_ABORT_STDERR("Couldn't get method ID getLength from irCodeInfo_class");
	if (!(irCodeInfo_getBitCount = (*env)->GetMethodID(env, irCodeInfo_class, "getBitCount", "()I")))
		JNI_ABORT_STDERR("Couldn't get method ID getBitCount from irCodeInfo_class");
	if (!(irCodeInfo_getGap = (*env)->GetMethodID(env, irCodeInfo_class, "getGap", "()I")))
		JNI_ABORT_STDERR("Couldn't get method ID getGap from irCodeInfo_class");
	if (!(irCodeInfo_getTrail = (*env)->GetMethodID(env, irCodeInfo_class, "getTrail", "()I")))
		JNI_ABORT_STDERR("Couldn't get method ID getTrail from irCodeInfo_class");
	if (!(irCodeInfo_getMinRepeat = (*env)->GetMethodID(env, irCodeInfo_class, "getMinRepeat", "()I")))
		JNI_ABORT_STDERR("Couldn't get method ID getMinRepeat from irCodeInfo_class");
	if (!(irCodeInfo_getCarrierFrequency = (*env)->GetMethodID(env, irCodeInfo_class, "getCarrierFrequency", "()I")))
		JNI_ABORT_STDERR("Couldn't get method ID getCarrierFrequency from irCodeInfo_class");
	if (!(irCodeInfo_getDutyCycle = (*env)->GetMethodID(env, irCodeInfo_class, "getDutyCycle", "()I")))
		JNI_ABORT_STDERR("Couldn't get method ID getDutyCycle from irCodeInfo_class");
	if (!(irCodeInfo_getHeader = (*env)->GetMethodID(env, irCodeInfo_class, "getHeader", "()[I")))
		JNI_ABORT_STDERR("Couldn't get method ID getHeader from irCodeInfo_class");
	if (!(irCodeInfo_getZero = (*env)->GetMethodID(env, irCodeInfo_class, "getZero", "()[I")))
		JNI_ABORT_STDERR("Couldn't get method ID getZero from irCodeInfo_class");
	if (!(irCodeInfo_getOne = (*env)->GetMethodID(env, irCodeInfo_class, "getOne", "()[I")))
		JNI_ABORT_STDERR("Couldn't get method ID getOne from irCodeInfo_class");
	if (!(irCodeInfo_getRepeat = (*env)->GetMethodID(env, irCodeInfo_class, "getRepeat", "()[I")))
		JNI_ABORT_STDERR("Couldn't get method ID getRepeat from irCodeInfo_class");
	if (!(irCodeInfo_getToggleMask = (*env)->GetMethodID(env, irCodeInfo_class, "getToggleMask", "()Lcom/phidgets/IRCode;")))
		JNI_ABORT_STDERR("Couldn't get method ID getToggleMask from irCodeInfo_class");

	if (!(irLearn_class = (*env)->FindClass(env,"com/phidgets/IRLearnedCode")))
		JNI_ABORT_STDERR("Couldn't FindClass com/phidgets/IRLearnedCode");
	if (!(irLearn_class = (jclass)(*env)->NewGlobalRef(env, irLearn_class)))
		JNI_ABORT_STDERR("Couldn't create global ref irLearn_class");
	if (!(irLearn_cons = (*env)->GetMethodID(env, irLearn_class, "<init>", "(Lcom/phidgets/IRCode;Lcom/phidgets/IRCodeInfo;)V")))
		JNI_ABORT_STDERR("Couldn't get method ID <init> from irLearn_class");
}

//Code event
static int CCONV code_handler(CPhidgetIRHandle h, void *arg, unsigned char *data, int dataLength, int bitCount, int repeat);
JNIEXPORT void JNICALL
Java_com_phidgets_IRPhidget_enableCodeEvents(JNIEnv *env, jobject obj, jboolean b)
{
	jlong gr = updateGlobalRef(env, obj, nativeCodeHandler_fid, b);
	CPhidgetIRHandle h = (CPhidgetIRHandle)(uintptr_t)(*env)->GetLongField(env, obj, handle_fid);
	CPhidgetIR_set_OnCode_Handler(h, b ? code_handler : 0, (void *)(uintptr_t)gr);
}
static int CCONV
code_handler(CPhidgetIRHandle h, void *arg, unsigned char *data, int dataLength, int bitCount, int repeat)
{
	JNIEnv *env;
	jobject obj;
	jobject codeEv;
	jobject ircode;
	jshortArray js;
	jshort *datas;
	int i;

	if ((*ph_vm)->AttachCurrentThread(ph_vm, (JNIEnvPtr)&env, NULL))
		JNI_ABORT_STDERR("Couldn't AttachCurrentThread");

	obj = (jobject)arg;

	//create and fill in short array
	js = (*env)->NewShortArray(env, dataLength);
	if (!js)
		return -1;
	datas = (*env)->GetShortArrayElements(env, js, 0);
	if (!datas)
		return -1;
	for (i=0; i<dataLength; i++)
		datas[i] = (jshort)data[i];
	(*env)->ReleaseShortArrayElements(env, js, datas, 0);

	if (!(ircode = (*env)->NewObject(env, irCode_class, irCode_cons, js, bitCount)))
	{
		return -1;
	}

	if (!(codeEv = (*env)->NewObject(env, codeEvent_class,
	    codeEvent_cons, obj, ircode, repeat)))
	{
		(*env)->DeleteLocalRef(env, ircode);
		return -1;
	}

	(*env)->CallVoidMethod(env, obj, fireCode_mid, codeEv);

	(*env)->DeleteLocalRef(env, codeEv);
	(*env)->DeleteLocalRef(env, ircode);

	(*ph_vm)->DetachCurrentThread(ph_vm);

	return 0;

}

//Learn event
static int CCONV learn_handler(CPhidgetIRHandle h, void *arg, unsigned char *data, int dataLength, CPhidgetIR_CodeInfoHandle codeInfo);
JNIEXPORT void JNICALL
Java_com_phidgets_IRPhidget_enableLearnEvents(JNIEnv *env, jobject obj, jboolean b)
{
	jlong gr = updateGlobalRef(env, obj, nativeLearnHandler_fid, b);
	CPhidgetIRHandle h = (CPhidgetIRHandle)(uintptr_t)(*env)->GetLongField(env, obj, handle_fid);
	CPhidgetIR_set_OnLearn_Handler(h, b ? learn_handler : 0, (void *)(uintptr_t)gr);
}
static int CCONV
learn_handler(CPhidgetIRHandle h, void *arg, unsigned char *data, int dataLength, CPhidgetIR_CodeInfoHandle codeInfo)
{
	JNIEnv *env;
	jobject obj;
	jobject learnEv;
	jobject ircode, ircodeinfo, irlearn, togglemask;
	jshortArray codeData;
	jintArray headerArray, zeroArray, oneArray, repeatArray;
	jshort *datas;
	jint *datai;
	int i,j;

	if ((*ph_vm)->AttachCurrentThread(ph_vm, (JNIEnvPtr)&env, NULL))
		JNI_ABORT_STDERR("Couldn't AttachCurrentThread");

	obj = (jobject)arg;

	//create and fill in short array
	if(dataLength > 0){
	codeData = (*env)->NewShortArray(env, dataLength);
	if (!codeData)
		return -1;
	datas = (*env)->GetShortArrayElements(env, codeData, 0);
	if (!datas)
		return -1;
	for (i=0; i<dataLength; i++)
		datas[i] = (jshort)data[i];
	(*env)->ReleaseShortArrayElements(env, codeData, datas, 0);
	}
	else
		codeData=NULL;
	//create IRCode object
	if (!(ircode = (*env)->NewObject(env, irCode_class, irCode_cons, codeData, codeInfo->bitCount)))
	{
		return -1;
	}

	//Header
	if(codeInfo->header[0])
	{
		headerArray = (*env)->NewIntArray(env, 2);
		if (!headerArray)
			return -1;
		datai = (*env)->GetIntArrayElements(env, headerArray, 0);
		if (!datai)
			return -1;
		for (i=0; i<2; i++)
			datai[i] = (jint)codeInfo->header[i];
		(*env)->ReleaseIntArrayElements(env, headerArray, datai, 0);
	}
	else
		headerArray=NULL;

	//Zero
	zeroArray = (*env)->NewIntArray(env, 2);
	if (!zeroArray)
		return -1;
	datai = (*env)->GetIntArrayElements(env, zeroArray, 0);
	if (!datai)
		return -1;
	for (i=0; i<2; i++)
		datai[i] = (jint)codeInfo->zero[i];
	(*env)->ReleaseIntArrayElements(env, zeroArray, datai, 0);

	//One
	oneArray = (*env)->NewIntArray(env, 2);
	if (!oneArray)
		return -1;
	datai = (*env)->GetIntArrayElements(env, oneArray, 0);
	if (!datai)
		return -1;
	for (i=0; i<2; i++)
		datai[i] = (jint)codeInfo->one[i];
	(*env)->ReleaseIntArrayElements(env, oneArray, datai, 0);

	//Repeat
	i=0;
	while(codeInfo->repeat[i])
		i++;
	if(i)
	{
		repeatArray = (*env)->NewIntArray(env, i);
		if (!repeatArray)
			return -1;
		datai = (*env)->GetIntArrayElements(env, repeatArray, 0);
		if (!datai)
			return -1;
		for (j=0; j<i; j++)
			datai[j] = (jint)codeInfo->repeat[j];
		(*env)->ReleaseIntArrayElements(env, repeatArray, datai, 0);
	}
	else{
		repeatArray = NULL;
	}

	//ToggleMask
	if(dataLength > 0){
		codeData = (*env)->NewShortArray(env, dataLength);
		if (!codeData)
			return -1;
		datas = (*env)->GetShortArrayElements(env, codeData, 0);
		if (!datas)
			return -1;
		for (i=0; i<dataLength; i++)
			datas[i] = (jshort)codeInfo->toggle_mask[i];
		(*env)->ReleaseShortArrayElements(env, codeData, datas, 0);
		if (!(togglemask = (*env)->NewObject(env, irCode_class, irCode_cons, codeData, codeInfo->bitCount)))
		{
			return -1;
		}
	}
	else{
		codeData=NULL;
	}
	//create IRCodeInfo object
	if (!(ircodeinfo = (*env)->NewObject(env, irCodeInfo_class, irCodeInfo_cons, 
		codeInfo->encoding, codeInfo->bitCount, headerArray, zeroArray, oneArray,
		codeInfo->trail, codeInfo->gap, repeatArray, codeInfo->min_repeat, togglemask,
		codeInfo->length, codeInfo->carrierFrequency, codeInfo->dutyCycle)))
	{
		(*env)->DeleteLocalRef(env, ircode);
		return -1;
	}

	//create IRLearn object
	if (!(irlearn = (*env)->NewObject(env, irLearn_class, irLearn_cons, ircode, ircodeinfo)))
	{
		(*env)->DeleteLocalRef(env, ircode);
		(*env)->DeleteLocalRef(env, ircodeinfo);
		return -1;
	}

	if (!(learnEv = (*env)->NewObject(env, learnEvent_class,
	    learnEvent_cons, obj, irlearn)))
	{
		(*env)->DeleteLocalRef(env, ircode);
		(*env)->DeleteLocalRef(env, ircodeinfo);
		(*env)->DeleteLocalRef(env, irlearn);
		return -1;
	}

	(*env)->CallVoidMethod(env, obj, fireLearn_mid, learnEv);

	(*env)->DeleteLocalRef(env, learnEv);
	(*env)->DeleteLocalRef(env, ircode);
	(*env)->DeleteLocalRef(env, ircodeinfo);
	(*env)->DeleteLocalRef(env, irlearn);

	(*ph_vm)->DetachCurrentThread(ph_vm);

	return 0;
}

//RawData event
static int CCONV rawData_handler(CPhidgetIRHandle h, void *arg, int *data, int dataLength);
JNIEXPORT void JNICALL
Java_com_phidgets_IRPhidget_enableRawDataEvents(JNIEnv *env, jobject obj, jboolean b)
{
	jlong gr = updateGlobalRef(env, obj, nativeRawDataHandler_fid, b);
	CPhidgetIRHandle h = (CPhidgetIRHandle)(uintptr_t)(*env)->GetLongField(env, obj, handle_fid);
	CPhidgetIR_set_OnRawData_Handler(h, b ? rawData_handler : 0, (void *)(uintptr_t)gr);
}
static int CCONV
rawData_handler(CPhidgetIRHandle h, void *arg, int *data, int dataLength)
{
	JNIEnv *env;
	jobject obj;
	jobject rawDataEv;
	jintArray js;
	jint *datas;
	int i;

	if ((*ph_vm)->AttachCurrentThread(ph_vm, (JNIEnvPtr)&env, NULL))
		JNI_ABORT_STDERR("Couldn't AttachCurrentThread");

	obj = (jobject)arg;

	//create and fill in int array
	js = (*env)->NewIntArray(env, dataLength);
	if (!js)
		return -1;
	datas = (*env)->GetIntArrayElements(env, js, 0);
	if (!datas)
		return -1;
	for (i=0; i<dataLength; i++)
		datas[i] = (jint)data[i];
	(*env)->ReleaseIntArrayElements(env, js, datas, 0);

	if (!(rawDataEv = (*env)->NewObject(env, rawDataEvent_class, rawDataEvent_cons, obj, js)))
		return -1;

	(*env)->CallVoidMethod(env, obj, fireRawData_mid, rawDataEv);

	(*env)->DeleteLocalRef(env, rawDataEv);

	(*ph_vm)->DetachCurrentThread(ph_vm);

	return 0;
}

JNI_CREATE(IR)

JNIEXPORT jobject JNICALL
Java_com_phidgets_IRPhidget_getLastCode(JNIEnv *env, jobject obj)
{
	CPhidgetIRHandle h = (CPhidgetIRHandle)(uintptr_t)(*env)->GetLongField( env, obj, handle_fid);
	int error;
	unsigned char data[IR_MAX_CODE_DATA_LENGTH];
	int dataLength=IR_MAX_CODE_DATA_LENGTH, bitCount, i;
	jobject ircode;
	jshortArray js;
	jshort *datas;

	if ((error = CPhidgetIR_getLastCode(h, data, &dataLength, &bitCount)))
	{
		PH_THROW(error);
		return NULL;
	}

	//create and fill in short array
	js = (*env)->NewShortArray(env, dataLength);
	if (!js)
	{
		PH_THROW(EPHIDGET_UNEXPECTED);
		return NULL;
	}
	datas = (*env)->GetShortArrayElements(env, js, 0);
	if (!datas)
	{
		PH_THROW(EPHIDGET_UNEXPECTED);
		return NULL;
	}
	for (i=0; i<dataLength; i++)
		datas[i] = (jshort)data[i];
	(*env)->ReleaseShortArrayElements(env, js, datas, 0);

	//create and return IRCode object
	if (!(ircode = (*env)->NewObject(env, irCode_class, irCode_cons, js, bitCount)))
	{
		PH_THROW(EPHIDGET_UNEXPECTED);
		return NULL;
	}

	return ircode;
}

JNIEXPORT jint JNICALL
Java_com_phidgets_IRPhidget_readRaw(JNIEnv *env, jobject obj, jintArray intArray, jint offset, jint count)
{
	CPhidgetIRHandle h = (CPhidgetIRHandle)(uintptr_t)(*env)->GetLongField( env, obj, handle_fid);
	int error, dataLength = count;
	jint *datai;

	//create and fill in int array
	datai = (*env)->GetIntArrayElements(env, intArray, 0);
	if (!datai)
	{
		PH_THROW(EPHIDGET_UNEXPECTED);
		return -1;
	}
	if ((error = CPhidgetIR_getRawData(h, datai, &dataLength)))
	{
		(*env)->ReleaseIntArrayElements(env, intArray, datai, 0);
		PH_THROW(error);
		return -1;
	}
	(*env)->ReleaseIntArrayElements(env, intArray, datai, 0);

	return dataLength;
}

JNIEXPORT jobject JNICALL
Java_com_phidgets_IRPhidget_getLastLearnedCode(JNIEnv *env, jobject obj)
{
	CPhidgetIRHandle h = (CPhidgetIRHandle)(uintptr_t)(*env)->GetLongField( env, obj, handle_fid);
	int error;
	unsigned char data[IR_MAX_CODE_DATA_LENGTH];
	int dataLength=IR_MAX_CODE_DATA_LENGTH;
	CPhidgetIR_CodeInfo codeInfoReal;
	CPhidgetIR_CodeInfoHandle codeInfo = &codeInfoReal;
	jobject ircode, ircodeinfo, irlearn, togglemask;
	jshortArray codeData;
	jintArray headerArray, zeroArray, oneArray, repeatArray;
	jshort *datas;
	jint *datai;
	int i,j;

	if ((error = CPhidgetIR_getLastLearnedCode(h, data, &dataLength, codeInfo)))
	{
		PH_THROW(error);
		return NULL;
	}

	//create and fill in short array
	if(dataLength > 0){
	codeData = (*env)->NewShortArray(env, dataLength);
	if (!codeData)
	{
		PH_THROW(EPHIDGET_UNEXPECTED);
		return NULL;
	}
	datas = (*env)->GetShortArrayElements(env, codeData, 0);
	if (!datas)
	{
		PH_THROW(EPHIDGET_UNEXPECTED);
		return NULL;
	}
	for (i=0; i<dataLength; i++)
		datas[i] = (jshort)data[i];
	(*env)->ReleaseShortArrayElements(env, codeData, datas, 0);
	}
	else
		codeData=NULL;
	//create IRCode object
	if (!(ircode = (*env)->NewObject(env, irCode_class, irCode_cons, codeData, codeInfo->bitCount)))
	{
		PH_THROW(EPHIDGET_UNEXPECTED);
		return NULL;
	}

	//Header
	if(codeInfo->header[0])
	{
		headerArray = (*env)->NewIntArray(env, 2);
		if (!headerArray)
		{
			PH_THROW(EPHIDGET_UNEXPECTED);
			return NULL;
		}
		datai = (*env)->GetIntArrayElements(env, headerArray, 0);
		if (!datai)
		{
			PH_THROW(EPHIDGET_UNEXPECTED);
			return NULL;
		}
		for (i=0; i<2; i++)
			datai[i] = (jint)codeInfo->header[i];
		(*env)->ReleaseIntArrayElements(env, headerArray, datai, 0);
	}
	else
		headerArray=NULL;

	//Zero
	zeroArray = (*env)->NewIntArray(env, 2);
	if (!zeroArray)
	{
		PH_THROW(EPHIDGET_UNEXPECTED);
		return NULL;
	}
	datai = (*env)->GetIntArrayElements(env, zeroArray, 0);
	if (!datai)
	{
		PH_THROW(EPHIDGET_UNEXPECTED);
		return NULL;
	}
	for (i=0; i<2; i++)
		datai[i] = (jint)codeInfo->zero[i];
	(*env)->ReleaseIntArrayElements(env, zeroArray, datai, 0);

	//One
	oneArray = (*env)->NewIntArray(env, 2);
	if (!oneArray)
	{
		PH_THROW(EPHIDGET_UNEXPECTED);
		return NULL;
	}
	datai = (*env)->GetIntArrayElements(env, oneArray, 0);
	if (!datai)
	{
		PH_THROW(EPHIDGET_UNEXPECTED);
		return NULL;
	}
	for (i=0; i<2; i++)
		datai[i] = (jint)codeInfo->one[i];
	(*env)->ReleaseIntArrayElements(env, oneArray, datai, 0);

	//Repeat
	i=0;
	while(codeInfo->repeat[i])
		i++;
	if(i)
	{
		repeatArray = (*env)->NewIntArray(env, i);
		if (!repeatArray)
		{
			PH_THROW(EPHIDGET_UNEXPECTED);
			return NULL;
		}
		datai = (*env)->GetIntArrayElements(env, repeatArray, 0);
		if (!datai)
		{
			PH_THROW(EPHIDGET_UNEXPECTED);
			return NULL;
		}
		for (j=0; j<i; j++)
			datai[j] = (jint)codeInfo->repeat[j];
		(*env)->ReleaseIntArrayElements(env, repeatArray, datai, 0);
	}
	else{ 
		repeatArray=NULL;
	}

	//ToggleMask
	
	if(dataLength > 0){
		codeData = (*env)->NewShortArray(env, dataLength);
		if (!codeData)
		{
			PH_THROW(EPHIDGET_UNEXPECTED);
			return NULL;
		}
		datas = (*env)->GetShortArrayElements(env, codeData, 0);
		if (!datas)
		{
			PH_THROW(EPHIDGET_UNEXPECTED);
			return NULL;
		}
		for (i=0; i<dataLength; i++){

			datas[i] = (jshort)codeInfo->toggle_mask[i];
		}
		(*env)->ReleaseShortArrayElements(env, codeData, datas, 0);
		}
	else{ 
		codeData=NULL;
	}
	
	if (!(togglemask = (*env)->NewObject(env, irCode_class, irCode_cons, codeData, codeInfo->bitCount)))
	{
		PH_THROW(EPHIDGET_UNEXPECTED);
		return NULL;
	}

	//create IRCodeInfo object 
	if (!(ircodeinfo = (*env)->NewObject(env, irCodeInfo_class, irCodeInfo_cons, 
		codeInfo->encoding, codeInfo->bitCount, headerArray, zeroArray, oneArray,
		codeInfo->trail, codeInfo->gap, repeatArray, codeInfo->min_repeat, togglemask,   
		codeInfo->length, codeInfo->carrierFrequency, codeInfo->dutyCycle)))
	{
		(*env)->DeleteLocalRef(env, ircode);
		PH_THROW(EPHIDGET_UNEXPECTED);
		return NULL;
	}

	//create IRLearn object
	if (!(irlearn = (*env)->NewObject(env, irLearn_class, irLearn_cons, ircode, ircodeinfo)))
	{
		(*env)->DeleteLocalRef(env, ircode);
		(*env)->DeleteLocalRef(env, ircodeinfo);
		PH_THROW(EPHIDGET_UNEXPECTED);
		return NULL;
	}

	return irlearn;
}

JNIEXPORT void JNICALL
Java_com_phidgets_IRPhidget_transmit(JNIEnv *env, jobject obj, jobject code, jobject irCodeInfo)
{
	CPhidgetIRHandle h = (CPhidgetIRHandle)(uintptr_t)(*env)->GetLongField( env, obj, handle_fid);
	jshortArray codeData;
	jintArray headerArray, zeroArray, oneArray, repeatArray;
	jobject togglemask;
	jshort *datas;
	jint *datai;
	unsigned char data[IR_MAX_CODE_DATA_LENGTH];
	jsize len;
	CPhidgetIR_CodeInfo codeInfoReal = {0};
	CPhidgetIR_CodeInfoHandle codeInfo = &codeInfoReal;
	int i;
	int error;

	//read in code
	if (!(codeData = (*env)->CallObjectMethod(env, code, irCode_getData)))
	{
		PH_THROW(EPHIDGET_UNEXPECTED);
		return;
	}
	datas = (*env)->GetShortArrayElements(env, codeData, 0);
	if (!datas)
	{
		PH_THROW(EPHIDGET_UNEXPECTED);
		return;
	}
	len = (*env)->GetArrayLength(env, codeData);
	for(i=0;i<len;i++)
	{
		data[i] = (unsigned char)datas[i];
	}
	(*env)->ReleaseShortArrayElements(env, codeData, datas, 0);

	//read in codeInfo
	//ints
	codeInfo->encoding = (*env)->CallIntMethod(env, irCodeInfo, irCodeInfo_getEncoding);
	codeInfo->length = (*env)->CallIntMethod(env, irCodeInfo, irCodeInfo_getLength);
	codeInfo->bitCount = (*env)->CallIntMethod(env, irCodeInfo, irCodeInfo_getBitCount);
	codeInfo->gap = (*env)->CallIntMethod(env, irCodeInfo, irCodeInfo_getGap);
	codeInfo->trail = (*env)->CallIntMethod(env, irCodeInfo, irCodeInfo_getTrail);
	codeInfo->min_repeat = (*env)->CallIntMethod(env, irCodeInfo, irCodeInfo_getMinRepeat);
	codeInfo->carrierFrequency = (*env)->CallIntMethod(env, irCodeInfo, irCodeInfo_getCarrierFrequency);
	codeInfo->dutyCycle = (*env)->CallIntMethod(env, irCodeInfo, irCodeInfo_getDutyCycle);
	//arrays/objects
	//Header
	headerArray = (*env)->CallObjectMethod(env, irCodeInfo, irCodeInfo_getHeader);
	if(headerArray)
	{
		datai = (*env)->GetIntArrayElements(env, headerArray, 0);
		if (!datai)
		{
			PH_THROW(EPHIDGET_UNEXPECTED);
			return;
		}
		for (i=0; i<2; i++)
			codeInfo->header[i] = datai[i];
		(*env)->ReleaseIntArrayElements(env, headerArray, datai, 0);
	}
	else
		headerArray=NULL;

	//Zero
	zeroArray = (*env)->CallObjectMethod(env, irCodeInfo, irCodeInfo_getZero);
	datai = (*env)->GetIntArrayElements(env, zeroArray, 0);
	if (!datai)
	{
		PH_THROW(EPHIDGET_UNEXPECTED);
		return;
	}
	for (i=0; i<2; i++)
		codeInfo->zero[i] = datai[i];
	(*env)->ReleaseIntArrayElements(env, zeroArray, datai, 0);

	//One
	oneArray = (*env)->CallObjectMethod(env, irCodeInfo, irCodeInfo_getOne);
	datai = (*env)->GetIntArrayElements(env, oneArray, 0);
	if (!datai)
	{
		PH_THROW(EPHIDGET_UNEXPECTED);
		return;
	}
	for (i=0; i<2; i++)
		codeInfo->one[i] = datai[i];
	(*env)->ReleaseIntArrayElements(env, oneArray, datai, 0);

	//Repeat
	repeatArray = (*env)->CallObjectMethod(env, irCodeInfo, irCodeInfo_getRepeat);
	if(repeatArray)
	{
		len = (*env)->GetArrayLength(env, repeatArray);
		if(len)
		{
			datai = (*env)->GetIntArrayElements(env, repeatArray, 0);
			if (!datai)
			{
				PH_THROW(EPHIDGET_UNEXPECTED);
				return;
			}
			for (i=0; i<len; i++)
				codeInfo->repeat[i] = datai[i];
			(*env)->ReleaseIntArrayElements(env, repeatArray, datai, 0);
		}
	}
	else
		repeatArray=NULL;

	//ToggleMask
	togglemask = (*env)->CallObjectMethod(env, irCodeInfo, irCodeInfo_getToggleMask);
	if(togglemask)
	{
		if (!(codeData = (*env)->CallObjectMethod(env, togglemask, irCode_getData)))
		{
			PH_THROW(EPHIDGET_UNEXPECTED);
			return;
		}
		datas = (*env)->GetShortArrayElements(env, codeData, 0);
		if (!datas)
		{
			PH_THROW(EPHIDGET_UNEXPECTED);
			return;
		}
		len = (*env)->GetArrayLength(env, codeData);
		for(i=0;i<len;i++)
		{
			codeInfo->toggle_mask[i] = (unsigned char)datas[i];
		}
		(*env)->ReleaseShortArrayElements(env, codeData, datas, 0);
	}
	else
		togglemask=NULL;

	if ((error = CPhidgetIR_Transmit(h, data, codeInfo)))
		PH_THROW(error);
}

JNIEXPORT void JNICALL
Java_com_phidgets_IRPhidget_transmitRepeat(JNIEnv *env, jobject obj)
{
	CPhidgetIRHandle h = (CPhidgetIRHandle)(uintptr_t)(*env)->GetLongField( env, obj, handle_fid);
	int error;
	if ((error = CPhidgetIR_TransmitRepeat(h)))
		PH_THROW(error);
}

JNIEXPORT void JNICALL
Java_com_phidgets_IRPhidget_transmitRaw(JNIEnv *env, jobject obj, jintArray data, jint offset, jint count, jint gap, jint carrierFrequency, jint dutyCycle)
{
	CPhidgetIRHandle h = (CPhidgetIRHandle)(uintptr_t)(*env)->GetLongField( env, obj, handle_fid);
	int error;
	jint *datai;

	datai = (*env)->GetIntArrayElements(env, data, 0);
	if (!datai)
	{
		PH_THROW(EPHIDGET_UNEXPECTED);
		return;
	}

	if ((error = CPhidgetIR_TransmitRaw(h, datai+offset, count, carrierFrequency, dutyCycle, gap)))
		PH_THROW(error);

	(*env)->ReleaseIntArrayElements(env, data, datai, 0);
}