generated from macoy/gamelib-project-template
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
2.3 KiB
66 lines
2.3 KiB
;; Arduino build expects things to have specific names and extensions. We'll create a build label
|
|
;; just to make our build directory match, which appears to be necessary.
|
|
(add-build-config-label "Keypad")
|
|
|
|
(set-cakelisp-option cakelisp-src-dir "Dependencies/cakelisp/src")
|
|
(add-cakelisp-search-directory "Dependencies/gamelib/src")
|
|
(add-cakelisp-search-directory "Dependencies/cakelisp/runtime")
|
|
(add-cakelisp-search-directory "src")
|
|
|
|
(import "CHelpers.cake")
|
|
|
|
(var row-pins (array (const int)) ;; Samples. Yellow wires.
|
|
(array 29 28 27 26)) ;; A3 A2 A1 A0
|
|
|
|
(var column-pins (array (const int)) ;; Sends power. White wires.
|
|
(array 2 3 4 5 6)) ;; 2 3 4 5 6
|
|
|
|
(var last-scan-matrix (array 4 (array 5 int)) (array 0))
|
|
(var matrix (array 4 (array 5 int)) (array 0))
|
|
|
|
(var matrix-to-keys (array 4 (array 5 char))
|
|
(array
|
|
(array 'n' '7' '4' '1' '0') ;; a3
|
|
(array '/' '8' '5' '2' '_') ;; a2
|
|
(array '*' '9' '6' '3' '.') ;; a1
|
|
(array '+' 'b' '_' '_' 'e'))) ;; a0
|
|
|
|
(defmacro print-line (line any)
|
|
(tokenize-push output (call-on println Serial (token-splice line)))
|
|
(return true))
|
|
|
|
(defun setup ()
|
|
(call-on begin Serial 115200)
|
|
;; (pinMode LED_BUILTIN OUTPUT)
|
|
(each-item-in-array row-pins row row-pin int
|
|
(pinMode row-pin INPUT))
|
|
(each-item-in-array column-pins column col-pin int
|
|
(pinMode col-pin INPUT_PULLUP)))
|
|
|
|
(defun scan-matrix ()
|
|
(each-item-in-array column-pins column col-pin int
|
|
(pinMode col-pin OUTPUT)
|
|
(digitalWrite col-pin LOW)
|
|
(delay 1) ;; milliseconds. Seems necessary to prevent extraneous key presses.
|
|
(each-item-in-array row-pins row row-pin int
|
|
(pinMode row-pin INPUT_PULLUP)
|
|
(set (at row column matrix) (= LOW (digitalRead row-pin)))
|
|
(pinMode row-pin OUTPUT))
|
|
(pinMode col-pin INPUT_PULLUP)))
|
|
|
|
(defun loop ()
|
|
(scan-matrix)
|
|
;; (print-line "\n-------")
|
|
(each-in-range (array-size row-pins) row
|
|
(each-in-range (array-size column-pins) column
|
|
(when (and (at row column matrix)
|
|
(not (at row column last-scan-matrix)))
|
|
;; (print-line "----")
|
|
;; (print-line row)
|
|
;; (print-line column)
|
|
;; (print-line (? (at row column matrix) "ON" "OFF"))
|
|
(print-line (at row column matrix-to-keys)))
|
|
(set (at row column last-scan-matrix)
|
|
(at row column matrix))))
|
|
;; wait for a bit
|
|
(ignore (delay 500)))
|
|
|