iotivity 0.9.0
[platform/upstream/iotivity.git] / examples / OICSensorBoard / sensors.h
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Corporation.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #include <stdlib.h>
22 #include <math.h>
23 #include "mraa.h"
24
25 #define ONBOARD_LED_PIN 13
26 #define TEMPERATURE_AIO_PIN 0
27 #define LIGHT_SENSOR_AIO_PIN 2
28
29 namespace Sensors
30 {
31 mraa_gpio_context led_gpio = NULL;
32 mraa_aio_context tmp_aio = NULL;
33 mraa_aio_context light_aio = NULL;
34
35 inline void SetupPins()
36 {
37     led_gpio = mraa_gpio_init(ONBOARD_LED_PIN); // Initialize pin 13
38     if (led_gpio != NULL)
39         mraa_gpio_dir(led_gpio, MRAA_GPIO_OUT); // Set direction to OUTPUT
40     tmp_aio = mraa_aio_init(TEMPERATURE_AIO_PIN);   // initialize pin 0
41     light_aio = mraa_aio_init(LIGHT_SENSOR_AIO_PIN);   // initialize pin 2
42 }
43
44 inline void ClosePins()
45 {
46     mraa_gpio_close(led_gpio);
47     mraa_aio_close(tmp_aio);
48     mraa_aio_close(light_aio);
49 }
50
51 inline void SetOnboardLed(int on)
52 {
53     if (led_gpio == NULL)
54     {
55         led_gpio = mraa_gpio_init(ONBOARD_LED_PIN); // Initialize pin 13
56         if (led_gpio != NULL)
57             mraa_gpio_dir(led_gpio, MRAA_GPIO_OUT); // Set direction to OUTPUT
58     }
59     if (led_gpio != NULL)
60         mraa_gpio_write(led_gpio, on); // Writes into GPIO
61 }
62
63 inline float GetTemperatureInC()
64 {
65     float ret = 0;
66     if (tmp_aio == NULL)
67     {
68         tmp_aio = mraa_aio_init(TEMPERATURE_AIO_PIN); // initialize pin 0
69     }
70     if (tmp_aio != NULL)
71     {
72         uint16_t adc_value = mraa_aio_read(tmp_aio); // read the raw value
73         //convert reading to temperature
74         float beta = 4090.0; //the beta of the thermistor, magic number
75         ret = beta / (log((4095.0 * 10 / adc_value - 10) / 10) + beta / 298.0) - 273.0;
76     }
77     return ret;
78 }
79
80 // This function returns light level between 0 and 4095
81 inline int GetLightLevel()
82 {
83     uint16_t adc_value = 0;
84     if (light_aio == NULL)
85         light_aio = mraa_aio_init(LIGHT_SENSOR_AIO_PIN); // initialize pin 2
86     if (light_aio != NULL)
87         adc_value = mraa_aio_read(light_aio); // read the raw value
88     return adc_value;
89 }
90 }