Reimplement modifiers as bit operations

This commit is contained in:
Cat /dev/Nulo 2021-12-23 01:11:44 -03:00
parent 8fefc8cee6
commit 112ee7b90a
4 changed files with 25 additions and 27 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
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

View file

@ -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

View file

@ -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.

View file

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