ATmega644(Sanguino) compatibility on Arduino 0012

The library source code is too big to post here in full, and I am still working on some changes that will simplify interfacing to panels that require different delays and also make the pins easier to select in the header file.

But for those of you in a rush, here is a modified top half of the ks0108.h that works with the sanguino. Replace the top half of the code in the ks0108.h file with the code below. All code from the following line is unchanged from the version downloaded from the playground: // macros for pasting port defines

The default settings here have the LCD data pins connected to Sanguino pins 0-7. Control pins are 20 to 23 (see the header file for specifics), but you can change these if necessary
The file can also be used with the Arduino by commenting out #define ATMEGA644

Not that the sanguino allows all data pins on the same port which makes the updates a little quicker, but note that you may need to add some additional delay in the cpp file if your panel can't cope with the extra speed.

I recommend waiting a few weeks for a finished and tested version that will be easier to use. But if you are in a rush to test some alpha code, have fun!

//  ks0108.h - Arduino library support for ks0108 and compatable graphic LCDs
// alpha version for sanguino testing

#include <inttypes.h>
//#include <wiring.h> // for boolean
typedef uint8_t boolean;
typedef uint8_t byte;
#include <avr/pgmspace.h>

#ifndef      KS0108_H
#define KS0108_H

/*********************************************************/
/*  Configuration for assigning LCD bits to Arduino Pins */
/*********************************************************/

// first we have the Sanguino defines
/*******************************************************************************************/
/* Sanguino/ ATmega644 defines                                                             */
/*******************************************************************************************/
#define ATMEGA644
#ifdef ATMEGA644

// Command pins assignments:
#define CSEL1                        20             // CS1 Bit   // swap pin assignments with CSEL2 if left/right image is reversed
#define CSEL2                        19             // CS2 Bit
#define R_W                              21             // R/W Bit
#define D_I                              22             // D/I Bit 
#define EN                              23             // EN Bit

// data pin assignments- on ATmega644 all data pins are assigned to the same port
#define dataPins0to7    // bits 0-7 assigned to sanguino pins 0-7
//#define dataPins8to15   // bits 0-7 assigned to sanguino pins 8-15 // note this conflicts with serial UART
//#define dataPins16to23  // bits 0-7 assigned to sanguino pins 16-23 
//#define dataPins24to31  // bits 0-7 assigned to sanguino pins 24-31

// these macros  map pins to ports using the defines above
// the following should not be changed unless you really know what your doing 
#ifdef dataPins0to7
#define LCD_DATA_LOW_NBL    B   // port B=pins 0-7 on ATmega466  
#define LCD_DATA_HIGH_NBL   B   // on ATmega644, high and low nibbles are on the same port
#endif
#ifdef dataPins8to15 
#define LCD_DATA_LOW_NBL    D   // port D=pins 8-15 (note serial UART uses 8 and 9) 
#define LCD_DATA_HIGH_NBL   D   
#endif
#ifdef dataPins16to23          
#define LCD_DATA_LOW_NBL    C   // port C=pins 16-23
#define LCD_DATA_HIGH_NBL   C 
#endif
#ifdef dataPins24to31          
#define LCD_DATA_LOW_NBL    A   // port A=pins 24-31 (note these are the analog ports)
#define LCD_DATA_HIGH_NBL   A
#endif


// ATmega644 macros to fast write data to pins, this version only works for pins 0-23
#define fastWriteHigh(_pin_) ( _pin_ < 8 ?  PORTB |= 1 << (_pin_ & 0x07) : ( _pin_ < 16 ?  PORTD |= 1 << ((_pin_ -8) & 0x07) : PORTC |= 1 << ((_pin_ -16) & 0x07)  ) ) 
#define fastWriteLow(_pin_) ( _pin_ < 8 ?   PORTB &= ~(1 << (_pin_  & 0x07)) : ( _pin_ < 16 ?  PORTD &= ~(1 << ((_pin_ -8) & 0x07) )  :  PORTC &= ~(1 << ((_pin_ -16) & 0x07) )  ) )

/*******************************************************************************************/
/* Arduino ATmega168 specific defines                                                             */
/*******************************************************************************************/
#else  
#define CSEL1                        15            // CS1 Bit   // swap pin assignments with CSEL2 if left/right image is reversed
#define CSEL2                        14            // CS2 Bit
#define R_W                              16            // R/W Bit
#define D_I                              17            // D/I Bit 
#define EN                              18            // EN Bit

/* Arduino pins used for LCD Data 
 * un-comment ONE of the following pin options that corresponds to the wiring of data bits 0-3 
 */
#define dataPins8to11   // bits 0-3 assigned to arduino pins 8-11, bits 4-7 assigned to arduino pins 4-7
//#define dataPins14to17 //bits 0-3 assigned to arduino pins 14-17, bits 4-7 assigned to arduino pins 4-7. (note command pins must be changed)
//#define dataPins0to3  // bits 0-3 assigned to arduino pins 0-3 , bits 4-7 assigned to arduino pins 4-7, this is marginally  the fastest option but  its only available on runtime board without hardware rs232.

// ATmega168 macros to fast write data to pins known at compile time, this is over 30 times faster than digitalWrite
#define fastWriteHigh(_pin_) ( _pin_ < 8 ?  PORTD |= 1 << (_pin_ & 0x07) : ( _pin_ < 14 ?  PORTB |= 1 << ((_pin_ -8) & 0x07) : PORTC |= 1 << ((_pin_ -14) & 0x07)  ) ) 
#define fastWriteLow(_pin_) ( _pin_ < 8 ?   PORTD &= ~(1 << (_pin_  & 0x07)) : ( _pin_ < 14 ?  PORTB &= ~(1 << ((_pin_ -8) & 0x07) )  :  PORTC &= ~(1 << ((_pin_ -14) & 0x07) )  ) )

// these macros  map pins to ports using the defines above
// the following should not be changed unless you really know what your doing 
#ifdef dataPins0to3
#define LCD_DATA_LOW_NBL   D   // port for low nibble: D=pins 0-3  
#endif
#ifdef dataPins14to17 
#define LCD_DATA_LOW_NBL   C   // port for low nibble: C=pins 14-17 (using this requires reasignment of command pins) 
#endif
#ifdef dataPins8to11            // the following is the defualt setting
#define LCD_DATA_LOW_NBL   B   // port for low nibble, B=pins 8-11
#endif

#define LCD_DATA_HIGH_NBL  D   // port for high nibble: D=pins 4-7, B & C not available on std arduino  
/* NOTE: all above options assume LCD data bits 4-7 are connected to arduino pins 4-7 */

#endif // end of Arduino/ATmega168 specific defines


/*******************************************************/
/*     end of pin configuration                        */
/*******************************************************/

/* option: uncomment the next line if all command pins are on the same port for slight speed & code size improvement */
//#define LCD_CMD_PORT            PORTC            // Command Output Register 

//#define HD44102   // uncomment this to build a 44102 version

/* 
#ifdef dataPins8to11 || dataPins14to17  // these arduino options split data across two ports 
#define LCD_DATA_NIBBLES                // if this is defined then data i/o is split into two operations
#endif 
the above is now done using the test below
*/
#if LCD_DATA_HIGH_NBL !=  LCD_DATA_LOW_NBL
#define LCD_DATA_NIBBLES               // if this is defined then data i/o is split into two operations
#endif 


// macros for pasting port defines

note that exisiting code from the header file needs to be appended starting with the macros for pasting port defines