avr-game

changeset 2:363acf3e61d4 tip

wha?
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 20 Mar 2015 21:03:38 +0200
parents 872e425f0e7f
children
files avrgame.c dotmatrix.c
diffstat 2 files changed, 80 insertions(+), 18 deletions(-) [+]
line diff
     1.1 --- a/avrgame.c	Tue Sep 09 06:49:28 2014 +0300
     1.2 +++ b/avrgame.c	Fri Mar 20 21:03:38 2015 +0200
     1.3 @@ -1,3 +1,4 @@
     1.4 +#include <string.h>
     1.5  #include <avr/io.h>
     1.6  #include <avr/interrupt.h>
     1.7  #include <util/delay.h>
     1.8 @@ -21,17 +22,25 @@
     1.9  };
    1.10  
    1.11  void redraw(void);
    1.12 +void draw_field(void);
    1.13 +void putpixel(int x, int y, int c);
    1.14  /*int lcd_stream_write(char c, FILE *fp);*/
    1.15  /*FILE stream_lcd = FDEV_SETUP_STREAM(lcd_stream_write, NULL, _FDEV_SETUP_WRITE);*/
    1.16  
    1.17 -static int bx, by, brot;
    1.18 -static unsigned char fb[DM_WIDTH * DM_HEIGHT / 8];
    1.19 +#define FB_SIZE		(DM_WIDTH * DM_HEIGHT / 8)
    1.20 +static unsigned char fb[FB_SIZE];
    1.21 +
    1.22 +/* gameplay */
    1.23 +static int bx = DM_WIDTH / 2, by = DM_HEIGHT / 2, brot;
    1.24 +#define FIELD_WIDTH	10
    1.25 +#define FIELD_HEIGHT 20
    1.26 +static unsigned char field[FIELD_WIDTH][FIELD_HEIGHT];
    1.27  
    1.28  int main(void)
    1.29  {
    1.30  	/* stdout = stderr = &stream_lcd; */
    1.31 -	DDRC = 0;	/* input */
    1.32 -	PORTC = 0;	/* disable pull-ups */
    1.33 +	DDRD = 0xf0;	/* lower nibble input */
    1.34 +	PORTD = 0xf0;	/* disable 4 lower pull-ups */
    1.35  
    1.36  	/* enable pin change interrupts (PCINT2) */
    1.37  	PCICR = (1 << PCIE2);
    1.38 @@ -39,11 +48,14 @@
    1.39  	PCMSK2 = 0xf;
    1.40  
    1.41  	dm_init();
    1.42 +	dm_test();
    1.43 +	for(;;);
    1.44  
    1.45  	sei();
    1.46  
    1.47  	for(;;) {
    1.48  		redraw();
    1.49 +		_delay_ms(1);
    1.50  	}
    1.51  
    1.52  	return 0;
    1.53 @@ -51,34 +63,86 @@
    1.54  
    1.55  void redraw(void)
    1.56  {
    1.57 +	memset(fb, 0, FB_SIZE);
    1.58 +	draw_field();
    1.59 +
    1.60 +	dm_copy(0, 0, fb, FB_SIZE);
    1.61  }
    1.62  
    1.63 +#define HSPANS	6
    1.64 +void draw_field(void)
    1.65 +{
    1.66 +	int i, j;
    1.67 +	unsigned char *fbptr = fb;
    1.68 +
    1.69 +	/* draw borders */
    1.70 +	memset(fbptr, 0xff, HSPANS * 2);	/* fill 2 full lines */
    1.71 +	fbptr += HSPANS * 2;
    1.72 +	fbptr[0] = 0x03; fbptr[HSPANS - 1] = 0xc0;
    1.73 +	fbptr += HSPANS;
    1.74 +	*fbptr++ = 0xfb;
    1.75 +	for(i=0; i<HSPANS - 2; i++) {
    1.76 +		*fbptr++ = 0xff;
    1.77 +	}
    1.78 +	*fbptr++ = 0xdf;
    1.79 +
    1.80 +
    1.81 +	for(i=0; i<DM_HEIGHT - 4; i++) {
    1.82 +		fbptr[0] = 0x0b;			/* left border flipped(11010000) */
    1.83 +		fbptr[HSPANS - 1] = 0xd0;	/* right border flipped(00001011) */
    1.84 +		fbptr += DM_WIDTH / 8;
    1.85 +	}
    1.86 +	return;
    1.87 +
    1.88 +	fbptr = fb;
    1.89 +	for(i=0; i<FIELD_HEIGHT * 4; i++) {
    1.90 +		int y = i / 4;
    1.91 +
    1.92 +		for(j=0; j<FIELD_WIDTH / 2; j++) {
    1.93 +			int x = j * 2;
    1.94 +			unsigned char span;
    1.95 +			if(field[x][y]) span |= 0xf0;
    1.96 +			if(field[x + 1][y]) span |= 0xf;
    1.97 +			fbptr[j] = span;
    1.98 +		}
    1.99 +		fbptr += DM_WIDTH / 8;
   1.100 +	}
   1.101 +}
   1.102 +
   1.103 +void putpixel(int x, int y, int c)
   1.104 +{
   1.105 +	unsigned char *pptr = fb + y * DM_WIDTH / 8 + x / 8;
   1.106 +	int bit = x % 8;
   1.107 +	*pptr = (*pptr & ~bit) | ((c & 1) << bit);
   1.108 +}
   1.109 +
   1.110 +
   1.111  ISR(PCINT2_vect)
   1.112  {
   1.113 -	static unsigned char prev_st = 0xff;
   1.114 -	unsigned char st = PINC;
   1.115 -	unsigned char diff = st ^ prev_st;
   1.116 +	static unsigned char prev_st;
   1.117 +	unsigned char st, diff;
   1.118  
   1.119 -	if((diff & (1 << BN_LEFT)) & ~st) {
   1.120 +	st = PIND & 0xf;
   1.121 +	diff = st ^ prev_st;
   1.122 +	prev_st = st;
   1.123 +
   1.124 +	if(diff & (1 << BN_LEFT) & st) {
   1.125  		bx -= 1;
   1.126  	}
   1.127 -	if((diff & (1 << BN_RIGHT)) & ~st) {
   1.128 +	if(diff & (1 << BN_RIGHT) & st) {
   1.129  		bx += 1;
   1.130  	}
   1.131 -	if((diff & (1 << BN_ROT)) & ~st) {
   1.132 +	if(diff & (1 << BN_ROT) & st) {
   1.133  		brot = (brot + 1) % 4;
   1.134  	}
   1.135 -	if((diff & (1 << BN_DROP)) & ~st) {
   1.136 -		/* TODO */
   1.137 +	if(diff & (1 << BN_DROP) & st) {
   1.138 +		by -= 1;
   1.139  	}
   1.140 -
   1.141 -	prev_st = st;
   1.142  }
   1.143  
   1.144  #if 0
   1.145  int lcd_stream_write(char c, FILE *fp)
   1.146  {
   1.147 -	/* TODO terminal shit handling */
   1.148  	return 0;
   1.149  }
   1.150  #endif
     2.1 --- a/dotmatrix.c	Tue Sep 09 06:49:28 2014 +0300
     2.2 +++ b/dotmatrix.c	Fri Mar 20 21:03:38 2015 +0200
     2.3 @@ -44,8 +44,6 @@
     2.4  
     2.5  int dm_init(void)
     2.6  {
     2.7 -	int vop;
     2.8 -
     2.9  	DM_DDR |= PINMASK;	/* set used pins as outputs */
    2.10  	SET(DM_PORT, DM_PIN_CE);
    2.11  	SET(DM_PORT, DM_PIN_RST);
    2.12 @@ -58,7 +56,7 @@
    2.13  	/* set Vop (???) */
    2.14  	start_send();
    2.15  	send(CMD, CMD_FUNCSET(FSET_EXTENDED));
    2.16 -	send(CMD, CMD_EXT_VOP(80));
    2.17 +	send(CMD, CMD_EXT_VOP(78));
    2.18  
    2.19  	/* select function set (vertical addressing, basic set) */
    2.20  	send(CMD, CMD_FUNCSET(FSET_VERTICAL | FSET_BASIC));