#include "rulbus.h"
#include <stdio.h>
#include <conio.h>
#include <windows.h>
enum Pin { pin0, pin1, pin2, pin3, pin4, pin5, pin6, pin7, };
const char* portName = "pport";
const Pin pinLED = pin0;
const Pin pinSwitch = pin3;
const char* portLED = "PB";
const char* portSwitch = "PB";
const char* lineLED = "PB0";
const char* lineSwitch = "PB3";
const int rack = 0;
const int address = 0x94;
inline int mask( int pin ) { return 1 << pin; }
inline int tstbit( int32 byte, int pin ) { return ( byte & mask(pin) ) != 0; }
inline int clrbit( int32& byte, int pin ) { return byte &= ~mask(pin); }
inline int setbit( int32& byte, int pin ) { return byte |= mask(pin); }
inline int xorbit( int32& byte, int pin ) { return byte ^= mask(pin); }
static void setPortDirection( int handle );
static void setLineDirection( int handle );
static void switchLED_mask ( int handle );
static void switchLED_line ( int handle );
int main()
{
printf( "Switch a LED on and off, a parallel interface demonstration." );
int32 handle;
rb8506_pport_open( &handle, portName );
printf( "\nsetting port direction..." );
setPortDirection( handle );
printf( "\nusing port data with bitmasks; press a key to contine..." );
switchLED_mask( handle );
printf( "\nsetting line direction..." );
setLineDirection( handle );
printf( "\nusing single line functions; press a key to contine..." );
switchLED_line( handle );
printf( "\n" );
return rb8506_pport_close( handle );
}
static void setPortDirection(int handle)
{
int32 dataSwitch, dataLED;
rb8506_pport_getPortDir( handle, portSwitch, &dataSwitch );
rb8506_pport_getPortDir( handle, portLED , &dataLED );
rb8506_pport_setPortDir( handle, portSwitch, dataSwitch & ~mask(pinSwitch) );
rb8506_pport_setPortDir( handle, portLED , dataLED | mask(pinLED ) );
}
static void switchLED_mask(int handle)
{
while ( !kbhit() )
{
int32 dataSwitch, dataLED;
rb8506_pport_getPortData( handle, portSwitch, &dataSwitch );
rb8506_pport_getPortData( handle, portLED , &dataLED );
if ( tstbit( dataSwitch, pinSwitch ) ) setbit( dataLED, pinLED );
else clrbit( dataLED, pinLED );
rb8506_pport_setPortData( handle, portLED, dataLED );
Sleep( 10 );
}
(void) getch();
}
static void setLineDirection(int handle)
{
rb8506_pport_setLineDir( handle, lineSwitch, 'i' );
rb8506_pport_setLineDir( handle, lineLED , 'o' );
}
static void switchLED_line(int handle)
{
while ( !kbhit() )
{
int32 switchOpen;
rb8506_pport_getLineLevel( handle, lineSwitch, &switchOpen );
rb8506_pport_setLineLevel( handle, lineLED , switchOpen );
Sleep( 10 );
}
(void) getch();
}