Add basic roll counters

This commit is contained in:
Frederik Walk 2023-07-16 16:48:08 +02:00
parent d81dd41d79
commit 71b17d4daa
3 changed files with 48 additions and 0 deletions

View File

@ -94,6 +94,8 @@ const Peripherals::StatusLed::Config led_config = {
const Peripherals::Display::Config display_config = { const Peripherals::Display::Config display_config = {
i2c_config.block, // Block i2c_config.block, // Block
0x3C, // Address 0x3C, // Address
500, // Roll Counter Timeout in Milliseconds
}; };
} // namespace Default } // namespace Default

View File

@ -19,6 +19,8 @@ class Display {
struct Config { struct Config {
i2c_inst_t *i2c_block; i2c_inst_t *i2c_block;
uint8_t i2c_address; uint8_t i2c_address;
uint32_t roll_counter_timeout_ms;
}; };
private: private:

View File

@ -201,6 +201,41 @@ static std::string modeToString(usb_mode_t mode) {
return "?"; return "?";
} }
static std::vector<uint16_t> updateRollCounters(const Utils::InputState::Drum &drum, const uint32_t timeout_ms) {
struct CounterState {
uint32_t last_triggered_time;
bool last_state;
uint16_t count;
};
static CounterState ka_left_state = {0, false, 0};
static CounterState don_left_state = {0, false, 0};
static CounterState don_right_state = {0, false, 0};
static CounterState ka_right_state = {0, false, 0};
uint32_t now = to_ms_since_boot(get_absolute_time());
auto check_roll = [&](CounterState &target, bool current_state) {
if (current_state && (target.last_state != current_state)) {
if ((now - target.last_triggered_time) > timeout_ms) {
target.count = 0;
}
target.last_triggered_time = now;
target.count++;
}
target.last_state = current_state;
};
check_roll(ka_left_state, drum.ka_left.triggered);
check_roll(don_left_state, drum.don_left.triggered);
check_roll(don_right_state, drum.don_right.triggered);
check_roll(ka_right_state, drum.ka_right.triggered);
return {ka_left_state.count, don_left_state.count, don_right_state.count, ka_right_state.count};
}
void Display::drawIdleScreen() { void Display::drawIdleScreen() {
// Header // Header
std::string mode_string = modeToString(m_usb_mode) + " mode"; std::string mode_string = modeToString(m_usb_mode) + " mode";
@ -222,6 +257,15 @@ void Display::drawIdleScreen() {
if (m_input_state.drum.ka_right.triggered) { if (m_input_state.drum.ka_right.triggered) {
ssd1306_bmp_show_image_with_offset(&m_display, ka_r_bmp.data(), ka_r_bmp.size(), 64 - 26, 12); ssd1306_bmp_show_image_with_offset(&m_display, ka_r_bmp.data(), ka_r_bmp.size(), 64 - 26, 12);
} }
// Roll counters
auto roll_counters = updateRollCounters(m_input_state.drum, m_config.roll_counter_timeout_ms);
int num = 1;
for (const auto &counter : roll_counters) {
ssd1306_draw_string(&m_display, 2, 12 * num, 1, std::to_string(counter).c_str());
num++;
}
// Player "LEDs" // Player "LEDs"
if (m_player_id != 0) { if (m_player_id != 0) {
for (uint8_t i = 0; i < 4; ++i) { for (uint8_t i = 0; i < 4; ++i) {