Reworked the bitmap functions to be of more general use

This commit is contained in:
Nils Faerber 2011-07-24 17:27:28 +02:00
parent fc01ed38c0
commit b42900c7bd
3 changed files with 54 additions and 228 deletions

View file

@ -1,11 +1,11 @@
# Copyright (C) 2011 Nils Faerber <nils.faerber@kernelconcepts.de>
# prefix for installation and search path (like icons)
PREFIX = /usr/local/
CFLAGS = -Wall -O2 $(CCFLAGS)
CFLAGS = -DDEBUG -Wall -O2 $(CCFLAGS)
PRGNAME = metawatch
MEMBERS = metawatch crc16ccitt
MEMBERS = metawatch crc16ccitt mw_main
# no need to change anything below this line
# ------------------------------------------

View file

@ -15,10 +15,27 @@
#include <termios.h>
#include <ctype.h>
#include "metawatch.h"
#include "metawatch_protocol.h"
#include "crc16ccitt.h"
#ifdef DEBUG
const char *mw_screen_mode_names[] = {
"idle screen",
"application screen",
"notification screen",
"scroll"
};
const char *mw_status_string[] = {
"Reserved",
"Mode Change",
"Display Timeout"
};
#endif
#define MW_FRAME_DELAY 0x00
void dump_frame(unsigned char *frame, int len)
@ -65,9 +82,11 @@ int mw_send_frame(int mw_fd, unsigned char msg_type, unsigned char options, unsi
return ret;
}
/*
* host to watch commands
*/
/* ----------------------------------------------------------------------
* Host to watch commands
* ---------------------------------------------------------------------- */
void mw_set_rtc(int mw_fd, unsigned char clk1224, unsigned char date_fmt)
{
@ -151,12 +170,11 @@ void mw_write_buffer(int mw_fd,
}
/* ------------------------------------------------------------------------ */
/* ----------------------------------------------------------------------
* Watch responses, events or notifications
* ---------------------------------------------------------------------- */
/*
* watch responses, events or notifications
*/
void mw_get_real_time_clock_response(int mw_fd, unsigned char *rtcrsp, int len)
{
struct tm mtm;
@ -196,7 +214,11 @@ void mw_status_change_event(int mw_fd, unsigned char option, unsigned char *stat
fprintf(stderr, "Status change event for mode %s: %s\n", mw_screen_mode_names[option&0x0f], mw_status_string[statrsp[0]]);
}
/* ---------------------------------------------------------------------- */
/* ----------------------------------------------------------------------
* Protocol handling
* ---------------------------------------------------------------------- */
int decode_frame(int mw_fd, unsigned char *buf, int len)
{
@ -253,84 +275,46 @@ int decode_frame(int mw_fd, unsigned char *buf, int len)
}
void bitmap_test(int mw_fd)
{
/* a nice checker-board pattern */
unsigned char checkbuf[24] = {
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
};
mw_write_buffer(mw_fd, MW_SCREEN_MODE_IDLE, 0, 31, checkbuf, 24);
mw_write_buffer(mw_fd, MW_SCREEN_MODE_IDLE, 0, 33, checkbuf, 24);
mw_write_buffer(mw_fd, MW_SCREEN_MODE_IDLE, 0, 35, checkbuf, 24);
mw_write_buffer(mw_fd, MW_SCREEN_MODE_IDLE, 0, 37, checkbuf, 24);
/* ----------------------------------------------------------------------
* Convenience functions not strictly part of the protocol
* ---------------------------------------------------------------------- */
mw_update_display(mw_fd, MW_SCREEN_MODE_IDLE, 1);
}
void flip_buffer_bytes(unsigned char invert, unsigned char *buf, int len)
/* if flip=1 bits in each byte are inverted 7->1, 6->2, 5->3,...
if invert=1 each byte is inverted
*/
void bmap_buffer_flipinvert(unsigned char flip, unsigned char invert, unsigned char *buf, int len)
{
int i;
unsigned char tmp;
while (len--) {
tmp = 0;
for (i=0; i<8; i++)
tmp |= ((*buf & (1<<i)) >> i) << (7-i);
// fprintf(stderr, "0x%02x -> 0x%02x\n", *buf, tmp);
if (flip) {
for (i=0; i<8; i++)
tmp |= ((*buf & (1<<i)) >> i) << (7-i);
// fprintf(stderr, "0x%02x -> 0x%02x\n", *buf, tmp);
} else
tmp = *buf;
*buf = invert ? ~tmp : tmp;
buf++;
}
}
void bitmap_read(int mw_fd)
void mw_send_bitmap(int mw_fd, unsigned char mode, int width, int height, int offset, unsigned char *bmapbuf, int buflen)
{
int ffd, ret;
char rbuf[256];
unsigned int width, height, i, x, y;
unsigned int i, x, y;
unsigned int rowlength;
unsigned char *bmapbuf;
unsigned char mw_buf[24];
ffd = open("test.pbm", O_RDONLY);
if (ffd < 0) {
perror("open");
return;
};
ret = read(ffd, rbuf, 3);
if (rbuf[0] != 'P' || rbuf[1] != '4') {
fprintf(stderr, "not a PBM file\n");
return;
}
memset(rbuf, 0, 256);
i = 0;
do {
ret = read(ffd, (rbuf+i), 1);
} while (!isspace(rbuf[i++]));
width = atoi(rbuf);
memset(rbuf, 0, 256);
i = 0;
do {
ret = read(ffd, (rbuf+i), 1);
} while (!isspace(rbuf[i++]));
height = atoi(rbuf);
fprintf(stderr, "bitmap resolution is %d x %d\n", width, height);
rowlength = ((width / 8) + 1);
if ((height + offset) > 96)
height = 96 - offset;
#ifdef DEBUG
fprintf(stderr, "row length = %d bytes\n", rowlength);
bmapbuf = malloc(rowlength * height);
ret = read(ffd, bmapbuf, rowlength * height);
close(ffd);
fprintf(stderr, "read %d of %d bytes\n", ret, rowlength * height);
#if 0
fprintf(stderr, "bitmap resolution is %d x %d\n", width, height);
fprintf(stderr, "\n");
for (y=0; y<height; y++) {
for (x=0; x<rowlength; x++) {
@ -342,156 +326,11 @@ void bitmap_read(int mw_fd)
fprintf(stderr, "\n");
#endif
flip_buffer_bytes(1, bmapbuf, rowlength * height);
for (y=0; y<height; y+=2) {
memset(mw_buf, 0, 24);
memcpy(mw_buf, (bmapbuf+(y*rowlength)), (rowlength > 12) ? 12 : rowlength);
memcpy((mw_buf+12), (bmapbuf+((y+1)*rowlength)), (rowlength > 12) ? 12 : rowlength);
mw_write_buffer(mw_fd, MW_SCREEN_MODE_IDLE, 0, 31+y, mw_buf, 24);
}
mw_update_display(mw_fd, MW_SCREEN_MODE_IDLE, 1);
free(bmapbuf);
}
void process_cmd(char *cmdline, int clinep, int mw_fd)
{
unsigned char mdata[32];
fprintf(stderr, "command: '%s'\n", cmdline);
if (strncmp(cmdline, "quit", 4) == 0) {
close(mw_fd);
exit(0);
}
if (strncmp(cmdline, "srtc", 4) == 0) {
fprintf(stderr, "Setting RTC from system time...");
mw_set_rtc(mw_fd, MW_RTC_CLOCK_24HR, MW_RTC_DATE_DDMM);
fprintf(stderr, "OK\n");
}
if (strncmp(cmdline, "grtc", 4) == 0) {
mw_send_frame(mw_fd, MW_GET_REAL_TIME_CLOCK, 0, NULL, 0);
}
if (strncmp(cmdline, "gistr", 5) == 0) {
mdata[0] = 0;
mw_send_frame(mw_fd, MW_GET_INFORMATION_STRING, 0, mdata, 1);
}
if (strncmp(cmdline, "gdtype", 6) == 0) {
mw_send_frame(mw_fd, MW_GET_DEVICE_TYPE, 0, NULL, 0);
}
if (strncmp(cmdline, "rvbat", 5) == 0) {
mw_send_frame(mw_fd, MW_READ_BATTERY_VOLTAGE_MSG, 0, NULL, 0);
}
if (strncmp(cmdline, "modecfg", 6) == 0) {
mw_configure_watch_mode(mw_fd, MW_SCREEN_MODE_IDLE, 0, 4, 1);
mw_update_display(mw_fd, MW_SCREEN_MODE_IDLE, 0);
}
if (strncmp(cmdline, "rbtcfg", 6) == 0) {
mdata[0] = 0; /* idle screen */
mdata[1] = 1; /* button index */
mdata[2] = 2; /* button press type */
mdata[3] = 3; /* callback message type */
mdata[4] = 4; /* callback message option */
mw_send_frame(mw_fd, MW_READ_BUTTON_CONFIG, 0, NULL, 0);
}
if (strncmp(cmdline, "svib", 4) == 0) {
mw_set_vibrate_mode(mw_fd, 1, 300, 300, 5);
}
if (strncmp(cmdline, "tbmp", 4) == 0) {
bitmap_test(mw_fd);
}
if (strncmp(cmdline, "rbmp", 4) == 0) {
bitmap_read(mw_fd);
mw_write_buffer(mw_fd, mode, 0, 31+y, mw_buf, 24);
}
}
int menu(int mw_fd)
{
fd_set mfds;
struct termios tconfd;
char cmdline[128];
unsigned char msg_buf[64];
unsigned char clinep = 0;
int rcvd;
tcgetattr(0, &tconfd);
cfmakeraw(&tconfd);
tconfd.c_oflag |= ONLCR | OPOST;
tconfd.c_lflag |= ISIG;
tcsetattr(0, TCSANOW, &tconfd);
FD_ZERO(&mfds);
FD_SET(0, &mfds);
FD_SET(mw_fd, &mfds);
memset(cmdline, 0, 128);
do {
rcvd = 0;
if (select(mw_fd+1, &mfds, NULL, NULL, NULL) > 0) {
if (FD_ISSET(mw_fd, &mfds)) {
rcvd = read(mw_fd, msg_buf, 64);
fprintf(stderr, "read %d bytes:\n", rcvd);
if (rcvd > 0) {
dump_frame(msg_buf, rcvd);
decode_frame(mw_fd, msg_buf, rcvd);
}
};
if (FD_ISSET(0, &mfds)) {
rcvd = read(0, (cmdline+clinep), 1);
if (rcvd > 0) {
if (cmdline[clinep] == '\r') {
printf("\n");
cmdline[clinep--] = '\0';
process_cmd(cmdline, clinep, mw_fd);
clinep = 0;
memset(cmdline, 0, 128);
} else {
clinep++;
if (clinep > 75)
clinep = 75;
printf("\r> %s", cmdline);
fflush(stdout);
}
}
};
} else
break;
FD_ZERO(&mfds);
FD_SET(0, &mfds);
FD_SET(mw_fd, &mfds);
} while (rcvd > 0);
return 0;
}
int main(int argc, char **argv)
{
int mw_fd;
if (argc != 2) {
fprintf(stderr, "Usage:\n\t%s <devicename>\n", argv[0]);
return 1;
};
crc16ccitt_init();
mw_fd = open(argv[1], O_RDWR);
if (mw_fd < 0) {
perror("open");
return 1;
};
menu(mw_fd);
fsync(mw_fd);
close(mw_fd);
return 0;
};

View file

@ -56,19 +56,6 @@
#define MW_SCREEN_MODE_NOTIFICATION 0x02
#define MW_SCREEN_MODE_SCROLL 0x03
const char *mw_screen_mode_names[] = {
"idle screen",
"application screen",
"notification screen",
"scroll"
};
const char *mw_status_string[] = {
"Reserved",
"Mode Change",
"Display Timeout"
};
#endif