summaryrefslogtreecommitdiff
path: root/RobotFirmware/lib/HexaNRL/src/Gamepad.h
blob: 5419aeb7023f28464bd39a0a6c503c2002e59706 (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
#pragma once
#include "NRLGamepad.h"
#include <functional>

// ============================================================
//  Gamepad — object-oriented gamepad interface
// ============================================================
//  Wraps the static NRLGamepad globals with named accessors,
//  built-in edge detection, and optional button bindings.
//
//  Global instance: gamepad1  (declared in RobotConfig)
//  Updated automatically at the top of each 100Hz TELEOP tick.
//
//  Usage:
//    drive.drive(gamepad1.leftY(), gamepad1.rightX());
//    if (gamepad1.justPressed(BTN_DPAD_RIGHT)) runAction(sequential({ ... }));
//      ^ justPressed fires on the press only — that is what keeps runAction()
//        from queuing a fresh copy of the sequence 50 times a second.
//    gamepad1.onPress(BTN_DPAD_DOWN, []{ gripper.setPosition(90); });  // in init()

class Gamepad {
public:
    // ----------------------------------------------------------
    //  Axis accessors  (-1.0 to +1.0)
    // ----------------------------------------------------------
    float leftX()  const;
    float leftY()  const;
    float rightX() const;
    float rightY() const;

    // ----------------------------------------------------------
    //  Button state
    // ----------------------------------------------------------
    bool pressed(uint16_t btn)      const;
    bool justPressed(uint16_t btn)  const;
    bool justReleased(uint16_t btn) const;

    // ----------------------------------------------------------
    //  Button bindings — register once in init(), not in loop()
    //  For sequences use justPressed in loop() instead.
    // ----------------------------------------------------------
    static constexpr uint8_t MAX_BINDINGS = 8;

    void onPress(uint16_t btn, std::function<void()> cb);
    void onRelease(uint16_t btn, std::function<void()> cb);

    // ----------------------------------------------------------
    //  Called by NRLRunner at top of each 100Hz TELEOP tick.
    //  Not for student use.
    // ----------------------------------------------------------
    void snapshot();

    // ----------------------------------------------------------
    //  reset() — Clear snapshots and bindings for OpMode restart.
    //  Called by NRLRunner when starting a new OpMode.
    //  Prevents stale button state from previous runs.
    // ----------------------------------------------------------
    void reset();

    // ----------------------------------------------------------
    //  ignoreHeldButtons() — Called by NRLRunner on the first tick
    //  of a run. Buttons already held at that moment (the X that
    //  pressed START) are invisible until released once, so they
    //  cannot fire justPressed()/onPress() as the run begins.
    //  Not for student use.
    // ----------------------------------------------------------
    void ignoreHeldButtons();

private:
    struct Snap { float lx=0, ly=0, rx=0, ry=0; uint16_t buttons=0; };
    Snap _curr, _prev;               // buttons = what pressed() reports
    uint16_t _edgeCurr = 0, _edgePrev = 0;  // what justPressed()/justReleased() compare
    uint16_t _raw = 0;               // last raw buttons read
    uint16_t _heldAtStart = 0;       // hidden from pressed() AND edges until released
    uint16_t _heldThroughOutage = 0; // hidden from edges only until released
    uint32_t _staleSeq = 0;          // packetSeq seen while input was stale
    bool     _awaitPacket = false;   // stale ended, no real packet yet

    struct Binding { uint16_t btn=0; std::function<void()> cb; };
    Binding _press[MAX_BINDINGS];
    Binding _release[MAX_BINDINGS];

    void _setBind(Binding (&arr)[MAX_BINDINGS], uint16_t btn, std::function<void()> cb);
};

extern Gamepad gamepad1;