diff --git a/.arduino-ci.yml b/.arduino-ci.yml index 37f758b2..bc6280df 100644 --- a/.arduino-ci.yml +++ b/.arduino-ci.yml @@ -16,6 +16,7 @@ unittest: platforms: - mega2560 libraries: + - "Ethernet" - "LiquidCrystal" - "RTClib" @@ -23,6 +24,6 @@ compile: platforms: - mega2560 libraries: + - "Ethernet" - "LiquidCrystal" - "RTClib" - diff --git a/src/Devices/Ethernet_TC.cpp b/src/Devices/Ethernet_TC.cpp new file mode 100644 index 00000000..209f8de8 --- /dev/null +++ b/src/Devices/Ethernet_TC.cpp @@ -0,0 +1,31 @@ +#include "Ethernet_TC.h" + +Ethernet_TC *Ethernet_TC::_instance = nullptr; + +// Establishes the Ethenet connection and sets class variables +Ethernet_TC::Ethernet_TC() { + pinMode(4, OUTPUT); + digitalWrite(4, HIGH); + if (Ethernet.begin(mac) == 0) { + Serial.println(F("Failed to configure Ethernet using DHCP")); + IPAddress defaultIP = IPAddress(192, 168, 1, 2); + Ethernet.begin(mac, defaultIP); + IP = defaultIP; + } +} + +Ethernet_TC *Ethernet_TC::getInstance() { + if (_instance == nullptr) { + _instance = new Ethernet_TC; + } + return _instance; +} + +void Ethernet_TC::renewDHCPLease() { + unsigned long current_millis = millis(); + + if (current_millis - previous_lease >= LEASE_INTERVAL) { + Ethernet.maintain(); + previous_lease = current_millis; + } +} diff --git a/src/Devices/Ethernet_TC.h b/src/Devices/Ethernet_TC.h new file mode 100644 index 00000000..40d55063 --- /dev/null +++ b/src/Devices/Ethernet_TC.h @@ -0,0 +1,29 @@ +/* +Based off Arduino's Ethernet library +Implements a wrapper for the Ethernet Class +*/ + +#pragma once + +#include "Ethernet.h" + +class Ethernet_TC { +public: + static Ethernet_TC *getInstance(); + IPAddress getIP() { + return IP; + }; + void renewDHCPLease(); + +protected: + Ethernet_TC(); + +private: + static Ethernet_TC *_instance; + byte mac[6] = {0x90, 0xA2, 0xDA, 0x00, 0x00, 0x00}; + IPAddress defaultIP; + IPAddress time_serverIP; + IPAddress IP; + unsigned long previous_lease; + const unsigned long LEASE_INTERVAL = 345600000; // 4 days in milliseconds +}; diff --git a/test/Ethernet.cpp b/test/Ethernet.cpp new file mode 100644 index 00000000..b9ad9ee8 --- /dev/null +++ b/test/Ethernet.cpp @@ -0,0 +1,10 @@ +#include +#include + +#include "Ethernet_TC.h" + +unittest(test) { + assertTrue(true); +} + +unittest_main()