Imported Upstream version 0.9.2
[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 #define SAMPLE_NUM 5
29
30 namespace Sensors
31 {
32 mraa_gpio_context led_gpio = NULL;
33 mraa_aio_context tmp_aio = NULL;
34 mraa_aio_context light_aio = NULL;
35
36 inline void SetupPins()
37 {
38     led_gpio = mraa_gpio_init(ONBOARD_LED_PIN); // Initialize pin 13
39     if (led_gpio != NULL)
40         mraa_gpio_dir(led_gpio, MRAA_GPIO_OUT); // Set direction to OUTPUT
41     tmp_aio = mraa_aio_init(TEMPERATURE_AIO_PIN);   // initialize pin 0
42     light_aio = mraa_aio_init(LIGHT_SENSOR_AIO_PIN);   // initialize pin 2
43 }
44
45 inline void ClosePins()
46 {
47     mraa_gpio_close(led_gpio);
48     mraa_aio_close(tmp_aio);
49     mraa_aio_close(light_aio);
50 }
51
52 inline void SetOnboardLed(int on)
53 {
54     if (led_gpio == NULL)
55     {
56         led_gpio = mraa_gpio_init(ONBOARD_LED_PIN); // Initialize pin 13
57         if (led_gpio != NULL)
58             mraa_gpio_dir(led_gpio, MRAA_GPIO_OUT); // Set direction to OUTPUT
59     }
60     if (led_gpio != NULL)
61         mraa_gpio_write(led_gpio, on); // Writes into GPIO
62 }
63
64 inline float GetAverageTemperatureRaw()
65 {
66     if (tmp_aio == NULL)
67     {
68         tmp_aio = mraa_aio_init(TEMPERATURE_AIO_PIN); // initialize pin 0
69     }
70     
71     uint16_t adc_value = 0;
72     for (int i=0; i< SAMPLE_NUM; i++)
73         adc_value += mraa_aio_read(tmp_aio);           // read the raw value
74     
75     float average = (float)adc_value/SAMPLE_NUM;
76     cout << "Temperature reading raw ..."  << average << endl;
77     
78     return average;
79 }
80
81 inline float GetTemperatureInC()
82 {
83     // Temperature calculation using simpilfy Steinhart-Hart equation
84     //
85     //          1/T = 1/T0 + 1/beta*ln (R/R0)
86     //
87     // where T0 = 25C room temp, R0 = 10000 ohms
88     //
89     float beta = 4090.0;            //the beta of the thermistor, magic number
90     float t_raw = GetAverageTemperatureRaw();
91     float R = 1023.0/t_raw -1;      // 
92     R = 10000.0/R;                  // 10K resistor divider circuit
93         
94     float T1 = log(R/10000.0)/beta; // natural log 
95     float T2 = T1 + 1.0/298.15;     // room temp 25C= 298.15K
96     float ret = 1.0/T2 - 273.0;
97  
98     return ret;
99 }
100
101 // This function returns light level between 0 and 4095
102 inline int GetLightLevel()
103 {
104     uint16_t adc_value = 0;
105     if (light_aio == NULL)
106         light_aio = mraa_aio_init(LIGHT_SENSOR_AIO_PIN); // initialize pin 2
107     if (light_aio != NULL)
108         adc_value = mraa_aio_read(light_aio); // read the raw value
109     return adc_value;
110 }
111 }