/************************************************************************
* *
* lptout - vkluchaet ili vyklychaet ukazannyy kanal v *
* portu LPT. Programma sozdana dlya raboty s *
* ustroystvom NK150 ot Master Kit. *
* *
************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/io.h>
/***********************************************************************/
#define MYNAME "lptout"
#define LPT_PORT 0x378
#define CHANNEL_1 0x1
#define CHANNEL_2 0x2
#define CHANNEL_3 0x4
#define CHANNEL_4 0x8
#define CHANNEL_5 0x10
#define CHANNEL_6 0x20
#define CHANNEL_7 0x40
#define CHANNEL_8 0x80
#define ALL_CHANNELS_ON 0xff
#define ALL_CHANNELS_OFF 0x0
/***********************************************************************/
void init_port(unsigned long port);
void close_port(unsigned long port);
void send_signal(unsigned char signal, unsigned long port);
unsigned char channel_to_signal(int ch);
unsigned char load_signal(unsigned long port);
/***********************************************************************/
/***********************************************************************/
int main(int argc, char **argv)
{
////////////////////////
int ch; // Nomer kanala
unsigned char signal; // Signal
////////////////////////
if ( argc != 2 )
{
fprintf(stderr, "usage: %s \"channel\"\n", MYNAME );
exit(1);
}
if ( !isdigit( (int) argv[1][0]) )
{
fprintf(stderr, "usage: %s \"channel\"\n", MYNAME );
exit(1);
}
ch = atoi(argv[1]); // Poluchaem nomer kanala
if ( ch < 0 || ch > 9 )
{
fprintf(stderr, "%s 0 <= channel <= 9\n", MYNAME );
exit(1);
}
signal = channel_to_signal(ch); // Delaem signal iz kanala
init_port(LPT_PORT); // Otkryvaem port
if ( (signal != ALL_CHANNELS_OFF) && (signal != ALL_CHANNELS_ON) )
signal ^= load_signal(LPT_PORT); // Dobavlyaem k signalu starye znacheniya
send_signal(signal, LPT_PORT); // Otpravlyaem signal
close_port(LPT_PORT); // Zakryvaem port
return 0;
}
/***********************************************************************/
void init_port(unsigned long port)
{
if ( ioperm(port, 1, 1) != 0 )
{
fprintf(stderr, "%s: cannot get port 0x%x: %s\n",
MYNAME, port, strerror(errno) );
exit(1);
}
}
/***********************************************************************/
void close_port(unsigned long port)
{
if ( ioperm(port, 1, 0) != 0 )
{
fprintf(stderr, "%s: cannot close port 0x%x: %s\n",
MYNAME, port, strerror(errno) );
exit(1);
}
}
/***********************************************************************/
void send_signal(unsigned char signal, unsigned long port)
{
outb(signal, port);
}
/***********************************************************************/
unsigned char channel_to_signal(int ch)
{
switch (ch)
{
case 1 : return CHANNEL_1;
case 2 : return CHANNEL_2;
case 3 : return CHANNEL_3;
case 4 : return CHANNEL_4;
case 5 : return CHANNEL_5;
case 6 : return CHANNEL_6;
case 7 : return CHANNEL_7;
case 8 : return CHANNEL_8;
case 9 : return ALL_CHANNELS_ON;
case 0 : return ALL_CHANNELS_OFF;
}
return ALL_CHANNELS_OFF;
}
/***********************************************************************/
unsigned char load_signal(unsigned long port)
{
return inb(port);
}
/***********************************************************************/
/***********************************************************************/
/***********************************************************************/