1 //******************************************************************
3 // Copyright 2014 Intel Corporation.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
25 #define ONBOARD_LED_PIN 13
26 #define TEMPERATURE_AIO_PIN 0
27 #define LIGHT_SENSOR_AIO_PIN 2
32 mraa_gpio_context led_gpio = NULL;
33 mraa_aio_context tmp_aio = NULL;
34 mraa_aio_context light_aio = NULL;
36 inline void SetupPins()
38 led_gpio = mraa_gpio_init(ONBOARD_LED_PIN); // Initialize pin 13
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
45 inline void ClosePins()
47 mraa_gpio_close(led_gpio);
48 mraa_aio_close(tmp_aio);
49 mraa_aio_close(light_aio);
52 inline void SetOnboardLed(int on)
56 led_gpio = mraa_gpio_init(ONBOARD_LED_PIN); // Initialize pin 13
58 mraa_gpio_dir(led_gpio, MRAA_GPIO_OUT); // Set direction to OUTPUT
61 mraa_gpio_write(led_gpio, on); // Writes into GPIO
64 inline float GetAverageTemperatureRaw()
68 tmp_aio = mraa_aio_init(TEMPERATURE_AIO_PIN); // initialize pin 0
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
75 float average = (float)adc_value/SAMPLE_NUM;
76 cout << "Temperature reading raw ..." << average << endl;
81 inline float GetTemperatureInC()
83 // Temperature calculation using simpilfy Steinhart-Hart equation
85 // 1/T = 1/T0 + 1/beta*ln (R/R0)
87 // where T0 = 25C room temp, R0 = 10000 ohms
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
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;
101 // This function returns light level between 0 and 4095
102 inline int GetLightLevel()
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