From f900f592d4ddc324631ed37b4a9e985e55c6f21a Mon Sep 17 00:00:00 2001 From: Ashelyn Rose Date: Fri, 4 Jun 2021 01:18:30 -0600 Subject: [PATCH] Convert pomodoro to rust --- compile.sh | 2 +- keymap/keymap.c | 26 ++----- keymap/pomodoro.c | 92 ------------------------ keymap/pomodoro.h | 26 +++---- keymap/pomodoro.rs | 169 +++++++++++++++++++++++++++++++++++++++++++++ keymap/rules.mk | 4 +- keymap/test.rs | 14 ---- 7 files changed, 187 insertions(+), 146 deletions(-) delete mode 100644 keymap/pomodoro.c create mode 100644 keymap/pomodoro.rs delete mode 100644 keymap/test.rs diff --git a/compile.sh b/compile.sh index 5acb399..30366da 100644 --- a/compile.sh +++ b/compile.sh @@ -2,7 +2,7 @@ set -e cd /qmk_firmware/ -rustc --emit=obj --target=thumbv7em-none-eabihf --codegen panic=abort -o test.o ./keyboards/massdrop/alt/keymaps/ashe/test.rs +rustc -O --emit=obj --target=thumbv7em-none-eabihf --codegen panic=abort -o pomodoro.o ./keyboards/massdrop/alt/keymaps/ashe/pomodoro.rs cd /qmk_firmware qmk compile -kb massdrop/alt -km ashe diff --git a/keymap/keymap.c b/keymap/keymap.c index 814d10d..52470f9 100644 --- a/keymap/keymap.c +++ b/keymap/keymap.c @@ -235,36 +235,25 @@ bool process_record_user(uint16_t keycode, keyrecord_t *record) { // Pomodoro new, play / payse case ADP_NEW: if (!record->event.pressed) return false; - if (current_pom_state == RUNNING) - current_pom_state = PAUSED; - else if (current_pom_state == PAUSED) - current_pom_state = RUNNING; - else if (current_pom_state == STOPPED) { - current_pom_state = RUNNING; - pom_current_secs = 0; - pom_second_start = 0; - } + new_pom(); return false; // Pomodoro cancel case ADP_CCL: if (!record->event.pressed) return false; - current_pom_state = STOPPED; - pom_current_secs = 0; - pom_second_start = 0; + cancel_pom(); return false; // Pomodoro delete case ADP_DEL: if (!record->event.pressed) return false; - current_pom_state = STOPPED; - poms_completed = 0; + delete_pom(); return false; // Show / hide case ADP_HID: if (!record->event.pressed) return false; - pom_visible = !pom_visible; + show_pom(); return false; case U_T_AUTO: @@ -348,12 +337,7 @@ void rgb_matrix_indicators_user(void) { set_layer_color(get_highest_layer(layer_state)); - if(current_pom_state == RUNNING) - count_pom(); - - if(current_pom_state != STOPPED || poms_completed > 0){ - draw_pom(); - } + tick_pom(); } const uint16_t NUMPAD_CODES[] = { diff --git a/keymap/pomodoro.c b/keymap/pomodoro.c deleted file mode 100644 index a6bf602..0000000 --- a/keymap/pomodoro.c +++ /dev/null @@ -1,92 +0,0 @@ -#include "keymap.h" -#include "pomodoro.h" -#include "lighting.h" - -#define POM_CURRENT_INDEX 68 -#define POM_LIGHT_COUNT 9 -#define POM_COMPLETE_INDEX POM_CURRENT_INDEX + POM_LIGHT_COUNT + 1 -#define POM_DURATION 1500 /* 25 * 60 */ - -uint8_t test_function(void); - -void draw_pom(void) { - static bool blink_on; - static short last_pos; - - const uint8_t test_val = test_function(); - - const uint8_t complete_hsv[3] = {HSV_TEAL}; - const uint8_t current_hsv[3] = {HSV_GOLDENROD}; - const uint8_t hidden_hsv[3] = C_MODI; - - // Clear row - if (pom_visible) { - if (current_pom_state != STOPPED) { - for(int i = POM_CURRENT_INDEX; i < POM_CURRENT_INDEX + POM_LIGHT_COUNT; i++) - hsv_matrix_set_color(i, (uint8_t[3]){0, 0, 0}); - } - - for(int i = POM_COMPLETE_INDEX; i < POM_COMPLETE_INDEX + 3; i++) - hsv_matrix_set_color(i, (uint8_t[3]){0, 0, 0}); - } - - // Draw completed - if (pom_visible) { - for(int i = POM_COMPLETE_INDEX; i < POM_COMPLETE_INDEX + poms_completed && i < POM_COMPLETE_INDEX + 3; i++ ) { - hsv_matrix_set_color(i, complete_hsv); - } - } else if (poms_completed > 0 && poms_completed <= 3) { - const short completed_position = POM_COMPLETE_INDEX + poms_completed - 1; - hsv_matrix_set_color(completed_position, hidden_hsv); - } - - hsv_matrix_set_color(POM_COMPLETE_INDEX + test_val - 1, complete_hsv); - - if (current_pom_state == STOPPED) - return; - - const short current_pos = POM_CURRENT_INDEX + (short)((float)pom_current_secs / POM_DURATION * POM_LIGHT_COUNT); - - if(last_pos != current_pos) { - last_pos = current_pos; - blink_on = true; - } else if (pom_second_start == 0) { - blink_on = !blink_on; - } - - // Current solid - if (pom_visible) { - for(int i = POM_CURRENT_INDEX; i < current_pos; i++) { - hsv_matrix_set_color(i, current_hsv); - } - } - - // Current blinking - if (blink_on) { - if (pom_visible) { - hsv_matrix_set_color(current_pos, current_hsv); - } else { - hsv_matrix_set_color(current_pos, hidden_hsv); - } - } -} - -void count_pom(void) { - // Start ms counter - if(pom_second_start == 0) - pom_second_start = timer_read32(); - - // Check if second completed - if (timer_elapsed32(pom_second_start) > 1000) { - pom_second_start = 0; - pom_current_secs++; - } - - // Check if pom completed - if (pom_current_secs >= POM_DURATION) { - poms_completed++; - current_pom_state = STOPPED; - pom_current_secs = 0; - pom_second_start = 0; - } -} \ No newline at end of file diff --git a/keymap/pomodoro.h b/keymap/pomodoro.h index 6074551..80bea56 100644 --- a/keymap/pomodoro.h +++ b/keymap/pomodoro.h @@ -1,18 +1,12 @@ #pragma once -enum pom_state { - STOPPED, - RUNNING, - PAUSED -}; - -enum pom_state current_pom_state; -bool pom_visible; -short poms_completed; -short pom_current_secs; -uint32_t pom_second_start; - -void draw_pom(void); -void count_pom(void); - -uint8_t test_function(void); \ No newline at end of file +const uint8_t complete_hsv[3] = {HSV_TEAL}; +const uint8_t current_hsv[3] = {HSV_GOLDENROD}; +const uint8_t hidden_hsv[3] = C_MODI; +const uint8_t blank_hsv[3] = {0, 0, 0}; + +void tick_pom(void); +void new_pom(void); +void cancel_pom(void); +void delete_pom(void); +void show_pom(void); diff --git a/keymap/pomodoro.rs b/keymap/pomodoro.rs new file mode 100644 index 0000000..95d829d --- /dev/null +++ b/keymap/pomodoro.rs @@ -0,0 +1,169 @@ +#![no_std] +#![no_builtins] +#![crate_type = "staticlib"] +#![allow(dead_code)] + +#[panic_handler] +fn my_panic(_info: &core::panic::PanicInfo) -> ! { + loop {} +} + +#[derive(PartialEq, Copy, Clone)] +enum PomState { + STOPPED, + RUNNING, + PAUSED +} + +const POM_DURATION : u16 = 1500; +const POM_CURRENT_INDEX : u32 = 68; +const POM_LIGHT_COUNT : u32 = 9; +const POM_COMPLETE_INDEX : u32 = POM_CURRENT_INDEX + POM_LIGHT_COUNT + 1; + +static mut current_pom_state : PomState = PomState::STOPPED; +static mut pom_visible : bool = false; +static mut poms_completed : u32 = 0; +static mut pom_current_secs : u16 = 0; +static mut pom_second_start : u32 = 0; +static mut blink_on : bool = false; +static mut last_pos : u32 = 0; + +extern "C" { + pub static complete_hsv : [u32; 3usize]; + pub static current_hsv : [u32; 3usize]; + pub static hidden_hsv : [u32; 3usize]; + pub static blank_hsv : [u32; 3usize]; + + fn timer_read32() -> u32; + fn timer_elapsed32(prev : u32) -> u32; + fn hsv_matrix_set_color(i : u32, color : *const [u32; 3usize]); +} + +#[no_mangle] +pub unsafe extern "C" fn new_pom() { + let previous_pom_state = current_pom_state; + + match previous_pom_state { + PomState::RUNNING => { + current_pom_state = PomState::PAUSED; + }, + PomState::PAUSED => { + current_pom_state = PomState::RUNNING; + }, + PomState::STOPPED => { + current_pom_state = PomState::RUNNING; + pom_current_secs = 0; + pom_second_start = 0; + } + } +} + +#[no_mangle] +pub unsafe extern "C" fn cancel_pom() { + current_pom_state = PomState::STOPPED; + pom_current_secs = 0; + pom_second_start = 0; +} + +#[no_mangle] +pub unsafe extern "C" fn delete_pom() { + cancel_pom(); + poms_completed = 0; +} + +#[no_mangle] +pub unsafe extern "C" fn show_pom() { + pom_visible = !pom_visible; +} + +#[no_mangle] +pub unsafe extern "C" fn tick_pom() { + if current_pom_state == PomState::RUNNING { + count_pom(); + } + + if current_pom_state != PomState::STOPPED || poms_completed > 0 { + draw_pom(); + } +} + +unsafe fn count_pom() { + if pom_second_start == 0 { + pom_second_start = timer_read32(); + } + + + if timer_elapsed32(pom_second_start) > 1000 { + pom_second_start = 0; + pom_current_secs += 1; + } + + if pom_current_secs >= POM_DURATION { + poms_completed += 1; + current_pom_state = PomState::STOPPED; + pom_current_secs = 0; + pom_second_start = 0; + } +} + +unsafe fn draw_pom() { + let current_pos = POM_CURRENT_INDEX + ((pom_current_secs as f32 / POM_DURATION as f32) * POM_LIGHT_COUNT as f32) as u32; + let completed_pos = POM_COMPLETE_INDEX + poms_completed - 1; + + // Clear row + if pom_visible { + if current_pom_state != PomState::STOPPED { + let mut i = POM_CURRENT_INDEX; + while i < POM_CURRENT_INDEX + POM_LIGHT_COUNT { + hsv_matrix_set_color(i, &blank_hsv); + i += 1; + } + } + + let mut i = POM_COMPLETE_INDEX; + while i < POM_COMPLETE_INDEX + 3 { + hsv_matrix_set_color(i, &blank_hsv); + i += 1; + } + } + + // Draw completed + if pom_visible { + let mut i = POM_COMPLETE_INDEX; + while i < POM_COMPLETE_INDEX + poms_completed { + hsv_matrix_set_color(i, &complete_hsv); + i += 1; + } + } else if poms_completed > 0 && poms_completed <= 3 { + hsv_matrix_set_color(completed_pos, &hidden_hsv); + } + + if current_pom_state == PomState::STOPPED { + return; + } + + if last_pos != current_pos { + last_pos = current_pos; + blink_on = true; + } else if pom_second_start == 0 { + blink_on = !blink_on; + } + + // Current solid + if pom_visible { + let mut i = POM_CURRENT_INDEX; + while i < current_pos { + hsv_matrix_set_color(i, ¤t_hsv); + i += 1; + } + } + + // Current blinking + if blink_on { + if pom_visible { + hsv_matrix_set_color(current_pos, ¤t_hsv); + } else { + hsv_matrix_set_color(current_pos, &hidden_hsv); + } + } +} \ No newline at end of file diff --git a/keymap/rules.mk b/keymap/rules.mk index 70b4561..d6544e8 100644 --- a/keymap/rules.mk +++ b/keymap/rules.mk @@ -14,5 +14,5 @@ EXTRAKEY_ENABLE = yes # Audio control and System control # COMBO_ENABLE # Key combo feature # LEADER_ENABLE # Enable leader key chording -SRC += lighting_funcs.c pomodoro.c -LIB_SRC += test.o \ No newline at end of file +SRC += lighting_funcs.c +LIB_SRC += pomodoro.o \ No newline at end of file diff --git a/keymap/test.rs b/keymap/test.rs deleted file mode 100644 index 80944f5..0000000 --- a/keymap/test.rs +++ /dev/null @@ -1,14 +0,0 @@ -#![no_std] -#![no_builtins] -#![crate_type = "staticlib"] -#![allow(dead_code)] - -#[panic_handler] -fn my_panic(_info: &core::panic::PanicInfo) -> ! { - loop {} -} - -#[no_mangle] -pub extern "C" fn test_function() -> u8 { - 2 -} \ No newline at end of file