avr-game

view avrgame.c @ 2:363acf3e61d4

wha?
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 20 Mar 2015 21:03:38 +0200
parents 872e425f0e7f
children
line source
1 #include <string.h>
2 #include <avr/io.h>
3 #include <avr/interrupt.h>
4 #include <util/delay.h>
5 #include "dotmatrix.h"
7 /* hardware setup
8 * - Port B[1,5]: lcd signals (see dotmatrix.h)
9 * - B1: clk
10 * - B2: Din
11 * - B3: DC
12 * - B4: CE
13 * - B5: RST
14 * - Port C[0,3]: buttons
15 */
17 enum {
18 BN_LEFT,
19 BN_RIGHT,
20 BN_ROT,
21 BN_DROP
22 };
24 void redraw(void);
25 void draw_field(void);
26 void putpixel(int x, int y, int c);
27 /*int lcd_stream_write(char c, FILE *fp);*/
28 /*FILE stream_lcd = FDEV_SETUP_STREAM(lcd_stream_write, NULL, _FDEV_SETUP_WRITE);*/
30 #define FB_SIZE (DM_WIDTH * DM_HEIGHT / 8)
31 static unsigned char fb[FB_SIZE];
33 /* gameplay */
34 static int bx = DM_WIDTH / 2, by = DM_HEIGHT / 2, brot;
35 #define FIELD_WIDTH 10
36 #define FIELD_HEIGHT 20
37 static unsigned char field[FIELD_WIDTH][FIELD_HEIGHT];
39 int main(void)
40 {
41 /* stdout = stderr = &stream_lcd; */
42 DDRD = 0xf0; /* lower nibble input */
43 PORTD = 0xf0; /* disable 4 lower pull-ups */
45 /* enable pin change interrupts (PCINT2) */
46 PCICR = (1 << PCIE2);
47 /* unmask interrupts PCINT16-19 (first 4 bits) */
48 PCMSK2 = 0xf;
50 dm_init();
51 dm_test();
52 for(;;);
54 sei();
56 for(;;) {
57 redraw();
58 _delay_ms(1);
59 }
61 return 0;
62 }
64 void redraw(void)
65 {
66 memset(fb, 0, FB_SIZE);
67 draw_field();
69 dm_copy(0, 0, fb, FB_SIZE);
70 }
72 #define HSPANS 6
73 void draw_field(void)
74 {
75 int i, j;
76 unsigned char *fbptr = fb;
78 /* draw borders */
79 memset(fbptr, 0xff, HSPANS * 2); /* fill 2 full lines */
80 fbptr += HSPANS * 2;
81 fbptr[0] = 0x03; fbptr[HSPANS - 1] = 0xc0;
82 fbptr += HSPANS;
83 *fbptr++ = 0xfb;
84 for(i=0; i<HSPANS - 2; i++) {
85 *fbptr++ = 0xff;
86 }
87 *fbptr++ = 0xdf;
90 for(i=0; i<DM_HEIGHT - 4; i++) {
91 fbptr[0] = 0x0b; /* left border flipped(11010000) */
92 fbptr[HSPANS - 1] = 0xd0; /* right border flipped(00001011) */
93 fbptr += DM_WIDTH / 8;
94 }
95 return;
97 fbptr = fb;
98 for(i=0; i<FIELD_HEIGHT * 4; i++) {
99 int y = i / 4;
101 for(j=0; j<FIELD_WIDTH / 2; j++) {
102 int x = j * 2;
103 unsigned char span;
104 if(field[x][y]) span |= 0xf0;
105 if(field[x + 1][y]) span |= 0xf;
106 fbptr[j] = span;
107 }
108 fbptr += DM_WIDTH / 8;
109 }
110 }
112 void putpixel(int x, int y, int c)
113 {
114 unsigned char *pptr = fb + y * DM_WIDTH / 8 + x / 8;
115 int bit = x % 8;
116 *pptr = (*pptr & ~bit) | ((c & 1) << bit);
117 }
120 ISR(PCINT2_vect)
121 {
122 static unsigned char prev_st;
123 unsigned char st, diff;
125 st = PIND & 0xf;
126 diff = st ^ prev_st;
127 prev_st = st;
129 if(diff & (1 << BN_LEFT) & st) {
130 bx -= 1;
131 }
132 if(diff & (1 << BN_RIGHT) & st) {
133 bx += 1;
134 }
135 if(diff & (1 << BN_ROT) & st) {
136 brot = (brot + 1) % 4;
137 }
138 if(diff & (1 << BN_DROP) & st) {
139 by -= 1;
140 }
141 }
143 #if 0
144 int lcd_stream_write(char c, FILE *fp)
145 {
146 return 0;
147 }
148 #endif