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
31 mraa_gpio_context led_gpio = NULL;
32 mraa_aio_context tmp_aio = NULL;
33 mraa_aio_context light_aio = NULL;
35 inline void SetupPins()
37 led_gpio = mraa_gpio_init(ONBOARD_LED_PIN); // Initialize pin 13
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
44 inline void ClosePins()
46 mraa_gpio_close(led_gpio);
47 mraa_aio_close(tmp_aio);
48 mraa_aio_close(light_aio);
51 inline void SetOnboardLed(int on)
55 led_gpio = mraa_gpio_init(ONBOARD_LED_PIN); // Initialize pin 13
57 mraa_gpio_dir(led_gpio, MRAA_GPIO_OUT); // Set direction to OUTPUT
60 mraa_gpio_write(led_gpio, on); // Writes into GPIO
63 inline float GetTemperatureInC()
68 tmp_aio = mraa_aio_init(TEMPERATURE_AIO_PIN); // initialize pin 0
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;
80 // This function returns light level between 0 and 4095
81 inline int GetLightLevel()
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