Digital_project 57340500039 57340500070
Dependencies: mbed
Fork of Digital_project by
Revision 2:6b2526c99e5c, committed 2015-12-09
- Comitter:
- 57340500039
- Date:
- Wed Dec 09 10:55:01 2015 +0000
- Parent:
- 1:a4c852d25ead
- Commit message:
- Digital project A06
Changed in this revision
diff -r a4c852d25ead -r 6b2526c99e5c LCD4884.cpp --- a/LCD4884.cpp Mon Dec 07 16:12:55 2015 +0000 +++ b/LCD4884.cpp Wed Dec 09 10:55:01 2015 +0000 @@ -17,11 +17,12 @@ #include <SPI.h> DigitalOut SpiClk(D2); //2- Serial Clock(Master Output) -DigitalOut SpiMosi(D3); //3- Master Output,Slave Input -DigitalOut LcdDC(D4); //4- Data/Command(command active low) -DigitalOut SpiCS(D5); //5- Chip Select,Slave Transmit Enable(active low,Master Output) +DigitalOut SpiMosi(D3); //3- Master Output,Slave Input +DigitalOut LcdDC(D4); //4- Data/Command(command active low) +DigitalOut SpiCS(D5); //5- Chip Select,Slave Transmit Enable(active low,Master Output) DigitalOut LcdRst(D6); //6- One Reset button -PwmOut LcdBl(D7); //7- LCD backlight +PwmOut LcdBl(D7); //7- LCD backlight +AnalogIn JoyStick(A0); //8- JoyStick analog input LCD4884::LCD4884() {}; @@ -163,4 +164,84 @@ LCD_write_byte(Pix,1); printf("%d %d %d\n",X,line,Pix); wait_ms(10); +} +/******************************************************************/ +/* write char in big font */ +void LCD4884::LCD_write_char_big (unsigned char X,unsigned char Y, unsigned char ch, char mode) +{ + unsigned char i, j; + unsigned char *pFont; + unsigned char ch_dat; + + pFont = (unsigned char *) big_number; + + if(ch == '.') + ch = 10; + else if (ch == '+') + ch = 11; + else if (ch == '-') + ch = 12; + else + ch = ch & 0x0f; + + for(i=0;i<3;i++) + { + LCD_set_XY ( X, Y+i); + + for(j=0; j<16; j++) + { + ch_dat = *(pFont+ch*48 + i*16 +j); + LCD_write_byte( (mode == MENU_NORMAL)? ch_dat : (ch_dat^0xff), 1); + } + } +} +/******************************************************************/ +void LCD4884::LCD_write_string_big ( unsigned char X,unsigned char Y, char *string, char mode ) +{ + while ( *string ) + { + LCD_write_char_big( X, Y, *string , mode ); + + if(*string++ == '.') + X += 5; + else + X += 12; + } +} + + +/******************************************************************/ +char LCD4884::LCD_joystick() +{ + float pos; + pos = JoyStick.read(); + //up = 1.00, down = 0.47, right = 0.73, left = 0.00, middle = 0.79 + if(pos>0.980000) return 'u'; + else if(0.460000<pos&&pos<0.480000) return 'd'; + else if(0.720000<pos&&pos<0.740000) return 'r'; + else if(0.020000>pos) return 'l'; + else if(0.770000<pos&&pos<0.810000) return 'm'; + +} +/******************************************************************/ +void LCD4884::LCD_draw_bmp_pixel(unsigned char X,unsigned char Y,unsigned char *map, + unsigned char Pix_x,unsigned char Pix_y) +{ + unsigned int i,n; + unsigned char row; + + if (Pix_y%8==0) + row=Pix_y/8; //row from 1 to 6;Pix_y from R0 to R47 + else + row=Pix_y/8+1; //Quotient+1 + + for (n=0;n<row;n++) + { + LCD_set_XY(X,Y); + for(i=0; i<Pix_x; i++) + { + LCD_write_byte(map[i+n*Pix_x], 1); // D/C=1:write data to display RAM + } + Y++; + } } \ No newline at end of file
diff -r a4c852d25ead -r 6b2526c99e5c LCD4884.h --- a/LCD4884.h Mon Dec 07 16:12:55 2015 +0000 +++ b/LCD4884.h Wed Dec 09 10:55:01 2015 +0000 @@ -48,6 +48,17 @@ void LCD_write_string(unsigned char X,unsigned char Y,char *s, char mode); // *s can be input as "<your string>" void LCD_clear(void); // blank the screen void LCD_pixel(unsigned char X, unsigned char Y); //X,Y = x,y coodinate, mode : MENU_NORMAL = off/ others = on + void LCD_write_char_big (unsigned char X,unsigned char Y, unsigned char ch, char mode); + void LCD_write_string_big ( unsigned char X,unsigned char Y, char *string, char mode ); + char LCD_joystick(void); // return u = up, d = down, l = left, r = right, m = middle + + + + //include by ploy + void LCD_draw_bmp_pixel(unsigned char X,unsigned char Y,unsigned char *map,unsigned char Pix_x,unsigned char Pix_y); + + + }; } extern LCD4884 lcd;
diff -r a4c852d25ead -r 6b2526c99e5c main.cpp --- a/main.cpp Mon Dec 07 16:12:55 2015 +0000 +++ b/main.cpp Wed Dec 09 10:55:01 2015 +0000 @@ -1,39 +1,656 @@ #include "mbed.h" #include "LCD4884.h" #include <string.h> +#include <time.h> +#include <stdlib.h> +#define maxLvl 3 +#define high 24 +#define width 24 + +Ticker t; +Timer me; DigitalOut myled(LED1); -DigitalIn b(USER_BUTTON); -DigitalOut backlight(D7); -AnalogIn joy(A0); //up = 1.00, down = 0.47, right = 0.73, left = 0.00, middle = 0.79 +DigitalOut buzz(PC_3); +AnalogIn LDR(A5); +Serial pc(PA_15, PB_7); + +/* + modeA = watch time + modeB = before alarm + modeC = set alarm + modeD = play games + modeE = watch Data + modeF = bluetooth +*/ + +LCD4884 joy; +int sec=0; +int hourTime = 0; +int minTime = 0; +int hourAlarm = 1; +int minAlarm = 0; +int isAlarm = 0; +int timeNow = 0; +int level = 0; + +int wakeUp[3]={0,0,0}; +//0 late, 1 not late, 2 set +int addTime(int min); +void plusTime(); +int ring(int a); +int alarmNow(); +int menu(int n,char *input[]); +void print(int n,unsigned char X, unsigned char Y); +void win(); +void lose(); +int snake_game(); +int crossRoad_game(); +int modeA(); +int modeB(); +int modeC(); +int modeD(); +int modeE(); +int modeF(); int main() { - char I[2] = {MENU_NORMAL,'1'}; - int i = 0; - float pos = 0.00; - backlight = 1; - unsigned char x=0,y=0; - LCD4884 test; + t.attach(&plusTime, 1.0); + + int mode = 0,temp; + char *str[6] = {"watch time","before alarm","set alarm","play games","watch Data","bluetooth"}; + joy.LCD_init(); + /* + while(1){ + joy.LCD_write_string(0x00,0x00,"aaaaaa",'0'); + //joy.LCD_clear(); + }*/ + + while(1) { - test.LCD_init(); - //test.LCD_clear(); - //test.LCD_write_byte(0x01, 0); - //test.LCD_write_byte(0x01, 1); - //test.LCD_write_string('0','0',"1324",'0'); - - - while(1) { - test.LCD_pixel(x,y); - pos = joy.read(); - if(pos>0.98){ - if(y==0) y=47; - else y-=1; } - else if(0.46<pos&&pos<0.48) y+=1; - else if(0.72<pos&&pos<0.74) x+=1; - else if(0.02>pos){ - if(x==0) x=83; - else x-=1; } - wait_ms(150); - test.LCD_clear(); + temp = menu(6,str); + switch(temp){ + case 0: modeA();break; + case 1: modeB();break; + case 2: modeC();break; + case 3: modeD();break; + case 4: modeE();break; + case 5: modeF();break; + } + wait_ms(100); + } + //return 0; +} + +//time +int addTime(int min){ + minTime += min; + hourTime += minTime/60; + hourTime%=24; + minTime%=60; + return 0; +} +void plusTime(){ + sec++; + if(sec==60){ + sec = 0; + addTime(1); + } + if(hourTime==hourAlarm&&minTime==minAlarm){ + alarmNow(); + } +} + +//alarm +int isLight(){ + float a; + a = LDR.read(); + if(a<=0.5) return 1; //สว่าง + return 0; +} +int ring(int a){ + if(a==1) buzz = 1; + else buzz = 0; +} +int alarmNow(){ //เพิ่มเชคว่า เลท/ไม่เลท + me.start(); + int random,light=0,re=0; + srand(time(NULL)); + random = rand()%2; + ring(1); + if(random==1){ + while(!snake_game()); + }else{ + while(!crossRoad_game()); + } + while(!light){ + light =isLight(); + } + ring(0); + isAlarm = 0; + me.stop(); + if(me.read()>180) wakeUp[0]++; + else wakeUp[1]++; +} + +//modeA = watch time +int modeA(){ + char temp[6],c; + while(1){ + temp[0] = '0'+hourTime/10; + temp[1] = hourTime%10+'0'; + temp[2] = '.'; + temp[3] = '0'+minTime/10; + temp[4] = minTime%10+'0'; + temp[5] = '\0'; + + joy.LCD_clear(); + joy.LCD_write_string_big(10,2,temp,0); + + c=joy.LCD_joystick(); + if(c=='l') return -1; + wait_ms(100); + } +} + +//modeB = before alarm +int modeB(){ + char c,temp[7]; + int a=0,b=0,show; + + while(1){ + joy.LCD_clear(); + if(isAlarm){ + a = hourTime*60 + minTime; + b = hourAlarm*60 + minAlarm; + if(a>b){ + show = 24*60 - a + b; + }else if(a<b){ + show = b-a; + } + temp[0] = (show/60)/10 + '0'; + temp[1] = (show/60)%10 + '0'; + temp[2] = '.'; + temp[3] = (show%60)/10 + '0'; + temp[4] = (show%60)%10 + '0'; + temp[5] = '\0'; + joy.LCD_write_string_big(10,2,temp,0); + }else joy.LCD_write_string_big(10,2,"--.--",0); + + c=joy.LCD_joystick(); + if(c=='l') return -1; + wait_ms(100); + + } +} + +//modeC = set alarm +int modeC(){ + int mode=0; + int x[2]={0,0}; + char c,temp[6]; + + while(1){ + joy.LCD_clear(); + + if(mode==0){ + temp[0] = '0'+x[0]/10; + temp[1] = x[0]%10+'0'; + temp[2] = '\0'; + joy.LCD_write_string_big(7,2,temp,1); + temp[0] = '0'+x[1]/10; + temp[1] = x[1]%10+'0'; + temp[2] = '\0'; + joy.LCD_write_string_big(40,2,".",0); + joy.LCD_write_string_big(47,2,temp,0); + }else if(mode==1){ + temp[0] = '0'+x[0]/10; + temp[1] = x[0]%10+'0'; + temp[2] = '\0'; + joy.LCD_write_string_big(7,2,temp,0); + temp[0] = '0'+x[1]/10; + temp[1] = x[1]%10+'0'; + temp[2] = '\0'; + joy.LCD_write_string_big(40,2,".",0); + joy.LCD_write_string_big(47,2,temp,1); + } + + c=joy.LCD_joystick(); + switch (c){ + case 'l': + if(mode==1) mode=0; + else{ + joy.LCD_clear(); + joy.LCD_write_string(0,1,"back to menu?",0); + joy.LCD_write_string(0,2,"(Y) move Right",0); + joy.LCD_write_string(0,3,"(N) move Left",0); + while(1){ + wait_ms(200); + c=joy.LCD_joystick(); + if(c=='l'){ + break; + }else if(c=='r'){ + return -1; + } + } + } + break; + case 'r': + if(mode==0) mode=1; + else{ + joy.LCD_clear(); + joy.LCD_write_string(0,0,"set alarm at",0); + + temp[0] = '0'+x[0]/10; + temp[1] = x[0]%10+'0'; + temp[2] = '.'; + temp[3] = '0'+x[1]/10; + temp[4] = x[1]%10+'0'; + temp[5] = '\0'; + joy.LCD_write_string(20,1,temp,0); + + joy.LCD_write_string(0,3,"confirm?",0); + joy.LCD_write_string(0,4,"(Y) move Right",0); + joy.LCD_write_string(0,5,"(N) move Left",0); + while(1){ + wait_ms(200); + c=joy.LCD_joystick(); + if(c=='r'){ + isAlarm = 1; + wakeUp[2]++; + hourAlarm = (temp[0]-'0')*10+(temp[1]-'0'); + minAlarm = (temp[3]-'0')*10+(temp[4]-'0'); + return 0; + }else if(c=='l'){ + break; + } + } + } + break; + case 'u': + switch(mode){ + case 0: x[0] = (x[0]+1)%24; break; + case 1: x[1] = (x[1]+1)%60; break; + } + break; + case 'd': + switch(mode){ + case 0: x[0] = (x[0]+23)%24; break; + case 1: x[1] = (x[1]+23)%60; break; + } + break; + } + wait_ms(200); + } +} + +//modeD = play games +int modeD(){ + int mode; + char *data[2]; + data[0] = "Snake"; + data[1] = "Cross Road"; + + mode = menu(2,data); + wait_ms(100); + if(mode==0) snake_game(); + else if(mode==1) crossRoad_game(); + return 0; +} + +//modeE = watch Data +int modeE(){ + char c,temp[15]; + int check=0; + + joy.LCD_clear(); + sprintf(temp,"Late : %d",wakeUp[0]); + joy.LCD_write_string(0,0,temp,0); + sprintf(temp,"Not Late : %d",wakeUp[1]); + joy.LCD_write_string(0,1,temp,0); + sprintf(temp,"Set Alarm : %d",wakeUp[2]); + joy.LCD_write_string(0,2,temp,0); + + sprintf(temp,"reset move joy"); + joy.LCD_write_string(0,4,temp,0); + sprintf(temp,"down 2 times."); + joy.LCD_write_string(0,5,temp,0); + while(1){ + c=joy.LCD_joystick(); + if(check==1){ + joy.LCD_clear(); + joy.LCD_write_string(0,0,"data reset",0); + wait(2); + for(check=0;check<3;check++) wakeUp[check] = 0; + joy.LCD_clear(); + sprintf(temp,"Late : %d",wakeUp[0]); + joy.LCD_write_string(0,0,temp,0); + sprintf(temp,"Not Late : %d",wakeUp[1]); + joy.LCD_write_string(0,1,temp,0); + sprintf(temp,"Set Alarm : %d",wakeUp[2]); + joy.LCD_write_string(0,2,temp,0); + + sprintf(temp,"reset move joy"); + joy.LCD_write_string(0,4,temp,0); + sprintf(temp,"down 2 times."); + joy.LCD_write_string(0,5,temp,0); + } + check = 0; + if(c=='l') return -1; + if(c=='d') check++; + wait_ms(200); } } + +//modeF = bluetooth +int modeF(){ //bluetooth mode here + char input[6],temp[15]; + input[0] = pc.getc(); + input[1] = pc.getc(); + input[2] = pc.getc(); + input[3] = pc.getc(); + input[4] = pc.getc(); + input[5] = '\0'; + + switch(input[0]){ + case '0': + hourTime = (input[1]-'0')*10+(input[2]-'0'); + minTime = (input[3]-'0')*10+(input[4]-'0'); + break; + case '1': + isAlarm = 1; + wakeUp[2]++; + hourAlarm = (input[1]-'0')*10+(input[2]-'0'); + minAlarm = (input[3]-'0')*10+(input[4]-'0'); + break; + case '2': + level = input[1]-'0'; + break; + case '3': + sprintf(temp,"%02d%02d%02d",wakeUp[0],wakeUp[1],wakeUp[2]); + pc.printf(temp); + break; + default : //do some thing + } + +} + +// game +void print(int n,unsigned char x, unsigned char y){ + if(n!=0) joy.LCD_pixel(x,y); +} +void win(){ + joy.LCD_clear(); + joy.LCD_write_string(20,2,"WIN!",0); +} +void lose(){ + joy.LCD_clear(); + joy.LCD_write_string(20,2,"LOSE!",1); +} + +int snake_game(){ + wait(0.5); + int map[24][24],num = high*width; + + int k,p=0,i,j; + int pScore,lvl=level,walk,path=1,temp,now; + int score = 4,food=0; + char c; + int input[high][width]; + unsigned char output[num/8]; + + srand(time(NULL)); + pScore = (rand()%10)+7; //pass score here! + + for(i=0;i<high;i++){ + for(j=0;j<width;j++){ + if(j==0||i==0||i==high-1||j==width-1) map[i][j] = -1; + else map[i][j] = 0; + } + } + + map[high/2][width/3] = 1; + map[high/2][(width/3)+1] = 2; + map[high/2][(width/3)+2] = 3; + map[high/2][(width/3)+3] = 4; + now = ((high/2))*width + (width/3)+3; + while(map[food/width][food%width]!=0){ + food = rand()%num; //random food + } + + joy.LCD_clear(); + joy.LCD_write_string(10,1,"move joy to",0); + joy.LCD_write_string(10,2,">> START <<",0); + + while(1){ + wait_ms(200); + c=joy.LCD_joystick(); + if(c=='u'||c=='d'||c=='l'||c=='r') break; + } + wait_ms(200); + //start game! + joy.LCD_clear(); + while(1){ + //print map[][] to 0,1 + + for(i=0;i<high;i++){ + for(j=0;j<width;j++){ + if((i*width + j)==food) input[i][j] = 1; + else if(map[i][j]!=0) input[i][j] = 1; + else input[i][j] = 0; + } + } + + //change to new Map + + p = 0; + for(i=0;i<72;i++) output[i] = 0; + + for(i=0;i<3;i++){ + for(j=0;j<24;j++){ + for(k=7;k>=0;k--){ + output[p] = output[p]*2 + input[i*8+k][j]; + //printf("%d ",input[i*8+7-k][j]); + } + p++; + } + } + + joy.LCD_clear(); + joy.LCD_draw_bmp_pixel(0,0,output,high,width); + + wait_ms(200); + //find path + c=joy.LCD_joystick(); + switch (c){ + case 'l': temp=-1; break; + case 'r': temp=1; break; + case 'u': temp=-width; break; + case 'd': temp=width; break; + default : temp=path; + } + if(temp!=-path)path=temp; + + if(map[(now+path)/width][(now+path)%width]!=0) break; //eat !food + + now+=path; + temp = 1; + + if(now==food){ + score+=1; + temp = food; + while(map[food/width][food%width]!=0||food==temp){ + food = rand()%num; //random food + } + map[food/width][food%width] = 1; + temp=0; + } + map[now/width][now%width]=score+1; + for(i=0;i<num;i++) if(map[i/width][i%width]>0) map[i/width][i%width]-=temp; + + + wait_ms((maxLvl - lvl+1)*200); + } + + if((score*lvl)-4>=pScore){ + win(); + wait(2); + return 1; + } + + lose(); + wait(2); + + return 0; +} +int crossRoad_game(){ + wait(0.5); + int car[high][width],num = high*width; + unsigned char i,j; + int k,p; + int pScore,lvl=level,walk,path=0,temp,now; + int score = 4,isWin=0; + char c; + int input[high][width]; + unsigned char output[num/8]; + + now = ((high-2))*width + (width/2); + srand(time(NULL)); + pScore = (rand()%10)+7; //pass score here! + + for(i=0;i<high;i++){ + for(j=0;j<width;j++){ + if(i==high-1) car[i][j] = -1; + else if(i==0) car[i][j] = -2; + else car[i][j] = 0; + } + }//set no car + + for(i=1;i<high-2;i++){ + for(j=0;j<((width*((i%2)+1))/10);j++){ + temp = rand()%(width); + car[i][temp] = 1; + } + }//set car + + joy.LCD_clear(); + joy.LCD_write_string(10,1,"move joy to",0); + joy.LCD_write_string(10,2,">> START <<",0); + while(1){ + wait_ms(200); + c=joy.LCD_joystick(); + if(c=='u'||c=='d'||c=='l'||c=='r') break; + } + + //start game! + joy.LCD_clear(); + while(1){ + + //print map[][] to 0,1 + for(i=0;i<num;i++){ + if(i==now) input[i/width][i%width] = 1; + else if(car[i/width][i%width]!=0) input[i/width][i%width] = 1; + else input[i/width][i%width] = 0; + } + + //change to new Map + p = 0; + for(i=0;i<72;i++) output[i] = 0; + for(i=0;i<3;i++){ + for(j=0;j<24;j++){ + for(k=7;k>=0;k--){ + output[p] = output[p]*2 + input[i*8+k][j]; + printf("%d ",input[i*8+7-k][j]); + } + p++; + } + } + joy.LCD_clear(); + joy.LCD_draw_bmp_pixel(0,0,output,high,width); + + + //find path + c=joy.LCD_joystick(); + switch (c){ + case 'a': path=-1; break; + case 'd': path=1; break; + case 'w': path=-width; break; + case 's': path=width; break; + default : path=0; + } + + for(i=1;i<high-2;i++){ + if(i%2==0){ // <<< + temp = car[i][0]; + for(j=0;j<width-1;j++) car[i][j] = car[i][j+1]; + car[i][width-1] = temp; + }else{ // >>> + temp = car[i][width-1]; + for(j=width-1;j>0;j--) car[i][j] = car[i][j-1]; + car[i][0] = temp; + } + }//car walk + + if(path*path==1){ //L,R + if(now+path<0){ + if((now+width)/width < (now+width+path)/width) now -= width; + else if((now+width)/width>(now+width+path)/width) now += width; + }else{ + if((now)/width<(now+path)/width) now -= width; + else if((now)/width>(now+path)/width) now += width; + } + } + + now+=path; + + if(isWin==1) break; + if(car[now/width][now%width]==-2) isWin = 1; + else if(car[now/width][now%width]==-1) now-=path; + else if(car[now/width][now%width]==1) break; + + path = 0; + + + wait_ms((maxLvl - lvl+1)*200); + } + if(isWin){ + win(); + wait(2); + return 1; + } + + lose(); + wait(2); + + return 0; +} +int menu(int n, char *input[]){ + wait_ms(100); + int i; + int mode = 0; + char c; + while(1){ + joy.LCD_clear(); + for(i=0;i<n;i++){ + if(mode==i){ + joy.LCD_write_string(0,i,input[i],1); + }else{ + joy.LCD_write_string(0,i,input[i],0); + } + } + + + c=joy.LCD_joystick(); + if(c=='r') break; + switch(c){ + case 'u': if(mode!=0) mode--; + break; + case 'd': if(mode!=n-1) mode++; + break; + case 'l': return -1; + break; + } + wait_ms(200); + } + wait_ms(200); + return mode; +}