Description
I am using Visual Studio Code 1.72.2 and PlatformIO 6.1.4. The board I am using is the Sparkfun ProMicro16 based on the 32u4. When compiling the example MIDIUSB_write.ino, I get the following warnings:
Processing micro (platform: atmelavr; board: sparkfun_promicro16; framework: arduino)
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/atmelavr/sparkfun_promicro16.html
PLATFORM: Atmel AVR (4.0.0) > SparkFun Pro Micro 5V/16MHz
HARDWARE: ATMEGA32U4 16MHz, 2.50KB RAM, 28KB Flash
DEBUG: Current (simavr) On-board (simavr)
PACKAGES:
- framework-arduino-avr @ 5.1.0
- tool-avrdude @ 1.60300.200527 (6.3.0)
- toolchain-atmelavr @ 1.70300.191015 (7.3.0)
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 7 compatible libraries
Scanning dependencies...
Dependency Graph
|-- MIDIUSB @ 1.0.5
Building in release mode
Compiling .pio\build\micro\liba61\MIDIUSB\MIDIUSB.cpp.o
Compiling .pio\build\micro\src\main.cpp.o
src\main.cpp: In function 'void noteOn(byte, byte, byte)':
src\main.cpp:11:42: warning: narrowing conversion of '(int)(144 | ((unsigned char)((int)channel)))' from 'int' to 'uint8_t {aka unsigned char}' inside { } [-Wnarrowing]
midiEventPacket_t noteOn = {0x09, 0x90 | channel, pitch, velocity};
~~~~~^~~~~~~~~
src\main.cpp: In function 'void noteOff(byte, byte, byte)':
src\main.cpp:16:43: warning: narrowing conversion of '(int)(128 | ((unsigned char)((int)channel)))' from 'int' to 'uint8_t {aka unsigned char}' inside { } [-Wnarrowing]
midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity};
~~~~~^~~~~~~~~
src\main.cpp: In function 'void controlChange(byte, byte, byte)':
src\main.cpp:30:41: warning: narrowing conversion of '(int)(176 | ((unsigned char)((int)channel)))' from 'int' to 'uint8_t {aka unsigned char}' inside { } [-Wnarrowing]
midiEventPacket_t event = {0x0B, 0xB0 | channel, control, value};
~~~~~^~~~~~~~~
Linking .pio\build\micro\firmware.elf
Checking size .pio\build\micro\firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM: [= ] 8.3% (used 213 bytes from 2560 bytes)
Flash: [== ] 15.9% (used 4562 bytes from 28672 bytes)
Building .pio\build\micro\firmware.hex
Doing a little reading about these warnings I see that in the c99 standards ISO/IEC 9899:1999 section "6.5.11 Bitwise exclusive OR operator" under constrains says "Each of the operands shall have integer type."
Digging through the code, the file MIDIUSB_Defs.h on lines 6 through 9 declares each element of the struct midiEventPacket_t as "uint8_t" which is fine in this case because of the range it should have, but the type should be uint16_t to comply with the standard. Making the following changes to this file clear the warnings:
#pragma once
#include <stdint.h>
typedef struct
{
uint16_t header;
uint16_t byte1;
uint16_t byte2;
uint16_t byte3;
} midiEventPacket_t;
In my case, these changes do not affect the functionality of the code. I only have this board with the 32u4 to test, so I don't know if it works on other boards, but I don't see why not.
I don't see these warnings using the Arduino IDE 1.8.19 but this is because they added "-Wno-error=narrowing" to the compilation flags which hides the warning issues, but does not solve the problem.