Skip to content

Commit bfc73d1

Browse files
committed
Udp examples
1 parent 85348fb commit bfc73d1

32 files changed

+342
-103
lines changed

ArduinoCore-Linux/cores/arduino/ArdStdio.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ class StdioDevice : public Stream {
126126

127127
};
128128

129-
extern StdioDevice Serial;
129+
static StdioDevice Serial;
130130

131131
}
132132

ArduinoCore-Linux/cores/arduino/Arduino.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include "RemoteSerial.h"
1212
#include "Hardware.h"
1313
#if !defined(SKIP_HARDWARE_SETUP)
14-
# include "HardwareSetup.h"
14+
# include "HardwareSetupRemote.h"
1515
#endif
1616
#if !defined(SKIP_HARDWARE_WIFI)
1717
# include "WiFi.h"
@@ -29,17 +29,10 @@
2929

3030
namespace arduino {
3131

32-
StdioDevice Serial; // output to screen
33-
HardwareImpl Hardware; // implementation for gpio, spi, i2c
34-
3532
#if !defined(SKIP_HARDWARE_WIFI)
3633
WifiMock WiFi; // So that we can use the WiFi
3734
#endif
3835

39-
#if !defined(SKIP_HARDWARE_SETUP)
40-
HardwareSetupRemoteClass HardwareSetup; // setup for implementation
41-
#endif
42-
4336

4437
#if PROVIDE_SERIALLIB
4538
SerialImpl Serial1("/dev/ttyACM0"); // output to serial port

ArduinoCore-Linux/cores/arduino/Arduino.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,16 @@
2828
// Not available under MSVC
2929
#define __attribute__(x) // nothing
3030
#define __builtin_constant_p(x) (0) // non-constant
31-
#endif
32-
33-
#if defined(_MSC_VER)
3431
// Temporary unsupported under Win/MSVC
3532
#define SKIP_HARDWARE_SETUP
3633
#define SKIP_HARDWARE_WIFI
3734
#endif
3835

39-
#include "ArduinoAPI.h"
40-
#include "ArdStdio.h"
4136
#include "Serial.h"
37+
#include "ArdStdio.h"
4238
#include "ArduinoLogger.h"
39+
#include "ArduinoAPI.h"
4340
#include "RemoteSerial.h"
44-
#include "HardwareSetup.h"
41+
#include "HardwareSetupRemote.h"
4542

4643
using namespace arduino;

ArduinoCore-Linux/cores/arduino/ArduinoLogger.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ namespace arduino {
1212

1313
class ArduinoLogger {
1414
public:
15+
ArduinoLogger() = default;
16+
1517
/**
1618
* @brief Supported log levels
1719
*

ArduinoCore-Linux/cores/arduino/EthernetUDP.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1515
*/
1616
#include "EthernetUDP.h"
17-
// #include <lwip/sockets.h>
18-
// #include <lwip/netdb.h>
1917
#include <errno.h>
2018
#include <fcntl.h>
2119
#include <netdb.h>

ArduinoCore-Linux/cores/arduino/Hardware.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,12 @@ class HardwareGPIO;
1212
* drive the specific interface
1313
**/
1414
struct HardwareImpl {
15-
public:
16-
HardwareImpl() {}
15+
HardwareImpl() = default;
1716
HardwareGPIO* gpio = nullptr;
1817
HardwareI2C* i2c = nullptr;
1918
HardwareSPI* spi = nullptr;
2019
};
2120

22-
extern HardwareImpl Hardware;
21+
static HardwareImpl Hardware;
2322

2423
} // namespace arduino

ArduinoCore-Linux/cores/arduino/HardwareSPI.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@
88

99
namespace arduino {
1010

11-
//HardwareSPI::HardwareSPI() {}
12-
13-
//HardwareSPI::~HardwareSPI() {}
14-
1511
uint8_t HardwareSPI::transfer(uint8_t data) {
1612
if (Hardware.spi != nullptr) {
1713
return Hardware.spi->transfer(data);
@@ -81,5 +77,6 @@ void HardwareSPI::end() {
8177
Hardware.spi->end();
8278
}
8379
}
80+
8481

8582
}

ArduinoCore-Linux/cores/arduino/HardwareSetup.h renamed to ArduinoCore-Linux/cores/arduino/HardwareSetupRemote.h

Lines changed: 50 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22

33
#include <exception>
44

5+
#include "ArduinoLogger.h"
56
#include "Hardware.h"
67
#include "RemoteGPIO.h"
78
#include "RemoteI2C.h"
89
#include "RemoteSPI.h"
910
#include "WiFiUdpStream.h"
1011

11-
// #include "RemoteI2S.h"
12-
1312
namespace arduino {
1413

1514
/**
@@ -18,21 +17,22 @@ namespace arduino {
1817

1918
class HardwareSetupRemoteClass {
2019
public:
21-
// as a default we use udp
22-
HardwareSetupRemoteClass(int port = 7000) {
23-
this->port = port;
24-
default_stream = new WiFiUDPStream();
25-
default_stream->begin(port);
20+
/// default constructor: you need to call begin() afterwards
21+
HardwareSetupRemoteClass() = default;
22+
23+
/// HardwareSetup uses the indicated stream
24+
HardwareSetupRemoteClass(Stream& stream) {
25+
begin(&stream, false);
2626
}
2727

28-
~HardwareSetupRemoteClass() { cleanup(); }
28+
/// HardwareSetup that uses udp
29+
HardwareSetupRemoteClass(int port) {
30+
this->port = port;
31+
}
2932

30-
// assigns the different protocols to the stream
33+
/// assigns the different protocols to the stream
3134
void begin(Stream* s, bool doHandShake = true) {
32-
cleanup();
33-
if (doHandShake) {
34-
handShake(s);
35-
}
35+
p_stream = s;
3636

3737
Hardware.i2c = &i2c;
3838
Hardware.spi = &spi;
@@ -41,45 +41,56 @@ class HardwareSetupRemoteClass {
4141
i2c.setStream(s);
4242
spi.setStream(s);
4343
gpio.setStream(s);
44+
45+
if (doHandShake) {
46+
handShake(s);
47+
}
4448
}
4549

46-
// start with the default udp stream.
50+
/// start with udp on the indicatd port
51+
void begin(int port){
52+
this->port = port;
53+
begin();
54+
}
55+
56+
/// start with the default udp stream.
4757
void begin() {
48-
if (default_stream == nullptr) {
49-
default_stream = new WiFiUDPStream();
50-
handShake(default_stream);
51-
IPAddress ip = default_stream->remoteIP();
52-
int port = default_stream->remotePort();
53-
default_stream->setTarget(ip, port);
54-
default_stream->write((const uint8_t*)"OK", 2);
55-
default_stream->flush();
58+
if (p_stream == nullptr) {
59+
default_stream.begin(port);
60+
handShake(&default_stream);
61+
IPAddress ip = default_stream.remoteIP();
62+
int port = default_stream.remotePort();
63+
default_stream.setTarget(ip, port);
64+
default_stream.write((const uint8_t*)"OK", 2);
65+
default_stream.flush();
66+
p_stream = &default_stream;
5667
}
5768
}
5869

5970
void end() {
60-
cleanup();
71+
if (p_stream == &default_stream) {
72+
default_stream.stop();
73+
}
6174
Hardware.i2c = nullptr;
6275
Hardware.spi = nullptr;
6376
Hardware.gpio = nullptr;
6477
}
6578

79+
auto& get_gpio() { return gpio; }
80+
auto& get_i2c() { return i2c; }
81+
auto& get_spi() { return spi; }
82+
6683
protected:
67-
WiFiUDPStream* default_stream;
84+
WiFiUDPStream default_stream;
85+
Stream *p_stream = nullptr;
6886
RemoteI2C i2c;
6987
RemoteSPI spi;
7088
RemoteGPIO gpio;
7189
int port;
7290

73-
void cleanup() {
74-
if (default_stream != nullptr) {
75-
delete default_stream;
76-
default_stream = nullptr;
77-
}
78-
}
79-
8091
void handShake(Stream* s) {
8192
while (true) {
82-
Logger.info("HardwareSetup", "waiting for device...");
93+
Logger.warning("HardwareSetup", "waiting for device...");
8394
try {
8495
// we wait for the Arduino to send us the Arduino-Emulator string
8596
if (s->available() >= 16) {
@@ -101,6 +112,11 @@ class HardwareSetupRemoteClass {
101112
}
102113
};
103114

104-
extern HardwareSetupRemoteClass HardwareSetup;
105-
115+
#if !defined(SKIP_HARDWARE_SETUP)
116+
static HardwareSetupRemoteClass HardwareSetupRemote{7000};
117+
#if !defined(USE_RPI)
118+
static auto& Wire = *Hardware.i2c;
119+
static auto& SPI = *Hardware.spi;
120+
#endif
121+
#endif
106122
} // namespace arduino

ArduinoCore-Linux/cores/arduino/Main.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,19 @@
88
__attribute__((weak)) void setup() {}
99
__attribute__((weak)) void loop() {}
1010

11+
void hardwareSetup(){
12+
#if !defined(SKIP_HARDWARE_SETUP)
13+
# if defined(USE_REMOTE)
14+
HardwareSetupRemote.begin();
15+
# endif
16+
# if (defined(USE_RPI))
17+
HardwareSetupRPI.begin();
18+
# endif
19+
#endif
20+
}
21+
1122
__attribute__((weak)) int main () {
23+
hardwareSetup();
1224
setup();
1325
while(true){
1426
loop();

ArduinoCore-Linux/cores/arduino/RemoteSPI.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ class RemoteSPI : public HardwareSPI {
3535
service.send((uint32_t)count);
3636
service.send(buf, count);
3737
service.flush();
38-
service.receive16();
38+
for (int j=0; j<count; j++) {
39+
((uint8_t*)buf)[j] = service.receive8();
40+
}
3941
}
4042

4143
void usingInterrupt(int interruptNumber) {

0 commit comments

Comments
 (0)