Commit 12e02dbf authored by Projects's avatar Projects

freertos: Automatic backlight adjustment (has to be calibrated).

parent 5bd33447
......@@ -132,6 +132,7 @@ C_SRC += \
../common/emlib/src/em_dma.c \
../common/emlib/src/em_emu.c \
../common/emlib/src/em_gpio.c \
../common/emlib/src/em_i2c.c \
../common/emlib/src/em_int.c \
../common/emlib/src/em_rmu.c \
../common/emlib/src/em_rtc.c \
......@@ -145,6 +146,8 @@ C_SRC += \
../common/drivers/backlight.c \
../common/drivers/buttons.c \
../common/drivers/buzzer.c \
../common/drivers/i2cdrv.c \
../common/drivers/light_sensor.c \
../common/drivers/rtc.c \
../common/drivers/vibra.c \
../common/gfx/font_helv11.c \
......@@ -175,6 +178,7 @@ src/apps/example_app.c \
src/apps/menu.c \
src/apps/menu_struct.c \
src/main.c \
src/blight_task.c \
src/irq_dispatcher.c \
src/low_power_tick_management.c
......
/*
* Copyright (C) 2014 Julian Lewis
* @author Maciej Suminski <maciej.suminski@cern.ch>
* @author Matthieu Cattin <matthieu.cattin@cern.ch>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
/**
* @brief Automatic backlight regulation.
*/
#include "blight_task.h"
#include <FreeRTOS.h>
#include <timers.h>
#include <inttypes.h>
#include <drivers/backlight.h>
#include <drivers/light_sensor.h>
///> Number of ticks between backlight adjustments
#define BLIGHT_TASK_PERIOD 3000
///> Entry that stores an appropriate backlight level value
///> depending on the light sensor readings.
static const struct {
uint16_t lux;
uint16_t level;
} blight_lut[] = {
{ 500, 100 },
{ 2000, 90 },
{ 4000, 80 },
{ 10000, 60 },
{ 20000, 30 },
{ 65535, 0 }
};
///> Number of pairs
const int blight_lut_size = sizeof(blight_lut) / sizeof(blight_lut[0]);
///> Current state
static bool auto_enabled = false;
///> Timer handle
static xTimerHandle timer_handle;
///> Currently used index in the lookup table
static uint8_t cur_idx = 0;
static void auto_backlight_task(void *params)
{
(void) params;
uint32_t lux;
if(!light_sensor_get_lux(&lux)) {
// Look for the right value
uint8_t index;
for(index = 0; index < blight_lut_size; ++index) {
if(lux < blight_lut[index].lux)
break;
}
// Change the backlight level if it is different than the previous one
if(index != cur_idx) {
backlight_set_level(blight_lut[index].level);
cur_idx = index;
}
}
}
void auto_backlight_init(void)
{
timer_handle = xTimerCreate((signed char*) "auto_blight",
BLIGHT_TASK_PERIOD, pdTRUE,
(void*) 0, auto_backlight_task);
}
void auto_backlight_enable(bool enable)
{
if(enable == auto_enabled)
return;
if(enable) {
if(xTimerStart(timer_handle, 0) != pdPASS) {
// TODO kernel panic
}
} else {
xTimerStop(timer_handle, 0);
}
auto_enabled = true;
}
/*
* Copyright (C) 2014 Julian Lewis
* @author Maciej Suminski <maciej.suminski@cern.ch>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
/**
* @brief Automatic backlight regulation.
*/
#ifndef BLIGHT_TASK_H
#define BLIGHT_TASK_H
#include <stdbool.h>
/**
* Initializes the automatic backlight adjustment task.
*/
void auto_backlight_init(void);
/**
* Enables/disables automatic backlight adjustment.
* @param enable decides if the automatic regulation should be enabled.
* Note that you are still able to change the backlight level manually, but
* it will be overridden next time the automatic adjustment task is
* executed.
*/
void auto_backlight_enable(bool enable);
#endif /* BLIGHT_TASK_H */
......@@ -29,14 +29,17 @@
#include <em_cmu.h>
#include <sleep.h>
#include <apps/app_list.h>
#include <drivers/i2cdrv.h>
#include <drivers/backlight.h>
#include <drivers/buttons.h>
#include <drivers/buzzer.h>
#include <drivers/lcd.h>
#include <drivers/rtc.h>
#include <drivers/vibra.h>
#include <gfx/ui.h>
#include <apps/app_list.h>
#include "blight_task.h"
int main(void)
{
......@@ -47,6 +50,9 @@ int main(void)
CMU_ClockEnable(cmuClock_HFPER, true);
CMU_ClockEnable(cmuClock_GPIO, true);
I2C_Init_TypeDef i2cInit = I2C_INIT_DEFAULT;
I2CDRV_Init(&i2cInit);
backlight_init();
buttons_init();
buzzer_init();
......@@ -54,6 +60,9 @@ int main(void)
rtc_init();
lcd_init();
ui_init();
auto_backlight_init();
auto_backlight_enable(true);
// Initialize SLEEP driver, no callbacks are used
SLEEP_Init(NULL, NULL);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment