File tree Expand file tree Collapse file tree 4 files changed +43
-7
lines changed
Filter options
Expand file tree Collapse file tree 4 files changed +43
-7
lines changed
Original file line number Diff line number Diff line change 32
32
#include " Udp.h"
33
33
34
34
/* Start UDP socket, listening at local port PORT */
35
- void UdpClass::begin (uint16_t port) {
35
+ uint8_t UdpClass::begin (uint16_t port) {
36
+ if (_sock != MAX_SOCK_NUM)
37
+ return 0 ;
38
+
39
+ for (int i = 0 ; i < MAX_SOCK_NUM; i++) {
40
+ uint8_t s = W5100.readSnSR (i);
41
+ if (s == SnSR::CLOSED || s == SnSR::FIN_WAIT) {
42
+ _sock = i;
43
+ break ;
44
+ }
45
+ }
46
+
47
+ if (_sock == MAX_SOCK_NUM)
48
+ return 0 ;
49
+
36
50
_port = port;
37
- _sock = 0 ; // TODO: should not be hardcoded
38
51
socket (_sock, SnMR::UDP, _port, 0 );
52
+
53
+ return 1 ;
39
54
}
40
55
41
56
/* Send packet contained in buf of length len to peer at specified ip, and port */
@@ -129,8 +144,15 @@ port = myPort;
129
144
return ret;
130
145
}
131
146
147
+ /* Release any resources being used by this UdpClass instance */
148
+ void UdpClass::stop ()
149
+ {
150
+ if (_sock == MAX_SOCK_NUM)
151
+ return ;
132
152
153
+ close (_sock);
133
154
155
+ EthernetClass::_server_port[_sock] = 0 ;
156
+ _sock = MAX_SOCK_NUM;
157
+ }
134
158
135
- /* Create one global object */
136
- UdpClass Udp;
Original file line number Diff line number Diff line change @@ -45,7 +45,8 @@ class UdpClass {
45
45
uint16_t _port; // local port to listen on
46
46
47
47
public:
48
- void begin (uint16_t ); // initialize, start listening on specified port
48
+ UdpClass () : _sock(MAX_SOCK_NUM) {};
49
+ uint8_t begin (uint16_t ); // initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use
49
50
int available (); // has data been received?
50
51
51
52
// C-style buffer-oriented functions
@@ -56,8 +57,8 @@ class UdpClass {
56
57
// readPacket that fills a character string buffer
57
58
int readPacket (char *, uint16_t , uint8_t *, uint16_t &);
58
59
60
+ // Finish with the UDP socket
61
+ void stop ();
59
62
};
60
63
61
- extern UdpClass Udp;
62
-
63
64
#endif
Original file line number Diff line number Diff line change @@ -35,6 +35,8 @@ unsigned int remotePort; // holds received packet's originating port
35
35
char packetBuffer[UDP_TX_PACKET_MAX_SIZE ]; // buffer to hold incoming packet,
36
36
char ReplyBuffer [] = " acknowledged" ; // a string to send back
37
37
38
+ // A UDP instance to let us send and receive packets over UDP
39
+ UdpClass Udp ;
38
40
39
41
void setup () {
40
42
// start the Ethernet and UDP:
Original file line number Diff line number Diff line change @@ -37,6 +37,9 @@ const int NTP_PACKET_SIZE= 48; // NTP time stamp is in the first 48 bytes of the
37
37
38
38
byte packetBuffer[ NTP_PACKET_SIZE ]; // buffer to hold incoming and outgoing packets
39
39
40
+ // A UDP instance to let us send and receive packets over UDP
41
+ UdpClass Udp ;
42
+
40
43
void setup ()
41
44
{
42
45
// start Ethernet and UDP
@@ -80,8 +83,16 @@ void loop()
80
83
Serial . print(" The UTC time is " ); // UTC is the time at Greenwich Meridian (GMT)
81
84
Serial . print((epoch % 86400L ) / 3600 ); // print the hour (86400 equals secs per day)
82
85
Serial . print(' :' );
86
+ if ( ((epoch % 3600 ) / 60 ) < 10 ) {
87
+ // In the first 10 minutes of each hour, we'll want a leading '0'
88
+ Serial . print(' 0' );
89
+ }
83
90
Serial . print((epoch % 3600 ) / 60 ); // print the minute (3600 equals secs per minute)
84
91
Serial . print(' :' );
92
+ if ( (epoch % 60 ) < 10 ) {
93
+ // In the first 10 seconds of each minute, we'll want a leading '0'
94
+ Serial . print(' 0' );
95
+ }
85
96
Serial . println(epoch % 60 ); // print the second
86
97
}
87
98
// wait ten seconds before asking for the time again
You can’t perform that action at this time.
0 commit comments