Browse Source

Got USB keypad working

Yay!
The printOnly folder corresponds to the build2/, which should be
cmake'd the same as src/ only use printOnly/ instead.
master
Macoy Madson 5 months ago
parent
commit
a37173ac16
  1. 7
      BuildPico.sh
  2. 4
      FlashPicoPrintOnly.sh
  3. 2
      ReadMe.org
  4. 50
      printOnly/CMakeLists.txt
  5. 59
      printOnly/pico_extras_import_optional.cmake
  6. 73
      printOnly/pico_sdk_import.cmake
  7. 36
      src/KeypadPico.cake
  8. 80
      src/KeypadPicoPrintOnly.cake

7
BuildPico.sh

@ -21,5 +21,12 @@ $CAKELISP --verbose-processes --skip-build \
src/Config_RP2040.cake \
src/KeypadPico.cake || exit $?
$CAKELISP --verbose-processes --skip-build \
src/Config_RP2040.cake \
src/KeypadPicoPrintOnly.cake || exit $?
cd build || exit $?
make -j || exit $?
cd ../build2 || exit $?
make -j || exit $?

4
FlashPicoPrintOnly.sh

@ -0,0 +1,4 @@
#!/bin/sh
sudo /home/macoy/Development/code/3rdParty/repositories/picotool/build/picotool load build2/keypad.uf2 || exit $?
sudo /home/macoy/Development/code/3rdParty/repositories/picotool/build/picotool verify build2/keypad.uf2 || exit $?

2
ReadMe.org

@ -51,7 +51,7 @@ Before flashing, plug in the kb2040 while holding the boot button down.
#+BEGIN_SRC sh
mkdir build
cd build
cmake .. -DPICO_SDK_PATH=/home/macoy/Development/code/3rdParty/repositories/pico-sdk -DPICO_BOARD=adafruit_kb2040
cmake ../src/ -DPICO_SDK_PATH=/home/macoy/Development/code/3rdParty/repositories/pico-sdk -DPICO_BOARD=adafruit_kb2040
cd ../
./BuildPico.sh
./FlashPico.sh

50
printOnly/CMakeLists.txt

@ -0,0 +1,50 @@
cmake_minimum_required(VERSION 3.13)
cmake_minimum_required(VERSION 3.12)
# Pull in SDK (must be before project)
include(pico_sdk_import.cmake)
include(pico_extras_import_optional.cmake)
project(keypad_project C CXX ASM)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)
if (PICO_SDK_VERSION_STRING VERSION_LESS "1.3.0")
message(FATAL_ERROR "Raspberry Pi Pico SDK version 1.3.0 (or later) required. Your version is ${PICO_SDK_VERSION_STRING}")
endif()
# Initialize the SDK
pico_sdk_init()
add_compile_options(-Wall
-Wno-format # int != int32_t as far as the compiler is concerned because gcc has int32_t as long int
-Wno-unused-function # we have some for the docs that aren't called
)
if (CMAKE_C_COMPILER_ID STREQUAL "GNU")
add_compile_options(-Wno-maybe-uninitialized)
endif()
add_executable(keypad)
target_sources(keypad PUBLIC
${CMAKE_CURRENT_LIST_DIR}/../cakelisp_cache/Keypad/KeypadPicoPrintOnly.cake.c
)
# Make sure TinyUSB can find tusb_config.h
target_include_directories(keypad PUBLIC
${CMAKE_CURRENT_LIST_DIR}../src)
# In addition to pico_stdlib required for common PicoSDK functionality, add dependency on tinyusb_device
# for TinyUSB device support and tinyusb_board for the additional board support library used by the example
#target_link_libraries(keypad PUBLIC pico_stdlib pico_unique_id tinyusb_device tinyusb_board)
target_link_libraries(keypad PUBLIC pico_stdlib pico_unique_id)
# Uncomment this line to enable fix for Errata RP2040-E5 (the fix requires use of GPIO 15)
#target_compile_definitions(keypad PUBLIC PICO_RP2040_USB_DEVICE_ENUMERATION_FIX=1)
# enable usb output, disable uart output
pico_enable_stdio_usb(keypad 1)
pico_enable_stdio_uart(keypad 0)
pico_add_extra_outputs(keypad)

59
printOnly/pico_extras_import_optional.cmake

@ -0,0 +1,59 @@
# This is a copy of <PICO_EXTRAS_PATH>/external/pico_extras_import.cmake
# This can be dropped into an external project to help locate pico-extras
# It should be include()ed prior to project()
if (DEFINED ENV{PICO_EXTRAS_PATH} AND (NOT PICO_EXTRAS_PATH))
set(PICO_EXTRAS_PATH $ENV{PICO_EXTRAS_PATH})
message("Using PICO_EXTRAS_PATH from environment ('${PICO_EXTRAS_PATH}')")
endif ()
if (DEFINED ENV{PICO_EXTRAS_FETCH_FROM_GIT} AND (NOT PICO_EXTRAS_FETCH_FROM_GIT))
set(PICO_EXTRAS_FETCH_FROM_GIT $ENV{PICO_EXTRAS_FETCH_FROM_GIT})
message("Using PICO_EXTRAS_FETCH_FROM_GIT from environment ('${PICO_EXTRAS_FETCH_FROM_GIT}')")
endif ()
if (DEFINED ENV{PICO_EXTRAS_FETCH_FROM_GIT_PATH} AND (NOT PICO_EXTRAS_FETCH_FROM_GIT_PATH))
set(PICO_EXTRAS_FETCH_FROM_GIT_PATH $ENV{PICO_EXTRAS_FETCH_FROM_GIT_PATH})
message("Using PICO_EXTRAS_FETCH_FROM_GIT_PATH from environment ('${PICO_EXTRAS_FETCH_FROM_GIT_PATH}')")
endif ()
if (NOT PICO_EXTRAS_PATH)
if (PICO_EXTRAS_FETCH_FROM_GIT)
include(FetchContent)
set(FETCHCONTENT_BASE_DIR_SAVE ${FETCHCONTENT_BASE_DIR})
if (PICO_EXTRAS_FETCH_FROM_GIT_PATH)
get_filename_component(FETCHCONTENT_BASE_DIR "${PICO_EXTRAS_FETCH_FROM_GIT_PATH}" REALPATH BASE_DIR "${CMAKE_SOURCE_DIR}")
endif ()
FetchContent_Declare(
pico_extras
GIT_REPOSITORY https://github.com/raspberrypi/pico-extras
GIT_TAG master
)
if (NOT pico_extras)
message("Downloading Raspberry Pi Pico Extras")
FetchContent_Populate(pico_extras)
set(PICO_EXTRAS_PATH ${pico_extras_SOURCE_DIR})
endif ()
set(FETCHCONTENT_BASE_DIR ${FETCHCONTENT_BASE_DIR_SAVE})
else ()
if (PICO_SDK_PATH AND EXISTS "${PICO_SDK_PATH}/../pico-extras")
set(PICO_EXTRAS_PATH ${PICO_SDK_PATH}/../pico-extras)
message("Defaulting PICO_EXTRAS_PATH as sibling of PICO_SDK_PATH: ${PICO_EXTRAS_PATH}")
endif()
endif ()
endif ()
if (PICO_EXTRAS_PATH)
set(PICO_EXTRAS_PATH "${PICO_EXTRAS_PATH}" CACHE PATH "Path to the PICO EXTRAS")
set(PICO_EXTRAS_FETCH_FROM_GIT "${PICO_EXTRAS_FETCH_FROM_GIT}" CACHE BOOL "Set to ON to fetch copy of PICO EXTRAS from git if not otherwise locatable")
set(PICO_EXTRAS_FETCH_FROM_GIT_PATH "${PICO_EXTRAS_FETCH_FROM_GIT_PATH}" CACHE FILEPATH "location to download EXTRAS")
get_filename_component(PICO_EXTRAS_PATH "${PICO_EXTRAS_PATH}" REALPATH BASE_DIR "${CMAKE_BINARY_DIR}")
if (NOT EXISTS ${PICO_EXTRAS_PATH})
message(FATAL_ERROR "Directory '${PICO_EXTRAS_PATH}' not found")
endif ()
set(PICO_EXTRAS_PATH ${PICO_EXTRAS_PATH} CACHE PATH "Path to the PICO EXTRAS" FORCE)
add_subdirectory(${PICO_EXTRAS_PATH} pico_extras)
endif()

73
printOnly/pico_sdk_import.cmake

@ -0,0 +1,73 @@
# This is a copy of <PICO_SDK_PATH>/external/pico_sdk_import.cmake
# This can be dropped into an external project to help locate this SDK
# It should be include()ed prior to project()
if (DEFINED ENV{PICO_SDK_PATH} AND (NOT PICO_SDK_PATH))
set(PICO_SDK_PATH $ENV{PICO_SDK_PATH})
message("Using PICO_SDK_PATH from environment ('${PICO_SDK_PATH}')")
endif ()
if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT} AND (NOT PICO_SDK_FETCH_FROM_GIT))
set(PICO_SDK_FETCH_FROM_GIT $ENV{PICO_SDK_FETCH_FROM_GIT})
message("Using PICO_SDK_FETCH_FROM_GIT from environment ('${PICO_SDK_FETCH_FROM_GIT}')")
endif ()
if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT_PATH} AND (NOT PICO_SDK_FETCH_FROM_GIT_PATH))
set(PICO_SDK_FETCH_FROM_GIT_PATH $ENV{PICO_SDK_FETCH_FROM_GIT_PATH})
message("Using PICO_SDK_FETCH_FROM_GIT_PATH from environment ('${PICO_SDK_FETCH_FROM_GIT_PATH}')")
endif ()
set(PICO_SDK_PATH "${PICO_SDK_PATH}" CACHE PATH "Path to the Raspberry Pi Pico SDK")
set(PICO_SDK_FETCH_FROM_GIT "${PICO_SDK_FETCH_FROM_GIT}" CACHE BOOL "Set to ON to fetch copy of SDK from git if not otherwise locatable")
set(PICO_SDK_FETCH_FROM_GIT_PATH "${PICO_SDK_FETCH_FROM_GIT_PATH}" CACHE FILEPATH "location to download SDK")
if (NOT PICO_SDK_PATH)
if (PICO_SDK_FETCH_FROM_GIT)
include(FetchContent)
set(FETCHCONTENT_BASE_DIR_SAVE ${FETCHCONTENT_BASE_DIR})
if (PICO_SDK_FETCH_FROM_GIT_PATH)
get_filename_component(FETCHCONTENT_BASE_DIR "${PICO_SDK_FETCH_FROM_GIT_PATH}" REALPATH BASE_DIR "${CMAKE_SOURCE_DIR}")
endif ()
# GIT_SUBMODULES_RECURSE was added in 3.17
if (${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.17.0")
FetchContent_Declare(
pico_sdk
GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk
GIT_TAG master
GIT_SUBMODULES_RECURSE FALSE
)
else ()
FetchContent_Declare(
pico_sdk
GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk
GIT_TAG master
)
endif ()
if (NOT pico_sdk)
message("Downloading Raspberry Pi Pico SDK")
FetchContent_Populate(pico_sdk)
set(PICO_SDK_PATH ${pico_sdk_SOURCE_DIR})
endif ()
set(FETCHCONTENT_BASE_DIR ${FETCHCONTENT_BASE_DIR_SAVE})
else ()
message(FATAL_ERROR
"SDK location was not specified. Please set PICO_SDK_PATH or set PICO_SDK_FETCH_FROM_GIT to on to fetch from git."
)
endif ()
endif ()
get_filename_component(PICO_SDK_PATH "${PICO_SDK_PATH}" REALPATH BASE_DIR "${CMAKE_BINARY_DIR}")
if (NOT EXISTS ${PICO_SDK_PATH})
message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' not found")
endif ()
set(PICO_SDK_INIT_CMAKE_FILE ${PICO_SDK_PATH}/pico_sdk_init.cmake)
if (NOT EXISTS ${PICO_SDK_INIT_CMAKE_FILE})
message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' does not appear to contain the Raspberry Pi Pico SDK")
endif ()
set(PICO_SDK_PATH ${PICO_SDK_PATH} CACHE PATH "Path to the Raspberry Pi Pico SDK" FORCE)
include(${PICO_SDK_INIT_CMAKE_FILE})

36
src/KeypadPico.cake

@ -11,6 +11,7 @@
(c-import
;; sdk
"<stdio.h>"
"pico/stdlib.h"
"bsp/board.h"
"tusb.h"
@ -41,7 +42,7 @@
(array HID_KEY_NUM_LOCK HID_KEY_KEYPAD_7 HID_KEY_KEYPAD_4 HID_KEY_KEYPAD_1 HID_KEY_KEYPAD_0) ;; a3
(array HID_KEY_KEYPAD_DIVIDE HID_KEY_KEYPAD_8 HID_KEY_KEYPAD_5 HID_KEY_KEYPAD_2 keycode-unused) ;; a2
(array HID_KEY_KEYPAD_MULTIPLY HID_KEY_KEYPAD_9 HID_KEY_KEYPAD_6 HID_KEY_KEYPAD_3 HID_KEY_KEYPAD_DECIMAL) ;; a1
(array HID_KEY_KEYPAD_ADD HID_KEY_KEYPAD_SUBTRACT keycode-unused keycode-unused HID_KEY_KEYPAD_ENTER))) ;; a0
(array HID_KEY_KEYPAD_SUBTRACT HID_KEY_KEYPAD_ADD keycode-unused keycode-unused HID_KEY_KEYPAD_ENTER))) ;; a0
(defmacro print-line (line any)
;; (tokenize-push output (call-on println Serial (token-splice line)))
@ -55,11 +56,11 @@
;; (pinMode LED_BUILTIN OUTPUT)
(each-item-in-array row-pins row row-pin int
(gpio_init row-pin)
(gpio_set_dir row-pin GPIO_IN))
(gpio_set_dir row-pin GPIO_IN)
(gpio_pull_up row-pin))
(each-item-in-array column-pins column col-pin int
(gpio_init col-pin)
(gpio_set_dir col-pin GPIO_IN)
(gpio_pull_up col-pin))
(gpio_set_dir col-pin GPIO_OUT))
(while true
(tud_task)
@ -138,34 +139,11 @@
(defun scan-matrix ()
(each-item-in-array column-pins column col-pin int
(gpio_set_dir col-pin GPIO_OUT)
(gpio_disable_pulls col-pin)
(gpio_put col-pin 0)
(sleep_ms 1) ;; Seems necessary to prevent extraneous key presses.
(each-item-in-array row-pins row row-pin int
(gpio_set_dir row-pin GPIO_IN)
(gpio_pull_up row-pin)
(set (at row column matrix) (= 0 (gpio_get row-pin)))
(gpio_set_dir row-pin GPIO_OUT)
(gpio_disable_pulls row-pin))
(gpio_set_dir col-pin GPIO_IN)
(gpio_pull_up col-pin)))
;; (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 (sleep_ms 500)))
(set (at row column matrix) (= 0 (gpio_get row-pin))))
(gpio_put col-pin 1)))
;; Invoked when received SET_REPORT control request or
;; received data on OUT endpoint ( Report ID = 0, Type = 0 )

80
src/KeypadPicoPrintOnly.cake

@ -0,0 +1,80 @@
;; 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")
(c-import
;; sdk
"<stdio.h>"
"pico/stdlib.h"
"bsp/board.h"
"stdint.h")
(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
(var keycode-unused (const uint8_t) 0)
(defmacro print-line (line any)
;; (tokenize-push output (call-on println Serial (token-splice line)))
(return true))
(defun main (&return int)
(stdio_init_all)
(each-item-in-array row-pins row row-pin int
(gpio_init row-pin)
(gpio_set_dir row-pin GPIO_IN)
(gpio_pull_up row-pin))
(each-item-in-array column-pins column col-pin int
(gpio_init col-pin)
(gpio_set_dir col-pin GPIO_OUT))
(while true
(scan-print-matrix))
;; Should never happen
(return 1))
(defun scan-matrix ()
(each-item-in-array column-pins column col-pin int
(gpio_set_dir col-pin GPIO_OUT)
(gpio_put col-pin 0)
(sleep_ms 1) ;; Seems necessary to prevent extraneous key presses.
(each-item-in-array row-pins row row-pin int
(set (at row column matrix) (= 0 (gpio_get row-pin))))
(gpio_put col-pin 1)))
(defun scan-print-matrix ()
(scan-matrix)
;; (printf "\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)))
;; (printf "----")
;; (printf "%d, %d" row column)
;; (printf "%s" (? (at row column matrix) "ON" "OFF"))
(printf "%c" (at row column matrix-to-keys)))
(set (at row column last-scan-matrix)
(at row column matrix))))
;; wait for a bit
(sleep_ms 100))
Loading…
Cancel
Save