wacom.c

Go to the documentation of this file.
00001 /*
00002  * File Name: wacom.c
00003  */
00004 
00005 /*
00006  * This file is part of sysd.
00007  *
00008  * sysd is free software: you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation, either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * sysd is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00020  */
00021 
00022 /**
00023  * Copyright (C) 2008 iRex Technologies B.V.
00024  * All rights reserved.
00025  */
00026 
00027 //----------------------------------------------------------------------------
00028 // Include Files
00029 //----------------------------------------------------------------------------
00030 
00031 // system include files, between < >
00032 #include <glib.h>
00033 #include <stdio.h>
00034 #include <fcntl.h>
00035 #include <unistd.h>
00036 #include <termios.h>
00037 #include <string.h>
00038 
00039 // local include files, between " "
00040 #include "log.h"
00041 #include "system.h"
00042 #include "wacom.h"
00043 
00044 
00045 //----------------------------------------------------------------------------
00046 // Type Declarations
00047 //----------------------------------------------------------------------------
00048 
00049 typedef struct {
00050     char *uname;
00051     char *cmd;
00052     char *help;
00053 } command;
00054 
00055 
00056 //----------------------------------------------------------------------------
00057 // Constants
00058 //----------------------------------------------------------------------------
00059 
00060 #define NUM_COMMANDS  8
00061 #define DEV_WACOM     "/dev/ttymxc1"
00062 
00063 
00064 //----------------------------------------------------------------------------
00065 // Static Variables
00066 //----------------------------------------------------------------------------
00067 
00068 static command commands[] = {
00069     {"stop",        "0", "Stop sampling"},
00070     {"sample-133",  "1", "Start sampling at 133 pts/sec"},
00071     {"sample-80",   "2", "Start sampling at 80 pts/sec"},
00072     {"sample-40",   "3", "Start sampling at 40 pts/sec"},
00073     {"survey-on",   "+", "Enable survey scan"},
00074     {"survey-off",  "-", "Disable survey scan"},
00075     {"tiltcor-on",  "t", "Enable tilt correction"},
00076     {"tiltcor-off", "u", "Disable tilt correction"},
00077 };
00078 
00079 static gboolean g_wacom_enabled = FALSE;
00080 
00081 
00082 //============================================================================
00083 // Local Function Definitions
00084 //============================================================================
00085 
00086 static gboolean send_command(char *name);
00087 static gint port_open(char *name);
00088 static void set_defaults(void);
00089 
00090 
00091 //============================================================================
00092 // Functions Implementation
00093 //============================================================================
00094 
00095 void wacom_scan_high()
00096 {
00097     LOGPRINTF("entry");
00098     if (g_wacom_enabled)
00099     {
00100         wacom_resume();
00101         send_command("sample-133");
00102     }
00103     else
00104     {
00105         WARNPRINTF("wacom is disabled, mode not changed");
00106     }
00107 }
00108 
00109 
00110 void wacom_scan_normal()
00111 {
00112     LOGPRINTF("entry");
00113     if (g_wacom_enabled)
00114     {
00115         wacom_resume();
00116         send_command("sample-80");
00117     }
00118     else
00119     {
00120         WARNPRINTF("wacom is disabled, mode not changed");
00121     }
00122 }
00123 
00124 
00125 void wacom_suspend()
00126 {
00127     LOGPRINTF("entry");
00128     if (g_wacom_enabled)
00129     {
00130         // since the wacom switches to normal mode (8pps) automatically, don't do anything
00131     }
00132     else
00133     {
00134         WARNPRINTF("wacom is disabled, mode not changed");
00135     }
00136 }
00137 
00138 
00139 void wacom_resume()
00140 {
00141     LOGPRINTF("entry");
00142     if (g_wacom_enabled)
00143     {
00144         // since the wacom switches to writing mode (133pps) automatically, don't do anything
00145     }
00146     else
00147     {
00148         WARNPRINTF("wacom is disabled, mode not changed");
00149     }
00150 }
00151 
00152 
00153 void wacom_tilt_correction_enable(gboolean tilt_enable)
00154 {
00155     LOGPRINTF("entry");
00156     if (g_wacom_enabled && tilt_enable)
00157     {
00158         send_command("tiltcor-on");
00159     }
00160     else if (g_wacom_enabled && !tilt_enable)
00161     {
00162         send_command("tiltcor-off");
00163     }
00164     else
00165     {
00166         WARNPRINTF("wacom is disabled, tilt correction not changed");
00167     }
00168 }
00169 
00170 
00171 void wacom_enable()
00172 {
00173     LOGPRINTF("entry");
00174     
00175     g_wacom_enabled = TRUE;
00176     sys_set_power("wacom", 1);
00177     set_defaults();
00178 }
00179 
00180 
00181 void wacom_disable()
00182 {
00183     LOGPRINTF("entry");
00184     
00185     sys_set_power("wacom", 0);
00186     g_wacom_enabled = FALSE;
00187 }
00188 
00189 
00190 void wacom_reset()
00191 {
00192     LOGPRINTF("entry");
00193     
00194     g_wacom_enabled = TRUE;
00195     sys_set_power("wacom_reset", 1);
00196     set_defaults();
00197 }
00198 
00199 
00200 gboolean wacom_is_enabled()
00201 {
00202     return g_wacom_enabled;
00203 }
00204 
00205 
00206 //============================================================================
00207 // Local Function Implementation
00208 //============================================================================
00209 
00210 static void set_defaults(void)
00211 {
00212     wacom_scan_high();
00213     wacom_tilt_correction_enable(FALSE);
00214 }
00215 
00216 
00217 static gboolean send_command(char *name)
00218 {
00219     LOGPRINTF("entry");
00220 
00221     g_return_val_if_fail(name != NULL, FALSE);
00222 
00223     gboolean retval = FALSE;
00224     gint fd = 0;
00225     gint num = 0;
00226 
00227     fd = port_open(DEV_WACOM);
00228     if (fd > 0)
00229     {
00230         gint i;
00231         for (i=0; i<NUM_COMMANDS; i++)
00232         {
00233             if (strcmp(name, commands[i].uname)==0)
00234             {
00235                 LOGPRINTF("sending command %s, %s", commands[i].uname, commands[i].help);
00236 
00237                 num = write(fd,commands[i].cmd, strlen(commands[i].cmd));
00238                 LOGPRINTF("- bytes sent %d (%s)", num, commands[i].cmd);
00239                 if (num < 0)
00240                 {
00241                     ERRORPRINTF("- error sending: %d (%s)", errno, strerror(errno));
00242                 }
00243                     
00244                 retval = TRUE;
00245             }
00246         }
00247         close(fd);
00248     }
00249     
00250     return retval;
00251 }
00252 
00253 
00254 static gint port_open(char *name)
00255 {
00256     LOGPRINTF("entry");
00257     
00258     g_return_val_if_fail(name != NULL, FALSE);
00259     
00260     gint fd;
00261     struct termios t;
00262     
00263     if ((fd = open(name, O_RDWR | O_NOCTTY)) < 0)
00264     {
00265         ERRORPRINTF("Can't open device %s\n", name);
00266         return FALSE;
00267     }
00268 
00269     tcgetattr(fd, &t);
00270 
00271     t.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
00272     t.c_oflag &= ~OPOST;
00273     t.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
00274     t.c_cflag &= ~(CSIZE|PARENB);
00275     t.c_cflag |= CS8|CLOCAL;
00276     t.c_cflag &= ~(CSTOPB);     // 1 stop bit 
00277     t.c_cflag &= ~(CSIZE);      // 8 data bits 
00278     t.c_cflag |= CS8;
00279     t.c_cflag &= ~(PARENB);     // no parity 
00280     t.c_iflag |= IXOFF;         // flow control XOff 
00281     t.c_cc[VMIN] = 0;           // vmin value 
00282     t.c_cc[VTIME] = 10;         // vtime value 
00283 
00284     cfsetispeed(&t, B19200);
00285     cfsetospeed(&t, B19200);
00286     tcflush(fd, TCIFLUSH);
00287     tcsetattr(fd, TCSANOW, &t);
00288     
00289     return fd;
00290 }
00291 
00292     
Generated by  doxygen 1.6.2-20100208