Revert "Reimplement modifiers as bit operations"

This reverts commit 112ee7b90a.
This commit is contained in:
Cat /dev/Nulo 2021-12-23 11:49:01 -03:00
parent 112ee7b90a
commit 49e2eb410b
4 changed files with 27 additions and 25 deletions

View file

@ -32,7 +32,7 @@ $(LAYOUT).s: $(LAYOUT).scm menelaus.scm keycodes.scm
avr-gcc -mmcu=$(MCU) -o $(LAYOUT).elf $(LAYOUT).s usb_keyboard.s avr-gcc -mmcu=$(MCU) -o $(LAYOUT).elf $(LAYOUT).s usb_keyboard.s
usb_keyboard.s: usb_keyboard.h usb_keyboard.c usb_keyboard.s: usb_keyboard.h usb_keyboard.c
avr-gcc -lm -std=gnu99 -S -D F_CPU=$(F_CPU)UL -mmcu=$(MCU) -c \ avr-gcc -std=gnu99 -S -D F_CPU=$(F_CPU)UL -mmcu=$(MCU) -c \
-o usb_keyboard.s usb_keyboard.c -o usb_keyboard.s usb_keyboard.c
udev: /etc/udev/rules.d/a-star.rules udev: /etc/udev/rules.d/a-star.rules

View file

@ -101,11 +101,12 @@
(define (combo modifier keycode) (list (car modifier) keycode)) (define (combo modifier keycode) (list (car modifier) keycode))
(define (uncombo keycode) (and (= 2 (length keycode)) (car (cdr 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-ctrl (modify 1))
(define mod-shift (modify 2)) (define mod-shift (modify 2))
(define mod-alt (modify 4)) (define mod-alt (modify 3))
(define mod-super (modify 8)) (define mod-super (modify 4))
(define mod-altgr (modify 64)) (define mod-altgr (modify 5))
(define (sft keycode) (combo mod-shift keycode)) ; shorthand (define (sft keycode) (combo mod-shift keycode)) ; shorthand
(define (altgr keycode) (combo mod-altgr keycode)) ; shorthand (define (altgr keycode) (combo mod-altgr keycode)) ; shorthand

View file

@ -245,13 +245,13 @@
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Vectors to store keycodes for the USB frame we are preparing to send. ;; Vectors to store keycodes for the USB frame we are preparing to send.
(define modifiers 0) (define modifiers (vector 0 0 0 0 0))
(define sticky-modifiers 0) (define sticky-modifiers (vector 0 0 0 0 0))
(define keycodes-down (vector 0 0 0 0 0 0)) (define keycodes-down (vector 0 0 0 0 0 0))
;; For each element of the keycodes-down or modifiers vector, which physical ;; For each element of the keycodes-down or modifiers vector, which physical
;; key caused it to be pressed? ;; key caused it to be pressed?
(define keys-for-modifiers (vector #f #f #f #f #f #f #f #f)) (define keys-for-modifiers (vector #f #f #f #f #f))
(define keys-for-frame (vector #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? ;; 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. ;; Record that a given key resulted in a specific modifier press.
(define (press-modifier keycode key) (define (press-modifier keycode key)
(set! modifiers (| modifiers keycode)) (vector-set! modifiers (- keycode 1) 1)
(vector-set! keys-for-modifiers (call-c-func "mathlog2" keycode) key)) (vector-set! keys-for-modifiers (- keycode 1) key))
(define (press-sticky-modifier keycode) (define (press-sticky-modifier keycode)
(set! sticky-modifiers (| sticky-modifiers keycode))) (vector-set! sticky-modifiers (- keycode 1) 1))
(define (release-sticky-modifier keycode) (define (release-sticky-modifier keycode)
(set! sticky-modifiers (^ sticky-modifiers keycode))) (vector-set! sticky-modifiers (- keycode 1) 0))
;; Record that a given key resulted in a specific non-modifier press. ;; Record that a given key resulted in a specific non-modifier press.
(define (press-normal-key keycode key) (define (press-normal-key keycode key)
@ -288,7 +288,13 @@
(press-modifier (unmodify keycode) key) (press-modifier (unmodify keycode) key)
(press-key-aux (uncombo keycode) key))) (press-key-aux (uncombo keycode) key)))
(press-modifier (unmodify keycode) key)) (press-modifier (unmodify keycode) key))
(press-normal-key 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)))))
;; Record a key press in the modifiers/keycodes-down vectors for the layout. ;; Record a key press in the modifiers/keycodes-down vectors for the layout.
@ -300,9 +306,9 @@
(define (release-modifier keycode key n) (define (release-modifier keycode key n)
(if (= (or (vector-ref keys-for-modifiers n) (- 0 1)) key) (if (= (or (vector-ref keys-for-modifiers n) (- 0 1)) key)
(begin (begin
(set! modifiers (^ modifiers (unmodify keycode))) (vector-set! modifiers n 0)
(vector-set! keys-for-modifiers n #f)) (vector-set! keys-for-modifiers n #f))
(and (< n 7) (release-modifier keycode key (+ n 1))))) (and (< n 4) (release-modifier keycode key (+ n 1)))))
;; Record a key release, clearing it out of the press tracking data. ;; Record a key release, clearing it out of the press tracking data.
(define (release-key key) (define (release-key key)
@ -321,7 +327,7 @@
#f) #f)
(if modifier-slot (if modifier-slot
(begin (begin
(release-modifier keycode key 0) (release-modifier modifier-slot key 0)
(release-key key)) (release-key key))
#f))))) #f)))))
@ -343,8 +349,10 @@
;; Actually send the USB frame. ;; Actually send the USB frame.
(define (usb-send m k0 k1 k2 k3 k4 k5) (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 is a special form and cannot be applied
(call-c-func "usb_send" (| m sticky-modifiers) k0 k1 k2 k3 k4 k5)) (call-c-func "usb_send" mods k0 k1 k2 k3 k4 k5))))
;; Scan the matrix, determine the appropriate keycodes, and send them. ;; Scan the matrix, determine the appropriate keycodes, and send them.
(define (loop) (define (loop)
@ -356,8 +364,7 @@
;; macro in one fell swoop. Primitive, but effective. ;; macro in one fell swoop. Primitive, but effective.
(free! (let ((keys-scanned (debounce-matrix))) (free! (let ((keys-scanned (debounce-matrix)))
(set-usb-frame (press/release-for keys-scanned)) (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)) (loop))
;; Prepare the GPIO pins. ;; Prepare the GPIO pins.

View file

@ -28,12 +28,6 @@
#include "usb_keyboard.h" #include "usb_keyboard.h"
#include <util/delay.h> #include <util/delay.h>
#include <math.h>
int16_t mathlog2(int x) {
return round(log(x)/log(2));
}
/************************************************************************** /**************************************************************************
* *
* Configurable Options * Configurable Options