qtmobility/plugins/sensors/n900/n900proximitysensor.cpp
changeset 4 90517678cc4f
parent 1 2b40d63a9c3d
child 11 06b8e2af4411
equal deleted inserted replaced
1:2b40d63a9c3d 4:90517678cc4f
    38 ** $QT_END_LICENSE$
    38 ** $QT_END_LICENSE$
    39 **
    39 **
    40 ****************************************************************************/
    40 ****************************************************************************/
    41 
    41 
    42 #include "n900proximitysensor.h"
    42 #include "n900proximitysensor.h"
       
    43 #include <QFile>
    43 #include <QDebug>
    44 #include <QDebug>
    44 #include <string.h>
    45 #include <string.h>
    45 #include <time.h>
    46 #include <time.h>
    46 
    47 
    47 const char *n900proximitysensor::id("n900.proximity");
    48 const char *n900proximitysensor::id("n900.proximity");
    49 
    50 
    50 n900proximitysensor::n900proximitysensor(QSensor *sensor)
    51 n900proximitysensor::n900proximitysensor(QSensor *sensor)
    51     : n900filebasedsensor(sensor)
    52     : n900filebasedsensor(sensor)
    52 {
    53 {
    53     setReading<QProximityReading>(&m_reading);
    54     setReading<QProximityReading>(&m_reading);
       
    55     addDataRate(100, 100); // 100Hz
       
    56     sensor->setDataRate(100); // default is 10Hz
       
    57 }
       
    58 
       
    59 void n900proximitysensor::start()
       
    60 {
       
    61     if (!QFile::exists(QLatin1String(filename)))
       
    62         goto error;
       
    63 
       
    64     n900filebasedsensor::start();
       
    65     return;
       
    66 
       
    67 error:
       
    68     sensorStopped();
    54 }
    69 }
    55 
    70 
    56 void n900proximitysensor::poll()
    71 void n900proximitysensor::poll()
    57 {
    72 {
    58     FILE *fd = fopen(filename, "r");
    73     FILE *fd = fopen(filename, "r");
    60     char buffer[20];
    75     char buffer[20];
    61     int rs = fscanf(fd, "%s", buffer);
    76     int rs = fscanf(fd, "%s", buffer);
    62     fclose(fd);
    77     fclose(fd);
    63     if (rs != 1) return;
    78     if (rs != 1) return;
    64 
    79 
    65     QProximityReading::Proximity proximity = QProximityReading::Undefined;
    80     bool close;
    66     if (strcmp(buffer, "closed") == 0) {
    81     if (strcmp(buffer, "closed") == 0) {
    67         proximity = QProximityReading::Close;
    82         close = true;
    68     } else {
    83     } else {
    69         proximity = QProximityReading::NotClose;
    84         close = false;
    70     }
    85     }
    71 
    86 
    72     m_reading.setTimestamp(clock());
    87     if (close != m_reading.close()) {
    73     m_reading.setProximity(proximity);
    88         m_reading.setTimestamp(clock());
    74 
    89         m_reading.setClose(close);
    75     newReadingAvailable();
    90         newReadingAvailable();
       
    91     }
    76 }
    92 }
    77 
    93