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/mbc7_accelerometer.c
1 #include <gbdk/platform.h>
2 #include <stdint.h>
3 #include <stdbool.h>
4
5
6 // **** IMPORTANT ****
7 //
8 // ** DO NOT ** use regular vsync() or wait_vbl_done() for at least 1.2 msec after
9 // latching accelerometer data with mbc7_accel_read(). Doing that will cause the
10 // MBC7 accelerometer sensor data to have errors.
11 //
12 // This is because vsync() puts the CPU into HALT (until the next frame)
13 // which turns off the cartridge PHI clock that the MBC7 accelerometer
14 // sensor relies on.
15 //
16 // This example provides an alternative vsync_no_halt() that is safe to use
17 // without timing limitations.
18
19
20 // Accelerometer Sensor data ranges
21 //
22 // Note: The ranges below are for regular single-speed
23 // processor mode. In GBC Double-speed mode there appears
24 // to be an offset of around +440 on the X axis and +480
25 // on the Y axis when the cart is flat/centered.
26 //
27 // Slow rotation, measuring mostly the pull
28 // earth's gravity when the sensor not flat.
29 //
30 // The range is for a full rotation of 90 degrees from flat
31 //
32 // X: Left Centered Right
33 // 0x8230 <------- 0x81BC -------> 0x8148
34 // +116 0 -116
35 //
36 // Y: Forward Centered Backward
37 // 0x8253 <------- 0x81DE -------> 0x8170
38 // +117 0 -110
39 //
40 //
41 // Higher end ranges for fast horizontal movements
42 //
43 // X: Left Centered Right
44 // 0x837E <------- 0x81BC -------> 0x8000
45 // +450 0 -444
46 //
47 // Y: Forward Centered Backward
48 // 0x837D <------- 0x81DE -------> 0x8020
49 // +416 0 -446
50
51
52 // ADXL202 or ADXL210 accelerometer
53 // https://www.analog.com/media/en/technical-documentation/obsolete-data-sheets/ADXL210.pdf
54
55
56
57 bool accel_origin_set = false;
58 uint16_t accel_x;
59 uint16_t accel_y;
60 uint16_t accel_x_origin = 0u;
61 uint16_t accel_y_origin = 0u;
62 int16_t accel_x_relative = 0;
63 int16_t accel_y_relative = 0;
64
65
66
67 // Call this once per frame to update the accelerometer data
68 //
69 // Optionally it may be installed as a VBL handler
70 void mbc7_accel_read(void) {
71 // Read the Raw sensor values
72 accel_x = ((uint16_t)(rMBC7_ACCEL_X_HI) << 8) | (uint16_t)rMBC7_ACCEL_X_LO;
73 accel_y = ((uint16_t)(rMBC7_ACCEL_Y_HI) << 8) | (uint16_t)rMBC7_ACCEL_Y_LO;
74
75 // Latch data that doesn't get read until next frame
76 // requires ~1.2 msec of settling time during which the CPU
77 // ** MUST NOT ** be put into HALT or STOP (so do not use regular
78 // vsync() or wait_vbl_done(), instead use the vsync_no_halt() ).
79 rMBC7_LATCH_1 = MBC7_LATCH_ERASE;
80 rMBC7_LATCH_2 = MBC7_LATCH_CAPTURE;
81 }
82
83
84 void mbc7_accel_init(void) {
85
86 // Enable SRAM MVBC7 access (required for accessing accelerometer data)
87 rMBC7_SRAM_ENABLE_1 = MBC7_SRAM_ENABLE_KEY_1;
88 rMBC7_SRAM_ENABLE_2 = MBC7_SRAM_ENABLE_KEY_2;
89
90 // Do a single read to prime the sensor
91 mbc7_accel_read();
92
93 // The data read won't be valid until ~1.2 msec after latching
94 // so for the initial one just delay
95 delay(2);
96 }
97
98
99 void mbc7_accel_setorigin(void) {
100 accel_x_origin = accel_x;
101 accel_y_origin = accel_y;
102 accel_origin_set = true;
103 }
104
105
106 void mbc7_accel_update(bool do_set_origin) {
107
108 mbc7_accel_read();
109
110 if (do_set_origin) {
111 mbc7_accel_setorigin();
112 }
113
114 if (accel_origin_set) {
115 // Calculate relative movement of sensor compared to user prompted origin
116 accel_x_relative = (int16_t)(accel_x_origin - accel_x);
117 accel_y_relative = (int16_t)(accel_y_origin - accel_y);
118 }
119 }
This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.