]> git.kernelconcepts.de Git - metawatch.git/blob - metawatch.c
Implement PBM read file and display
[metawatch.git] / metawatch.c
1 /*
2  * (c) 2011 Siegen, Germany by Nils Faerber <nils.faerber@kernelconcepts.de>
3  *
4  * license LGPL
5  */
6
7 #include <stdio.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <fcntl.h>
11 #include <unistd.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <time.h>
15 #include <termios.h>
16 #include <ctype.h>
17
18 #include "metawatch_protocol.h"
19 #include "crc16ccitt.h"
20
21
22 #define MW_FRAME_DELAY          0x00
23
24 void dump_frame(unsigned char *frame, int len)
25 {
26         int i;
27
28         for (i=0; i<len; i++)
29                 fprintf(stderr, "0x%02x ", frame[i]);
30         fprintf(stderr, "\n");
31 }
32
33
34 int mw_send_frame(int mw_fd, unsigned char msg_type, unsigned char options, unsigned char *data, unsigned char len)
35 {
36         unsigned short crc;
37         unsigned char frame[64];
38         int tlen = len + 6; /* payload + 6 bytes frameing */
39         int ret;
40
41         memset(frame, 0, 64);
42         frame[0] = MW_SOF;
43         frame[1] = len + 6;
44         frame[2] = msg_type;
45         frame[3] = options;
46         if (data != NULL && len > 0)
47                 memcpy(frame+4, data, len);
48
49         crc = crc16ccitt(frame, len+4);
50         *(unsigned short *)(frame+len+4) = crc;
51
52 #ifdef DEBUG
53         dump_frame(frame, tlen);
54 #endif
55
56         while (((ret = write(mw_fd, frame, tlen)) >= 0) && (tlen > 0))
57                 tlen -= ret;
58
59         if (MW_FRAME_DELAY)
60                 usleep(MW_FRAME_DELAY);
61
62         if (tlen == 0 && ret >= 0)
63                 return 0;
64         else
65                 return ret;
66 }
67
68 /*
69  * host to watch commands
70  */
71
72 void mw_set_rtc(int mw_fd, unsigned char clk1224, unsigned char date_fmt)
73 {
74         time_t mtime;
75         struct tm mtm;
76         unsigned short year;
77         unsigned char data[32];
78
79         mtime = time(NULL);
80         localtime_r(&mtime, &mtm);
81
82         year = mtm.tm_year + 1900;  
83         data[0] = (year & 0x0f00) >> 8;
84         data[1] = (year & 0x00ff);
85         data[2] = mtm.tm_mon + 1;
86         data[3] = mtm.tm_mday;
87         data[4] = mtm.tm_wday;
88         data[5] = mtm.tm_hour;
89         data[6] = mtm.tm_min;
90         data[7] = mtm.tm_sec;
91         data[8] = clk1224;
92         data[9] = date_fmt;
93
94         mw_send_frame(mw_fd, MW_SET_REAL_TIME_CLOCK, 0, data, 10);
95 }
96
97 void mw_set_vibrate_mode(int mw_fd, unsigned char enable, unsigned short on_time, unsigned short off_time, unsigned char cycles)
98 {
99         unsigned char mdata[7];
100
101         mdata[0] = enable;
102         *(unsigned short *)(mdata+1) = on_time; /* miliseconds */
103         *(unsigned short *)(mdata+3) = off_time; /* miliseconds */
104         mdata[5] = cycles;
105         mw_send_frame(mw_fd, MW_SET_VIBRATE_MODE, 0, mdata, 6);
106 }
107
108 void mw_configure_watch_mode(int mw_fd, unsigned char mode, unsigned char save, unsigned char timeout, unsigned char invert)
109 {
110         unsigned char mdata[3];
111
112         mdata[0] = timeout; /* seconds */
113         mdata[1] = invert; /* 0=normal, 1=invert */
114         mw_send_frame(mw_fd, MW_CONFIGURE_MODE, (mode & 0x0f) | ((save & 0x01) << 4), mdata, 2);
115 }
116
117 void mw_update_display(int mw_fd, unsigned char mode, unsigned char copy)
118 {
119         mw_send_frame(mw_fd, MW_UPDATE_DISPLAY, (mode & 0x0f) | ((copy & 0x01) << 4), NULL, 0);
120 }
121
122 void mw_load_template(int mw_fd, unsigned char mode, unsigned char template_select)
123 {
124         mw_send_frame(mw_fd, MW_LOAD_TEMPLATE, (mode & 0x0f), &template_select, 1);
125 }
126
127 /*
128  * send line for screen-mode mode from *buffer to watch, starting at display row row_offset
129  */
130 void mw_write_buffer(int mw_fd,
131                 unsigned char mode,
132                 unsigned char numlines,         /* number of lines, 0=two lines or 1=one line */
133                 unsigned char row_offset,       /* start at row_offset in display, e.g. lower part in idle @31 */
134                 unsigned char *buffer, int buflen)
135 {
136         unsigned char mdata[32];
137
138         buflen = 12 * (buflen / 12);    /* crop to 12 bytes */
139         if ((numlines == 0 && buflen < 12) || (numlines == 1 && buflen < 24)) {
140                 fprintf(stderr, "mw_write_buffer: bufferlen does not match number of lines\n");
141                 return;
142         };
143         memset(mdata, 0, 32);
144         mdata[0] = row_offset;
145         memcpy((mdata+1), buffer, 12);
146         if (numlines == 0) {
147                 mdata[13] = row_offset+1;
148                 memcpy((mdata+14), (buffer+12), 12);
149         };
150         mw_send_frame(mw_fd, MW_WRITE_BUFFER, (mode & 0x0f) | (((numlines & 0x01)<< 4) & 0x10), mdata, numlines ? 13 : 26);
151 }
152
153
154 /* ------------------------------------------------------------------------ */
155
156
157 /*
158  * watch responses, events or notifications
159  */
160 void mw_get_real_time_clock_response(int mw_fd, unsigned char *rtcrsp, int len)
161 {
162         struct tm mtm;
163         unsigned short year;
164         unsigned char clk1224, date_fmt;
165
166         if (len != 10) {
167                 fprintf(stderr, "get real time clock response too short %d != 10\n", len);
168                 return;
169         }
170
171         year = *(unsigned short *)rtcrsp;
172         mtm.tm_year = year - 1900;
173         mtm.tm_mon = rtcrsp[2] - 1;
174         mtm.tm_mday = rtcrsp[3];
175         mtm.tm_wday = rtcrsp[4];
176         mtm.tm_hour = rtcrsp[5];
177         mtm.tm_min = rtcrsp[6];
178         mtm.tm_sec = rtcrsp[7];
179         clk1224 = rtcrsp[8];
180         date_fmt = rtcrsp[9];
181
182         fprintf(stderr, "watch RTC is %s, clock format is %s, date format is %s\n", asctime(&mtm), clk1224 ? "24h" : "AM/PM", date_fmt ? "DD/MM" : "MM/DD");
183 }
184
185 void mw_get_battery_voltage_response(int mw_fd, unsigned char *batrsp, int len)
186 {
187         unsigned short voltage = *(unsigned short *)batrsp;
188         unsigned char power_good = batrsp[2];
189         unsigned char bat_charging = batrsp[3];
190
191         fprintf(stderr, "battery is at %dV, %s and %s\n", voltage, power_good ? "power is good" : "power fault", bat_charging ? "charging" : "not charging");
192 }
193
194 void mw_status_change_event(int mw_fd, unsigned char option, unsigned char *statrsp, int len)
195 {
196         fprintf(stderr, "Status change event for mode %s: %s\n", mw_screen_mode_names[option&0x0f], mw_status_string[statrsp[0]]);      
197 }
198
199 /* ---------------------------------------------------------------------- */
200
201 int decode_frame(int mw_fd, unsigned char *buf, int len)
202 {
203         unsigned short crc;
204         unsigned char msglen;
205         unsigned char msgtype;
206         unsigned char msgopt;
207         unsigned char *msgdata;
208
209         /* check frame */
210         crc = *(unsigned short *)(buf+len-2);
211         if (crc != crc16ccitt(buf, len-2)) {
212                 fprintf(stderr, "decode frame CRC error\n");
213                 return 1;
214         } else
215                 fprintf(stderr, "decode frame CRC OK\n");
216         if (buf[0] != MW_SOF) {
217                 fprintf(stderr, "decode frame SOF not found\n");
218                 return 1;
219         } else
220         fprintf(stderr, "decode frame found SOF\n");
221
222         msglen = buf[1];
223         msgtype = buf[2];
224         msgopt = buf[3];
225         msgdata = (buf+4);
226
227         switch (msgtype) {
228                 case MW_GET_REAL_TIME_CLOCK_RSP:
229                         mw_get_real_time_clock_response(mw_fd, msgdata, len-4-2);
230                         break;
231                 case MW_GET_INFORMATION_STRING_RSP:
232                         msgdata[len-4-2] = 0;
233                         fprintf(stderr, "Got info string '%s'\n", (msgdata+4));
234                         break;
235                 case MW_READ_BATTERY_VOLTAGE_RSP:
236                         mw_get_battery_voltage_response(mw_fd, msgdata, len-4-2);
237                         break;
238                 case MW_LOW_BATTERY_WARNING_MSG:
239                         fprintf(stderr, "Watch battery low, please connect charger\n");
240                         break;
241                 case MW_LOW_BATTERY_BT_OFF_MSG:
242                         fprintf(stderr, "Watch battery extremely low - radio will turn off\n");
243                         break;
244                 case MW_STATUS_CHANGE_EVENT:
245                         mw_status_change_event(mw_fd, msgopt, msgdata, len-4-2);
246                         break;
247                 default:
248                         fprintf(stderr, "Unkown msgtype 0x%02x\n", msgtype);
249                         break;
250         };
251
252         return 0;
253 }
254
255
256 void bitmap_test(int mw_fd)
257 {
258         /* a nice checker-board pattern */
259         unsigned char checkbuf[24] = {
260                 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
261                 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
262                 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
263                 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
264         };
265         mw_write_buffer(mw_fd, MW_SCREEN_MODE_IDLE, 0, 31, checkbuf, 24);
266         mw_write_buffer(mw_fd, MW_SCREEN_MODE_IDLE, 0, 33, checkbuf, 24);
267         mw_write_buffer(mw_fd, MW_SCREEN_MODE_IDLE, 0, 35, checkbuf, 24);
268         mw_write_buffer(mw_fd, MW_SCREEN_MODE_IDLE, 0, 37, checkbuf, 24);
269
270         mw_update_display(mw_fd, MW_SCREEN_MODE_IDLE, 1);
271 }
272
273
274 void flip_buffer_bytes(unsigned char invert, unsigned char *buf, int len)
275 {
276         int i;
277         unsigned char tmp;
278
279         while (len--) {
280                 tmp = 0;
281                 for (i=0; i<8; i++)
282                         tmp |= ((*buf & (1<<i)) >> i) << (7-i);
283                 // fprintf(stderr, "0x%02x -> 0x%02x\n", *buf, tmp);
284                 *buf = invert ? ~tmp : tmp;
285                 buf++;
286         }
287 }
288
289 void bitmap_read(int mw_fd)
290 {
291         int ffd, ret;
292         char rbuf[256];
293         unsigned int width, height, i, x, y;
294         unsigned int rowlength;
295         unsigned char *bmapbuf;
296         unsigned char mw_buf[24];
297
298         ffd = open("test.pbm", O_RDONLY);
299         if (ffd < 0) {
300                 perror("open");
301                 return;
302         };
303         ret = read(ffd, rbuf, 3);
304         if (rbuf[0] != 'P' || rbuf[1] != '4') {
305                 fprintf(stderr, "not a PBM file\n");
306                 return;
307         }
308         memset(rbuf, 0, 256);
309         i = 0;
310         do {
311                 ret = read(ffd, (rbuf+i), 1);
312         } while (!isspace(rbuf[i++]));
313         width = atoi(rbuf);
314
315         memset(rbuf, 0, 256);
316         i = 0;
317         do {
318                 ret = read(ffd, (rbuf+i), 1);
319         } while (!isspace(rbuf[i++]));
320         height = atoi(rbuf);
321
322         fprintf(stderr, "bitmap resolution is %d x %d\n", width, height);
323
324         rowlength = ((width / 8) + 1);
325         fprintf(stderr, "row length = %d bytes\n", rowlength);
326
327         bmapbuf = malloc(rowlength * height);
328
329         ret = read(ffd, bmapbuf, rowlength * height);
330         close(ffd);
331         fprintf(stderr, "read %d of %d bytes\n", ret, rowlength * height);
332
333 #if 0
334         fprintf(stderr, "\n");
335         for (y=0; y<height; y++) {
336                 for (x=0; x<rowlength; x++) {
337                         for (i=0; i<8; i++)
338                                 fprintf(stderr, "%c", (bmapbuf[(y*rowlength)+x] & (1<<(7-i))) ? '.' : ' ');
339                 }
340                 fprintf(stderr, "\n");
341         }
342         fprintf(stderr, "\n");
343 #endif
344
345         flip_buffer_bytes(1, bmapbuf, rowlength * height);
346
347         for (y=0; y<height; y+=2) {
348                 memset(mw_buf, 0, 24);
349                 memcpy(mw_buf, (bmapbuf+(y*rowlength)), (rowlength > 12) ? 12 : rowlength);
350                 memcpy((mw_buf+12), (bmapbuf+((y+1)*rowlength)), (rowlength > 12) ? 12 : rowlength);
351                 mw_write_buffer(mw_fd, MW_SCREEN_MODE_IDLE, 0, 31+y, mw_buf, 24);
352         }
353         mw_update_display(mw_fd, MW_SCREEN_MODE_IDLE, 1);
354
355         free(bmapbuf);
356 }
357
358
359 void process_cmd(char *cmdline, int clinep, int mw_fd)
360 {
361         unsigned char mdata[32];
362
363         fprintf(stderr, "command: '%s'\n", cmdline);
364
365         if (strncmp(cmdline, "quit", 4) == 0) {
366                 close(mw_fd);
367                 exit(0);
368         }
369
370         if (strncmp(cmdline, "srtc", 4) == 0) {
371                 fprintf(stderr, "Setting RTC from system time...");
372                 mw_set_rtc(mw_fd, MW_RTC_CLOCK_24HR, MW_RTC_DATE_DDMM);
373                 fprintf(stderr, "OK\n");
374         }
375         if (strncmp(cmdline, "grtc", 4) == 0) {
376                 mw_send_frame(mw_fd, MW_GET_REAL_TIME_CLOCK, 0, NULL, 0);
377         }
378         if (strncmp(cmdline, "gistr", 5) == 0) {
379                 mdata[0] = 0;
380                 mw_send_frame(mw_fd, MW_GET_INFORMATION_STRING, 0, mdata, 1);
381         }
382         if (strncmp(cmdline, "gdtype", 6) == 0) {
383                 mw_send_frame(mw_fd, MW_GET_DEVICE_TYPE, 0, NULL, 0);
384         }
385         if (strncmp(cmdline, "rvbat", 5) == 0) {
386                 mw_send_frame(mw_fd, MW_READ_BATTERY_VOLTAGE_MSG, 0, NULL, 0);
387         }
388         if (strncmp(cmdline, "modecfg", 6) == 0) {
389                 mw_configure_watch_mode(mw_fd, MW_SCREEN_MODE_IDLE, 0, 4, 1);
390                 mw_update_display(mw_fd, MW_SCREEN_MODE_IDLE, 0);
391         }
392         if (strncmp(cmdline, "rbtcfg", 6) == 0) {
393                 mdata[0] = 0; /* idle screen */
394                 mdata[1] = 1; /* button index */
395                 mdata[2] = 2; /* button press type */
396                 mdata[3] = 3; /* callback message type */
397                 mdata[4] = 4; /* callback message option */
398                 mw_send_frame(mw_fd, MW_READ_BUTTON_CONFIG, 0, NULL, 0);
399         }
400         if (strncmp(cmdline, "svib", 4) == 0) {
401                 mw_set_vibrate_mode(mw_fd, 1, 300, 300, 5);
402         }
403         if (strncmp(cmdline, "tbmp", 4) == 0) {
404                 bitmap_test(mw_fd);
405         }
406         if (strncmp(cmdline, "rbmp", 4) == 0) {
407                 bitmap_read(mw_fd);
408         }
409 }
410
411
412 int menu(int mw_fd)
413 {
414         fd_set mfds;
415         struct termios tconfd;
416         char cmdline[128];
417         unsigned char msg_buf[64];
418         unsigned char clinep = 0;
419         int rcvd;
420
421         tcgetattr(0, &tconfd);
422         cfmakeraw(&tconfd);
423         tconfd.c_oflag |= ONLCR | OPOST;
424         tconfd.c_lflag |= ISIG;
425         tcsetattr(0, TCSANOW, &tconfd);
426
427         FD_ZERO(&mfds);
428         FD_SET(0, &mfds);
429         FD_SET(mw_fd, &mfds);
430
431         memset(cmdline, 0, 128);
432
433         do {
434                 rcvd = 0;
435                 if (select(mw_fd+1, &mfds, NULL, NULL, NULL) > 0) {
436                         if (FD_ISSET(mw_fd, &mfds)) {
437                                 rcvd = read(mw_fd, msg_buf, 64);
438                                 fprintf(stderr, "read %d bytes:\n", rcvd);
439                                 if (rcvd > 0) {
440                                         dump_frame(msg_buf, rcvd);
441                                         decode_frame(mw_fd, msg_buf, rcvd);
442                                 }
443                         };
444                         if (FD_ISSET(0, &mfds)) {
445                                 rcvd = read(0, (cmdline+clinep), 1);
446                                 if (rcvd > 0) {
447                                         if (cmdline[clinep] == '\r') {
448                                                 printf("\n");
449                                                 cmdline[clinep--] = '\0';
450                                                 process_cmd(cmdline, clinep, mw_fd);
451                                                 clinep = 0;
452                                                 memset(cmdline, 0, 128);
453                                         } else {
454                                                 clinep++;
455                                                 if (clinep > 75)
456                                                         clinep = 75;
457                                                 printf("\r> %s", cmdline);
458                                                 fflush(stdout);
459                                         }
460                                 }
461                         };
462                 } else
463                         break;
464                 FD_ZERO(&mfds);
465                 FD_SET(0, &mfds);
466                 FD_SET(mw_fd, &mfds);
467         } while (rcvd > 0);
468
469         return 0;
470 }
471
472 int main(int argc, char **argv)
473 {
474         int mw_fd;
475
476         if (argc != 2) {
477                 fprintf(stderr, "Usage:\n\t%s <devicename>\n", argv[0]);
478                 return 1;
479         };
480   
481         crc16ccitt_init();
482
483         mw_fd = open(argv[1], O_RDWR);
484         if (mw_fd < 0) {
485                 perror("open");
486                 return 1;
487         };
488
489         menu(mw_fd);
490
491         fsync(mw_fd);
492
493         close(mw_fd);
494
495         return 0;
496 };
497