#include "stdio.h"
#include "conio.h"
#include <dos.h>
#include<graphics.h>
/*****
Key struct:
position:x1,y1,x2,y2
State:isdown
white or black key:iswhite
nearby key:lwbk,rwbk
*******/
typedef struct _Key{
int x1;
int y1;
int x2;
int y2;
int isdown;
int iswhite;
int lwbk;
int rwbk;
} Key;
/*****
static grobal Variables statement:
7 white keys:wk[7]
5 black keys:bk[5]
width,height:kw,kh
frequant:frequ[8]
pcfrequant:pcfre
keynum
count
*******/
static wknum=14;
static Key wk[14];
static bknum=10;
static Key bk[10];
static int kw=32,kh=160;
static int leftwidth=2*32;
int frequ[8]={262, 294, 330, 349, 392, 440, 494, 524};
static long int pcfre=1190000;
static int keyindex;
static int count;
/*****
function statement:
makesound():
initkey();
change key state:setup(),setdown()
initdrawkey();
*******/
void makesound();
Key set(int x1,int w,int h);
void setup(Key *k);
void setdown(Key *k);
void keyinit();
void initdrawkey();
/*initkey*/
Key initset(int x1,int w,int h,int isw,int lwbk,int rwbk){
Key k;
k.x1=x1;
k.y1=0;
k.x2=x1+w;
k.y2=h;
k.isdown=0;
k.iswhite=isw;
k.lwbk=lwbk;
k.rwbk=rwbk;
return k;
}
/*keyinit()*/
void keyinit(){
wk[0]=initset(leftwidth+0*kw,kw,kh,1,-1,0);
wk[1]=initset(leftwidth+1*kw,kw,kh,1,0,1);
wk[2]=initset(leftwidth+2*kw,kw,kh,1,1,-1);
wk[3]=initset(leftwidth+3*kw,kw,kh,1,-1,2);
wk[4]=initset(leftwidth+4*kw,kw,kh,1,2,3);
wk[5]=initset(leftwidth+5*kw,kw,kh,1,3,4);
wk[6]=initset(leftwidth+6*kw,kw,kh,1,4,-1);
wk[7]=initset(leftwidth+7*kw,kw,kh,1,-1,6);
wk[8]=initset(leftwidth+8*kw,kw,kh,1,6,7);
wk[9]=initset(leftwidth+9*kw,kw,kh,1,7,-1);
wk[10]=initset(leftwidth+10*kw,kw,kh,1,-1,8);
wk[11]=initset(leftwidth+11*kw,kw,kh,1,8,9);
wk[12]=initset(leftwidth+12*kw,kw,kh,1,9,10);
wk[13]=initset(leftwidth+13*kw,kw,kh,1,10,-1);
bk[0]= initset(leftwidth+kw-kw/4,kw/2,kh/2,0,0,1);
bk[1]= initset(leftwidth+kw*2-kw/4,kw/2,kh/2,0,1,2);
bk[2]= initset(leftwidth+kw*4-kw/4,kw/2,kh/2,0,3,4);
bk[3]= initset(leftwidth+kw*5-kw/4,kw/2,kh/2,0,4,5);
bk[4]= initset(leftwidth+kw*6-kw/4,kw/2,kh/2,0,5,6);
bk[5]= initset(leftwidth+kw*8-kw/4,kw/2,kh/2,0,7,8);
bk[6]= initset(leftwidth+kw*9-kw/4,kw/2,kh/2,0,8,9);
bk[7]= initset(leftwidth+kw*11-kw/4,kw/2,kh/2,0,10,11);
bk[8]= initset(leftwidth+kw*12-kw/4,kw/2,kh/2,0,11,12);
bk[9]= initset(leftwidth+kw*13-kw/4,kw/2,kh/2,0,12,13);
}
/*draw black key*/
void drawblackkey(Key *k){
if(!(*k).isdown){
setfillstyle(SOLID_FILL,DARKGRAY);
bar((*k).x1,(*k).y1,(*k).x2,(*k).y2);
}
else{
setfillstyle(SOLID_FILL,LIGHTBLUE);
bar((*k).x1,(*k).y1,(*k).x2,(*k).y2);
}
}
/*draw white key*/
void drawwhitekey(Key *k){
if(!(*k).isdown){
setfillstyle(SOLID_FILL,WHITE);
bar((*k).x1,(*k).y1,(*k).x2,(*k).y2);
setcolor(DARKGRAY);
rectangle((*k).x1,(*k).y1,(*k).x2,(*k).y2);
/*draw nearby key*/
if(!(*k).lwbk>-1){
drawblackkey(&bk[(*k).lwbk]);
}
if(!(*k).rwbk>-1){
drawblackkey(&bk[(*k).rwbk]);
}
}
else{
setfillstyle(SOLID_FILL,LIGHTBLUE);
bar((*k).x1,(*k).y1,(*k).x2,(*k).y2);
setcolor(DARKGRAY);
rectangle((*k).x1,(*k).y1,(*k).x2,(*k).y2);
/*draw nearby key*/
if(!(*k).lwbk>-1){
drawblackkey(&bk[(*k).lwbk]);
}
if(!(*k).rwbk>-1){
drawblackkey(&bk[(*k).rwbk]);
}
}
}
/*setdown() and setup()*/
void setdown(Key *k){
(*k).isdown=1;
if((*k).iswhite)
drawwhitekey(k);
else
drawblackkey(k);
}
void setup(Key *k){
(*k).isdown=0;
if((*k).iswhite)
drawwhitekey(k);
else
drawblackkey(k);
}
/*initdrawkey()*/
void initdrawkey(){
int gdriver,gmode,i;
gdriver=DETECT;
initgraph(&gdriver,&gmode,"c:\\tc");
setbkcolor(YELLOW);
cleardevice();
for(i=0;i<wknum;i++){
setfillstyle(SOLID_FILL,WHITE);
bar(wk[i].x1,wk[i].y1,wk[i].x2,wk[i].y2);
setcolor(DARKGRAY);
rectangle(wk[i].x1,wk[i].y1,wk[i].x2,wk[i].y2);
}
for(i=0;i<bknum;i++){
setfillstyle(SOLID_FILL,DARKGRAY);
bar(bk[i].x1,bk[i].y1,bk[i].x2,bk[i].y2);
}
}
/*makesound()*/
void makesound(){
count=(int)(pcfre/frequ[keyindex]);
asm mov al, 10110110b
asm out 43h, al
asm mov ax,count
asm out 42h, al
asm mov al, ah
asm out 42h, al
asm in al, 61h
asm or al, 03h
asm out 61h, al
delay(0xfffff000);
asm in al, 61h
asm and al, 11111100b
asm out 61h, al
}
void main(){
keyinit();
initdrawkey();
settextstyle(4, 0, 10);
outtextxy(200, 230, "Interface Principle Course Design");
outtextxy(200, 250, "------------Piano----------------");
outtextxy(200, 270, "-------Input 1...7 Play----------");
outtextxy(200, 290, "-------Input CTRL+C Exit---------");
outtextxy(200, 310, "-------Auther:Ying Wenjie--------");
outtextxy(200, 340, "---------Date:2008-7-3-----------");
while(1){
keyindex=getch();
if(keyindex==3)
break;
keyindex=keyindex-0x30;
if(keyindex<1||keyindex>8)
continue;
keyindex--;
setdown(&wk[keyindex]);
makesound();
setup(&wk[keyindex]);
}
}
#include "conio.h"
#include <dos.h>
#include<graphics.h>
/*****
Key struct:
position:x1,y1,x2,y2
State:isdown
white or black key:iswhite
nearby key:lwbk,rwbk
*******/
typedef struct _Key{
int x1;
int y1;
int x2;
int y2;
int isdown;
int iswhite;
int lwbk;
int rwbk;
} Key;
/*****
static grobal Variables statement:
7 white keys:wk[7]
5 black keys:bk[5]
width,height:kw,kh
frequant:frequ[8]
pcfrequant:pcfre
keynum
count
*******/
static wknum=14;
static Key wk[14];
static bknum=10;
static Key bk[10];
static int kw=32,kh=160;
static int leftwidth=2*32;
int frequ[8]={262, 294, 330, 349, 392, 440, 494, 524};
static long int pcfre=1190000;
static int keyindex;
static int count;
/*****
function statement:
makesound():
initkey();
change key state:setup(),setdown()
initdrawkey();
*******/
void makesound();
Key set(int x1,int w,int h);
void setup(Key *k);
void setdown(Key *k);
void keyinit();
void initdrawkey();
/*initkey*/
Key initset(int x1,int w,int h,int isw,int lwbk,int rwbk){
Key k;
k.x1=x1;
k.y1=0;
k.x2=x1+w;
k.y2=h;
k.isdown=0;
k.iswhite=isw;
k.lwbk=lwbk;
k.rwbk=rwbk;
return k;
}
/*keyinit()*/
void keyinit(){
wk[0]=initset(leftwidth+0*kw,kw,kh,1,-1,0);
wk[1]=initset(leftwidth+1*kw,kw,kh,1,0,1);
wk[2]=initset(leftwidth+2*kw,kw,kh,1,1,-1);
wk[3]=initset(leftwidth+3*kw,kw,kh,1,-1,2);
wk[4]=initset(leftwidth+4*kw,kw,kh,1,2,3);
wk[5]=initset(leftwidth+5*kw,kw,kh,1,3,4);
wk[6]=initset(leftwidth+6*kw,kw,kh,1,4,-1);
wk[7]=initset(leftwidth+7*kw,kw,kh,1,-1,6);
wk[8]=initset(leftwidth+8*kw,kw,kh,1,6,7);
wk[9]=initset(leftwidth+9*kw,kw,kh,1,7,-1);
wk[10]=initset(leftwidth+10*kw,kw,kh,1,-1,8);
wk[11]=initset(leftwidth+11*kw,kw,kh,1,8,9);
wk[12]=initset(leftwidth+12*kw,kw,kh,1,9,10);
wk[13]=initset(leftwidth+13*kw,kw,kh,1,10,-1);
bk[0]= initset(leftwidth+kw-kw/4,kw/2,kh/2,0,0,1);
bk[1]= initset(leftwidth+kw*2-kw/4,kw/2,kh/2,0,1,2);
bk[2]= initset(leftwidth+kw*4-kw/4,kw/2,kh/2,0,3,4);
bk[3]= initset(leftwidth+kw*5-kw/4,kw/2,kh/2,0,4,5);
bk[4]= initset(leftwidth+kw*6-kw/4,kw/2,kh/2,0,5,6);
bk[5]= initset(leftwidth+kw*8-kw/4,kw/2,kh/2,0,7,8);
bk[6]= initset(leftwidth+kw*9-kw/4,kw/2,kh/2,0,8,9);
bk[7]= initset(leftwidth+kw*11-kw/4,kw/2,kh/2,0,10,11);
bk[8]= initset(leftwidth+kw*12-kw/4,kw/2,kh/2,0,11,12);
bk[9]= initset(leftwidth+kw*13-kw/4,kw/2,kh/2,0,12,13);
}
/*draw black key*/
void drawblackkey(Key *k){
if(!(*k).isdown){
setfillstyle(SOLID_FILL,DARKGRAY);
bar((*k).x1,(*k).y1,(*k).x2,(*k).y2);
}
else{
setfillstyle(SOLID_FILL,LIGHTBLUE);
bar((*k).x1,(*k).y1,(*k).x2,(*k).y2);
}
}
/*draw white key*/
void drawwhitekey(Key *k){
if(!(*k).isdown){
setfillstyle(SOLID_FILL,WHITE);
bar((*k).x1,(*k).y1,(*k).x2,(*k).y2);
setcolor(DARKGRAY);
rectangle((*k).x1,(*k).y1,(*k).x2,(*k).y2);
/*draw nearby key*/
if(!(*k).lwbk>-1){
drawblackkey(&bk[(*k).lwbk]);
}
if(!(*k).rwbk>-1){
drawblackkey(&bk[(*k).rwbk]);
}
}
else{
setfillstyle(SOLID_FILL,LIGHTBLUE);
bar((*k).x1,(*k).y1,(*k).x2,(*k).y2);
setcolor(DARKGRAY);
rectangle((*k).x1,(*k).y1,(*k).x2,(*k).y2);
/*draw nearby key*/
if(!(*k).lwbk>-1){
drawblackkey(&bk[(*k).lwbk]);
}
if(!(*k).rwbk>-1){
drawblackkey(&bk[(*k).rwbk]);
}
}
}
/*setdown() and setup()*/
void setdown(Key *k){
(*k).isdown=1;
if((*k).iswhite)
drawwhitekey(k);
else
drawblackkey(k);
}
void setup(Key *k){
(*k).isdown=0;
if((*k).iswhite)
drawwhitekey(k);
else
drawblackkey(k);
}
/*initdrawkey()*/
void initdrawkey(){
int gdriver,gmode,i;
gdriver=DETECT;
initgraph(&gdriver,&gmode,"c:\\tc");
setbkcolor(YELLOW);
cleardevice();
for(i=0;i<wknum;i++){
setfillstyle(SOLID_FILL,WHITE);
bar(wk[i].x1,wk[i].y1,wk[i].x2,wk[i].y2);
setcolor(DARKGRAY);
rectangle(wk[i].x1,wk[i].y1,wk[i].x2,wk[i].y2);
}
for(i=0;i<bknum;i++){
setfillstyle(SOLID_FILL,DARKGRAY);
bar(bk[i].x1,bk[i].y1,bk[i].x2,bk[i].y2);
}
}
/*makesound()*/
void makesound(){
count=(int)(pcfre/frequ[keyindex]);
asm mov al, 10110110b
asm out 43h, al
asm mov ax,count
asm out 42h, al
asm mov al, ah
asm out 42h, al
asm in al, 61h
asm or al, 03h
asm out 61h, al
delay(0xfffff000);
asm in al, 61h
asm and al, 11111100b
asm out 61h, al
}
void main(){
keyinit();
initdrawkey();
settextstyle(4, 0, 10);
outtextxy(200, 230, "Interface Principle Course Design");
outtextxy(200, 250, "------------Piano----------------");
outtextxy(200, 270, "-------Input 1...7 Play----------");
outtextxy(200, 290, "-------Input CTRL+C Exit---------");
outtextxy(200, 310, "-------Auther:Ying Wenjie--------");
outtextxy(200, 340, "---------Date:2008-7-3-----------");
while(1){
keyindex=getch();
if(keyindex==3)
break;
keyindex=keyindex-0x30;
if(keyindex<1||keyindex>8)
continue;
keyindex--;
setdown(&wk[keyindex]);
makesound();
setup(&wk[keyindex]);
}
}