Teensy 2.0++ matrix keypad library written in C?

Status
Not open for further replies.
Hello folks,

I've been working on a fork of this Github repo (https://github.com/bertrandom/snowball-thrower) where the Teensy is emulating a nintendo switch controller. I wanted to add a matrix style keypad input to the Teensy 2.0++ to allow for user input when the program is running. However, the keypad example here (https://www.pjrc.com/teensy/td_libs_Keypad.html) is written in C++, whereas the code I'm working with is written in C. Is there a C version of the matrix keypad library that someone knows of, or is there a nice way to get the C++ code to work with C?

Some additional details:
  • The Teensy matrix keypad documentation is the same as the arduino matrix keypad documented here: https://playground.arduino.cc/Code/Keypad/
  • I am using C+makefile on windows 10.
  • I am using a 4x4 matrix keypad wired to C0-C7 on the Teensy 2.0++. I have run the C++ matrix keypad code using Teensyduino and it works like a charm. But doesn't work with C+makefile code.
  • Example of a matrix keypad and a project using it: https://www.circuitbasics.com/how-to-set-up-a-keypad-on-an-arduino/. I'm using the exact same 4x4 matrix keypad model as pictured.

Thanks for any input you can provide :)! Been awhile since I've coded in C so I'm a bit rusty; if I'm making an obvious blunder please let me know.
 
You can simply compile the necessary files as C++ if you edit the makefile accordingly. I covered the process in this Stack Exchange answer. Since you're not using an ATmega16U2, you probably don't need Hoodloader, but you might need to include (or mock) some of the Arduino-related Teensy Core files used by the Keypad library.

That being said, reading a keypad is really straightforward, you don't need a library for it.

Pieter
 
Thanks for the input and detailed explanation in your Stack Exchange answer Pieter. And as for your second thought, I think you might be right. It may just be more effective to create my own keypad reader in c than to convert the code to C++ and add all the needed library support. Are there any good guides out there for how to do this? Otherwise I'll scan over the usb_keyboard library and the keypad library to reverse engineer it.
 
Thanks for your help Pieter, and sorry for the delay. Got pulled away from this project for a bit and just came back to it. I decided to start by trying to compile the necessary files as C++ by editing the makefile as you suggested. It worked well until I started adding the Arduino-related Teensy Core files used by the Keypad library. I used the core files from my Teensyduino arduino IDE install located in C:\Program Files (x86)\Arduino\hardware\teensy\avr\cores\teensy, however the moment I added in the keypad the compiler exploded with errors, with the first in the cascade being:
Code:
In file included from ./teensycore/Stream.h:24:0,
                 from ./teensycore/HardwareSerial.h:5,
                 from ./teensycore/WProgram.h:25,
                 from ./teensycore/Arduino.h:1,
                 from ./Keypad-master/src/Key.h:35,
                 from ./Keypad-master/src/Key.cpp:30:
./teensycore/Print.h:46:2: warning: identifier 'constexpr' is a keyword in C++11 [-Wc++0x-compat]
  constexpr Print() : write_error(0) {}
  ^
This pointed to a problem with print.cpp in the teensy core library. I added CPP_FLAGS = -std=c++11 to my make file, but this only led to further errors:
Code:
In file included from ./teensycore/Stream.h:24:0,
                 from ./teensycore/HardwareSerial.h:5,
                 from ./teensycore/WProgram.h:25,
                 from ./teensycore/Arduino.h:1,
                 from ./Keypad-master/src/Key.h:35,
                 from ./Keypad-master/src/Key.cpp:30:
./teensycore/Print.h:71:21: error: 'Printable' does not name a type
  size_t print(const Printable &obj)  { return obj.printTo(*this); }
                     ^
To make sure I wasn't screwing up the makefile, I changed the core libraries to the default arduino ones and almost had success, except for the fact the the core arduino library does not contain the pin layout of the teensy (and the two libraries don't seem to be compabtible after copying and pasting the necessary files):
Code:
In file included from ./arduinocore/Print.cpp:27:0:
./arduinocore/Arduino.h:258:26: fatal error: pins_arduino.h: No such file or directory
 #include "pins_arduino.h"

I'm at a loss at this point, so any advice would be much appreciated. Is there a better location from where I find Teensyduino files. It should be noted that installing software to my teensy from the arduino IDE (teensyduino) works like a charm, so I'm probably missing some important flags in my makefile. Here is the current state of my makefile:
Code:
#
#             LUFA Library
#     Copyright (C) Dean Camera, 2014.
#
#  dean [at] fourwalledcubicle [dot] com
#           www.lufa-lib.org
#
# --------------------------------------
#         LUFA Project Makefile.
# --------------------------------------

# Run "make help" for target help.

# Set the MCU accordingly to your device (e.g. at90usb1286 for a Teensy 2.0++, or atmega16u2 for an Arduino UNO R3)
MCU          = at90usb1286
ARCH         = AVR8
F_CPU        = 16000000
F_USB        = $(F_CPU)
OPTIMIZATION = s
TARGET       = Joystick
SRC          = $(TARGET).c Descriptors.c main.cpp $(LUFA_SRC_USB)
LUFA_PATH    = ./lufa/LUFA
CC_FLAGS     = -DUSE_LUFA_CONFIG_HEADER -IConfig/
LD_FLAGS     =
#CPP_FLAGS    = -std=c++11


ARDUINO_PATH =  ./arduinocore
SRC         +=  $(ARDUINO_PATH)/Print.cpp
SRC         +=  $(ARDUINO_PATH)/HardwareSerial.cpp
CXX_FLAGS   += -I$(ARDUINO_PATH)
CC_FLAGS    += -I$(ARDUINO_PATH)

#TEENSY_PATH  = ./teensycore
#CXX_FLAGS   += -I$(TEENSY_PATH)
#CC_FLAGS    += -I$(TEENSY_PATH)

KEYPAD_PATH  = ./Keypad-master/src
SRC         += $(KEYPAD_PATH)/Keypad.cpp
SRC         += $(KEYPAD_PATH)/Key.cpp
CXX_FLAGS   += -I$(KEYPAD_PATH) 
CC_FLAGS    += -I$(KEYPAD_PATH)

# Default target
all:

# Include LUFA build script makefiles
include $(LUFA_PATH)/Build/lufa_core.mk
include $(LUFA_PATH)/Build/lufa_sources.mk
include $(LUFA_PATH)/Build/lufa_build.mk
include $(LUFA_PATH)/Build/lufa_cppcheck.mk
include $(LUFA_PATH)/Build/lufa_doxygen.mk
include $(LUFA_PATH)/Build/lufa_dfu.mk
include $(LUFA_PATH)/Build/lufa_hid.mk
include $(LUFA_PATH)/Build/lufa_avrdude.mk
include $(LUFA_PATH)/Build/lufa_atprogram.mk

# Target for LED/buzzer to alert when print is done
with-alert: all
with-alert: CC_FLAGS += -DALERT_WHEN_DONE
 
Status
Not open for further replies.
Back
Top