fork/GramFirmware
git clone https://git.julia.blue/fork/GramFirmware
818d962
gram
1#ifndef _UTIL_STATE_UTIL_HPP 2#define _UTIL_STATE_UTIL_HPP 3 4#include "stdlib.hpp" 5 6#include <config.pb.h> 7 8/* InputState utils */ 9 10inline void set_button (uint64_t & buttons ,Button button_index ,bool pressed ) { 11if (button_index == BTN_UNSPECIFIED ) { 12return ; 13 } 14Button button_index_adjusted = (Button )(button_index - 1 ); 15buttons = 16 (buttons & ~(1ULL <<button_index_adjusted )) | ((uint64_t )pressed <<button_index_adjusted ); 17} 18 19inline bool get_button (const uint64_t & buttons ,Button button_index ) { 20if (button_index == BTN_UNSPECIFIED ) { 21return false; 22 } 23return buttons & (1ULL << (button_index - 1 )); 24} 25 26inline uint64_t make_button_mask (const Button * buttons ,size_t buttons_count ) { 27uint64_t button_mask = 0 ; 28for (size_t j = 0 ;j < buttons_count ;j ++ ) { 29button_mask |= (1ULL << (buttons [j ]- 1 )); 30 } 31return button_mask ; 32} 33 34inline bool all_buttons_held (const uint64_t & buttons ,uint64_t button_mask ) { 35return button_mask != 0 && (buttons & button_mask )== button_mask ; 36} 37 38inline bool any_button_held (const uint64_t & buttons ,uint64_t button_mask ) { 39return button_mask != 0 && (buttons & button_mask ); 40} 41 42/* OutputState utils */ 43 44inline void set_output (uint32_t & buttons ,DigitalOutput output_index ,bool pressed ) { 45if (output_index == GP_UNSPECIFIED ) { 46return ; 47 } 48DigitalOutput output_index_adjusted = (DigitalOutput )(output_index - 1 ); 49buttons = 50 (buttons & ~(1UL <<output_index_adjusted )) | ((uint32_t )pressed <<output_index_adjusted ); 51} 52 53inline uint8_t OutputState ::* axis_pointer (AnalogAxis axis ) { 54switch (axis ) { 55case AXIS_LSTICK_X : 56return & OutputState ::leftStickX ; 57case AXIS_LSTICK_Y : 58return & OutputState ::leftStickY ; 59case AXIS_RSTICK_X : 60return & OutputState ::rightStickX ; 61case AXIS_RSTICK_Y : 62return & OutputState ::rightStickY ; 63case AXIS_LTRIGGER : 64return & OutputState ::triggerLAnalog ; 65case AXIS_RTRIGGER : 66return & OutputState ::triggerRAnalog ; 67default : 68return nullptr ; 69 } 70} 71 72constexpr const char * digital_output_name (DigitalOutput output ) { 73switch (output ) { 74case GP_A : 75return "A" ; 76case GP_B : 77return "B" ; 78case GP_X : 79return "X" ; 80case GP_Y : 81return "Y" ; 82case GP_LB : 83return "L1" ; 84case GP_RB : 85return "R1" ; 86case GP_LT : 87return "L2" ; 88case GP_RT : 89return "R2" ; 90case GP_START : 91return "Start" ; 92case GP_SELECT : 93return "Select" ; 94case GP_HOME : 95return "Home" ; 96case GP_CAPTURE : 97return "Capture" ; 98case GP_DPAD_UP : 99return "D-Pad Up" ; 100case GP_DPAD_DOWN : 101return "D-Pad Down" ; 102case GP_DPAD_LEFT : 103return "D-Pad Left" ; 104case GP_DPAD_RIGHT : 105return "D-Pad Right" ; 106case GP_LSTICK_CLICK : 107return "L3" ; 108case GP_RSTICK_CLICK : 109return "R3" ; 110default : 111return "Unknown" ; 112 } 113} 114 115#endif