From c9b5ce175c01fe988e260aab46b5f977b637d753 Mon Sep 17 00:00:00 2001 From: Nulo Date: Thu, 23 Dec 2021 13:21:54 -0300 Subject: [PATCH] Revert "Revert "Reimplement modifiers as bit operations"" This reverts commit 49e2eb410bc54147bd9817da6f1467a85776e9fb. --- Makefile | 2 +- keycodes.scm | 7 +++---- menelaus.scm | 37 +++++++++++++++---------------------- usb_keyboard.c | 6 ++++++ 4 files changed, 25 insertions(+), 27 deletions(-) diff --git a/Makefile b/Makefile index 45ee728..18991c7 100644 --- a/Makefile +++ b/Makefile @@ -32,7 +32,7 @@ $(LAYOUT).s: $(LAYOUT).scm menelaus.scm keycodes.scm avr-gcc -mmcu=$(MCU) -o $(LAYOUT).elf $(LAYOUT).s usb_keyboard.s usb_keyboard.s: usb_keyboard.h usb_keyboard.c - avr-gcc -std=gnu99 -S -D F_CPU=$(F_CPU)UL -mmcu=$(MCU) -c \ + avr-gcc -lm -std=gnu99 -S -D F_CPU=$(F_CPU)UL -mmcu=$(MCU) -c \ -o usb_keyboard.s usb_keyboard.c udev: /etc/udev/rules.d/a-star.rules diff --git a/keycodes.scm b/keycodes.scm index 39234d4..9ceaa39 100644 --- a/keycodes.scm +++ b/keycodes.scm @@ -101,12 +101,11 @@ (define (combo modifier keycode) (list (car modifier) keycode)) (define (uncombo keycode) (and (= 2 (length keycode)) (car (cdr keycode)))) -;; We're treating these a little differently; they are not literal USB values. (define mod-ctrl (modify 1)) (define mod-shift (modify 2)) -(define mod-alt (modify 3)) -(define mod-super (modify 4)) -(define mod-altgr (modify 5)) +(define mod-alt (modify 4)) +(define mod-super (modify 8)) +(define mod-altgr (modify 64)) (define (sft keycode) (combo mod-shift keycode)) ; shorthand (define (altgr keycode) (combo mod-altgr keycode)) ; shorthand diff --git a/menelaus.scm b/menelaus.scm index c43830d..1476fbe 100644 --- a/menelaus.scm +++ b/menelaus.scm @@ -245,13 +245,13 @@ ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Vectors to store keycodes for the USB frame we are preparing to send. -(define modifiers (vector 0 0 0 0 0)) -(define sticky-modifiers (vector 0 0 0 0 0)) +(define modifiers 0) +(define sticky-modifiers 0) (define keycodes-down (vector 0 0 0 0 0 0)) ;; For each element of the keycodes-down or modifiers vector, which physical ;; key caused it to be pressed? -(define keys-for-modifiers (vector #f #f #f #f #f)) +(define keys-for-modifiers (vector #f #f #f #f #f #f #f #f)) (define keys-for-frame (vector #f #f #f #f #f #f)) ;; Given a physical key index, what keycode does it map to in the layout? @@ -261,13 +261,13 @@ ;; Record that a given key resulted in a specific modifier press. (define (press-modifier keycode key) - (vector-set! modifiers (- keycode 1) 1) - (vector-set! keys-for-modifiers (- keycode 1) key)) + (set! modifiers (| modifiers keycode)) + (vector-set! keys-for-modifiers (call-c-func "mathlog2" keycode) key)) (define (press-sticky-modifier keycode) - (vector-set! sticky-modifiers (- keycode 1) 1)) + (set! sticky-modifiers (| sticky-modifiers keycode))) (define (release-sticky-modifier keycode) - (vector-set! sticky-modifiers (- keycode 1) 0)) + (set! sticky-modifiers (^ sticky-modifiers keycode))) ;; Record that a given key resulted in a specific non-modifier press. (define (press-normal-key keycode key) @@ -288,13 +288,7 @@ (press-modifier (unmodify keycode) key) (press-key-aux (uncombo keycode) key))) (press-modifier (unmodify keycode) key)) - (let ((sticky (find sticky-modifiers 1))) - (if sticky - (begin - (press-modifier (+ sticky 1) key) - (release-sticky-modifier (+ sticky 1))) - #f) - (press-normal-key keycode key))))) + (press-normal-key keycode key)))) ;; Record a key press in the modifiers/keycodes-down vectors for the layout. @@ -306,9 +300,9 @@ (define (release-modifier keycode key n) (if (= (or (vector-ref keys-for-modifiers n) (- 0 1)) key) (begin - (vector-set! modifiers n 0) + (set! modifiers (^ modifiers (unmodify keycode))) (vector-set! keys-for-modifiers n #f)) - (and (< n 4) (release-modifier keycode key (+ n 1))))) + (and (< n 7) (release-modifier keycode key (+ n 1))))) ;; Record a key release, clearing it out of the press tracking data. (define (release-key key) @@ -327,7 +321,7 @@ #f) (if modifier-slot (begin - (release-modifier modifier-slot key 0) + (release-modifier keycode key 0) (release-key key)) #f))))) @@ -349,10 +343,8 @@ ;; Actually send the USB frame. (define (usb-send m k0 k1 k2 k3 k4 k5) - (let ((mods (+ (vector-ref m 0) (* (vector-ref m 1) 2)))) ; + isn't variadic - (let ((mods (+ mods (+ (+ (* (vector-ref m 2) 4) (* (vector-ref m 3) 8)) (* (vector-ref m 4) 64))))) - ;; call-c-func is a special form and cannot be applied - (call-c-func "usb_send" mods k0 k1 k2 k3 k4 k5)))) + ;; call-c-func is a special form and cannot be applied + (call-c-func "usb_send" (| m sticky-modifiers) k0 k1 k2 k3 k4 k5)) ;; Scan the matrix, determine the appropriate keycodes, and send them. (define (loop) @@ -364,7 +356,8 @@ ;; macro in one fell swoop. Primitive, but effective. (free! (let ((keys-scanned (debounce-matrix))) (set-usb-frame (press/release-for keys-scanned)) - (apply usb-send (cons modifiers (vector->list keycodes-down))))) + (apply usb-send (cons modifiers (vector->list keycodes-down))) + (if (> (vector-ref keycodes-down 0) 0) (set! sticky-modifiers 0) #f))) (loop)) ;; Prepare the GPIO pins. diff --git a/usb_keyboard.c b/usb_keyboard.c index 0f6e8e8..fd1d6ee 100644 --- a/usb_keyboard.c +++ b/usb_keyboard.c @@ -28,6 +28,12 @@ #include "usb_keyboard.h" #include +#include + +int16_t mathlog2(int x) { + return round(log(x)/log(2)); +} + /************************************************************************** * * Configurable Options