Code to write to I2C YwRobot display from PI command

Compile with  gcc -o i2c_lcd i2c_lcd.c -l wiringPi

Usage:
i2c_lcd -i  init display
i2c_lcd -c  clear display
i2c_lcd -1:2:3:4 “text”  print text to line 1:2:3:4

#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>   /* Standard input/output definitions */
#include <string.h>  /* String function definitions */
#include <unistd.h>  /* UNIX standard function definitions */
#include <fcntl.h>   /* File control definitions */
#include <errno.h>   /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */
#include <time.h>

char * lockfile="i2c_lcd.lock";
int i2c_address=0x27;
int fd;
bool backlight=true;
int last_data;

void init_lcd(void);
void I2CWriteDirect(int);
void I2CWriteDirectBl(int);
void lcd_strobe(void);
void lcd_write(int);
void lcd_puts(char *, int );
void test_write(void);
void lcd_write_char(int);
void lcd_clear(void);
void lcd_cursoroff(void);
void checklock(void);
void clearlock(void);
void AllLines(char * );

void main(int argc, char *argv[]){
wiringPiSetup();
if (false==(fd=wiringPiI2CSetup(i2c_address))){
fprintf(stderr,"Can't open i2c on port %i\n",i2c_address);

exit(1);
}

if (argc==1){
printf("Usage:\n");
printf("i2c_lcd -i  init display\n");
printf("i2c_lcd -c  clear display\n");
printf("i2c_lcd -1:2:3:4 \"text\"  print text to line 1:2:3:4\n");
printf("\n");
}

int i;
for (i = 1; i < argc; i++)  /* Skip argv[0] (program name). */
{
if (strcmp(argv[i], "-i") == 0)  /* Process optional arguments. */
{
init_lcd();
lcd_cursoroff();
}
if (strcmp(argv[i], "-1") == 0)  /* Process optional arguments. */
{
checklock();
lcd_puts(argv[i+1],1);
}
if (strcmp(argv[i], "-2") == 0)  /* Process optional arguments. */
{
checklock();
lcd_puts(argv[i+1],2);
}
if (strcmp(argv[i], "-3") == 0)  /* Process optional arguments. */
{
checklock();
lcd_puts(argv[i+1],3);
}
if (strcmp(argv[i], "-4") == 0)  /* Process optional arguments. */
{
checklock();
lcd_puts(argv[i+1],4);
}
if (strcmp(argv[i], "-c") == 0)  /* Process optional arguments. */
{
checklock();
lcd_clear();
}
if (strcmp(argv[i], "-f") == 0)  /* Process optional arguments. */
{
checklock();
AllLines(argv[i+1]);
}

}

clearlock();

//   init_lcd();
//   lcd_clear();
//   test_write();
//    lcd_puts("test string",3);
//    lcd_puts("Last line",4);
}

void checklock(void){
if( access( lockfile, F_OK ) != -1 ) {
printf("Lock Exists\n");
while(access( lockfile, F_OK ) != -1 ){usleep(10000);}
}
FILE *fp = fopen(lockfile, "ab+");
}

void clearlock(void){
if( access( lockfile, F_OK ) != -1 ) {
unlink(lockfile);
}
}

void I2CWriteDirectD(int data){
data=data & 0xFF;
I2CWriteDirect(data);
last_data=data;
}

void I2CWriteDirect(int data){
data=data & 0xFF;
if (backlight){
wiringPiI2CWrite(fd,data | 0x08);
}else{
wiringPiI2CWrite(fd,data);
}
}

void lcd_strobe(void){
usleep(400);
I2CWriteDirect((last_data | 0x04));
usleep(400);
I2CWriteDirect((last_data ));
//   usleep(400);
}

void lcd_write(int data){
I2CWriteDirectD((data >> 4)<<4);
lcd_strobe();
I2CWriteDirectD((data & 0x0F)<<4);
lcd_strobe();
}

void lcd_write_char(int data){
I2CWriteDirectD(((data >> 4)<<4) | 0x01);
lcd_strobe();
I2CWriteDirectD(((data & 0x0F)<<4) | 0x01);
lcd_strobe();
}

void test_write(){
lcd_write(0xC0);
lcd_write_char('T');
lcd_write_char('E');
lcd_write_char('S');
lcd_write_char('T');
}

void init_lcd(void){
/*   Port definitions
addr, bl,strobe,rw,rs,   d4,d5,d6,d7
0x27, 3, 2     , 1, 0,   4, 5, 6, 7
*/
//set 4 bit mode
I2CWriteDirectD(0x30); //write
lcd_strobe();
usleep(5000);
lcd_strobe();
usleep(5000);
lcd_strobe();
usleep(5000);
I2CWriteDirectD(0x20);
lcd_strobe();
usleep(5000);

lcd_write(0x28);
lcd_write(0x08);
lcd_write(0x01);
lcd_write(0x06);
lcd_write(0x0C);
lcd_write(0x0F);

}

// put string function
void lcd_puts(char * stringin, int line){
if (line == 1){lcd_write(0x80); }
if (line == 2){lcd_write(0xC0);}
if (line == 3){lcd_write(0x94);}
if (line == 4){lcd_write(0xD4);}

char buffer[22];
int a=0;
for(a=0;a<20;a++){buffer[a]=' ';}

a=0;
bool ok=true;
while(ok){
if(stringin[a]!=0){
buffer[a]=stringin[a];
a++;
if (a>19){ok=false;}
}else{
ok=false;
buffer[a]=0;
}
buffer[a]=0;
}

//  printf("Buffer %i:%s\n",line,buffer);

//  for (a=0;a<20;a++){
//    lcd_write_char(buffer[a]);
//  }
a=0;
while((a<20) && (buffer[a])){
lcd_write_char(buffer[a++]);
}
}

// clear lcd and set to home
void lcd_clear(void){
lcd_write(0x1);
lcd_write(0x2);
}

void lcd_cursoroff(void){
lcd_write(0x0c); //cursor and blink off
}

void AllLines(char * filename){

FILE * fp;
char * line = NULL;
size_t len = 0;
ssize_t read;

fp = fopen(filename, "r");
if (fp == NULL) {
fprintf(stderr,"Cant opem %s\n",filename);
exit(EXIT_FAILURE);
}

int l=0;
while (((read = getline(&line, &len, fp)) != -1) && l<4) {

int a=0;
for(a=0;a<len;a++){
if (line[a]!=0){
if (line[a]<' '){line[a]=0;}
}
}

l++;
lcd_puts(line,l);
}

}