Merge pull request #158 from RobTillaart/develop

update examples
This commit is contained in:
Rob Tillaart 2020-05-24 13:31:24 +02:00 committed by GitHub
commit 6680ecdffd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 428 additions and 14 deletions

View File

@ -0,0 +1,294 @@
//
// FILE: LineFormatter_Ethernet.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: demo LineFormatter for EthernetClient
// DATE: 2020-05-23
// URL: https://github.com/RobTillaart/LineFormatter
//
#include <LineFormatter.h>
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip( 192, 168, 1, 177);
const int EthernetPIN = 10;
EthernetServer server(80); // change to your config
char httpRequest[40];
uint8_t reqCnt;
////////////////////////////////////////////////////////////////////
//
// HTTP HELPER CODE
//
void HTTP_header(EthernetClient* cl, const char *contentType, bool keepAlive = false, int refresh = 5)
{
cl->println("HTTP/1.1 200 OK");
cl->print("Content-Type: ");
cl->println( contentType );
cl->println("Connection: ");
cl->println(keepAlive ? "keep-alive" : "close");
cl->println("Refresh: ");
cl->println(refresh);
cl->println();
}
////////////////////////////////////////////////////////////////////
//
// Based upon webServer example demo
//
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
// Ethernet.init(pin) to configure the CS pin
Ethernet.init(EthernetPIN);
Ethernet.begin(mac, ip);
if (Ethernet.hardwareStatus() == EthernetNoHardware)
{
Serial.println("No hardware found");
while (1);
}
Serial.println(Ethernet.hardwareStatus());
Serial.println(Ethernet.linkStatus());
if (Ethernet.linkStatus() == LinkOFF)
{
Serial.println("Cable is not connected.");
Serial.println("Connect cable");
}
while (Ethernet.linkStatus() == LinkOFF); // wait for cable
server.begin();
Serial.print("ServerIP: ");
Serial.println(Ethernet.localIP());
}
void loop()
{
// listen for incoming clients
EthernetClient client = server.available();
if (client)
{
Serial.print("\n<CONNECTION>\n ");
// Serial.println(client.remoteIP());
// Serial.println(client.remotePort());
// an http request ends with a blank line
boolean currentLineIsBlank = true;
reqCnt = 0;
while (client.connected())
{
if (client.available())
{
char c = client.read();
Serial.write(c); // collect HHTP request here..
if (reqCnt < 39)
{
httpRequest[reqCnt++] = c;
httpRequest[reqCnt] = 0;
}
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank)
{
if (strstr(httpRequest, "table1.txt"))
{
uint32_t start = micros();
// send a standard http response header
HTTP_header(&client, "text/txt", true, 5);
LineFormatter LF(&client);
test_table_1(&LF);
uint32_t stop = micros();
Serial.println(stop - start);
Serial.println(LF.getLineCount());
Serial.println(LF.getLineCount() * 1e6 / (stop - start));
break;
}
if (strstr(httpRequest, "table2.txt"))
{
uint32_t start = micros();
// send a standard http response header
HTTP_header(&client, "text/txt", true, 5);
LineFormatter LF(&client);
test_table_2(&LF);
uint32_t stop = micros();
Serial.println(stop - start);
Serial.println(LF.getLineCount());
Serial.println(LF.getLineCount() * 1e6 / (stop - start));
break;
}
// default page is simple HTML
// send a standard http response header
HTTP_header(&client, "text/html", true, 5);
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<head>");
client.println("<title>Demo LineFormatter</title>");
client.println("</head>");
client.println("<body>");
client.println("<h1>Demo LineFormatter for EthernetClient</h1>");
client.println("<p>Get <a href=\"table1.txt\">TXT 1</a>.</p>");
client.println("<p>Get <a href=\"table2.txt\">TXT 2</a>.</p>");
client.println("</body>");
client.println("</html>");
break;
}
if (c == '\n') {
Serial.print(" ");
// you're starting a new line
currentLineIsBlank = true;
} else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
client.stop();
Serial.println("</CONNECTION>");
}
}
void test_table_1(LineFormatter L)
{
L.println();
L.println(__FUNCTION__);
L.repeat(strlen(__FUNCTION__), "=", 2);
L.clearTabs();
L.addTab(3);
L.addTab(10);
L.addTab(20);
L.addTab(30);
L.addRelTab(6);
L.addRelTab(6);
L.addRelTab(10);
L.addRelTab(6);
L.addRelTab(6);
int measurement = 1;
L.println("\tIdx\tTime\tValue\tA0\tA1\tA2\tA3\tA4\tA5");
L.repeat(8, "----+----|", 1);
L.setAutoNewLine(5);
for (int i = 0; i < 20; i++)
{
if (i == 10)
{
L.println("switch to normal tab behaviour");
L.repeat(8, "----+----|", 1);
L.clearTabs(); // just to show diff
}
L.tab();
L.print(measurement++);
L.tab();
L.print(millis());
L.tab();
L.print(random(12345));
L.tab();
L.print(analogRead(A0));
L.tab();
L.print(analogRead(A1));
L.tab();
L.print(analogRead(A2));
L.tab();
L.print(analogRead(A3));
L.tab();
L.print(analogRead(A4));
L.tab();
L.print(analogRead(A5));
L.println();
// delay(random(100));
}
L.repeat(8, "----+----|", 1);
L.setAutoNewLine(0);
L.repeat(3, '\n');
}
void test_table_2(LineFormatter L)
{
L.println();
L.println(__FUNCTION__);
L.repeat(strlen(__FUNCTION__), "=", 2);
L.clearTabs();
L.addTab(3);
L.addTab(10);
L.addTab(20);
L.addTab(30);
L.addRelTab(6);
L.addRelTab(6);
L.addRelTab(10);
L.addRelTab(6);
L.addRelTab(6);
// L.println(L.getTabCount());
int measurement = 1;
L.println("\tIdx\tTime\tValue\tA0\tA1\tA2\tA3\tA4\tA5");
L.repeat(7, "----+----|", 1);
L.setAutoNewLine(5);
for (int i = 0; i < 20; i++)
{
L.tab();
L.print(measurement++);
if (i % 5 == 0)
{
L.tab();
L.print(millis());
L.tab();
L.print(random(12345));
}
else
{
L.tab(2);
}
L.tab();
L.print(analogRead(A0));
L.tab();
L.print(analogRead(A1));
L.tab();
L.print(analogRead(A2));
L.tab();
L.print(analogRead(A3));
L.tab();
L.print(analogRead(A4));
L.tab();
L.print(analogRead(A5));
L.println();
// delay(random(100));
}
L.repeat(7, "----+----|", 1);
L.setAutoNewLine(0);
L.repeat(3, '\n');
}
// -- END OF FILE --

View File

@ -0,0 +1,120 @@
//
// FILE: LineFormatter_SDcard.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: demo LineFormatter for SDcard
// DATE: 2020-05-23
// URL: https://github.com/RobTillaart/LineFormatter
//
#include <SPI.h>
#include <SD.h>
// SPI PINS
// MOSI 11
// MISO 12
// CLOCK 13
// CS 10
#define CS 10 // adjust this ChipSelect line if needed !
#include <LineFormatter.h>
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
// initialize the SD card
if (!SD.begin(CS))
{
Serial.println("Error: SD card failure");
while (1);
}
// remove file for proper timing
SD.remove("data.txt");
delay(1000);
File logfile = SD.open("data.txt", FILE_WRITE);
if (!logfile)
{
Serial.println("Error: SD card failure");
while (1);
}
LineFormatter LF(&logfile);
test_table(&LF);
logfile.close();
Serial.println("\nDone...");
}
void loop()
{
}
void test_table(LineFormatter L)
{
L.println();
L.println(__FUNCTION__);
L.repeat(strlen(__FUNCTION__), "=", 2);
L.clearTabs();
L.addTab(3);
L.addTab(10);
L.addTab(20);
L.addTab(30);
L.addRelTab(6);
L.addRelTab(6);
L.addRelTab(10);
L.addRelTab(6);
L.addRelTab(6);
// L.println(L.getTabCount());
int measurement = 1;
L.println("\tIdx\tTime\tValue\tA0\tA1\tA2\tA3\tA4\tA5");
L.repeat(7, "----+----|", 1);
L.setAutoNewLine(5);
for (int i = 0; i < 100; i++)
{
Serial.print('.');
if (i && (i % 20 == 0)) Serial.println();
L.tab();
L.print(measurement++);
if (i % 5 == 0)
{
L.tab();
L.print(millis());
L.tab();
L.print(random(12345));
}
else
{
L.tab(2);
}
L.tab();
L.print(analogRead(A0));
L.tab();
L.print(analogRead(A1));
L.tab();
L.print(analogRead(A2));
L.tab();
L.print(analogRead(A3));
L.tab();
L.print(analogRead(A4));
L.tab();
L.print(analogRead(A5));
L.println();
}
L.repeat(7, "----+----|", 1);
L.setAutoNewLine(0);
L.repeat(3, '\n');
}
// -- END OF FILE --

View File

@ -49,11 +49,11 @@ void test_ruler()
L.addTab(10);
L.addTab(12);
L.addTab(30);
L.addRelTab(5);
L.addRelTab(5);
L.addRelTab(6);
L.addRelTab(6);
L.addRelTab(10);
L.addRelTab(5);
L.addRelTab(5);
L.addRelTab(6);
L.addRelTab(6);
L.printRuler(100);
L.clearTabs();
@ -73,16 +73,16 @@ void test_table_1()
L.addTab(10);
L.addTab(20);
L.addTab(30);
L.addRelTab(5);
L.addRelTab(5);
L.addRelTab(6);
L.addRelTab(6);
L.addRelTab(10);
L.addRelTab(5);
L.addRelTab(5);
L.addRelTab(6);
L.addRelTab(6);
int measurement = 1;
L.println("\tIdx\tTime\tValue\tA0\tA1\tA2\tA3\tA4\tA5");
L.repeat(7, "----+----|", 1);
L.repeat(8, "----+----|", 1);
L.setAutoNewLine(5);
for (int i = 0; i < 20; i++)
@ -130,17 +130,17 @@ void test_table_2()
L.addTab(10);
L.addTab(20);
L.addTab(30);
L.addRelTab(5);
L.addRelTab(5);
L.addRelTab(6);
L.addRelTab(6);
L.addRelTab(10);
L.addRelTab(5);
L.addRelTab(5);
L.addRelTab(6);
L.addRelTab(6);
// L.println(L.getTabCount());
int measurement = 1;
L.println("\tIdx\tTime\tValue\tA0\tA1\tA2\tA3\tA4\tA5");
L.repeat(7, "----+----|", 1);
L.repeat(8, "----+----|", 1);
L.setAutoNewLine(5);
for (int i = 0; i < 20; i++)