Example #1
0
u32 btn_wait()
{
	u32 res = 0, btn = btn_read();
	int pwr = 0;

	// Power button down, raise a filter.
	if (btn & BTN_POWER)
	{
		pwr = 1;
		btn &= 0xFFFFFFFE;
	}

	do
	{
		res = btn_read();
		// Power button up, remove filter.
		if (!(res & BTN_POWER) && pwr)
			pwr = 0;
		// Power button still down.
		else if (pwr)
			res &= 0xFFFFFFFE;
	} while (btn == res);

	return res;
}
Example #2
0
u32 btn_wait_timeout(u32 time_ms)
{
	u32 timeout = get_tmr() + (time_ms * 1000);
	u32 res = btn_read();
	u32 btn = res;

	do
	{
		//Keep the new value until timeout is reached
		if (btn == res)
			res = btn_read();
	} while (get_tmr() < timeout);

	return res;
}
Example #3
0
File: ui.c Project: evegard/tdt4258
void ui_state_title(void)
{
    int x = 0, y = 0, dx = 1, dy = 1, w = 20, h = 20, i = 0;

    snd_play("sounds/intro-valkyrie.raw");

    while (1) {
        image_draw(0, 0, img_bg);
        image_draw(x, y, img_soldier);
        screen_show_buffer();

        if (y + h == SCREEN_HEIGHT && dy == 1) dy = -1;
        if (y == 0 && dy == -1) dy = 1;
        if (x + w == SCREEN_WIDTH && dx == 1) dx = -1;
        if (x == 0 && dx == -1) dx = 1;
        x += dx; y += dy;

        led_setup = (i++ / 10) % 2 ? 0xaa : 0x55;
        led_update();

        if ((btn_ignore = btn_read()) > 0) {
            ui_state = UI_GAME_SOLDIER;
            game_init();
            return;
        }
    }
}
Example #4
0
File: ui.c Project: evegard/tdt4258
void ui_state_scoreboard(void)
{
    volatile int i;
    int x, y, dx, dy, w, h;
    image_t *bg, *sprite;

    ui_state = UI_TITLE;

    if (game_soldier_score == UI_MAX_POINTS) {
        bg = img_soldier_win;
        sprite = img_tank;
    } else {
        bg = img_tank_win;
        sprite = img_soldier;
    }

    w = sprite->width;
    h = sprite->height;

    snd_play_wait("sounds/point.raw");
    image_draw(0, 0, bg);

    x = rand() % 9 + 1; y = 0;
    dx = rand() % 3 + 1; dy = 1;

    while (1) {
        image_draw(x, y, sprite);
        screen_show_buffer();

        if (y + h == SCREEN_HEIGHT && dy >= 0) dy *= -0.8;
        if (x + w == SCREEN_WIDTH || x == 0) {
            x = rand() % 9 + 1; y = 0;
            dx = rand() % 3 + 1; dy = 1;
            if (rand() % 2) {
                x = SCREEN_WIDTH - w - x;
                dx *= -1;
            }
        }
        x += dx; x = MIN(x, SCREEN_WIDTH - w); x = MAX(x, 0);
        y += dy; y = MIN(y, SCREEN_HEIGHT - h);
        dy += 1;

        for (i = 0; i < 1000000; i++);

        if ((btn_ignore = btn_read()) > 0)
            return;
    }
}
Example #5
0
int main(int argc, char *argv[]) {

	uint8_t ctr = 0;
	GPAD_ST gpads[GPADSNUM];
	BTN_DEV_ST button;
	int16_t clockpin;
	int16_t strobepin;
	int16_t data1pin;
	int16_t data2pin;
	int16_t clockpin_v2;
	int16_t strobepin_v2;
	int16_t data1pin_v2;
	int16_t data2pin_v2;

	if (!bcm2835_init())
		return 1;
	
	if (readConfigurationfile() != 0 ) {
		return 1;
	}

	if (strcmp(confres.adapter_version,"1x")==0) {
		clockpin = RPI_GPIO_P1_19;
		strobepin = RPI_GPIO_P1_23;
		data1pin = RPI_GPIO_P1_05;
		data2pin = RPI_GPIO_P1_07;
		clockpin_v2 = RPI_V2_GPIO_P1_19;
		strobepin_v2 = RPI_V2_GPIO_P1_23;
		data1pin_v2 = RPI_V2_GPIO_P1_05;
		data2pin_v2 = RPI_V2_GPIO_P1_07;
		printf("[SNESDev-Rpi] Using pin setup for RetroPie GPIO-Adapter Version 1.X\n");

	} else if (strcmp(confres.adapter_version,"2x")==0) {
		clockpin = RPI_GPIO_P1_16;
		strobepin = RPI_GPIO_P1_18;
		data1pin = RPI_GPIO_P1_15;
		data2pin = RPI_GPIO_P1_13;
		clockpin_v2 = RPI_V2_GPIO_P1_16;
		strobepin_v2 = RPI_V2_GPIO_P1_18;
		data1pin_v2 = RPI_V2_GPIO_P1_15;
		data2pin_v2 = RPI_V2_GPIO_P1_13;
		printf("[SNESDev-Rpi] Using pin setup for RetroPie GPIO-Adapter Version 2.X\n");

	} else {
		clockpin = RPI_GPIO_P1_16;
		strobepin = RPI_GPIO_P1_18;
		data1pin = RPI_GPIO_P1_15;
		data2pin = RPI_GPIO_P1_13;
		clockpin_v2 = RPI_V2_GPIO_P1_16;
		strobepin_v2 = RPI_V2_GPIO_P1_18;
		data1pin_v2 = RPI_V2_GPIO_P1_15;
		data2pin_v2 = RPI_V2_GPIO_P1_13;
		printf("[SNESDev-Rpi] Using pin setup for RetroPie GPIO-Adapter Version 2.X\n");
	}


	if (confres.gamepad1_enabled || confres.gamepad2_enabled) {

		gpads[0].port = 1;
		gpads[1].port = 1;
		// check board revision and set pins to be used
		// these are acutally also used by the gamecon driver
		if (get_rpi_revision()==1)
		{
			gpads[0].pin_clock = clockpin;
			gpads[0].pin_strobe = strobepin;
			gpads[0].pin_data = data1pin;
			gpads[1].pin_clock = clockpin;
			gpads[1].pin_strobe = strobepin;
			gpads[1].pin_data = data2pin;		
		} else {
			gpads[0].pin_clock = clockpin_v2;
			gpads[0].pin_strobe = strobepin_v2;
			gpads[0].pin_data = data1pin_v2;
			gpads[1].pin_clock = clockpin_v2;
			gpads[1].pin_strobe = strobepin_v2;
			gpads[1].pin_data = data2pin_v2;		
		}
		if (strcmp(confres.gamepad1_type,"nes")==0) {
			gpads[0].type = GPAD_TYPE_NES;
		} else {
			gpads[0].type = GPAD_TYPE_SNES;
		}
		if (strcmp(confres.gamepad2_type,"nes")==0) {
			gpads[1].type = GPAD_TYPE_NES;
		} else {
			gpads[1].type = GPAD_TYPE_SNES;
		}

		if (confres.gamepad1_enabled) {
			printf("[SNESDev-Rpi] Enabling game pad 1 with type '%s'.\n", confres.gamepad1_type);
			gpad_open(&gpads[0]);
			switch (gpads->type) {
			case GPAD_TYPE_SNES:
				uinput_gpad_open(&uinp_gpads[0], UINPUT_GPAD_TYPE_SNES);
				break;
			case GPAD_TYPE_NES:
				uinput_gpad_open(&uinp_gpads[0], UINPUT_GPAD_TYPE_NES);
				break;
			default:
				break;
			}
		}
		if (confres.gamepad2_enabled) {
			printf("[SNESDev-Rpi] Enabling game pad 2 with type '%s'.\n", confres.gamepad2_type);
			gpad_open(&gpads[1]);
			switch (gpads->type) {
			case GPAD_TYPE_SNES:
				uinput_gpad_open(&uinp_gpads[1], UINPUT_GPAD_TYPE_SNES);
				break;
			case GPAD_TYPE_NES:
				uinput_gpad_open(&uinp_gpads[1], UINPUT_GPAD_TYPE_NES);
				break;
			default:
				break;
			}
		}
	}

	if (confres.button_enabled) {
		printf("[SNESDev-Rpi] Enabling button.\n");
		button.port = 1;
		if (get_rpi_revision()==1) {
			button.pin = BUTTONPIN;
		} else {
			button.pin = BUTTONPIN_V2;
		}		
		btn_open(&button);
		uinput_kbd_open(&uinp_kbd);
	}

	register_signalhandlers();

	///* enter the main loop */
	doRun = 1;
	while (doRun) {
		if (confres.gamepad1_enabled || confres.gamepad2_enabled) {						
			/* read states of the buttons */
			for (ctr = 0; ctr < GPADSNUM; ctr++) {
				gpad_read(&gpads[ctr]);
			}

			///* send an event (pressed or released) for each button */
			for (ctr = 0; ctr < GPADSNUM; ctr++) {
				if ((ctr==0 && !(confres.gamepad1_enabled)) || 
					(ctr==1 && !(confres.gamepad2_enabled))) {
					continue;
				}

				gpad_read(&gpads[ctr]);
				processPadBtn(gpads[ctr].state, EV_KEY, GPAD_SNES_A, BTN_A,
						&uinp_gpads[ctr]);
				processPadBtn(gpads[ctr].state, EV_KEY, GPAD_SNES_B, BTN_B,
						&uinp_gpads[ctr]);
				processPadBtn(gpads[ctr].state, EV_KEY, GPAD_SNES_X, BTN_X,
						&uinp_gpads[ctr]);
				processPadBtn(gpads[ctr].state, EV_KEY, GPAD_SNES_Y, BTN_Y,
						&uinp_gpads[ctr]);
				processPadBtn(gpads[ctr].state, EV_KEY, GPAD_SNES_L, BTN_TL,
						&uinp_gpads[ctr]);
				processPadBtn(gpads[ctr].state, EV_KEY, GPAD_SNES_R, BTN_TR,
						&uinp_gpads[ctr]);
				processPadBtn(gpads[ctr].state, EV_KEY, GPAD_SNES_SELECT,
						BTN_SELECT, &uinp_gpads[ctr]);
				processPadBtn(gpads[ctr].state, EV_KEY, GPAD_SNES_START,
						BTN_START, &uinp_gpads[ctr]);

				if ((gpads[ctr].state & GPAD_SNES_LEFT) == GPAD_SNES_LEFT) {
					uinput_gpad_write(&uinp_gpads[ctr], ABS_X, 0, EV_ABS);
				} else if ((gpads[ctr].state & GPAD_SNES_RIGHT) == GPAD_SNES_RIGHT) {
					uinput_gpad_write(&uinp_gpads[ctr], ABS_X, 4, EV_ABS);
				} else {
					uinput_gpad_write(&uinp_gpads[ctr], ABS_X, 2, EV_ABS);
				}
				if ((gpads[ctr].state & GPAD_SNES_UP) == GPAD_SNES_UP) {
					uinput_gpad_write(&uinp_gpads[ctr], ABS_Y, 0, EV_ABS);
				} else if ((gpads[ctr].state & GPAD_SNES_DOWN) == GPAD_SNES_DOWN) {
					uinput_gpad_write(&uinp_gpads[ctr], ABS_Y, 4, EV_ABS);
				} else {
					uinput_gpad_write(&uinp_gpads[ctr], ABS_Y, 2, EV_ABS);
				}
			}
		}

		if (confres.button_enabled) {
			btn_read(&button);

			switch (button.state) {
			case BTN_STATE_IDLE:
				break;
			case BTN_STATE_PRESSED:
				if (button.pressedCtr == 1 && button.duration >= 1) {
					uinput_kbd_write(&uinp_kbd, KEY_R, 1, EV_KEY);
				}
				break;
			case BTN_STATE_RELEASED:
					if (button.pressedCtr == 1 && button.duration >= 1) {
						uinput_kbd_write(&uinp_kbd, KEY_R, 0, EV_KEY);
					} else if (button.pressedCtr == 3 && button.duration >= 1) {
						// Sending ESC
						uinput_kbd_write(&uinp_kbd, KEY_ESC, 1, EV_KEY);
						usleep(50000);
						uinput_kbd_write(&uinp_kbd, KEY_ESC, 0, EV_KEY);
					} else if (button.pressedCtr == 5 && button.duration >= 1) {
						uinput_kbd_write(&uinp_kbd, KEY_F4, 1, EV_KEY);
						usleep(50000);
						uinput_kbd_write(&uinp_kbd, KEY_F4, 0, EV_KEY);
						// shutting down
						system("shutdown -t 3 -h now");
					}
				break;
			}
		}

		/* wait for some time to keep the CPU load low */
		if (confres.button_enabled && !(confres.gamepad1_enabled || confres.gamepad2_enabled)) {
			bcm2835_delay(FRAMEWAITLONG);
		} else {
			bcm2835_delay(FRAMEWAIT);
		}
	}

	return 0;

}