]> git.kernelconcepts.de Git - metawatch.git/blob - metawatch.c
Add button message callback handling and sample code
[metawatch.git] / metawatch.c
1 /*
2  * (c) 2011 Siegen, Germany by Nils Faerber <nils.faerber@kernelconcepts.de>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include <stdio.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <fcntl.h>
24 #include <unistd.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <time.h>
28 #include <termios.h>
29 #include <ctype.h>
30
31 #include "metawatch.h"
32 #include "metawatch_protocol.h"
33 #include "crc16ccitt.h"
34
35
36 #ifdef DEBUG
37 const char *mw_screen_mode_names[] = {
38         "idle screen",
39         "application screen",
40         "notification screen",
41         "scroll"
42 };
43
44 const char *mw_status_string[] = {
45         "Reserved",
46         "Mode Change",
47         "Display Timeout"
48 };
49 #endif
50
51
52 #define MW_FRAME_DELAY          0x00
53
54 /* ----------------------------------------------------------------------
55  * Debugging helpers
56  * ---------------------------------------------------------------------- */
57
58 void dump_frame(unsigned char *frame, int len)
59 {
60         int i;
61
62         for (i=0; i<len; i++)
63                 fprintf(stderr, "0x%02x ", frame[i]);
64         fprintf(stderr, "\n");
65 }
66
67
68 int mw_send_frame(mwdevice_t *mwdevice, unsigned char msg_type, unsigned char options, unsigned char *data, unsigned char len)
69 {
70         unsigned short crc;
71         unsigned char frame[64];
72         int tlen = len + 6; /* payload + 6 bytes frameing */
73         int ret;
74
75         memset(frame, 0, 64);
76         frame[0] = MW_SOF;
77         frame[1] = len + 6;
78         frame[2] = msg_type;
79         frame[3] = options;
80         if (data != NULL && len > 0)
81                 memcpy(frame+4, data, len);
82
83         crc = crc16ccitt(frame, len+4);
84         *(unsigned short *)(frame+len+4) = crc;
85
86 #ifdef DEBUG
87         dump_frame(frame, tlen);
88 #endif
89
90         while (((ret = write(mwdevice->mw_fd, frame, tlen)) >= 0) && (tlen > 0))
91                 tlen -= ret;
92
93         if (MW_FRAME_DELAY)
94                 usleep(MW_FRAME_DELAY);
95
96         if (tlen == 0 && ret >= 0)
97                 return 0;
98         else
99                 return ret;
100 }
101
102
103 /* ----------------------------------------------------------------------
104  * Host to watch commands
105  * ---------------------------------------------------------------------- */
106
107
108 int mw_set_rtc(mwdevice_t *mwdevice, unsigned char clk1224, unsigned char date_fmt)
109 {
110         time_t mtime;
111         struct tm mtm;
112         unsigned short year;
113         unsigned char data[32];
114
115         mtime = time(NULL);
116         localtime_r(&mtime, &mtm);
117
118         year = mtm.tm_year + 1900;  
119         data[0] = (year & 0x0f00) >> 8;
120         data[1] = (year & 0x00ff);
121         data[2] = mtm.tm_mon + 1;
122         data[3] = mtm.tm_mday;
123         data[4] = mtm.tm_wday;
124         data[5] = mtm.tm_hour;
125         data[6] = mtm.tm_min;
126         data[7] = mtm.tm_sec;
127         data[8] = clk1224;
128         data[9] = date_fmt;
129
130         return mw_send_frame(mwdevice, MW_SET_REAL_TIME_CLOCK, 0, data, 10);
131 }
132
133 int mw_nval_operation(mwdevice_t *mwdevice, unsigned char operation, unsigned short identifier, unsigned char size, void *mdata)
134 {
135         unsigned char data[32];
136
137         data[0] = (identifier & 0xff00) >> 8;
138         data[1] = (identifier & 0x00ff);
139         data[2] = size;
140         if (size > 0 && mdata != NULL)
141                 memcpy((data+3), mdata, size);
142
143         if (operation == MW_NVAL_OPERATION_READ || operation == MW_NVAL_OPERATION_WRITE) {
144                 return mw_send_frame(mwdevice, MW_NVAL_OPERATION, operation, data, 3+size);
145         } else
146                 return -1;
147 }
148
149 int mw_set_vibrate_mode(mwdevice_t *mwdevice, unsigned char enable, unsigned short on_time, unsigned short off_time, unsigned char cycles)
150 {
151         unsigned char mdata[7];
152
153         mdata[0] = enable;
154         *(unsigned short *)(mdata+1) = on_time; /* miliseconds */
155         *(unsigned short *)(mdata+3) = off_time; /* miliseconds */
156         mdata[5] = cycles;
157
158         return mw_send_frame(mwdevice, MW_SET_VIBRATE_MODE, 0, mdata, 6);
159 }
160
161 int mw_configure_watch_mode(mwdevice_t *mwdevice, unsigned char mode, unsigned char save, unsigned char timeout, unsigned char invert)
162 {
163         unsigned char mdata[3];
164
165         mdata[0] = timeout; /* seconds */
166         mdata[1] = invert; /* 0=normal, 1=invert */
167
168         return mw_send_frame(mwdevice, MW_CONFIGURE_MODE, (mode & 0x0f) | ((save & 0x01) << 4), mdata, 2);
169 }
170
171 int mw_update_display(mwdevice_t *mwdevice, unsigned char mode, unsigned char copy)
172 {
173         mwdevice->screen_mode = (mode & 0x0f);
174         return mw_send_frame(mwdevice, MW_UPDATE_DISPLAY, (mode & 0x0f) | ((copy & 0x01) << 4), NULL, 0);
175 }
176
177 int mw_load_template(mwdevice_t *mwdevice, unsigned char mode, unsigned char template_select)
178 {
179         return mw_send_frame(mwdevice, MW_LOAD_TEMPLATE, (mode & 0x0f), &template_select, 1);
180 }
181
182 /*
183  * send line for screen-mode mode from *buffer to watch, starting at display row row_offset
184  * this is only for digital LCD watch
185  */
186 int mw_write_buffer(mwdevice_t *mwdevice,
187                 unsigned char mode,
188                 unsigned char numlines,         /* number of lines, 0=two lines or 1=one line */
189                 unsigned char row_offset,       /* start at row_offset in display, e.g. lower part in idle @31 */
190                 unsigned char *buffer, int buflen)
191 {
192         unsigned char mdata[32];
193
194         if (mwdevice->devtype != MW_DEVICE_TYPE_DIGITAL && mwdevice->devtype != MW_DEVICE_TYPE_DEVB_DIGI)
195                 return -1;
196
197         buflen = 12 * (buflen / 12);    /* crop to 12 bytes */
198         if ((numlines == 0 && buflen < 12) || (numlines == 1 && buflen < 24)) {
199                 fprintf(stderr, "mw_write_buffer: bufferlen does not match number of lines\n");
200                 return -1;
201         };
202         memset(mdata, 0, 32);
203         mdata[0] = row_offset;
204         memcpy((mdata+1), buffer, 12);
205         if (numlines == 0) {
206                 mdata[13] = row_offset+1;
207                 memcpy((mdata+14), (buffer+12), 12);
208         };
209
210         return mw_send_frame(mwdevice, MW_WRITE_BUFFER, (mode & 0x0f) | (((numlines & 0x01)<< 4) & 0x10), mdata, numlines ? 13 : 26);
211 }
212
213 /*
214    Options:
215         B0   : row select, 0 first row, 1 second row
216         B1   : display select, 0 upper OLED, 1 lower OLED
217         B2   : if 1 send an event upon completion
218         B3..4: scroll type,
219         B5..7: unused
220    Scroll types:
221         B0: First buffer in a chain of scroll buffers
222         B1: Any buffer except first or last
223         B2: Last buffer of a chain
224         B3: reserved / unused
225    Payload:
226         0: Start index col
227         1..: data
228 */
229 int mw_write_oled_buffer(mwdevice_t *mwdevice,
230         unsigned char mode, /* idle or scroll */
231         unsigned char oled, /* which OLED */
232         unsigned char numcols,
233         unsigned char col_index, /* starting index */
234         unsigned char *buffer, int buflen)
235 {
236         unsigned char mdata[32];
237         int i;
238
239         if (mwdevice->devtype != MW_DEVICE_TYPE_ANA_DIGI && mwdevice->devtype != MW_DEVICE_TYPE_DEVB_ANA_DIGI)
240                 return -1;
241         
242         fprintf(stderr, "write oled buf len = %d\n", buflen);
243         /* lower row first since display wil be updated after completion of upper row */
244         if (buflen > 80) {
245                 for (i=80; i<buflen; i+=20) {
246                         mdata[0] = (i-80);
247                         memcpy((mdata+1), (buffer+i), 20);
248                         mw_send_frame(mwdevice, MW_WRITE_OLED_IDLE_DISPLAY_MSG, 0 | (oled ? 2 : 0), mdata, 21);
249                 }
250         }
251         for (i=0; i<80; i+=20) {
252                 mdata[0] = i;
253                 memcpy((mdata+1), (buffer+i), 20);
254                 mw_send_frame(mwdevice, MW_WRITE_OLED_IDLE_DISPLAY_MSG, 1 | (oled ? 2 : 0), mdata, 21);
255         }
256
257         return 0;
258 }
259
260 int mw_enable_button(mwdevice_t *mwdevice,
261                 unsigned char mode,
262                 unsigned char button_index,
263                 unsigned char press_type,
264                 unsigned char callback_type,
265                 unsigned char callback_option)
266 {
267         unsigned char mdata[32];
268
269         memset(mdata, 0, 32);
270         mdata[0] = mode;
271         mdata[1] = button_index;
272         mdata[2] = press_type;
273         mdata[3] = callback_type;
274         mdata[4] = callback_option;
275
276         return mw_send_frame(mwdevice, MW_ENABLE_BUTTON, 0, mdata, 5);
277 }
278
279 int mw_disable_button(mwdevice_t *mwdevice,
280                 unsigned char mode,
281                 unsigned char button_index,
282                 unsigned char press_type)
283 {
284         unsigned char mdata[32];
285
286         memset(mdata, 0, 32);
287         mdata[0] = mode;
288         mdata[1] = button_index;
289         mdata[2] = press_type;
290
291         return mw_send_frame(mwdevice, MW_ENABLE_BUTTON, 0, mdata, 3);
292 }
293
294 int mw_advance_watch_hands(mwdevice_t *mwdevice, unsigned char hours, unsigned char minutes, unsigned char seconds)
295 {
296         unsigned char mdata[4];
297         
298         if (hours > 12)
299                 return -1;
300         if (minutes > 60)
301                 return -1;
302         if (seconds > 60)
303                 return -1;
304
305         mdata[0] = hours;
306         mdata[1] = minutes;
307         mdata[2] = seconds;
308
309         return mw_send_frame(mwdevice, MW_ADVANCE_WATCH_HANDS, 0, mdata, 3);
310 }
311
312
313 /* ----------------------------------------------------------------------
314  * Watch responses, events or notifications
315  * ---------------------------------------------------------------------- */
316
317 int mw_get_device_type_response(mwdevice_t *mwdevice, unsigned char devtype)
318 {
319 #ifdef DEBUG
320         fprintf(stderr, "Got device type ");
321         switch(devtype) {
322                 case 0:
323                         fprintf(stderr, "Reserved\n");
324                         break;
325                 case 1:
326                         fprintf(stderr, "Ana-Digi\n");
327                         break;
328                 case 2:
329                         fprintf(stderr, "Digital\n");
330                         break;
331                 case 3:
332                         fprintf(stderr, "Development Board Digital\n");
333                         break;
334                 case 4:
335                         fprintf(stderr, "Development Board Ana-Digi\n");
336                         break;
337                 default:
338                         fprintf(stderr, "unknown %d\n", devtype);
339                         break;
340         };
341 #endif
342         mwdevice->devtype = devtype;
343         if (mwdevice->mw_get_device_type_response_cb != NULL)
344                 mwdevice->mw_get_device_type_response_cb(mwdevice, devtype, mwdevice->mw_gdtypersp_data);
345         return 0;
346 }
347
348 void mw_set_get_device_type_response_cb(mwdevice_t *mwdevice, void (*mw_get_device_type_response_cb) (mwdevice_t *mwdevice, unsigned char devtype, void *user_data), void *user_data)
349 {
350         if (mw_get_device_type_response_cb != NULL)
351                 mwdevice->mw_get_device_type_response_cb = mw_get_device_type_response_cb;
352         if (user_data != NULL)
353                 mwdevice->mw_gdtypersp_data = user_data;
354 }
355
356 int mw_nval_operation_response(mwdevice_t *mwdevice, unsigned char operation, unsigned short identifier, unsigned char size, void *mdata)
357 {
358         return -1;
359 }
360
361 int mw_get_real_time_clock_response(mwdevice_t *mwdevice, unsigned char *rtcrsp, int len)
362 {
363         struct tm mtm;
364         unsigned short year;
365         unsigned char clk1224, date_fmt;
366
367         if (len != 10) {
368                 fprintf(stderr, "get real time clock response length wrong %d != 10\n", len);
369                 return -1;
370         }
371
372         year = *(unsigned short *)rtcrsp;
373         mtm.tm_year = year - 1900;
374         mtm.tm_mon = rtcrsp[2] - 1;
375         mtm.tm_mday = rtcrsp[3];
376         mtm.tm_wday = rtcrsp[4];
377         mtm.tm_hour = rtcrsp[5];
378         mtm.tm_min = rtcrsp[6];
379         mtm.tm_sec = rtcrsp[7];
380         clk1224 = rtcrsp[8];
381         date_fmt = rtcrsp[9];
382
383 #ifdef DEBUG
384         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");
385 #endif
386         if (mwdevice->mw_get_real_time_clock_response_cb != NULL)
387                 mwdevice->mw_get_real_time_clock_response_cb(mwdevice, &mtm, mwdevice->mw_grtcrsp_data);
388
389         return 0;
390 }
391
392 void mw_set_get_real_time_clock_response_cb(mwdevice_t *mwdevice, void (*mw_get_real_time_clock_response_cb) (mwdevice_t *mwdevice, struct tm *mw_tm, void *user_data), void *user_data)
393 {
394         if (mw_get_real_time_clock_response_cb != NULL)
395                 mwdevice->mw_get_real_time_clock_response_cb = mw_get_real_time_clock_response_cb;
396         if (user_data != NULL)
397                 mwdevice->mw_grtcrsp_data = user_data;
398 }
399
400
401 int mw_get_battery_voltage_response(mwdevice_t *mwdevice, unsigned char *batrsp, int len)
402 {
403         unsigned short voltage = *(unsigned short *)batrsp;
404         unsigned char power_good = batrsp[2];
405         unsigned char bat_charging = batrsp[3];
406
407 #ifdef DEBUG
408         fprintf(stderr, "battery is at %dmV, %s and %s\n", voltage, power_good ? "power is good" : "power fault", bat_charging ? "charging" : "not charging");
409 #endif
410
411         if (mwdevice->mw_get_battery_voltage_response_cb != NULL)
412                 mwdevice->mw_get_battery_voltage_response_cb(mwdevice, &voltage, &power_good, &bat_charging, mwdevice->mw_gbatvrsp_data);
413
414         return 0;
415 }
416
417 void mw_set_get_battery_voltage_response_cb(mwdevice_t *mwdevice, void (*mw_get_battery_voltage_response_cb) (mwdevice_t *mwdevice, unsigned short *voltage, unsigned char *pgood, unsigned char *charging, void *user_data), void *user_data)
418 {
419         if (mw_get_battery_voltage_response_cb != NULL)
420                 mwdevice->mw_get_battery_voltage_response_cb = mw_get_battery_voltage_response_cb;
421         if (user_data != NULL)
422                 mwdevice->mw_gbatvrsp_data = user_data;
423 }
424
425 int mw_read_button_config_response(mwdevice_t *mwdevice, unsigned char *btnrsp, int len)
426 {
427 #ifdef DEBUG
428         fprintf(stderr, "read button config response\n");
429         fprintf(stderr, "screen mode      : 0x%02x\n", btnrsp[0]);
430         fprintf(stderr, "button index     : 0x%02x\n", btnrsp[1]);
431         fprintf(stderr, "mask table       : 0x%02x (", btnrsp[2]);
432         fprintf(stderr, "%s ", (btnrsp[2] & 0x01) ? "Absolute, " : "");
433         fprintf(stderr, "%s ", (btnrsp[2] & 0x02) ? "Press&Release, " : "");
434         fprintf(stderr, "%s ", (btnrsp[2] & 0x04) ? "Press&Hold, " : "");
435         fprintf(stderr, "%s ", (btnrsp[2] & 0x08) ? "Press&LongHold, " : "");
436         fprintf(stderr, "%s ", (btnrsp[2] & 0x10) ? "Immediate" : "");
437         fprintf(stderr, ")\n");
438         fprintf(stderr, "callback msg type: 0x%02x\n", btnrsp[3]);
439         fprintf(stderr, "callback msg opts: 0x%02d\n", btnrsp[4]);
440 #endif
441
442         return 0;
443 }
444
445 void mw_set_read_button_config_response_cb(mwdevice_t *mwdevice, void (*mw_read_button_config_response_cb) (mwdevice_t *mwdevice, void *user_data), void *user_data)
446 {
447         if (mw_read_button_config_response_cb != NULL)
448                 mwdevice->mw_read_button_config_response_cb = mw_read_button_config_response_cb;
449         if (user_data != NULL)
450                 mwdevice->mw_rbtncnfrsp_data = user_data;
451 }
452
453 int mw_button_event_message(mwdevice_t *mwdevice, unsigned char *btnevt, int len, unsigned char opts)
454 {
455 #ifdef DEBUG
456         fprintf(stderr, "  button %d options %d\n", btnevt[0], opts);
457 #endif
458
459         if (mwdevice->mw_button_event_message_cb != NULL)
460                 mwdevice->mw_button_event_message_cb(mwdevice, btnevt[0], opts, mwdevice->mw_btnevmsg_data);
461
462         return 0;
463 }
464
465 void mw_set_button_event_message_cb(mwdevice_t *mwdevice, void (*mw_button_event_message_cb) (mwdevice_t *mwdevice, unsigned char buttons, unsigned char options, void *user_data), void *user_data)
466 {
467         if (mw_button_event_message_cb != NULL)
468                 mwdevice->mw_button_event_message_cb = mw_button_event_message_cb;
469         if (user_data != NULL)
470                 mwdevice->mw_btnevmsg_data = user_data;
471 }
472
473 int mw_read_light_sensor_response(mwdevice_t *mwdevice, unsigned char *lightrsp, int len)
474 {
475         unsigned char power_good = lightrsp[0];
476         unsigned char bat_charging = lightrsp[1];
477         unsigned short light_level = *(unsigned short *)(lightrsp+2);
478
479 #ifdef DEBUG
480         fprintf(stderr, "light sensor is at %d, power stat: %s and %s\n", light_level, power_good ? "power is good" : "power fault", bat_charging ? "charging" : "not charging");
481 #endif
482
483         if (mwdevice->mw_read_light_sensor_response_cb != NULL)
484                 mwdevice->mw_read_light_sensor_response_cb(mwdevice, &light_level, mwdevice->mw_rlsrsp_data);
485
486         return 0;
487 }
488
489 void mw_set_read_light_sensor_response_cb(mwdevice_t *mwdevice, void (*mw_read_light_sensor_response_cb) (mwdevice_t *mwdevice, unsigned short *light_level, void *user_data), void *user_data)
490 {
491         if (mw_read_light_sensor_response_cb != NULL)
492                 mwdevice->mw_read_light_sensor_response_cb = mw_read_light_sensor_response_cb;
493         if (user_data != NULL)
494                 mwdevice->mw_rlsrsp_data = user_data;
495 }
496
497 int mw_status_change_event(mwdevice_t *mwdevice, unsigned char option, unsigned char *statrsp, int len)
498 {
499         unsigned char mode = (option & 0x0f);
500         unsigned char status = statrsp[0];
501 #ifdef DEBUG
502         fprintf(stderr, "Status change event for mode %s: %s\n", mw_screen_mode_names[mode], mw_status_string[status]); 
503 #endif
504
505         // mwdevice->screen_mode = mode;
506         if (mwdevice->mw_status_change_event_cb != NULL)
507                 mwdevice->mw_status_change_event_cb(mwdevice, &mode, &status, mwdevice->mw_stchev_data);
508
509         return 0;
510 }
511
512 void mw_set_status_change_event_cb(mwdevice_t *mwdevice, void (*mw_status_change_event_cb) (mwdevice_t *mwdevice, unsigned char *scrmode, unsigned char *status, void *user_data), void *user_data)
513 {
514         if (mw_status_change_event_cb != NULL)
515                 mwdevice->mw_status_change_event_cb = mw_status_change_event_cb;
516         if (user_data != NULL)
517                 mwdevice->mw_stchev_data = user_data;
518 }
519
520
521 /* ----------------------------------------------------------------------
522  * Protocol handling
523  * ---------------------------------------------------------------------- */
524
525 int mw_decode_frame(mwdevice_t *mwdevice, unsigned char *buf, int len)
526 {
527         unsigned short crc;
528         unsigned char msglen;
529         unsigned char msgtype;
530         unsigned char msgopt;
531         unsigned char *msgdata;
532         unsigned char msgdatalen;
533
534         /* check frame */
535         crc = *(unsigned short *)(buf+len-2);
536         if (crc != crc16ccitt(buf, len-2)) {
537                 fprintf(stderr, "decode frame CRC error\n");
538                 return -1;
539         }
540         if (buf[0] != MW_SOF) {
541                 fprintf(stderr, "decode frame SOF not found\n");
542                 return -1;
543         }
544
545         msglen = buf[1];
546         msgtype = buf[2];
547         msgopt = buf[3];
548         msgdata = (buf+4);
549         msgdatalen = msglen - 4 - 2;
550
551         switch (msgtype) {
552                 case MW_GET_DEVICE_TYPE_RSP:
553                         mw_get_device_type_response(mwdevice, msgdata[0]);
554                         break;
555                 case MW_GET_INFORMATION_STRING_RSP:
556                         msgdata[len-2] = 0;
557                         fprintf(stderr, "Got info string '%s'\n", msgdata);
558                         break;
559                 case MW_GET_REAL_TIME_CLOCK_RSP:
560                         mw_get_real_time_clock_response(mwdevice, msgdata, msgdatalen);
561                         break;
562                 case MW_READ_BATTERY_VOLTAGE_RSP:
563                         mw_get_battery_voltage_response(mwdevice, msgdata, msgdatalen);
564                         break;
565                 case MW_READ_LIGHT_SENSOR_RSP:
566                         mw_read_light_sensor_response(mwdevice, msgdata, msgdatalen);
567                         break;
568                 case MW_LOW_BATTERY_WARNING_MSG:
569                         fprintf(stderr, "Watch battery low, please connect charger\n");
570                         break;
571                 case MW_READ_BUTTON_CONFIG_RSP:
572                         mw_read_button_config_response(mwdevice, msgdata, msgdatalen);
573                         break;
574                 case MW_BUTTON_EVENT_MESSAGE:
575                         fprintf(stderr, "Button event message\n");
576                         mw_button_event_message(mwdevice, msgdata, msgdatalen, msgopt);
577                         break;
578                 case MW_LOW_BATTERY_BT_OFF_MSG:
579                         fprintf(stderr, "Watch battery extremely low - radio will turn off\n");
580                         break;
581                 case MW_NVAL_OPERATION_RSP:
582                         fprintf(stderr, "NVAL operation response - ");
583                         switch (msgopt) {
584                                 case 0x00:
585                                         fprintf(stderr, "success\n");
586                                         break;
587                                 case 0x01:
588                                         fprintf(stderr, "failure!\n");
589                                         break;
590                                 case 0x09:
591                                         fprintf(stderr, "Item Not initialized (Identifier Not found\n");
592                                         break;
593                                 case 0x0a:
594                                         fprintf(stderr, "Operation failed\n");
595                                         break;
596                                 case 0x0c:
597                                         fprintf(stderr, "Bad Item length\n");
598                                         break;
599                                 default:
600                                         fprintf(stderr, "unknown status 0x%02x\n", msgopt);
601                                         break;
602                         };
603                         break;
604                 case MW_STATUS_CHANGE_EVENT:
605                         mw_status_change_event(mwdevice, msgopt, msgdata, msgdatalen);
606                         break;
607                 default:
608                         fprintf(stderr, "Unkown msgtype 0x%02x\n", msgtype);
609                         break;
610         };
611
612         return msglen;
613 }
614
615
616 int mw_feed_msg_buffer(mwdevice_t *mwdevice, unsigned char *buf, int len)
617 {
618         char msgbuf[64];
619         int tlen;
620
621         if (len <= 0)
622                 return -1;
623
624         memcpy((mwdevice->pbuf+mwdevice->pbuf_len), buf, len);
625         mwdevice->pbuf_len += len;
626         
627         while (mwdevice->pbuf_len > 0) {
628                 /* scan for MW_SOF */
629                 while (mwdevice->pbuf[0] != MW_SOF) {
630                         memmove(mwdevice->pbuf, (mwdevice->pbuf+1), --mwdevice->pbuf_len);
631                 }
632                 tlen = mwdevice->pbuf[1];
633                 /* OK, there is an SOF but the message is too short */
634                 if (tlen > mwdevice->pbuf_len) {
635                         /* we have to wait for more data to come in */
636                         break;
637                 }
638                 /* here we have a complete msg */
639                 memcpy(msgbuf, mwdevice->pbuf, tlen);
640                 mw_decode_frame(mwdevice, (unsigned char *)msgbuf, tlen);
641                 memmove(mwdevice->pbuf, (mwdevice->pbuf+tlen), tlen);
642                 mwdevice->pbuf_len -= tlen;
643         }
644         return 0;
645 }
646
647
648 /* ----------------------------------------------------------------------
649  * General code usage
650  * ---------------------------------------------------------------------- */
651
652 int mw_init(mwdevice_t *mwdevice, int mw_fd)
653 {
654         memset(mwdevice, 0, sizeof(mwdevice_t));
655         mwdevice->mw_fd = mw_fd;
656         memset(mwdevice->pbuf, 0, MW_PBUF_LEN);
657         mwdevice->pbuf_len = 0;
658
659         /* figure out which device we run with */
660         mw_send_frame(mwdevice, MW_GET_DEVICE_TYPE, 0, NULL, 0);
661
662         return 0;
663 }
664
665 /* ----------------------------------------------------------------------
666  * Convenience functions not strictly part of the protocol
667  * ---------------------------------------------------------------------- */
668
669 int mw_get_resolution(mwdevice_t *mwdevice, unsigned int *width, unsigned int *height)
670 {
671         if (width == NULL || height == NULL)
672                 return -1;
673
674         switch (mwdevice->devtype) {
675                 case MW_DEVICE_TYPE_RESERVED:
676                         return -1;
677                 case MW_DEVICE_TYPE_ANA_DIGI:
678                 case MW_DEVICE_TYPE_DEVB_ANA_DIGI:
679                         *width = 80;
680                         *height = 16;
681                         break;
682                 case MW_DEVICE_TYPE_DIGITAL:
683                 case MW_DEVICE_TYPE_DEVB_DIGI:
684                         *width = 96;
685                         *height = 96;
686                         break;
687                 default:
688                         break;
689         };
690
691         return 0;
692 }
693
694
695 /* if flip=1 bits in each byte are inverted 7->1, 6->2, 5->3,...
696    if invert=1 each byte is inverted
697  */
698 void bmap_buffer_flipinvert(unsigned char flip, unsigned char invert, unsigned char *buf, int len)
699 {
700         int i;
701         unsigned char tmp;
702
703         while (len--) {
704                 tmp = 0;
705                 if (flip) {
706                         for (i=0; i<8; i++)
707                                 tmp |= ((*buf & (1<<i)) >> i) << (7-i);
708                         // fprintf(stderr, "0x%02x -> 0x%02x\n", *buf, tmp);
709                 } else
710                         tmp = *buf;
711                 *buf = invert ? ~tmp : tmp;
712                 buf++;
713         }
714 }
715
716
717 void mw_send_bitmap(mwdevice_t *mwdevice, unsigned char mode, int width, int height, int offset, unsigned char *bmapbuf, int buflen)
718 {
719 #ifdef DEBUG
720         unsigned int i, x;
721 #endif
722         unsigned int y, rowlength;
723         unsigned char mw_buf[24];
724
725         rowlength = (((width-1) / 8) + 1);
726         if ((height + offset) > 96)
727                 height = 96 - offset;
728
729 #ifdef DEBUG
730         fprintf(stderr, "row length = %d bytes\n", rowlength);
731         fprintf(stderr, "bitmap resolution is %d x %d\n", width, height);
732         fprintf(stderr, "\n");
733         for (y=0; y<height; y++) {
734                 for (x=0; x<rowlength; x++) {
735                         for (i=0; i<8; i++)
736                                 fprintf(stderr, "%c", (bmapbuf[(y*rowlength)+x] & (1<<(7-i))) ? '.' : ' ');
737                 }
738                 fprintf(stderr, "\n");
739         }
740         fprintf(stderr, "\n");
741 #endif
742
743         for (y=0; y<height; y+=2) {
744                 memset(mw_buf, 0, 24);
745                 memcpy(mw_buf, (bmapbuf+(y*rowlength)), (rowlength > 12) ? 12 : rowlength);
746                 memcpy((mw_buf+12), (bmapbuf+((y+1)*rowlength)), (rowlength > 12) ? 12 : rowlength);
747                 mw_write_buffer(mwdevice, mode, 0, offset+y, mw_buf, 24);
748         }
749 }
750