CHGame 0.4.1
Libraries for the CHGame handheld
Loading...
Searching...
No Matches
Input.h
Go to the documentation of this file.
1/// @file Input.h
2/// @brief Buttons and frame pacing: the Arduboy-flavoured front of the CHGame board.
3///
4/// Started from the helper shared by CHSpriteView/CHMultiSprite/CHStlView and
5/// reworked for the casino games, which all used this copy:
6/// - button masks are parenthesised, so ~UP_BUTTON and A|B behave;
7/// - every query reads the state captured by pollButtons(), so a frame sees
8/// one consistent snapshot and injected input (debug protocol, simulator)
9/// behaves exactly like a real press;
10/// - nextFrame() uses a microsecond accumulator, so 60 fps is 60.0, not the
11/// 62.5 that 1000/60 = 16 ms gave;
12/// - a lockstep mode lets the debug protocol step the game frame by frame;
13/// - auto-repeat for held buttons (menus, bet adjust);
14/// - holding START for 3 s leaves for the SD game menu (startExits).
15#pragma once
16#include <stdint.h>
17
18/// @defgroup chgame_input Buttons and frame pacing
19/// @ingroup lib_chgame
20/// @brief The global @ref ::chgame object: the frame clock and the eight buttons.
21///
22/// Call chgame.boot() once in `setup()`, then in `loop()`:
23/// @code
24/// if (!chgame.nextFrame()) return; // not time for a frame yet
25/// chgame.pollButtons(); // one snapshot of the buttons for this frame
26/// if (chgame.justPressed(A_BUTTON)) { ... }
27/// if (chgame.pressed(LEFT_BUTTON | B_BUTTON)) { ... } // both held
28/// @endcode
29///
30/// The button names and the calls are the Arduboy2 library's, so code
31/// written for it reads the same here. The pins in brackets are CHGame
32/// Rev0's, then Rev1's where they differ; a sketch sees the same masks on
33/// both boards.
34/// @{
35
36#define A_BUTTON (1u << 0) ///< The A button (PB1; PB11 on Rev1).
37#define B_BUTTON (1u << 1) ///< The B button (PB6 on both boards).
38#define UP_BUTTON (1u << 2) ///< D-pad up (PB4; PB0 on Rev1).
39#define DOWN_BUTTON (1u << 3) ///< D-pad down (PC14; PB1 on Rev1).
40#define LEFT_BUTTON (1u << 4) ///< D-pad left (PB3; PC3 on Rev1).
41#define RIGHT_BUTTON (1u << 5) ///< D-pad right (PC15; PC0 on Rev1).
42#define START_BUTTON (1u << 6) ///< START (PB8 on both boards). Held 3 s, it leaves for the menu (CHGame::startExits).
43#define SELECT_BUTTON (1u << 7) ///< SELECT (PB7 on both boards).
44
45/// @brief Read the buttons straight from the hardware.
46/// @return A mask of the buttons held right now (pressed = 1), `A_BUTTON` ...
47/// `SELECT_BUTTON` ORed together.
48/// @details Implemented per platform (device: GPIO registers; simulator: the
49/// scripted input). A game normally uses CHGame::pollButtons() and the
50/// queries instead, which also see injected input.
52
53/// @brief Leave the game for the bootloader's game menu. Never returns.
54/// @details A plain reset, with no boot request
55/// (platform/bootloader/shared/chgame_bootreq.h). Under a bootloader without
56/// the menu (0.2.4) the game simply starts again. In the simulator it prints
57/// a line and ends the run. First it calls `chgame_beforeExit()` if the
58/// sketch has one (a weak symbol; a library that drives hardware which must
59/// be off across a reset defines it: the add-on CHPower library turns the
60/// Rev 1.5's output and host power off), as the debug protocol's B does.
61[[noreturn]] void chgame_exitToMenu();
62
63/// @brief The board's buttons and frame clock. There is one, @ref ::chgame.
64class CHGame {
65public:
66 /// @brief Set up the button pins (inputs with pull-ups) and take a first reading.
67 /// @details Call once at the top of `setup()`. A button held at boot does
68 /// not count as a press on the first frame.
69 void boot();
70 /// @brief Set the frame rate nextFrame() paces to.
71 /// @param fps Frames per second (1-255). The default is 60.
72 void setFrameRate(uint8_t fps);
73 /// @brief Whether it is time for the next frame.
74 /// @return true once per frame period (and counts the frame in frameCount);
75 /// false otherwise, so `loop()` returns at once.
76 /// @details A game that falls more than three frames behind (a debug
77 /// pause, a save) starts afresh instead of sprinting to catch up. In
78 /// lockstep (the debug protocol's `L1`) it returns true only for the
79 /// frames the tool asked for.
80 bool nextFrame();
81
82 /// @brief Take this frame's snapshot of the buttons.
83 /// @details Call once per frame, after nextFrame(). Every query below
84 /// reads this snapshot, so a frame sees one consistent state. It ORs in
85 /// `injected` (the debug protocol's `K`), counts how long each button has
86 /// been held (for repeat()), and calls exitToMenu() when START has been
87 /// held 3 s and startExits is set.
89 /// @brief The buttons held in this frame's snapshot.
90 /// @return A mask of `A_BUTTON` ... `SELECT_BUTTON`.
91 uint8_t buttons() const { return cur; }
92 /// @brief Whether all the buttons in a mask are held.
93 /// @param b One button or several ORed together.
94 /// @return true if every one of them is held.
95 bool pressed(uint8_t b) const { return (cur & b) == b; }
96 /// @brief Whether any of the buttons in a mask is held.
97 /// @param b One button or several ORed together.
98 /// @return true if at least one of them is held.
99 bool anyPressed(uint8_t b) const { return (cur & b) != 0; }
100 /// @brief Whether a button went down this frame.
101 /// @param b One button or several ORed together.
102 /// @return true if any of them is held now and was not in the previous frame.
103 bool justPressed(uint8_t b) const { return (cur & ~prev & b) != 0; }
104 /// @brief Whether a button was let go this frame.
105 /// @param b One button or several ORed together.
106 /// @return true if any of them was held in the previous frame and is not now.
107 bool justReleased(uint8_t b) const { return (prev & ~cur & b) != 0; }
108 /// @brief Every button that went down this frame.
109 /// @return A mask of the buttons pressed now and not in the previous frame.
110 uint8_t justPressedMask() const { return (uint8_t)(cur & ~prev); }
111 /// @brief Press edge, then auto-repeat while held: for menus and adjusting a bet.
112 /// @param b One button or several ORed together.
113 /// @param delay Frames held before the repeat starts (default 18, 0.3 s at 60 fps).
114 /// @param rate Frames between repeats after that (default 5).
115 /// @return true on the frame a button went down, then every `rate` frames
116 /// once it has been held longer than `delay`.
117 bool repeat(uint8_t b, uint8_t delay = 18, uint8_t rate = 5) const;
118 /// @brief Forget this frame's presses: justPressed() and justReleased() are
119 /// false until the buttons change again.
120 /// @details Use it when a screen changes, so the press that changed it
121 /// does not also act on the new one.
122 void clearButtonState() { prev = cur; }
123
124 /// @brief Whether frameCount is a multiple of n: something to do every n frames.
125 /// @param n The period in frames (not 0).
126 /// @return true on every nth frame.
127 bool everyXFrames(uint16_t n) const { return (frameCount % n) == 0; }
128
129 /// @brief Leave the game for the SD game menu. Never returns.
130 /// @see chgame_exitToMenu()
131 [[noreturn]] void exitToMenu() { chgame_exitToMenu(); }
132
133 uint32_t frameCount = 0; ///< Frames since boot, counted by nextFrame().
134 uint8_t injected = 0; ///< Buttons ORed into the physical ones (the debug protocol's `K`).
135 /// START held 3 s calls exitToMenu(); a game that needs a long START hold
136 /// clears it in setup().
137 bool startExits = true;
138#ifdef CHSIM
139 int32_t lockstep = 0; // the simulator starts paused, driven by N
140#else
141 /// Lockstep: < 0 free-running, else the frames nextFrame() may still run
142 /// (set by the debug protocol's `L` and `N`).
143 int32_t lockstep = -1;
144#endif
145
146private:
147 uint8_t cur = 0, prev = 0;
148 uint16_t held[8] = {}; // frames each button has been held
149 uint32_t period = 16667, next = 0;
150};
151
152/// @brief The one instance: chgame.boot(), chgame.pressed() ...
153extern CHGame chgame;
154
155/// @}
The board's buttons and frame clock. There is one, chgame.
Definition Input.h:64
uint8_t injected
Buttons ORed into the physical ones (the debug protocol's K).
Definition Input.h:134
void clearButtonState()
Forget this frame's presses: justPressed() and justReleased() are false until the buttons change agai...
Definition Input.h:122
uint32_t frameCount
Frames since boot, counted by nextFrame().
Definition Input.h:133
bool anyPressed(uint8_t b) const
Whether any of the buttons in a mask is held.
Definition Input.h:99
bool justPressed(uint8_t b) const
Whether a button went down this frame.
Definition Input.h:103
void exitToMenu()
Leave the game for the SD game menu.
Definition Input.h:131
bool repeat(uint8_t b, uint8_t delay=18, uint8_t rate=5) const
Press edge, then auto-repeat while held: for menus and adjusting a bet.
int32_t lockstep
Lockstep: < 0 free-running, else the frames nextFrame() may still run (set by the debug protocol's L ...
Definition Input.h:143
bool nextFrame()
Whether it is time for the next frame.
bool pressed(uint8_t b) const
Whether all the buttons in a mask are held.
Definition Input.h:95
void pollButtons()
Take this frame's snapshot of the buttons.
bool justReleased(uint8_t b) const
Whether a button was let go this frame.
Definition Input.h:107
void boot()
Set up the button pins (inputs with pull-ups) and take a first reading.
uint8_t buttons() const
The buttons held in this frame's snapshot.
Definition Input.h:91
bool everyXFrames(uint16_t n) const
Whether frameCount is a multiple of n: something to do every n frames.
Definition Input.h:127
bool startExits
START held 3 s calls exitToMenu(); a game that needs a long START hold clears it in setup().
Definition Input.h:137
uint8_t justPressedMask() const
Every button that went down this frame.
Definition Input.h:110
void setFrameRate(uint8_t fps)
Set the frame rate nextFrame() paces to.
void chgame_exitToMenu()
Leave the game for the bootloader's game menu.
CHGame chgame
The one instance: chgame.boot(), chgame.pressed() ...
uint8_t chgame_readButtons()
Read the buttons straight from the hardware.