コード例 #1
0
static int data__poll(struct sensors_data_context_t *dev, sensors_data_t* values)
{
    int akm_fd = dev->events_fd[0];
    int cm_fd = dev->events_fd[1];
    int ls_fd = dev->events_fd[2];

    if (akm_fd < 0) {
        LOGE("invalid compass file descriptor, fd=%d", akm_fd);
        return -1;
    }

    if (cm_fd < 0) {
        LOGE("invalid proximity-sensor file descriptor, fd=%d", cm_fd);
        return -1;
    }

    if (ls_fd < 0) {
        LOGE("invalid light-sensor file descriptor, fd=%d", ls_fd);
        return -1;
    }

    // there are pending sensors, returns them now...
    if (dev->pendingSensors) {
        LOGV("pending sensors 0x%08x", dev->pendingSensors);
        return pick_sensor(dev, values);
    }

    // wait until we get a complete event for an enabled sensor
    uint32_t new_sensors = 0;
    while (1) {
        /* read the next event; first, read the compass event, then the
           proximity event */
        struct input_event event;
        int got_syn = 0;
        int exit = 0;
        int nread;
        fd_set rfds;
        int n;

        FD_ZERO(&rfds);
        FD_SET(akm_fd, &rfds);
        FD_SET(cm_fd, &rfds);
        FD_SET(ls_fd, &rfds);
        n = select(__MAX(akm_fd, __MAX(cm_fd, ls_fd)) + 1, &rfds,
                   NULL, NULL, NULL);
        LOGV("return from select: %d\n", n);
        if (n < 0) {
            LOGE("%s: error from select(%d, %d): %s",
                 __FUNCTION__,
                 akm_fd, cm_fd, strerror(errno));
            return -1;
        }

        if (FD_ISSET(akm_fd, &rfds)) {
            nread = read(akm_fd, &event, sizeof(event));
            if (nread == sizeof(event)) {
                new_sensors |= data__poll_process_akm_abs(dev, akm_fd, &event);
                LOGV("akm abs %08x", new_sensors);
                got_syn = event.type == EV_SYN;
                exit = got_syn && event.code == SYN_CONFIG;
                if (got_syn) {
                    LOGV("akm syn %08x", new_sensors);
                    data__poll_process_syn(dev, &event, new_sensors);
                    new_sensors = 0;
                }
            }
            else LOGE("akm read too small %d", nread);
        }
        else LOGV("akm fd is not set");

        if (FD_ISSET(cm_fd, &rfds)) {
            nread = read(cm_fd, &event, sizeof(event));
            if (nread == sizeof(event)) {
                new_sensors |= data__poll_process_cm_abs(dev, cm_fd, &event);
                LOGV("cm abs %08x", new_sensors);
                got_syn |= event.type == EV_SYN;
                exit |= got_syn && event.code == SYN_CONFIG;
                if (got_syn) {
                    LOGV("cm syn %08x", new_sensors);
                    data__poll_process_syn(dev, &event, new_sensors);
                    new_sensors = 0;
                }
            }
            else LOGE("cm read too small %d", nread);
        }
        else LOGV("cm fd is not set");

        if (FD_ISSET(ls_fd, &rfds)) {
            nread = read(ls_fd, &event, sizeof(event));
            if (nread == sizeof(event)) {
                new_sensors |= data__poll_process_ls_abs(dev, ls_fd, &event);
                LOGV("ls abs %08x", new_sensors);
                got_syn |= event.type == EV_SYN;
                exit |= got_syn && event.code == SYN_CONFIG;
                if (got_syn) {
                    LOGV("ls syn %08x", new_sensors);
                    data__poll_process_syn(dev, &event, new_sensors);
                    new_sensors = 0;
                }
            }
            else LOGE("ls read too small %d", nread);
        }
        else LOGV("ls fd is not set");

        if (exit) {
            // we use SYN_CONFIG to signal that we need to exit the
            // main loop.
            //LOGV("got empty message: value=%d", event->value);
            LOGV("exit");
            return 0x7FFFFFFF;
        }

        if (got_syn && dev->pendingSensors) {
            LOGV("got syn, picking sensor");
            return pick_sensor(dev, values);
        }
    }
}
コード例 #2
0
static int data__poll(struct sensors_data_context_t *dev, sensors_data_t* values)
{ 
	struct input_event event;

    int acc_fd = dev->events_fd[0];
    int ori_fd = dev->events_fd[1];
    //int ls_fd = dev->events_fd[2];
    int got_syn = 0;
    int exit = 0;
    int nread;
    fd_set rfds;
    int n;

    if (acc_fd < 0) {
        LOGE("invalid compass file descriptor, fd=%d", acc_fd);
        return -1;
    }

    if (ori_fd < 0) {
        LOGE("invalid proximity-sensor file descriptor, fd=%d", ori_fd);
        return -1;
    }

    /*if (ls_fd < 0) {
        LOGE("invalid light-sensor file descriptor, fd=%d", ls_fd);
        return -1;
    }*/

    // there are pending sensors, returns them now...
    if (dev->pendingSensors) {
        LOGV("pending sensors 0x%08x", dev->pendingSensors);
        return pick_sensor(dev, values);
    }

    // wait until we get a complete event for an enabled sensor
    uint32_t new_sensors = 0;

    while (1) {
        FD_ZERO(&rfds);
        FD_SET(acc_fd, &rfds);
        FD_SET(ori_fd, &rfds);
        //FD_SET(ls_fd, &rfds);

		/*n = select(__MAX(acc_fd, __MAX(ori_fd, ls_fd)) + 1, &rfds, NULL, NULL, NULL);*/

		n = select( __MAX(ori_fd, acc_fd) + 1, &rfds,
				NULL, NULL, NULL);

        if (n < 0) {
            LOGE("%s: error from select(%d, %d): %s",
                 __FUNCTION__,
                 acc_fd, ori_fd, strerror(errno));
            return -1;
        }
		
        if(delay_time_ms != 0)
            usleep(delay_time_ms * 200);
		
        if (FD_ISSET(acc_fd, &rfds)) {
            nread = read(acc_fd, &event, sizeof(event));
            if (nread == sizeof(event)) {
                new_sensors |= data__poll_process_acc_abs(dev, acc_fd, &event);
                LOGV("acc abs %08x", new_sensors);
                got_syn = event.type == EV_SYN;
                exit = got_syn && event.code == SYN_CONFIG;
                if (got_syn) {
                    LOGV("acc syn %08x", new_sensors);
                    data__poll_process_syn(dev, &event, SENSORS_ACCELERATION);
                    new_sensors = 0;
                }
            }
            else LOGE("acc read too small %d", nread);
        }
        else LOGV("acc fd is not set");

        if (FD_ISSET(ori_fd, &rfds)) {
            nread = read(ori_fd, &mrawData, sizeof(mrawData));
            if (nread == sizeof(mrawData)) {
			usleep(1000000);
#if 0
				 LOGE("mSensor1 data:  x,y,z:  %d, %d, %d\n", 
				 	 mrawData.x, 
				 	 mrawData.y, 
				 	 mrawData.z); 
#endif

				g_compass_data.x = mrawData.x;
				g_compass_data.y = mrawData.y;
				g_compass_data.z = mrawData.z;

                new_sensors |= data__poll_process_ori_abs(dev, ori_fd);
                LOGV("ori abs %08x", new_sensors);
                //got_syn |= event.type == EV_SYN;
                //exit |= got_syn && event.code == SYN_CONFIG;
				got_syn |= 1;
                if (got_syn) {
                    LOGV("ori syn %08x", new_sensors);
		    data__poll_process_syn_not_inputdev(dev, SENSORS_ORIENTATION|SENSORS_MAGNETIC_FIELD);
                    new_sensors = 0;
                }
            }
            else LOGV("ori read too small %d", nread);
        }
        else LOGV("ori fd is not set");

#if 0
        if (FD_ISSET(ls_fd, &rfds)) {
            nread = read(ls_fd, &event, sizeof(event));
            if (nread == sizeof(event)) {
                new_sensors |= data__poll_process_ls_abs(dev, ls_fd, &event);
                LOGV("ls abs %08x", new_sensors);
                got_syn |= event.type == EV_SYN;
                exit |= got_syn && event.code == SYN_CONFIG;
                if (got_syn) {
                    LOGV("ls syn %08x", new_sensors);
                    data__poll_process_syn(dev, &event, SENSORS_LIGHT);
                    new_sensors = 0;
                }
            }
            else 
                LOGV("ls read too small %d", nread);  
        }
        else 
            LOGV("ls fd is not set");
#endif

        if (exit) {
            // we use SYN_CONFIG to signal that we need to exit the
            // main loop.
            //LOGV("got empty message: value=%d", event->value);
	    LOGE("exit");
	    dev->pendingSensors = 0;
	    return 0x7FFFFFFF;
        }

        if (got_syn && dev->pendingSensors) {
            LOGV("got syn, picking sensor");
            /* LOGE("sensor is:%d [%f, %f, %f]", */
            /*         values->sensor, */
            /*         values->vector.x, */
            /*         values->vector.y, */
			/* 	 values->vector.z */
			/* 	 ); */
            return pick_sensor(dev, values);
        }
    }
}