File: lcdgui.cc

package info (click to toggle)
gpsim-lcd 0.1.1-9
  • links: PTS
  • area: main
  • in suites: woody
  • size: 904 kB
  • ctags: 270
  • sloc: sh: 7,823; cpp: 3,229; asm: 437; pascal: 150; perl: 93; makefile: 72
file content (439 lines) | stat: -rw-r--r-- 10,025 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/* lcd.c
   Copyright (C) 2000 Scott Dattalo

This is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

This is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with lcd.c; see the file COPYING.  If not, write to
the Free Software Foundation, 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.  */


#include <gtk/gtk.h>

typedef char _5X7 [7][6];
#include "lcdfont.h"
#include "lcd.h"

static gchar **xpm_template;


//GtkWidget *test_pix=NULL;
//GdkPixmap *test_pixmap=NULL;

//GdkPixmap *LCD_font[FONT_LEN];
//gint total_chars = 0;

//

gchar ch,n;


/****************************************************************
 * CreateXPMdataFromLCDdata -
 *
 * It'd probably be easier to just create pixmaps straight from
 * xpm maps. However, that would confine the lcd characters to
 * just one size. This routine will take an lcd character (that
 * is defined somewhat like a bitmap) and create a pixmap. The
 * size of the pixmap is determined by the size that display
 * requires for each cell. (For example, on displays with a 5x7
 * cell, the lcd display will map several crt pixels into one
 * lcd pixel.)
 */

gchar  **CreateXPMdataFromLCDdata(LcdDisplay *lcdP, _5X7 *ch )
{

  guint i,j,k,rows,cols,m,ii,jj,colors;
  guint bc,pc;
  char buffer[256];
  gchar **xpm_template;

  // total rows in the xpm
  rows = 9 + lcdP->dots.y * lcdP->pixels.y;
  cols = 1 + lcdP->dots.x * lcdP->pixels.x;

  xpm_template = (char **)malloc(rows * sizeof(gchar *));

  colors = 3;
  sprintf(buffer, "%d %d %d 1", cols, 1+lcdP->dots.y * lcdP->pixels.y,
	  colors);
  xpm_template[0] = (gchar *)strdup(buffer);
  xpm_template[1] = (gchar *)strdup("  c None");
  xpm_template[2] = (gchar *)strdup("B c #113311");
  xpm_template[3] = (gchar *)strdup("G c #668866");
  //  xpm_template[4] = (gchar *)strdup("Q c #789878");

  for(i=4; i<rows; i++) {
    xpm_template[i] = (gchar *)malloc(cols+1);
    memset(xpm_template[i], ' ', cols);
    xpm_template[i][cols] = 0;
    //xpm_template[i] = (gchar *)strdup("                ");
  }

  for(j=0; j<lcdP->dots.y; j++) {

    k = 5 + lcdP->pixels.y*j;

    
    for(i=0; i<lcdP->dots.x; i++) {
      pc = (ch[0][j][i] == '.') ? 'B' : ' ';
      bc = (ch[0][j][i] == '.') ? 'G' : ' ';

      m = i*lcdP->pixels.y;

      for(jj=k; jj<k+lcdP->pixels.y-1; jj++) {

	xpm_template[jj][m] = bc;
	for(ii=m+1; ii<m+lcdP->pixels.x; ii++)
	  xpm_template[jj][ii] = pc;
      }
      for(ii=m; ii<m+lcdP->pixels.x; ii++)
	xpm_template[k+lcdP->pixels.y-1][ii] = bc;
    }

    //if we want to have the right edge a different color...
    //for(jj=k; jj<k+3; jj++) 
    //  xpm_template[jj][5*3] = ' ';

    
  }

  return xpm_template;
}

#if 0
/*
 * CreateWidgetFromXpm (borrowed from Harlow)
 *
 * Using the window information and the string with the icon color/data, 
 * create a widget that represents the data.  Once done, this widget can
 * be added to buttons or other container widgets.
 */
GtkWidget *CreateWidgetFromXpm (GtkWidget *parent_window,LCD_display *lcd, gchar **xpm_data)
{
    GdkBitmap *mask;

    GtkWidget *pixmap_widget;

    test_pixmap = gdk_pixmap_create_from_xpm_d (
                                 parent_window->window, 
                                 &mask,
                                 lcd->dot_color,
                                 (gchar **) xpm_data);

    //pixmap_widget = gtk_pixmap_new (test_pixmap, mask);
    //gtk_widget_show (pixmap_widget);

    return (pixmap_widget);
}

#endif


/***************************************************************
 * CreateFont
 *   Here we read the lcdfont.h file and build LCD character 
 * pixmaps.
 *
 */

LcdFont::LcdFont (gint characters,  GtkWidget *parent_window, LcdDisplay *lcdP)
{
  gint i;

  num_elements = characters;
  pixmaps = (GdkPixmap **)malloc( sizeof (GdkPixmap *) * num_elements);

  for(i=0; i<num_elements; i++) {

    if(strlen(test[i][0]) < 5)
      pixmaps[i] = NULL;
    else
      pixmaps[i] = gdk_pixmap_create_from_xpm_d (
				 parent_window->window, 
                                 NULL,
                                 lcdP->dot_color,
                                 CreateXPMdataFromLCDdata(lcdP,&test[i]));
  }
  
}

GdkPixmap *LcdDisplay::get_pixmap(gint row, gint col)
{

  if(fontP) {

    if(fontP->pixmaps[ch_data[row][col]])
      return fontP->pixmaps[ch_data[row][col]];
    else
      return fontP->pixmaps[0];
  }

  return NULL;
}


static gint
lcd_expose_event (GtkWidget *widget,
		  GdkEvent  *event,
		  gpointer   user_data)
{

  LcdDisplay *lcdP;
  guint max_width;
  guint max_height;


  g_return_val_if_fail (widget != NULL, TRUE);
  g_return_val_if_fail (GTK_IS_DRAWING_AREA (widget), TRUE);

  lcdP = (LcdDisplay *)user_data;

  max_width = widget->allocation.width;
  max_height = widget->allocation.height;

  lcdP->update(widget,max_width,max_height);

  return TRUE;
}

void LcdDisplay::update(  GtkWidget *widget,
			     guint new_width,
			     guint new_height)
{


  GdkDrawable *drawable;
  GdkGC *lcd_gc;
  guint i,j;


  drawable = widget->window;
  lcd_gc = widget->style->bg_gc[GTK_STATE_NORMAL];
  gdk_gc_set_foreground(lcd_gc,dot_color);

  w_width = new_width;
  w_height = new_height;

  gdk_draw_rectangle (drawable, lcd_gc,
		      TRUE,
		      0,
		      0,
		      w_width,
		      w_height);


  // Don't display anything if the display is disabled
  //if(display_is_off())
  //  return;

  // If there is no font, then go create it.

  if(!fontP) {
    fontP = new LcdFont(FONT_LEN,widget,this);
    //CreateFont(widget,this); //,xpm_test);
  }

  gint cw = get_char_width();
  gint ch = get_char_height();
  gint border = get_border();


  for(j=0; j<rows; j++)
    for(i=0; i<cols; i++)
      gdk_draw_pixmap (widget->window,lcd_gc,get_pixmap(j,i),
		       0,0, 
		       border+i*cw, border+j*(ch+border), 
		       cw,ch);

}

static gint
cursor_event (GtkWidget          *widget,
	      GdkEvent           *event,
	      gpointer  *user_data)
{
  if ((event->type == GDK_BUTTON_PRESS) &&
      ((event->button.button == 1) ||
       (event->button.button == 3)))
    {
      return TRUE;
    }

  return FALSE;
}


GdkColor *NewColor(gint32 red, gint32 green, gint32 blue)
{
  GdkColor *c = (GdkColor *) g_malloc(sizeof(GdkColor));


  c->red = red;
  c->green = green;
  c->blue = blue;

  gdk_color_alloc(gdk_colormap_get_system(), c);

  return c;

}


/**********************************************************
 *
 *
 */

void LcdDisplay::CreateGraphics (void)
{

  GtkWidget *window;
  GtkWidget *main_vbox;
  GtkWidget *frame;
  GtkWidget *vbox;
  GtkStyle  *style;
  gint i,j,q='A';
  char buf[30];

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  if(window) {


    //
    //   Allocate memory for the LCD font.
    //

    sprintf(buf,"%d X %d",rows,cols);
    title = (gchar *)strdup(buf);

    ch_data = (gint **)malloc(rows * sizeof(gint *));
    for(i=0; i<rows; i++) {
      ch_data[i] = (gint *)malloc(cols * sizeof(gint));
      for(j=0; j<cols; j++)
	ch_data[i][j] = q++ % FONT_LEN;
    }

    gtk_widget_realize (window);

    gtk_window_set_title(GTK_WINDOW(window), "LCD");

    gtk_signal_connect (GTK_OBJECT (window), "destroy",
			GTK_SIGNAL_FUNC (gtk_main_quit), NULL);
      
    main_vbox = gtk_vbox_new (FALSE, 5);
    gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 0);
    gtk_container_add (GTK_CONTAINER (window), main_vbox);

    vbox =
      gtk_widget_new (gtk_vbox_get_type (),
		      "GtkBox::homogeneous", FALSE,
		      //"GtkBox::spacing", 5,
		      //"GtkContainer::border_width", 10,
		      "GtkWidget::parent", main_vbox,
		      "GtkWidget::visible", TRUE,
		      NULL);


    frame =
      gtk_widget_new (gtk_frame_get_type (),
		      "GtkFrame::shadow", GTK_SHADOW_ETCHED_IN,
		      "GtkFrame::label_xalign", 0.5,
		      "GtkFrame::label", title,
		      //"GtkContainer::border_width", 10,
		      "GtkWidget::parent", vbox,
		      "GtkWidget::visible", TRUE,
		      NULL);


    darea = gtk_drawing_area_new ();
    gtk_widget_set_usize (darea, 
			  cols*get_char_width()+2*get_border(), 
			  rows*(get_char_height()+get_border())+get_border());
    gtk_container_add (GTK_CONTAINER (frame), darea);

    gtk_signal_connect (GTK_OBJECT (darea),
			"expose_event",
			GTK_SIGNAL_FUNC (lcd_expose_event),
			this);
    gtk_widget_set_events (darea, GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK);
    gtk_signal_connect (GTK_OBJECT (darea),
			"button_press_event",
			GTK_SIGNAL_FUNC (cursor_event),
			NULL);

    gtk_widget_show (darea);

    dot_color = NewColor(0x7800,0xa800,0x7800);
    gc = gdk_gc_new(darea->window);
    g_assert(gc!= (GdkGC*)NULL);

    gtk_widget_show_all (window);

  }


}

void LcdDisplay::move_cursor(int new_row, int new_column)
{

  if( (new_row >= 0  && new_row < rows)  &&
      (new_column >= 0  && new_column < cols) ) {

    cursor.row = new_row;
    cursor.col = new_column;
  }

}

void LcdDisplay::clear_display(void)
{
  int i,j;

  if(!ch_data)
    return;

  for(i=0; i<rows; i++)
    for(j=0; j<cols; j++)
      ch_data[i][j] = 0;
  
  move_cursor(0,0);
}

void LcdDisplay::write_data(int data)
{

  if(cursor.col < cols) {
    ch_data[cursor.row][cursor.col] = data & 0xff;
    cursor.col++;
  }

}

void LcdDisplay::write_ddram_address(int data)
{
  //
  // The first 0x40 memory locations are mapped to
  // row 0 and the second 0x40 to row 1. Now only
  // the first 40 (decimal not hex) locations are
  // valid RAM. And of course, only the first 20 
  //of these can be displayed in a 2x20 display.
  //

  data &= 0x7f;

  cursor.col = (data & 0x3f) % 40;
  cursor.row = (data & 0x40) ? 1 : 0;

}