Skip to content

Commit

Permalink
Update code to use its own c++ class and CMake library
Browse files Browse the repository at this point in the history
Now using a c++ class.
This update contains some potentially breaking changes to any existing code you may have that uses this library.
The RGBMacroLibrary has been reworked into its own class RGBMacroPad which can be seen in use in Main.cpp CMake also now sees the library as a proper library making implementation even easier.        
This will allow better integration into your code and much easier usage when working with the library.
This library basically becomes an extension of tinyUSB and the Pimoroni libraries to create a really simple deployment of the keypad with keyboard keybinds. 
Changing the library to be like this makes it easy to work with for beginners as well as allowing you to easily merge this with another project.
The libraries SendKeypress function is exposed in the event you wish to send keypresses/combinations yourself while using this library.
I may add more examples in the future to better show the uses of the library.
  • Loading branch information
SuperNinja-4965 authored Feb 10, 2023
2 parents cd0684f + 53c2da4 commit 285c263
Show file tree
Hide file tree
Showing 11 changed files with 349 additions and 189 deletions.
11 changes: 1 addition & 10 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,6 @@ jobs:
git clone https://github.com/pimoroni/pimoroni-pico.git ./libs/pimoroni-pico
git -C ./libs/pimoroni-pico submodule update --init --recursive
# DISABLED because build seems to fail here.
# Build the example code from the library useful for further testing
#- name: Build Pimoroni Examples
# run: |
# export PICO_SDK_PATH='/home/runner/work/Raspberry-Pi-Pico-MacroPad/Raspberry-Pi-Pico-MacroPad/pico/pico-sdk'
# mkdir ./libs/pimoroni-pico/build
# cmake -S ./libs/pimoroni-pico -B ./libs/pimoroni-pico/build
# make -C ./libs/pimoroni-pico/build -j ${{steps.core_count.outputs.output}}

# Builds the code
- name: Build the Code
run: |
Expand All @@ -102,5 +93,5 @@ jobs:
- uses: actions/upload-artifact@v2
with:
name: UF2-Compiled-Binary.uf2
path: ./build/Pico-MacroPad.uf2
path: ./build/examples/Simple/SimpleExample.uf2
retention-days: 60
42 changes: 9 additions & 33 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,47 +21,23 @@
# THE SOFTWARE.
#

cmake_minimum_required(VERSION 3.13)
cmake_minimum_required(VERSION 3.5)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)

# Pull in Raspberry Pi Pico SDK
include(pico_sdk_import.cmake)

# Include pimoroni pico
set(PIMORONI_PICO_PATH ${CMAKE_CURRENT_LIST_DIR}/libs/pimoroni-pico)
include(pimoroni_pico_import.cmake)
include(libs/pimoroni-pico/libraries/pico_rgb_keypad/pico_rgb_keypad.cmake)

# Define our project
project(Pico-MacroPad C CXX ASM)
project(RGBMacroPad C CXX ASM)

# Initialise the Raspberry Pi Pico SDK
pico_sdk_init()

# Check to see if TinyUSB has been included in the SDK
if (TARGET tinyusb_device)
# Import the pimoroni drivers and libraries
add_subdirectory(libs/pimoroni-pico/drivers)
add_subdirectory(libs/pimoroni-pico/libraries/pico_rgb_keypad)

# Add our binary
add_executable(
Pico-MacroPad
src/Main.cpp
src/RGBMacroLibrary.cpp
src/usb_descriptors.c
)

# Make sure TinyUSB can find tusb_config.h
target_include_directories(Pico-MacroPad PUBLIC
${CMAKE_CURRENT_LIST_DIR}/src)

# Set the Pico program information
pico_set_program_name(Pico-MacroPad "Raspberry Pi Pico MacroPad")
pico_set_program_version(Pico-MacroPad "2022-09-1")

# Pull in pico libraries that we need
target_link_libraries(Pico-MacroPad PUBLIC pico_stdlib pico_rgb_keypad tinyusb_device tinyusb_board)

# create map/bin/hex file etc.
pico_add_extra_outputs(Pico-MacroPad)

elseif(PICO_ON_DEVICE)
message(WARNING "Not building Pico-MacroPad because TinyUSB submodule is not initialized in the SDK")
endif()
add_subdirectory(src)
add_subdirectory(examples)
26 changes: 26 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# The MIT License (MIT)
#
# Copyright (c) 2022 Joshua Glass (SuperNinja_4965)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#

cmake_minimum_required(VERSION 3.5)

add_subdirectory(Simple)
51 changes: 51 additions & 0 deletions examples/Simple/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# The MIT License (MIT)
#
# Copyright (c) 2022 Joshua Glass (SuperNinja_4965)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#

cmake_minimum_required(VERSION 3.5)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)

# Check to see if TinyUSB has been included in the SDK
if (TARGET tinyusb_device)
# Add our binary
add_executable(
SimpleExample
Main.cpp
)

# Make sure TinyUSB can find tusb_config.h
target_include_directories(SimpleExample PUBLIC ${CMAKE_CURRENT_LIST_DIR})

# Set the Pico program information
pico_set_program_name(SimpleExample "Raspberry Pi Pico MacroPad")
pico_set_program_version(SimpleExample "2023-02-1")

# Pull in pico libraries that we need
target_link_libraries(SimpleExample PUBLIC pico_stdlib RGBMacroPad)

# create map/bin/hex file etc.
pico_add_extra_outputs(SimpleExample)

elseif(PICO_ON_DEVICE)
message(WARNING "Not building Pico-MacroPad because TinyUSB submodule is not initialized in the SDK")
endif()
79 changes: 79 additions & 0 deletions examples/Simple/Main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2022 Joshua Glass (SuperNinja_4965)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/

// Standard library includes.
#include "pico/stdlib.h"
// Include our Macropad Library
#include "RGBMacroLibrary.hpp"

//------------------------------------------------------------------------------------------------------------------------+
// Useful info:
// The RGBMacroLibrary uses 2 of the RP2040's timers.
// To find out what keys work you can look here: https://github.com/hathach/tinyusb/blob/master/src/class/hid/hid.h
// Any keys in the REPORT_ID_KEYBOARD and REPORT_ID_CONSUMER_CONTROL devices will work.
//------------------------------------------------------------------------------------------------------------------------+

RGBMacroPad MacroPad;

// Code enters here.
int main() {
//False disables the blinking task, 300000 is the sleep timer duration in ms.
MacroPad.DimLedDuration = 300000;
MacroPad.UseBlinking = false;
// Ready the device.
MacroPad.InitializeDevice();

// Setup our keys These are just the keys I personally use.
MacroPad.SetupButton(0, 0x20, 0x00, 0x20, HID_KEY_VOLUME_UP, 0, REPORT_ID_KEYBOARD);
MacroPad.SetupButton(1, 0x00, 0x20, 0x00, HID_KEY_S, KEYBOARD_MODIFIER_LEFTGUI + KEYBOARD_MODIFIER_LEFTCTRL + KEYBOARD_MODIFIER_LEFTSHIFT + KEYBOARD_MODIFIER_LEFTALT, REPORT_ID_KEYBOARD);
MacroPad.SetupButton(2, 0x20, 0x00, 0x20, HID_USAGE_CONSUMER_PLAY_PAUSE, 0, REPORT_ID_CONSUMER_CONTROL);
MacroPad.SetupButton(3, 0x20, 0x00, 0x00, HID_KEY_DELETE, KEYBOARD_MODIFIER_LEFTCTRL + KEYBOARD_MODIFIER_LEFTALT, REPORT_ID_KEYBOARD);

MacroPad.SetupButton(4, 0x20, 0x00, 0x20, HID_KEY_VOLUME_DOWN, 0, REPORT_ID_KEYBOARD);
MacroPad.SetupButton(5, 0x20, 0x00, 0x00, HID_KEY_H, KEYBOARD_MODIFIER_LEFTGUI + KEYBOARD_MODIFIER_LEFTCTRL + KEYBOARD_MODIFIER_LEFTSHIFT + KEYBOARD_MODIFIER_LEFTALT, REPORT_ID_KEYBOARD);
MacroPad.SetupButton(6, 0x20, 0x00, 0x20, HID_USAGE_CONSUMER_SCAN_PREVIOUS, 0, REPORT_ID_CONSUMER_CONTROL);
MacroPad.SetupButton(7, 0x20, 0x00, 0x20, HID_USAGE_CONSUMER_SCAN_NEXT, 0, REPORT_ID_CONSUMER_CONTROL);

MacroPad.SetupButton(8, 0x20, 0x00, 0x20, HID_KEY_MUTE, 0, REPORT_ID_KEYBOARD);
MacroPad.SetupButton(9, 0x00, 0x20, 0x20, HID_KEY_A, KEYBOARD_MODIFIER_LEFTGUI + KEYBOARD_MODIFIER_LEFTCTRL + KEYBOARD_MODIFIER_LEFTSHIFT + KEYBOARD_MODIFIER_LEFTALT, REPORT_ID_KEYBOARD);
MacroPad.SetupButton(10, 0x00, 0x00, 0x20, HID_KEY_D, KEYBOARD_MODIFIER_LEFTGUI + KEYBOARD_MODIFIER_LEFTCTRL + KEYBOARD_MODIFIER_LEFTSHIFT + KEYBOARD_MODIFIER_LEFTALT, REPORT_ID_KEYBOARD);
MacroPad.SetupButton(11, 0x20, 0x00, 0x00, 0, 0, REPORT_ID_TINYPICO);

MacroPad.SetupButton(12, 0x00, 0x00, 0x00, HID_KEY_CAPS_LOCK, 0, REPORT_ID_KEYBOARD);
MacroPad.SetupButton(13, 0x00, 0x00, 0x00, HID_KEY_NUM_LOCK, 0, REPORT_ID_KEYBOARD);
// Uncomment this line if you want to use scroll lock. Comment the line below if you don't want to use it. (dont forget to update the line below)
//SetupButton(14, 0x00, 0x00, 0x00, HID_KEY_SCROLL_LOCK, 0, REPORT_ID_KEYBOARD);
MacroPad.SetupButton(14, 0x00, 0x20, 0x00, HID_KEY_T, KEYBOARD_MODIFIER_LEFTCTRL + KEYBOARD_MODIFIER_LEFTALT, REPORT_ID_KEYBOARD);
MacroPad.SetupButton(15, 0x00, 0x20, 0x20, HID_KEY_L, KEYBOARD_MODIFIER_LEFTGUI, REPORT_ID_KEYBOARD);
//MacroPad.RemoveButtonSetup(ButtonNum); // Use this to remove a buttons config.

while (true)
{
// Run the libraries loop to handle keypresses.
MacroPad.Loop();
}

return 0;
}
56 changes: 56 additions & 0 deletions pimoroni_pico_import.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# This file can be dropped into a project to help locate the Pimoroni Pico libraries
# It will also set up the required include and module search paths.

if (DEFINED ENV{PIMORONI_PICO_FETCH_FROM_GIT} AND (NOT PIMORONI_PICO_FETCH_FROM_GIT))
set(PIMORONI_PICO_FETCH_FROM_GIT $ENV{PIMORONI_PICO_FETCH_FROM_GIT})
message("Using PIMORONI_PICO_FETCH_FROM_GIT from environment ('${PIMORONI_PICO_FETCH_FROM_GIT}')")
endif ()

if (DEFINED ENV{PIMORONI_PICO_FETCH_FROM_GIT_PATH} AND (NOT PIMORONI_PICO_FETCH_FROM_GIT_PATH))
set(PIMORONI_PICO_FETCH_FROM_GIT_PATH $ENV{PIMORONI_PICO_FETCH_FROM_GIT_PATH})
message("Using PIMORONI_PICO_FETCH_FROM_GIT_PATH from environment ('${PIMORONI_PICO_FETCH_FROM_GIT_PATH}')")
endif ()

if (NOT PIMORONI_PICO_PATH)
if (PIMORONI_PICO_FETCH_FROM_GIT)
include(FetchContent)
set(FETCHCONTENT_BASE_DIR_SAVE ${FETCHCONTENT_BASE_DIR})
if (PIMORONI_PICO_FETCH_FROM_GIT_PATH)
get_filename_component(FETCHCONTENT_BASE_DIR "${PIMORONI_PICO_FETCH_FROM_GIT_PATH}" REALPATH BASE_DIR "${CMAKE_SOURCE_DIR}")
endif ()
FetchContent_Declare(
pimoroni_pico
GIT_REPOSITORY https://github.com/pimoroni/pimoroni-pico
GIT_TAG main
)
if (NOT pimoroni_pico)
message("Downloading PIMORONI_PICO SDK")
FetchContent_Populate(pimoroni_pico)
set(PIMORONI_PICO_PATH ${pimoroni_pico_SOURCE_DIR})
endif ()
set(FETCHCONTENT_BASE_DIR ${FETCHCONTENT_BASE_DIR_SAVE})
elseif(PICO_SDK_PATH AND EXISTS "${PICO_SDK_PATH}/../pimoroni-pico")
set(PIMORONI_PICO_PATH ${PICO_SDK_PATH}/../pimoroni-pico)
message("Defaulting PIMORONI_PICO_PATH as sibling of PICO_SDK_PATH: ${PIMORONI_PICO_PATH}")
elseif(EXISTS "${CMAKE_CURRENT_BINARY_DIR}/../../pimoroni-pico/")
set(PIMORONI_PICO_PATH ${CMAKE_CURRENT_BINARY_DIR}/../../pimoroni-pico/)
else()
message(FATAL_ERROR "Pimoroni Pico location was not specified. Please set PIMORONI_PICO_PATH.")
endif()
endif()

get_filename_component(PIMORONI_PICO_PATH "${PIMORONI_PICO_PATH}" REALPATH BASE_DIR "${CMAKE_BINARY_DIR}")
if (NOT EXISTS ${PIMORONI_PICO_PATH})
message(FATAL_ERROR "Directory '${PIMORONI_PICO_PATH}' not found")
endif ()

if (NOT EXISTS ${PIMORONI_PICO_PATH}/pimoroni_pico_import.cmake)
message(FATAL_ERROR "Directory '${PIMORONI_PICO_PATH}' does not appear to contain the Pimoroni Pico libraries")
endif ()

message("PIMORONI_PICO_PATH is ${PIMORONI_PICO_PATH}")

set(PIMORONI_PICO_PATH ${PIMORONI_PICO_PATH} CACHE PATH "Path to the Pimoroni Pico libraries" FORCE)

include_directories(${PIMORONI_PICO_PATH})
list(APPEND CMAKE_MODULE_PATH ${PIMORONI_PICO_PATH})
47 changes: 47 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# The MIT License (MIT)
#
# Copyright (c) 2022 Joshua Glass (SuperNinja_4965)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#

cmake_minimum_required(VERSION 3.5)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)

# Check to see if TinyUSB has been included in the SDK
if (TARGET tinyusb_device)
# Add our library
add_library(RGBMacroPad INTERFACE)

target_sources(RGBMacroPad INTERFACE
${CMAKE_CURRENT_LIST_DIR}/RGBMacroLibrary.cpp
${CMAKE_CURRENT_LIST_DIR}/usb_descriptors.c
)

# Make sure TinyUSB can find tusb_config.h
target_include_directories(RGBMacroPad INTERFACE ${CMAKE_CURRENT_LIST_DIR})

# Pull in pico libraries that we need
target_link_libraries(RGBMacroPad INTERFACE pico_stdlib pico_rgb_keypad tinyusb_device tinyusb_board)

elseif(PICO_ON_DEVICE)
message(WARNING "Not building Pico-MacroPad because TinyUSB submodule is not initialized in the SDK")
endif()

Loading

0 comments on commit 285c263

Please sign in to comment.