aboutsummaryrefslogtreecommitdiff
path: root/src/joy.c
blob: c1b433aa823a1c5c6d810d63b0b3a7d09adbecbe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <stdint.h>
#include <stdio.h>
#include <pc.h>
#include <dos.h>

#include "vga.h"
#include "keyb.h"
#include "control.h"

#include "joy.h"

#define PORT 0x201

#define BUTTON1 0x10
#define BUTTON2 0x20
#define AXISX   1
#define AXISY   2

#define TIMEOUT 9999
#define CALIBRATING_LOOPS 10

static uint16_t j_up, j_down, j_left, j_right;

static uint16_t read_axis(uint16_t *x, uint16_t *y)
{
    uint16_t i, value;
    uint16_t mask = AXISX | AXISY;

    *x = *y = 0;

    disable();
    outportb(PORT, 0);
    for (i = 1; mask && i < TIMEOUT; i++)
    {
        value = inportb(PORT) ^ mask;
        if (value & AXISX)
        {
            *x = i;
            mask ^= AXISX;
        }
        if (value & AXISY)
        {
            *y = i;
            mask ^= AXISY;
        }
    }
    enable();

    return i;
}

static uint8_t calibrate_release(uint16_t *x, uint16_t *y)
{
    uint8_t i;
    uint16_t xacc, yacc;

    while (1)
    {
        if (keys[KEY_ESC])
        {
            while (keys[KEY_ESC])
                wait_vsync();
            return 0;
        }

        xacc = yacc = 0;
        for (i = 0; i < CALIBRATING_LOOPS; i++)
        {
            read_axis(x, y);
            xacc += *x;
            yacc += *y;
        }

        if (!(inportb(PORT) & BUTTON1))
        {
            while (!(inportb(PORT) & BUTTON1))
                wait_vsync();

            *x = xacc / CALIBRATING_LOOPS;
            *y = yacc / CALIBRATING_LOOPS;

            return 1;
        }
        wait_vsync();
    }
}

static uint8_t calibrate_circle(uint16_t joy_values[4])
{
    uint8_t i;
    uint16_t x, y;

    while (1)
    {
        if (keys[KEY_ESC])
        {
            while (keys[KEY_ESC])
                wait_vsync();
            return 0;
        }

        for (i = 0; i < CALIBRATING_LOOPS; i++)
        {
            read_axis(&x, &y);
            // up
            if (y < joy_values[0])
                joy_values[0] = y;
            // down
            if (y > joy_values[1])
                joy_values[1] = y;
            // left
            if (x < joy_values[2])
                joy_values[2] = x;
            // right
            if (x > joy_values[3])
                joy_values[3] = x;
        }

        if (!(inportb(PORT) & BUTTON1))
        {
            while (!(inportb(PORT) & BUTTON1))
                wait_vsync();

            return 1;
        }
        wait_vsync();
    }
}

uint8_t joy_detect()
{
    uint16_t xcenter, ycenter;

    // this read doesn't matter
    if (read_axis(&xcenter, &ycenter) == TIMEOUT)
        return 0;

    printf("Joystick detected!\n");

    printf("Please center the joystick and press BUTTON 1 (ESC to abort)\n");
    if (!calibrate_release(&xcenter, &ycenter))
        return 0;

    // up, down, left, right
    uint16_t joy_values[4] = { ycenter, ycenter, xcenter, xcenter };

    printf("Please move the joystick in circles and then press BUTTON 1 (ESC to abort)\n");
    if (!calibrate_circle(joy_values))
        return 0;

    printf("Please center the joystick and press BUTTON 1 (ESC to abort)\n");
    if (!calibrate_release(&xcenter, &ycenter))
        return 0;

    j_up = ycenter - ((ycenter - joy_values[0]) / 2);
    j_down = ycenter + ((joy_values[1] - ycenter) / 2);
    j_left = xcenter - ((xcenter - joy_values[2]) / 2);
    j_right = xcenter + ((joy_values[3] - xcenter) / 2);

    return 1;
}

uint8_t joy_read()
{
    uint8_t r = CTL_NONE;
    uint8_t b;

    b = inportb(PORT);
    if (!(b & BUTTON1))
        r |= CTL_FIRE1;
    if (!(b & BUTTON2))
        r |= CTL_FIRE2;

    uint16_t x, y;

    read_axis(&x, &y);
    if (y < j_up)
        r |= CTL_UP;
    if (y > j_down)
        r |= CTL_DOWN;
    if (x < j_left)
        r |= CTL_LEFT;
    if (x > j_right)
        r |= CTL_RIGHT;

    return r;
}