My sketch, barebones, compiles without complaint. The library includes my homegrown SAMD_port.h.
However, when I add my homegrown Functions_Lib.cpp to the library, even though it is not referenced in the barebones sketch, the compiler barfs.
/**
* @file MKR_sketch_barebones.ino
* @brief Example all libraries, min functionality -- to demonstrate
* @author Robert Hadow
* @date February 2021
* @par Revision History:
*
**/
#include <SD.h>
//#define CHIPSEL_594X /**< AD5940 or AD5941 Remember to uncomment inside ad5940.h file */
#include <TimeLib.h> // remember to delete or rename Time.h in the Time library
#include <ad5940.h>
#include "SAMD_Port.h" // My own header
#include <RTCZero.h>
#include <FlashStorage.h>
#include <ArduinoLowPower.h>
#include <SPI.h>
#include <Wire.h>
//#include "Functions_Lib.cpp"
// Global declarations for SPI
const char chipSelectSD = 10; // for SD card check this https://www.arduino.cc/en/Guide/MKRMEMShield
const char chipSelectAD = 10; // for AD chip
int delayTime = 50;
// Global declarations for time
time_t posixTimeInt = 1614262756; //Number of seconds since 1970-01-01-00:00:00 (Unix EPOCH)
tmElements_t posixTimeStr;
// Global declarations for SD utility library functions: https://www.arduino.cc/en/Tutorial/LibraryExamples/CardInfo
Sd2Card card;
SdVolume volume;
SdFile root;
// see above for pin assignments
//Global declarations for storing deviceID in flash
// Create a structure that describes *this hardware unit*.
// The "valid" variable is set to "true" once
// the structure is filled with actual data for the first time.
struct deviceDescription {
boolean valid;
unsigned int bigSerialNumber;
unsigned int buildDate;
};
// Create a "deviceDescription" structure and call it "thisDevice"
struct deviceDescription thisDevice; // playing 2021/02/26
// Make a place for deviceID in the flash
FlashStorage(deviceID, deviceDescription);
// I replaced SERIAL_PORT_MONITOR with Serial 2021-02-24 1614
void setup() {
Serial.begin(9600);
// Serial.begin(9600);
while (!Serial) { }
//while (!SERIAL_PORT_MONITOR) { }
//displayPowerState();
// Then crank up SPI (trying SD card first)
pinMode(chipSelectSD, OUTPUT);
SPI.begin();
SPI.setBitOrder(MSBFIRST);
// Check SD card
Serial.print("\nInitializing SD card...");
// we'll use the initialization code from the utility libraries
// since we're just testing if the card is working!
if (!card.init(SPI_HALF_SPEED, chipSelectSD)) {
Serial.println("SD initialization failed.");
while (1);
} else {
Serial.println("SD card is operational.");
}
// Then crank up AD5940
} // end of setup
void loop() {
// main code here, running repeatedly:
}
The error comes like this:
In file included from C:\Users\Robert\Documents\ArduinoData\packages\arduino\hardware\samd\1.8.10\libraries\Wire/Wire.h:24:0,
from C:\Users\Robert\Documents\Arduino\libraries\SAMD_PORT-master\Functions_Lib.cpp:11:
C:\Users\Robert\Documents\Arduino\libraries\SAMD_PORT-master\Functions_Lib.cpp: In function 'void displayPowerState()':
C:\Users\Robert\Documents\ArduinoData\packages\arduino\hardware\samd\1.8.10\variants\mkrwifi1010/variant.h:223:39: error: 'SerialUSB' was not declared in this scope
#define Serial SerialUSB
^
C:\Users\Robert\Documents\Arduino\libraries\SAMD_PORT-master\Functions_Lib.cpp:30:7: note: in expansion of macro 'Serial'
Serial.println("Battery Not Charging.");
^~~~~~
Here is the homegrown H file that works fine.
/**
* @file SAMD_Port.h
* @brief AD5940 library. This file contains AD5940 library functions required of user, specific to MCU SAMD
* @author Robert Hadow
* @date February 2021
* @par Revision History:
*
* For more information see: https://wiki.analog.com/resources/eval/user-guides/eval-ad5940/tools/porting_source_code
*
**/
void AD5940_CsClr(void);
void AD5940_CsSet(void);
void AD5940_RstClr(void);
void AD5940_RstSet(void);
void AD5940_Delay10us(uint32_t time);
void AD5940_ReadWriteNBytes(unsigned char *pSendBuffer,unsigned char *pRecvBuff,unsigned long length);
Here is the offending cpp file that the compiler processes, even though it's not part of the barebone INO.
/**
* @file Functions_Lib.cpp
* @brief Functions for BIG device.
* @author Robert Hadow
* @date February 2021
* @par Revision History:
*
*
**/
#include <Wire.h>
#define PMIC_ADDRESS 0x6B
#define PMIC_REG08 0x08
#ifndef SERIAL_PORT_MONITOR
#define SERIAL_PORT_MONITOR Serial
#endif
void displayPowerState() {
Wire.beginTransmission(PMIC_ADDRESS);
Wire.write(PMIC_REG08);
Wire.requestFrom(PMIC_ADDRESS, 1);
int8_t ps = Wire.read(); // ps power state
Wire.endTransmission();
ps = (ps & 24) >> 4; //mask and shift
switch(ps) {
case 00:
Serial.println("Battery Not Charging.");
case 01:
Serial.println("Battery Pre-charge VBATLOWV>");
case 10:
Serial.println("Battery Fast Charging.");
case 11:
Serial.println("Battery charge complete.");
default:
Serial.println("No battery charging information available.");
}
}
Question (1): Why does the IDE compiler touch the Function.Lib.cpp file when it isn't referenced in the INO sketch at all?
Question (2): Kernighan and Ritchie tell me that a control line of the form
#define identifier token-sequence
causes the preprocessor to replace subsequent sequences of the identifier with the given sequence of tokens ... A second #define for the same identifier is erroneous unless the second token is identical to the first ...
Is the order in which the #include statements appear important?
Question (3): What boneheaded mistake did I make? I do want to include the function(s) described in Function_Lib.cpp into my INO.