this supports 4x3, 4x4, 4x5 + red, yelllow, green, blue menu buttons
under display + 4 navkeys + enter or 65 key "qwerty" as 8x 8 with
Altkey by firing ctrl & Alt.

Also in any column on the 5x5, you can easily make r, y, g, b or enter
key work as Shift

I tried the tortoise CVS/SVN thing and didn't like it integrating with
Explorer. Can someone suggest a different client for librarian?


This is a work in progress:

keypad5.jal
-- Title: keyboard, scans 4x4 keybaord and returns text on key, 0x00
if none pressed
-- 65 keypad will be pbased on this.
-- 254 is last key no change
-- is error
--
-- Author: Javier Martínez, Copyright (c) 2003, all rights reserved.
-- Adapted-by: Eur van Andel, [email protected], Joep Suijs
-- Adapted by: Michael Watterson 2010
-- Compiler: >=2.4g
--
-- This file is part of jallib (http://jallib.googlecode.com)
-- Released under the ZLIB license (http://www.opensource.org/licenses/
zlib-license.html)
--
-- Description: This library is used to scan a 4x3 or 4x4 keyboard.
-- returning text "1" .. "9", "0", "*", "#", "A" .. "D"
-- Additionally a 5th row and col add 4 menu keys Red, Green, Yellow,
Blue
-- returning "r", "g", "y" & "b"
-- and 5 way Navipad (or 5 separate switches) Up, Left, Right, down &
Enter
-- returning "^", "<", ">", "v" and code 13 (CR key or Ctrl M)
--
-- To use it, you need to define four variables:
--    keydrive is the  port  with the cols connected.
--    keydrive_direction is the related direction register.
--    keyscan  is the  port with the rows connected.
-- --
--    Example:
-- --
--    var volatile byte keyscan  is portb      -- rows of keyboard
--    var volatile byte keyscan_direction is portb_direction
--    var volatile byte keydrive is portd      -- columns of keyboard
--    var volatile byte keydrive_direction is portd_direction
--    include keypad5
-- Note: if you don't want keyboard.jal change the direction of your
pins, make sure all
-- keydrive pins are set to output before you call the library and
define:
--    var volatile byte keydrive_direction is keydrive
-- --
-- The library has two functions to read the keyboard:
-- getkey()       returns the key pressed.
-- getkey_once()  returns a pressed key only once (and 'none', 0x10
until the next key is pressed)
-- --
-- Hardware setup: connect the keyboard rows to a half port (higher or
lower 4 bits of a port)
-- and connect the cols to a half port. Put a pull-down resistor (10k
- 47k) on each row pin.
-- Adaptation: soldering your wires in the right order is a lot harder
than you think.
-- Make sure you keep the rows and cols together and connect them to
right port.
-- You could either adapt your wiring to match the default
configuration or reconfigure the
-- library by defining the next 8 constants before you include
keyboard.jal, like:
--    const row1 = 0b0000_0100
--    const row2 = 0b0000_0001
--    const row3 = 0b0000_0010
--    const row4 = 0b0000_1000
--
--    const col1 = 0b0000_1000
--    const col2 = 0b0000_0010
--    const col3 = 0b0000_0100
--    const col4 = 0b0000_0001
-- Connect the keyboard and press the buttons. Adapt the row and
column constants so
-- pressing the buttons give the right result.
-- Note: you need to specify all 8 constants, even when you have a 4x3
keyboard. In
-- this case you can specify:
--    const col4 = 0b0000_0000
-- --
-- --
--      1 2 3 A   ^  row1
--      4 5 6 B   <  row2
--      7 8 9 C   v  row3
--      * 0 # D   >  row4
--      R G Y B   E
-- --
--  col 1 2 3 4   5
--

if (defined(row1) == false) then
   ; use default row/col definition
   const row1 = 0b0000_0001
   const row2 = 0b0000_0010
   const row3 = 0b0000_0100
   const row4 = 0b0000_1000
   const row5 = 0b0001_0000
   const srow1 = 0b0001_0001
   const srow2 = 0b0001_0010
   const srow3 = 0b0001_0100
   const srow4 = 0b0001_1000

   const col1 = 0b0000_0001
   const col2 = 0b0000_0010
   const col3 = 0b0000_0100
   const col4 = 0b0000_1000
   const col5 = 0b0001_0000
end if

--
----------------------------------------------------------------------------
-- getkey - return the key pressed (until it is released)
--
----------------------------------------------------------------------------
-- return:  "0 .. 9"      numeric key pressed
--          "*"          *
--           "#"          #
--          "A ..D"      A .. D
--          0      no key pressed.
--          255          error (more then one key pressed)
--
----------------------------------------------------------------------------
function getkey() return byte is
var byte scan1, scan2, scan3, scan4, scan5
var byte oldScanDir, oldDriveDir
-- we may be sharing ports with Graphics LCD, so save & restore
direction
   oldDriveDir = keydrive_direction
   oldScanDir = keyScan_direction
   keyscan_direction =  all_input
         --               LSB        MSB
   keydrive_direction =  ! col1
   keydrive = col1
   scan1 = keyscan & 0x1F  -- active col [1] [4] [7] [*]  [r]
   keydrive_direction = ! col2
   keydrive = col2
   scan2 = keyscan & 0x1F  -- active col [2] [5] [8] [0]  [g]
   keydrive_direction = ! col3
   keydrive = col3
   scan3 = keyscan & 0x1F  -- active col [3] [6] [9] [#]  [y]
   keydrive_direction = ! col4
   keydrive = col4
   scan4 = keyscan & 0x1F  -- active col [A] [B] [C] [D]  [b]
   keydrive_direction = ! col5
   keydrive = col5
   scan5 = keyscan & 0x1F  -- active col [^] [<] [v] [>]  [e]

   keydrive_direction = oldDriveDir
   keyscan_direction =  oldScanDir

   if  ( scan4 == 0x00 ) &( scan3 == 0x00 ) & ( scan2 == 0x00  ) &
      ( scan1 == 0x00 ) & ( scan5 == 0x00  ) then return 0x0 -- null

   elsif scan1 == row1 then return "1"
   elsif scan1 == row2 then return "4"
   elsif scan1 == row3 then return "7"
   elsif scan1 == row4 then return "*"
   elsif scan1 == row5 then return "r"

   elsif scan2 == row1 then return "2"
   elsif scan2 == row2 then return "5"
   elsif scan2 == row3 then return "8"
   elsif scan2 == row4 then return "0"
   elsif scan2 == row5 then return "g"

   elsif scan3 == row1 then return "3"
   elsif scan3 == row2 then return "6"
   elsif scan3 == row3 then return "9"
   elsif scan3 == row4 then return "#"
   elsif scan3 == row5 then return "y"

   elsif scan4 == row1 then return "A"
   elsif scan4 == row2 then return "B"
   elsif scan4 == row3 then return "C"
   elsif scan4 == row4 then return "D"
   elsif scan4 == row5 then return "b"

   elsif scan5 == row1 then return 1   -- Ctrl A  (Esc A is ANSI Up )
   elsif scan5 == row2 then return 2   -- Ctrl B  (Esc A is ANSI
Down )
   elsif scan5 == row3 then return 3   -- Ctrl C   (Esc A is ANSI
Right )
   elsif scan5 == row4 then return 4   -- Ctrl D   (Esc A is ANSI
Left )
   elsif scan5 == row5 then return 13  -- CR key or Enter, Ctrl M
   elsif scan5 == srow1 then return "^"
   elsif scan5 == srow2 then return "v"
   elsif scan5 == srow3 then return ">"
   elsif scan5 == srow4 then return "<"
   end if

   return 255              -- error

end function

var byte lastkey = 254

--
----------------------------------------------------------------------------
-- getkey_once - return the value of a key pressed only once
--
----------------------------------------------------------------------------
-- returns: see getkey()
--
----------------------------------------------------------------------------
function getkey_once() return byte is

   var byte key = getkey()

   if (key == lastkey) then return 254 end if
   lastkey = key
   return key

end function
--
--------------------------------------------------------------------------------------
-- END keypad5.jal
--
--------------------------------------------------------------------------------------

modified ks0108.jal
-- Title: lcd_ks0108 - Library for KS0108 compatible LCD
-- Author: Serkan Ayyýldýz Copyright (c) 2006..2009, all rights
reserved.
-- Adapted-by: Joep Suijs
-- some small changes: Michael Watterson (c)2010
-- Compiler: >=2.2
--
-- This file is part of jallib (http://jallib.googlecode.com)
-- Released under the ZLIB license (http://www.opensource.org/licenses/
zlib-license.html)
--
-- Sources:
--
-- Description: Library for KS0108 compatible graphic lcd with 128x64
resolution.
--
--
-- Notes:
-- See schematic for Reset pin. It's not SW driven.
--
const KS0108_LEFT = 0
const KS0108_RIGHT = 1
const KS0108_CMD_ON = 0x3F
const KS0108_CMD_OFF = 0x3E
const KS0108_CMD_PAGE = 0xB8
const KS0108_CMD_COLUMN = 0x40
const KS0108_CMD_TOP_RAM = 0xC0

-- Purpose: Write a byte of data to the specified chip
-- Inputs: 1) chipSelect - which chip to write the data to
-- 2) data - the byte of data to write
procedure _ks0108_write(byte in side, byte in data) is
if side == 1 then -- Choose which side to write to
GLCD_CS2 = high
else
GLCD_CS1 = high
end if
GLCD_RW = low -- Set for writing
GLCD_DATAPRT = data -- Put the data on the port
GLCD_DATAPRT_DIR = all_output
asm nop asm nop -- delay_cycles(1)
GLCD_E = high -- Pulse the enable pin
asm nop asm nop asm nop asm nop asm nop
asm nop asm nop asm nop asm nop asm nop
--delay2_us
GLCD_E = low
GLCD_CS1 = low -- Reset the chip select lines
GLCD_CS2 = low
--delay_2us()
asm nop asm nop asm nop asm nop asm nop
asm nop asm nop asm nop asm nop asm nop
end procedure

-- Purpose: Reads a byte of data from the specified chip
-- Ouputs: A byte of data read from the chip
function _ks0108_read(byte in side) return byte is
var byte data -- Stores the data read from the LCD
GLCD_DATAPRT_DIR = all_input -- Set port d to input

if side == 1 then -- Choose which side to write to
GLCD_CS2 = high
else
GLCD_CS1 = high
end if
GLCD_RW = high -- Set for reading

asm nop asm nop -- delay_cycles(1)
GLCD_E = high -- Pulse the enable pin

asm nop asm nop asm nop asm nop asm nop
asm nop asm nop asm nop asm nop asm nop
data = GLCD_DATAPRT -- Get the data from the display's output register
GLCD_E = low

GLCD_CS1 = low -- Reset the chip select lines
GLCD_CS2 = low
asm nop asm nop asm nop asm nop asm nop
asm nop asm nop asm nop asm nop asm nop

return data -- Return the read data
end function

-- Purpose: Turn the display on
procedure lcd_on() is
_ks0108_write(KS0108_LEFT, KS0108_CMD_ON)
_ks0108_write(KS0108_RIGHT, KS0108_CMD_ON)
end procedure

-- Purpose: Turn the display off
procedure lcd_off() is
_ks0108_write(KS0108_LEFT, KS0108_CMD_OFF)
_ks0108_write(KS0108_RIGHT, KS0108_CMD_OFF)
end procedure

-- Purpose: Set the page number
-- Inputs: A page number (0 - 7)
procedure _ks0108_page(byte in side , byte in page) is
_ks0108_write(side, KS0108_CMD_PAGE | page)
end procedure

-- Purpose: Set the column address
-- Inputs: The column address (0 - 63)
procedure _ks0108_column(byte in side, byte in column) is
_ks0108_write(side, KS0108_CMD_COLUMN | column)
end procedure

-- Purpose: Specify reads and writes are instructions
procedure _ks0108_inst() is
GLCD_DI = low
end procedure

-- Purpose: Specify reads and writes are data
procedure _ks0108_data() is
GLCD_DI = high
end procedure

-- Purpose: Turn a pixel on a graphic LCD on or off
-- Inputs: 1) x - the x coordinate of the pixel
-- 2) y - the y coordinate of the pixel
-- 3) color - ON or OFF
procedure lcd_write_pixel(byte in x, byte in y, bit in onoff) is
var byte data , yy
var byte side = KS0108_LEFT -- Stores which chip to use on the LCD

if x > 63 then -- Check for first or second display area
x = x - 64
side = KS0108_RIGHT
end if

_ks0108_inst() -- Set for instruction
_ks0108_column(side,x) -- Set the horizontal address
_ks0108_page(side,(y / 8)) -- Set the page address
_ks0108_data() -- Set for data
_ks0108_read(side) -- Need two reads to get data at new address
data = _ks0108_read(side)

if onoff == 1 then
-- bit_set(data, y%8) -- Turn the pixel on
yy = y % 8
data = data | ( 1 << yy )
else -- or
-- bit_clear(data, y%8) -- turn the pixel off
yy = y % 8
data = data & !( 1 << yy )
end if
_ks0108_inst() -- Set for instruction
_ks0108_column(side,x) -- Set the horizontal address
_ks0108_data() -- Set for data
_ks0108_write(side, data) -- Write the pixel data
end procedure

procedure _ks0108_write_byte(byte in x, byte in y, byte in veri) is
var byte side = KS0108_LEFT -- Stores which chip to use on the LCD

if x > 63 then -- Check for first or second display area
x = x - 64
side = KS0108_RIGHT
end if

_ks0108_inst() -- Set for instruction
_ks0108_column(side,x) -- Set the horizontal address
_ks0108_page(side,(y / 8)) -- Set the page address

_ks0108_data() -- Set for data
_ks0108_write(side, ! veri) -- Write the pixel data
end procedure

-- Purpose: Fill the LCD screen with the passed in color
-- Inputs: ON - turn all the pixels on
-- OFF - turn all the pixels off
procedure lcd_fill(byte in data) is
var byte i, j
i = 0 -- Loop through the vertical pages
for 8 loop
_ks0108_inst() -- Set for instruction
_ks0108_page(KS0108_LEFT,i) -- Set page address
_ks0108_page(KS0108_RIGHT,i)
_ks0108_column(KS0108_LEFT,0) -- Set horizontal address to 0
_ks0108_column(KS0108_RIGHT,0)
_ks0108_data() -- Set for data

-- Loop through the horizontal sections
for 64 loop
_ks0108_write(KS0108_LEFT ,data) -- Write the byte of data
_ks0108_write(KS0108_RIGHT,data)
end loop
i = i + 1
end loop
end procedure

-- Purpose: Initialize the LCD.
-- Call before using any other LCD function.
procedure lcd_init() is
-- Initialze some pins

GLCD_DATAPRT = 0x00
GLCD_DATAPRT_DIR = all_output

GLCD_RW_DIRECTION = output
GLCD_CS1_DIRECTION = output
GLCD_E_DIRECTION = output
GLCD_DI_DIRECTION = output
--GLCD_RST_DIRECTION = output
GLCD_CS2_DIRECTION = output

--GLCD_RST = high
GLCD_E = low
GLCD_CS1 = low

_ks0108_inst() -- Set for instruction
_ks0108_write(KS0108_LEFT, KS0108_CMD_TOP_RAM | 0) -- First RAM line
at the top of the screen
_ks0108_write(KS0108_RIGHT, KS0108_CMD_TOP_RAM | 0) -- First RAM line
at the top of the screen
_ks0108_page (KS0108_LEFT ,0) -- Set the page address to 0
_ks0108_page (KS0108_RIGHT,0)
_ks0108_column(KS0108_LEFT ,0) -- Set the column address to 0
_ks0108_column(KS0108_RIGHT,0)
lcd_on() -- Turn the display on


lcd_fill(0) -- Clear the display
end procedure


; 91 * 5 bytes = 455
const byte _font_5x7_table[] = {
0x00, 0x00, 0x00, 0x00, 0x00, ;; space, 32
0x00, 0x00, 0x2f, 0x00, 0x00, ;; !, 33
0x00, 0x07, 0x00, 0x07, 0x00, ;; ", 34
0x14, 0x7f, 0x14, 0x7f, 0x14, ;; #, 35
0x24, 0x2a, 0x7f, 0x2a, 0x12, ;; $, 36
0xc4, 0xc8, 0x10, 0x26, 0x46, ;; %, 37
0x36, 0x49, 0x55, 0x22, 0x50, ;; &, 38
0x00, 0x05, 0x03, 0x00, 0x00, ;; ', 39
0x00, 0x1c, 0x22, 0x41, 0x00, ;; (, 40
0x00, 0x41, 0x22, 0x1c, 0x00, ;; ), 41
0x14, 0x08, 0x3E, 0x08, 0x14, ;; *, 42
0x08, 0x08, 0x3E, 0x08, 0x08, ;; +, 43
0x00, 0x00, 0x50, 0x30, 0x00, ;; ,, 44
0x10, 0x10, 0x10, 0x10, 0x10, ;; -, 45
0x00, 0x60, 0x60, 0x00, 0x00, ;; ., 46
0x20, 0x10, 0x08, 0x04, 0x02, ;; /, 47
0x3E, 0x51, 0x49, 0x45, 0x3E, ;; 0, 48
0x00, 0x42, 0x7F, 0x40, 0x00, ;; 1, 49
0x42, 0x61, 0x51, 0x49, 0x46, ;; 2, 50
0x21, 0x41, 0x45, 0x4B, 0x31, ;; 3, 51
0x18, 0x14, 0x12, 0x7F, 0x10, ;; 4, 52
0x27, 0x45, 0x45, 0x45, 0x39, ;; 5, 53
0x3C, 0x4A, 0x49, 0x49, 0x30, ;; 6, 54
0x01, 0x71, 0x09, 0x05, 0x03, ;; 7, 55
0x36, 0x49, 0x49, 0x49, 0x36, ;; 8, 56
0x06, 0x49, 0x49, 0x29, 0x1E, ;; 9, 57
0x00, 0x36, 0x36, 0x00, 0x00, ;; :, 58
0x00, 0x56, 0x36, 0x00, 0x00, ;; ;, 59
0x08, 0x14, 0x22, 0x41, 0x00, ;; <, 60
0x14, 0x14, 0x14, 0x14, 0x14, ;; =, 61
0x00, 0x41, 0x22, 0x14, 0x08, ;; >, 62
0x02, 0x01, 0x51, 0x09, 0x06, ;; ?, 63
0x32, 0x49, 0x59, 0x51, 0x3E, ;; @, 64
0x7E, 0x11, 0x11, 0x11, 0x7E, ;; A, 65
0x7F, 0x49, 0x49, 0x49, 0x36, ;; B, 66
0x3E, 0x41, 0x41, 0x41, 0x22, ;; C, 67
0x7F, 0x41, 0x41, 0x22, 0x1C, ;; D, 68
0x7F, 0x49, 0x49, 0x49, 0x41, ;; E, 69
0x7F, 0x09, 0x09, 0x09, 0x01, ;; F, 70
0x3E, 0x41, 0x49, 0x49, 0x7A, ;; G, 71
0x7F, 0x08, 0x08, 0x08, 0x7F, ;; H, 72
0x00, 0x41, 0x7F, 0x41, 0x00, ;; I, 73
0x20, 0x40, 0x41, 0x3F, 0x01, ;; J, 74
0x7F, 0x08, 0x14, 0x22, 0x41, ;; K, 75
0x7F, 0x40, 0x40, 0x40, 0x40, ;; L, 76
0x7F, 0x02, 0x0C, 0x02, 0x7F, ;; M, 77
0x7F, 0x04, 0x08, 0x10, 0x7F, ;; N, 78
0x3E, 0x41, 0x41, 0x41, 0x3E, ;; O, 79
0x7F, 0x09, 0x09, 0x09, 0x06, ;; P, 80
0x3E, 0x41, 0x51, 0x21, 0x5E, ;; Q, 81
0x7F, 0x09, 0x19, 0x29, 0x46, ;; R, 82
0x46, 0x49, 0x49, 0x49, 0x31, ;; S, 83
0x01, 0x01, 0x7F, 0x01, 0x01, ;; T, 84
0x3F, 0x40, 0x40, 0x40, 0x3F, ;; U, 85
0x1F, 0x20, 0x40, 0x20, 0x1F, ;; V, 86
0x3F, 0x40, 0x38, 0x40, 0x3F, ;; W, 87
0x63, 0x14, 0x08, 0x14, 0x63, ;; X, 88
0x07, 0x08, 0x70, 0x08, 0x07, ;; Y, 89
0x61, 0x51, 0x49, 0x45, 0x43, ;; Z, 90
0x00, 0x7F, 0x41, 0x41, 0x00, ;; [, 91
0x55, 0x2A, 0x55, 0x2A, 0x55, ;; 5, 92
0x00, 0x41, 0x41, 0x7F, 0x00, ;; ], 93
0x04, 0x02, 0x01, 0x02, 0x04, ;; ^, 94
0x40, 0x40, 0x40, 0x40, 0x40, ;; _, 95
0x00, 0x01, 0x02, 0x04, 0x00, ;; ', 96
0x20, 0x54, 0x54, 0x54, 0x78, ;; a, 97
0x7F, 0x48, 0x44, 0x44, 0x38, ;; b, 98
0x38, 0x44, 0x44, 0x44, 0x20, ;; c, 99
0x38, 0x44, 0x44, 0x48, 0x7F, ;; d, 100
0x38, 0x54, 0x54, 0x54, 0x18, ;; e, 101
0x08, 0x7E, 0x09, 0x01, 0x02, ;; f, 102
0x0C, 0x52, 0x52, 0x52, 0x3E, ;; g, 103
0x7F, 0x08, 0x04, 0x04, 0x78, ;; h, 104
0x00, 0x44, 0x7D, 0x40, 0x00, ;; i, 105
0x20, 0x40, 0x44, 0x3D, 0x00, ;; j, 106
0x7F, 0x10, 0x28, 0x44, 0x00, ;; k, 107
0x00, 0x41, 0x7F, 0x40, 0x00, ;; l, 108
0x7C, 0x04, 0x18, 0x04, 0x78, ;; m, 109
0x7C, 0x08, 0x04, 0x04, 0x78, ;; n, 110
0x38, 0x44, 0x44, 0x44, 0x38, ;; o, 111
0x7C, 0x14, 0x14, 0x14, 0x08, ;; p, 112
0x08, 0x14, 0x14, 0x18, 0x7C, ;; q, 113
0x7C, 0x08, 0x04, 0x04, 0x08, ;; r, 114
0x48, 0x54, 0x54, 0x54, 0x20, ;; s, 115
0x04, 0x3F, 0x44, 0x40, 0x20, ;; t, 116
0x3C, 0x40, 0x40, 0x20, 0x7C, ;; u, 117
0x1C, 0x20, 0x40, 0x20, 0x1C, ;; v, 118
0x3C, 0x40, 0x30, 0x40, 0x3C, ;; w, 119
0x44, 0x28, 0x10, 0x28, 0x44, ;; x, 120
0x0C, 0x50, 0x50, 0x50, 0x3C, ;; y, 121
0x44, 0x64, 0x54, 0x4C, 0x44 ;; z, 122

}

;; Writes an inverted character to the display
;; note: the vertical position of chars is in 8-bit steps.
procedure lcd_write_char(byte in x, byte in y, byte in ch ) is
var word indx = 0
var byte cx

if (ch < 32) then return end if
if (ch > 122) then return end if

indx = indx + 5 * word(ch - 32)

for 5 loop
cx = _font_5x7_table[indx]

_ks0108_inst() -- Set for instruction

if (x < 0) then
-- ignore (this enables us to write partial chars on the edge)
elsif (x < 64) then
_ks0108_column(KS0108_LEFT,x) -- Set the horizontal address
_ks0108_page(KS0108_LEFT, (y / 8)) -- Set the page address
_ks0108_data() -- Set for data
_ks0108_write(KS0108_LEFT, cx) -- Write the pixel data
elsif (x < 128) then
_ks0108_column(KS0108_RIGHT, x-64) -- Set the horizontal address
_ks0108_page(KS0108_RIGHT, (y / 8)) -- Set the page address
_ks0108_data() -- Set for data
_ks0108_write(KS0108_RIGHT, cx) -- Write the pixel data
end if
-- x > 128 is also ignored (this enables us to write partial chars on
the edge)

indx = indx + 1
x = x + 1
end loop
end procedure
--
--------------------------------------------------------------------------------------
-- END ks0108.jal
--
--------------------------------------------------------------------------------------

updated graphics stuff  glcd_common.jal

-- Title: glcd_common - generic routines for graphic lcd
-- Author: Stef Mientki, Serkan Ayyýldýz Copyright (c) 2006..2009, all
rights reserved.
-- Adapted-by: Joep Suijs
-- Added to by Michael Watterson
-- Compiler: >=2.2
--
-- This file is part of jallib (http://jallib.googlecode.com)
-- Released under the ZLIB license (http://www.opensource.org/licenses/
zlib-license.html)
--
-- Sources:
--
-- Description: Generic routines for graphic lcd.
-- glcd_box
-- glcd_orth_ line
-- --
-- And mapping to char based lcd:
-- lcd_put
-- lcd_...
--
-- Notes:
-- Added by Michael Watterson. All take ink so you can erase them
-- to do: Add XOR drawing, Bold on Text by OR with same font offset by
1 pixel
-- Arbitrary lines LCD_Line
-- circle

-- ----------------------------------------------------------
-- ----------------------------------------------------------
-- Only does 45 degrees, Hor or Vert
procedure lcd_orth_line(byte in x0, byte in y0, byte in x1, byte in
y1) is
 var byte xi,yi , xfark,yfark, fark , xx

   xi = x0
   yi = y0
   if x1 >= x0  then  xfark = x1 - x0 else  xfark = x0 - x1 end if
   if y1 >= y0  then  yfark = y1 - y0 else  yfark = y0 - y1 end if

   if xfark >= yfark then fark = xfark else fark = yfark end if
   xx = 0
   for fark loop
      lcd_write_pixel ( xi, yi ,1)
      if xx < xfark then
          if x1 >= x0 then xi = xi + 1 else xi = xi - 1 end if
      end if
      if xx < yfark then
          if y1 >= y0 then yi = yi + 1 else yi = yi - 1 end if
      end if
      xx = xx + 1
   end loop
end procedure

-- ----------------------------------------------------------

procedure lcd_circle(byte in cx, byte in cy ,byte in radius, bit in
color)  is
var sbyte x, y, xchange, ychange, radiusError

    x = radius
    y = 0
    xchange = 1 - 2 * radius
    ychange = 1
    radiusError = 0
    while(x >= y) loop
        lcd_write_pixel(cx+x, cy+y, color)
        lcd_write_pixel(cx-x, cy+y, color)
        lcd_write_pixel(cx-x, cy-y, color)
        lcd_write_pixel(cx+x, cy-y, color)
        lcd_write_pixel(cx+y, cy+x, color)
        lcd_write_pixel(cx-y, cy+x, color)
        lcd_write_pixel(cx-y, cy-x, color)
        lcd_write_pixel(cx+y, cy-x, color)
        y= y+1
        radiusError = radiusError+ ychange
        ychange = ychange + 2
            if ( (2*radiusError + xchange) > 0 )  then
               x = x-1
                     radiusError = radiusError +xchange
                     xchange = xchange + 2
                 End if
    End Loop
End procedure
-- ----------------------------------------------------------
procedure lcd_box(byte in x0, byte in y0, byte in x1, byte in y1) is
   lcd_orth_line ( x0,y0, x1,y0 )
   lcd_orth_line ( x1,y0, x1,y1 )
   lcd_orth_line ( x1,y1, x0,y1 )
   lcd_orth_line ( x0,y1, x0,y0 )
end procedure


var byte glcd_char_x_pos = 0;
var byte glcd_char_y_pos = 0;
-- ----------------------------------------------------------
-- ----------------------------------------------------------
procedure LCD_Line(byte in X1, byte in Y1, byte in X2, byte in Y2, bit
in color) is
var byte  plotX, plotY
var sword CurrentX, CurrentY, Xinc, Yinc
var sword Dx, Dy, TwoDx, TwoDy
var sword TwoDxAccumulatedError, TwoDyAccumulatedError
    Dx = X1
    TwoDx = X2
    Dx = (TwoDx-Dx)
    Dy = Y1
    TwoDy = Y2
    Dy = (TwoDy-Dy)

    TwoDx = Dx + Dx
    TwoDy = Dy + Dy

    CurrentX = X1
    CurrentY = Y1

    Xinc = 1
    Yinc = 1

    if(Dx < 0)Then
          Xinc = -1
          Dx = -Dx
          TwoDx = -TwoDx
    end if

    if (Dy < 0) Then
       Yinc = -1
       Dy = -Dy
       TwoDy = -TwoDy
    end if

    lcd_write_pixel(X1,Y1, color)

    if ((Dx +Dy) >  0)Then
       if (Dy <= Dx) Then
          TwoDxAccumulatedError = 0
          Repeat
              CurrentX = CurrentX +Xinc
              TwoDxAccumulatedError = TwoDxAccumulatedError + TwoDy
              if(TwoDxAccumulatedError > Dx)Then
                  CurrentY = CurrentY +Yinc
                  TwoDxAccumulatedError = TwoDxAccumulatedError -
TwoDx
              end if
               plotX = CurrentX
               plotY = CurrentY
               lcd_write_pixel(plotX,plotY, color)
          Until (CurrentX == X2)
       else
           TwoDyAccumulatedError = 0
           Repeat
               CurrentY = CurrentY + Yinc
               TwoDyAccumulatedError = TwoDyAccumulatedError + TwoDx
               if(TwoDyAccumulatedError>Dy)Then
                   CurrentX = CurrentX + Xinc
                   TwoDyAccumulatedError = TwoDyAccumulatedError -
TwoDy
               End if
               plotX = CurrentX
               plotY = CurrentY
               lcd_write_pixel(plotX,plotY, color)
           Until (CurrentY == Y2)
       end if
    end if
End procedure




-- ----------------------------------------------------------
-- ----------------------------------------------------------
-- ----------------------------------------------------------
-- Pseudo variable 'lcd' as alternative for lcd_writechar(<byte>)
-- ----------------------------------------------------------
procedure lcd'put(byte in char) is
    lcd_write_char(glcd_char_x_pos, glcd_char_y_pos, char)
    glcd_char_x_pos = glcd_char_x_pos + 5
end procedure

-- ----------------------------------------------------------
-- Send byte to the LCD and automatically set the cursor one
-- position right.
-- There is no check on line overflow.
-- ----------------------------------------------------------
procedure  lcd_writechar(byte in c) is
   lcd = c
end procedure

-- ------------------------------------------------------------
-- Clear screen
-- (and set the cursor to the upper left corner: row 0, column 0)
-- ------------------------------------------------------------
procedure  lcd_clearscreen() is
   lcd_fill(0)           -- Clear the display
   glcd_char_x_pos = 0;
   glcd_char_y_pos = 0;
end procedure

-- ------------------------------------------------------------
-- Set cursor position
-- Specify row and column in base-0 notation (first line is 0).
-- ------------------------------------------------------------
procedure  lcd_setcursor(byte in row, byte in col)  is
   glcd_char_y_pos = row * 7 -- up to 9 rows
   glcd_char_x_pos = col * 5 -- up to 25 cols
end procedure

--
----------------------------------------------------------------------------
-- cursor returns home(line 1, position 1)
--
----------------------------------------------------------------------------
procedure lcd_home()  is
   glcd_char_y_pos = 0
   glcd_char_x_pos = 0
end procedure
--
----------------------------------------------------------------------------
--
----------------------------------------------------------------------------
-- END glcd_common.jal
--
----------------------------------------------------------------------------

Main test Program
---

-- Title: Test JE-AN1286404 panel
-- Author: Michael Watterson Copyright (c) 2010, all rights reserved.
-- Adapted-by:
-- Compiler: >=2.4k
--

include 16f877a
-- Also could be 16F887 for re-ordering as 877 is obsolete

pragma target clock 20_000_000                  -- xtal frequency
pragma target OSC        hs
pragma target LVP        disabled
pragma target WDT        disabled
pragma target PWRTE      enabled
pragma target BROWNOUT   disabled
pragma target CP         disabled
pragma target WRT        NO_PROTECTION


enable_digital_io()

-- ---------------------------- LIBRARIES
-----------------------------
--Serial before all so we can debug
-- ok, now setup serial;@jallib section serial
const serial_hw_baudrate = 9_600
include serial_hardware


--GLCD ports
var   bit   GLCD_CS2      is pin_b0
alias  GLCD_CS2_direction is pin_b0_direction
var   bit   GLCD_CS1      is pin_b1
alias  GLCD_CS1_direction is pin_b1_direction
var   bit   GLCD_RW       is pin_b4
alias  GLCD_RW_direction is pin_b4_direction
var   bit   GLCD_DI       is pin_b5 -- LCD command/data select.
alias  GLCD_DI_direction is pin_b5_direction
var   bit   GLCD_E        is pin_E2
alias  GLCD_E_direction is pin_E2_direction
var   bit   GLCD_LED        is pin_E1
alias  GLCD_LED_direction is pin_E1_direction

var   byte  GLCD_dataprt is portd    -- LCD data
alias GLCD_DATAPRT_DIR is portd_direction

include lcd_ks0108


-- 5x5 Keypad setup
-- keyboard map (4x4, col4 is left out with 4x3 keyboard)
-- --
--      1 2 3 A   ^  row1
--      4 5 6 B   <  row2
--      7 8 9 C   v  row3
--      * 0 # D   >  row4
--      R G Y B   E
-- --
--  col 1 2 3 4   5

--keypad layout (4 X 3 HAS 7 PINS)
;pin numbers of keypad shown in <> brackets
;
;               <3>     <1>     <5>
;       <2>     1       2       3
;       <7>     4       5       6
;       <6>     7       8       9
;       <4>     *       0       #

; *top view* of keypad pins: 1,2,3,4,5,6,7
; pin 1 is near * key. Buttons A to D not fitted
--
-- 4x4 pad
;pin numbers of keypad shown in <> brackets
;
;          <1> <2> <3> <4>
;       <5>     1       2       3       A
;       <6>     4       5       6       B
;       <7>     7       8       9       C
;       <8>     *       0       #       D

; *top view* of keypad pins: 1,2,3,4,5,6,7,8
; pin 1 is near * key.
--
var volatile byte keyscan  is portb      -- rows of keyboard
var volatile byte keyscan_direction is portb_direction
var volatile byte keydrive is portd      -- columns of keyboard
var volatile byte keydrive_direction is portd_direction
include keypad5


include glcd_common
-- graphics routines

var bit led      is pin_A5
pin_A5_direction =  output
var bit led_direction   is pin_A5_direction
var bit led2            is pin_a4
var bit led2_direction  is pin_a4_direction

include delay

-- set all IO as digital
enable_digital_io()


serial_hw_init()

--
include print
var byte dummy

-- 0,0--------------------------------------------------------127,0
-- ----------------------------------------------------------------
-- ----------------------------------------------------------------
-- ----------------------------------------------------------------
-- ----------------------------------------------------------------
-- ----------------------------------------------------------------
-- ----------------------------------------------------------------
-- 0,63------------------------------------------------------127,63



const byte str1[] = "Hello"
const byte str2[] = "Batcave!"
var byte press
--
lcd_init()
lcd_box(0,0,127,63)
--lcd_fill(255)
lcd_line(2,2,31,15,1)
lcd_line(96,15,125,2,1)
lcd_line(2,61,31,48,1)
lcd_line(96,48,125,61,1)
lcd_setcursor(2,10)
print_string(lcd,str1)
lcd_setcursor(4,9)
print_string(lcd, str2)
lcd_circle(64,31, 30, 0)
--lcd_circle(64,25, 24, 0)
--
led_direction = output
for 4 loop
   LED = on
   delay_100ms(3)
   LED = off
   delay_100ms(3)
end loop
--const byte str1[] = "Hello serial world"   -- define a string
print_string(serial_hw_data, str1)  -- output string to serial

-- inform user PIC is ready !
serial_hw_write("!")

-- let's build our loop
var byte char   -- will store received char
var byte lastPress = 0
var word counter = 10
lcd_setcursor(6,1)
forever loop
        if serial_hw_read(char) then
                serial_hw_write(char)   -- that's the echo...
        end if

        counter = counter - 1;
        if (counter == 0) then
           counter = 50
      --serial_hw_data = "."
      LED = ! LED
   end if

   led2 = on
   _usec_delay(10000)
--   lcd_line(2,51,42,51,0)
--   lcd_line(2,40,42,62,1)
--   _usec_delay(100000)
--   lcd_line(2,40,42,62,0)
--   lcd_line(22,40,22,62,0)
--   _usec_delay(100000)
--   lcd_line(2,61,31,48,0)
--   _usec_delay(100000)
--   lcd_line(96,48,125,61,0)
--   lcd_circle(64,31, 30, 0)
--   lcd_circle(64,25, 24, 1)
   led2 = off
--   _usec_delay(100000)
--   lcd_line(96,48,125,61,1)
--   _usec_delay(100000)
--   lcd_line(2,61,31,48,1)
--   _usec_delay(100000)
--   lcd_line(2,2,31,15,1)
--   _usec_delay(100000)
--   lcd_line(96,15,125,2,1)
--   lcd_circle(64,25, 24, 0)
--   lcd_circle(64,31, 30, 1)
--
-- Now only process a keypress once.
-- since this is in a loop with a short delay and we wait till we see
getkey get
-- the same press twice, this is debounced and does not repeat.
   press = getkey_once()
   if (press == 254) Then
         if (lastKey == 0) then lastpress = 0 end if
         if lastkey != lastpress  then
            lcd = lastkey
            serial_hw_write(lastkey)
            lastpress = lastkey
         end if
   end if

end loop

-- 
You received this message because you are subscribed to the Google Groups 
"jallib" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/jallib?hl=en.

Reply via email to