Skip to content
This repository was archived by the owner on Mar 25, 2021. It is now read-only.

Stuck on testing Ethernet Class #14

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .arduino-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ unittest:
platforms:
- mega2560
libraries:
- "Ethernet"
- "LiquidCrystal"
- "RTClib"

compile:
platforms:
- mega2560
libraries:
- "Ethernet"
- "LiquidCrystal"
- "RTClib"

31 changes: 31 additions & 0 deletions src/Devices/Ethernet_TC.cpp
Original file line number Diff line number Diff line change
@@ -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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the begin() method is successful, does IP get initialized?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The IP is either initialized through DHCP in the begin(mac) contructor, or (I moved the constructor so it won't be declared unless needed) it will be given a defaultIP with the begin(mac, IP) constructor

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;
}
}
29 changes: 29 additions & 0 deletions src/Devices/Ethernet_TC.h
Original file line number Diff line number Diff line change
@@ -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
};
10 changes: 10 additions & 0 deletions test/Ethernet.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <Arduino.h>
#include <ArduinoUnitTests.h>

#include "Ethernet_TC.h"

unittest(test) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that we should be able to do some testing! Get a reference to the singleton, query for the IP, renew the lease, etc.

assertTrue(true);
}

unittest_main()