Debouncing.

This commit is contained in:
Phil Hagelberg 2019-07-01 22:57:30 -07:00
parent 5b0d2c2a41
commit 66406219ac
2 changed files with 24 additions and 4 deletions

View file

@ -8,7 +8,6 @@
(define max-keys 6) ; a single USB frame can only send 6 keycodes plus modifiers (define max-keys 6) ; a single USB frame can only send 6 keycodes plus modifiers
;;; matrix ;;; matrix
(define (offset-for row col) (define (offset-for row col)
@ -43,7 +42,24 @@
(car rows-left) columns))) (car rows-left) columns)))
(scan-matrix keys-pressed key-count (cdr rows-left))))) (scan-matrix keys-pressed key-count (cdr rows-left)))))
;;; debouncing
(define this-scan (vector 0 0 0 0 0 0 0))
(define last-scan (vector 0 0 0 0 0 0 0))
(define debounce-passes 8)
(define (debounce-matrix keys-pressed last-count passes-left)
(vector-copy this-scan last-scan 0 6 0)
(if (< 0 passes-left)
(let ((this-count (scan-matrix this-scan 1 rows)))
(if (and (= this-count last-count)
(equal? this-scan last-scan))
(debounce-matrix keys-pressed this-count (- passes-left 1))
(debounce-matrix keys-pressed this-count passes-left)))
(begin (vector-copy this-scan keys-pressed 0 6 0)
last-count)))
;;; layout ;;; layout
(define (keycode-for keys-pressed which-key keycodes) (define (keycode-for keys-pressed which-key keycodes)
@ -65,7 +81,6 @@
#f) #f)
(keycodes-for keys-pressed (- key-count 1) keycodes)))) (keycodes-for keys-pressed (- key-count 1) keycodes))))
;;; showtime ;;; showtime
(define (init) (define (init)
@ -85,7 +100,7 @@
;; scanning the matrix tells us only which physical keys were ;; scanning the matrix tells us only which physical keys were
;; pressed and how many; it doesn't tell us which keycodes to ;; pressed and how many; it doesn't tell us which keycodes to
;; send yet. ;; send yet.
(let ((key-count (scan-matrix keys-pressed 1 rows))) (let ((key-count (debounce-matrix keys-pressed 1 debounce-passes)))
(apply usb-send (keycodes-for keys-pressed (- key-count 1) (apply usb-send (keycodes-for keys-pressed (- key-count 1)
(vector 0 0 0 0 0 0 0)))))) (vector 0 0 0 0 0 0 0))))))
(loop)) (loop))

View file

@ -16,6 +16,11 @@
;; microscheme has this as a separate form but it's just for ;; microscheme has this as a separate form but it's just for
(define (for-each-vector f v) (for ([x v]) (f x))) (define (for-each-vector f v) (for ([x v]) (f x)))
(define (vector-copy src dest src-start src-finish dest-start)
;; ignore src-start and dest-start
(for ([i (range src-finish)])
(vector-set! dest i (vector-ref src i))))
(define last-usb-frame #f) ; save this off so we can test it (define last-usb-frame #f) ; save this off so we can test it
(define (call-c-func f-name . args) (define (call-c-func f-name . args)