From 58b9c37bf4479c436e3477e1753e48527276a595 Mon Sep 17 00:00:00 2001 From: Phil Hagelberg Date: Mon, 1 Jul 2019 19:59:42 -0700 Subject: [PATCH] Scan all the things. 6kro but no modifiers. --- README.md | 2 +- keycodes.scm | 16 ++++------- menelaus.scm | 78 ++++++++++++++++++++++++++++++++++++++++------------ 3 files changed, 66 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index c997a9d..2870c95 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,6 @@ for Teensy models will be added later. ## License -Copyright © 2014 Phil Hagelberg and contributors +Copyright © 2014-2019 Phil Hagelberg and contributors Released under the [GNU GPL version 3](https://www.gnu.org/licenses/gpl.html). diff --git a/keycodes.scm b/keycodes.scm index c461fa2..7b70f9e 100644 --- a/keycodes.scm +++ b/keycodes.scm @@ -40,14 +40,8 @@ (define key-tab 43) (define key-enter 40) -(define mod-ctrl 0) -(define mod-shift 0) -(define mod-alt 0) -(define mod-gui 0) -(define mod-fn 0) - -;; (define mod-ctrl #x01) -;; (define mod-shift #x02) -;; (define mod-alt #x04) -;; (define mod-gui #x08) -;; (define mod-fn #x16) +(define mod-ctrl #x01) +(define mod-shift #x02) +(define mod-alt #x04) +(define mod-gui #x08) +(define mod-fn #x16) diff --git a/menelaus.scm b/menelaus.scm index ac5caf1..d656907 100644 --- a/menelaus.scm +++ b/menelaus.scm @@ -4,29 +4,71 @@ (define rows (list 0 1 2 3)) (define row-pins (vector 3 2 1 0)) (define columns (list 0 1 2 3 4 5 6 7 8 9 10)) -(define column-pins (vector 11 12 18 19 10 4 7 8 9 5 6)) +(define column-pins (vector 6 5 9 8 7 4 10 19 18 12 11)) -(for-each-vector output row-pins) -(for-each-vector high row-pins) -(for-each-vector input column-pins) -(for-each-vector high column-pins) ; activate pullup resistors +(define max-keys 6) -(call-c-func "usb_init") -(pause 200) - -(define (scan-column last offset) - (if (low? (vector-ref column-pins (mod offset 11))) - (vector-ref layout offset) - last)) - -(define (scan-row last row) +(define (init) + (for-each-vector output row-pins) (for-each-vector high row-pins) - (low (vector-ref row-pins row)) - (fold scan-column last (map (lambda (col) (+ col (* row 11))) columns))) + (for-each-vector input column-pins) + (for-each-vector high column-pins) ; activate pullup resistors + + (call-c-func "usb_init") + (pause 200)) + +(define (offset-for row col) + (+ col (* row (length columns)))) + +(define (usb-send modifiers key1 key2 key3 key4 key5 key6) + (call-c-func "usb_send" modifiers key1 key2 key3 key4 key5 key6)) + +(define (scan-key keys-pressed key-count row col) + ;; pullup resistors mean a closed circuit is low rather than high + (if (low? (vector-ref column-pins col)) + (begin + (if (<= key-count max-keys) + (vector-set! keys-pressed key-count (offset-for row col)) + #f) + (+ key-count 1)) + key-count)) + +(define (scan-column keys-pressed key-count row columns-left) + (if (= (length columns-left) 0) + key-count + (let ((key-count (scan-key keys-pressed key-count row (car columns-left)))) + (scan-column keys-pressed key-count row (cdr columns-left))))) + +(define (activate-row row) + (for-each-vector high row-pins) + (low (vector-ref row-pins row))) + +(define (scan-matrix keys-pressed key-count rows-left) + (if (= (length rows-left) 0) + key-count + (let ((_ (activate-row (car rows-left))) + (key-count (scan-column keys-pressed key-count + (car rows-left) columns))) + (scan-matrix keys-pressed key-count (cdr rows-left))))) + +(define (layout-lookup key-position) + (vector-ref layout key-position)) + +(define (keycodes-for keys-pressed key-count keycodes) + (if (= 0 key-count) + (vector->list keycodes) + (let ((keycode (vector-ref layout (vector-ref keys-pressed key-count)))) + ;; (printf "keycode ~s ~s~n" keycode key-count) + (vector-set! keycodes key-count keycode) + (keycodes-for keys-pressed (- key-count 1) keycodes)))) (define (loop) - (let ((pressed (free! (fold scan-row 0 rows)))) - (call-c-func "usb_send" 0 pressed 0 0 0 0 0)) + (free! (let ((keys-pressed (vector 0 0 0 0 0 0 0))) + (let ((key-count (scan-matrix keys-pressed 1 rows))) + (apply usb-send (keycodes-for keys-pressed (- key-count 1) + (vector 0 0 0 0 0 0 0)))))) (loop)) +;;; showtime! +(init) (loop)