rendered paste body#include<stdio.h>#include<stdlib.h>#include<fcntl.h>#include<unistd.h>#include<errno.h>#include<getopt.h>#include<ctype.h>#include<comedi.h>#include<comedilib.h>void banner();void banner2();void banner3();int main(int argc, char *argv[]){/*=====================================================================================================================*/ /*Variable declarations*/ int stype; unsigned int switches; int num; unsigned int mask; comedi_t *device; unsigned int subdevice = 2; char *filename = "/dev/comedi0"; unsigned int ducks = 5; int loadgun =0; /*==================================================================================================================*/ banner();/*======================================================================================================================*/ /*SETUP START*/ device=comedi_open(filename); if(!device) /*open comedi device , exit with error msg if something goes wrong*/ { comedi_perror(filename); exit(0); }/*====================================================================================================================*/ /*check we have a valid bidirectional dio port subdevice*/ stype=comedi_get_subdevice_type(device,subdevice); if(stype!=COMEDI_SUBD_DIO) { printf("%d is not a digital I/O subdevice\n",subdevice); exit(0); } int i = 0; int retval = 0; for(i=0;i<8;i++) retval=comedi_dio_config(device,subdevice,i,COMEDI_OUTPUT); /*SET UP END*//*====================================================================================================================*//*====================================================================================================================*/ /*Program logic*/ mask = 0x00; switches = (unsigned int) switches; comedi_dio_bitfield(device,subdevice,mask, &switches); switches = switches & 0x00ff00; switches >>= 8; switches = ~switches; switches = switches & 0x000000ff; /*====================================================================================================================== |-------> SWITCHES <---------|======================================================================================================================*/ /*SWITCH 8 (TOP SWITCH)*/ /*on or off power switch*/ while(((switches & 128) != 128) && (ducks != 255) && (ducks !=0) ) /*loop ducks while top switch is off*/ { /*display ducks*/ mask = 0xff; switches = (unsigned int) ducks; comedi_dio_bitfield(device,subdevice,mask, &switches); /*read swicthes*/ mask = 0x00; comedi_dio_bitfield(device,subdevice,mask, &switches); switches = switches & 0x00ff00; //0110 0110 0000 0111 0100 0100 //0000 0000 1111 1111 0000 0000 & 0x00ff00 //----------------------------- //0000 0000 0000 0111 0000 0000 switches >>= 8; //0000 0000 0000 0111 0000 0000 //0000 0000 0000 0000 0000 0111 switches = ~switches; //1111 1111 1111 1111 1111 1000 switches = switches & 0x000000ff; printf("switch = %d\n", switches); /*==================================================================================================================*/ /*SWITCH 6 & 7(speed of ducks)*/ if (((switches & 64) == 64) && (switches & 32) ==32) /*sw6 on or off for slow or fast speeds*/ { printf("\t\tspeed 3\n"); usleep(500000); /*semi fast*/ } else if (((switches & 64) != 64) && (switches & 32) ==32) { printf("\t\tspeed 4\n"); usleep(250000);/*not so slow*/ } else if (((switches & 64) == 64) && (switches & 32) !=32) { printf("\t\tspeed 2\n"); usleep(1000000);/*fast*/ } else { printf("\t\tspeed 1\n"); usleep(1500000); /*slow*/ }/*==================================================================================================================*/ /*SWITCH 5 (change direction)*/ /*CYCLE DOWN*/ if ((switches & 16) == 16) { printf("Going down\n"); ducks >>= 1; /*shift ducks down*/ if ((switches & 1) == 1) { if ((switches & 1) != 1) { loadgun =1; printf("gun is loaded!! %d\n",loadgun); } else { //We fired a shot /* If there is a duck*/ if (((ducks & 1) == 1) && (loadgun == 1)) { ducks --; loadgun = 0; printf("gun is empty!! %d\n",loadgun); } else { /* There is no duck */ ducks += 256; ducks ++; } } } else { //We did not fire if ((ducks & 1) == 1) { ducks += 256; } } }/*---------------------------------------------------------------------------------------------------------------*/ /*CYCLE UP*/ else if ((switches & 16) !=16) { printf("Going up\n"); ducks<<=1;/*Shift ducks up*/ if ((ducks & 256) == 256) { ducks -= 256; ducks ++; } } /*==============================================================================================================*/ /*SWITCH 3*/ /*=====================================================================================================================*/ /*SWITCH TWO second firing switch*/ /*===================================================================================================================*/ /*SWITCH ONE (BOTTOM SWITCH) firing switch*/ if ((switches & 1) == 1) { /*Switch 1 is on*/ printf("Switch 1 is on - firing a shot\n"); /*We are firing*/ if (((ducks & 1) == 1) && (loadgun ==1)) { /*There is a duck there!*/ printf("Duck hit!!\n"); ducks -= 1;/*We shot the duck -remove it*/ loadgun = 0; printf("gun is empty!! %d",loadgun); } else if ((ducks &1) != 1) { /*You missed there is no duck there*/ printf("you missed, unlucky!!\n"); ducks ++; loadgun = 0; printf("gun is empty!! %d\n",loadgun); } } else { /*Switch 0 is NOT on*/ printf("Switch 1 is NOT on\n"); loadgun = 1; printf("gun is loaded!! %d\n",loadgun); }/*0----------------------------------------------------------------------------------------------------------------*/ } if (ducks == 255) { printf("\n\n\t\tYou lose, ducks have taken over!!\n"); banner3(); } if (ducks == 0) { printf("\n\t\tYou win, the world is safe from ducks!!\n"); } /*----------------------------------------------------------------------------------------- on off switch test ------------------------------------------------------------------------------------------*/ if ((switches & 128) == 128) { printf("\n\n\t\tTOP SWITCH IS ON!! TOP SWITCH MUST BE OFF TO PLAY DUCKSHOOT\n"); banner2(); return 0; }