gbdk-2020 | GameBoy Development Kit |
| download: https://git.y1.nz/archives/gbdk.tar.gz | |
| README | Files | Log | Refs | LICENSE |
gbdk-lib/examples/gb/mbc7_accelerometer/src/main.c
1 #include <gbdk/platform.h>
2 #include <stdint.h>
3 #include <stdbool.h>
4 #include <stdio.h>
5 #include <gbdk/console.h>
6
7 #include "mbc7_accelerometer.h"
8 #include "vsync_no_halt.h"
9
10
11 /* **** IMPORTANT ****
12
13 ** DO NOT ** use regular vsync() or wait_vbl_done() for at least 1.2 msec after
14 latching accelerometer data with mbc7_accel_read(). Doing that will cause the
15 MBC7 accelerometer sensor data to have errors.
16
17 This is because vsync() puts the CPU into HALT (until the next frame)
18 which turns off the cartridge PHI clock that the MBC7 accelerometer
19 sensor relies on.
20
21 This example provides an alternative vsync_no_halt() that is safe to use
22 without timing limitations.
23 */
24
25
26 // New and previous values of the joypad input
27 uint8_t joy = 0, old_joy;
28 // User input macros
29 #define INPUT_UPDATE (old_joy=joy,joy=joypad())
30 #define INPUT_BUTTON(key) (joy&(key))
31 #define INPUT_BUTTONPRESS(key) ((joy & ~old_joy) & (key))
32
33
34 // A plus shaped spite for showing the accelerometer movement
35 #define SPR_TILE_0 0u // Use tile number 0
36 #define SPR_ID_CROSS 0u // Use sprite id number 0
37
38 #define SCRN_CENTER_X (((DEVICE_SCREEN_PX_WIDTH - 8u) / 2u) + DEVICE_SPRITE_PX_OFFSET_X)
39 #define SCRN_CENTER_Y (((DEVICE_SCREEN_PX_HEIGHT - 8u) / 2u) + DEVICE_SPRITE_PX_OFFSET_Y)
40
41 const uint8_t sprite_tile[] = {
42 0b00111100, 0b00011000,
43 0b01111110, 0b00011000,
44 0b11111111, 0b00011000,
45 0b11111111, 0b11111111,
46 0b11111111, 0b11111111,
47 0b11111111, 0b00011000,
48 0b01111110, 0b00011000,
49 0b00111100, 0b00011000,
50 };
51
52
53 // How much to divide the relative accelerometer data by.
54 // Dividing by larger amounts filters out small unwanted movements, but reduces sensitivity
55 #define ACCEL_DIVIDE_AMT 8
56
57
58 void main(void)
59 {
60 bool do_set_origin = false;
61 bool show_data = false;
62
63 HIDE_SPRITES;
64 // Assign a sprite with a tile, but hide it by default
65 set_sprite_data(SPR_TILE_0, 1u, sprite_tile);
66 set_sprite_tile(SPR_ID_CROSS, SPR_TILE_0);
67
68 printf("-B to Center\n");
69 printf("-SELECT toggles info");
70
71 mbc7_accel_init();
72
73 while(1) {
74
75 // Check for Gamepad input
76 INPUT_UPDATE;
77 switch (INPUT_BUTTONPRESS(J_B | J_SELECT)) {
78 case J_B: do_set_origin = true; break;
79 case J_SELECT: show_data ^= true; break;
80 }
81
82 mbc7_accel_update(do_set_origin);
83
84 // Once the origin has been set, clear the flag and show some data about it
85 if (do_set_origin) {
86 do_set_origin = false;
87 // Display origin, center the sprite and turn on sprites
88 gotoxy(0,3);
89 printf("origin x:%x\norigin y:%x", accel_x_origin, accel_y_origin);
90 SHOW_SPRITES;
91 move_sprite(SPR_ID_CROSS, SCRN_CENTER_X, SCRN_CENTER_Y);
92 }
93
94 // Use the Accelerometer data if the origin has been set
95 if (accel_origin_set) {
96
97 // Move the sprite around the screen using the accelerometer data
98 scroll_sprite(SPR_ID_CROSS, accel_x_relative / ACCEL_DIVIDE_AMT, accel_y_relative / ACCEL_DIVIDE_AMT);
99
100 if (show_data) {
101 gotoxy(0,6);
102 printf("%x\n"
103 "%x\n"
104 "%d \n"
105 "%d \n",
106 (uint16_t)accel_x, (uint16_t)accel_y,
107 (int16_t)accel_x_relative, (int16_t)accel_y_relative);
108 }
109 }
110
111 // Use an alternate to regular vsync() / wait_vbl_done() which
112 // does not put the CPU into HALT. This is important since HALT
113 // interferes with the reading the MBC7 Accelerometer sensor data.
114 vsync_no_halt();
115 }
116 }
This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.