1 /*
2  * Copyright (C) 2015 Intel Corporation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /*
18  * A Proximity sensor with a true/false value read in via GPIO
19  * is implemented. The Sensor HAL returns a float for the proximity
20  * distance... but in the case of a GPIO sensor, we only have close and
21  * no-close (1/0). We fake the distance by returning the values below.
22  */
23 #define kProximityClose   0.0
24 #define kProximityFar   100.0
25 
26 
27 /*
28  * We monitor GPIO48 for the proximity reading.
29  * This corresponids to IO7 on the Arduino shield and is
30  * not multiplexed with any other functionality.
31  *
32  * The mraa library expects the Arudino sheild pin number here when
33  * talking to the Arduino expansion board. Change this appropriate when
34  * using a different GPIO pin or different Edison breakout board.
35  */
36 #define kPinGPIO 7
37 
38 #include <cutils/log.h>
39 #include "ProximityGPIO.hpp"
40 #include "SensorsHAL.hpp"
41 
42 struct sensor_t ProximityGPIO::sensorDescription = {
43   .name = "Proximity GPIO Sensor",
44   .vendor = "Unknown",
45   .version = 1,
46   .handle = -1,
47   .type = SENSOR_TYPE_PROXIMITY,
48   .maxRange = 100.0f,
49   .resolution = 1.0f,
50   .power = 0.001f,
51   .minDelay = 10,
52   .fifoReservedEventCount = 0,
53   .fifoMaxEventCount = 0,
54   .stringType = SENSOR_STRING_TYPE_PROXIMITY,
55   .requiredPermission = "",
56   .maxDelay = 1000,
57   .flags = SENSOR_FLAG_ON_CHANGE_MODE,
58   .reserved = {},
59 };
60 
createSensor(int pollFd)61 Sensor * ProximityGPIO::createSensor(int pollFd) {
62   return new ProximityGPIO(pollFd, kPinGPIO);
63 }
64 
initModule()65 void ProximityGPIO::initModule() {
66   SensorContext::addSensorModule(&sensorDescription, createSensor);
67 }
68 
ProximityGPIO(int pollFd,int pin)69 ProximityGPIO::ProximityGPIO(int pollFd, int pin) : upm::GroveButton(pin), pollFd(pollFd) {
70   handle = sensorDescription.handle;
71   type = SENSOR_TYPE_PROXIMITY;
72 }
73 
~ProximityGPIO()74 ProximityGPIO::~ProximityGPIO() {}
75 
pollEvents(sensors_event_t * data,int count)76 int ProximityGPIO::pollEvents(sensors_event_t* data, int count) {
77   data->distance = !value() ? kProximityClose: kProximityFar;
78   return 1;
79 }
80 
activate(int handle,int enabled)81 int ProximityGPIO::activate(int handle, int enabled) {
82   /* start or stop the acquisition thread */
83   return activateAcquisitionThread(pollFd, handle, enabled);
84 }
85