CHGame 0.4.1
Libraries for the CHGame handheld
Loading...
Searching...
No Matches
Buttons and frame pacing

The global chgame object: the frame clock and the eight buttons. More...

Classes

class  CHGame
 The board's buttons and frame clock. There is one, chgame. More...
 

Macros

#define A_BUTTON   (1u << 0)
 The A button (PB1; PB11 on Rev1).
 
#define B_BUTTON   (1u << 1)
 The B button (PB6 on both boards).
 
#define UP_BUTTON   (1u << 2)
 D-pad up (PB4; PB0 on Rev1).
 
#define DOWN_BUTTON   (1u << 3)
 D-pad down (PC14; PB1 on Rev1).
 
#define LEFT_BUTTON   (1u << 4)
 D-pad left (PB3; PC3 on Rev1).
 
#define RIGHT_BUTTON   (1u << 5)
 D-pad right (PC15; PC0 on Rev1).
 
#define START_BUTTON   (1u << 6)
 START (PB8 on both boards). Held 3 s, it leaves for the menu (CHGame::startExits).
 
#define SELECT_BUTTON   (1u << 7)
 SELECT (PB7 on both boards).
 

Functions

uint8_t chgame_readButtons ()
 Read the buttons straight from the hardware.
 
void chgame_exitToMenu ()
 Leave the game for the bootloader's game menu.
 

Variables

CHGame chgame
 The one instance: chgame.boot(), chgame.pressed() ...
 

Detailed Description

The global chgame object: the frame clock and the eight buttons.

Call chgame.boot() once in setup(), then in loop():

if (!chgame.nextFrame()) return; // not time for a frame yet
chgame.pollButtons(); // one snapshot of the buttons for this frame
if (chgame.pressed(LEFT_BUTTON | B_BUTTON)) { ... } // both held
bool justPressed(uint8_t b) const
Whether a button went down this frame.
Definition Input.h:103
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.
#define B_BUTTON
The B button (PB6 on both boards).
Definition Input.h:37
#define A_BUTTON
The A button (PB1; PB11 on Rev1).
Definition Input.h:36
CHGame chgame
The one instance: chgame.boot(), chgame.pressed() ...
#define LEFT_BUTTON
D-pad left (PB3; PC3 on Rev1).
Definition Input.h:40

The button names and the calls are the Arduboy2 library's, so code written for it reads the same here. The pins in brackets are CHGame Rev0's, then Rev1's where they differ; a sketch sees the same masks on both boards.

Macro Definition Documentation

◆ A_BUTTON

#define A_BUTTON   (1u << 0)

The A button (PB1; PB11 on Rev1).

◆ B_BUTTON

#define B_BUTTON   (1u << 1)

The B button (PB6 on both boards).

◆ UP_BUTTON

#define UP_BUTTON   (1u << 2)

D-pad up (PB4; PB0 on Rev1).

◆ DOWN_BUTTON

#define DOWN_BUTTON   (1u << 3)

D-pad down (PC14; PB1 on Rev1).

◆ LEFT_BUTTON

#define LEFT_BUTTON   (1u << 4)

D-pad left (PB3; PC3 on Rev1).

◆ RIGHT_BUTTON

#define RIGHT_BUTTON   (1u << 5)

D-pad right (PC15; PC0 on Rev1).

◆ START_BUTTON

#define START_BUTTON   (1u << 6)

START (PB8 on both boards). Held 3 s, it leaves for the menu (CHGame::startExits).

◆ SELECT_BUTTON

#define SELECT_BUTTON   (1u << 7)

SELECT (PB7 on both boards).

Function Documentation

◆ chgame_readButtons()

uint8_t chgame_readButtons ( )

Read the buttons straight from the hardware.

Returns
A mask of the buttons held right now (pressed = 1), A_BUTTON ... SELECT_BUTTON ORed together.

Implemented per platform (device: GPIO registers; simulator: the scripted input). A game normally uses CHGame::pollButtons() and the queries instead, which also see injected input.

◆ chgame_exitToMenu()

void chgame_exitToMenu ( )

Leave the game for the bootloader's game menu.

Never returns.

A plain reset, with no boot request (platform/bootloader/shared/chgame_bootreq.h). Under a bootloader without the menu (0.2.4) the game simply starts again. In the simulator it prints a line and ends the run. First it calls chgame_beforeExit() if the sketch has one (a weak symbol; a library that drives hardware which must be off across a reset defines it: the add-on CHPower library turns the Rev 1.5's output and host power off), as the debug protocol's B does.

Variable Documentation

◆ chgame

CHGame chgame
extern

The one instance: chgame.boot(), chgame.pressed() ...