For those who are interested I've generated a workable solution to this problem that allows HID/CDC/USB to be turned off (or on) by just editing one file. Here's how
In with the libraries create a new folder called 'USBcontrol'
Copy this little 'USBcongig.h file into it.
//USBconfig.h Switches to control how much USB code is included
#define NO_HID 1 //1= switch off HID: 0 turns it back on
#define NO_CDC 1 //1= switch off CDC: 0 turns it back on
#define NO_USB 1 //1= switch off all USB: 0 turns it back on
Now modify 'main.cpp' as follows:
/*
main.cpp - Main loop for Arduino sketches
Copyright (c) 2005-2013 Arduino Team. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Modified by Bruce Durant April 2015 to allow USB code for the 32U4 Leonardo
type boards to be ecluded from the compile
*/
#include <Arduino.h>
#include <C:\Program Files\Arduino\libraries\USBcontrol\USBconfig.h> //new line to point to where 'NO_USB' is defined
//Declared weak in Arduino.h to allow user redefinitions.
int atexit(void (*func)()) { return 0; }
// Weak empty variant initialization function.
// May be redefined by variant files.
void initVariant() __attribute__((weak));
void initVariant() { }
int main(void)
{
init();
initVariant();
#if !NO_USB //wrap #if defined(USBCON)etc in #if - #endif
#if defined(USBCON)
USBDevice.attach();
#endif
#endif
setup();
for (;;) {
loop();
if (serialEventRun) serialEventRun();
}
return 0;
}
finally modify the 'USBDesc.h' file
/* Copyright (c) 2011, Peter Barrett
**
** Permission to use, copy, modify, and/or distribute this software for
** any purpose with or without fee is hereby granted, provided that the
** above copyright notice and this permission notice appear in all copies.
**
** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
** SOFTWARE.
Modified by Bruce Durant April 2015 to allow USB code for the 32U4 Leonardo
type boards to be ecluded from the compile
*/
#include <C:\Program Files\Arduino\libraries\USBcontrol\USBconfig.h> //new line to point to where to find 'NO_HID' & 'NO_CDC' are defined
#if !NO_CDC //wrap #define CDC_ENABLED in #if - #endif
#define CDC_ENABLED
#endif
#if !NO_HID //wrap #define HID_ENABLED in #if - #endif
#define HID_ENABLED
#endif
#ifdef CDC_ENABLED
#define CDC_INTERFACE_COUNT 2
#define CDC_ENPOINT_COUNT 3
#else
#define CDC_INTERFACE_COUNT 0
#define CDC_ENPOINT_COUNT 0
#endif
#ifdef HID_ENABLED
#define HID_INTERFACE_COUNT 1
#define HID_ENPOINT_COUNT 1
#else
#define HID_INTERFACE_COUNT 0
#define HID_ENPOINT_COUNT 0
#endif
#define CDC_ACM_INTERFACE 0 // CDC ACM
#define CDC_DATA_INTERFACE 1 // CDC Data
#define CDC_FIRST_ENDPOINT 1
#define CDC_ENDPOINT_ACM (CDC_FIRST_ENDPOINT) // CDC First
#define CDC_ENDPOINT_OUT (CDC_FIRST_ENDPOINT+1)
#define CDC_ENDPOINT_IN (CDC_FIRST_ENDPOINT+2)
#define HID_INTERFACE (CDC_ACM_INTERFACE + CDC_INTERFACE_COUNT) // HID Interface
#define HID_FIRST_ENDPOINT (CDC_FIRST_ENDPOINT + CDC_ENPOINT_COUNT)
#define HID_ENDPOINT_INT (HID_FIRST_ENDPOINT)
#define INTERFACE_COUNT (MSC_INTERFACE + MSC_INTERFACE_COUNT)
#ifdef CDC_ENABLED
#define CDC_RX CDC_ENDPOINT_OUT
#define CDC_TX CDC_ENDPOINT_IN
#endif
#ifdef HID_ENABLED
#define HID_TX HID_ENDPOINT_INT
#endif
#define IMANUFACTURER 1
#define IPRODUCT 2
You might need to modify the two #include lines at the beginning of these two pieces of code if your libraries/chosen location for the 'USBconfih.h' file is different to mine.
Now when the compiler goes and grabs all the bits of code that have been called up it will skip over HID or CDC or all the USB stuff as defined in the USBconfig.h file. This has exactly the same effect as if you had 'remmed' out the '#define CDC_ENABLED' etc but you don't need to go digging in the labberinth to re-modify the standard code to turn these things back on for the next project.
Word of warning though once you compile a sketch with USB turned off you need to uploadit with a ISP and you have no access to the USB/serial outputs to monitor.
The inverted logic is necessary because by default if you want USB to work you can just rename USBConfig.h as USBconfigON.h in your system then as far as the IDE is concerned the three values of NO_HID etc don't exist and USBDesc.h/main.cpp will be enabled.
This is not a good a putting a flag/global variable in your sketch to turn one of these thing off but that doesn't work. I suspect that is because the pre compile does not know the value of said variable when it first rounds up all the bits of code so they get included rather than left out.
You can probably do even better doing something similar to the 'iom32u4.h' file and its friends but that is used in a lot of places and I ran into some problems when I tried.