65 lines
1.9 KiB
C++
65 lines
1.9 KiB
C++
// Used for non-EthernetUDPWrapper_h relay control
|
|
#ifndef NonLatchingRelay_h
|
|
#include "EthernetUDPWrapper.h"
|
|
#endif
|
|
|
|
#ifndef Arduino_H
|
|
#include <Arduino.h>
|
|
#endif
|
|
|
|
EthernetUDPWrapper::EthernetUDPWrapper(unsigned int pin, unsigned int port, IPAddress ip) : _ip(ip), _UDP(){
|
|
this->_udp_port = port;
|
|
this->_CS_pin = pin;
|
|
}
|
|
|
|
bool EthernetUDPWrapper::begin(){
|
|
Ethernet.init(this->_CS_pin);
|
|
Ethernet.begin(this->_mac, this->_ip);
|
|
|
|
// Check for Ethernet hardware present
|
|
if (Ethernet.hardwareStatus() == EthernetNoHardware) { return false; }
|
|
if (Ethernet.linkStatus() == LinkOFF) { return false;}
|
|
|
|
this->_UDP.begin(this->_udp_port);
|
|
|
|
return true;
|
|
}
|
|
|
|
// Checks for a new message available and buffers it into _incoming_packet_buffer
|
|
bool EthernetUDPWrapper::is_message_available() {
|
|
this->_incoming_packet_size = this->_UDP.parsePacket();
|
|
|
|
if (this->_incoming_packet_size > 0) {
|
|
this->_UDP.read(this->_incoming_packet_buffer, UDP_TX_PACKET_MAX_SIZE);
|
|
this->_remote_ip_address = this->_UDP.remoteIP();
|
|
this->_remote_udp_port = this->_UDP.remotePort();
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// Returns the pointer to the c_string with the message
|
|
char* EthernetUDPWrapper::get_incoming_packet() {
|
|
return &(this->_incoming_packet_buffer[0]);
|
|
}
|
|
|
|
// Returns the length of the buffered incoming packet
|
|
int EthernetUDPWrapper::get_incoming_packet_length() {
|
|
return this->_incoming_packet_size;
|
|
}
|
|
|
|
// Sends a response to the last sender
|
|
void EthernetUDPWrapper::send_response(char* msg, int length) {
|
|
if (msg == nullptr || length <= 0) {
|
|
return;
|
|
}
|
|
|
|
this->_UDP.beginPacket(this->_remote_ip_address, this->_remote_udp_port);
|
|
for (int i = 0; i < length; i++) {
|
|
this->_UDP.write(*(msg++));
|
|
}
|
|
|
|
this->_UDP.endPacket();
|
|
memset(this->_incoming_packet_buffer, 0, UDP_TX_PACKET_MAX_SIZE);
|
|
} |