#include <SPI.h>
#include <Ethernet.h>
#include <Dns.h>
#include <SD.h>
#include <mac.h>
// ------ LCD ----
const int ADDRESS_BUFFER = 25;
const int SCRATCH_BUFFER = 25;
const int MAX_RES = 15;
const int LCD_NOTE_A = 220;
const int LCD_BACKLIGHT_ON = 17;
const int LCD_CLEAR = 12;
const int LCD_LF = 10;
//--------Network ---------
DNSClient dnsClient;
bool bNetworkAvailable = false;
struct address_struct
{
char szMacAddress[ADDRESS_BUFFER] = {0}; // MAC Address String
char szLcdMacAddress[ADDRESS_BUFFER]={0}; // MAC Address on LCD (no '-' chars...
char szIpAddress[ADDRESS_BUFFER]={0}; // Ip Address String
char szDnsAddress[ADDRESS_BUFFER]={0}; // DNS Address String
char szGatewayAddress[ADDRESS_BUFFER]={0}; // Default Gateway Address String
char szWebServiceAddress[ADDRESS_BUFFER]={0}; // Web Service Address
};
//--------address struct------
address_struct address;
//---------- Buffers -----------
char szHostName[SCRATCH_BUFFER]={0}; // buffer for IP host name
char szLine1[MAX_RES]={0};
char szLine2[MAX_RES]={0};
//---------- SD card stuff--------
bool bCardRead = false;
//-------- SPI Control -----
const int CS_ETHERNET = 10;
const int CS_SDCARD = 4;
/////////////////////////////// Setup /////////////////////
void setup()
{
Serial3.begin(9600);
pinMode(4, INPUT_PULLUP);
pinMode(10, INPUT_PULLUP);
LCD_Display("Initializing", "Please wait");
delay(1000);
bCardRead = TestCardAccess();
if(bCardRead == true)
{
Serial.println("Card Reader OK");
}
else
{
//Failure to read the configuration card
LCD_Display("SD Card Read", "-FAILED-");
Serial3.write(LCD_NOTE_A);
Serial3.write(LCD_NOTE_A);
exit(1);
}
Serial3.write(LCD_BACKLIGHT_ON);
LCD_Display("Test", "Ready");
//Use DHCP if possible
bNetworkAvailable=MakeDHCPRequest(true);
if(bNetworkAvailable == true)
{
// print the Ethernet board/shield's IP address:-
LCD_Display("IP Address:", address.szIpAddress);
}
else
{
LCD_Display("DHCP Request", "-FAILED-");
}
}
///////////////////////////// The main loop ////////////////////////////
void loop()
{
delay(5000);
Serial.println("Testing");
}
/*------------------------------------------------------------------
LCD_Display
Display contents of a buffer on the 16x2 LCD grid. The first line
is used to identify Lenox Laser's controller, while the 2nd line
can be used for custom display.
IN: pointer to a char buffer for line 1 (16 chars max)
IN: pointer to a char buffer for line 2 (16 chars max)
Returns: void
------------------------------------------------------------------*/
void LCD_Display(char* szLine1, char* szLine2)
{
Serial3.write(LCD_CLEAR);
if(szLine1)
{
Serial3.println(szLine1);
}
if(szLine2)
{
Serial3.write(LCD_LF);
Serial3.println(szLine2);
}
Serial3.write(LCD_NOTE_A
}
/*------------------------------------------------------------------
TestCardAccess
This function updates a value on the SDCard
Returns: void
------------------------------------------------------------------*/
bool TestCardAccess()
{
bool bCardRead = false;
if (!SD.begin(CS_SDCARD))
{
Serial.println("A SD Card was not found...");
}
else
{
Serial.println("A SD Card was successfully initialized...");
}
return bCardRead;
}
/*---------------------------------------------------------------
MakeDHCPRequest()
Attempts a DHCP request for network connectivity.
IN: bLCD - Boolean to indicate whether to display the network
initialization attempt.
Returns: Boolean result
---------------------------------------------------------------*/
bool MakeDHCPRequest(bool bLCD)
{
if(bLCD == true)
{
LCD_Display("Initializing", "Network...");
}
Serial.println("Getting address information, please wait...");
if (Ethernet.begin(GetFirmwareMAC()))
{
Serial3.write(LCD_NOTE_A);
dnsClient.begin(Ethernet.dnsServerIP());
return true;
}
else
{
Serial.println("Failed to get address from DHCP server");
Serial3.write(LCD_CLEAR);
Serial3.println("Address FAIL!");
Serial3.write(LCD_NOTE_A);
return false;
}
}
/*------------------------------------------------------------------
GetFirmwareMAC
Reads the firmware MAC burned into teensy 3.0+. Needs the mac.h
header...
IN: void
Returns: a pointer to a SRAM byte buffer that can be used by the
Ethernet object's begin() method.
-----------------------------------------------------------------*/
byte* GetFirmwareMAC()
{
read_mac();
sprintf(address.szMacAddress, "%02x-%02x-%02x-%02x-%02x-%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
sprintf(address.szLcdMacAddress, "%02x%02x%02x%02x%02x%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
sprintf(szHostName, "WIZnet0%01x%02x%02x", mac[3], mac[4], mac[5]);
return mac;
}
Reply With Quote Reply With Quote