blk_box_tc/main/drivers/bottom_half.h
2025-08-21 19:32:27 -05:00

139 lines
4.7 KiB
C

#ifndef BOTTOM_HALF_HPP
#define BOTTOM_HALF_HPP
#include "driver/i2c.h"
#include "driver/gpio.h"
#define BOTTOM_I2C_NUM I2C_NUM_0
#define BOTTOM_I2C_ADDR 126
#define BOTTOM_PIN_INTERUPT GPIO_NUM_13
#define DELTA_BIT_KP 0
#define DELTA_BIT_BUTTON_SWITCH 1
#define DELTA_BIT_TOUCH 2
/// @brief An enum for the possible keypad buttons.
typedef enum {
kd = 0,
pound = 1,
k0 = 2,
star = 3,
kc = 4,
k9 = 5,
k8 = 6,
k7 = 7,
kb = 8,
k6 = 9,
k5 = 10,
k4 = 11,
ka = 12,
k3 = 13,
k2 = 14,
k1 = 15,
} KeypadKey;
/// @brief An enum for the possible buttons.
typedef enum {
b1 = 0,
b2 = 1,
b3 = 2,
b4 = 3,
button_green = 0,
button_red = 1,
button_yellow = 2,
button_blue = 3,
} ButtonKey;
/// @brief An enum for the possible switches.
typedef enum {
s1 = 0,
s2 = 1,
s3 = 2,
s4 = 3,
} SwitchKey;
/// @brief Initializes communication with the bottom half.
void init_bottom_half();
/// Clears all pending pressed/released switches/buttons/keys/touch sensors.
void clear_all_pressed_released();
/// @brief Gets the key that was just pressed (if any)
/// @param kp an OUT variable for the key that was pressed (if any)
/// @return true if there was a key that was just pressed
bool get_keypad_pressed(KeypadKey* kp);
/// @brief Gets the key that was just released (if any)
/// @param kp an OUT variable for the key that was released (if any)
/// @return true if there was a key that was just released
bool get_keypad_released(KeypadKey* kp);
/// @brief Converts a `KeypadKey` to a char
/// @param kp The value to convert
/// @return The char representing the key
char char_of_keypad_key(KeypadKey kp);
// TODO: add a get_keypad state?
/// @brief Gets the button that was just pressed (if any)
/// @param button an OUT variable for the button that was pressed (if any)
/// @return true if there was a button that was just pressed
bool get_button_pressed(ButtonKey* button);
/// @brief Gets the button that was just released (if any)
/// @param button an OUT variable for the button that was released (if any)
/// @return true if there was a button that was just released
bool get_button_released(ButtonKey* button);
/// @brief Gets the raw state of the buttons b1-b4 as bitflags.
/// B1 is MSB and B4 is LSB.
/// @return Bitflags for b1-b4.
uint8_t get_button_state();
/// @brief Gets the switch that was just flipped up (if any)
/// @param switch_ an OUT variable for the switch that was flipped up (if any)
/// @return true if there was a switch that was just flipped up
bool get_switch_flipped_up(SwitchKey* switch_);
/// @brief Gets the switch that was just flipped down (if any)
/// @param switch_ an OUT variable for the switch that was flipped down (if any)
/// @return true if there was a switch that was just flipped down
bool get_switch_flipped_down(SwitchKey* switch_);
/// @brief Gets the switch that was just flipped (if any)
/// @param switch_ an OUT variable for the switch that was flipped (if any)
/// @return true if there was a switch that was just flipped
bool get_switch_flipped(SwitchKey* switch_);
/// @brief Gets the raw state of the switches s1-s4 as bitflags.
/// S1 is MSB and S4 is LSB.
/// @return Bitflags for s1-s4.
uint8_t get_switch_state();
/// @brief Gets the switch that was just touched (if any)
/// @param switch_ an OUT variable for the switch that was touched (if any)
/// @return true if there was a switch that was just touched
bool get_switch_touch_pressed(SwitchKey* switch_);
/// @brief Gets the switch that was just un-touched (if any)
/// @param switch_ an OUT variable for the switch that was un-touched (if any)
/// @return true if there was a switch that was just un-touched
bool get_switch_touch_released(SwitchKey* switch_);
/// @brief Gets the raw state of the touched switches s1-s4 as bitflags.
/// S1 is MSB and S4 is LSB.
/// @return Bitflags for the touched state of S1-S4.
uint8_t get_switch_touch_state();
/// @brief Gets the state of the fingerprint sensor
/// @return true if the fingerprint sensor is touched
bool get_touch_state();
/// @brief Gets whether or not the touch sensor was just pressed
/// @return true if the touch sensor was just pressed
bool get_touch_pressed();
/// @brief Gets whether or not the touch sensor was just released
/// @return true if the touch sensor was just released
bool get_touch_released();
/// @brief A helper function for internal use.
///
/// Takes one key from the bitfield and sets the `kp` variable accordingly if the bitfield is not 0.
/// @param kp Out. The keypad key to set.
/// @param keypad_bitfield A pointer to the keypad bitfield to take a key from
/// @return true if a key was taken from the bitfield
bool take_key(KeypadKey* kp, uint16_t* keypad_bitfield);
// TODO: add touch sensor for switch
#endif /* BOTTOM_HALF_HPP */