blk_box_lib/include/blk_box_drivers/char_lcd.hpp
2026-04-04 11:38:16 -05:00

105 lines
3.6 KiB
C++

#ifndef CHAR_LCD_HPP
#define CHAR_LCD_HPP
#include <cstdint>
#define LCD_ADDR 0x27
#define LCD_ROWS 4
#define LCD_COLS 20
/// A possible display mode for the cursor.
enum class CursorMode: uint8_t {
/// The cursor is not displayed.
Hide = 0b00,
/// The cursor is displayed as an underline.
Show = 0b01,
/// The cursor is displayed as a blinking block.
Blink = 0b11,
};
/// Initializes the 2004 Character LCD
void init_lcd();
class LCDController {
public:
/// Clears the display and resets the cursor to the home position (0, 0).
static void clear();
/// Gets the backlight state of the LCD.
static bool get_backlight();
/// Sets the backlight state of the LCD.
static void set_backlight(bool backlight);
/// Shows or hides the display.
static void set_display_show(bool show_display);
/// Gets the display state of the LCD.
static bool get_display_show();
/// Moves the position of the resting cursor.
///
/// If the resting cursor mode is `Hide`, then this position has no effect on the display, but is still stored
/// for when it is put into resting cursor mode.
static void set_resting_cursor_pos(uint8_t row, uint8_t col);
/// Gets the position of the resting cursor.
///
/// This will return the value of the resting cursor position even if the resting cursor mode is `Hide`
/// even though the values are not meaningful during that time.
static void get_cursor_resting_position(uint8_t* row, uint8_t* col);
/// This puts the display in and out of resting cursor mode.
///
/// If the resting mode is not `Hide`, then the cursor will be displayed in the resting position when not printing.
///
/// The cursor mode will change to the "cursor print mode"
/// during prints, then return to it's resting location and
/// switch back to the "cursor resting mode".
static void set_resting_cursor_mode(CursorMode new_mode);
/// Gets the display mode of the cursor when it is resting.
static CursorMode get_resting_cursor_mode();
/// Sets the display mode of the cursor during printing.
static void set_cursor_print_mode(CursorMode new_mode);
/// Gets the display mode of the cursor during printing.
static CursorMode get_cursor_print_mode();
/// Sets the character map for a custom character.
///
/// There are 8 locations (1..=8), for 8 characters.
/// You can print these characters by sending '\x01' - '\x08'.
static void create_custom_char(uint8_t location, const uint8_t charmap[]);
/// Prints a string to the given row and column.
///
/// Do not print across lines, as that leads to goofy behavior.
static void print(uint8_t row, uint8_t col, const char* str);
/// Enables or disables the header row.
///
/// Try to keep it enabled, since its used as a status display and shows the starcode the user is typing.
///
/// But can be disabled if needed.
static void set_lcd_header_enabled(bool enable);
/// Gets whether the header row is enabled.
static bool header_enabled();
/// Prints the LCD header. Usually, you will not need to call this manually.
static void print_header();
/// Locks the LCD to allow you to perform multiple operations uninterrupted.
///
/// Every lock should have an unlock. Do not hold the lock for too long.
///
/// This will wait up to `ticks_to_wait` ticks to acquire the lock, and return `false` if it fails to acquire the lock within that time.
static bool lock(uint32_t ticks_to_wait);
/// Unlocks the LCD after a lock.
static void unlock();
};
#endif /* CHAR_LCD_HPP */