Sunteți pe pagina 1din 4

LIGHTCHASER THEO

PROGRAM IN C++
I MATHEWS BOBY prompted to enter the delay between the functions to draw lines, rectangles,
the consecutiveLEDs to glow in milli- output text, change colour, etc.
FF h i s I i g h t c h a s e rp r o g r a m i s seconds.Next, you have to enter the 7. coordinates( ) and click(h f). This
written in C++ and compiled number of times you need to display function checks whether the mouse
I
t using Turbo C++ compiler.It the lighting pattern. pointer is inside any of the rectangu-
can be used to create lighting anima- Now, press any key to start the Iar buttons being drawn. If the pointer
tion sequencesto attract attention in Iight display.Justpressany key on the is inside, it invokes the click(int) func-
advertising and promotion. In addi- keyboard once to stop the current dis- tion, where the argument represents
tion, it can be used to produce pleas- play. You can exit from the program the rectangle in whicl-r the pointer en-
ing lighting effectsand disco lights. by clicking'exit' button. tered. Now, the click(int) function
The program can be implemented checks for a left button click event. lf
with a simple interfacing circuit using Software there is a click event, the correspond-
eight LEDs connectedacrossthe PC's A total of eight functions have been ing LEDs display sequence comes inti'r
parallel port. The software is easy to defined in the software program ex- action.
'main (
understand and anyone can easily cluding ).' Each function is ex- 8. main( ). The program's main( )
modify it to produce a new display plained below: contains an infinite loop through
sequence. f. initialize( ). This function which the functions menu( ) and coor-
initialises graphics, mouse and also dinates( ) are executed one after the
GUI-basedprogram clears the parallel port. The third ar- other. First, the initialise( ) function is
Graphics function is included in the gument to the'initgraph'function is a invoked followed by the infinite loop.
program to make a GUI (graphical string specifying to the path of the
user interface)menu-driven applica-
'egavga.bgi'
graphic driver file. If the Executionof the program
tion. Mouse function makes it user- file is present in the folder where the We will now see how the execution
friendly. You can use the mouse to execution program is stored, you of the program takes place. First, the
click various options in the menu. needn't specify anything in the pro- code present in the main( ) function
When you run the program, you gram. Mouse is initialised by passing is executed. Before the loop starts,
'51' (=9a33
can see three separatesectionson the a value in hex) to the soft- the initialise( ) function is invoked.
I
I screen/namely, 'styles,' 'settings'and ware interrupt interface function The mouse and the graphics are
I 'instructions.' 'int86.'
initialised and the parallel port is
r
r Under 'styles'section,you can see 2. shozumouse( ). This function cleared. The address for parallel port
a total of nine rectangularbuttons with mousepointer on the output is'0x0378' in hexadecimal system. In-
red borders. Eight of them are num- :i:H formation is sent to the port r.t'ith the
bered '1' to '8' and the last one is 'exit'
'outport(int
3. hiilemouse( ). This function hides command portid, int
button. You can select a display pat- the mouse pointer from the output value)' Thc bir-rary equivalent (as a
'value'
tern style from any given eight options screen. This is done when we have to byte) is sent to the data pins

lr for displaying the LEDs.


'Settings'is the input sectionwhere
output some text, etc on the screen.
Otherwise, the pointer can cause prob-
of the port.
After initialisation, the next
'menu(
you can enter delay times and num- lcms to the text or drawing. )' function is invoked. This
ber of times to be displayed. 4. Readmouse( ). This function is function draws the required boxes
To make the program user- invoked whenever we need the coor- (buttons) and outputs other texts. In-
friendly, there is an 'instructions' sec- dinates of the current position of side the menu( ) function, first call the
tion. The input parameters displayed mouse.'x1' stores x-coordinate and' y7' clear( ) function to clear the screen. Be-
in the settings section are explained stores y-coordinate value. fore clearing the screen, the mouse
for the user in the instruction section. 5. Clear( ). This function is invoked pointer is hidden. After performing all
While you run the program, keep an when we need to clear the screen. the display functions, the mouse
I eye on the instructions section. 6. Menu( ). This function displays pointer appears again by invoking the

I
I
After selecting a style, you are the menu of the program. It contains showmouse( ) function.

li
Next, executethe co- statement on the value of variable'2.'
ordinates() funcfion. In- LEOl So you enter into
o 14R14 Displayformats
side the coordinates( ) o- ^vw the loop. Inside the
vl
function, the program
continuously polls the
U o-
o R2
a loop, you can see
'flag1' equated to
The LEDs will glow according to the
J 7 l byte available at the parallel port. The
coordinatesof the mouse 3 .ff. d '0.'Now, read the byte available is the binary equivalent
E o- 7l
pointer, that is, savesthe F o status of the of the value sent to the port using the
position of the mouse Uz o--
^L.^ a mouse through the outport command. Let us seedifferent
Y 18 .ff.
z
pointer on the screenand (J a readmouse() func- display formats available in this pro-
ot
c h e c k s w h e t h e r t h e tr
F -l
tion. The next two gram.
pointer is inside any of I ct ? .16^ a lines of the code In the sourcecode,you can seethat
J 7l

the rectangles drawn. U


? .11 a prevent some there are different 'cases'based on the
This is done using logi-
f
ot 7l problems associ- value of'2.'In eachcase,you have a
otI
cal AND operator'&&.' I ^l'^ d ated with the mo- 'do while' loop which works for the
U
I 7l
Check whether the I Rl-F8=2200 LEDS tion of the mouse number of times that you enter. Inside
receivedvalue of coordi- oz o l with its button the loop, it outputs the value to the
nates (both 'X' and 'Y') o -l
? clicked. Also, they parallel port. You may see that some
N
of the pointer is greater prevent multiple odd valueslike 85.170.240and 15hav-
than the smallest values o l? clicks. ing interesting binary equivalents due
o
of coordinatesof the rect- 25 Next, check the to their symmetry can be sent to the
o
angle and smaller than negated value of port.
the largestvalue of coor- Fig. 1: lnbrtacing circuit of light chaser 'k'. This is done to
85= 01010101
170= 10101010
240= 11110000
15
dinates of the rectangle.program in C++ check whether the = 00001111
You can refer to the code button is released. 'outport'
After each statement,
to have an insight of this simple tech- If the button is released,enter the code 'delay (
there is a statement called )'
nique. If the pointer is inside any of corresponding to the true part of if' which suspends the execution of the
the nine rectangle boxes drawn, the condition. next statement for the time specified
click(int) function is invoked. Here, the Here, checkingof the value of vari- inside the parentheses in milliseconds.
argument represents in which rect- able'z' is done.If you click a rectangle This brings a delay for glowing of the
angle the pointer is present. Also you other than the one inscribedwith'exit' consecutive LEDs.
can see that there is a break statement button, you are prompted to enter the The binary values sent to the port
based on the variable flag. This will values for delay and the number of for a one-time display in each case are
be discussedlater. times to display. given in the table on next page.
Now, imagine that the mouse was Here, we have used an interesting To exit a sequence before it goes to
in the first rectangle, that is, the rect- function in C++. The function is 'atol completion, you may press any key
angle in which 'f is inscribed. This (string)'.Thisfunction convertsa string while the display is
'on.'
Make sure
will invoke the click (int) function and into long type. We have used this be- that you press the key only once. You
the argumentpassedis '1.' Now, go to cause if one enters charactersother can see in the source code that in each
the click(int) function, which is the than digits, it can cause problems in 'if'
loop, there is an condition whose
most important part of this program. the program. So first get a string and true part is executed when a keystroke
The value that you pass to the func- convert it into 'long' data t1pe. is available. This is'done using the
tion is stored in the variable '2.' Next, After entering values for the above- bioskey (1) function, which returns a
you can see a loop based on the vari- mentioned item, press 'enter' to start non-zero value when a keystroke is
ablevalue of 'k'. The variable'k'holds the display. This is given in the in- cietected. lf a keystroke is detected, it
a non-zero value if a left mouse but- structions section of the program. If 'f
breaks from the loop storing in
ton click takes place. Otherwise, it is you press any other key, you will go 'flagl.'
Later,'flagf is checked and if
'0.' back to the position where the program '1,'
its value is the key pressed is stored
Remember that here the mouse had started. You won't exit the pro- in the variable 'ch.'
pointer was only inside the rectangle gram. If you exit the sequence in the nor-
and no click event took place. So, if Checking of whether you pressed mal way, that is, at the end of the dis-
you do not click the mouse, it will re- 'cnter' or any other key is done using play, you go back to the coordinates( )
turn to the coordinates() function and the bioskey(0) function. It waits for a function after changing the value of
continue polling the position of the keystroke and returns an integer value 'fIag' '1.'
to Thereafter, inside the co-
mouse. It will go to the click( ) func- which is different for different keys. ordinates( ) function, the value of the
tion only if the pointer is inside any of For 'enter'key, the value equals'7181.' flag is checked and if it is
'1'
you stop
the rectangles. After you press 'enter' key, you will from the loop and go back to the
'k'be- go to different 'cases' with a switch
Now, click a button. Then main( ) function. The 'exit' button ex-
d=
lr BinaryValues
Sentto lhePortlor a 0ne-Time
Display Through PC's Parallel
Port Using Visual Basic'
by Adeeb Raja in March

00000001 00000001 00000001 10101010 111'r0000 1 1 1 1 1 1 1 11 0 0 0 0 0 0 1 0000001 1 2005 issue of EFY maga-
00000010 00000011 0 0 0 0 0 0 1 10 1 0 1 0 1 0 1 0 0 0 0 1 1 1 1 00000000 01000010 000001 10 zine.
Construct the hard-
00000100 00000111 000001 11 00100100 00001100
ware according to the fig-
00001000 00001111 00001 111 00011000 00011000
ure given. Connect the 25-
00010000 00011111 0 0 0 1 1 1 1 1 00110000
pin male connector to the
0 0 1 0 0 0 0 00 0 1 1 1 1 1 1 0 0 1 1 1 1 1 1 , 01100000
parallel port. The files
0'1000000 01111111 0 1 1 1 1 1 I1 1 11000000 'egavga.bgi'
and'trip.chr'
1 0 0 0 0 0 0 01 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
must be present in the
11111111
folder where the main ex-
01111111
ecutable program is lo-
00t11111
cated. Note that'trip.chr'
00011111 present in the same folder
00001 111 is a font file.
000001 11 EFY notes. 1. The
0000001 1 source code and other rel-
'
00000001 evant files of this project
ecutes the exit(0) statement and you Pins 18 to 25 are grounded. A 25-pin have been included in this month's
can exit the program. male connector must be used to inter- EFY{D.
face the external circuit to the PC. Eight 2. This program cannot be used
Hardware transistors (8C547) can be used to with Windows XP because it cannot
The hardwarccomprisesinterfacingof drive the higher-power LEDs with ex- accessthe port due to security reasons.
the parallelport of the PC with LEDs. ternal power supply. In this circuit, Also if there are some other software
The parallel port consistsof 25 pins, transistors are not used. For connect- installed that uses parallel port like
out of which eight data pins starting ing heavy loads using relays, you may microcontroller programmer, the pro-
from 2 and ending al9 are used here. refer to the project 'Device Control gram may not work satisfactorily.

SOMETHINC IS DISPLAYED, ouftextxy(11O,316, ----- );


I ouftextxy(.120,31 0,"INSTRUCTIONS");
#include<ictream.h> relis.x.ax=2; outtextxy(,120,316,' ------- -" );
#include<conio.h> int86(0x33,&regs,&regs), ktcolor(4);
#include<drx.h> I rcrtangle(70,1 40,170,160);
*include<stdio.h> r<tangle(270,140.370,1 60);
#includc<stdlib.h> void clear0//CLEAtt MAIN MENU | @langle(47O,140,57O,'l6O);
#includecgraphics.h> t rtrtangle(70,196,170,216);
#include<bios.h> hidemouse0; re ctanBle(Z7 O,196,370,216);
cleardevice0; wta gle(470,1,570,216),
corot int fORT-Ox0378;//PORT ADDRESSOF PARAL- I r<<tangle(70,252,'l7O,272);
LEL T,|ORT r Kta gle(270,252,370,272);
long int glob' delay=0,tlow-num=0;//VARIABLES void readmouse0//READS COORDINATES OF rectangle(470,252,570,272);
USEDFOR TIMINC MOUSE'SCURRENT POsITION, *tcolor(i 3);
int v=2.a=85,r=15,s=O,o,i,i,flat=0,fla{l=0;/ / AND SE- I ouftextxy(120,146,"1 );
QUENCINC LED5 regs.x-ax=3; outtextxy(320,146,"2");
char d[5I.n[5l,ch[2]; int85(0x33,&rets,&regs); outtextxy(520,1i16,"3");
xl=rets.x.s; / / xI CONTATNSX-COORDINATE. outtextxy(l 20.202,"4");
mim REGSregs;//FOR MOUSE CONTROL. y\=tegs-x.dx;/ / yl CONTAINS Y-COORDINATE. outtextxy(320,202,"5");
inr x=50,y=200;// AS COORDINATES. k=regs.x.bx&l;//CHECKS FOR MOUSE'SLEFT-BUT- outtextxy(520,202,"6");
inr x1,y7,k;// AS MOUSE IIOINTER'SCOORDINATES TON CLICK. outtextxl' (124,25a,"7' );
l outtextxy(320,258,"8");
VOidiNitiAIiS0 / / INITIALISE MOUSE & GRAPFIICS, outtextxy(505,258,"EXIT");
{ void mdu0 *tcolor(15);
int gd=DETECT,gn; I outtextxy(3,10,360,"* CLICK A STYLE");
initgraph(&gd,&gn,"'); / /THIRD ARCUMENT IS clear0j showmou*0;
PATH OF ECAVCA.BCI FILE. setcolc(2); I
retangle(0,0,getmaxx0,getmaxy0);
/ /INITIALISEMOLSE.
regs.x.ar-0; Ihe(0,100,getmaxx0,l00); void click(int z),//CHECK FOR MOUSE CLICK.
int86(Or33,&regs,&regs); line(OJ00,getmaxx0,300); {
line(getmas0/ 2,300,getmax\0 / 2,getmaxy0) ; while(k)
mtport(POR1,0):/ /C L tARS PORT. setcolo{ 15); I
t *ttextstyle( 1,0,5); flagl=0;
outtextxy(110,20,'8 CHANNEL CHAISER"); readmou*0;
void showmou*0//SHOWS MOUSE ON SCREEN. rtcolor(14); while((inportb(0x3da)&8));
{ settexlstyle(0,0,1); '** while( !(inportb(0x3da)&8));
rw.x.ax=l; outtextxy(110,70, if(!K)//.K, STORESZERO IF A LEFI-BUTTON CLICK
int85{(k33,&!egs,&regsi; rtcolor(9); TAKES PLACE.
I outtextxy(300,1 10."STYLES"); I
outtextxy(300,1 16,'----'); ir(zt=9)
t
void hidoosd)//HIDES MOUSE.INVOKED WHEN outtextxv(140,310,"SETTINCS"); {
t
il
hidenouse0; glow nurn--j {
outtextxy(340,380,"*
ENTER DEI-AY"); outport(PORT,0); flag1=1;
outtextxy(100,356,"D8LAY: "); Iwhile(glow_num>0); break;
gotoxy(21,23); outport(PORT,0); I
gets(d); if(flag1) gets(ch), delay(glow_delay);
glow-delay=atol(d); break; I
outtextxy(340,400,"*
ENTER NUMBER OF TIMES"); case 4l glow_num--;
');
outtextxy(100,388,
TIMES i glow-num*=2; / /ST\LE4 outport(PORT,0);
gotoxy(21,25); do ]while(glow_num>0);
gets(n)i { outport(POItT,0);
glow_num=atol(n); if(a==170) if(flag1)gets(ch);
outtextxy(340,420,'*
PRESSENTERTO START"); a=85; break;
if(bioskey(0)!=7181) elseif(a--85) casev:
1 a=170; {
showmouse0; outport(PORT,a); ck)seBraph0;
menu0; jf(bioskey(1))//EXITSIF A KEY IS PRESSED exit(0);//EXITSFROM THE PIIOGRAM
break; I )
I flagl=1; l
outtextxy(340,440,'+
PRESSANY KEY ONCE TO RE- break; fl;rg=1;
TrRE"); I l
showmouse0;
{ delay(glow_delay); I

It: l glow-num--; l
switch(z) )while(glow-num>0);
{ outport(PORT,0);
case 1: if(flagi) gets(ch); vojd coordinates0//CHECKS WHETHER MOUSU'S
do break, POINTER IS INSIDE
{ case5: I //ANY OF THE RECTANCLESDI{AWN,
for(i=1;i<=128;i=i+2)//FOR STYLE1 glow num+=2; //STYLE5 showmouseO; //lF INSIDE INVOKESFUNCTION
do 'click(int)'.
I

f
outport(PORT,i)j I while(1)
if(bioskey(i))//EXITSlF A KEY IS PRESSED if(r==240) {
I r-15; readmouse0;
flag1=1; elseif(r==15) if(((x1>70&&x1<170)&&y1>140)&&y1<160)
break; r=240; I
I outport(PORT,r), click(1);
delav(glow-delay); if(bioskey(1))//EXITSlF A KEY lS PRESSED if(flag)
I I break;//GOESTO MAIN
clow num--j flag1=1; l
lwhile(glow_num>0); break; if(((x1>270&&xl<32))&&yl> l 40)&&y1<160)
if(flag1)gets(ch); ) I
outport(PORT,0); delay(glow-delay); click(2);
break; Blow_num--; if(flarj)
case2: lwhile(glow-num>0); break;//GOESTO MAIN
do outport(PORT,0), I
{ if(fla91 ) gets(ch); if (((x1>470&&x1
<570)&&y1>140)&&y1<160)
i=0;v=l; / /ST\I.E2 break; {
do case 6: click(3);
I glow-num*=2; /ISTYLF.6 if(flag)
i=i+v; do break;/ /GOES TO MAIN
I l
ougort(PORT,i); if(s==255) if(((x1>70&&x1<170)&&y1>196)&&y1<216)
if(bioskey(1))//EXITSIF A KEY IS PRESSED s=0; {
l elseif(s==0) click(4);
fla81=1; s=255; if(flag)
break; outport(PORT,s); break;//GOES TO MAIN
I if(bioskey(1))//EXITSIF A KEY IS PRESSED )
delay(glow_delay); { if(((x1>270&&x1<370)&&y1
>196)&&y1
<216)
)while(i<=255); flagl=1; {
glow_num--; break; click(5);
outport(PORT,0); l if(flag)
Iwhile(glow-num>0); delay(glow delay); break;/ /GOES TO MAIN
outport(PORT,o); glow num--: I
if(fla91)gets(ch); iwhile(glow_num>0); if(((x1>470&&x1<570)&&y1>1e6)&&y1
<216)
break; outport(PORT,0); {
case3: if(flagl) gets(ch); click(6);
do break: if(flag)
l case 7i / /sTvLET break;//COESTO MAIN
i=0;v=1; / /STYLE3 d<r 1
do l <170)&&y1>252)&&y1<272)
if(((x1>70&&x1
I t=0; I
i=i+v; for(i=129;j<s;i=((i+(3.j))/2)) click(7);
outport(PORT,i); { if(flag)
v--v*2; outport(PORT,i); break;//GOESTO MAIN
if(bioskey(1))//EXITSIF A KEY IS PRESSED if(bioskey(l))//EXITS IF A KEY IS PRESSED l
I I >252)&&y1<272)
if(((x1>270&&x1<370)&&yl
flagl=1; flag1=1; {
break; breaki click(8);
I l if(flag)
delay(glow_delay); delay(glow_delay); break;,/,/COES TO MAIN
if(i==255) if(j==21 I
I i=4; if(((x1>470&&xl
<570)&&y1
>252)&&y1
<272)
o=128; else click(9);
do j*t; I
{ ) l
outport(PORT,i); outport(PORT,O);
i=i-oi glow_num--; void mainO
o=o/2) Iwhile(glow_num>0); l
if&ioskey(1))//EXITS IF A KEY IS PRESSED if(fla91)gets(ch); initialise0;
{ break; while(1)
flagl=l; case 8l //STYLE 8 {
break; do menut),
I I c@rdinates0;
delay(glow_delay); for(i=3,i<=192;i=it2) flag=9;
Iwhile(i>=1); { I
l outport(PORT,i); ).
)while(i<-255); if(bioskey(1))//EXITSIF A KEY IS PRESSED

S-ar putea să vă placă și