Skip to content

Commit a8f9631

Browse files
committed
Add back preprocessor ifs
To avoid usage of (sometimes) unneeded libraries, but also to hopefully help supporting more boards.
1 parent 26f680f commit a8f9631

4 files changed

Lines changed: 36 additions & 24 deletions

File tree

Modules/Nano/RCnano/CheckShield.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
1+
#if SupportEthernet == 1
22
// uses SPI.h
33
// use 'if(ShieldFound())' before 'ether.begin(sizeof Ethernet::buffer, LocalMac, selectPin);' to prevent hang
44

@@ -65,4 +65,4 @@ static byte readOp(byte op, byte address) {
6565
disableChip();
6666
return result;
6767
}
68-
68+
#endif

Modules/Nano/RCnano/RCnano.ino

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,9 @@
1-
#include <Adafruit_MCP23008.h>
2-
#include <Adafruit_MCP23X08.h>
3-
#include <Adafruit_MCP23X17.h>
4-
#include <Adafruit_MCP23XXX.h>
51
#include <Wire.h>
62
#include <EEPROM.h>
73

8-
#include <Adafruit_BusIO_Register.h>
9-
#include <Adafruit_I2CDevice.h>
10-
#include <Adafruit_I2CRegister.h>
11-
#include <Adafruit_SPIDevice.h>
12-
13-
#include <SPI.h>
14-
#include <EtherCard.h>
15-
16-
# define InoDescription "RCnano : 29-Mar-2023"
4+
#define SupportEthernet 1 // Requires Ethercard library
5+
#define SupportMCP23017 1 // Requires Adafruit MCP23008 and MCP23017 libraries
6+
#define InoDescription "RCnano : 29-Mar-2023"
177
const int16_t InoID = 2903; // change to send defaults to eeprom
188
int16_t StoredID; // Defaults ID stored in eeprom
199

@@ -26,7 +16,7 @@ struct ModuleConfig
2616
uint8_t IPpart3 = 1; // IP address, 3rd octet
2717
uint8_t RelayOnSignal = 0; // value that turns on relays
2818
uint8_t FlowOnDirection = 0; // sets on value for flow valve or sets motor direction
29-
uint8_t UseMCP23017 = 1; // 0 use Nano pins for relays, 1 use MCP23017 for relays
19+
uint8_t UseMCP23017 = SupportMCP23017 == 1 ? 1 : 0; // 0 use Nano pins for relays, 1 use MCP23017 for relays
3020
uint8_t Relays[16];
3121
uint8_t Debounce = 3; // minimum ms pin change, base debounce
3222
};
@@ -62,6 +52,9 @@ struct SensorConfig
6252

6353
SensorConfig Sensor[2];
6454

55+
#if SupportEthernet == 1
56+
#include <SPI.h>
57+
#include <EtherCard.h>
6558
// If using the ENC28J60 ethernet shield these pins
6659
// are used by it and unavailable for relays:
6760
// 7,8,10,11,12,13. It also pulls pin D2 high.
@@ -79,6 +72,17 @@ unsigned int DestinationPort = 29999; // Rate Controller listening port
7972
byte Ethernet::buffer[300]; // udp send and receive buffer
8073
bool ENCfound;
8174
static byte selectPin = 10;
75+
#endif
76+
77+
#if SupportMCP23017 == 1
78+
#include <Adafruit_MCP23008.h>
79+
#include <Adafruit_MCP23X08.h>
80+
#include <Adafruit_MCP23X17.h>
81+
#include <Adafruit_MCP23XXX.h>
82+
#include <Adafruit_BusIO_Register.h>
83+
#include <Adafruit_I2CDevice.h>
84+
#include <Adafruit_I2CRegister.h>
85+
#include <Adafruit_SPIDevice.h>
8286

8387
Adafruit_MCP23X17 mcp;
8488

@@ -105,6 +109,8 @@ Adafruit_MCP23X17 mcp;
105109
#define Relay15 1
106110
#define Relay16 0
107111

112+
#endif
113+
108114
const uint16_t LOOP_TIME = 50; //in msec = 20hz
109115
uint32_t LoopLast = LOOP_TIME;
110116

@@ -204,16 +210,15 @@ void setup()
204210
if (MDL.SensorCount < 1) MDL.SensorCount = 1;
205211
if (MDL.SensorCount > 2) MDL.SensorCount = 2;
206212

213+
#if UseMCP23017 == 1
207214
if (MDL.UseMCP23017)
208-
{
209215
Serial.println("Using MCP23017 for relays.");
210-
}
211216
else
212-
{
217+
#endif
213218
Serial.println("Using Nano pins for relays.");
214-
}
215219

216220
Wire.begin();
221+
#if SupportMCP23017 == 1
217222
if (MDL.UseMCP23017)
218223
{
219224
// I/O expander on default address 0x20
@@ -262,6 +267,7 @@ void setup()
262267
}
263268
}
264269
else
270+
#endif
265271
{
266272
// Nano pins
267273
for (int i = 0; i < 16; i++)
@@ -282,6 +288,7 @@ void setup()
282288
attachInterrupt(digitalPinToInterrupt(Sensor[0].FlowPin), ISR0, FALLING);
283289
attachInterrupt(digitalPinToInterrupt(Sensor[1].FlowPin), ISR1, FALLING);
284290

291+
#if SupportEthernet == 1
285292
Serial.println("");
286293
Serial.println("Starting Ethernet ...");
287294

@@ -319,6 +326,7 @@ void setup()
319326
Serial.println("");
320327
Serial.println("Ethernet controller not found.");
321328
}
329+
#endif
322330

323331
Serial.println("");
324332
Serial.println("Finished Setup.");
@@ -358,15 +366,18 @@ void loop()
358366
{
359367
SendLast = millis();
360368
SendSerial();
369+
#if SupportEthernet == 1
361370
SendUDPwired();
371+
#endif
362372
}
363373

374+
#if SupportEthernet == 1
364375
if (ENCfound)
365376
{
366377
//this must be called for ethercard functions to work.
367378
ether.packetLoop(ether.packetReceive());
368379
}
369-
380+
#endif
370381
ReceiveSerial();
371382
}
372383

Modules/Nano/RCnano/Relays.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ void CheckRelays()
4444
NewLo |= PowerRelayLo;
4545
NewHi |= PowerRelayHi;
4646

47+
#if UseMCP23017 == 1
4748
if (MDL.UseMCP23017 == 1)
4849
{
4950
if (IOexpanderFound)
@@ -68,7 +69,7 @@ void CheckRelays()
6869
}
6970
}
7071
else
71-
{
72+
#endif
7273
// use Nano pins
7374
for (int j = 0; j < 2; j++)
7475
{
@@ -81,5 +82,4 @@ void CheckRelays()
8182
}
8283
}
8384
}
84-
}
8585
}

Modules/Nano/RCnano/UDPwiredComm.ino

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
1+
#if SupportEthernet == 1
22
byte UDPpacket[30];
33
uint16_t UDPpgn;
44

@@ -347,3 +347,4 @@ void ReceiveUDPwired(uint16_t dest_port, uint8_t src_ip[IP_LEN], uint16_t src_po
347347
}
348348
}
349349
}
350+
#endif

0 commit comments

Comments
 (0)