Skip to content

Instantly share code, notes, and snippets.

@Jiwoks
Created June 25, 2013 16:38
Show Gist options
  • Select an option

  • Save Jiwoks/5860024 to your computer and use it in GitHub Desktop.

Select an option

Save Jiwoks/5860024 to your computer and use it in GitHub Desktop.
/* Test du port parallele et afficheur LCD
* Broche LCD Enable sur Broche Init du port //
* Broche LCD RegisterSelect sur Broche Strobe sur port //
* Compiler avec gcc lcd.c -o lcd -lm
* Pour sortir proprement Ctrl+D puis Enter
*/
#include <sys/ioctl.h>
#include <string.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/errno.h>
#include <linux/ppdev.h>
#include <linux/parport.h>
#include <math.h>
void clean_stdin(void)
{
int c;
do {
c = getchar();
} while (c != '\n' && c != EOF);
}
void SendCommand(int fd, int value, int command){
struct ppdev_frob_struct valout;
valout.mask=command;
if(value==0){
valout.val=0;
}else{
valout.val=command;
}
if (ioctl (fd, PPFCONTROL, &valout) < 0) {
exit(EXIT_FAILURE);
}
}
void SendData(int fd, int data){
if (ioctl (fd, PPWDATA, &data) < 0) {
fprintf(stderr,"Send Error : %s (%d)\n",
strerror(errno),errno);
exit(EXIT_FAILURE);
}
}
void ToggleEnable(fd){
SendCommand(fd,1,PARPORT_CONTROL_INIT);
usleep(40);
SendCommand(fd,0,PARPORT_CONTROL_INIT);
}
void SendLCDData(int fd, int data){
SendCommand(fd,0,PARPORT_CONTROL_STROBE);
SendData(fd,data);
ToggleEnable(fd);
}
void SendLCDCommand(int fd, int command){
SendCommand(fd,1,PARPORT_CONTROL_STROBE);
SendData(fd,command);
ToggleEnable(fd);
}
int main(){
int fd;
char c;
int lc=0;
char * cPort = malloc(0);
printf("Veuillez saisir le périphérique associé au port parallèle: ");
while ((c = getchar()) != '\n') {
cPort=realloc(cPort,lc+1);
cPort[lc]=c;
lc++;
}
cPort=realloc(cPort,lc+1);
cPort[lc] = '\0';
if (lc==0){
cPort=realloc(cPort,25);
sprintf(cPort,"/dev/.static/dev/parport0");
}
//Ouverture du port
if ((fd = open(cPort, O_RDWR)) < 0) {
fprintf(stderr,"Open %s Error : %s (%d)\n",cPort,
strerror(errno),errno);
exit(EXIT_FAILURE);
}
//On prend la main sur le port
if (ioctl(fd, PPCLAIM) < 0) {
fprintf(stderr,"PPCLAIM ioctl Error : %s (%d)\n",
strerror(errno),errno);
exit(EXIT_FAILURE);
}
SendLCDCommand(fd,0x38); //Initialisation du LCD
SendLCDCommand(fd,0x0F); //Initialisation du LCD
SendLCDCommand(fd,0x01);
SendLCDCommand(fd,0x02);
printf("En attende de caractères:\n");
//Attente d'un texte
while ((c = getchar())!=EOF) {
SendLCDData(fd,(int) c);
}
//On rend la main sur le port
if (ioctl(fd, PPRELEASE) < 0) {
fprintf(stderr,"PPRELEASE ioctl Error : %s (%d)\n",
strerror(errno),errno);
exit(EXIT_FAILURE);
}
//On referme le port
if(close(fd) < 0) {
fprintf(stderr,"Close Error : %s (%d)\n",
strerror(errno),errno);
exit(EXIT_FAILURE);
}
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment