Class: MatrixOrbital::GLK

Inherits:
File
  • Object
show all
Defined in:
lib/matrixorbital/glk.rb

Constant Summary collapse

TYPEMAP =
{
  0x10 => 'GLC12232',
  0x13 => 'GLC24064',
  0x15 => 'GLK24064-25',
  0x22 => 'GLK12232-25',
  0x24 => 'GLK12232-25-SM',
  0x26 => 'GLK24064-16-1U',
  0x27 => 'GLK19264-7T-1U',
  0x28 => 'GLK12232-16',
  0x29 => 'GLK12232-16-SM',
  0x72 => 'GLK240128-25'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(serialport = '/dev/ttyS0', baudrate = 19200, manual_lcd_type = nil) ⇒ GLK

Connect to an LCD screen. All of the parametes are optional. By default the LCD screen type will be detected automatically.


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
# File 'lib/matrixorbital/glk.rb', line 18

def initialize(serialport='/dev/ttyS0', baudrate=19200, manual_lcd_type=nil)

  # Does the device exist?
  unless File.exists? serialport
    raise "Serial port '#{serialport}' does not exist."
  end

  # Use the lcd_type given, or ask the module
  unless manual_lcd_type.nil?
    @lcd_type = manual_lcd_type
  end

  # Store the baudrate
  
  # Configure the serial port
  # FIXME: use pure ruby
  @baudrate = baudrate
  system("stty -F #{serialport} raw speed #{baudrate} cs8 -ixon -echo cbreak -isig -parenb > /dev/null") or
    raise "Failed to set parameters on the serial port."

  # Now, open the serial port
  super(serialport, "rb+")

  # Disable buffering
  self.sync = true
  
  # Flush the input buffer
  
end

Instance Attribute Details

#baudrateObject (readonly)

Returns the value of attribute baudrate


13
14
15
# File 'lib/matrixorbital/glk.rb', line 13

def baudrate
  @baudrate
end

Instance Method Details

#auto_transmit_key_presses=(state) ⇒ Object

When auto transmit key presses is turned on all key presses are sent immediately to the host system without the use of the poll_keypad method. This is the default mode on power up.

When auto transmit key presses is turned off up to 10 key presses are buffered until the unit is polled by the host system.


190
191
192
193
194
195
196
# File 'lib/matrixorbital/glk.rb', line 190

def auto_transmit_key_presses=(state)
  if state
    send_command( 0x41 )
  else
    send_command( 0x4F )
  end
end

#autoscroll=(state) ⇒ Object

This command enabled and disables autoscrolling. When auto scrolling is on, it causes the display to shift the entire display's contents up to make room for a new line of text when the text reaches the end of the scroll row defined in the font metrics (the bottom right character position)


171
172
173
174
175
176
177
# File 'lib/matrixorbital/glk.rb', line 171

def autoscroll=(state)
  if state
    send_command( 0x51 )
  else
    send_command( 0x52 )
  end
end

#backlight=(state) ⇒ Object

Turn the LCD backlight on/off immediately and stay on/off.


95
96
97
98
99
100
101
102
# File 'lib/matrixorbital/glk.rb', line 95

def backlight=(state)
  if state
    # FIXME: backlight hard coded to stay on permanently
    send_command( 0x42, 0 )
  else
    send_command( 0x46 )
  end
end

#brightness=(value) ⇒ Object

This command sets the display's brightness to value, where value is a value between 0 to 255.


154
155
156
157
# File 'lib/matrixorbital/glk.rb', line 154

def brightness=(value)
  raise "Brightness value is out of range" if (value<0 or value>255)
  send_command( 0x99, value )
end

#clear_key_bufferObject

This command clears any unread key presses.


180
181
182
# File 'lib/matrixorbital/glk.rb', line 180

def clear_key_buffer
  send_command( 0x45 )
end

#clear_screenObject

This command clears the display and resets the text insertion position to the top left position of the screen defined in the font metrics.


234
235
236
# File 'lib/matrixorbital/glk.rb', line 234

def clear_screen
  send_command( 0x58 )
end

#contrast=(value) ⇒ Object

This command sets the display's contrast to value, where value is a value between 0 to 255. Lower values cause 'on' elements in the display area to appear lighter, while higher values cause 'on' elements to appear darker.


140
141
142
143
# File 'lib/matrixorbital/glk.rb', line 140

def contrast=(value)
  raise "Contrast value is out of range" if (value<0 or value>255)
  send_command( 0x50, value )
end

#cursor_coordinate=(params) ⇒ Object

This command positions the insertion point at a specific pixel (X,Y), which references the top left corner of the font insertion point.

Example:

lcd.cursor_coordinate = [100,40]

131
132
133
134
# File 'lib/matrixorbital/glk.rb', line 131

def cursor_coordinate=(params)
  x,y = params
  send_command( 0x79, x, y )
end

#cursor_homeObject

This command moves the text insertion point to the top left of the display area, based on the current font metrics.


106
107
108
# File 'lib/matrixorbital/glk.rb', line 106

def cursor_home
  send_command( 0x48 )
end

#cursor_position=(params) ⇒ Object

This command sets the text insertion point to the [col] and [row] specified. The insertion point is positioned using the base size of the current font (this command does not position the insertion point at a specific pixel).

Example:

lcd.cursor_position = [10,4]

119
120
121
122
# File 'lib/matrixorbital/glk.rb', line 119

def cursor_position=(params)
  col,row = params
  send_command( 0x47, col, row )
end

#debounce_time=(ms) ⇒ Object

This command sets the time (in miliseconds) between key press and key read. All key types with the exception of latched piezo switches will 'bounce' for a varying time, depending on their physical characteristics.


218
219
220
221
222
# File 'lib/matrixorbital/glk.rb', line 218

def debounce_time=(ms)
  time = (ms.to_f / 6.554).to_i
  raise "Debounce time is out of range" if (value<0 or value>255)
  send_command( 0x63, value )
end

#delete_bitmap(refid) ⇒ Object

This command erases a single bitmap file from the LCD's internal memory.


279
280
281
# File 'lib/matrixorbital/glk.rb', line 279

def delete_bitmap(refid)
  send_command( 0xAD, 0x01, refid )
end

#delete_font(refid) ⇒ Object

This command erases a single font file from the LCD's internal memory.


284
285
286
# File 'lib/matrixorbital/glk.rb', line 284

def delete_font(refid)
  send_command( 0xAD, 0x00, refid )
end

#draw_bitmap(refid, x, y) ⇒ Object

This command will draw a bitmap that is located in the on board memory. The bitmap is referenced by the bitmaps reference identification number, which is established when the bitmap is uploaded to the display module. The bitmap will be drawn beginning at the top left, from the specified x,y coordinates.


243
244
245
# File 'lib/matrixorbital/glk.rb', line 243

def draw_bitmap(refid, x, y)
  send_command( 0x62, refid, x, y )
end

#draw_line(x1, y1, x2, y2) ⇒ Object

This command will draw a line from x1, y1 to x2, y2 using the current drawing color. Lines may be drawn from any part of the display to any other part. However, it may be important to note that the line may in-terpolate differently right to left, or left to right. This means that a line drawn in white from right to left may not fully erase the same line drawn in black from left to right.


260
261
262
# File 'lib/matrixorbital/glk.rb', line 260

def draw_line(x1, y1, x2, y2)
  send_command( 0x6C, x1, y1, x2, y2 )
end

#draw_line_continue(x, y) ⇒ Object

This command will draw a line with the current drawing color from the last line end (x2,y2) to x, y. This command uses the global drawing color.


267
268
269
# File 'lib/matrixorbital/glk.rb', line 267

def draw_line_continue(x, y)
  send_command( 0x65, x, y )
end

#draw_pixel(x, y) ⇒ Object

This command will draw a pixel at x, y using the current drawing color.


249
250
251
# File 'lib/matrixorbital/glk.rb', line 249

def draw_pixel(x, y)
  send_command( 0x70, x, y )
end

#draw_rect(color, x1, y1, x2, y2) ⇒ Object

This command draws a rectangular box in the specified color. The top left corner is specified by x1, y1 and the bottom right corner by x2, y2.


274
275
276
# File 'lib/matrixorbital/glk.rb', line 274

def draw_rect(color, x1, y1, x2, y2)
  send_command( 0x72, color, x1, y1, x2, y2 )
end

#draw_solid_rect(color, x1, y1, x2, y2) ⇒ Object

This command draws a solid rectangle in the specified color. The top left corner is specified by x1, y1 and the bottom right corner by x2, y2. Since this command involves considerable processing overhead, we strongly recommend the use of flow control, particularly if the command is to be repeated frequently.


341
342
343
# File 'lib/matrixorbital/glk.rb', line 341

def draw_solid_rect(color, x1, y1, x2, y2)
  send_command( 0x78, color, x1, y1, x2, y2 )
end

#drawing_color=(color) ⇒ Object

This command sets the drawing color for subsequent graphic commands that do not have the drawing color passed as a parameter. The parameter color is the value of the color where white is <em>false<em> and black <em>true<em>.


228
229
230
# File 'lib/matrixorbital/glk.rb', line 228

def drawing_color=(color)
  send_command( 0x63, color ? 0 : 1 )
end

#filesystem_directoryObject

This command will return a directory of the contents of the file system. It returns an array of directory entires, where each entry is a hash.


318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/matrixorbital/glk.rb', line 318

def filesystem_directory
  send_command( 0xB3 )

  #my lsb = getchar()
  
  #my @bytes = getbytes( 4 )

  #my count = 0;
  #count |= (@bytes[0] & 0xFF) << 0;
  #count |= (@bytes[1] & 0xFF) << 8;
  #count |= (@bytes[2] & 0xFF) << 16;
  #count |= (@bytes[3] & 0xFF) << 24;
  
  #return count;
  
  #return lsb;
end

#filesystem_spaceObject

This command will return the number of bytes that are remaining in the on board memory.


303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/matrixorbital/glk.rb', line 303

def filesystem_space
  send_command( 0xAF )

  #my count = getint()
  
  #count |= ( & 0xFF) << 0;
  #count |= (getchar() & 0xFF) << 8;
  #count |= (getchar() & 0xFF) << 16;
  #count |= (getchar() & 0xFF) << 24;
  #
  #return count;
end

#firmware_versionObject

Returns the firmware version of the LCD module that you are communicating with as a dotted integer (for example '5.4').


414
415
416
417
418
419
420
421
422
423
424
425
# File 'lib/matrixorbital/glk.rb', line 414

def firmware_version
  return @firmware_version unless @firmware_version.nil?

  # Request firmware version number
  send_command( 0x36 )

  # Read back one byte
  value = getc
  major = (value & 0xF0) >> 4
  minor = value & 0x0F
  return @firmware_version = "#{major}.#{minor}"
end

#flow_control=(state) ⇒ Object

Turn flow control on or off.


84
85
86
87
88
89
90
91
# File 'lib/matrixorbital/glk.rb', line 84

def flow_control=(state)
  if state
    raise "Flow control is unsupported"
    #  send_command( 0x3A )
  else
    send_command( 0x3B )
  end
end

#font=(refid) ⇒ Object

Set the current font to the specified font refernce identifer. The font ID is es-tablished when the font is saved to the display.


290
291
292
# File 'lib/matrixorbital/glk.rb', line 290

def font=(refid)
  send_command( 0x31, refid )
end

#gpo_off(num) ⇒ Object

This command turns Off general purpose output num.


346
347
348
# File 'lib/matrixorbital/glk.rb', line 346

def gpo_off(num)
   send_command( 0x56, num )
end

#gpo_on(num) ⇒ Object

This command turns On general purpose output num.


351
352
353
# File 'lib/matrixorbital/glk.rb', line 351

def gpo_on(num)
  send_command( 0x57, num )
end

#i2c_slave_address=(address) ⇒ Object

This command sets the I2C write address of the module between 0x00 and 0xFF. The I2C write address must be an even number and the read address is automatically set to one higher. For example if the I2 C write address is set to 0x50, then the read address is 0x51.


52
53
54
55
# File 'lib/matrixorbital/glk.rb', line 52

def i2c_slave_address=(address)
  raise "I2C slave address is out of range" if (value<0 or value>255)
  send_command( 0x33, address )
end

#lcd_baudrate=(lcd_baudrate) ⇒ Object

This command sets the lcd's RS-232 port to the specified baudrate. The change takes place immediately.


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/matrixorbital/glk.rb', line 59

def lcd_baudrate=(lcd_baudrate)
  case lcd_baudrate
    when 9600 then
      send_command( 0x39, 0xCF )
    when 14400 then
      send_command( 0x39, 0x8A )
    when 19200 then
      send_command( 0x39, 0x67 )
    when 28800 then
      send_command( 0x39, 0x44 )
    when 38400 then
      send_command( 0x39, 0x33 )
    when 57600 then
      send_command( 0x39, 0x22 )
    when 76800 then
      send_command( 0x39, 0x19 )
    when 115200 then
      send_command( 0x39, 0x10 )
    else
      raise "Invalid/unsupported baud rate: #{lcd_baudrate}"
  end
end

#lcd_dimensionsObject

Returns the dimensions (in pixels) of the LCD module you are talking to as an array, width followed by height.


460
461
462
463
464
465
466
467
# File 'lib/matrixorbital/glk.rb', line 460

def lcd_dimensions
  # Parse the LCD type to work out the dimensions
  if (lcd_type =~ /^(GLC|GLK)(\d{3})(\d{2})-|$/)
    return [$2,$3]
  else
    raise "Can't get screen dimensions: unknown LCD module"
  end
end

#lcd_typeObject

Return the Product Idenfier for the LCD module (for example 'GLK24064-25'). This can be determined automatically or passed as a parameter to new().


443
444
445
446
447
448
449
450
451
452
453
454
455
456
# File 'lib/matrixorbital/glk.rb', line 443

def lcd_type
  return @lcd_type unless @lcd_type.nil?

  # Request LCD module type
  send_command( 0x37 )

  # Read back one byte
  type = getc
  if TYPEMAP.has_key?(type)
    @lcd_type = TYPEMAP[type]
  else
    @lcd_type = "Unknown-#{type}"
  end
end

#led_green(num) ⇒ Object

Turn LED number num on - Green.

The command is only supported on LCD modules with on-board LEDS (such as the GLK19264-7T-1U)


395
396
397
398
399
# File 'lib/matrixorbital/glk.rb', line 395

def led_green(num)
  gpo_base = led_gpo_base(num)
  gpo_on( gpo_base )
  gpo_off( gpo_base+1 )
end

#led_off(num) ⇒ Object

Turn LED number num off.

The command is only supported on LCD modules with on-board LEDS (such as the GLK19264-7T-1U)


375
376
377
378
379
# File 'lib/matrixorbital/glk.rb', line 375

def led_off(num)
  gpo_base = led_gpo_base(num)
  gpo_on( gpo_base )
  gpo_on( gpo_base+1 )
end

#led_red(num) ⇒ Object

Turn LED number num on - Red.

The command is only supported on LCD modules with on-board LEDS (such as the GLK19264-7T-1U)


385
386
387
388
389
# File 'lib/matrixorbital/glk.rb', line 385

def led_red(num)
  gpo_base = led_gpo_base(num)
  gpo_off( gpo_base )
  gpo_on( gpo_base+1 )
end

#led_yellow(num) ⇒ Object

Turn LED number num on - Yellow.

The command is only supported on LCD modules with on-board LEDS (such as the GLK19264-7T-1U)


405
406
407
408
409
# File 'lib/matrixorbital/glk.rb', line 405

def led_yellow(num)
  gpo_base = led_gpo_base(num)
  gpo_off( gpo_base )
  gpo_off( gpo_base+1 )
end

#poll_key_pressObject

This command returns any buffered key presses as an array of key codes. When the display receives this command, it will immediately return any buffered key presses which may have not been read already.


201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/matrixorbital/glk.rb', line 201

def poll_key_press
  send_command( 0x26 ) 
  
  keys = []
  begin
    # Read in key preses, while the Most Significant Bit is set
    key = getc
    keys << (key & 0x7F) if (key != 0x00)
  end while (key & 0x80)
  
  return keys
end

#puts_stdout(*args) ⇒ Object

Send text string to display and STDOUT too


365
366
367
368
# File 'lib/matrixorbital/glk.rb', line 365

def puts_stdout(*args)
  $stdout.puts(*args)
  self.puts(*args)
end

#save_brightness(brightness) ⇒ Object

Like the brightness= method, only this command saves the value so that it is not lost after power down.


161
162
163
164
# File 'lib/matrixorbital/glk.rb', line 161

def save_brightness(brightness)
  raise "Brightness value is out of range" if (value<0 or value>255)
  send_command( 0x98, brightness )
end

#save_contrast(value) ⇒ Object

Like the contrast= method, only this command saves the value so that it is not lost after power down.


147
148
149
150
# File 'lib/matrixorbital/glk.rb', line 147

def save_contrast(value)
  raise "Contrast value is out of range" if (value<0 or value>255)
  send_command( 0x91, value )
end

#send_command(command, *args) ⇒ Object

Send a raw command to the display, where <em>args<em> is an array of integer bytes to be sent to the lcd module.


359
360
361
362
# File 'lib/matrixorbital/glk.rb', line 359

def send_command(command, *args)
  args.unshift(0xFE, command)
  self.print( args.pack('C*') )
end

#wipe_filesystemObject

This command completely erases the display's non-volatile memory. It removes all fonts, font metrics, bitmaps, and settings (current font, cursor position, communication speed, etc.).


297
298
299
# File 'lib/matrixorbital/glk.rb', line 297

def wipe_filesystem
  send_command( 0x21, 0x59, 0x21 )
end