I would consider using HoodLoader2 instead of that LUFA project. If you can't do that, you can compile parts of the Arduino Core together with the LUFA project.
The main steps:
- Add the necessary Arduino source files to the
SRC variable in the Makefile.
- Add the Arduino core folder to the include path by appending to the
CC_FLAGS and CXX_FLAGS variables.
- Move the main function to a new
main.cpp file and add it to the Makefile SRC as well.
- Include the
Joystick.h file in your main file in an extern "C" block.
- Include the
HardwareSerial.h file in your main file as well.
My files after these changes:
Makefile
# Set the MCU accordingly to your device (e.g. at90usb1286 for a Teensy 2.0++, or atmega16u2 for an Arduino UNO R3)
MCU = atmega16u2
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 =
ARDUINO_PATH = ${HOME}/.arduino15/packages/arduino/hardware/avr/1.8.1/cores/arduino
SRC += $(ARDUINO_PATH)/HardwareSerial.cpp
SRC += $(ARDUINO_PATH)/HardwareSerial1.cpp
SRC += $(ARDUINO_PATH)/Print.cpp
CXX_FLAGS += -I$(ARDUINO_PATH) -I./HoodLoader2/avr/variants/HoodLoader2
CC_FLAGS += -I$(ARDUINO_PATH) -I./HoodLoader2/avr/variants/HoodLoader2
...
main.cpp
extern "C" {
#include "Joystick.h"
}
#include <HardwareSerial.h>
// Main entry point.
int main(void) {
// We'll start by performing hardware and peripheral setup.
SetupHardware();
// We'll then enable global interrupts for our use.
GlobalInterruptEnable();
// Initialize the UART
Serial1.begin(115200);
Serial1.println(F("Hello, world"));
// Once that's done, we'll enter an infinite loop.
for (;;)
{
// We need to run our task to process and deliver data for our IN and OUT endpoints.
HID_Task();
// We also need to run the main USB management task.
USB_USBTask();
}
}
Joystick.c
Deleted the main function.
To get it to compile after these changes:
# (inside of the snowball-thrower folder)
git submodule add https://github.com/NicoHood/HoodLoader2.git HoodLoader2
make
This compiles without problems for me, but I have no idea if it actually works.
Edit:
You'll notice that the program uses more RAM than the ATmeg16U2 has available. You could solve this by moving the step array to PROGMEM, or by shrinking the Serial buffers.
avr-size --mcu=atmega16u2 --format=avr Joystick.elf
AVR Memory Usage
----------------
Device: atmega16u2
Program: 5076 bytes (31.0% Full)
(.text + .data + .bootloader)
Data: 527 bytes (102.9% Full)
(.data + .bss + .noinit)
[INFO] : Finished building project "Joystick".
extern "C". If you need to call your C++ function from C code, declare itextern "C"as well. You can then use all of the C++ features from the Arduino core if you want.