Commit 0c8f043f authored by Projects's avatar Projects

ui: Experimental battery status icon.

parent 6b897bf0
......@@ -29,6 +29,7 @@
#define __MAX17047_H_
#include <stdint.h>
#include <stdbool.h>
// Two-wire protocol address
#define MAX17047_ADDRESS 0x6C
......@@ -58,7 +59,7 @@
#define MAX17047_REG_DESIGN_CAP 0x18
#define MAX17047_REG_AVG_VCELL 0x19
#define MAX17047_REG_MAX_MIN_TEMP 0x1A
#define MAX17047_REG_MAX_MIN_VCELL 0z1B
#define MAX17047_REG_MAX_MIN_VCELL 0x1B
#define MAX17047_REG_MAX_MIN_CURRENT 0x1C
#define MAX17047_REG_CONFIG 0x1D
#define MAX17047_REG_ICHG_TERM 0x1E
......@@ -177,7 +178,7 @@ uint8_t max17047_write_reg(uint8_t address, uint8_t length, uint8_t* buffer);
uint8_t max17047_init(void);
/**
* @brief Saves the leared-value and application register.
* @brief Saves the learned-value and application register.
*
* This function should be called periodically (e.g. End-of-charge, End-of-discharge,
* prior to entering shutdown state).
......@@ -236,4 +237,13 @@ uint8_t max17047_get_charge(void);
*/
uint16_t max17047_get_time_left(void);
/**
* @brief Checks if the battery is charging or discharging.
* @return true if the battery is charging, false otherwise.
*/
static inline bool max17047_is_charging(void)
{
return (max17047_get_current() > 0);
}
#endif /*__MAX17047_H_ */
......@@ -27,13 +27,17 @@
#ifndef EVENT_H
#define EVENT_H
#include <stdint.h>
#include <stdbool.h>
/**
* Possible event types.
*/
enum event_type {
BUTTON_PRESSED,
SENSOR_INT,
RTC_TICK
RTC_TICK,
BATTERY_STATUS
};
/**
......@@ -55,6 +59,11 @@ enum sensor_type {
ACCELEROMETER
};
struct battery_info {
uint8_t percentage;
bool charging;
};
/**
* Structure describing events received by applications.
*/
......@@ -66,6 +75,7 @@ struct event {
union {
enum button_name button;
enum sensor_type sensor;
struct battery_info battery;
} data;
};
......
......@@ -149,6 +149,7 @@ C_SRC += \
../common/drivers/buzzer.c \
../common/drivers/i2cdrv.c \
../common/drivers/light_sensor.c \
../common/drivers/max17047.c \
../common/drivers/rtc.c \
../common/drivers/vibra.c \
../common/gfx/font_helv11.c \
......@@ -182,6 +183,7 @@ src/apps/example_app.c \
src/apps/menu.c \
src/apps/menu_struct.c \
src/main.c \
src/battery_task.c \
src/blight_task.c \
src/irq_dispatcher.c \
src/low_power_tick_management.c \
......
......@@ -112,13 +112,10 @@ void clock_main(void* params) {
return; // go back to the main menu
// no break; fall through
case RTC_TICK: // and BUTTON_PRESSED
ui_update(&evt); // forward event to widgets
default: // suppress warnings
ui_update(&evt); // forward event to widgets
break;
}
}
}
}
......
......@@ -172,16 +172,16 @@ void menu_main(void* params) {
if(xQueueReceive(appQueue, &evt, 0)) {
switch(evt.type) {
case BUTTON_PRESSED:
if(evt.data.button == BUT_TL)
if(evt.data.button == BUT_TL) {
go_back();
else if(evt.data.button == BUT_TR)
} else if(evt.data.button == BUT_TR) {
// run the selected application or submenu
run(&(*current_menu)->entries[selected_item]);
else
ui_update(&evt);
}
break;
default: // suppress warnings
ui_update(&evt);
break;
}
}
......
......@@ -26,16 +26,52 @@
*/
#include "status_bar.h"
#include <bitmaps.h>
#include <event.h>
#include <math.h>
static const unsigned int BATTERY_POS = 111;
static const unsigned int BATTERY_BARS = 10;
static unsigned percentage;
static bool charging;
static void status_bar_event(struct ui_widget *w, const struct event *evt)
{
// TODO update info about GPS signal, time, etc.
if(evt->type == BATTERY_STATUS) {
if(abs(percentage - evt->data.battery.percentage) > 5 || charging != evt->data.battery.charging) {
percentage = evt->data.battery.percentage;
charging = evt->data.battery.charging;
w->flags |= WF_DIRTY;
}
}
}
static void status_bar_redraw(struct ui_widget *w)
{
gfx_round_box(&w->dc, 30, -10, 127 - 30, 10, 9, COLOR_BLACK);
gfx_centered_text(&w->dc, &font_helv11, 0, "Home", 1);
unsigned i;
gfx_clear(&w->dc, 0);
/*gfx_round_box(&w->dc, 30, -10, 127 - 30, 10, 9, COLOR_BLACK);*/
/*gfx_centered_text(&w->dc, &font_helv11, 0, "Home", 1);*/
if(charging) {
gfx_draw_bitmap(&w->dc, BATTERY_POS, 0, &battery_charging);
} else {
gfx_draw_bitmap(&w->dc, BATTERY_POS, 0, &battery);
if(percentage > 100) {
// Indicate wrong reading (draw the exclamation mark)
gfx_line(&w->dc, BATTERY_POS + 6, 2, BATTERY_POS + 6, 7, 1);
gfx_set_pixel(&w->dc, BATTERY_POS + 6, 9, 1);
} else {
// Draw bars
for(i = 0; i < percentage / BATTERY_BARS; ++i)
gfx_line(&w->dc, BATTERY_POS + 2 + i, 2, BATTERY_POS + 2 + i, 9, 1);
}
}
}
struct ui_widget status_bar = {
......
/*
* 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 Battery status monitor.
*/
#include "battery_task.h"
#include <drivers/max17047.h>
#include <apps/application.h>
#include <event.h>
#include <FreeRTOS.h>
#include <timers.h>
#include <stdbool.h>
///> Number of ticks between battery status polling
#define BATTERY_TASK_PERIOD 4000
///> Timer handle
static xTimerHandle timer_handle;
static bool cur_charging;
static unsigned cur_percentage;
static void battery_monitor_task(void *params)
{
(void) params;
struct event evt;
bool charging = max17047_is_charging();
unsigned percentage = max17047_get_charge();
// Send an event if measurements are different
if(charging != cur_charging || percentage != cur_percentage) {
evt.type = BATTERY_STATUS;
evt.data.battery.percentage = percentage;
evt.data.battery.charging = charging;
cur_percentage = percentage;
cur_charging = charging;
xQueueSendToBack(appQueue, &evt, 0);
}
}
void battery_monitor_init(void)
{
// Set invalid values to force an update
cur_charging = false;
cur_percentage = 255;
timer_handle = xTimerCreate((signed char*) "battery_monitor",
BATTERY_TASK_PERIOD, pdTRUE,
(void*) 0, battery_monitor_task);
if(xTimerStart(timer_handle, 0) != pdPASS) {
// TODO kernel panic
}
}
/*
* 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 Battery status monitor.
*/
#ifndef BATTERY_TASK_H
#define BATTERY_TASK_H
/**
* Initializes and starts the battery monitor.
*/
void battery_monitor_init(void);
#endif /* BATTERY_TASK_H */
......@@ -39,6 +39,7 @@
#include <gfx/ui.h>
#include <apps/app_list.h>
#include "battery_task.h"
#include "blight_task.h"
#include "state.h"
......@@ -60,8 +61,10 @@ int main(void)
vibra_init();
rtc_init();
lcd_init();
ui_init();
auto_backlight_init();
battery_monitor_init();
state_init();
// Initialize SLEEP driver, no callbacks are used
......
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