Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /******************************************************************************/
- /* BLINKY.C: LED Flasher */
- /******************************************************************************/
- // Program zmodyfikowany do potrzeb laboratorium IISI Systemy Wbudowane
- /* This file is part of the uVision/ARM development tools. */
- /* Copyright (c) 2005-2006 Keil Software. All rights reserved. */
- /* This software may only be used under the terms of a valid, current, */
- /* end user licence from KEIL for a compatible version of KEIL software */
- /* development tools. Nothing else gives you the right to use this software. */
- /******************************************************************************/
- #define uint16_t unsigned int;
- #include <AT91SAM7X256.H> /* AT91SAMT7X256 definitions */
- #include "lcd.h"
- #include <string.h>
- #include <math.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include "adc.h"
- // deklaracje
- extern void InitSpi(void);
- extern void InitLcd(void);
- extern void LCDClearScreen(void);
- extern void WriteSpiCommand(unsigned int data);
- extern void WriteSpiData(unsigned int data);
- extern void LCDClearScreen(void);
- extern void LCDSetXY(int x, int y);
- extern void LCDSetPixel(int x, int y, int color);
- extern void LCDSetLine(int x1, int y1, int x2, int y2, int color);
- extern void LCDSetRect(int x0, int y0, int x1, int y1, unsigned char fill, int color);
- extern void LCDSetCircle(int x0, int y0, int radius, int color);
- extern void LCDPutChar(char c, int x, int y, int size, int fcolor, int bcolor);
- extern void LCDPutString (char *lcd_string, const char *font_style, unsigned char x, unsigned char y,
- unsigned char fcolor, unsigned char bcolor);
- extern void LCDPutStr(char *pString, int x, int y, int Size, int fColor, int bColor);
- extern void Delay (unsigned long a);
- /********************** Makra i zmienne globalne **************************/
- // ARM7X256 Leds Definition
- //maska dla pinow do ktorych podlaczone sa diody, którymi bedziemy sterowac
- #define LEDS_MASK (AT91C_PIO_PB20)
- //konfiguracja wybranych linii PIO
- //linie pracuja jako GPIO sterowane przez uzutkownika (a nie przez peryferia) = PIO_PER
- //ustawienie pinow jako wyjscia = PIO_OER
- //odblokowanie mozliwosc zapisu za pomoca rejestru ODSR = PIO_OWER
- #define LED_INIT {AT91C_BASE_PIOB->PIO_PER = LEDS_MASK; AT91C_BASE_PIOB->PIO_OER = LEDS_MASK; AT91C_BASE_PIOB->PIO_OWER = LEDS_MASK;}
- //definicja ustawiania stanu niskiego wyjscia PB20 = zgaszenie podswietlenia LED wyswietlacza LCD
- #define LED_BCK_OFF AT91C_BASE_PIOB->PIO_CODR = AT91C_PIO_PB20
- //definicja ustawiania stanu wysokiego wyjscia PB20 = zalaczenie podswietlenia LED wyswietlacza LCD
- #define LED_BCK_ON AT91C_BASE_PIOB->PIO_SODR = AT91C_PIO_PB20
- #define LEDS_OFF AT91C_BASE_PIOB->PIO_CODR = LEDS_MASK
- //definicja ustawiania stanu wysokiego wyjscia PB20 = zalaczenie podswietlenia LED wyswietlacza LCD
- #define LEDS_ON AT91C_BASE_PIOB->PIO_SODR = LEDS_MASK
- //Inputs
- #define LEFT_KEY_DOWN (((AT91C_BASE_PIOB->PIO_PDSR) & AT91C_PIO_PB24)==0)
- #define RIGHT_KEY_DOWN (((AT91C_BASE_PIOB->PIO_PDSR) & AT91C_PIO_PB25)==0)
- #define LEFT_STICK (((AT91C_BASE_PIOA->PIO_PDSR) & AT91C_PIO_PB7)==0)
- #define INPUTS_INIT {AT91C_BASE_PIOB->PIO_PER = (AT91C_PIO_PB24|AT91C_PIO_PB25); AT91C_BASE_PIOB->PIO_ODR = (AT91C_PIO_PB24|AT91C_PIO_PB25); }
- /**************************GRA***************************/
- #define JOYSTICK_LEFT ( ( (AT91C_BASE_PIOA->PIO_PDSR) & AT91C_PIO_PA7 ) == 0 )
- #define JOYSTICK_DOWN ( ( (AT91C_BASE_PIOA->PIO_PDSR) & AT91C_PIO_PA8 ) == 0 )
- #define JOYSTICK_UP ( ( (AT91C_BASE_PIOA->PIO_PDSR) & AT91C_PIO_PA9 ) == 0 )
- #define JOYSTICK_RIGHT ( ( (AT91C_BASE_PIOA->PIO_PDSR) & AT91C_PIO_PA14 ) == 0 )
- #define JOYSTICK_HIT ( ( (AT91C_BASE_PIOA->PIO_PDSR) & AT91C_PIO_PA23 ) == 0 )
- const unsigned char BACKGROUND_COLOR = CYAN ;
- void showStatusBar(int);
- enum
- {
- REDRAW=1,
- DO_NOT_REDRAW=2
- };
- enum
- {
- X = 1,
- Y = 0
- } ;
- void delay(unsigned int time)
- {
- unsigned int i = 0;
- time *= 1000 ;
- for( i=0; i<time; ++i ) {}
- }
- /****** GRA ******/
- //Kulka
- typedef struct Kulka
- {
- int kX ; // = 0;
- int kY ; // = 0;
- int kRadius ; // = 3 ;
- unsigned char kColor ; // = BLACK ;
- int kRuch;
- } Kulka ;
- Kulka k = {130/2, 130/2, 3, BLACK};
- //Gracz/deska
- typedef struct Gracz
- {
- int X;
- int pkt;
- } Gracz;
- Gracz p = {130/2, 0};
- static const int ROZMIAR_DESKI;
- //Klocek
- typedef struct Brick
- {
- int X;
- int Y;
- int stan;
- } Brick;
- Brick b1 = {30,30,1};
- Brick b2 = {60,30,1};
- Brick b3 = {90,30,1};
- // stan menu
- int isMenuOpen = 0;
- // czas rozpoczecia gry
- unsigned int startTime = 0 ;
- unsigned int overallPauseTime = 0;
- unsigned int singlePauseTime = 0;
- // szybkosc gry
- unsigned int gameSpeed = 550 ;
- /****** INTERFEJS ******/
- // rozmiary ekranu
- static const int SCREEN_X_BEG = 0 ;
- static const int SCREEN_X_END = 131 ;
- static const int SCREEN_Y_BEG = 0 ;
- static const int SCREEN_Y_END = 131 ;
- //rozmiary menu
- static const int X_BEG = 121 ;
- static const int Y_BEG = 10 ;
- static const int X_END = 10 ;
- static const int Y_END = 121 ;
- //czcionka
- static const int SFONT_SIZE[2] = {6,8} ; // small font size (y,x)
- static const int MFONT_SIZE[2] = {8,8} ; // medium font size (y,x)
- static const int LFONT_SIZE[2] = {8,16} ; // large font size (y,x)
- //pasek stanu
- static const int STATUS_BAR_HEIGHT = 15 ;
- void showGameTime_Pos(int x, int y)
- {
- char liczba[10] ;
- unsigned int time = ((*AT91C_RTTC_RTVR) - startTime) - overallPauseTime;
- unsigned int min = time/60 ;
- unsigned int sec = time%60 ;
- if( (min<10) && (sec<10) )
- sprintf( liczba, "0%d:0%d", min, sec ) ;
- else if( (min<10) )
- sprintf( liczba, "0%d:%d", min, sec ) ;
- else if( (sec<10) )
- sprintf( liczba, "%d:0%d", min, sec ) ;
- else
- sprintf( liczba, "%d:%d", min, sec ) ;
- LCDPutStr( liczba, x , y, MEDIUM, ORANGE, BLACK ) ;
- }
- void showCredits(void)
- {
- const unsigned int X_END_WIN = X_END+30 ; // 40
- const unsigned int X_BEG_WIN = X_BEG-30 ; // 91
- const unsigned int Y_END_WIN = Y_END-2 ; // 119
- const unsigned int Y_BEG_WIN = Y_BEG+2 ; // 12
- unsigned int i ;
- int offsetX ;
- int offsetY ;
- int len;
- char* authors[2] = {"T. Wreczycki"} ;
- LCDSetRect( X_END_WIN , Y_BEG_WIN , X_BEG_WIN , Y_END_WIN , FILL, BLACK ) ;
- for(i = 0; i < 2; ++i)
- {
- len = strlen(authors[i]) ;
- offsetY = len * MFONT_SIZE[ Y ] ;
- offsetY = Y_BEG_WIN + ((Y_END_WIN-Y_BEG_WIN)-offsetY)/2 ;
- offsetX = X_END_WIN + 10 + i * MFONT_SIZE[ X ] + i*10 ;
- LCDPutStr( authors[i], offsetX , offsetY, MEDIUM, ORANGE, BLACK ) ;
- }
- //delay( 10000 ) ;
- LCDSetRect( X_END_WIN , Y_BEG_WIN , X_BEG_WIN , Y_END_WIN, FILL, RED ) ;
- }
- void newGame (void)
- {
- p.X = 130/2;
- p.pkt = 0 ;
- k.kX = 0;
- k.kY = 0;
- b1.stan=1;
- b2.stan=1;
- b3.stan=1;
- gameSpeed = 550 ;
- startTime = *AT91C_RTTC_RTVR ;
- overallPauseTime = 0;
- singlePauseTime = 0;
- srand( startTime ) ;
- // odrysuj ekran z tlem
- showStatusBar(REDRAW);
- LCDSetRect( SCREEN_X_BEG + STATUS_BAR_HEIGHT, SCREEN_Y_BEG , SCREEN_X_END , SCREEN_Y_END , FILL , BACKGROUND_COLOR );
- }
- void showPoints (void)
- {
- const unsigned int X_END_WIN = X_END+30 ; // 40
- const unsigned int X_BEG_WIN = X_BEG-30 ; // 91
- const unsigned int Y_END_WIN = Y_END-30 ; // 91
- const unsigned int Y_BEG_WIN = Y_BEG+30 ; // 40
- char liczba[10] ;
- int offsetX ;
- int offsetY ;
- int len ;
- LCDSetRect( X_END_WIN , Y_BEG_WIN , X_BEG_WIN , Y_END_WIN , FILL, BLACK ) ;
- sprintf( liczba, "%d", p.pkt ) ;
- len = strlen(liczba) ;
- offsetY = len * MFONT_SIZE[ Y ] ;
- offsetY = Y_BEG_WIN + ((Y_END_WIN-Y_BEG_WIN)-offsetY)/2 ;
- offsetX = MFONT_SIZE[ X ] ;
- offsetX = X_BEG_WIN + ((X_END_WIN-X_BEG_WIN)-offsetX)/2 ;
- LCDPutStr( liczba, offsetX , offsetY, MEDIUM, ORANGE, BLACK ) ;
- delay( 10000 ) ;
- LCDSetRect( X_END_WIN , Y_BEG_WIN , X_BEG_WIN , Y_END_WIN, FILL, RED ) ;
- }
- void showPoints_Pos(int x, int yEnd)
- {
- char liczba[5];
- unsigned int yBeg;
- unsigned int len;
- sprintf(liczba, "%d", p.pkt);
- len = strlen(liczba);
- yBeg = yEnd - (len * MFONT_SIZE[ Y ]);
- LCDPutStr( liczba, x , yBeg, MEDIUM, ORANGE, BLACK ) ;
- }
- void menu(void)
- {
- static const char * title = "Arkanoid" ;
- static const int MENU_ENTRIES = 3 ;
- static const char * menuEntry[MENU_ENTRIES] = { "AUTORZY", "ILOSC PUNKTOW", "NOWA GRA" } ;
- int len ;
- int offsetX ;
- int offsetY ;
- int currentMenuEntryPos ;
- unsigned int i ;
- unsigned char backgroundColor ;
- singlePauseTime = *AT91C_RTTC_RTVR ;
- // tlo menu
- LCDSetRect ( X_BEG , Y_BEG , X_END , Y_END , FILL , RED );
- len = strlen(title) ;
- offsetY = len * LFONT_SIZE[ Y ] ;
- offsetY = Y_BEG + ((Y_END-Y_BEG)-offsetY)/2 ;
- LCDPutStr( (char*)title, X_END, offsetY, LARGE, ORANGE, BLACK);
- currentMenuEntryPos = 0;
- while(1)
- {
- for( i = 0; i < MENU_ENTRIES; ++i )
- {
- if( i == currentMenuEntryPos )
- backgroundColor = GREEN ;
- else
- backgroundColor = BLACK ;
- len = strlen(menuEntry[i]) ;
- offsetY = len * MFONT_SIZE[ Y ] ;
- offsetY = Y_BEG + ((Y_END-Y_BEG)-offsetY)/2 ;
- offsetX = (X_END + 40) + i*10 ;
- LCDPutStr( (char*)menuEntry[i], offsetX, offsetY, MEDIUM, ORANGE, backgroundColor);
- }
- //delay(200);
- if ( LEFT_KEY_DOWN )
- {
- isMenuOpen = ~isMenuOpen;
- while( LEFT_KEY_DOWN ) {}
- break ;
- } // ?? delay(200);
- else if ( JOYSTICK_UP )
- {
- --currentMenuEntryPos ;
- if( currentMenuEntryPos < 0 )
- currentMenuEntryPos = 0 ;
- while( JOYSTICK_UP ) {}
- }
- else if ( JOYSTICK_DOWN )
- {
- ++currentMenuEntryPos ;
- if( currentMenuEntryPos >= MENU_ENTRIES )
- currentMenuEntryPos = MENU_ENTRIES-1 ;
- while( JOYSTICK_DOWN ) {}
- }
- else if ( JOYSTICK_RIGHT )
- {
- switch( currentMenuEntryPos )
- {
- case 0:
- showCredits() ;
- break ;
- case 1:
- showPoints() ;
- isMenuOpen = ~isMenuOpen;
- while( JOYSTICK_RIGHT ) {}
- break ;
- case 2:
- newGame() ;
- isMenuOpen = ~isMenuOpen;
- while( JOYSTICK_RIGHT ) {}
- return ;
- default:
- break ;
- }
- }
- }
- singlePauseTime = *AT91C_RTTC_RTVR - singlePauseTime;
- overallPauseTime += singlePauseTime;
- }
- int changeSpeed(void)
- {
- int trim_val = GetAdcChanel(ADC_CHN_7);
- trim_val=(trim_val >> 2);
- return trim_val;
- }
- void defeat(void)
- {
- int len ;
- unsigned int offsetY ;
- unsigned int offsetX ;
- const unsigned int NOTIFICATIONS_NUMBER = 2 ;
- const char* notification[NOTIFICATIONS_NUMBER] = {"PORAZKA!", "SPROBOJ PONOWNIE"} ;
- char gamePointsInfo [18] ;
- len = strlen( notification[0] ) ;
- offsetY = len * LFONT_SIZE[ Y ] ;
- offsetY = Y_BEG + ((Y_END-Y_BEG)-offsetY)/2 ;
- // sprawdzic pozniszy fragment - te wyliczenia moga byc niepewne
- offsetX = LFONT_SIZE[ X ] ;
- offsetX = X_END + ((X_BEG-X_END)-offsetX)/2 ;
- LCDPutStr( (char*)notification[0], (offsetX-8) , offsetY, LARGE, RED, BLACK);
- len = strlen( notification[1] ) ;
- offsetY = len * MFONT_SIZE[ Y ] ;
- offsetY = Y_BEG + ((Y_END-Y_BEG)-offsetY)/2 ;
- // sprawdzic pozniszy fragment - te wyliczenia moga byc niepewne
- offsetX = LFONT_SIZE[ X ] ;
- offsetX = X_END + ((X_BEG-X_END)-offsetX)/2 ;
- LCDPutStr( (char*)notification[1], (offsetX +12) , offsetY, MEDIUM, RED, BLACK);
- sprintf( gamePointsInfo , "Punkty: %d" , p.pkt ) ;
- len = strlen( gamePointsInfo ) ;
- offsetY = len * LFONT_SIZE[ Y ] ;
- offsetY = Y_BEG + ((Y_END-Y_BEG)-offsetY)/2 ;
- // sprawdzic pozniszy fragment - te wyliczenia moga byc niepewne
- offsetX = LFONT_SIZE[ X ] ;
- offsetX = X_END + ((X_BEG-X_END)-offsetX)/2 ;
- LCDPutStr( gamePointsInfo , (offsetX + 24) , offsetY , LARGE , RED , BLACK ) ;
- delay(50000) ;
- newGame() ;
- }
- void showStatusBar( int redraw )
- {
- if(redraw == REDRAW)
- LCDSetRect( SCREEN_X_BEG , SCREEN_Y_BEG , STATUS_BAR_HEIGHT , SCREEN_Y_END , FILL , BLACK );
- showGameTime_Pos(2,2);
- showPoints_Pos(2,129);
- }
- void displayDeska(void)
- {
- LCDSetRect ( p.X-10, p.X+10, 0, 5, FILL, BLACK);
- }
- void eraseDeska(void)
- {
- LCDSetRect ( p.X-10, p.X+10, 0, 5, FILL, BACKGROUND_COLOR);
- }
- void lgKulka(void)
- {
- k.kX -= 5;
- k.kY += 5;
- k.kRuch = 1;
- }
- void pgKulka(void)
- {
- k.kX += 5;
- k.kY += 5;
- k.kRuch = 2;
- }
- void ldKulka(void)
- {
- k.kX -= 5;
- k.kY -= 5;
- k.kRuch = 3;
- }
- void pdKulka(void)
- {
- k.kX += 5;
- k.kY -= 5;
- k.kRuch = 4;
- }
- void moveKulka(void)
- {
- static const int LEFT_EDGE = SCREEN_Y_BEG ;
- static const int RIGHT_EDGE = SCREEN_Y_END -1 ;
- static const int BOTTOM_EDGE = SCREEN_X_END -1 -5 ;
- static const int UPPER_EDGE = STATUS_BAR_HEIGHT ;
- LCDSetCircle( k.kX , k.kY , k.kRadius, BACKGROUND_COLOR);
- if (k.kX == LEFT_EDGE)
- {
- if (k.kRuch == 1)
- {
- pgKulka();
- }
- else if (k.kRuch == 3)
- {
- pdKulka();
- }
- }
- else if (k.kX == RIGHT_EDGE)
- {
- if (k.kRuch == 2)
- {
- lgKulka();
- }
- else if (k.kRuch == 4)
- {
- ldKulka();
- }
- }
- //brick 1
- else if (k.kY >= b1.Y - 10 && k.kY <= b1.Y + 10 && k.kX >= b1.X - 10 && k.kX <= b1.X + 10)
- {
- if (k.kRuch == 1)
- {
- ldKulka();
- }
- else if (k.kRuch == 2)
- {
- pdKulka();
- }
- b1.stan=0;
- }
- //brick 2
- else if (k.kY >= b2.Y - 10 && k.kY <= b2.Y + 10 && k.kX >= b2.X - 10 && k.kX <= b2.X + 10)
- {
- if (k.kRuch == 1)
- {
- ldKulka();
- }
- else if (k.kRuch == 2)
- {
- pdKulka();
- }
- b2.stan=0;
- }
- //brick 3
- else if (k.kY >= b3.Y - 10 && k.kY <= b3.Y + 10 && k.kX >= b3.X - 10 && k.kX <= b3.X + 10)
- {
- if (k.kRuch == 1)
- {
- ldKulka();
- }
- else if (k.kRuch == 2)
- {
- pdKulka();
- }
- b3.stan=0;
- }
- else if (k.kY == UPPER_EDGE)
- {
- if (k.kRuch == 1)
- {
- ldKulka();
- }
- else if (k.kRuch == 2)
- {
- pdKulka();
- }
- }
- else if (k.kY == BOTTOM_EDGE)
- {
- if (k.kX >= p.X - 10 && k.kX <= p.X +10)
- {
- if (k.kRuch == 3)
- {
- lgKulka();
- }
- else if (k.kRuch == 4)
- {
- pgKulka();
- }
- }
- else defeat();
- }
- else if (k.kRuch == 1)
- {
- lgKulka();
- }
- else if (k.kRuch == 2)
- {
- pgKulka();
- }
- else if (k.kRuch == 2)
- {
- ldKulka();
- }
- else if (k.kRuch == 2)
- {
- pdKulka();
- }
- LCDSetCircle( k.kX , k.kY , k.kRadius, k.kColor);
- }
- void drawBrick(void)
- {
- if (b1.stan == 1)
- {
- LCDSetRect( b1.X -10 , b1.X +10 , b1.Y -10 , b1.Y +10 , FILL , BLACK );
- }
- else
- {
- LCDSetRect( b1.X -10 , b1.X +10 , b1.Y -10 , b1.Y +10 , FILL , BACKGROUND_COLOR );
- }
- if (b2.stan == 1)
- {
- LCDSetRect( b2.X -10 , b2.X +10 , b2.Y -10 , b2.Y +10 , FILL , BLACK );
- }
- else
- {
- LCDSetRect( b2.X -10 , b2.X +10 , b2.Y -10 , b2.Y +10 , FILL , BACKGROUND_COLOR );
- }
- if (b3.stan ==1)
- {
- LCDSetRect( b3.X -10 , b3.X +10 , b3.Y -10 , b3.Y +10 , FILL , BLACK );
- }
- else
- {
- LCDSetRect( b3.X -10 , b3.X +10 , b3.Y -10 , b3.Y +10 , FILL , BACKGROUND_COLOR );
- }
- }
- /*********************************************************/
- //
- // Main Program
- //
- int main (void)
- {
- unsigned i;
- // Enable the Clock of the PIO
- AT91C_BASE_PMC->PMC_PCER = (1 << AT91C_ID_PIOB) | (1 << AT91C_ID_PIOA);
- LED_INIT;
- InitADC();
- AT91C_BASE_PIOB->PIO_ODR = (AT91C_PIO_PB24|AT91C_PIO_PB25); //keys SW1 i SW2 - jako wejscia
- // ustawienie prescaler'a na odmierzanie 1 sekundy
- *AT91C_RTTC_RTMR |= 0x8000 ;
- // inicjalizacja funkcji losujacej
- //srand( seed ) ;
- // Initialize SPI interface to LCD
- InitSpi();
- // Init LCD
- InitLcd();
- // Voltage control (contrast setting)
- WriteSpiCommand(VOLCTR);
- WriteSpiData(-20); // P1 = 32 volume value (experiment with this value to get the best contrast)
- WriteSpiData(3); // P2 = 3 resistance ratio (only value that works)
- // clear the screen
- LCDClearScreen();
- // rozpoczecie nowej gry
- newGame();
- while(1)
- {
- if ( JOYSTICK_LEFT )
- {
- if( p.X >= 0 )
- {
- eraseDeska();
- p.X -= 5 ;
- }
- }
- else if ( JOYSTICK_RIGHT )
- {
- if( p.X <= 130 )
- {
- eraseDeska();
- p.X += 5 ;
- }
- }
- else if ( LEFT_KEY_DOWN )
- {
- //delay(200);
- isMenuOpen = ~isMenuOpen;
- //delay(200);
- while( LEFT_KEY_DOWN ) {}
- }
- if ( isMenuOpen )
- {
- menu();
- showStatusBar(REDRAW);
- LCDSetRect( SCREEN_X_BEG + STATUS_BAR_HEIGHT, SCREEN_Y_BEG , SCREEN_X_END , SCREEN_Y_END , FILL , BACKGROUND_COLOR );
- }
- drawBrick();
- displayDeska();
- moveKulka();
- showStatusBar(DO_NOT_REDRAW);
- delay(gameSpeed - changeSpeed() );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement