You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/Berry.md
+56Lines changed: 56 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -809,6 +809,62 @@ Module `webserver` also defines the following constants:
809
809
810
810
See the [Berry Cookbook](Berry-Cookbook.md) for examples.
811
811
812
+
### `udp` class
813
+
814
+
Class `udp` provides ability to send and received UDP packets, including multicast addresses.
815
+
816
+
You need to create an object of class `udp`. Such object can send packets and listen to local ports. If you don't specify a local port, the client will take a random source port. Otherwise the local port is used as source port.
817
+
818
+
When creating a local port, you need to use `udp->begin(<ip>, <port)>`. If `<ip>` is empty string `""` then the port is open on all interfaces (wifi and ethernet).
819
+
820
+
General Function|Parameters and details
821
+
:---|:---
822
+
udp()<aclass="cmnd"id="udp_ctor">|`udp() -> <instance udp>`<br>Creates an instance of `udp` class.
823
+
begin<aclass="cmnd"id="udp_begin">|`begin(ip:string, port:int) -> bool`<BR>Create a UDP listener and sender on interface `ip` and `port`. If `ip` is an empty string, the listener connects to all interfaces (aka 0.0.0.0)<BR>Returns `true` if successful.
824
+
begin_multicast<aclass="cmnd"id="udp_begin_mcast">|`begin(ip:string, port:int) -> bool`<BR>Create a UDP listener and sender on interface `ip` and `port`. `ip` must be a multicast address.<BR>Returns `true` if successful.
825
+
send<aclass="cmnd"id="udp_send">|`send(addr:string, port:int, payload:bytes) -> bool`<BR>Sends a packet to address `addr`, port `port` and message as `bytes()` buffer.<BR>Returns `true` if successful.
826
+
send_multicast<aclass="cmnd"id="udp_send_mcast">|`send(payload:bytes) -> bool`<BR>Sends a payload as `bytes()` buffer to the multicast address. `begin_multicast()` must have been previously called.<BR>Returns `true` if successful.
827
+
read<aclass="cmnd"id="udp_read">|`read() -> bytes() or nil`<BR>Reads any received udp packet as bytes() buffer, or `nil` if no packet was received.
828
+
829
+
#### Sending udp packets
830
+
831
+
```ruby
832
+
> u = udp()
833
+
> u.begin("", 2000) # listen on all interfaces, port 2000
834
+
true
835
+
> u.send("192.168.1.10", 2000, bytes("414243")) # send 'ABC' to 192.168.1.10:2000, source port is 2000
836
+
true
837
+
```
838
+
839
+
#### Receive udp packets
840
+
841
+
You need to do polling on `udp->read()`. If no packet was received, the call immediately returns `nil`.
842
+
843
+
```ruby
844
+
> u = udp()
845
+
> u.begin("", 2000) # listen on all interfaces, port 2000
846
+
true
847
+
> u.read() # if no packet received, returns `nil`
848
+
>
849
+
850
+
> u.read() # if no packet received, returns `nil`
851
+
bytes("414243") # received packet as `bytes()`
852
+
```
853
+
854
+
#### Send and receive multicast
855
+
856
+
```ruby
857
+
> u = udp()
858
+
> u.begin_multicast("224.3.0.1", 3000) # connect to multicast 224.3.0.1:3000 on all interfaces
859
+
true
860
+
861
+
> client.send_multicast(bytes("33303030"))
862
+
863
+
> u.read() # if no packet received, returns `nil`
864
+
> u.read()
865
+
bytes("414243") # received packet as `bytes()`
866
+
```
867
+
812
868
### Adressable leds (WS2812, SK6812)
813
869
814
870
There is native support for adressable leds via NeoPixelBus, with support for animations. Currently supported: WS2812, SK6812.
0 commit comments