KEYPAD WITH I2C; How to write a librarY?

I wrote a function to read my folie keypad with a I2C; it is called “Read_I2C_Key()”.

You can push two keys together for special purposes (but not in the same line or row).
The function returns an integer, containing 5 bits for the first key (1…16) and the second key.

Because I use the function within several programs, it should be handy to have the function within a library.

Can anybody help me?

/*
Entry float with 4x4 keypad (8 terminals) and PCF8574
Author: C.W.A. Baltus
Date: 5-3-2016
Edited: 5-3-2016
Based on book Franzis, C-Control-Pro, pages 121 and following, Conrad "Tastatur modul (2C), including CC-software

See Instruction_I2C-Keyad.docx
Two keys can be pressed together, but not in the same row or the same column
It can return a integer (bits 1 upto 5 for Key1, bits 6 upto 9 for Key2

Hardware:
- Aruidno Mega2560, 
- 4*4 folie keypad (8 terminals) + PCF8574, 
- I2C LCD 20*4 + PCF8574

Software: Adrduino 1.6.1
*/

//=====================================================================================================
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address

const int I2C_keypad_W=0x22;                     //PCF8574, A1=A2=A3=0,    writing address Ox20 of 32 dec
const char strKeylist[18]="0123C456V789A.0 E";   //Definition of symbols on keypad
String strValidseries = "0123456789.";
byte mask = B11111;

int ReturnKey;         // Holds key1 and key2
int intKey1;           //Decimal value of key1 (1..16, 5 LSB of ReturnKey)
int intKey2;           //Decimal value of key 2 (1..16, bits 6,7,8,en 9 van ReturnKey)
String strInstring;    //Can hold the input
int intN =0;           //index of entry

//=====================================================================================================
void setup()           //void en setup met kleine letter
{
   Wire.begin();       // Join I2C bus
   Serial.begin(9600); // Open serial communcation
   lcd.begin(20,4);    // initialize the lcd 
   }

//=====================================================================================================
void loop(){
  ReturnKey= Read_I2C_Key();
  intKey1 = ReturnKey & mask;   
  intKey2 = ReturnKey>>5;      
  
  if (ReturnKey!=0) 
    {Serial.print(intKey1); Serial.print("\t"); Serial.print(intKey2);Serial.print("\t"); Serial.print(strKeylist[intKey1]); Serial.print("\t"); Serial.println(strValidseries.indexOf(strKeylist[intKey1]));
     
    if (strValidseries.indexOf(strKeylist[intKey1])>=0)   //Only decimals and dot,  without range delivers -1
        {strInstring = strInstring + strKeylist[intKey1];
        lcd.clear();
        lcd.print(strInstring);
        intN = intN + 1;
        }
     if (intKey1==4)  //CE = clear entry
        {lcd.clear();
        //lcd.setCursor(0,1);
        intN=intN-1;
        strInstring = strInstring.substring(0,intN);
        lcd.print(strInstring);
        }
    if (intKey1==16)
        {Serial.println (strInstring);
        lcd.setCursor(0,1);
        lcd.print (StrToFloat(strInstring));
        }
    delay(300);  
    }  //End if
  
  }   //end of loop

//----------------------------------------------------------
int Read_I2C_Key(){

long starttime, finishtime, elapsedtime;

starttime=millis();

//Definition of local parameters
byte bytKey, bytLine[9][3], bytKeyreturn[3];       //changed 20150502: numbers form 0..8
int intN,intM=1;

bytKeyreturn[1]=0; bytKeyreturn[2]=0;  

//Put sequential a "0" on the lines 8, 7, 6 and 5  
bytLine[8][1]=0x70;  bytLine[8][2]=4;   //0111 0000=7 , Line8=Column4=zero
bytLine[7][1]=0xB0;  bytLine[7][2]=3;   //1011 0000=11, Line7=Column3=zero
bytLine[6][1]=0xD0;  bytLine[6][2]=2;   //1101 0000=13, line6=Column2=zero
bytLine[5][1]=0xE0;  bytLine[5][2]=1;   //1110 0000=14, Line5=Column1=zero 

for(intN=5;intN<9;intN++) {     
    Wire.beginTransmission(I2C_keypad_W);// transmit to device PCF8574
    Wire.write(bytLine[intN][1] + 0x0F);
    Wire.endTransmission();    // stop transmitting
    
    //Read the row
    Wire.beginTransmission(I2C_keypad_W);// transmit to device PCF8574
    Wire.requestFrom(I2C_keypad_W,1);    // request from I2C_keypad only 1 byte
    bytKey=Wire.read(); 
    Wire.endTransmission();    // stop transmitting
 
    if(bytKey==(bytLine[intN][1]+0x0E)) {bytKeyreturn[intM]= (0 + bytLine[intN][2]); intM=intM+1;}  //0x0E=1110 key pressed in line 1
    if(bytKey==(bytLine[intN][1]+0x0D)) {bytKeyreturn[intM]= (4 + bytLine[intN][2]); intM=intM+1;}  //0x0D=1101 key pressed in line 2
    if(bytKey==(bytLine[intN][1]+0x0B)) {bytKeyreturn[intM]= (8 + bytLine[intN][2]); intM=intM+1;}  //0x0B=1011 key pressed in line 3
    if(bytKey==(bytLine[intN][1]+0x07)) {bytKeyreturn[intM]= (12+ bytLine[intN][2]); intM=intM+1;}  //0x07=0111 key pressed in line 4
    
    finishtime=millis();   //This routine takes only 2 ms
    elapsedtime = finishtime-starttime;      
    } //end of loop
    
return (bytKeyreturn[2]*32 + bytKeyreturn[1]);
//return bytKeyreturn[1];

}  //end read key

//====================================================================
float StrToFloat(String str)
  {  char carray[str.length() + 1]; //determine size of the array
  str.toCharArray(carray, sizeof(carray)); //put str into an array
  return atof(carray);
  }   //End StrToFloat

http://www.arduino.cc/en/Hacking/LibraryTutorial

Thank Aarg,

It will try it myself.