Skip to content

Commit 5cc4ebd

Browse files
committed
fbtft: add low-battery icon
Signed-off-by: Vincent-FK <vincent.buso@funkey-project.com>
1 parent 1a39243 commit 5cc4ebd

5 files changed

Lines changed: 100 additions & 0 deletions

File tree

drivers/staging/fbtft/fb_text.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,37 @@
1818
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
1919
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
2020

21+
#define LOW_BATTERY_ICON_TOP_LEFT_X 10
22+
#define LOW_BATTERY_ICON_TOP_LEFT_Y 10
23+
#define LOW_BATTERY_ICON_WIDTH 35
24+
#define LOW_BATTERY_ICON_HEIGHT 20
25+
//#define LOW_BATTERY_FORE_COLOR 65535
26+
#define LOW_BATTERY_FORE_COLOR 0xF800
27+
#define LOW_BATTERY_BACK_COLOR 0
28+
29+
/* Battery icon from: https://github.com/martinohanlon/grrl-bat-monitor */
30+
static u16 lowBatteryIcon [LOW_BATTERY_ICON_HEIGHT][LOW_BATTERY_ICON_WIDTH] = {
31+
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
32+
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
33+
{0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0},
34+
{0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0},
35+
{0, 0, 1, 1, 0, 0, 0,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0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0},
45+
{0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0},
46+
{0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0},
47+
{0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0},
48+
{0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0},
49+
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
50+
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
51+
2152
static u8 monaco_font[] = {
2253

2354
/*
@@ -2750,3 +2781,26 @@ void DrawText(u16 *framebuffer, int framebufferWidth,
27502781
x += 6;
27512782
}
27522783
}
2784+
2785+
void draw_low_battery(u16 *framebuffer, int framebufferWidth, int framebufferHeight)
2786+
{
2787+
int x_offset = LOW_BATTERY_ICON_TOP_LEFT_X;
2788+
int y_offset = LOW_BATTERY_ICON_TOP_LEFT_Y;
2789+
int x, y;
2790+
2791+
/* Sanity check - width */
2792+
if (x_offset >= framebufferWidth)
2793+
x_offset = framebufferWidth-1-LOW_BATTERY_ICON_WIDTH;
2794+
2795+
/* Sanity check - height */
2796+
if (y_offset >= framebufferHeight)
2797+
y_offset = framebufferHeight-1-LOW_BATTERY_ICON_HEIGHT;
2798+
2799+
/* Printing low battery icon */
2800+
for (y = 0; y < MIN(LOW_BATTERY_ICON_HEIGHT, framebufferHeight - y_offset); ++y) {
2801+
for (x = 0; x < MIN(LOW_BATTERY_ICON_WIDTH, framebufferWidth - x_offset); ++x) {
2802+
if (lowBatteryIcon[y][x])
2803+
framebuffer[(y + y_offset) * framebufferWidth + (x + x_offset)] = LOW_BATTERY_FORE_COLOR;
2804+
}
2805+
}
2806+
}

drivers/staging/fbtft/fb_text.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
void DrawText(u16 *framebuffer, int framebufferWidth,
2727
int framebufferStrideBytes, int framebufferHeight,
2828
const char *text, int x, int y, u16 color, u16 bgColor);
29+
void draw_low_battery(u16 *framebuffer, int framebufferWidth,
30+
int framebufferHeight);
2931

3032
/*extern unsigned char fontdata8x8[64*16];
3133
extern unsigned char fontdata6x8[256-32][8];*/

drivers/staging/fbtft/fbtft-core.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,13 @@ void fbtft_post_process_screen(struct fbtft_par *par)
560560
screen_post_process = true;
561561
}
562562

563+
/* Low battery icon */
564+
if (par->low_battery) {
565+
par->write_line_start = 0;
566+
par->write_line_end = par->info->var.yres - 1;
567+
screen_post_process = true;
568+
}
569+
563570
/* Post process */
564571
if (screen_post_process) {
565572
/* Change vmem ptr to send by SPI */
@@ -583,6 +590,11 @@ void fbtft_post_process_screen(struct fbtft_par *par)
583590
par->write_line_end = y_notif + MONACO_HEIGHT;
584591
}
585592

593+
/* Low battery icon */
594+
if (par->low_battery) {
595+
draw_low_battery((u16 *)par->vmem_post_process, par->info->var.xres, par->info->var.yres);
596+
}
597+
586598
/* Soft rotation */
587599
if (par->pdata->rotate_soft)
588600
fbtft_rotate_soft((u16*)par->vmem_post_process, par->info->var.yres, par->pdata->rotate_soft);

drivers/staging/fbtft/fbtft-sysfs.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,35 @@ static ssize_t show_notification(struct device *device,
387387
static struct device_attribute notification_device_attr =
388388
__ATTR(notification, 0660, show_notification, store_notification);
389389

390+
static ssize_t store_low_battery(struct device *device,
391+
struct device_attribute *attr,
392+
const char *buf, size_t count)
393+
{
394+
struct fb_info *fb_info = dev_get_drvdata(device);
395+
struct fbtft_par *par = fb_info->par;
396+
int ret;
397+
398+
ret = kstrtoul(buf, 10, &par->low_battery);
399+
if (ret)
400+
return ret;
401+
402+
par->low_battery = par->low_battery?1:0;
403+
404+
return count;
405+
}
406+
407+
static ssize_t show_low_battery(struct device *device,
408+
struct device_attribute *attr, char *buf)
409+
{
410+
struct fb_info *fb_info = dev_get_drvdata(device);
411+
struct fbtft_par *par = fb_info->par;
412+
413+
return snprintf(buf, PAGE_SIZE, "%lu\n", par->low_battery);
414+
}
415+
416+
static struct device_attribute low_battery_device_attr =
417+
__ATTR(low_battery, 0660, show_low_battery, store_low_battery);
418+
390419
void fbtft_expand_debug_value(unsigned long *debug)
391420
{
392421
switch (*debug & 0x7) {
@@ -448,6 +477,7 @@ static struct device_attribute debug_device_attr =
448477
void fbtft_sysfs_init(struct fbtft_par *par)
449478
{
450479
device_create_file(par->info->dev, &debug_device_attr);
480+
device_create_file(par->info->dev, &low_battery_device_attr);
451481
device_create_file(par->info->dev, &rotate_soft_device_attr);
452482
device_create_file(par->info->dev, &notification_device_attr);
453483
device_create_file(par->info->dev, &overlay_device_attrs[0]);
@@ -458,6 +488,7 @@ void fbtft_sysfs_init(struct fbtft_par *par)
458488
void fbtft_sysfs_exit(struct fbtft_par *par)
459489
{
460490
device_remove_file(par->info->dev, &debug_device_attr);
491+
device_remove_file(par->info->dev, &low_battery_device_attr);
461492
device_remove_file(par->info->dev, &rotate_soft_device_attr);
462493
device_remove_file(par->info->dev, &notification_device_attr);
463494
device_remove_file(par->info->dev, &overlay_device_attrs[0]);

drivers/staging/fbtft/fbtft.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ struct fbtft_par {
253253
} overlay;
254254
char notification[FBTFT_NOTIF_MAX_SIZE+1];
255255
unsigned long debug;
256+
unsigned long low_battery;
256257
bool first_update_done;
257258
ktime_t update_time;
258259
long avg_fps;

0 commit comments

Comments
 (0)