hdaps.cc

Go to the documentation of this file.
00001 /*
00002  * HDAPS Player driver
00003  */
00004 
00054 #include "hdaps.h"
00055 
00056 
00057 static int hdaps_position(int *x, int *y);
00058 
00060 // Driver setup and shutdown
00062 
00063 HDAPS::HDAPS(ConfigFile* cf, int section)
00064     : Driver(cf, section, true, PLAYER_MSGQUEUE_DEFAULT_MAXLEN)
00065 {
00066    // zero ids, so that we'll know later which interfaces were requested
00067    memset(&joystick_id, 0, sizeof(joystick_id));
00068 
00069    if (cf->ReadDeviceAddr(&joystick_id, section, "provides",
00070                           PLAYER_JOYSTICK_CODE, -1, NULL) == 0) {
00071       if (this->AddInterface(joystick_id) != 0) {
00072          this->SetError(-1);
00073          return;
00074       }
00075    }
00076 }
00077 
00078 int HDAPS::Setup()
00079 {
00080    int xpos, ypos;
00081 
00082    if (hdaps_position(&xpos, &ypos)) {
00083       PLAYER_WARN("Failed to read HDAPS position data\n"
00084                   "Maybe you need to run: modprobe hdaps");
00085    }
00086 
00087    this->StartThread();
00088    return 0;
00089 }
00090 
00091 int HDAPS::Shutdown()
00092 {
00093    this->StopThread();
00094    return 0;
00095 }
00096 
00097 HDAPS::~HDAPS (void)
00098 {
00099 }
00100 
00101 void
00102 HDAPS::Main()
00103 {
00104    int xpos, ypos;
00105 
00106    for(;;) {
00107       pthread_testcancel();
00108 
00109       this->Lock();
00110 
00111       memset(&data, 0, sizeof(data));
00112       if (hdaps_position(&xpos, &ypos) == 0) {
00113          data.xpos = (uint32_t) xpos;
00114          data.ypos = (uint32_t) ypos;
00115       }
00116 
00117       this->Unlock();
00118 
00119       if (memcmp(&prev_data, &data, sizeof(data))) {
00120          PutData();
00121          memcpy(&prev_data, &data, sizeof(data));
00122       }
00123 
00124       // Handle pending messages
00125       if (!this->InQueue->Empty()) {
00126          ProcessMessages();
00127       }
00128    }
00129 }
00130 
00131 
00133 // Message processing
00135 
00136 int
00137 HDAPS::ProcessMessage(MessageQueue *queue, player_msghdr *hdr, void *data)
00138 {
00139    PLAYER_WARN("unknown message");
00140    return -1;
00141 }
00142 
00143 
00145 // Publish / Subscribe / Unsubscribe
00147 
00148 int
00149 HDAPS::Subscribe(player_devaddr_t id)
00150 {
00151    return Driver::Subscribe(id);
00152 }
00153 
00154 int
00155 HDAPS::Unsubscribe(player_devaddr_t id)
00156 {
00157    return Driver::Unsubscribe(id);
00158 }
00159 
00160 void
00161 HDAPS::PutData(void)
00162 {
00163    PLAYER_MSG0(1, "Publishing joystick data");
00164    this->Publish(joystick_id, NULL, PLAYER_MSGTYPE_DATA,
00165                  PLAYER_JOYSTICK_DATA_STATE,
00166                  (void *) &data, sizeof(data));
00167 }
00168 
00169 
00171 // HDAPS position handling
00173 
00174 static int hdaps_position(int *x, int *y)
00175 {
00176    char buf[BUFSIZ];
00177    int fd, ret;
00178 
00179    fd = open(SYSFS_HDAPS_POSITION, O_RDONLY);
00180    if (fd < 0) {
00181       PLAYER_ERROR1("error opening: %s", strerror(errno));
00182       return fd;
00183    }
00184 
00185    ret = read(fd, buf, sizeof(buf));
00186    if (ret > 0) {
00187       ret = 0;
00188       if (sscanf (buf, "(%d,%d)\n", x, y) != 2)
00189          ret = 1;
00190 
00191    } else if (ret < 0) {
00192       PLAYER_ERROR1("error reading: %s", strerror(errno));
00193 
00194    } else {
00195       PLAYER_ERROR("Failed to read position");
00196       ret = 1;
00197    }
00198 
00199    if (close (fd))
00200       PLAYER_ERROR1("error closing: %s", strerror(errno));
00201 
00202    return ret;
00203 }
00204 
00205 
00207 // Driver module registration and initializaion
00209 
00210 Driver*
00211 HDAPS_Init(ConfigFile* cf, int section)
00212 {
00213    return (Driver*)(new HDAPS(cf, section));
00214 }
00215 
00216 void HDAPS_Register(DriverTable* table)
00217 {
00218    table->AddDriver("hdaps", HDAPS_Init);
00219 }
00220 
00221 /* Need the extern to avoid C++ name-mangling  */
00222 extern "C" {
00223 
00224 int player_driver_init(DriverTable* table)
00225 {
00226    PLAYER_MSG0(1, "Registering HDAPS driver.");
00227    HDAPS_Register(table);
00228    return 0;
00229 }
00230 
00231 }

Generated on Sat Jan 13 03:11:09 2007 for ERSP Player driver by  doxygen 1.5.1