]> git.kernelconcepts.de Git - metawatch.git/blob - metawatch.c
More callbacks, further GUI work
[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_set_vibrate_mode(mwdevice_t *mwdevice, unsigned char enable, unsigned short on_time, unsigned short off_time, unsigned char cycles)
134 {
135         unsigned char mdata[7];
136
137         mdata[0] = enable;
138         *(unsigned short *)(mdata+1) = on_time; /* miliseconds */
139         *(unsigned short *)(mdata+3) = off_time; /* miliseconds */
140         mdata[5] = cycles;
141
142         return mw_send_frame(mwdevice, MW_SET_VIBRATE_MODE, 0, mdata, 6);
143 }
144
145 int mw_configure_watch_mode(mwdevice_t *mwdevice, unsigned char mode, unsigned char save, unsigned char timeout, unsigned char invert)
146 {
147         unsigned char mdata[3];
148
149         mdata[0] = timeout; /* seconds */
150         mdata[1] = invert; /* 0=normal, 1=invert */
151
152         return mw_send_frame(mwdevice, MW_CONFIGURE_MODE, (mode & 0x0f) | ((save & 0x01) << 4), mdata, 2);
153 }
154
155 int mw_update_display(mwdevice_t *mwdevice, unsigned char mode, unsigned char copy)
156 {
157         return mw_send_frame(mwdevice, MW_UPDATE_DISPLAY, (mode & 0x0f) | ((copy & 0x01) << 4), NULL, 0);
158 }
159
160 int mw_load_template(mwdevice_t *mwdevice, unsigned char mode, unsigned char template_select)
161 {
162         return mw_send_frame(mwdevice, MW_LOAD_TEMPLATE, (mode & 0x0f), &template_select, 1);
163 }
164
165 /*
166  * send line for screen-mode mode from *buffer to watch, starting at display row row_offset
167  * this is only for digital LCD watch
168  */
169 int mw_write_buffer(mwdevice_t *mwdevice,
170                 unsigned char mode,
171                 unsigned char numlines,         /* number of lines, 0=two lines or 1=one line */
172                 unsigned char row_offset,       /* start at row_offset in display, e.g. lower part in idle @31 */
173                 unsigned char *buffer, int buflen)
174 {
175         unsigned char mdata[32];
176
177         if (mwdevice->devtype != MW_DEVICE_TYPE_DIGITAL && mwdevice->devtype != MW_DEVICE_TYPE_DEVB_DIGI)
178                 return -1;
179
180         buflen = 12 * (buflen / 12);    /* crop to 12 bytes */
181         if ((numlines == 0 && buflen < 12) || (numlines == 1 && buflen < 24)) {
182                 fprintf(stderr, "mw_write_buffer: bufferlen does not match number of lines\n");
183                 return -1;
184         };
185         memset(mdata, 0, 32);
186         mdata[0] = row_offset;
187         memcpy((mdata+1), buffer, 12);
188         if (numlines == 0) {
189                 mdata[13] = row_offset+1;
190                 memcpy((mdata+14), (buffer+12), 12);
191         };
192
193         return mw_send_frame(mwdevice, MW_WRITE_BUFFER, (mode & 0x0f) | (((numlines & 0x01)<< 4) & 0x10), mdata, numlines ? 13 : 26);
194 }
195
196 /*
197    Options:
198         B0   : row select, 0 first row, 1 second row
199         B1   : display select, 0 upper OLED, 1 lower OLED
200         B2   : if 1 send an event upon completion
201         B3..4: scroll type,
202         B5..7: unused
203    Scroll types:
204         B0: First buffer in a chain of scroll buffers
205         B1: Any buffer except first or last
206         B2: Last buffer of a chain
207         B3: reserved / unused
208    Payload:
209         0: Start index col
210         1..: data
211 */
212 int mw_write_oled_buffer(mwdevice_t *mwdevice,
213         unsigned char mode, /* idle or scroll */
214         unsigned char oled, /* which OLED */
215         unsigned char numcols,
216         unsigned char col_index, /* starting index */
217         unsigned char *buffer, int buflen)
218 {
219         unsigned char mdata[32];
220         int i;
221
222         if (mwdevice->devtype != MW_DEVICE_TYPE_ANA_DIGI && mwdevice->devtype != MW_DEVICE_TYPE_DEVB_ANA_DIGI)
223                 return -1;
224         
225         fprintf(stderr, "write oled buf len = %d\n", buflen);
226         /* lower row first since display wil be updated after completion of upper row */
227         if (buflen > 80) {
228                 for (i=80; i<buflen; i+=20) {
229                         mdata[0] = (i-80);
230                         memcpy((mdata+1), (buffer+i), 20);
231                         mw_send_frame(mwdevice, MW_WRITE_OLED_IDLE_DISPLAY_MSG, 0 | (oled ? 2 : 0), mdata, 21);
232                 }
233         }
234         for (i=0; i<80; i+=20) {
235                 mdata[0] = i;
236                 memcpy((mdata+1), (buffer+i), 20);
237                 mw_send_frame(mwdevice, MW_WRITE_OLED_IDLE_DISPLAY_MSG, 1 | (oled ? 2 : 0), mdata, 21);
238         }
239
240         return 0;
241 }
242
243 int mw_enable_button(mwdevice_t *mwdevice,
244                 unsigned char mode,
245                 unsigned char button_index,
246                 unsigned char press_type,
247                 unsigned char callback_type,
248                 unsigned char callback_option)
249 {
250         unsigned char mdata[32];
251
252         memset(mdata, 0, 32);
253         mdata[0] = mode;
254         mdata[1] = button_index;
255         mdata[2] = press_type;
256         mdata[3] = callback_type;
257         mdata[4] = callback_option;
258
259         return mw_send_frame(mwdevice, MW_ENABLE_BUTTON, 0, mdata, 5);
260 }
261
262 int mw_disable_button(mwdevice_t *mwdevice,
263                 unsigned char mode,
264                 unsigned char button_index,
265                 unsigned char press_type)
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
274         return mw_send_frame(mwdevice, MW_ENABLE_BUTTON, 0, mdata, 3);
275 }
276
277 int mw_advance_watch_hands(mwdevice_t *mwdevice, unsigned char hours, unsigned char minutes, unsigned char seconds)
278 {
279         unsigned char mdata[4];
280         
281         if (hours > 12)
282                 return -1;
283         if (minutes > 60)
284                 return -1;
285         if (seconds > 60)
286                 return -1;
287
288         mdata[0] = hours;
289         mdata[1] = minutes;
290         mdata[2] = seconds;
291
292         return mw_send_frame(mwdevice, MW_ADVANCE_WATCH_HANDS, 0, mdata, 3);
293 }
294
295 /* ----------------------------------------------------------------------
296  * Watch responses, events or notifications
297  * ---------------------------------------------------------------------- */
298
299
300 int mw_get_device_type_response(mwdevice_t *mwdevice, unsigned char devtype)
301 {
302 #ifdef DEBUG
303         fprintf(stderr, "Got device type ");
304         switch(devtype) {
305                 case 0:
306                         fprintf(stderr, "Reserved\n");
307                         break;
308                 case 1:
309                         fprintf(stderr, "Ana-Digi\n");
310                         break;
311                 case 2:
312                         fprintf(stderr, "Digital\n");
313                         break;
314                 case 3:
315                         fprintf(stderr, "Development Board Digital\n");
316                         break;
317                 case 4:
318                         fprintf(stderr, "Development Board Ana-Digi\n");
319                         break;
320                 default:
321                         fprintf(stderr, "unknown %d\n", devtype);
322                         break;
323         };
324 #endif
325         mwdevice->devtype = devtype;
326         if (mwdevice->mw_get_device_type_response_cb != NULL)
327                 mwdevice->mw_get_device_type_response_cb(mwdevice, devtype, mwdevice->mw_gdtypersp_data);
328         return 0;
329 }
330
331 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)
332 {
333         if (mw_get_device_type_response_cb != NULL)
334                 mwdevice->mw_get_device_type_response_cb = mw_get_device_type_response_cb;
335         if (user_data != NULL)
336                 mwdevice->mw_gdtypersp_data = user_data;
337 }
338
339
340 int mw_get_real_time_clock_response(mwdevice_t *mwdevice, unsigned char *rtcrsp, int len)
341 {
342         struct tm mtm;
343         unsigned short year;
344         unsigned char clk1224, date_fmt;
345
346         if (len != 10) {
347                 fprintf(stderr, "get real time clock response length wrong %d != 10\n", len);
348                 return -1;
349         }
350
351         year = *(unsigned short *)rtcrsp;
352         mtm.tm_year = year - 1900;
353         mtm.tm_mon = rtcrsp[2] - 1;
354         mtm.tm_mday = rtcrsp[3];
355         mtm.tm_wday = rtcrsp[4];
356         mtm.tm_hour = rtcrsp[5];
357         mtm.tm_min = rtcrsp[6];
358         mtm.tm_sec = rtcrsp[7];
359         clk1224 = rtcrsp[8];
360         date_fmt = rtcrsp[9];
361
362 #ifdef DEBUG
363         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");
364 #endif
365         if (mwdevice->mw_get_real_time_clock_response_cb != NULL)
366                 mwdevice->mw_get_real_time_clock_response_cb(mwdevice, &mtm, mwdevice->mw_grtcrsp_data);
367
368         return 0;
369 }
370
371 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)
372 {
373         if (mw_get_real_time_clock_response_cb != NULL)
374                 mwdevice->mw_get_real_time_clock_response_cb = mw_get_real_time_clock_response_cb;
375         if (user_data != NULL)
376                 mwdevice->mw_grtcrsp_data = user_data;
377 }
378
379
380 int mw_get_battery_voltage_response(mwdevice_t *mwdevice, unsigned char *batrsp, int len)
381 {
382         unsigned short voltage = *(unsigned short *)batrsp;
383         unsigned char power_good = batrsp[2];
384         unsigned char bat_charging = batrsp[3];
385
386 #ifdef DEBUG
387         fprintf(stderr, "battery is at %dmV, %s and %s\n", voltage, power_good ? "power is good" : "power fault", bat_charging ? "charging" : "not charging");
388 #endif
389
390         if (mwdevice->mw_get_battery_voltage_response_cb != NULL)
391                 mwdevice->mw_get_battery_voltage_response_cb(mwdevice, &voltage, &power_good, &bat_charging, mwdevice->mw_gbatvrsp_data);
392
393         return 0;
394 }
395
396 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)
397 {
398         if (mw_get_battery_voltage_response_cb != NULL)
399                 mwdevice->mw_get_battery_voltage_response_cb = mw_get_battery_voltage_response_cb;
400         if (user_data != NULL)
401                 mwdevice->mw_gbatvrsp_data = user_data;
402 }
403
404 int mw_read_button_config_response(mwdevice_t *mwdevice, unsigned char *btnrsp, int len)
405 {
406 #ifdef DEBUG
407         fprintf(stderr, "read button config response\n");
408         fprintf(stderr, "screen mode      : 0x%02x\n", btnrsp[0]);
409         fprintf(stderr, "button index     : 0x%02x\n", btnrsp[1]);
410         fprintf(stderr, "mask table       : 0x%02x (", btnrsp[2]);
411         fprintf(stderr, "%s ", (btnrsp[2] & 0x01) ? "Absolute, " : "");
412         fprintf(stderr, "%s ", (btnrsp[2] & 0x02) ? "Press&Release, " : "");
413         fprintf(stderr, "%s ", (btnrsp[2] & 0x04) ? "Press&Hold, " : "");
414         fprintf(stderr, "%s ", (btnrsp[2] & 0x08) ? "Press&LongHold, " : "");
415         fprintf(stderr, "%s ", (btnrsp[2] & 0x10) ? "Immediate" : "");
416         fprintf(stderr, ")\n");
417         fprintf(stderr, "callback msg type: 0x%02x\n", btnrsp[3]);
418         fprintf(stderr, "callback msg opts: 0x%02d\n", btnrsp[4]);
419 #endif
420
421         return 0;
422 }
423
424 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)
425 {
426         if (mw_read_button_config_response_cb != NULL)
427                 mwdevice->mw_read_button_config_response_cb = mw_read_button_config_response_cb;
428         if (user_data != NULL)
429                 mwdevice->mw_rbtncnfrsp_data = user_data;
430 }
431
432 int mw_read_light_sensor_response(mwdevice_t *mwdevice, unsigned char *lightrsp, int len)
433 {
434         unsigned char power_good = lightrsp[0];
435         unsigned char bat_charging = lightrsp[1];
436         unsigned short light_level = *(unsigned short *)(lightrsp+2);
437
438 #ifdef DEBUG
439         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");
440 #endif
441
442         if (mwdevice->mw_read_light_sensor_response_cb != NULL)
443                 mwdevice->mw_read_light_sensor_response_cb(mwdevice, &light_level, mwdevice->mw_rlsrsp_data);
444
445         return 0;
446 }
447
448 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)
449 {
450         if (mw_read_light_sensor_response_cb != NULL)
451                 mwdevice->mw_read_light_sensor_response_cb = mw_read_light_sensor_response_cb;
452         if (user_data != NULL)
453                 mwdevice->mw_rlsrsp_data = user_data;
454 }
455
456 int mw_status_change_event(mwdevice_t *mwdevice, unsigned char option, unsigned char *statrsp, int len)
457 {
458         unsigned char mode = (option & 0x0f);
459         unsigned char status = statrsp[0];
460 #ifdef DEBUG
461         fprintf(stderr, "Status change event for mode %s: %s\n", mw_screen_mode_names[option&0x0f], mw_status_string[statrsp[0]]);      
462 #endif
463
464         if (mwdevice->mw_status_change_event_cb != NULL)
465                 mwdevice->mw_status_change_event_cb(mwdevice, &mode, &status, mwdevice->mw_stchev_data);
466
467         return 0;
468 }
469
470 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)
471 {
472         if (mw_status_change_event_cb != NULL)
473                 mwdevice->mw_status_change_event_cb = mw_status_change_event_cb;
474         if (user_data != NULL)
475                 mwdevice->mw_stchev_data = user_data;
476 }
477
478
479 /* ----------------------------------------------------------------------
480  * Protocol handling
481  * ---------------------------------------------------------------------- */
482
483 int decode_frame(mwdevice_t *mwdevice, unsigned char *buf, int len)
484 {
485         unsigned short crc;
486         unsigned char msglen;
487         unsigned char msgtype;
488         unsigned char msgopt;
489         unsigned char *msgdata;
490         unsigned char msgdatalen;
491
492         /* check frame */
493         crc = *(unsigned short *)(buf+len-2);
494         if (crc != crc16ccitt(buf, len-2)) {
495                 fprintf(stderr, "decode frame CRC error\n");
496                 return -1;
497         }
498         if (buf[0] != MW_SOF) {
499                 fprintf(stderr, "decode frame SOF not found\n");
500                 return -1;
501         }
502
503         msglen = buf[1];
504         msgtype = buf[2];
505         msgopt = buf[3];
506         msgdata = (buf+4);
507         msgdatalen = msglen - 4 - 2;
508
509         switch (msgtype) {
510                 case MW_GET_DEVICE_TYPE_RSP:
511                         mw_get_device_type_response(mwdevice, msgdata[0]);
512                         break;
513                 case MW_GET_INFORMATION_STRING_RSP:
514                         msgdata[len-2] = 0;
515                         fprintf(stderr, "Got info string '%s'\n", msgdata);
516                         break;
517                 case MW_GET_REAL_TIME_CLOCK_RSP:
518                         mw_get_real_time_clock_response(mwdevice, msgdata, msgdatalen);
519                         break;
520                 case MW_READ_BATTERY_VOLTAGE_RSP:
521                         mw_get_battery_voltage_response(mwdevice, msgdata, msgdatalen);
522                         break;
523                 case MW_READ_LIGHT_SENSOR_RSP:
524                         mw_read_light_sensor_response(mwdevice, msgdata, msgdatalen);
525                         break;
526                 case MW_LOW_BATTERY_WARNING_MSG:
527                         fprintf(stderr, "Watch battery low, please connect charger\n");
528                         break;
529                 case MW_READ_BUTTON_CONFIG_RSP:
530                         mw_read_button_config_response(mwdevice, msgdata, msgdatalen);
531                         break;
532                 case MW_BUTTON_EVENT_MESSAGE:
533                         fprintf(stderr, "Button event message\n");
534                         break;
535                 case MW_LOW_BATTERY_BT_OFF_MSG:
536                         fprintf(stderr, "Watch battery extremely low - radio will turn off\n");
537                         break;
538                 case MW_STATUS_CHANGE_EVENT:
539                         mw_status_change_event(mwdevice, msgopt, msgdata, msgdatalen);
540                         break;
541                 default:
542                         fprintf(stderr, "Unkown msgtype 0x%02x\n", msgtype);
543                         break;
544         };
545
546         return msglen;
547 }
548
549
550 /* ----------------------------------------------------------------------
551  * General code usage
552  * ---------------------------------------------------------------------- */
553
554 int mw_init(mwdevice_t *mwdevice, int mw_fd)
555 {
556         memset(mwdevice, 0, sizeof(mwdevice_t));
557         mwdevice->mw_fd = mw_fd;
558
559         /* figure out which device we run with */
560         mw_send_frame(mwdevice, MW_GET_DEVICE_TYPE, 0, NULL, 0);
561
562         return 0;
563 }
564
565 /* ----------------------------------------------------------------------
566  * Convenience functions not strictly part of the protocol
567  * ---------------------------------------------------------------------- */
568
569 int mw_get_resolution(mwdevice_t *mwdevice, unsigned int *width, unsigned int *height)
570 {
571         if (width == NULL || height == NULL)
572                 return -1;
573
574         switch (mwdevice->devtype) {
575                 case MW_DEVICE_TYPE_RESERVED:
576                         return -1;
577                 case MW_DEVICE_TYPE_ANA_DIGI:
578                 case MW_DEVICE_TYPE_DEVB_ANA_DIGI:
579                         *width = 80;
580                         *height = 16;
581                         break;
582                 case MW_DEVICE_TYPE_DIGITAL:
583                 case MW_DEVICE_TYPE_DEVB_DIGI:
584                         *width = 96;
585                         *height = 96;
586                         break;
587                 default:
588                         break;
589         };
590
591         return 0;
592 }
593
594
595 /* if flip=1 bits in each byte are inverted 7->1, 6->2, 5->3,...
596    if invert=1 each byte is inverted
597  */
598 void bmap_buffer_flipinvert(unsigned char flip, unsigned char invert, unsigned char *buf, int len)
599 {
600         int i;
601         unsigned char tmp;
602
603         while (len--) {
604                 tmp = 0;
605                 if (flip) {
606                         for (i=0; i<8; i++)
607                                 tmp |= ((*buf & (1<<i)) >> i) << (7-i);
608                         // fprintf(stderr, "0x%02x -> 0x%02x\n", *buf, tmp);
609                 } else
610                         tmp = *buf;
611                 *buf = invert ? ~tmp : tmp;
612                 buf++;
613         }
614 }
615
616
617 void mw_send_bitmap(mwdevice_t *mwdevice, unsigned char mode, int width, int height, int offset, unsigned char *bmapbuf, int buflen)
618 {
619 #ifdef DEBUG
620         unsigned int i, x;
621 #endif
622         unsigned int y, rowlength;
623         unsigned char mw_buf[24];
624
625         rowlength = (((width-1) / 8) + 1);
626         if ((height + offset) > 96)
627                 height = 96 - offset;
628
629 #ifdef DEBUG
630         fprintf(stderr, "row length = %d bytes\n", rowlength);
631         fprintf(stderr, "bitmap resolution is %d x %d\n", width, height);
632         fprintf(stderr, "\n");
633         for (y=0; y<height; y++) {
634                 for (x=0; x<rowlength; x++) {
635                         for (i=0; i<8; i++)
636                                 fprintf(stderr, "%c", (bmapbuf[(y*rowlength)+x] & (1<<(7-i))) ? '.' : ' ');
637                 }
638                 fprintf(stderr, "\n");
639         }
640         fprintf(stderr, "\n");
641 #endif
642
643         for (y=0; y<height; y+=2) {
644                 memset(mw_buf, 0, 24);
645                 memcpy(mw_buf, (bmapbuf+(y*rowlength)), (rowlength > 12) ? 12 : rowlength);
646                 memcpy((mw_buf+12), (bmapbuf+((y+1)*rowlength)), (rowlength > 12) ? 12 : rowlength);
647                 mw_write_buffer(mwdevice, mode, 0, offset+y, mw_buf, 24);
648         }
649 }
650